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_priv->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
26 #define pair_dbg(fmt, ...) \
27 dev_dbg(&asrc_priv->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[] = {
45 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
48 static unsigned char output_clk_map_imx35[] = {
49 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
52 /* i.MX53 uses the same map for input and output */
53 static unsigned char input_clk_map_imx53[] = {
54 /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
55 0x0, 0x1, 0x2, 0x7, 0x4, 0x5, 0x6, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xf, 0xe, 0xd,
58 static unsigned char output_clk_map_imx53[] = {
59 /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
60 0x8, 0x9, 0xa, 0x7, 0xc, 0x5, 0x6, 0xb, 0x0, 0x1, 0x2, 0x3, 0x4, 0xf, 0xe, 0xd,
63 static unsigned char *clk_map[2];
66 * Select the pre-processing and post-processing options
67 * Make sure to exclude following unsupported cases before
68 * calling this function:
69 * 1) inrate > 8.125 * outrate
70 * 2) inrate > 16.125 * outrate
72 * inrate: input sample rate
73 * outrate: output sample rate
74 * pre_proc: return value for pre-processing option
75 * post_proc: return value for post-processing option
77 static void fsl_asrc_sel_proc(int inrate, int outrate,
78 int *pre_proc, int *post_proc)
83 /* select pre_proc between [0, 2] */
84 if (inrate * 8 > 33 * outrate)
86 else if (inrate * 8 > 15 * outrate) {
91 } else if (inrate < 76000)
93 else if (inrate > 152000)
98 /* Condition for selection of post-processing */
99 post_proc_cond2 = (inrate * 15 > outrate * 16 && outrate < 56000) ||
100 (inrate > 56000 && outrate < 56000);
101 post_proc_cond0 = inrate * 23 < outrate * 8;
105 else if (post_proc_cond0)
114 * It assigns pair by the order of A->C->B because allocation of pair B,
115 * within range [ANCA, ANCA+ANCB-1], depends on the channels of pair A
116 * while pair A and pair C are comparatively independent.
118 static int fsl_asrc_request_pair(int channels, struct fsl_asrc_pair *pair)
120 enum asrc_pair_index index = ASRC_INVALID_PAIR;
121 struct fsl_asrc *asrc_priv = pair->asrc_priv;
122 struct device *dev = &asrc_priv->pdev->dev;
123 unsigned long lock_flags;
126 spin_lock_irqsave(&asrc_priv->lock, lock_flags);
128 for (i = ASRC_PAIR_A; i < ASRC_PAIR_MAX_NUM; i++) {
129 if (asrc_priv->pair[i] != NULL)
134 if (i != ASRC_PAIR_B)
138 if (index == ASRC_INVALID_PAIR) {
139 dev_err(dev, "all pairs are busy now\n");
141 } else if (asrc_priv->channel_avail < channels) {
142 dev_err(dev, "can't afford required channels: %d\n", channels);
145 asrc_priv->channel_avail -= channels;
146 asrc_priv->pair[index] = pair;
147 pair->channels = channels;
151 spin_unlock_irqrestore(&asrc_priv->lock, lock_flags);
159 * It clears the resource from asrc_priv and releases the occupied channels.
161 static void fsl_asrc_release_pair(struct fsl_asrc_pair *pair)
163 struct fsl_asrc *asrc_priv = pair->asrc_priv;
164 enum asrc_pair_index index = pair->index;
165 unsigned long lock_flags;
167 /* Make sure the pair is disabled */
168 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
169 ASRCTR_ASRCEi_MASK(index), 0);
171 spin_lock_irqsave(&asrc_priv->lock, lock_flags);
173 asrc_priv->channel_avail += pair->channels;
174 asrc_priv->pair[index] = NULL;
177 spin_unlock_irqrestore(&asrc_priv->lock, lock_flags);
181 * Configure input and output thresholds
183 static void fsl_asrc_set_watermarks(struct fsl_asrc_pair *pair, u32 in, u32 out)
185 struct fsl_asrc *asrc_priv = pair->asrc_priv;
186 enum asrc_pair_index index = pair->index;
188 regmap_update_bits(asrc_priv->regmap, REG_ASRMCR(index),
189 ASRMCRi_EXTTHRSHi_MASK |
190 ASRMCRi_INFIFO_THRESHOLD_MASK |
191 ASRMCRi_OUTFIFO_THRESHOLD_MASK,
193 ASRMCRi_INFIFO_THRESHOLD(in) |
194 ASRMCRi_OUTFIFO_THRESHOLD(out));
198 * Calculate the total divisor between asrck clock rate and sample rate
200 * It follows the formula clk_rate = samplerate * (2 ^ prescaler) * divider
202 static u32 fsl_asrc_cal_asrck_divisor(struct fsl_asrc_pair *pair, u32 div)
206 /* Calculate the divisors: prescaler [2^0, 2^7], divder [1, 8] */
207 for (ps = 0; div > 8; ps++)
210 return ((div - 1) << ASRCDRi_AxCPi_WIDTH) | ps;
214 * Calculate and set the ratio for Ideal Ratio mode only
216 * The ratio is a 32-bit fixed point value with 26 fractional bits.
218 static int fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair *pair,
219 int inrate, int outrate)
221 struct fsl_asrc *asrc_priv = pair->asrc_priv;
222 enum asrc_pair_index index = pair->index;
227 pair_err("output rate should not be zero\n");
231 /* Calculate the intergal part of the ratio */
232 ratio = (inrate / outrate) << IDEAL_RATIO_DECIMAL_DEPTH;
234 /* ... and then the 26 depth decimal part */
237 for (i = 1; i <= IDEAL_RATIO_DECIMAL_DEPTH; i++) {
240 if (inrate < outrate)
243 ratio |= 1 << (IDEAL_RATIO_DECIMAL_DEPTH - i);
250 regmap_write(asrc_priv->regmap, REG_ASRIDRL(index), ratio);
251 regmap_write(asrc_priv->regmap, REG_ASRIDRH(index), ratio >> 24);
257 * Configure the assigned ASRC pair
259 * It configures those ASRC registers according to a configuration instance
260 * of struct asrc_config which includes in/output sample rate, width, channel
261 * and clock settings.
263 static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair)
265 struct asrc_config *config = pair->config;
266 struct fsl_asrc *asrc_priv = pair->asrc_priv;
267 enum asrc_pair_index index = pair->index;
268 u32 inrate, outrate, indiv, outdiv;
269 u32 clk_index[2], div[2];
270 int in, out, channels;
271 int pre_proc, post_proc;
276 pair_err("invalid pair config\n");
280 /* Validate channels */
281 if (config->channel_num < 1 || config->channel_num > 10) {
282 pair_err("does not support %d channels\n", config->channel_num);
286 /* Validate output width */
287 if (config->output_word_width == ASRC_WIDTH_8_BIT) {
288 pair_err("does not support 8bit width output\n");
292 inrate = config->input_sample_rate;
293 outrate = config->output_sample_rate;
294 ideal = config->inclk == INCLK_NONE;
296 /* Validate input and output sample rates */
297 for (in = 0; in < ARRAY_SIZE(supported_asrc_rate); in++)
298 if (inrate == supported_asrc_rate[in])
301 if (in == ARRAY_SIZE(supported_asrc_rate)) {
302 pair_err("unsupported input sample rate: %dHz\n", inrate);
306 for (out = 0; out < ARRAY_SIZE(supported_asrc_rate); out++)
307 if (outrate == supported_asrc_rate[out])
310 if (out == ARRAY_SIZE(supported_asrc_rate)) {
311 pair_err("unsupported output sample rate: %dHz\n", outrate);
315 if ((outrate >= 5512 && outrate <= 30000) &&
316 (outrate > 24 * inrate || inrate > 8 * outrate)) {
317 pair_err("exceed supported ratio range [1/24, 8] for \
318 inrate/outrate: %d/%d\n", inrate, outrate);
322 /* Validate input and output clock sources */
323 clk_index[IN] = clk_map[IN][config->inclk];
324 clk_index[OUT] = clk_map[OUT][config->outclk];
326 /* We only have output clock for ideal ratio mode */
327 clk = asrc_priv->asrck_clk[clk_index[ideal ? OUT : IN]];
329 div[IN] = clk_get_rate(clk) / inrate;
331 pair_err("failed to support input sample rate %dHz by asrck_%x\n",
332 inrate, clk_index[ideal ? OUT : IN]);
336 clk = asrc_priv->asrck_clk[clk_index[OUT]];
338 /* Use fixed output rate for Ideal Ratio mode (INCLK_NONE) */
340 div[OUT] = clk_get_rate(clk) / IDEAL_RATIO_RATE;
342 div[OUT] = clk_get_rate(clk) / outrate;
345 pair_err("failed to support output sample rate %dHz by asrck_%x\n",
346 outrate, clk_index[OUT]);
350 /* Set the channel number */
351 channels = config->channel_num;
353 if (asrc_priv->channel_bits < 4)
356 /* Update channels for current pair */
357 regmap_update_bits(asrc_priv->regmap, REG_ASRCNCR,
358 ASRCNCR_ANCi_MASK(index, asrc_priv->channel_bits),
359 ASRCNCR_ANCi(index, channels, asrc_priv->channel_bits));
361 /* Default setting: Automatic selection for processing mode */
362 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
363 ASRCTR_ATSi_MASK(index), ASRCTR_ATS(index));
364 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
365 ASRCTR_USRi_MASK(index), 0);
367 /* Set the input and output clock sources */
368 regmap_update_bits(asrc_priv->regmap, REG_ASRCSR,
369 ASRCSR_AICSi_MASK(index) | ASRCSR_AOCSi_MASK(index),
370 ASRCSR_AICS(index, clk_index[IN]) |
371 ASRCSR_AOCS(index, clk_index[OUT]));
373 /* Calculate the input clock divisors */
374 indiv = fsl_asrc_cal_asrck_divisor(pair, div[IN]);
375 outdiv = fsl_asrc_cal_asrck_divisor(pair, div[OUT]);
377 /* Suppose indiv and outdiv includes prescaler, so add its MASK too */
378 regmap_update_bits(asrc_priv->regmap, REG_ASRCDR(index),
379 ASRCDRi_AOCPi_MASK(index) | ASRCDRi_AICPi_MASK(index) |
380 ASRCDRi_AOCDi_MASK(index) | ASRCDRi_AICDi_MASK(index),
381 ASRCDRi_AOCP(index, outdiv) | ASRCDRi_AICP(index, indiv));
383 /* Implement word_width configurations */
384 regmap_update_bits(asrc_priv->regmap, REG_ASRMCR1(index),
385 ASRMCR1i_OW16_MASK | ASRMCR1i_IWD_MASK,
386 ASRMCR1i_OW16(config->output_word_width) |
387 ASRMCR1i_IWD(config->input_word_width));
389 /* Enable BUFFER STALL */
390 regmap_update_bits(asrc_priv->regmap, REG_ASRMCR(index),
391 ASRMCRi_BUFSTALLi_MASK, ASRMCRi_BUFSTALLi);
393 /* Set default thresholds for input and output FIFO */
394 fsl_asrc_set_watermarks(pair, ASRC_INPUTFIFO_THRESHOLD,
395 ASRC_INPUTFIFO_THRESHOLD);
397 /* Configure the following only for Ideal Ratio mode */
401 /* Clear ASTSx bit to use Ideal Ratio mode */
402 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
403 ASRCTR_ATSi_MASK(index), 0);
405 /* Enable Ideal Ratio mode */
406 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
407 ASRCTR_IDRi_MASK(index) | ASRCTR_USRi_MASK(index),
408 ASRCTR_IDR(index) | ASRCTR_USR(index));
410 fsl_asrc_sel_proc(inrate, outrate, &pre_proc, &post_proc);
412 /* Apply configurations for pre- and post-processing */
413 regmap_update_bits(asrc_priv->regmap, REG_ASRCFG,
414 ASRCFG_PREMODi_MASK(index) | ASRCFG_POSTMODi_MASK(index),
415 ASRCFG_PREMOD(index, pre_proc) |
416 ASRCFG_POSTMOD(index, post_proc));
418 return fsl_asrc_set_ideal_ratio(pair, inrate, outrate);
422 * Start the assigned ASRC pair
424 * It enables the assigned pair and makes it stopped at the stall level.
426 static void fsl_asrc_start_pair(struct fsl_asrc_pair *pair)
428 struct fsl_asrc *asrc_priv = pair->asrc_priv;
429 enum asrc_pair_index index = pair->index;
430 int reg, retry = 10, i;
432 /* Enable the current pair */
433 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
434 ASRCTR_ASRCEi_MASK(index), ASRCTR_ASRCE(index));
436 /* Wait for status of initialization */
439 regmap_read(asrc_priv->regmap, REG_ASRCFG, ®);
440 reg &= ASRCFG_INIRQi_MASK(index);
441 } while (!reg && --retry);
443 /* Make the input fifo to ASRC STALL level */
444 regmap_read(asrc_priv->regmap, REG_ASRCNCR, ®);
445 for (i = 0; i < pair->channels * 4; i++)
446 regmap_write(asrc_priv->regmap, REG_ASRDI(index), 0);
448 /* Enable overload interrupt */
449 regmap_write(asrc_priv->regmap, REG_ASRIER, ASRIER_AOLIE);
453 * Stop the assigned ASRC pair
455 static void fsl_asrc_stop_pair(struct fsl_asrc_pair *pair)
457 struct fsl_asrc *asrc_priv = pair->asrc_priv;
458 enum asrc_pair_index index = pair->index;
460 /* Stop the current pair */
461 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
462 ASRCTR_ASRCEi_MASK(index), 0);
466 * Get DMA channel according to the pair and direction.
468 struct dma_chan *fsl_asrc_get_dma_channel(struct fsl_asrc_pair *pair, bool dir)
470 struct fsl_asrc *asrc_priv = pair->asrc_priv;
471 enum asrc_pair_index index = pair->index;
474 sprintf(name, "%cx%c", dir == IN ? 'r' : 't', index + 'a');
476 return dma_request_slave_channel(&asrc_priv->pdev->dev, name);
478 EXPORT_SYMBOL_GPL(fsl_asrc_get_dma_channel);
480 static int fsl_asrc_dai_startup(struct snd_pcm_substream *substream,
481 struct snd_soc_dai *dai)
483 struct fsl_asrc *asrc_priv = snd_soc_dai_get_drvdata(dai);
485 /* Odd channel number is not valid for older ASRC (channel_bits==3) */
486 if (asrc_priv->channel_bits == 3)
487 snd_pcm_hw_constraint_step(substream->runtime, 0,
488 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
491 return snd_pcm_hw_constraint_list(substream->runtime, 0,
492 SNDRV_PCM_HW_PARAM_RATE, &fsl_asrc_rate_constraints);
495 static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
496 struct snd_pcm_hw_params *params,
497 struct snd_soc_dai *dai)
499 struct fsl_asrc *asrc_priv = snd_soc_dai_get_drvdata(dai);
500 int width = params_width(params);
501 struct snd_pcm_runtime *runtime = substream->runtime;
502 struct fsl_asrc_pair *pair = runtime->private_data;
503 unsigned int channels = params_channels(params);
504 unsigned int rate = params_rate(params);
505 struct asrc_config config;
508 ret = fsl_asrc_request_pair(channels, pair);
510 dev_err(dai->dev, "fail to request asrc pair\n");
514 pair->config = &config;
517 width = ASRC_WIDTH_16_BIT;
519 width = ASRC_WIDTH_24_BIT;
521 if (asrc_priv->asrc_width == 16)
522 word_width = ASRC_WIDTH_16_BIT;
524 word_width = ASRC_WIDTH_24_BIT;
526 config.pair = pair->index;
527 config.channel_num = channels;
528 config.inclk = INCLK_NONE;
529 config.outclk = OUTCLK_ASRCK1_CLK;
531 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
532 config.input_word_width = width;
533 config.output_word_width = word_width;
534 config.input_sample_rate = rate;
535 config.output_sample_rate = asrc_priv->asrc_rate;
537 config.input_word_width = word_width;
538 config.output_word_width = width;
539 config.input_sample_rate = asrc_priv->asrc_rate;
540 config.output_sample_rate = rate;
543 ret = fsl_asrc_config_pair(pair);
545 dev_err(dai->dev, "fail to config asrc pair\n");
552 static int fsl_asrc_dai_hw_free(struct snd_pcm_substream *substream,
553 struct snd_soc_dai *dai)
555 struct snd_pcm_runtime *runtime = substream->runtime;
556 struct fsl_asrc_pair *pair = runtime->private_data;
559 fsl_asrc_release_pair(pair);
564 static int fsl_asrc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
565 struct snd_soc_dai *dai)
567 struct snd_pcm_runtime *runtime = substream->runtime;
568 struct fsl_asrc_pair *pair = runtime->private_data;
571 case SNDRV_PCM_TRIGGER_START:
572 case SNDRV_PCM_TRIGGER_RESUME:
573 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
574 fsl_asrc_start_pair(pair);
576 case SNDRV_PCM_TRIGGER_STOP:
577 case SNDRV_PCM_TRIGGER_SUSPEND:
578 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
579 fsl_asrc_stop_pair(pair);
588 static const struct snd_soc_dai_ops fsl_asrc_dai_ops = {
589 .startup = fsl_asrc_dai_startup,
590 .hw_params = fsl_asrc_dai_hw_params,
591 .hw_free = fsl_asrc_dai_hw_free,
592 .trigger = fsl_asrc_dai_trigger,
595 static int fsl_asrc_dai_probe(struct snd_soc_dai *dai)
597 struct fsl_asrc *asrc_priv = snd_soc_dai_get_drvdata(dai);
599 snd_soc_dai_init_dma_data(dai, &asrc_priv->dma_params_tx,
600 &asrc_priv->dma_params_rx);
605 #define FSL_ASRC_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | \
606 SNDRV_PCM_FMTBIT_S16_LE | \
607 SNDRV_PCM_FMTBIT_S20_3LE)
609 static struct snd_soc_dai_driver fsl_asrc_dai = {
610 .probe = fsl_asrc_dai_probe,
612 .stream_name = "ASRC-Playback",
617 .rates = SNDRV_PCM_RATE_KNOT,
618 .formats = FSL_ASRC_FORMATS,
621 .stream_name = "ASRC-Capture",
626 .rates = SNDRV_PCM_RATE_KNOT,
627 .formats = FSL_ASRC_FORMATS,
629 .ops = &fsl_asrc_dai_ops,
632 static bool fsl_asrc_readable_reg(struct device *dev, unsigned int reg)
676 static bool fsl_asrc_volatile_reg(struct device *dev, unsigned int reg)
696 static bool fsl_asrc_writeable_reg(struct device *dev, unsigned int reg)
737 static struct reg_default fsl_asrc_reg[] = {
738 { REG_ASRCTR, 0x0000 }, { REG_ASRIER, 0x0000 },
739 { REG_ASRCNCR, 0x0000 }, { REG_ASRCFG, 0x0000 },
740 { REG_ASRCSR, 0x0000 }, { REG_ASRCDR1, 0x0000 },
741 { REG_ASRCDR2, 0x0000 }, { REG_ASRSTR, 0x0000 },
742 { REG_ASRRA, 0x0000 }, { REG_ASRRB, 0x0000 },
743 { REG_ASRRC, 0x0000 }, { REG_ASRPM1, 0x0000 },
744 { REG_ASRPM2, 0x0000 }, { REG_ASRPM3, 0x0000 },
745 { REG_ASRPM4, 0x0000 }, { REG_ASRPM5, 0x0000 },
746 { REG_ASRTFR1, 0x0000 }, { REG_ASRCCR, 0x0000 },
747 { REG_ASRDIA, 0x0000 }, { REG_ASRDOA, 0x0000 },
748 { REG_ASRDIB, 0x0000 }, { REG_ASRDOB, 0x0000 },
749 { REG_ASRDIC, 0x0000 }, { REG_ASRDOC, 0x0000 },
750 { REG_ASRIDRHA, 0x0000 }, { REG_ASRIDRLA, 0x0000 },
751 { REG_ASRIDRHB, 0x0000 }, { REG_ASRIDRLB, 0x0000 },
752 { REG_ASRIDRHC, 0x0000 }, { REG_ASRIDRLC, 0x0000 },
753 { REG_ASR76K, 0x0A47 }, { REG_ASR56K, 0x0DF3 },
754 { REG_ASRMCRA, 0x0000 }, { REG_ASRFSTA, 0x0000 },
755 { REG_ASRMCRB, 0x0000 }, { REG_ASRFSTB, 0x0000 },
756 { REG_ASRMCRC, 0x0000 }, { REG_ASRFSTC, 0x0000 },
757 { REG_ASRMCR1A, 0x0000 }, { REG_ASRMCR1B, 0x0000 },
758 { REG_ASRMCR1C, 0x0000 },
761 static const struct regmap_config fsl_asrc_regmap_config = {
766 .max_register = REG_ASRMCR1C,
767 .reg_defaults = fsl_asrc_reg,
768 .num_reg_defaults = ARRAY_SIZE(fsl_asrc_reg),
769 .readable_reg = fsl_asrc_readable_reg,
770 .volatile_reg = fsl_asrc_volatile_reg,
771 .writeable_reg = fsl_asrc_writeable_reg,
772 .cache_type = REGCACHE_FLAT,
776 * Initialize ASRC registers with a default configurations
778 static int fsl_asrc_init(struct fsl_asrc *asrc_priv)
780 /* Halt ASRC internal FP when input FIFO needs data for pair A, B, C */
781 regmap_write(asrc_priv->regmap, REG_ASRCTR, ASRCTR_ASRCEN);
783 /* Disable interrupt by default */
784 regmap_write(asrc_priv->regmap, REG_ASRIER, 0x0);
786 /* Apply recommended settings for parameters from Reference Manual */
787 regmap_write(asrc_priv->regmap, REG_ASRPM1, 0x7fffff);
788 regmap_write(asrc_priv->regmap, REG_ASRPM2, 0x255555);
789 regmap_write(asrc_priv->regmap, REG_ASRPM3, 0xff7280);
790 regmap_write(asrc_priv->regmap, REG_ASRPM4, 0xff7280);
791 regmap_write(asrc_priv->regmap, REG_ASRPM5, 0xff7280);
793 /* Base address for task queue FIFO. Set to 0x7C */
794 regmap_update_bits(asrc_priv->regmap, REG_ASRTFR1,
795 ASRTFR1_TF_BASE_MASK, ASRTFR1_TF_BASE(0xfc));
797 /* Set the processing clock for 76KHz to 133M */
798 regmap_write(asrc_priv->regmap, REG_ASR76K, 0x06D6);
800 /* Set the processing clock for 56KHz to 133M */
801 return regmap_write(asrc_priv->regmap, REG_ASR56K, 0x0947);
805 * Interrupt handler for ASRC
807 static irqreturn_t fsl_asrc_isr(int irq, void *dev_id)
809 struct fsl_asrc *asrc_priv = (struct fsl_asrc *)dev_id;
810 struct device *dev = &asrc_priv->pdev->dev;
811 enum asrc_pair_index index;
814 regmap_read(asrc_priv->regmap, REG_ASRSTR, &status);
816 /* Clean overload error */
817 regmap_write(asrc_priv->regmap, REG_ASRSTR, ASRSTR_AOLE);
820 * We here use dev_dbg() for all exceptions because ASRC itself does
821 * not care if FIFO overflowed or underrun while a warning in the
822 * interrupt would result a ridged conversion.
824 for (index = ASRC_PAIR_A; index < ASRC_PAIR_MAX_NUM; index++) {
825 if (!asrc_priv->pair[index])
828 if (status & ASRSTR_ATQOL) {
829 asrc_priv->pair[index]->error |= ASRC_TASK_Q_OVERLOAD;
830 dev_dbg(dev, "ASRC Task Queue FIFO overload\n");
833 if (status & ASRSTR_AOOL(index)) {
834 asrc_priv->pair[index]->error |= ASRC_OUTPUT_TASK_OVERLOAD;
835 pair_dbg("Output Task Overload\n");
838 if (status & ASRSTR_AIOL(index)) {
839 asrc_priv->pair[index]->error |= ASRC_INPUT_TASK_OVERLOAD;
840 pair_dbg("Input Task Overload\n");
843 if (status & ASRSTR_AODO(index)) {
844 asrc_priv->pair[index]->error |= ASRC_OUTPUT_BUFFER_OVERFLOW;
845 pair_dbg("Output Data Buffer has overflowed\n");
848 if (status & ASRSTR_AIDU(index)) {
849 asrc_priv->pair[index]->error |= ASRC_INPUT_BUFFER_UNDERRUN;
850 pair_dbg("Input Data Buffer has underflowed\n");
857 static int fsl_asrc_probe(struct platform_device *pdev)
859 struct device_node *np = pdev->dev.of_node;
860 struct fsl_asrc *asrc_priv;
861 struct resource *res;
866 asrc_priv = devm_kzalloc(&pdev->dev, sizeof(*asrc_priv), GFP_KERNEL);
870 asrc_priv->pdev = pdev;
872 /* Get the addresses and IRQ */
873 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
874 regs = devm_ioremap_resource(&pdev->dev, res);
876 return PTR_ERR(regs);
878 asrc_priv->paddr = res->start;
880 asrc_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "mem", regs,
881 &fsl_asrc_regmap_config);
882 if (IS_ERR(asrc_priv->regmap)) {
883 dev_err(&pdev->dev, "failed to init regmap\n");
884 return PTR_ERR(asrc_priv->regmap);
887 irq = platform_get_irq(pdev, 0);
891 ret = devm_request_irq(&pdev->dev, irq, fsl_asrc_isr, 0,
892 dev_name(&pdev->dev), asrc_priv);
894 dev_err(&pdev->dev, "failed to claim irq %u: %d\n", irq, ret);
898 asrc_priv->mem_clk = devm_clk_get(&pdev->dev, "mem");
899 if (IS_ERR(asrc_priv->mem_clk)) {
900 dev_err(&pdev->dev, "failed to get mem clock\n");
901 return PTR_ERR(asrc_priv->mem_clk);
904 asrc_priv->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
905 if (IS_ERR(asrc_priv->ipg_clk)) {
906 dev_err(&pdev->dev, "failed to get ipg clock\n");
907 return PTR_ERR(asrc_priv->ipg_clk);
910 asrc_priv->spba_clk = devm_clk_get(&pdev->dev, "spba");
911 if (IS_ERR(asrc_priv->spba_clk))
912 dev_warn(&pdev->dev, "failed to get spba clock\n");
914 for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
915 sprintf(tmp, "asrck_%x", i);
916 asrc_priv->asrck_clk[i] = devm_clk_get(&pdev->dev, tmp);
917 if (IS_ERR(asrc_priv->asrck_clk[i])) {
918 dev_err(&pdev->dev, "failed to get %s clock\n", tmp);
919 return PTR_ERR(asrc_priv->asrck_clk[i]);
923 if (of_device_is_compatible(np, "fsl,imx35-asrc")) {
924 asrc_priv->channel_bits = 3;
925 clk_map[IN] = input_clk_map_imx35;
926 clk_map[OUT] = output_clk_map_imx35;
928 asrc_priv->channel_bits = 4;
929 clk_map[IN] = input_clk_map_imx53;
930 clk_map[OUT] = output_clk_map_imx53;
933 ret = fsl_asrc_init(asrc_priv);
935 dev_err(&pdev->dev, "failed to init asrc %d\n", ret);
939 asrc_priv->channel_avail = 10;
941 ret = of_property_read_u32(np, "fsl,asrc-rate",
942 &asrc_priv->asrc_rate);
944 dev_err(&pdev->dev, "failed to get output rate\n");
948 ret = of_property_read_u32(np, "fsl,asrc-width",
949 &asrc_priv->asrc_width);
951 dev_err(&pdev->dev, "failed to get output width\n");
955 if (asrc_priv->asrc_width != 16 && asrc_priv->asrc_width != 24) {
956 dev_warn(&pdev->dev, "unsupported width, switching to 24bit\n");
957 asrc_priv->asrc_width = 24;
960 platform_set_drvdata(pdev, asrc_priv);
961 pm_runtime_enable(&pdev->dev);
962 spin_lock_init(&asrc_priv->lock);
964 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_asrc_component,
967 dev_err(&pdev->dev, "failed to register ASoC DAI\n");
975 static int fsl_asrc_runtime_resume(struct device *dev)
977 struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
980 ret = clk_prepare_enable(asrc_priv->mem_clk);
983 ret = clk_prepare_enable(asrc_priv->ipg_clk);
985 goto disable_mem_clk;
986 if (!IS_ERR(asrc_priv->spba_clk)) {
987 ret = clk_prepare_enable(asrc_priv->spba_clk);
989 goto disable_ipg_clk;
991 for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
992 ret = clk_prepare_enable(asrc_priv->asrck_clk[i]);
994 goto disable_asrck_clk;
1000 for (i--; i >= 0; i--)
1001 clk_disable_unprepare(asrc_priv->asrck_clk[i]);
1002 if (!IS_ERR(asrc_priv->spba_clk))
1003 clk_disable_unprepare(asrc_priv->spba_clk);
1005 clk_disable_unprepare(asrc_priv->ipg_clk);
1007 clk_disable_unprepare(asrc_priv->mem_clk);
1011 static int fsl_asrc_runtime_suspend(struct device *dev)
1013 struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
1016 for (i = 0; i < ASRC_CLK_MAX_NUM; i++)
1017 clk_disable_unprepare(asrc_priv->asrck_clk[i]);
1018 if (!IS_ERR(asrc_priv->spba_clk))
1019 clk_disable_unprepare(asrc_priv->spba_clk);
1020 clk_disable_unprepare(asrc_priv->ipg_clk);
1021 clk_disable_unprepare(asrc_priv->mem_clk);
1025 #endif /* CONFIG_PM */
1027 #ifdef CONFIG_PM_SLEEP
1028 static int fsl_asrc_suspend(struct device *dev)
1030 struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
1032 regmap_read(asrc_priv->regmap, REG_ASRCFG,
1033 &asrc_priv->regcache_cfg);
1035 regcache_cache_only(asrc_priv->regmap, true);
1036 regcache_mark_dirty(asrc_priv->regmap);
1041 static int fsl_asrc_resume(struct device *dev)
1043 struct fsl_asrc *asrc_priv = dev_get_drvdata(dev);
1046 /* Stop all pairs provisionally */
1047 regmap_read(asrc_priv->regmap, REG_ASRCTR, &asrctr);
1048 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
1049 ASRCTR_ASRCEi_ALL_MASK, 0);
1051 /* Restore all registers */
1052 regcache_cache_only(asrc_priv->regmap, false);
1053 regcache_sync(asrc_priv->regmap);
1055 regmap_update_bits(asrc_priv->regmap, REG_ASRCFG,
1056 ASRCFG_NDPRi_ALL_MASK | ASRCFG_POSTMODi_ALL_MASK |
1057 ASRCFG_PREMODi_ALL_MASK, asrc_priv->regcache_cfg);
1059 /* Restart enabled pairs */
1060 regmap_update_bits(asrc_priv->regmap, REG_ASRCTR,
1061 ASRCTR_ASRCEi_ALL_MASK, asrctr);
1065 #endif /* CONFIG_PM_SLEEP */
1067 static const struct dev_pm_ops fsl_asrc_pm = {
1068 SET_RUNTIME_PM_OPS(fsl_asrc_runtime_suspend, fsl_asrc_runtime_resume, NULL)
1069 SET_SYSTEM_SLEEP_PM_OPS(fsl_asrc_suspend, fsl_asrc_resume)
1072 static const struct of_device_id fsl_asrc_ids[] = {
1073 { .compatible = "fsl,imx35-asrc", },
1074 { .compatible = "fsl,imx53-asrc", },
1077 MODULE_DEVICE_TABLE(of, fsl_asrc_ids);
1079 static struct platform_driver fsl_asrc_driver = {
1080 .probe = fsl_asrc_probe,
1083 .of_match_table = fsl_asrc_ids,
1087 module_platform_driver(fsl_asrc_driver);
1089 MODULE_DESCRIPTION("Freescale ASRC ASoC driver");
1090 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
1091 MODULE_ALIAS("platform:fsl-asrc");
1092 MODULE_LICENSE("GPL v2");