1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Freescale ASRC ALSA SoC Digital Audio Interface (DAI) driver
4 //
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
6 //
7 // Author: Nicolin Chen <nicoleotsuka@gmail.com>
8
9 #include <linux/clk.h>
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/dma/imx-dma.h>
15 #include <linux/pm_runtime.h>
16 #include <sound/dmaengine_pcm.h>
17 #include <sound/pcm_params.h>
18
19 #include "fsl_asrc.h"
20
21 #define IDEAL_RATIO_DECIMAL_DEPTH 26
22 #define DIVIDER_NUM 64
23 #define INIT_RETRY_NUM 50
24
25 #define pair_err(fmt, ...) \
26 dev_err(&asrc->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
27
28 #define pair_dbg(fmt, ...) \
29 dev_dbg(&asrc->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
30
31 #define pair_warn(fmt, ...) \
32 dev_warn(&asrc->pdev->dev, "Pair %c: " fmt, 'A' + index, ##__VA_ARGS__)
33
34 /* Corresponding to process_option */
35 static unsigned int supported_asrc_rate[] = {
36 5512, 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
37 64000, 88200, 96000, 128000, 176400, 192000,
38 };
39
40 static struct snd_pcm_hw_constraint_list fsl_asrc_rate_constraints = {
41 .count = ARRAY_SIZE(supported_asrc_rate),
42 .list = supported_asrc_rate,
43 };
44
45 /*
46 * The following tables map the relationship between asrc_inclk/asrc_outclk in
47 * fsl_asrc.h and the registers of ASRCSR
48 */
49 static unsigned char input_clk_map_imx35[ASRC_CLK_MAP_LEN] = {
50 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
51 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
52 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
53 };
54
55 static unsigned char output_clk_map_imx35[ASRC_CLK_MAP_LEN] = {
56 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
57 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
58 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
59 };
60
61 /* i.MX53 uses the same map for input and output */
62 static unsigned char input_clk_map_imx53[ASRC_CLK_MAP_LEN] = {
63 /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
64 0x0, 0x1, 0x2, 0x7, 0x4, 0x5, 0x6, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xf, 0xe, 0xd,
65 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
66 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
67 };
68
69 static unsigned char output_clk_map_imx53[ASRC_CLK_MAP_LEN] = {
70 /* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf */
71 0x8, 0x9, 0xa, 0x7, 0xc, 0x5, 0x6, 0xb, 0x0, 0x1, 0x2, 0x3, 0x4, 0xf, 0xe, 0xd,
72 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
73 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7,
74 };
75
76 /*
77 * i.MX8QM/i.MX8QXP uses the same map for input and output.
78 * clk_map_imx8qm[0] is for i.MX8QM asrc0
79 * clk_map_imx8qm[1] is for i.MX8QM asrc1
80 * clk_map_imx8qxp[0] is for i.MX8QXP asrc0
81 * clk_map_imx8qxp[1] is for i.MX8QXP asrc1
82 */
83 static unsigned char clk_map_imx8qm[2][ASRC_CLK_MAP_LEN] = {
84 {
85 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
86 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
87 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
88 },
89 {
90 0xf, 0xf, 0xf, 0xf, 0xf, 0x7, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
91 0x0, 0x1, 0x2, 0x3, 0xb, 0xc, 0xf, 0xf, 0xd, 0xe, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
92 0x4, 0x5, 0x6, 0xf, 0x8, 0x9, 0xa, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
93 },
94 };
95
96 static unsigned char clk_map_imx8qxp[2][ASRC_CLK_MAP_LEN] = {
97 {
98 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
99 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0xf, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xf, 0xf,
100 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
101 },
102 {
103 0xf, 0xf, 0xf, 0xf, 0xf, 0x7, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
104 0x0, 0x1, 0x2, 0x3, 0x7, 0x8, 0xf, 0xf, 0x9, 0xa, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
105 0xf, 0xf, 0x6, 0xf, 0xf, 0xf, 0xa, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
106 },
107 };
108
109 static unsigned char clk_map_imx952[ASRC_CLK_MAP_LEN] = {
110 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0,
111 0x0, 0x1, 0x2, 0x3, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x4, 0x5, 0x6, 0x8, 0xf, 0xf,
112 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x7, 0x9, 0xa, 0xb, 0xc, 0xd, 0xf, 0xf, 0xf, 0xf,
113 };
114
115 /*
116 * According to RM, the divider range is 1 ~ 8,
117 * prescaler is power of 2 from 1 ~ 128.
118 */
119 static int asrc_clk_divider[DIVIDER_NUM] = {
120 1, 2, 4, 8, 16, 32, 64, 128, /* divider = 1 */
121 2, 4, 8, 16, 32, 64, 128, 256, /* divider = 2 */
122 3, 6, 12, 24, 48, 96, 192, 384, /* divider = 3 */
123 4, 8, 16, 32, 64, 128, 256, 512, /* divider = 4 */
124 5, 10, 20, 40, 80, 160, 320, 640, /* divider = 5 */
125 6, 12, 24, 48, 96, 192, 384, 768, /* divider = 6 */
126 7, 14, 28, 56, 112, 224, 448, 896, /* divider = 7 */
127 8, 16, 32, 64, 128, 256, 512, 1024, /* divider = 8 */
128 };
129
130 /*
131 * Check if the divider is available for internal ratio mode
132 */
fsl_asrc_divider_avail(int clk_rate,int rate,int * div)133 static bool fsl_asrc_divider_avail(int clk_rate, int rate, int *div)
134 {
135 u32 rem, i;
136 u64 n;
137
138 if (div)
139 *div = 0;
140
141 if (clk_rate == 0 || rate == 0)
142 return false;
143
144 n = clk_rate;
145 rem = do_div(n, rate);
146
147 if (div)
148 *div = n;
149
150 if (rem != 0)
151 return false;
152
153 for (i = 0; i < DIVIDER_NUM; i++) {
154 if (n == asrc_clk_divider[i])
155 break;
156 }
157
158 if (i == DIVIDER_NUM)
159 return false;
160
161 return true;
162 }
163
164 /**
165 * fsl_asrc_sel_proc - Select the pre-processing and post-processing options
166 * @inrate: input sample rate
167 * @outrate: output sample rate
168 * @pre_proc: return value for pre-processing option
169 * @post_proc: return value for post-processing option
170 *
171 * Make sure to exclude following unsupported cases before
172 * calling this function:
173 * 1) inrate > 8.125 * outrate
174 * 2) inrate > 16.125 * outrate
175 *
176 */
fsl_asrc_sel_proc(int inrate,int outrate,int * pre_proc,int * post_proc)177 static void fsl_asrc_sel_proc(int inrate, int outrate,
178 int *pre_proc, int *post_proc)
179 {
180 bool post_proc_cond2;
181 bool post_proc_cond0;
182
183 /* select pre_proc between [0, 2] */
184 if (inrate * 8 > 33 * outrate)
185 *pre_proc = 2;
186 else if (inrate * 8 > 15 * outrate) {
187 if (inrate > 152000)
188 *pre_proc = 2;
189 else
190 *pre_proc = 1;
191 } else if (inrate < 76000)
192 *pre_proc = 0;
193 else if (inrate > 152000)
194 *pre_proc = 2;
195 else
196 *pre_proc = 1;
197
198 /* Condition for selection of post-processing */
199 post_proc_cond2 = (inrate * 15 > outrate * 16 && outrate < 56000) ||
200 (inrate > 56000 && outrate < 56000);
201 post_proc_cond0 = inrate * 23 < outrate * 8;
202
203 if (post_proc_cond2)
204 *post_proc = 2;
205 else if (post_proc_cond0)
206 *post_proc = 0;
207 else
208 *post_proc = 1;
209 }
210
211 /**
212 * fsl_asrc_request_pair - Request ASRC pair
213 * @channels: number of channels
214 * @pair: pointer to pair
215 *
216 * It assigns pair by the order of A->C->B because allocation of pair B,
217 * within range [ANCA, ANCA+ANCB-1], depends on the channels of pair A
218 * while pair A and pair C are comparatively independent.
219 */
fsl_asrc_request_pair(int channels,struct fsl_asrc_pair * pair)220 static int fsl_asrc_request_pair(int channels, struct fsl_asrc_pair *pair)
221 {
222 enum asrc_pair_index index = ASRC_INVALID_PAIR;
223 struct fsl_asrc *asrc = pair->asrc;
224 struct device *dev = &asrc->pdev->dev;
225 int i, ret = 0;
226
227 guard(spinlock_irqsave)(&asrc->lock);
228
229 for (i = ASRC_PAIR_A; i < ASRC_PAIR_MAX_NUM; i++) {
230 if (asrc->pair[i] != NULL)
231 continue;
232
233 index = i;
234
235 if (i != ASRC_PAIR_B)
236 break;
237 }
238
239 if (index == ASRC_INVALID_PAIR) {
240 dev_err(dev, "all pairs are busy now\n");
241 ret = -EBUSY;
242 } else if (asrc->channel_avail < channels) {
243 dev_err(dev, "can't afford required channels: %d\n", channels);
244 ret = -EINVAL;
245 } else {
246 asrc->channel_avail -= channels;
247 asrc->pair[index] = pair;
248 pair->channels = channels;
249 pair->index = index;
250 }
251
252 return ret;
253 }
254
255 /**
256 * fsl_asrc_release_pair - Release ASRC pair
257 * @pair: pair to release
258 *
259 * It clears the resource from asrc and releases the occupied channels.
260 */
fsl_asrc_release_pair(struct fsl_asrc_pair * pair)261 static void fsl_asrc_release_pair(struct fsl_asrc_pair *pair)
262 {
263 struct fsl_asrc *asrc = pair->asrc;
264 enum asrc_pair_index index = pair->index;
265
266 /* Make sure the pair is disabled */
267 regmap_update_bits(asrc->regmap, REG_ASRCTR,
268 ASRCTR_ASRCEi_MASK(index), 0);
269
270 guard(spinlock_irqsave)(&asrc->lock);
271
272 asrc->channel_avail += pair->channels;
273 asrc->pair[index] = NULL;
274 pair->error = 0;
275 }
276
277 /**
278 * fsl_asrc_set_watermarks- configure input and output thresholds
279 * @pair: pointer to pair
280 * @in: input threshold
281 * @out: output threshold
282 */
fsl_asrc_set_watermarks(struct fsl_asrc_pair * pair,u32 in,u32 out)283 static void fsl_asrc_set_watermarks(struct fsl_asrc_pair *pair, u32 in, u32 out)
284 {
285 struct fsl_asrc *asrc = pair->asrc;
286 enum asrc_pair_index index = pair->index;
287
288 regmap_update_bits(asrc->regmap, REG_ASRMCR(index),
289 ASRMCRi_EXTTHRSHi_MASK |
290 ASRMCRi_INFIFO_THRESHOLD_MASK |
291 ASRMCRi_OUTFIFO_THRESHOLD_MASK,
292 ASRMCRi_EXTTHRSHi |
293 ASRMCRi_INFIFO_THRESHOLD(in) |
294 ASRMCRi_OUTFIFO_THRESHOLD(out));
295 }
296
297 /**
298 * fsl_asrc_cal_asrck_divisor - Calculate the total divisor between asrck clock rate and sample rate
299 * @pair: pointer to pair
300 * @div: divider
301 *
302 * It follows the formula clk_rate = samplerate * (2 ^ prescaler) * divider
303 */
fsl_asrc_cal_asrck_divisor(struct fsl_asrc_pair * pair,u32 div)304 static u32 fsl_asrc_cal_asrck_divisor(struct fsl_asrc_pair *pair, u32 div)
305 {
306 u32 ps;
307
308 /* Calculate the divisors: prescaler [2^0, 2^7], divder [1, 8] */
309 for (ps = 0; div > 8; ps++)
310 div >>= 1;
311
312 return ((div - 1) << ASRCDRi_AxCPi_WIDTH) | ps;
313 }
314
315 /**
316 * fsl_asrc_set_ideal_ratio - Calculate and set the ratio for Ideal Ratio mode only
317 * @pair: pointer to pair
318 * @inrate: input rate
319 * @outrate: output rate
320 *
321 * The ratio is a 32-bit fixed point value with 26 fractional bits.
322 */
fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair * pair,int inrate,int outrate)323 static int fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair *pair,
324 int inrate, int outrate)
325 {
326 struct fsl_asrc *asrc = pair->asrc;
327 enum asrc_pair_index index = pair->index;
328 unsigned long ratio;
329 int i;
330
331 if (!outrate) {
332 pair_err("output rate should not be zero\n");
333 return -EINVAL;
334 }
335
336 /* Calculate the intergal part of the ratio */
337 ratio = (inrate / outrate) << IDEAL_RATIO_DECIMAL_DEPTH;
338
339 /* ... and then the 26 depth decimal part */
340 inrate %= outrate;
341
342 for (i = 1; i <= IDEAL_RATIO_DECIMAL_DEPTH; i++) {
343 inrate <<= 1;
344
345 if (inrate < outrate)
346 continue;
347
348 ratio |= 1 << (IDEAL_RATIO_DECIMAL_DEPTH - i);
349 inrate -= outrate;
350
351 if (!inrate)
352 break;
353 }
354
355 regmap_write(asrc->regmap, REG_ASRIDRL(index), ratio);
356 regmap_write(asrc->regmap, REG_ASRIDRH(index), ratio >> 24);
357
358 return 0;
359 }
360
361 /**
362 * fsl_asrc_config_pair - Configure the assigned ASRC pair
363 * @pair: pointer to pair
364 * @use_ideal_rate: boolean configuration
365 *
366 * It configures those ASRC registers according to a configuration instance
367 * of struct asrc_config which includes in/output sample rate, width, channel
368 * and clock settings.
369 *
370 * Note:
371 * The ideal ratio configuration can work with a flexible clock rate setting.
372 * Using IDEAL_RATIO_RATE gives a faster converting speed but overloads ASRC.
373 * For a regular audio playback, the clock rate should not be slower than an
374 * clock rate aligning with the output sample rate; For a use case requiring
375 * faster conversion, set use_ideal_rate to have the faster speed.
376 */
fsl_asrc_config_pair(struct fsl_asrc_pair * pair,bool use_ideal_rate)377 static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair, bool use_ideal_rate)
378 {
379 struct fsl_asrc_pair_priv *pair_priv = pair->private;
380 struct asrc_config *config = pair_priv->config;
381 struct fsl_asrc *asrc = pair->asrc;
382 struct fsl_asrc_priv *asrc_priv = asrc->private;
383 enum asrc_pair_index index = pair->index;
384 enum asrc_word_width input_word_width;
385 enum asrc_word_width output_word_width;
386 u32 inrate, outrate, indiv, outdiv;
387 u32 clk_index[2], div[2];
388 u64 clk_rate;
389 int in, out, channels;
390 int pre_proc, post_proc;
391 struct clk *clk;
392 bool ideal, div_avail;
393
394 if (!config) {
395 pair_err("invalid pair config\n");
396 return -EINVAL;
397 }
398
399 /* Validate channels */
400 if (config->channel_num < 1 || config->channel_num > 10) {
401 pair_err("does not support %d channels\n", config->channel_num);
402 return -EINVAL;
403 }
404
405 switch (snd_pcm_format_width(config->input_format)) {
406 case 8:
407 input_word_width = ASRC_WIDTH_8_BIT;
408 break;
409 case 16:
410 input_word_width = ASRC_WIDTH_16_BIT;
411 break;
412 case 24:
413 input_word_width = ASRC_WIDTH_24_BIT;
414 break;
415 default:
416 pair_err("does not support this input format, %d\n",
417 config->input_format);
418 return -EINVAL;
419 }
420
421 switch (snd_pcm_format_width(config->output_format)) {
422 case 16:
423 output_word_width = ASRC_WIDTH_16_BIT;
424 break;
425 case 24:
426 output_word_width = ASRC_WIDTH_24_BIT;
427 break;
428 default:
429 pair_err("does not support this output format, %d\n",
430 config->output_format);
431 return -EINVAL;
432 }
433
434 inrate = config->input_sample_rate;
435 outrate = config->output_sample_rate;
436 ideal = config->inclk == INCLK_NONE;
437
438 /* Validate input and output sample rates */
439 for (in = 0; in < ARRAY_SIZE(supported_asrc_rate); in++)
440 if (inrate == supported_asrc_rate[in])
441 break;
442
443 if (in == ARRAY_SIZE(supported_asrc_rate)) {
444 pair_err("unsupported input sample rate: %dHz\n", inrate);
445 return -EINVAL;
446 }
447
448 for (out = 0; out < ARRAY_SIZE(supported_asrc_rate); out++)
449 if (outrate == supported_asrc_rate[out])
450 break;
451
452 if (out == ARRAY_SIZE(supported_asrc_rate)) {
453 pair_err("unsupported output sample rate: %dHz\n", outrate);
454 return -EINVAL;
455 }
456
457 if ((outrate >= 5512 && outrate <= 30000) &&
458 (outrate > 24 * inrate || inrate > 8 * outrate)) {
459 pair_err("exceed supported ratio range [1/24, 8] for \
460 inrate/outrate: %d/%d\n", inrate, outrate);
461 return -EINVAL;
462 }
463
464 /* Validate input and output clock sources */
465 clk_index[IN] = asrc_priv->clk_map[IN][config->inclk];
466 clk_index[OUT] = asrc_priv->clk_map[OUT][config->outclk];
467
468 /* We only have output clock for ideal ratio mode */
469 clk = asrc_priv->asrck_clk[clk_index[ideal ? OUT : IN]];
470
471 clk_rate = clk_get_rate(clk);
472 div_avail = fsl_asrc_divider_avail(clk_rate, inrate, &div[IN]);
473
474 /*
475 * The divider range is [1, 1024], defined by the hardware. For non-
476 * ideal ratio configuration, clock rate has to be strictly aligned
477 * with the sample rate. For ideal ratio configuration, clock rates
478 * only result in different converting speeds. So remainder does not
479 * matter, as long as we keep the divider within its valid range.
480 */
481 if (div[IN] == 0 || (!ideal && !div_avail)) {
482 pair_err("failed to support input sample rate %dHz by asrck_%x\n",
483 inrate, clk_index[ideal ? OUT : IN]);
484 return -EINVAL;
485 }
486
487 div[IN] = min_t(u32, 1024, div[IN]);
488
489 clk = asrc_priv->asrck_clk[clk_index[OUT]];
490 clk_rate = clk_get_rate(clk);
491 if (ideal && use_ideal_rate)
492 div_avail = fsl_asrc_divider_avail(clk_rate, IDEAL_RATIO_RATE, &div[OUT]);
493 else
494 div_avail = fsl_asrc_divider_avail(clk_rate, outrate, &div[OUT]);
495
496 /* Output divider has the same limitation as the input one */
497 if (div[OUT] == 0 || (!ideal && !div_avail)) {
498 pair_err("failed to support output sample rate %dHz by asrck_%x\n",
499 outrate, clk_index[OUT]);
500 return -EINVAL;
501 }
502
503 div[OUT] = min_t(u32, 1024, div[OUT]);
504
505 /* Set the channel number */
506 channels = config->channel_num;
507
508 if (asrc_priv->soc->channel_bits < 4)
509 channels /= 2;
510
511 /* Update channels for current pair */
512 regmap_update_bits(asrc->regmap, REG_ASRCNCR,
513 ASRCNCR_ANCi_MASK(index, asrc_priv->soc->channel_bits),
514 ASRCNCR_ANCi(index, channels, asrc_priv->soc->channel_bits));
515
516 /* Default setting: Automatic selection for processing mode */
517 regmap_update_bits(asrc->regmap, REG_ASRCTR,
518 ASRCTR_ATSi_MASK(index), ASRCTR_ATS(index));
519 regmap_update_bits(asrc->regmap, REG_ASRCTR,
520 ASRCTR_IDRi_MASK(index) | ASRCTR_USRi_MASK(index),
521 ASRCTR_USR(index));
522
523 /* Set the input and output clock sources */
524 regmap_update_bits(asrc->regmap, REG_ASRCSR,
525 ASRCSR_AICSi_MASK(index) | ASRCSR_AOCSi_MASK(index),
526 ASRCSR_AICS(index, clk_index[IN]) |
527 ASRCSR_AOCS(index, clk_index[OUT]));
528
529 /* Calculate the input clock divisors */
530 indiv = fsl_asrc_cal_asrck_divisor(pair, div[IN]);
531 outdiv = fsl_asrc_cal_asrck_divisor(pair, div[OUT]);
532
533 /* Suppose indiv and outdiv includes prescaler, so add its MASK too */
534 regmap_update_bits(asrc->regmap, REG_ASRCDR(index),
535 ASRCDRi_AOCPi_MASK(index) | ASRCDRi_AICPi_MASK(index) |
536 ASRCDRi_AOCDi_MASK(index) | ASRCDRi_AICDi_MASK(index),
537 ASRCDRi_AOCP(index, outdiv) | ASRCDRi_AICP(index, indiv));
538
539 /* Implement word_width configurations */
540 regmap_update_bits(asrc->regmap, REG_ASRMCR1(index),
541 ASRMCR1i_OW16_MASK | ASRMCR1i_IWD_MASK,
542 ASRMCR1i_OW16(output_word_width) |
543 ASRMCR1i_IWD(input_word_width));
544
545 /* Enable BUFFER STALL */
546 regmap_update_bits(asrc->regmap, REG_ASRMCR(index),
547 ASRMCRi_BUFSTALLi_MASK, ASRMCRi_BUFSTALLi);
548
549 /* Set default thresholds for input and output FIFO */
550 fsl_asrc_set_watermarks(pair, ASRC_INPUTFIFO_THRESHOLD,
551 ASRC_INPUTFIFO_THRESHOLD);
552
553 /* Configure the following only for Ideal Ratio mode */
554 if (!ideal)
555 return 0;
556
557 /* Clear ASTSx bit to use Ideal Ratio mode */
558 regmap_update_bits(asrc->regmap, REG_ASRCTR,
559 ASRCTR_ATSi_MASK(index), 0);
560
561 /* Enable Ideal Ratio mode */
562 regmap_update_bits(asrc->regmap, REG_ASRCTR,
563 ASRCTR_IDRi_MASK(index) | ASRCTR_USRi_MASK(index),
564 ASRCTR_IDR(index) | ASRCTR_USR(index));
565
566 fsl_asrc_sel_proc(inrate, outrate, &pre_proc, &post_proc);
567
568 /* Apply configurations for pre- and post-processing */
569 regmap_update_bits(asrc->regmap, REG_ASRCFG,
570 ASRCFG_PREMODi_MASK(index) | ASRCFG_POSTMODi_MASK(index),
571 ASRCFG_PREMOD(index, pre_proc) |
572 ASRCFG_POSTMOD(index, post_proc));
573
574 return fsl_asrc_set_ideal_ratio(pair, inrate, outrate);
575 }
576
577 /**
578 * fsl_asrc_start_pair - Start the assigned ASRC pair
579 * @pair: pointer to pair
580 *
581 * It enables the assigned pair and makes it stopped at the stall level.
582 */
fsl_asrc_start_pair(struct fsl_asrc_pair * pair)583 static void fsl_asrc_start_pair(struct fsl_asrc_pair *pair)
584 {
585 struct fsl_asrc *asrc = pair->asrc;
586 enum asrc_pair_index index = pair->index;
587 int reg, retry = INIT_RETRY_NUM, i;
588
589 /* Enable the current pair */
590 regmap_update_bits(asrc->regmap, REG_ASRCTR,
591 ASRCTR_ASRCEi_MASK(index), ASRCTR_ASRCE(index));
592
593 /* Wait for status of initialization */
594 do {
595 udelay(5);
596 regmap_read(asrc->regmap, REG_ASRCFG, ®);
597 reg &= ASRCFG_INIRQi_MASK(index);
598 } while (!reg && --retry);
599
600 /* NOTE: Doesn't treat initialization timeout as an error */
601 if (!retry)
602 pair_warn("initialization isn't finished\n");
603
604 /* Make the input fifo to ASRC STALL level */
605 regmap_read(asrc->regmap, REG_ASRCNCR, ®);
606 for (i = 0; i < pair->channels * 4; i++)
607 regmap_write(asrc->regmap, REG_ASRDI(index), 0);
608
609 /* Enable overload interrupt */
610 regmap_write(asrc->regmap, REG_ASRIER, ASRIER_AOLIE);
611 }
612
613 /**
614 * fsl_asrc_stop_pair - Stop the assigned ASRC pair
615 * @pair: pointer to pair
616 */
fsl_asrc_stop_pair(struct fsl_asrc_pair * pair)617 static void fsl_asrc_stop_pair(struct fsl_asrc_pair *pair)
618 {
619 struct fsl_asrc *asrc = pair->asrc;
620 enum asrc_pair_index index = pair->index;
621
622 /* Stop the current pair */
623 regmap_update_bits(asrc->regmap, REG_ASRCTR,
624 ASRCTR_ASRCEi_MASK(index), 0);
625 }
626
627 /**
628 * fsl_asrc_get_dma_channel- Get DMA channel according to the pair and direction.
629 * @pair: pointer to pair
630 * @dir: DMA direction
631 */
fsl_asrc_get_dma_channel(struct fsl_asrc_pair * pair,bool dir)632 static struct dma_chan *fsl_asrc_get_dma_channel(struct fsl_asrc_pair *pair,
633 bool dir)
634 {
635 struct fsl_asrc *asrc = pair->asrc;
636 enum asrc_pair_index index = pair->index;
637 char name[4];
638
639 sprintf(name, "%cx%c", dir == IN ? 'r' : 't', index + 'a');
640
641 return dma_request_slave_channel(&asrc->pdev->dev, name);
642 }
643
fsl_asrc_dai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)644 static int fsl_asrc_dai_startup(struct snd_pcm_substream *substream,
645 struct snd_soc_dai *dai)
646 {
647 struct fsl_asrc *asrc = snd_soc_dai_get_drvdata(dai);
648 struct fsl_asrc_priv *asrc_priv = asrc->private;
649
650 /* Odd channel number is not valid for older ASRC (channel_bits==3) */
651 if (asrc_priv->soc->channel_bits == 3)
652 snd_pcm_hw_constraint_step(substream->runtime, 0,
653 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
654
655
656 return snd_pcm_hw_constraint_list(substream->runtime, 0,
657 SNDRV_PCM_HW_PARAM_RATE, &fsl_asrc_rate_constraints);
658 }
659
660 /* Select proper clock source for internal ratio mode */
fsl_asrc_select_clk(struct fsl_asrc_priv * asrc_priv,struct fsl_asrc_pair * pair,int in_rate,int out_rate)661 static void fsl_asrc_select_clk(struct fsl_asrc_priv *asrc_priv,
662 struct fsl_asrc_pair *pair,
663 int in_rate,
664 int out_rate)
665 {
666 struct fsl_asrc_pair_priv *pair_priv = pair->private;
667 struct asrc_config *config = pair_priv->config;
668 int rate[2], select_clk[2]; /* Array size 2 means IN and OUT */
669 int clk_rate, clk_index;
670 int i, j;
671
672 rate[IN] = in_rate;
673 rate[OUT] = out_rate;
674
675 /* Select proper clock source for internal ratio mode */
676 for (j = 0; j < 2; j++) {
677 for (i = 0; i < ASRC_CLK_MAP_LEN; i++) {
678 clk_index = asrc_priv->clk_map[j][i];
679 clk_rate = clk_get_rate(asrc_priv->asrck_clk[clk_index]);
680 /* Only match a perfect clock source with no remainder */
681 if (fsl_asrc_divider_avail(clk_rate, rate[j], NULL))
682 break;
683 }
684
685 select_clk[j] = i;
686 }
687
688 /* Switch to ideal ratio mode if there is no proper clock source */
689 if (select_clk[IN] == ASRC_CLK_MAP_LEN || select_clk[OUT] == ASRC_CLK_MAP_LEN) {
690 select_clk[IN] = INCLK_NONE;
691 select_clk[OUT] = OUTCLK_ASRCK1_CLK;
692 }
693
694 config->inclk = select_clk[IN];
695 config->outclk = select_clk[OUT];
696 }
697
fsl_asrc_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)698 static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
699 struct snd_pcm_hw_params *params,
700 struct snd_soc_dai *dai)
701 {
702 struct fsl_asrc *asrc = snd_soc_dai_get_drvdata(dai);
703 struct fsl_asrc_priv *asrc_priv = asrc->private;
704 struct snd_pcm_runtime *runtime = substream->runtime;
705 struct fsl_asrc_pair *pair = runtime->private_data;
706 struct fsl_asrc_pair_priv *pair_priv = pair->private;
707 unsigned int channels = params_channels(params);
708 unsigned int rate = params_rate(params);
709 struct asrc_config config;
710 int ret;
711
712 ret = fsl_asrc_request_pair(channels, pair);
713 if (ret) {
714 dev_err(dai->dev, "fail to request asrc pair\n");
715 return ret;
716 }
717
718 pair_priv->config = &config;
719
720 config.pair = pair->index;
721 config.channel_num = channels;
722
723 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
724 config.input_format = params_format(params);
725 config.output_format = asrc->asrc_format;
726 config.input_sample_rate = rate;
727 config.output_sample_rate = asrc->asrc_rate;
728 } else {
729 config.input_format = asrc->asrc_format;
730 config.output_format = params_format(params);
731 config.input_sample_rate = asrc->asrc_rate;
732 config.output_sample_rate = rate;
733 }
734
735 fsl_asrc_select_clk(asrc_priv, pair,
736 config.input_sample_rate,
737 config.output_sample_rate);
738
739 ret = fsl_asrc_config_pair(pair, false);
740 if (ret) {
741 dev_err(dai->dev, "fail to config asrc pair\n");
742 return ret;
743 }
744
745 return 0;
746 }
747
fsl_asrc_dai_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)748 static int fsl_asrc_dai_hw_free(struct snd_pcm_substream *substream,
749 struct snd_soc_dai *dai)
750 {
751 struct snd_pcm_runtime *runtime = substream->runtime;
752 struct fsl_asrc_pair *pair = runtime->private_data;
753
754 if (pair)
755 fsl_asrc_release_pair(pair);
756
757 return 0;
758 }
759
fsl_asrc_dai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)760 static int fsl_asrc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
761 struct snd_soc_dai *dai)
762 {
763 struct snd_pcm_runtime *runtime = substream->runtime;
764 struct fsl_asrc_pair *pair = runtime->private_data;
765
766 switch (cmd) {
767 case SNDRV_PCM_TRIGGER_START:
768 case SNDRV_PCM_TRIGGER_RESUME:
769 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
770 fsl_asrc_start_pair(pair);
771 break;
772 case SNDRV_PCM_TRIGGER_STOP:
773 case SNDRV_PCM_TRIGGER_SUSPEND:
774 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
775 fsl_asrc_stop_pair(pair);
776 break;
777 default:
778 return -EINVAL;
779 }
780
781 return 0;
782 }
783
fsl_asrc_dai_probe(struct snd_soc_dai * dai)784 static int fsl_asrc_dai_probe(struct snd_soc_dai *dai)
785 {
786 struct fsl_asrc *asrc = snd_soc_dai_get_drvdata(dai);
787
788 snd_soc_dai_init_dma_data(dai, &asrc->dma_params_tx,
789 &asrc->dma_params_rx);
790
791 return 0;
792 }
793
794 static const struct snd_soc_dai_ops fsl_asrc_dai_ops = {
795 .probe = fsl_asrc_dai_probe,
796 .startup = fsl_asrc_dai_startup,
797 .hw_params = fsl_asrc_dai_hw_params,
798 .hw_free = fsl_asrc_dai_hw_free,
799 .trigger = fsl_asrc_dai_trigger,
800 };
801
802 #define FSL_ASRC_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | \
803 SNDRV_PCM_FMTBIT_S16_LE | \
804 SNDRV_PCM_FMTBIT_S24_3LE)
805
806 static struct snd_soc_dai_driver fsl_asrc_dai = {
807 .playback = {
808 .stream_name = "ASRC-Playback",
809 .channels_min = 1,
810 .channels_max = 10,
811 .rate_min = 5512,
812 .rate_max = 192000,
813 .rates = SNDRV_PCM_RATE_KNOT,
814 .formats = FSL_ASRC_FORMATS |
815 SNDRV_PCM_FMTBIT_S8,
816 },
817 .capture = {
818 .stream_name = "ASRC-Capture",
819 .channels_min = 1,
820 .channels_max = 10,
821 .rate_min = 5512,
822 .rate_max = 192000,
823 .rates = SNDRV_PCM_RATE_KNOT,
824 .formats = FSL_ASRC_FORMATS,
825 },
826 .ops = &fsl_asrc_dai_ops,
827 };
828
fsl_asrc_readable_reg(struct device * dev,unsigned int reg)829 static bool fsl_asrc_readable_reg(struct device *dev, unsigned int reg)
830 {
831 switch (reg) {
832 case REG_ASRCTR:
833 case REG_ASRIER:
834 case REG_ASRCNCR:
835 case REG_ASRCFG:
836 case REG_ASRCSR:
837 case REG_ASRCDR1:
838 case REG_ASRCDR2:
839 case REG_ASRSTR:
840 case REG_ASRPM1:
841 case REG_ASRPM2:
842 case REG_ASRPM3:
843 case REG_ASRPM4:
844 case REG_ASRPM5:
845 case REG_ASRTFR1:
846 case REG_ASRCCR:
847 case REG_ASRDOA:
848 case REG_ASRDOB:
849 case REG_ASRDOC:
850 case REG_ASRIDRHA:
851 case REG_ASRIDRLA:
852 case REG_ASRIDRHB:
853 case REG_ASRIDRLB:
854 case REG_ASRIDRHC:
855 case REG_ASRIDRLC:
856 case REG_ASR76K:
857 case REG_ASR56K:
858 case REG_ASRMCRA:
859 case REG_ASRFSTA:
860 case REG_ASRMCRB:
861 case REG_ASRFSTB:
862 case REG_ASRMCRC:
863 case REG_ASRFSTC:
864 case REG_ASRMCR1A:
865 case REG_ASRMCR1B:
866 case REG_ASRMCR1C:
867 return true;
868 default:
869 return false;
870 }
871 }
872
fsl_asrc_volatile_reg(struct device * dev,unsigned int reg)873 static bool fsl_asrc_volatile_reg(struct device *dev, unsigned int reg)
874 {
875 switch (reg) {
876 case REG_ASRSTR:
877 case REG_ASRDIA:
878 case REG_ASRDIB:
879 case REG_ASRDIC:
880 case REG_ASRDOA:
881 case REG_ASRDOB:
882 case REG_ASRDOC:
883 case REG_ASRFSTA:
884 case REG_ASRFSTB:
885 case REG_ASRFSTC:
886 case REG_ASRCFG:
887 return true;
888 default:
889 return false;
890 }
891 }
892
fsl_asrc_writeable_reg(struct device * dev,unsigned int reg)893 static bool fsl_asrc_writeable_reg(struct device *dev, unsigned int reg)
894 {
895 switch (reg) {
896 case REG_ASRCTR:
897 case REG_ASRIER:
898 case REG_ASRCNCR:
899 case REG_ASRCFG:
900 case REG_ASRCSR:
901 case REG_ASRCDR1:
902 case REG_ASRCDR2:
903 case REG_ASRSTR:
904 case REG_ASRPM1:
905 case REG_ASRPM2:
906 case REG_ASRPM3:
907 case REG_ASRPM4:
908 case REG_ASRPM5:
909 case REG_ASRTFR1:
910 case REG_ASRCCR:
911 case REG_ASRDIA:
912 case REG_ASRDIB:
913 case REG_ASRDIC:
914 case REG_ASRIDRHA:
915 case REG_ASRIDRLA:
916 case REG_ASRIDRHB:
917 case REG_ASRIDRLB:
918 case REG_ASRIDRHC:
919 case REG_ASRIDRLC:
920 case REG_ASR76K:
921 case REG_ASR56K:
922 case REG_ASRMCRA:
923 case REG_ASRMCRB:
924 case REG_ASRMCRC:
925 case REG_ASRMCR1A:
926 case REG_ASRMCR1B:
927 case REG_ASRMCR1C:
928 return true;
929 default:
930 return false;
931 }
932 }
933
934 static const struct reg_default fsl_asrc_reg[] = {
935 { REG_ASRCTR, 0x0000 }, { REG_ASRIER, 0x0000 },
936 { REG_ASRCNCR, 0x0000 }, { REG_ASRCFG, 0x0000 },
937 { REG_ASRCSR, 0x0000 }, { REG_ASRCDR1, 0x0000 },
938 { REG_ASRCDR2, 0x0000 }, { REG_ASRSTR, 0x0000 },
939 { REG_ASRRA, 0x0000 }, { REG_ASRRB, 0x0000 },
940 { REG_ASRRC, 0x0000 }, { REG_ASRPM1, 0x0000 },
941 { REG_ASRPM2, 0x0000 }, { REG_ASRPM3, 0x0000 },
942 { REG_ASRPM4, 0x0000 }, { REG_ASRPM5, 0x0000 },
943 { REG_ASRTFR1, 0x0000 }, { REG_ASRCCR, 0x0000 },
944 { REG_ASRDIA, 0x0000 }, { REG_ASRDOA, 0x0000 },
945 { REG_ASRDIB, 0x0000 }, { REG_ASRDOB, 0x0000 },
946 { REG_ASRDIC, 0x0000 }, { REG_ASRDOC, 0x0000 },
947 { REG_ASRIDRHA, 0x0000 }, { REG_ASRIDRLA, 0x0000 },
948 { REG_ASRIDRHB, 0x0000 }, { REG_ASRIDRLB, 0x0000 },
949 { REG_ASRIDRHC, 0x0000 }, { REG_ASRIDRLC, 0x0000 },
950 { REG_ASR76K, 0x0A47 }, { REG_ASR56K, 0x0DF3 },
951 { REG_ASRMCRA, 0x0000 }, { REG_ASRFSTA, 0x0000 },
952 { REG_ASRMCRB, 0x0000 }, { REG_ASRFSTB, 0x0000 },
953 { REG_ASRMCRC, 0x0000 }, { REG_ASRFSTC, 0x0000 },
954 { REG_ASRMCR1A, 0x0000 }, { REG_ASRMCR1B, 0x0000 },
955 { REG_ASRMCR1C, 0x0000 },
956 };
957
958 static const struct regmap_config fsl_asrc_regmap_config = {
959 .reg_bits = 32,
960 .reg_stride = 4,
961 .val_bits = 32,
962
963 .max_register = REG_ASRMCR1C,
964 .reg_defaults = fsl_asrc_reg,
965 .num_reg_defaults = ARRAY_SIZE(fsl_asrc_reg),
966 .readable_reg = fsl_asrc_readable_reg,
967 .volatile_reg = fsl_asrc_volatile_reg,
968 .writeable_reg = fsl_asrc_writeable_reg,
969 .cache_type = REGCACHE_FLAT,
970 };
971
972 /**
973 * fsl_asrc_init - Initialize ASRC registers with a default configuration
974 * @asrc: ASRC context
975 */
fsl_asrc_init(struct fsl_asrc * asrc)976 static int fsl_asrc_init(struct fsl_asrc *asrc)
977 {
978 unsigned long ipg_rate;
979
980 /* Halt ASRC internal FP when input FIFO needs data for pair A, B, C */
981 regmap_write(asrc->regmap, REG_ASRCTR, ASRCTR_ASRCEN);
982
983 /* Disable interrupt by default */
984 regmap_write(asrc->regmap, REG_ASRIER, 0x0);
985
986 /* Apply recommended settings for parameters from Reference Manual */
987 regmap_write(asrc->regmap, REG_ASRPM1, 0x7fffff);
988 regmap_write(asrc->regmap, REG_ASRPM2, 0x255555);
989 regmap_write(asrc->regmap, REG_ASRPM3, 0xff7280);
990 regmap_write(asrc->regmap, REG_ASRPM4, 0xff7280);
991 regmap_write(asrc->regmap, REG_ASRPM5, 0xff7280);
992
993 /* Base address for task queue FIFO. Set to 0x7C */
994 regmap_update_bits(asrc->regmap, REG_ASRTFR1,
995 ASRTFR1_TF_BASE_MASK, ASRTFR1_TF_BASE(0xfc));
996
997 /*
998 * Set the period of the 76KHz and 56KHz sampling clocks based on
999 * the ASRC processing clock.
1000 * On iMX6, ipg_clk = 133MHz, REG_ASR76K = 0x06D6, REG_ASR56K = 0x0947
1001 */
1002 ipg_rate = clk_get_rate(asrc->ipg_clk);
1003 regmap_write(asrc->regmap, REG_ASR76K, ipg_rate / 76000);
1004 return regmap_write(asrc->regmap, REG_ASR56K, ipg_rate / 56000);
1005 }
1006
1007 /**
1008 * fsl_asrc_isr- Interrupt handler for ASRC
1009 * @irq: irq number
1010 * @dev_id: ASRC context
1011 */
fsl_asrc_isr(int irq,void * dev_id)1012 static irqreturn_t fsl_asrc_isr(int irq, void *dev_id)
1013 {
1014 struct fsl_asrc *asrc = (struct fsl_asrc *)dev_id;
1015 struct device *dev = &asrc->pdev->dev;
1016 enum asrc_pair_index index;
1017 u32 status;
1018
1019 regmap_read(asrc->regmap, REG_ASRSTR, &status);
1020
1021 /* Clean overload error */
1022 regmap_write(asrc->regmap, REG_ASRSTR, ASRSTR_AOLE);
1023
1024 /*
1025 * We here use dev_dbg() for all exceptions because ASRC itself does
1026 * not care if FIFO overflowed or underrun while a warning in the
1027 * interrupt would result a ridged conversion.
1028 */
1029 for (index = ASRC_PAIR_A; index < ASRC_PAIR_MAX_NUM; index++) {
1030 if (!asrc->pair[index])
1031 continue;
1032
1033 if (status & ASRSTR_ATQOL) {
1034 asrc->pair[index]->error |= ASRC_TASK_Q_OVERLOAD;
1035 dev_dbg(dev, "ASRC Task Queue FIFO overload\n");
1036 }
1037
1038 if (status & ASRSTR_AOOL(index)) {
1039 asrc->pair[index]->error |= ASRC_OUTPUT_TASK_OVERLOAD;
1040 pair_dbg("Output Task Overload\n");
1041 }
1042
1043 if (status & ASRSTR_AIOL(index)) {
1044 asrc->pair[index]->error |= ASRC_INPUT_TASK_OVERLOAD;
1045 pair_dbg("Input Task Overload\n");
1046 }
1047
1048 if (status & ASRSTR_AODO(index)) {
1049 asrc->pair[index]->error |= ASRC_OUTPUT_BUFFER_OVERFLOW;
1050 pair_dbg("Output Data Buffer has overflowed\n");
1051 }
1052
1053 if (status & ASRSTR_AIDU(index)) {
1054 asrc->pair[index]->error |= ASRC_INPUT_BUFFER_UNDERRUN;
1055 pair_dbg("Input Data Buffer has underflowed\n");
1056 }
1057 }
1058
1059 return IRQ_HANDLED;
1060 }
1061
fsl_asrc_get_fifo_addr(u8 dir,enum asrc_pair_index index)1062 static int fsl_asrc_get_fifo_addr(u8 dir, enum asrc_pair_index index)
1063 {
1064 return REG_ASRDx(dir, index);
1065 }
1066
1067 /* Get sample numbers in FIFO */
fsl_asrc_get_output_fifo_size(struct fsl_asrc_pair * pair)1068 static unsigned int fsl_asrc_get_output_fifo_size(struct fsl_asrc_pair *pair)
1069 {
1070 struct fsl_asrc *asrc = pair->asrc;
1071 enum asrc_pair_index index = pair->index;
1072 u32 val;
1073
1074 regmap_read(asrc->regmap, REG_ASRFST(index), &val);
1075
1076 val &= ASRFSTi_OUTPUT_FIFO_MASK;
1077
1078 return val >> ASRFSTi_OUTPUT_FIFO_SHIFT;
1079 }
1080
fsl_asrc_m2m_output_ready(struct fsl_asrc_pair * pair)1081 static bool fsl_asrc_m2m_output_ready(struct fsl_asrc_pair *pair)
1082 {
1083 struct fsl_asrc *asrc = pair->asrc;
1084 enum asrc_pair_index index = pair->index;
1085 u32 val;
1086 int ret;
1087
1088 /* Check output fifo status if it exceeds the watermark. */
1089 ret = regmap_read_poll_timeout(asrc->regmap, REG_ASRFST(index), val,
1090 (ASRFSTi_OUTPUT_FIFO_FILL(val) >= ASRC_M2M_OUTPUTFIFO_WML),
1091 1, 1000);
1092
1093 if (ret) {
1094 pair_warn("output is not ready\n");
1095 return false;
1096 }
1097
1098 return true;
1099 }
1100
fsl_asrc_m2m_prepare(struct fsl_asrc_pair * pair)1101 static int fsl_asrc_m2m_prepare(struct fsl_asrc_pair *pair)
1102 {
1103 struct fsl_asrc_pair_priv *pair_priv = pair->private;
1104 struct fsl_asrc *asrc = pair->asrc;
1105 struct device *dev = &asrc->pdev->dev;
1106 struct asrc_config config;
1107 int ret;
1108
1109 /* fill config */
1110 config.pair = pair->index;
1111 config.channel_num = pair->channels;
1112 config.input_sample_rate = pair->rate[IN];
1113 config.output_sample_rate = pair->rate[OUT];
1114 config.input_format = pair->sample_format[IN];
1115 config.output_format = pair->sample_format[OUT];
1116 config.inclk = INCLK_NONE;
1117 config.outclk = OUTCLK_ASRCK1_CLK;
1118
1119 pair_priv->config = &config;
1120 ret = fsl_asrc_config_pair(pair, true);
1121 if (ret) {
1122 dev_err(dev, "failed to config pair: %d\n", ret);
1123 return ret;
1124 }
1125
1126 pair->first_convert = 1;
1127
1128 return 0;
1129 }
1130
fsl_asrc_m2m_start(struct fsl_asrc_pair * pair)1131 static int fsl_asrc_m2m_start(struct fsl_asrc_pair *pair)
1132 {
1133 if (pair->first_convert) {
1134 fsl_asrc_start_pair(pair);
1135 pair->first_convert = 0;
1136 }
1137 /*
1138 * Clear DMA request during the stall state of ASRC:
1139 * During STALL state, the remaining in input fifo would never be
1140 * smaller than the input threshold while the output fifo would not
1141 * be bigger than output one. Thus the DMA request would be cleared.
1142 */
1143 fsl_asrc_set_watermarks(pair, ASRC_FIFO_THRESHOLD_MIN,
1144 ASRC_FIFO_THRESHOLD_MAX);
1145
1146 /* Update the real input threshold to raise DMA request */
1147 fsl_asrc_set_watermarks(pair, ASRC_M2M_INPUTFIFO_WML,
1148 ASRC_M2M_OUTPUTFIFO_WML);
1149
1150 return 0;
1151 }
1152
fsl_asrc_m2m_stop(struct fsl_asrc_pair * pair)1153 static int fsl_asrc_m2m_stop(struct fsl_asrc_pair *pair)
1154 {
1155 if (!pair->first_convert) {
1156 fsl_asrc_stop_pair(pair);
1157 pair->first_convert = 1;
1158 }
1159
1160 return 0;
1161 }
1162
1163 /* calculate capture data length according to output data length and sample rate */
fsl_asrc_m2m_calc_out_len(struct fsl_asrc_pair * pair,int input_buffer_length)1164 static int fsl_asrc_m2m_calc_out_len(struct fsl_asrc_pair *pair, int input_buffer_length)
1165 {
1166 unsigned int in_width, out_width;
1167 unsigned int channels = pair->channels;
1168 unsigned int in_samples, out_samples;
1169 unsigned int out_length;
1170
1171 in_width = snd_pcm_format_physical_width(pair->sample_format[IN]) / 8;
1172 out_width = snd_pcm_format_physical_width(pair->sample_format[OUT]) / 8;
1173
1174 in_samples = input_buffer_length / in_width / channels;
1175 out_samples = pair->rate[OUT] * in_samples / pair->rate[IN];
1176 out_length = (out_samples - ASRC_OUTPUT_LAST_SAMPLE) * out_width * channels;
1177
1178 return out_length;
1179 }
1180
fsl_asrc_m2m_get_maxburst(u8 dir,struct fsl_asrc_pair * pair)1181 static int fsl_asrc_m2m_get_maxburst(u8 dir, struct fsl_asrc_pair *pair)
1182 {
1183 struct fsl_asrc *asrc = pair->asrc;
1184 struct fsl_asrc_priv *asrc_priv = asrc->private;
1185 int wml = (dir == IN) ? ASRC_M2M_INPUTFIFO_WML : ASRC_M2M_OUTPUTFIFO_WML;
1186
1187 if (!asrc_priv->soc->use_edma)
1188 return wml * pair->channels;
1189 else
1190 return 1;
1191 }
1192
fsl_asrc_m2m_get_cap(struct fsl_asrc_m2m_cap * cap)1193 static int fsl_asrc_m2m_get_cap(struct fsl_asrc_m2m_cap *cap)
1194 {
1195 cap->fmt_in = FSL_ASRC_FORMATS;
1196 cap->fmt_out = FSL_ASRC_FORMATS | SNDRV_PCM_FMTBIT_S8;
1197
1198 cap->rate_in = supported_asrc_rate;
1199 cap->rate_in_count = ARRAY_SIZE(supported_asrc_rate);
1200 cap->rate_out = supported_asrc_rate;
1201 cap->rate_out_count = ARRAY_SIZE(supported_asrc_rate);
1202 cap->chan_min = 1;
1203 cap->chan_max = 10;
1204
1205 return 0;
1206 }
1207
fsl_asrc_m2m_pair_resume(struct fsl_asrc_pair * pair)1208 static int fsl_asrc_m2m_pair_resume(struct fsl_asrc_pair *pair)
1209 {
1210 struct fsl_asrc *asrc = pair->asrc;
1211 int i;
1212
1213 for (i = 0; i < pair->channels * 4; i++)
1214 regmap_write(asrc->regmap, REG_ASRDI(pair->index), 0);
1215
1216 pair->first_convert = 1;
1217 return 0;
1218 }
1219
1220 static int fsl_asrc_runtime_resume(struct device *dev);
1221 static int fsl_asrc_runtime_suspend(struct device *dev);
1222
fsl_asrc_probe(struct platform_device * pdev)1223 static int fsl_asrc_probe(struct platform_device *pdev)
1224 {
1225 struct device_node *np = pdev->dev.of_node;
1226 struct fsl_asrc_priv *asrc_priv;
1227 struct fsl_asrc *asrc;
1228 struct resource *res;
1229 void __iomem *regs;
1230 int irq, ret, i;
1231 u32 asrc_fmt = 0;
1232 u32 map_idx;
1233 char tmp[16];
1234 u32 width;
1235
1236 asrc = devm_kzalloc(&pdev->dev, sizeof(*asrc), GFP_KERNEL);
1237 if (!asrc)
1238 return -ENOMEM;
1239
1240 asrc_priv = devm_kzalloc(&pdev->dev, sizeof(*asrc_priv), GFP_KERNEL);
1241 if (!asrc_priv)
1242 return -ENOMEM;
1243
1244 asrc->pdev = pdev;
1245 asrc->private = asrc_priv;
1246
1247 /* Get the addresses and IRQ */
1248 regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1249 if (IS_ERR(regs))
1250 return PTR_ERR(regs);
1251
1252 asrc->paddr = res->start;
1253
1254 asrc->regmap = devm_regmap_init_mmio(&pdev->dev, regs, &fsl_asrc_regmap_config);
1255 if (IS_ERR(asrc->regmap)) {
1256 dev_err(&pdev->dev, "failed to init regmap\n");
1257 return PTR_ERR(asrc->regmap);
1258 }
1259
1260 irq = platform_get_irq(pdev, 0);
1261 if (irq < 0)
1262 return irq;
1263
1264 ret = devm_request_irq(&pdev->dev, irq, fsl_asrc_isr, 0,
1265 dev_name(&pdev->dev), asrc);
1266 if (ret) {
1267 dev_err(&pdev->dev, "failed to claim irq %u: %d\n", irq, ret);
1268 return ret;
1269 }
1270
1271 asrc->mem_clk = devm_clk_get(&pdev->dev, "mem");
1272 if (IS_ERR(asrc->mem_clk)) {
1273 dev_err(&pdev->dev, "failed to get mem clock\n");
1274 return PTR_ERR(asrc->mem_clk);
1275 }
1276
1277 asrc->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
1278 if (IS_ERR(asrc->ipg_clk)) {
1279 dev_err(&pdev->dev, "failed to get ipg clock\n");
1280 return PTR_ERR(asrc->ipg_clk);
1281 }
1282
1283 asrc->spba_clk = devm_clk_get(&pdev->dev, "spba");
1284 if (IS_ERR(asrc->spba_clk))
1285 dev_warn(&pdev->dev, "failed to get spba clock\n");
1286
1287 for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
1288 sprintf(tmp, "asrck_%x", i);
1289 asrc_priv->asrck_clk[i] = devm_clk_get(&pdev->dev, tmp);
1290 if (IS_ERR(asrc_priv->asrck_clk[i])) {
1291 dev_err(&pdev->dev, "failed to get %s clock\n", tmp);
1292 return PTR_ERR(asrc_priv->asrck_clk[i]);
1293 }
1294 }
1295
1296 asrc_priv->soc = of_device_get_match_data(&pdev->dev);
1297 asrc->use_edma = asrc_priv->soc->use_edma;
1298 asrc->start_before_dma = asrc_priv->soc->start_before_dma;
1299 asrc->get_dma_channel = fsl_asrc_get_dma_channel;
1300 asrc->request_pair = fsl_asrc_request_pair;
1301 asrc->release_pair = fsl_asrc_release_pair;
1302 asrc->get_fifo_addr = fsl_asrc_get_fifo_addr;
1303 asrc->pair_priv_size = sizeof(struct fsl_asrc_pair_priv);
1304
1305 asrc->m2m_prepare = fsl_asrc_m2m_prepare;
1306 asrc->m2m_start = fsl_asrc_m2m_start;
1307 asrc->m2m_stop = fsl_asrc_m2m_stop;
1308 asrc->get_output_fifo_size = fsl_asrc_get_output_fifo_size;
1309 asrc->m2m_calc_out_len = fsl_asrc_m2m_calc_out_len;
1310 asrc->m2m_get_maxburst = fsl_asrc_m2m_get_maxburst;
1311 asrc->m2m_pair_resume = fsl_asrc_m2m_pair_resume;
1312 asrc->m2m_get_cap = fsl_asrc_m2m_get_cap;
1313 asrc->m2m_output_ready = fsl_asrc_m2m_output_ready;
1314
1315 if (of_device_is_compatible(np, "fsl,imx35-asrc")) {
1316 asrc_priv->clk_map[IN] = input_clk_map_imx35;
1317 asrc_priv->clk_map[OUT] = output_clk_map_imx35;
1318 } else if (of_device_is_compatible(np, "fsl,imx53-asrc")) {
1319 asrc_priv->clk_map[IN] = input_clk_map_imx53;
1320 asrc_priv->clk_map[OUT] = output_clk_map_imx53;
1321 } else if (of_device_is_compatible(np, "fsl,imx8qm-asrc") ||
1322 of_device_is_compatible(np, "fsl,imx8qxp-asrc")) {
1323 ret = of_property_read_u32(np, "fsl,asrc-clk-map", &map_idx);
1324 if (ret) {
1325 dev_err(&pdev->dev, "failed to get clk map index\n");
1326 return ret;
1327 }
1328
1329 if (map_idx > 1) {
1330 dev_err(&pdev->dev, "unsupported clk map index\n");
1331 return -EINVAL;
1332 }
1333 if (of_device_is_compatible(np, "fsl,imx8qm-asrc")) {
1334 asrc_priv->clk_map[IN] = clk_map_imx8qm[map_idx];
1335 asrc_priv->clk_map[OUT] = clk_map_imx8qm[map_idx];
1336 } else {
1337 asrc_priv->clk_map[IN] = clk_map_imx8qxp[map_idx];
1338 asrc_priv->clk_map[OUT] = clk_map_imx8qxp[map_idx];
1339 }
1340 } else if (of_device_is_compatible(np, "fsl,imx952-asrc")) {
1341 asrc_priv->clk_map[IN] = clk_map_imx952;
1342 asrc_priv->clk_map[OUT] = clk_map_imx952;
1343 }
1344
1345 asrc->channel_avail = 10;
1346
1347 ret = of_property_read_u32(np, "fsl,asrc-rate",
1348 &asrc->asrc_rate);
1349 if (ret) {
1350 dev_err(&pdev->dev, "failed to get output rate\n");
1351 return ret;
1352 }
1353
1354 ret = of_property_read_u32(np, "fsl,asrc-format", &asrc_fmt);
1355 asrc->asrc_format = asrc_fmt;
1356 if (ret) {
1357 ret = of_property_read_u32(np, "fsl,asrc-width", &width);
1358 if (ret) {
1359 dev_err(&pdev->dev, "failed to decide output format\n");
1360 return ret;
1361 }
1362
1363 switch (width) {
1364 case 16:
1365 asrc->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
1366 break;
1367 case 24:
1368 asrc->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1369 break;
1370 default:
1371 dev_warn(&pdev->dev,
1372 "unsupported width, use default S24_LE\n");
1373 asrc->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1374 break;
1375 }
1376 }
1377
1378 if (!(FSL_ASRC_FORMATS & pcm_format_to_bits(asrc->asrc_format))) {
1379 dev_warn(&pdev->dev, "unsupported width, use default S24_LE\n");
1380 asrc->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1381 }
1382
1383 platform_set_drvdata(pdev, asrc);
1384 spin_lock_init(&asrc->lock);
1385 pm_runtime_enable(&pdev->dev);
1386 if (!pm_runtime_enabled(&pdev->dev)) {
1387 ret = fsl_asrc_runtime_resume(&pdev->dev);
1388 if (ret)
1389 goto err_pm_disable;
1390 }
1391
1392 ret = pm_runtime_resume_and_get(&pdev->dev);
1393 if (ret < 0)
1394 goto err_pm_get_sync;
1395
1396 ret = fsl_asrc_init(asrc);
1397 if (ret) {
1398 dev_err(&pdev->dev, "failed to init asrc %d\n", ret);
1399 goto err_pm_get_sync;
1400 }
1401
1402 ret = pm_runtime_put_sync(&pdev->dev);
1403 if (ret < 0 && ret != -ENOSYS)
1404 goto err_pm_get_sync;
1405
1406 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_asrc_component,
1407 &fsl_asrc_dai, 1);
1408 if (ret) {
1409 dev_err(&pdev->dev, "failed to register ASoC DAI\n");
1410 goto err_pm_get_sync;
1411 }
1412
1413 ret = fsl_asrc_m2m_init(asrc);
1414 if (ret) {
1415 dev_err(&pdev->dev, "failed to init m2m device %d\n", ret);
1416 goto err_pm_get_sync;
1417 }
1418
1419 return 0;
1420
1421 err_pm_get_sync:
1422 if (!pm_runtime_status_suspended(&pdev->dev))
1423 fsl_asrc_runtime_suspend(&pdev->dev);
1424 err_pm_disable:
1425 pm_runtime_disable(&pdev->dev);
1426 return ret;
1427 }
1428
fsl_asrc_remove(struct platform_device * pdev)1429 static void fsl_asrc_remove(struct platform_device *pdev)
1430 {
1431 struct fsl_asrc *asrc = dev_get_drvdata(&pdev->dev);
1432
1433 fsl_asrc_m2m_exit(asrc);
1434
1435 pm_runtime_disable(&pdev->dev);
1436 if (!pm_runtime_status_suspended(&pdev->dev))
1437 fsl_asrc_runtime_suspend(&pdev->dev);
1438 }
1439
fsl_asrc_runtime_resume(struct device * dev)1440 static int fsl_asrc_runtime_resume(struct device *dev)
1441 {
1442 struct fsl_asrc *asrc = dev_get_drvdata(dev);
1443 struct fsl_asrc_priv *asrc_priv = asrc->private;
1444 int reg, retry = INIT_RETRY_NUM;
1445 int i, ret;
1446 u32 asrctr;
1447
1448 ret = clk_prepare_enable(asrc->mem_clk);
1449 if (ret)
1450 return ret;
1451 ret = clk_prepare_enable(asrc->ipg_clk);
1452 if (ret)
1453 goto disable_mem_clk;
1454 if (!IS_ERR(asrc->spba_clk)) {
1455 ret = clk_prepare_enable(asrc->spba_clk);
1456 if (ret)
1457 goto disable_ipg_clk;
1458 }
1459 for (i = 0; i < ASRC_CLK_MAX_NUM; i++) {
1460 ret = clk_prepare_enable(asrc_priv->asrck_clk[i]);
1461 if (ret)
1462 goto disable_asrck_clk;
1463 }
1464
1465 /* Stop all pairs provisionally */
1466 regmap_read(asrc->regmap, REG_ASRCTR, &asrctr);
1467 regmap_update_bits(asrc->regmap, REG_ASRCTR,
1468 ASRCTR_ASRCEi_ALL_MASK, 0);
1469
1470 /* Restore all registers */
1471 regcache_cache_only(asrc->regmap, false);
1472 regcache_mark_dirty(asrc->regmap);
1473 regcache_sync(asrc->regmap);
1474
1475 regmap_update_bits(asrc->regmap, REG_ASRCFG,
1476 ASRCFG_NDPRi_ALL_MASK | ASRCFG_POSTMODi_ALL_MASK |
1477 ASRCFG_PREMODi_ALL_MASK, asrc_priv->regcache_cfg);
1478
1479 /* Restart enabled pairs */
1480 regmap_update_bits(asrc->regmap, REG_ASRCTR,
1481 ASRCTR_ASRCEi_ALL_MASK, asrctr);
1482
1483 /* Wait for status of initialization for all enabled pairs */
1484 do {
1485 udelay(5);
1486 regmap_read(asrc->regmap, REG_ASRCFG, ®);
1487 reg = (reg >> ASRCFG_INIRQi_SHIFT(0)) & 0x7;
1488 } while ((reg != ((asrctr >> ASRCTR_ASRCEi_SHIFT(0)) & 0x7)) && --retry);
1489
1490 /*
1491 * NOTE: Doesn't treat initialization timeout as an error
1492 * Some of the pairs may success, then still can continue.
1493 */
1494 if (!retry) {
1495 for (i = ASRC_PAIR_A; i < ASRC_PAIR_MAX_NUM; i++) {
1496 if ((asrctr & ASRCTR_ASRCEi_MASK(i)) && !(reg & (1 << i)))
1497 dev_warn(dev, "Pair %c initialization isn't finished\n", 'A' + i);
1498 }
1499 }
1500
1501 return 0;
1502
1503 disable_asrck_clk:
1504 for (i--; i >= 0; i--)
1505 clk_disable_unprepare(asrc_priv->asrck_clk[i]);
1506 if (!IS_ERR(asrc->spba_clk))
1507 clk_disable_unprepare(asrc->spba_clk);
1508 disable_ipg_clk:
1509 clk_disable_unprepare(asrc->ipg_clk);
1510 disable_mem_clk:
1511 clk_disable_unprepare(asrc->mem_clk);
1512 return ret;
1513 }
1514
fsl_asrc_runtime_suspend(struct device * dev)1515 static int fsl_asrc_runtime_suspend(struct device *dev)
1516 {
1517 struct fsl_asrc *asrc = dev_get_drvdata(dev);
1518 struct fsl_asrc_priv *asrc_priv = asrc->private;
1519 int i;
1520
1521 regmap_read(asrc->regmap, REG_ASRCFG,
1522 &asrc_priv->regcache_cfg);
1523
1524 regcache_cache_only(asrc->regmap, true);
1525
1526 for (i = 0; i < ASRC_CLK_MAX_NUM; i++)
1527 clk_disable_unprepare(asrc_priv->asrck_clk[i]);
1528 if (!IS_ERR(asrc->spba_clk))
1529 clk_disable_unprepare(asrc->spba_clk);
1530 clk_disable_unprepare(asrc->ipg_clk);
1531 clk_disable_unprepare(asrc->mem_clk);
1532
1533 return 0;
1534 }
1535
fsl_asrc_suspend(struct device * dev)1536 static int fsl_asrc_suspend(struct device *dev)
1537 {
1538 struct fsl_asrc *asrc = dev_get_drvdata(dev);
1539 int ret;
1540
1541 fsl_asrc_m2m_suspend(asrc);
1542 ret = pm_runtime_force_suspend(dev);
1543 return ret;
1544 }
1545
fsl_asrc_resume(struct device * dev)1546 static int fsl_asrc_resume(struct device *dev)
1547 {
1548 struct fsl_asrc *asrc = dev_get_drvdata(dev);
1549 int ret;
1550
1551 ret = pm_runtime_force_resume(dev);
1552 fsl_asrc_m2m_resume(asrc);
1553 return ret;
1554 }
1555
1556 static const struct dev_pm_ops fsl_asrc_pm = {
1557 RUNTIME_PM_OPS(fsl_asrc_runtime_suspend, fsl_asrc_runtime_resume, NULL)
1558 SYSTEM_SLEEP_PM_OPS(fsl_asrc_suspend, fsl_asrc_resume)
1559 };
1560
1561 static const struct fsl_asrc_soc_data fsl_asrc_imx35_data = {
1562 .use_edma = false,
1563 .channel_bits = 3,
1564 };
1565
1566 static const struct fsl_asrc_soc_data fsl_asrc_imx53_data = {
1567 .use_edma = false,
1568 .channel_bits = 4,
1569 };
1570
1571 static const struct fsl_asrc_soc_data fsl_asrc_imx8qm_data = {
1572 .use_edma = true,
1573 .channel_bits = 4,
1574 };
1575
1576 static const struct fsl_asrc_soc_data fsl_asrc_imx8qxp_data = {
1577 .use_edma = true,
1578 .channel_bits = 4,
1579 };
1580
1581 static const struct fsl_asrc_soc_data fsl_asrc_imx952_data = {
1582 .use_edma = true,
1583 .channel_bits = 4,
1584 .start_before_dma = true,
1585 };
1586
1587 static const struct of_device_id fsl_asrc_ids[] = {
1588 { .compatible = "fsl,imx35-asrc", .data = &fsl_asrc_imx35_data },
1589 { .compatible = "fsl,imx53-asrc", .data = &fsl_asrc_imx53_data },
1590 { .compatible = "fsl,imx8qm-asrc", .data = &fsl_asrc_imx8qm_data },
1591 { .compatible = "fsl,imx8qxp-asrc", .data = &fsl_asrc_imx8qxp_data },
1592 { .compatible = "fsl,imx952-asrc", .data = &fsl_asrc_imx952_data },
1593 {}
1594 };
1595 MODULE_DEVICE_TABLE(of, fsl_asrc_ids);
1596
1597 static struct platform_driver fsl_asrc_driver = {
1598 .probe = fsl_asrc_probe,
1599 .remove = fsl_asrc_remove,
1600 .driver = {
1601 .name = "fsl-asrc",
1602 .of_match_table = fsl_asrc_ids,
1603 .pm = pm_ptr(&fsl_asrc_pm),
1604 },
1605 };
1606 module_platform_driver(fsl_asrc_driver);
1607
1608 MODULE_DESCRIPTION("Freescale ASRC ASoC driver");
1609 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
1610 MODULE_ALIAS("platform:fsl-asrc");
1611 MODULE_LICENSE("GPL v2");
1612