1 // SPDX-License-Identifier: GPL-2.0
3 // Freescale ASRC ALSA SoC Digital Audio Interface (DAI) driver
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
7 // Author: Nicolin Chen <nicoleotsuka@gmail.com>
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_data/dma-imx.h>
15 #include <linux/pm_runtime.h>
16 #include <sound/dmaengine_pcm.h>
17 #include <sound/pcm_params.h>
21 #define IDEAL_RATIO_DECIMAL_DEPTH 26
23 #define pair_err(fmt, ...) \
24 dev_err(&asrc->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
26 #define pair_dbg(fmt, ...) \
27 dev_dbg(&asrc->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
29 /* Corresponding to process_option */
30 static unsigned int supported_asrc_rate[] = {
31 5512, 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
32 64000, 88200, 96000, 128000, 176400, 192000,
35 static struct snd_pcm_hw_constraint_list fsl_asrc_rate_constraints = {
36 .count = ARRAY_SIZE(supported_asrc_rate),
37 .list = supported_asrc_rate,
41 * The following tables map the relationship between asrc_inclk/asrc_outclk in
42 * fsl_asrc.h and the registers of ASRCSR
44 static unsigned char input_clk_map_imx35[ASRC_CLK_MAP_LEN] = {
45 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
46 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
47 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
50 static unsigned char output_clk_map_imx35[ASRC_CLK_MAP_LEN] = {
51 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
52 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
53 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
56 /* i.MX53 uses the same map for input and output */
57 static unsigned char input_clk_map_imx53[ASRC_CLK_MAP_LEN] = {
58 /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
59 0x0, 0x1, 0x2, 0x7, 0x4, 0x5, 0x6, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xf, 0xe, 0xd,
60 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
61 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
64 static unsigned char output_clk_map_imx53[ASRC_CLK_MAP_LEN] = {
65 /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
66 0x8, 0x9, 0xa, 0x7, 0xc, 0x5, 0x6, 0xb, 0x0, 0x1, 0x2, 0x3, 0x4, 0xf, 0xe, 0xd,
67 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
68 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
72 * i.MX8QM/i.MX8QXP uses the same map for input and output.
73 * clk_map_imx8qm[0] is for i.MX8QM asrc0
74 * clk_map_imx8qm[1] is for i.MX8QM asrc1
75 * clk_map_imx8qxp[0] is for i.MX8QXP asrc0
76 * clk_map_imx8qxp[1] is for i.MX8QXP asrc1
78 static unsigned char clk_map_imx8qm[2][ASRC_CLK_MAP_LEN] = {
80 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
81 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
82 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
85 0xf, 0xf, 0xf, 0xf, 0xf, 0x7, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
86 0x0, 0x1, 0x2, 0x3, 0xb, 0xc, 0xf, 0xf, 0xd, 0xe, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
87 0x4, 0x5, 0x6, 0xf, 0x8, 0x9, 0xa, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
91 static unsigned char clk_map_imx8qxp[2][ASRC_CLK_MAP_LEN] = {
93 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
94 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0xf, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xf, 0xf,
95 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
98 0xf, 0xf, 0xf, 0xf, 0xf, 0x7, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
99 0x0, 0x1, 0x2, 0x3, 0x7, 0x8, 0xf, 0xf, 0x9, 0xa, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
100 0xf, 0xf, 0x6, 0xf, 0xf, 0xf, 0xa, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
105 * fsl_asrc_sel_proc - Select the pre-processing and post-processing options
106 * @inrate: input sample rate
107 * @outrate: output sample rate
108 * @pre_proc: return value for pre-processing option
109 * @post_proc: return value for post-processing option
111 * Make sure to exclude following unsupported cases before
112 * calling this function:
113 * 1) inrate > 8.125 * outrate
114 * 2) inrate > 16.125 * outrate
117 static void fsl_asrc_sel_proc(int inrate, int outrate,
118 int *pre_proc, int *post_proc)
120 bool post_proc_cond2;
121 bool post_proc_cond0;
123 /* select pre_proc between [0, 2] */
124 if (inrate * 8 > 33 * outrate)
126 else if (inrate * 8 > 15 * outrate) {
131 } else if (inrate < 76000)
133 else if (inrate > 152000)
138 /* Condition for selection of post-processing */
139 post_proc_cond2 = (inrate * 15 > outrate * 16 && outrate < 56000) ||
140 (inrate > 56000 && outrate < 56000);
141 post_proc_cond0 = inrate * 23 < outrate * 8;
145 else if (post_proc_cond0)
152 * fsl_asrc_request_pair - Request ASRC pair
153 * @channels: number of channels
154 * @pair: pointer to pair
156 * It assigns pair by the order of A->C->B because allocation of pair B,
157 * within range [ANCA, ANCA+ANCB-1], depends on the channels of pair A
158 * while pair A and pair C are comparatively independent.
160 static int fsl_asrc_request_pair(int channels, struct fsl_asrc_pair *pair)
162 enum asrc_pair_index index = ASRC_INVALID_PAIR;
163 struct fsl_asrc *asrc = pair->asrc;
164 struct device *dev = &asrc->pdev->dev;
165 unsigned long lock_flags;
168 spin_lock_irqsave(&asrc->lock, lock_flags);
170 for (i = ASRC_PAIR_A; i < ASRC_PAIR_MAX_NUM; i++) {
171 if (asrc->pair[i] != NULL)
176 if (i != ASRC_PAIR_B)
180 if (index == ASRC_INVALID_PAIR) {
181 dev_err(dev, "all pairs are busy now\n");
183 } else if (asrc->channel_avail < channels) {
184 dev_err(dev, "can't afford required channels: %d\n", channels);
187 asrc->channel_avail -= channels;
188 asrc->pair[index] = pair;
189 pair->channels = channels;
193 spin_unlock_irqrestore(&asrc->lock, lock_flags);
199 * fsl_asrc_release_pair - Release ASRC pair
200 * @pair: pair to release
202 * It clears the resource from asrc and releases the occupied channels.
204 static void fsl_asrc_release_pair(struct fsl_asrc_pair *pair)
206 struct fsl_asrc *asrc = pair->asrc;
207 enum asrc_pair_index index = pair->index;
208 unsigned long lock_flags;
210 /* Make sure the pair is disabled */
211 regmap_update_bits(asrc->regmap, REG_ASRCTR,
212 ASRCTR_ASRCEi_MASK(index), 0);
214 spin_lock_irqsave(&asrc->lock, lock_flags);
216 asrc->channel_avail += pair->channels;
217 asrc->pair[index] = NULL;
220 spin_unlock_irqrestore(&asrc->lock, lock_flags);
224 * fsl_asrc_set_watermarks- configure input and output thresholds
225 * @pair: pointer to pair
226 * @in: input threshold
227 * @out: output threshold
229 static void fsl_asrc_set_watermarks(struct fsl_asrc_pair *pair, u32 in, u32 out)
231 struct fsl_asrc *asrc = pair->asrc;
232 enum asrc_pair_index index = pair->index;
234 regmap_update_bits(asrc->regmap, REG_ASRMCR(index),
235 ASRMCRi_EXTTHRSHi_MASK |
236 ASRMCRi_INFIFO_THRESHOLD_MASK |
237 ASRMCRi_OUTFIFO_THRESHOLD_MASK,
239 ASRMCRi_INFIFO_THRESHOLD(in) |
240 ASRMCRi_OUTFIFO_THRESHOLD(out));
244 * fsl_asrc_cal_asrck_divisor - Calculate the total divisor between asrck clock rate and sample rate
245 * @pair: pointer to pair
248 * It follows the formula clk_rate = samplerate * (2 ^ prescaler) * divider
250 static u32 fsl_asrc_cal_asrck_divisor(struct fsl_asrc_pair *pair, u32 div)
254 /* Calculate the divisors: prescaler [2^0, 2^7], divder [1, 8] */
255 for (ps = 0; div > 8; ps++)
258 return ((div - 1) << ASRCDRi_AxCPi_WIDTH) | ps;
262 * fsl_asrc_set_ideal_ratio - Calculate and set the ratio for Ideal Ratio mode only
263 * @pair: pointer to pair
264 * @inrate: input rate
265 * @outrate: output rate
267 * The ratio is a 32-bit fixed point value with 26 fractional bits.
269 static int fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair *pair,
270 int inrate, int outrate)
272 struct fsl_asrc *asrc = pair->asrc;
273 enum asrc_pair_index index = pair->index;
278 pair_err("output rate should not be zero\n");
282 /* Calculate the intergal part of the ratio */
283 ratio = (inrate / outrate) << IDEAL_RATIO_DECIMAL_DEPTH;
285 /* ... and then the 26 depth decimal part */
288 for (i = 1; i <= IDEAL_RATIO_DECIMAL_DEPTH; i++) {
291 if (inrate < outrate)
294 ratio |= 1 << (IDEAL_RATIO_DECIMAL_DEPTH - i);
301 regmap_write(asrc->regmap, REG_ASRIDRL(index), ratio);
302 regmap_write(asrc->regmap, REG_ASRIDRH(index), ratio >> 24);
308 * fsl_asrc_config_pair - Configure the assigned ASRC pair
309 * @pair: pointer to pair
310 * @use_ideal_rate: boolean configuration
312 * It configures those ASRC registers according to a configuration instance
313 * of struct asrc_config which includes in/output sample rate, width, channel
314 * and clock settings.
317 * The ideal ratio configuration can work with a flexible clock rate setting.
318 * Using IDEAL_RATIO_RATE gives a faster converting speed but overloads ASRC.
319 * For a regular audio playback, the clock rate should not be slower than an
320 * clock rate aligning with the output sample rate; For a use case requiring
321 * faster conversion, set use_ideal_rate to have the faster speed.
323 static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair, bool use_ideal_rate)
325 struct fsl_asrc_pair_priv *pair_priv = pair->private;
326 struct asrc_config *config = pair_priv->config;
327 struct fsl_asrc *asrc = pair->asrc;
328 struct fsl_asrc_priv *asrc_priv = asrc->private;
329 enum asrc_pair_index index = pair->index;
330 enum asrc_word_width input_word_width;
331 enum asrc_word_width output_word_width;
332 u32 inrate, outrate, indiv, outdiv;
333 u32 clk_index[2], div[2], rem[2];
335 int in, out, channels;
336 int pre_proc, post_proc;
341 pair_err("invalid pair config\n");
345 /* Validate channels */
346 if (config->channel_num < 1 || config->channel_num > 10) {
347 pair_err("does not support %d channels\n", config->channel_num);
351 switch (snd_pcm_format_width(config->input_format)) {
353 input_word_width = ASRC_WIDTH_8_BIT;
356 input_word_width = ASRC_WIDTH_16_BIT;
359 input_word_width = ASRC_WIDTH_24_BIT;
362 pair_err("does not support this input format, %d\n",
363 config->input_format);
367 switch (snd_pcm_format_width(config->output_format)) {
369 output_word_width = ASRC_WIDTH_16_BIT;
372 output_word_width = ASRC_WIDTH_24_BIT;
375 pair_err("does not support this output format, %d\n",
376 config->output_format);
380 inrate = config->input_sample_rate;
381 outrate = config->output_sample_rate;
382 ideal = config->inclk == INCLK_NONE;
384 /* Validate input and output sample rates */
385 for (in = 0; in < ARRAY_SIZE(supported_asrc_rate); in++)
386 if (inrate == supported_asrc_rate[in])
389 if (in == ARRAY_SIZE(supported_asrc_rate)) {
390 pair_err("unsupported input sample rate: %dHz\n", inrate);
394 for (out = 0; out < ARRAY_SIZE(supported_asrc_rate); out++)
395 if (outrate == supported_asrc_rate[out])
398 if (out == ARRAY_SIZE(supported_asrc_rate)) {
399 pair_err("unsupported output sample rate: %dHz\n", outrate);
403 if ((outrate >= 5512 && outrate <= 30000) &&
404 (outrate > 24 * inrate || inrate > 8 * outrate)) {
405 pair_err("exceed supported ratio range [1/24, 8] for \
406 inrate/outrate: %d/%d\n", inrate, outrate);
410 /* Validate input and output clock sources */
411 clk_index[IN] = asrc_priv->clk_map[IN][config->inclk];
412 clk_index[OUT] = asrc_priv->clk_map[OUT][config->outclk];
414 /* We only have output clock for ideal ratio mode */
415 clk = asrc_priv->asrck_clk[clk_index[ideal ? OUT : IN]];
417 clk_rate = clk_get_rate(clk);
418 rem[IN] = do_div(clk_rate, inrate);
419 div[IN] = (u32)clk_rate;
422 * The divider range is [1, 1024], defined by the hardware. For non-
423 * ideal ratio configuration, clock rate has to be strictly aligned
424 * with the sample rate. For ideal ratio configuration, clock rates
425 * only result in different converting speeds. So remainder does not
426 * matter, as long as we keep the divider within its valid range.
428 if (div[IN] == 0 || (!ideal && (div[IN] > 1024 || rem[IN] != 0))) {
429 pair_err("failed to support input sample rate %dHz by asrck_%x\n",
430 inrate, clk_index[ideal ? OUT : IN]);
434 div[IN] = min_t(u32, 1024, div[IN]);
436 clk = asrc_priv->asrck_clk[clk_index[OUT]];
437 clk_rate = clk_get_rate(clk);
438 if (ideal && use_ideal_rate)
439 rem[OUT] = do_div(clk_rate, IDEAL_RATIO_RATE);
441 rem[OUT] = do_div(clk_rate, outrate);
444 /* Output divider has the same limitation as the input one */
445 if (div[OUT] == 0 || (!ideal && (div[OUT] > 1024 || rem[OUT] != 0))) {
446 pair_err("failed to support output sample rate %dHz by asrck_%x\n",
447 outrate, clk_index[OUT]);
451 div[OUT] = min_t(u32, 1024, div[OUT]);
453 /* Set the channel number */
454 channels = config->channel_num;
456 if (asrc_priv->soc->channel_bits < 4)
459 /* Update channels for current pair */
460 regmap_update_bits(asrc->regmap, REG_ASRCNCR,
461 ASRCNCR_ANCi_MASK(index, asrc_priv->soc->channel_bits),
462 ASRCNCR_ANCi(index, channels, asrc_priv->soc->channel_bits));
464 /* Default setting: Automatic selection for processing mode */
465 regmap_update_bits(asrc->regmap, REG_ASRCTR,
466 ASRCTR_ATSi_MASK(index), ASRCTR_ATS(index));
467 regmap_update_bits(asrc->regmap, REG_ASRCTR,
468 ASRCTR_USRi_MASK(index), 0);
470 /* Set the input and output clock sources */
471 regmap_update_bits(asrc->regmap, REG_ASRCSR,
472 ASRCSR_AICSi_MASK(index) | ASRCSR_AOCSi_MASK(index),
473 ASRCSR_AICS(index, clk_index[IN]) |
474 ASRCSR_AOCS(index, clk_index[OUT]));
476 /* Calculate the input clock divisors */
477 indiv = fsl_asrc_cal_asrck_divisor(pair, div[IN]);
478 outdiv = fsl_asrc_cal_asrck_divisor(pair, div[OUT]);
480 /* Suppose indiv and outdiv includes prescaler, so add its MASK too */
481 regmap_update_bits(asrc->regmap, REG_ASRCDR(index),
482 ASRCDRi_AOCPi_MASK(index) | ASRCDRi_AICPi_MASK(index) |
483 ASRCDRi_AOCDi_MASK(index) | ASRCDRi_AICDi_MASK(index),
484 ASRCDRi_AOCP(index, outdiv) | ASRCDRi_AICP(index, indiv));
486 /* Implement word_width configurations */
487 regmap_update_bits(asrc->regmap, REG_ASRMCR1(index),
488 ASRMCR1i_OW16_MASK | ASRMCR1i_IWD_MASK,
489 ASRMCR1i_OW16(output_word_width) |
490 ASRMCR1i_IWD(input_word_width));
492 /* Enable BUFFER STALL */
493 regmap_update_bits(asrc->regmap, REG_ASRMCR(index),
494 ASRMCRi_BUFSTALLi_MASK, ASRMCRi_BUFSTALLi);
496 /* Set default thresholds for input and output FIFO */
497 fsl_asrc_set_watermarks(pair, ASRC_INPUTFIFO_THRESHOLD,
498 ASRC_INPUTFIFO_THRESHOLD);
500 /* Configure the following only for Ideal Ratio mode */
504 /* Clear ASTSx bit to use Ideal Ratio mode */
505 regmap_update_bits(asrc->regmap, REG_ASRCTR,
506 ASRCTR_ATSi_MASK(index), 0);
508 /* Enable Ideal Ratio mode */
509 regmap_update_bits(asrc->regmap, REG_ASRCTR,
510 ASRCTR_IDRi_MASK(index) | ASRCTR_USRi_MASK(index),
511 ASRCTR_IDR(index) | ASRCTR_USR(index));
513 fsl_asrc_sel_proc(inrate, outrate, &pre_proc, &post_proc);
515 /* Apply configurations for pre- and post-processing */
516 regmap_update_bits(asrc->regmap, REG_ASRCFG,
517 ASRCFG_PREMODi_MASK(index) | ASRCFG_POSTMODi_MASK(index),
518 ASRCFG_PREMOD(index, pre_proc) |
519 ASRCFG_POSTMOD(index, post_proc));
521 return fsl_asrc_set_ideal_ratio(pair, inrate, outrate);
525 * fsl_asrc_start_pair - Start the assigned ASRC pair
526 * @pair: pointer to pair
528 * It enables the assigned pair and makes it stopped at the stall level.
530 static void fsl_asrc_start_pair(struct fsl_asrc_pair *pair)
532 struct fsl_asrc *asrc = pair->asrc;
533 enum asrc_pair_index index = pair->index;
534 int reg, retry = 10, i;
536 /* Enable the current pair */
537 regmap_update_bits(asrc->regmap, REG_ASRCTR,
538 ASRCTR_ASRCEi_MASK(index), ASRCTR_ASRCE(index));
540 /* Wait for status of initialization */
543 regmap_read(asrc->regmap, REG_ASRCFG, ®);
544 reg &= ASRCFG_INIRQi_MASK(index);
545 } while (!reg && --retry);
547 /* Make the input fifo to ASRC STALL level */
548 regmap_read(asrc->regmap, REG_ASRCNCR, ®);
549 for (i = 0; i < pair->channels * 4; i++)
550 regmap_write(asrc->regmap, REG_ASRDI(index), 0);
552 /* Enable overload interrupt */
553 regmap_write(asrc->regmap, REG_ASRIER, ASRIER_AOLIE);
557 * fsl_asrc_stop_pair - Stop the assigned ASRC pair
558 * @pair: pointer to pair
560 static void fsl_asrc_stop_pair(struct fsl_asrc_pair *pair)
562 struct fsl_asrc *asrc = pair->asrc;
563 enum asrc_pair_index index = pair->index;
565 /* Stop the current pair */
566 regmap_update_bits(asrc->regmap, REG_ASRCTR,
567 ASRCTR_ASRCEi_MASK(index), 0);
571 * fsl_asrc_get_dma_channel- Get DMA channel according to the pair and direction.
572 * @pair: pointer to pair
573 * @dir: DMA direction
575 static struct dma_chan *fsl_asrc_get_dma_channel(struct fsl_asrc_pair *pair,
578 struct fsl_asrc *asrc = pair->asrc;
579 enum asrc_pair_index index = pair->index;
582 sprintf(name, "%cx%c", dir == IN ? 'r' : 't', index + 'a');
584 return dma_request_slave_channel(&asrc->pdev->dev, name);
587 static int fsl_asrc_dai_startup(struct snd_pcm_substream *substream,
588 struct snd_soc_dai *dai)
590 struct fsl_asrc *asrc = snd_soc_dai_get_drvdata(dai);
591 struct fsl_asrc_priv *asrc_priv = asrc->private;
593 /* Odd channel number is not valid for older ASRC (channel_bits==3) */
594 if (asrc_priv->soc->channel_bits == 3)
595 snd_pcm_hw_constraint_step(substream->runtime, 0,
596 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
599 return snd_pcm_hw_constraint_list(substream->runtime, 0,
600 SNDRV_PCM_HW_PARAM_RATE, &fsl_asrc_rate_constraints);
603 /* Select proper clock source for internal ratio mode */
604 static void fsl_asrc_select_clk(struct fsl_asrc_priv *asrc_priv,
605 struct fsl_asrc_pair *pair,
609 struct fsl_asrc_pair_priv *pair_priv = pair->private;
610 struct asrc_config *config = pair_priv->config;
611 int rate[2], select_clk[2]; /* Array size 2 means IN and OUT */
612 int clk_rate, clk_index;
616 rate[OUT] = out_rate;
618 /* Select proper clock source for internal ratio mode */
619 for (j = 0; j < 2; j++) {
620 for (i = 0; i < ASRC_CLK_MAP_LEN; i++) {
621 clk_index = asrc_priv->clk_map[j][i];
622 clk_rate = clk_get_rate(asrc_priv->asrck_clk[clk_index]);
623 /* Only match a perfect clock source with no remainder */
624 if (clk_rate != 0 && (clk_rate / rate[j]) <= 1024 &&
625 (clk_rate % rate[j]) == 0)
632 /* Switch to ideal ratio mode if there is no proper clock source */
633 if (select_clk[IN] == ASRC_CLK_MAP_LEN || select_clk[OUT] == ASRC_CLK_MAP_LEN) {
634 select_clk[IN] = INCLK_NONE;
635 select_clk[OUT] = OUTCLK_ASRCK1_CLK;
638 config->inclk = select_clk[IN];
639 config->outclk = select_clk[OUT];
642 static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
643 struct snd_pcm_hw_params *params,
644 struct snd_soc_dai *dai)
646 struct fsl_asrc *asrc = snd_soc_dai_get_drvdata(dai);
647 struct fsl_asrc_priv *asrc_priv = asrc->private;
648 struct snd_pcm_runtime *runtime = substream->runtime;
649 struct fsl_asrc_pair *pair = runtime->private_data;
650 struct fsl_asrc_pair_priv *pair_priv = pair->private;
651 unsigned int channels = params_channels(params);
652 unsigned int rate = params_rate(params);
653 struct asrc_config config;
656 ret = fsl_asrc_request_pair(channels, pair);
658 dev_err(dai->dev, "fail to request asrc pair\n");
662 pair_priv->config = &config;
664 config.pair = pair->index;
665 config.channel_num = channels;
667 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
668 config.input_format = params_format(params);
669 config.output_format = asrc->asrc_format;
670 config.input_sample_rate = rate;
671 config.output_sample_rate = asrc->asrc_rate;
673 config.input_format = asrc->asrc_format;
674 config.output_format = params_format(params);
675 config.input_sample_rate = asrc->asrc_rate;
676 config.output_sample_rate = rate;
679 fsl_asrc_select_clk(asrc_priv, pair,
680 config.input_sample_rate,
681 config.output_sample_rate);
683 ret = fsl_asrc_config_pair(pair, false);
685 dev_err(dai->dev, "fail to config asrc pair\n");
692 static int fsl_asrc_dai_hw_free(struct snd_pcm_substream *substream,
693 struct snd_soc_dai *dai)
695 struct snd_pcm_runtime *runtime = substream->runtime;
696 struct fsl_asrc_pair *pair = runtime->private_data;
699 fsl_asrc_release_pair(pair);
704 static int fsl_asrc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
705 struct snd_soc_dai *dai)
707 struct snd_pcm_runtime *runtime = substream->runtime;
708 struct fsl_asrc_pair *pair = runtime->private_data;
711 case SNDRV_PCM_TRIGGER_START:
712 case SNDRV_PCM_TRIGGER_RESUME:
713 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
714 fsl_asrc_start_pair(pair);
716 case SNDRV_PCM_TRIGGER_STOP:
717 case SNDRV_PCM_TRIGGER_SUSPEND:
718 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
719 fsl_asrc_stop_pair(pair);
728 static const struct snd_soc_dai_ops fsl_asrc_dai_ops = {
729 .startup = fsl_asrc_dai_startup,
730 .hw_params = fsl_asrc_dai_hw_params,
731 .hw_free = fsl_asrc_dai_hw_free,
732 .trigger = fsl_asrc_dai_trigger,
735 static int fsl_asrc_dai_probe(struct snd_soc_dai *dai)
737 struct fsl_asrc *asrc = snd_soc_dai_get_drvdata(dai);
739 snd_soc_dai_init_dma_data(dai, &asrc->dma_params_tx,
740 &asrc->dma_params_rx);
745 #define FSL_ASRC_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | \
746 SNDRV_PCM_FMTBIT_S16_LE | \
747 SNDRV_PCM_FMTBIT_S24_3LE)
749 static struct snd_soc_dai_driver fsl_asrc_dai = {
750 .probe = fsl_asrc_dai_probe,
752 .stream_name = "ASRC-Playback",
757 .rates = SNDRV_PCM_RATE_KNOT,
758 .formats = FSL_ASRC_FORMATS |
762 .stream_name = "ASRC-Capture",
767 .rates = SNDRV_PCM_RATE_KNOT,
768 .formats = FSL_ASRC_FORMATS,
770 .ops = &fsl_asrc_dai_ops,
773 static bool fsl_asrc_readable_reg(struct device *dev, unsigned int reg)
817 static bool fsl_asrc_volatile_reg(struct device *dev, unsigned int reg)
837 static bool fsl_asrc_writeable_reg(struct device *dev, unsigned int reg)
878 static struct reg_default fsl_asrc_reg[] = {
879 { REG_ASRCTR, 0x0000 }, { REG_ASRIER, 0x0000 },
880 { REG_ASRCNCR, 0x0000 }, { REG_ASRCFG, 0x0000 },
881 { REG_ASRCSR, 0x0000 }, { REG_ASRCDR1, 0x0000 },
882 { REG_ASRCDR2, 0x0000 }, { REG_ASRSTR, 0x0000 },
883 { REG_ASRRA, 0x0000 }, { REG_ASRRB, 0x0000 },
884 { REG_ASRRC, 0x0000 }, { REG_ASRPM1, 0x0000 },
885 { REG_ASRPM2, 0x0000 }, { REG_ASRPM3, 0x0000 },
886 { REG_ASRPM4, 0x0000 }, { REG_ASRPM5, 0x0000 },
887 { REG_ASRTFR1, 0x0000 }, { REG_ASRCCR, 0x0000 },
888 { REG_ASRDIA, 0x0000 }, { REG_ASRDOA, 0x0000 },
889 { REG_ASRDIB, 0x0000 }, { REG_ASRDOB, 0x0000 },
890 { REG_ASRDIC, 0x0000 }, { REG_ASRDOC, 0x0000 },
891 { REG_ASRIDRHA, 0x0000 }, { REG_ASRIDRLA, 0x0000 },
892 { REG_ASRIDRHB, 0x0000 }, { REG_ASRIDRLB, 0x0000 },
893 { REG_ASRIDRHC, 0x0000 }, { REG_ASRIDRLC, 0x0000 },
894 { REG_ASR76K, 0x0A47 }, { REG_ASR56K, 0x0DF3 },
895 { REG_ASRMCRA, 0x0000 }, { REG_ASRFSTA, 0x0000 },
896 { REG_ASRMCRB, 0x0000 }, { REG_ASRFSTB, 0x0000 },
897 { REG_ASRMCRC, 0x0000 }, { REG_ASRFSTC, 0x0000 },
898 { REG_ASRMCR1A, 0x0000 }, { REG_ASRMCR1B, 0x0000 },
899 { REG_ASRMCR1C, 0x0000 },
902 static const struct regmap_config fsl_asrc_regmap_config = {
907 .max_register = REG_ASRMCR1C,
908 .reg_defaults = fsl_asrc_reg,
909 .num_reg_defaults = ARRAY_SIZE(fsl_asrc_reg),
910 .readable_reg = fsl_asrc_readable_reg,
911 .volatile_reg = fsl_asrc_volatile_reg,
912 .writeable_reg = fsl_asrc_writeable_reg,
913 .cache_type = REGCACHE_FLAT,
917 * fsl_asrc_init - Initialize ASRC registers with a default configuration
918 * @asrc: ASRC context
920 static int fsl_asrc_init(struct fsl_asrc *asrc)
922 unsigned long ipg_rate;
924 /* Halt ASRC internal FP when input FIFO needs data for pair A, B, C */
925 regmap_write(asrc->regmap, REG_ASRCTR, ASRCTR_ASRCEN);
927 /* Disable interrupt by default */
928 regmap_write(asrc->regmap, REG_ASRIER, 0x0);
930 /* Apply recommended settings for parameters from Reference Manual */
931 regmap_write(asrc->regmap, REG_ASRPM1, 0x7fffff);
932 regmap_write(asrc->regmap, REG_ASRPM2, 0x255555);
933 regmap_write(asrc->regmap, REG_ASRPM3, 0xff7280);
934 regmap_write(asrc->regmap, REG_ASRPM4, 0xff7280);
935 regmap_write(asrc->regmap, REG_ASRPM5, 0xff7280);
937 /* Base address for task queue FIFO. Set to 0x7C */
938 regmap_update_bits(asrc->regmap, REG_ASRTFR1,
939 ASRTFR1_TF_BASE_MASK, ASRTFR1_TF_BASE(0xfc));
942 * Set the period of the 76KHz and 56KHz sampling clocks based on
943 * the ASRC processing clock.
944 * On iMX6, ipg_clk = 133MHz, REG_ASR76K = 0x06D6, REG_ASR56K = 0x0947
946 ipg_rate = clk_get_rate(asrc->ipg_clk);
947 regmap_write(asrc->regmap, REG_ASR76K, ipg_rate / 76000);
948 return regmap_write(asrc->regmap, REG_ASR56K, ipg_rate / 56000);
952 * fsl_asrc_isr- Interrupt handler for ASRC
954 * @dev_id: ASRC context
956 static irqreturn_t fsl_asrc_isr(int irq, void *dev_id)
958 struct fsl_asrc *asrc = (struct fsl_asrc *)dev_id;
959 struct device *dev = &asrc->pdev->dev;
960 enum asrc_pair_index index;
963 regmap_read(asrc->regmap, REG_ASRSTR, &status);
965 /* Clean overload error */
966 regmap_write(asrc->regmap, REG_ASRSTR, ASRSTR_AOLE);
969 * We here use dev_dbg() for all exceptions because ASRC itself does
970 * not care if FIFO overflowed or underrun while a warning in the
971 * interrupt would result a ridged conversion.
973 for (index = ASRC_PAIR_A; index < ASRC_PAIR_MAX_NUM; index++) {
974 if (!asrc->pair[index])
977 if (status & ASRSTR_ATQOL) {
978 asrc->pair[index]->error |= ASRC_TASK_Q_OVERLOAD;
979 dev_dbg(dev, "ASRC Task Queue FIFO overload\n");
982 if (status & ASRSTR_AOOL(index)) {
983 asrc->pair[index]->error |= ASRC_OUTPUT_TASK_OVERLOAD;
984 pair_dbg("Output Task Overload\n");
987 if (status & ASRSTR_AIOL(index)) {
988 asrc->pair[index]->error |= ASRC_INPUT_TASK_OVERLOAD;
989 pair_dbg("Input Task Overload\n");
992 if (status & ASRSTR_AODO(index)) {
993 asrc->pair[index]->error |= ASRC_OUTPUT_BUFFER_OVERFLOW;
994 pair_dbg("Output Data Buffer has overflowed\n");
997 if (status & ASRSTR_AIDU(index)) {
998 asrc->pair[index]->error |= ASRC_INPUT_BUFFER_UNDERRUN;
999 pair_dbg("Input Data Buffer has underflowed\n");
1006 static int fsl_asrc_get_fifo_addr(u8 dir, enum asrc_pair_index index)
1008 return REG_ASRDx(dir, index);
1011 static int fsl_asrc_probe(struct platform_device *pdev)
1013 struct device_node *np = pdev->dev.of_node;
1014 struct fsl_asrc_priv *asrc_priv;
1015 struct fsl_asrc *asrc;
1016 struct resource *res;
1023 asrc = devm_kzalloc(&pdev->dev, sizeof(*asrc), GFP_KERNEL);
1027 asrc_priv = devm_kzalloc(&pdev->dev, sizeof(*asrc_priv), GFP_KERNEL);
1032 asrc->private = asrc_priv;
1034 /* Get the addresses and IRQ */
1035 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1036 regs = devm_ioremap_resource(&pdev->dev, res);
1038 return PTR_ERR(regs);
1040 asrc->paddr = res->start;
1042 asrc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "mem", regs,
1043 &fsl_asrc_regmap_config);
1044 if (IS_ERR(asrc->regmap)) {
1045 dev_err(&pdev->dev, "failed to init regmap\n");
1046 return PTR_ERR(asrc->regmap);
1049 irq = platform_get_irq(pdev, 0);
1053 ret = devm_request_irq(&pdev->dev, irq, fsl_asrc_isr, 0,
1054 dev_name(&pdev->dev), asrc);
1056 dev_err(&pdev->dev, "failed to claim irq %u: %d\n", irq, ret);
1060 asrc->mem_clk = devm_clk_get(&pdev->dev, "mem");
1061 if (IS_ERR(asrc->mem_clk)) {
1062 dev_err(&pdev->dev, "failed to get mem clock\n");
1063 return PTR_ERR(asrc->mem_clk);
1066 asrc->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
1067 if (IS_ERR(asrc->ipg_clk)) {
1068 dev_err(&pdev->dev, "failed to get ipg clock\n");
1069 return PTR_ERR(asrc->ipg_clk);
1072 asrc->spba_clk = devm_clk_get(&pdev->dev, "spba");
1073 if (IS_ERR(asrc->spba_clk))
1074 dev_warn(&pdev->dev, "failed to get spba clock\n");
1076 for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
1077 sprintf(tmp, "asrck_%x", i);
1078 asrc_priv->asrck_clk[i] = devm_clk_get(&pdev->dev, tmp);
1079 if (IS_ERR(asrc_priv->asrck_clk[i])) {
1080 dev_err(&pdev->dev, "failed to get %s clock\n", tmp);
1081 return PTR_ERR(asrc_priv->asrck_clk[i]);
1085 asrc_priv->soc = of_device_get_match_data(&pdev->dev);
1086 if (!asrc_priv->soc) {
1087 dev_err(&pdev->dev, "failed to get soc data\n");
1091 asrc->use_edma = asrc_priv->soc->use_edma;
1092 asrc->get_dma_channel = fsl_asrc_get_dma_channel;
1093 asrc->request_pair = fsl_asrc_request_pair;
1094 asrc->release_pair = fsl_asrc_release_pair;
1095 asrc->get_fifo_addr = fsl_asrc_get_fifo_addr;
1096 asrc->pair_priv_size = sizeof(struct fsl_asrc_pair_priv);
1098 if (of_device_is_compatible(np, "fsl,imx35-asrc")) {
1099 asrc_priv->clk_map[IN] = input_clk_map_imx35;
1100 asrc_priv->clk_map[OUT] = output_clk_map_imx35;
1101 } else if (of_device_is_compatible(np, "fsl,imx53-asrc")) {
1102 asrc_priv->clk_map[IN] = input_clk_map_imx53;
1103 asrc_priv->clk_map[OUT] = output_clk_map_imx53;
1104 } else if (of_device_is_compatible(np, "fsl,imx8qm-asrc") ||
1105 of_device_is_compatible(np, "fsl,imx8qxp-asrc")) {
1106 ret = of_property_read_u32(np, "fsl,asrc-clk-map", &map_idx);
1108 dev_err(&pdev->dev, "failed to get clk map index\n");
1113 dev_err(&pdev->dev, "unsupported clk map index\n");
1116 if (of_device_is_compatible(np, "fsl,imx8qm-asrc")) {
1117 asrc_priv->clk_map[IN] = clk_map_imx8qm[map_idx];
1118 asrc_priv->clk_map[OUT] = clk_map_imx8qm[map_idx];
1120 asrc_priv->clk_map[IN] = clk_map_imx8qxp[map_idx];
1121 asrc_priv->clk_map[OUT] = clk_map_imx8qxp[map_idx];
1125 ret = fsl_asrc_init(asrc);
1127 dev_err(&pdev->dev, "failed to init asrc %d\n", ret);
1131 asrc->channel_avail = 10;
1133 ret = of_property_read_u32(np, "fsl,asrc-rate",
1136 dev_err(&pdev->dev, "failed to get output rate\n");
1140 ret = of_property_read_u32(np, "fsl,asrc-format", &asrc->asrc_format);
1142 ret = of_property_read_u32(np, "fsl,asrc-width", &width);
1144 dev_err(&pdev->dev, "failed to decide output format\n");
1150 asrc->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
1153 asrc->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1156 dev_warn(&pdev->dev,
1157 "unsupported width, use default S24_LE\n");
1158 asrc->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1163 if (!(FSL_ASRC_FORMATS & (1ULL << asrc->asrc_format))) {
1164 dev_warn(&pdev->dev, "unsupported width, use default S24_LE\n");
1165 asrc->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1168 platform_set_drvdata(pdev, asrc);
1169 pm_runtime_enable(&pdev->dev);
1170 spin_lock_init(&asrc->lock);
1171 regcache_cache_only(asrc->regmap, true);
1173 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_asrc_component,
1176 dev_err(&pdev->dev, "failed to register ASoC DAI\n");
1184 static int fsl_asrc_runtime_resume(struct device *dev)
1186 struct fsl_asrc *asrc = dev_get_drvdata(dev);
1187 struct fsl_asrc_priv *asrc_priv = asrc->private;
1191 ret = clk_prepare_enable(asrc->mem_clk);
1194 ret = clk_prepare_enable(asrc->ipg_clk);
1196 goto disable_mem_clk;
1197 if (!IS_ERR(asrc->spba_clk)) {
1198 ret = clk_prepare_enable(asrc->spba_clk);
1200 goto disable_ipg_clk;
1202 for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
1203 ret = clk_prepare_enable(asrc_priv->asrck_clk[i]);
1205 goto disable_asrck_clk;
1208 /* Stop all pairs provisionally */
1209 regmap_read(asrc->regmap, REG_ASRCTR, &asrctr);
1210 regmap_update_bits(asrc->regmap, REG_ASRCTR,
1211 ASRCTR_ASRCEi_ALL_MASK, 0);
1213 /* Restore all registers */
1214 regcache_cache_only(asrc->regmap, false);
1215 regcache_mark_dirty(asrc->regmap);
1216 regcache_sync(asrc->regmap);
1218 regmap_update_bits(asrc->regmap, REG_ASRCFG,
1219 ASRCFG_NDPRi_ALL_MASK | ASRCFG_POSTMODi_ALL_MASK |
1220 ASRCFG_PREMODi_ALL_MASK, asrc_priv->regcache_cfg);
1222 /* Restart enabled pairs */
1223 regmap_update_bits(asrc->regmap, REG_ASRCTR,
1224 ASRCTR_ASRCEi_ALL_MASK, asrctr);
1229 for (i--; i >= 0; i--)
1230 clk_disable_unprepare(asrc_priv->asrck_clk[i]);
1231 if (!IS_ERR(asrc->spba_clk))
1232 clk_disable_unprepare(asrc->spba_clk);
1234 clk_disable_unprepare(asrc->ipg_clk);
1236 clk_disable_unprepare(asrc->mem_clk);
1240 static int fsl_asrc_runtime_suspend(struct device *dev)
1242 struct fsl_asrc *asrc = dev_get_drvdata(dev);
1243 struct fsl_asrc_priv *asrc_priv = asrc->private;
1246 regmap_read(asrc->regmap, REG_ASRCFG,
1247 &asrc_priv->regcache_cfg);
1249 regcache_cache_only(asrc->regmap, true);
1251 for (i = 0; i < ASRC_CLK_MAX_NUM; i++)
1252 clk_disable_unprepare(asrc_priv->asrck_clk[i]);
1253 if (!IS_ERR(asrc->spba_clk))
1254 clk_disable_unprepare(asrc->spba_clk);
1255 clk_disable_unprepare(asrc->ipg_clk);
1256 clk_disable_unprepare(asrc->mem_clk);
1260 #endif /* CONFIG_PM */
1262 static const struct dev_pm_ops fsl_asrc_pm = {
1263 SET_RUNTIME_PM_OPS(fsl_asrc_runtime_suspend, fsl_asrc_runtime_resume, NULL)
1264 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1265 pm_runtime_force_resume)
1268 static const struct fsl_asrc_soc_data fsl_asrc_imx35_data = {
1273 static const struct fsl_asrc_soc_data fsl_asrc_imx53_data = {
1278 static const struct fsl_asrc_soc_data fsl_asrc_imx8qm_data = {
1283 static const struct fsl_asrc_soc_data fsl_asrc_imx8qxp_data = {
1288 static const struct of_device_id fsl_asrc_ids[] = {
1289 { .compatible = "fsl,imx35-asrc", .data = &fsl_asrc_imx35_data },
1290 { .compatible = "fsl,imx53-asrc", .data = &fsl_asrc_imx53_data },
1291 { .compatible = "fsl,imx8qm-asrc", .data = &fsl_asrc_imx8qm_data },
1292 { .compatible = "fsl,imx8qxp-asrc", .data = &fsl_asrc_imx8qxp_data },
1295 MODULE_DEVICE_TABLE(of, fsl_asrc_ids);
1297 static struct platform_driver fsl_asrc_driver = {
1298 .probe = fsl_asrc_probe,
1301 .of_match_table = fsl_asrc_ids,
1305 module_platform_driver(fsl_asrc_driver);
1307 MODULE_DESCRIPTION("Freescale ASRC ASoC driver");
1308 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
1309 MODULE_ALIAS("platform:fsl-asrc");
1310 MODULE_LICENSE("GPL v2");