]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mxc/asrc/mxc_asrc.c
ENGR00275310 mxc: asrc: update asrc by dma_slave_config is updated.
[karo-tx-linux.git] / drivers / mxc / asrc / mxc_asrc.c
1 /*
2  * Copyright 2008-2013 Freescale Semiconductor, Inc. All Rights Reserved.
3  *
4  * The code contained herein is licensed under the GNU General Public
5  * License. You may obtain a copy of the GNU General Public License
6  * Version 2 or later at the following locations:
7  *
8  * http://www.opensource.org/licenses/gpl-license.html
9  * http://www.gnu.org/copyleft/gpl.html
10  *
11  * @file mxc_asrc.c
12  *
13  * @brief MXC Asynchronous Sample Rate Converter
14  *
15  * @ingroup Audio
16  */
17
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/miscdevice.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/clk.h>
24 #include <linux/slab.h>
25 #include <linux/ctype.h>
26 #include <linux/regmap.h>
27 #include <linux/pagemap.h>
28 #include <linux/vmalloc.h>
29 #include <linux/types.h>
30 #include <linux/version.h>
31 #include <linux/interrupt.h>
32 #include <linux/proc_fs.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/fsl_devices.h>
35 #include <linux/sched.h>
36 #include <asm/irq.h>
37 #include <linux/memory.h>
38 #include <linux/delay.h>
39 #include <linux/of_address.h>
40 #include <linux/of_irq.h>
41 #include <linux/of_platform.h>
42 #include <linux/platform_data/dma-imx.h>
43 #include <linux/mxc_asrc.h>
44
45 #define ASRC_PROC_PATH "driver/asrc"
46
47 #define ASRC_RATIO_DECIMAL_DEPTH 26
48
49 DEFINE_SPINLOCK(data_lock);
50 DEFINE_SPINLOCK(pair_lock);
51 DEFINE_SPINLOCK(input_int_lock);
52 DEFINE_SPINLOCK(output_int_lock);
53
54 /* Sample rates are aligned with that defined in pcm.h file */
55 static const unsigned char asrc_process_table[][8][2] = {
56         /* 32kHz 44.1kHz 48kHz   64kHz   88.2kHz 96kHz   176kHz  192kHz */
57         {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},},      /* 5512Hz */
58         {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},},      /* 8kHz */
59         {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},},      /* 11025Hz */
60         {{0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},},      /* 16kHz */
61         {{0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},},      /* 22050Hz */
62         {{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0},},      /* 32kHz */
63         {{0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0},},      /* 44.1kHz */
64         {{0, 2}, {0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0}, {0, 0},},      /* 48kHz */
65         {{1, 2}, {0, 2}, {0, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 0},},      /* 64kHz */
66         {{1, 2}, {1, 2}, {1, 2}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1},},      /* 88.2kHz */
67         {{1, 2}, {1, 2}, {1, 2}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1},},      /* 96kHz */
68         {{2, 2}, {2, 2}, {2, 2}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1},},      /* 176kHz */
69         {{2, 2}, {2, 2}, {2, 2}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1},},      /* 192kHz */
70 };
71
72 static struct asrc_data *asrc;
73
74 /*
75  * The following tables map the relationship between asrc_inclk/asrc_outclk in
76  * mxc_asrc.h and the registers of ASRCSR
77  */
78 static unsigned char input_clk_map_v1[] = {
79         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
80 };
81
82 static unsigned char output_clk_map_v1[] = {
83         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
84 };
85
86 /* V2 uses the same map for input and output */
87 static unsigned char input_clk_map_v2[] = {
88 /*      0x0  0x1  0x2  0x3  0x4  0x5  0x6  0x7  0x8  0x9  0xa  0xb  0xc  0xd  0xe  0xf*/
89         0x0, 0x1, 0x2, 0x7, 0x4, 0x5, 0x6, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xf, 0xe, 0xd,
90 };
91
92 static unsigned char output_clk_map_v2[] = {
93 /*      0x0  0x1  0x2  0x3  0x4  0x5  0x6  0x7  0x8  0x9  0xa  0xb  0xc  0xd  0xe  0xf*/
94         0x8, 0x9, 0xa, 0x7, 0xc, 0x5, 0x6, 0xb, 0x0, 0x1, 0x2, 0x3, 0x4, 0xf, 0xe, 0xd,
95 };
96
97 static unsigned char *input_clk_map, *output_clk_map;
98
99 /* ALL registers of ASRC are 24-bit efficient */
100 static u32 asrc_regmap_read(struct regmap *map, unsigned int reg,
101                               unsigned int *val)
102 {
103 #ifndef ASRC_USE_REGMAP
104         *val = readl((void __iomem *)asrc->vaddr + reg) & 0xffffff;
105         return *val;
106 #else
107         return regmap_read(map, reg, val);
108 #endif
109 }
110
111 static void asrc_regmap_write(struct regmap *map, unsigned int reg,
112                                unsigned int val)
113 {
114 #ifndef ASRC_USE_REGMAP
115         writel(val & 0xffffff, (void __iomem *)asrc->vaddr + reg);
116 #else
117         return regmap_write(map, reg, val);
118 #endif
119 }
120
121 static void asrc_regmap_update_bits(struct regmap *map, unsigned int reg,
122                                      unsigned int mask, unsigned int val)
123 {
124 #ifndef ASRC_USE_REGMAP
125         u32 regval;
126
127         regval = readl((void __iomem *)asrc->vaddr + reg) & 0xffffff;
128         regval = (regval & ~mask) | (val & mask);
129         writel(regval & 0xffffff, (void __iomem *)asrc->vaddr + reg);
130 #else
131         regmap_update_bits(map, reg, mask, val);
132 #endif
133 }
134
135 /* Set ASRC_REG_ASRCNCR reg, only supporting one-pair setting at once */
136 static int asrc_set_channel_number(enum asrc_pair_index index, u32 val)
137 {
138         u32 num;
139
140         asrc_regmap_read(asrc->regmap, REG_ASRCNCR, &num);
141
142         switch (index) {
143         case ASRC_PAIR_A:
144                 num &= ~ASRCNCR_ANCA_MASK(asrc->channel_bits);
145                 num |= val;
146                 break;
147         case ASRC_PAIR_B:
148                 num &= ~ASRCNCR_ANCB_MASK(asrc->channel_bits);
149                 num |= val << asrc->channel_bits;
150                 break;
151         case ASRC_PAIR_C:
152                 num &= ~ASRCNCR_ANCC_MASK(asrc->channel_bits);
153                 num |= val << asrc->channel_bits * 2;
154                 break;
155         default:
156                 dev_err(asrc->dev, "ASRC pair number not exists.\n");
157                 return -EINVAL;
158         }
159
160         asrc_regmap_write(asrc->regmap, REG_ASRCNCR, num);
161
162         return 0;
163 }
164
165 #ifdef DEBUG
166 u32 asrc_reg[] = {
167         REG_ASRCTR,
168         REG_ASRIER,
169         REG_ASRCNCR,
170         REG_ASRCFG,
171         REG_ASRCSR,
172         REG_ASRCDR1,
173         REG_ASRCDR2,
174         REG_ASRSTR,
175         REG_ASRRA,
176         REG_ASRRB,
177         REG_ASRRC,
178         REG_ASRPM1,
179         REG_ASRPM2,
180         REG_ASRPM3,
181         REG_ASRPM4,
182         REG_ASRPM5,
183         REG_ASRTFR1,
184         REG_ASRCCR,
185         REG_ASRIDRHA,
186         REG_ASRIDRLA,
187         REG_ASRIDRHB,
188         REG_ASRIDRLB,
189         REG_ASRIDRHC,
190         REG_ASRIDRLC,
191         REG_ASR76K,
192         REG_ASR56K,
193         REG_ASRMCRA,
194         REG_ASRFSTA,
195         REG_ASRMCRB,
196         REG_ASRFSTB,
197         REG_ASRMCRC,
198         REG_ASRFSTC,
199         REG_ASRMCR1A,
200         REG_ASRMCR1B,
201         REG_ASRMCR1C,
202 };
203
204 static void dump_regs(void)
205 {
206         u32 reg, val;
207         int i;
208
209         for (i = 0; i < ARRAY_SIZE(asrc_reg); i++) {
210                 reg = asrc_reg[i];
211                 asrc_regmap_read(asrc->regmap, reg, &val);
212                 pr_debug("REG addr=0x%x val=0x%x\n", reg, val);
213         }
214 }
215 #else
216 static void dump_regs(void) {}
217 #endif
218
219 /* Only used for Ideal Ratio mode */
220 static int asrc_set_clock_ratio(enum asrc_pair_index index,
221                 int inrate, int outrate)
222 {
223         unsigned long val = 0;
224         int integ;
225         int i;
226
227         if (outrate == 0) {
228                 dev_err(asrc->dev, "Wrong output sample rate: %d\n", outrate);
229                 return -EINVAL;
230         }
231
232         /* Formula: r = (1 << ASRC_RATIO_DECIMAL_DEPTH) / outrate * inrate; */
233         for (integ = 0; inrate >= outrate; integ++)
234                 inrate -= outrate;
235
236         val |= (integ << ASRC_RATIO_DECIMAL_DEPTH);
237
238         for (i = 1; i <= ASRC_RATIO_DECIMAL_DEPTH; i++) {
239                 if ((inrate * 2) >= outrate) {
240                         val |= (1 << (ASRC_RATIO_DECIMAL_DEPTH - i));
241                         inrate = inrate * 2 - outrate;
242                 } else
243                         inrate = inrate << 1;
244
245                 if (inrate == 0)
246                         break;
247         }
248
249         asrc_regmap_write(asrc->regmap, REG_ASRIDRL(index), val);
250         asrc_regmap_write(asrc->regmap, REG_ASRIDRH(index), (val >> 24));
251
252         return 0;
253 }
254
255 /* Corresponding to asrc_process_table */
256 static int supported_input_rate[] = {
257         5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200,
258         96000, 176400, 192000,
259 };
260
261 static int supported_output_rate[] = {
262         32000, 44100, 48000, 64000, 88200, 96000, 176400, 192000,
263 };
264
265 static int asrc_set_process_configuration(enum asrc_pair_index index,
266                 int inrate, int outrate)
267 {
268         int in, out;
269
270         for (in = 0; in < ARRAY_SIZE(supported_input_rate); in++) {
271                 if (inrate == supported_input_rate[in])
272                         break;
273         }
274
275         if (in == ARRAY_SIZE(supported_input_rate)) {
276                 dev_err(asrc->dev, "Unsupported input sample rate: %d\n", in);
277                 return -EINVAL;
278         }
279
280         for (out = 0; out < ARRAY_SIZE(supported_output_rate); out++) {
281                 if (outrate == supported_output_rate[out])
282                         break;
283         }
284
285         if (out == ARRAY_SIZE(supported_output_rate)) {
286                 dev_err(asrc->dev, "Unsupported output sample rate: %d\n", out);
287                 return -EINVAL;
288         }
289
290         asrc_regmap_update_bits(asrc->regmap, REG_ASRCFG,
291                         ASRCFG_PREMODx_MASK(index) | ASRCFG_POSTMODx_MASK(index),
292                         ASRCFG_PREMOD(index, asrc_process_table[in][out][0]) |
293                         ASRCFG_POSTMOD(index, asrc_process_table[in][out][1]));
294
295         return 0;
296 }
297
298 static int asrc_get_asrck_clock_divider(int samplerate)
299 {
300         unsigned int prescaler, divider;
301         unsigned int ratio, ra;
302         unsigned long bitclk;
303         unsigned int i;
304
305         if (samplerate == 0) {
306                 dev_err(asrc->dev, "Wrong sample rate: %d\n", samplerate);
307                 return -EINVAL;
308         }
309
310         bitclk = clk_get_rate(asrc->asrc_clk);
311
312         ra = bitclk/samplerate;
313         ratio = ra;
314
315         /* Calculate the prescaler */
316         for (i = 0; ratio > 8; i++)
317                 ratio >>= 1;
318
319         prescaler = i;
320
321         /* Calculate the divider */
322         if (i)
323                 divider = ((ra + (1 << (i - 1)) - 1) >> i) - 1;
324         else
325                 divider = ra - 1;
326
327         /* The totally divider is (2 ^ prescaler) * divider */
328         return (divider << ASRCDRx_AxCPx_WIDTH) + prescaler;
329 }
330
331 int asrc_req_pair(int chn_num, enum asrc_pair_index *index)
332 {
333         struct asrc_pair *pair;
334         unsigned long lock_flags;
335         int imax = 0, busy = 0, i, ret = 0;
336
337         spin_lock_irqsave(&data_lock, lock_flags);
338
339         for (i = ASRC_PAIR_A; i < ASRC_PAIR_MAX_NUM; i++) {
340                 pair = &asrc->asrc_pair[i];
341                 if (chn_num > pair->chn_max) {
342                         imax++;
343                         continue;
344                 } else if (pair->active) {
345                         busy++;
346                         continue;
347                 }
348                 /* Save the current qualified pair */
349                 *index = i;
350
351                 /* Check if this pair is a perfect one */
352                 if (chn_num == pair->chn_max)
353                         break;
354         }
355
356         if (imax == ASRC_PAIR_MAX_NUM) {
357                 dev_err(asrc->dev, "No pair could afford requested channel number.\n");
358                 ret = -EINVAL;
359         } else if (busy == ASRC_PAIR_MAX_NUM) {
360                 dev_err(asrc->dev, "All pairs are busy now.\n");
361                 ret = -EBUSY;
362         } else if (busy + imax >= ASRC_PAIR_MAX_NUM) {
363                 pr_err("All affordable pairs are busy now.\n");
364                 ret = -EBUSY;
365         } else {
366                 pair = &asrc->asrc_pair[*index];
367                 pair->chn_num = chn_num;
368                 pair->active = 1;
369         }
370
371         spin_unlock_irqrestore(&data_lock, lock_flags);
372
373         if (!ret)
374                 clk_enable(asrc->asrc_clk);
375
376         return ret;
377 }
378 EXPORT_SYMBOL(asrc_req_pair);
379
380 void asrc_release_pair(enum asrc_pair_index index)
381 {
382         struct asrc_pair *pair = &asrc->asrc_pair[index];
383         unsigned long lock_flags;
384
385         spin_lock_irqsave(&data_lock, lock_flags);
386
387         pair->active = 0;
388         pair->overload_error = 0;
389
390         /* Disable PAIR */
391         asrc_regmap_update_bits(asrc->regmap, REG_ASRCTR,
392                         ASRCTR_ASRCEx_MASK(index), 0);
393
394         spin_unlock_irqrestore(&data_lock, lock_flags);
395 }
396 EXPORT_SYMBOL(asrc_release_pair);
397
398 int asrc_config_pair(struct asrc_config *config)
399 {
400         u32 inrate = config->input_sample_rate, indiv;
401         u32 outrate = config->output_sample_rate, outdiv;
402         unsigned long lock_flags;
403         int index = config->pair;
404         int channel_num;
405         int ret;
406
407         /* Set the channel number */
408         spin_lock_irqsave(&data_lock, lock_flags);
409         asrc->asrc_pair[index].chn_num = config->channel_num;
410         spin_unlock_irqrestore(&data_lock, lock_flags);
411
412         if (asrc->channel_bits > 3)
413                 channel_num = config->channel_num;
414         else
415                 channel_num = (config->channel_num + 1) / 2;
416
417         asrc_set_channel_number(index, channel_num);
418
419         /* Set the clock source */
420         asrc_regmap_update_bits(asrc->regmap, REG_ASRCSR,
421                         ASRCSR_AICSx_MASK(index) | ASRCSR_AOCSx_MASK(index),
422                         ASRCSR_AICS(index, input_clk_map[config->inclk]) |
423                         ASRCSR_AOCS(index, output_clk_map[config->outclk]));
424
425         /* Default setting: Automatic selection for processing mode */
426         asrc_regmap_update_bits(asrc->regmap, REG_ASRCTR,
427                         ASRCTR_ATSx_MASK(index), ASRCTR_ATS(index));
428         asrc_regmap_update_bits(asrc->regmap, REG_ASRCTR,
429                         ASRCTR_USRx_MASK(index), 0);
430
431         /* Default Input Clock Divider Setting */
432         switch (config->inclk & ASRCSR_AxCSx_MASK) {
433         case INCLK_SPDIF_RX:
434                 indiv = ASRC_PRESCALER_SPDIF_RX;
435                 break;
436         case INCLK_SPDIF_TX:
437                 indiv = ASRC_PRESCALER_SPDIF_TX;
438                 break;
439         case INCLK_ASRCK1_CLK:
440                 indiv = asrc_get_asrck_clock_divider(inrate);
441                 break;
442         default:
443                 switch (config->input_word_width) {
444                 case ASRC_WIDTH_16_BIT:
445                         indiv = ASRC_PRESCALER_I2S_16BIT;
446                         break;
447                 case ASRC_WIDTH_24_BIT:
448                         indiv = ASRC_PRESCALER_I2S_24BIT;
449                         break;
450                 default:
451                         dev_err(asrc->dev, "Unsupported input word width %d\n",
452                                         config->input_word_width);
453                         return -EINVAL;
454                 }
455                 break;
456         }
457
458         /* Default Output Clock Divider Setting */
459         switch (config->outclk & ASRCSR_AxCSx_MASK) {
460         case OUTCLK_SPDIF_RX:
461                 outdiv = ASRC_PRESCALER_SPDIF_RX;
462                 break;
463         case OUTCLK_SPDIF_TX:
464                 outdiv = ASRC_PRESCALER_SPDIF_TX;
465                 break;
466         case OUTCLK_ASRCK1_CLK:
467                 if ((config->inclk & ASRCSR_AxCSx_MASK) == INCLK_NONE)
468                         outdiv = ASRC_PRESCALER_IDEAL_RATIO;
469                 else
470                         outdiv = asrc_get_asrck_clock_divider(outrate);
471                 break;
472         default:
473                 switch (config->output_word_width) {
474                 case ASRC_WIDTH_16_BIT:
475                         outdiv = ASRC_PRESCALER_I2S_16BIT;
476                         break;
477                 case ASRC_WIDTH_24_BIT:
478                         outdiv = ASRC_PRESCALER_I2S_24BIT;
479                         break;
480                 default:
481                         dev_err(asrc->dev, "Unsupported output word width %d\n",
482                                         config->input_word_width);
483                         return -EINVAL;
484                 }
485                 break;
486         }
487
488         /* indiv and outdiv'd include prescaler's value, so add its MASK too */
489         asrc_regmap_update_bits(asrc->regmap, REG_ASRCDR(index),
490                         ASRCDRx_AOCPx_MASK(index) | ASRCDRx_AICPx_MASK(index) |
491                         ASRCDRx_AOCDx_MASK(index) | ASRCDRx_AICDx_MASK(index),
492                         ASRCDRx_AOCP(index, outdiv) | ASRCDRx_AICP(index, indiv));
493
494         /* Check whether ideal ratio is a must */
495         switch (config->inclk & ASRCSR_AxCSx_MASK) {
496         case INCLK_NONE:
497                 /* Clear ASTSx bit to use ideal ratio */
498                 asrc_regmap_update_bits(asrc->regmap, REG_ASRCTR,
499                                 ASRCTR_ATSx_MASK(index), 0);
500
501                 asrc_regmap_update_bits(asrc->regmap, REG_ASRCTR,
502                                 ASRCTR_IDRx_MASK(index) | ASRCTR_USRx_MASK(index),
503                                 ASRCTR_IDR(index) | ASRCTR_USR(index));
504
505                 ret = asrc_set_clock_ratio(index, inrate, outrate);
506                 if (ret)
507                         return ret;
508
509                 ret = asrc_set_process_configuration(index, inrate, outrate);
510                 if (ret)
511                         return ret;
512
513                 break;
514         case INCLK_ASRCK1_CLK:
515                 /* This case and default are both remained for v1 */
516                 if (inrate == 44100 || inrate == 88200) {
517                         dev_err(asrc->dev, "Unsupported sample rate %d by ASRC clock.\n",
518                                         inrate);
519                         return -EINVAL;
520                 }
521                 break;
522         default:
523                 if ((config->outclk & ASRCSR_AxCSx_MASK) != OUTCLK_ASRCK1_CLK)
524                         break;
525
526                 if (outrate == 44100 || outrate == 88200) {
527                         dev_err(asrc->dev, "Unsupported sample rate %d by ASRC clock.\n",
528                                         outrate);
529                         return -EINVAL;
530                 }
531                 break;
532         }
533
534         /* Config input and output wordwidth */
535         if (config->output_word_width == ASRC_WIDTH_8_BIT) {
536                 dev_err(asrc->dev, "Unsupported wordwidth for output: 8bit.\n");
537                 dev_err(asrc->dev, "Output only support: 16bit or 24bit.\n");
538                 return -EINVAL;
539         }
540
541         asrc_regmap_update_bits(asrc->regmap, REG_ASRMCR1(index),
542                         ASRMCR1x_OW16_MASK | ASRMCR1x_IWD_MASK,
543                         ASRMCR1x_OW16(config->output_word_width) |
544                         ASRMCR1x_IWD(config->input_word_width));
545
546         /* Enable BUFFER STALL */
547         asrc_regmap_update_bits(asrc->regmap, REG_ASRMCR(index),
548                         ASRMCRx_BUFSTALLx_MASK, ASRMCRx_BUFSTALLx);
549
550         /* Set Threshold for input and output FIFO */
551         ret = asrc_set_watermark(index, ASRC_INPUTFIFO_THRESHOLD,
552                         ASRC_INPUTFIFO_THRESHOLD);
553
554         return ret;
555 }
556 EXPORT_SYMBOL(asrc_config_pair);
557
558 #define ASRC_MAX_FIFO_THRESHOLD         63
559
560 int asrc_set_watermark(enum asrc_pair_index index, u32 in_wm, u32 out_wm)
561 {
562         if (in_wm > ASRC_MAX_FIFO_THRESHOLD ||
563                         out_wm > ASRC_MAX_FIFO_THRESHOLD) {
564                 dev_err(asrc->dev, "Error watermark!\n");
565                 return -EINVAL;
566         }
567
568         asrc_regmap_update_bits(asrc->regmap, REG_ASRMCR(index),
569                         ASRMCRx_EXTTHRSHx_MASK | ASRMCRx_INFIFO_THRESHOLD_MASK |
570                         ASRMCRx_OUTFIFO_THRESHOLD_MASK,
571                         ASRMCRx_EXTTHRSHx | ASRMCRx_INFIFO_THRESHOLD(in_wm) |
572                         ASRMCRx_OUTFIFO_THRESHOLD(out_wm));
573
574         return 0;
575 }
576 EXPORT_SYMBOL(asrc_set_watermark);
577
578 void asrc_start_conv(enum asrc_pair_index index)
579 {
580         unsigned long lock_flags;
581         int reg, retry;
582
583         spin_lock_irqsave(&data_lock, lock_flags);
584
585         asrc_regmap_update_bits(asrc->regmap, REG_ASRCTR,
586                         ASRCTR_ASRCEx_MASK(index), ASRCTR_ASRCE(index));
587
588         /* Wait for status of initialization */
589         for (retry = 10, reg = 0; !reg && retry; --retry) {
590                 udelay(5);
591                 asrc_regmap_read(asrc->regmap, REG_ASRCFG, &reg);
592                 reg &= ASRCFG_INIRQx_MASK(index);
593         }
594
595         /* Overload Interrupt Enable */
596         asrc_regmap_write(asrc->regmap, REG_ASRIER, ASRIER_AOLIE);
597
598         spin_unlock_irqrestore(&data_lock, lock_flags);
599
600         return;
601 }
602 EXPORT_SYMBOL(asrc_start_conv);
603
604 void asrc_stop_conv(enum asrc_pair_index index)
605 {
606         unsigned long lock_flags;
607
608         spin_lock_irqsave(&data_lock, lock_flags);
609
610         asrc_regmap_update_bits(asrc->regmap, REG_ASRCTR,
611                         ASRCTR_ASRCEx_MASK(index), 0);
612
613         spin_unlock_irqrestore(&data_lock, lock_flags);
614
615         return;
616 }
617 EXPORT_SYMBOL(asrc_stop_conv);
618
619 void asrc_finish_conv(enum asrc_pair_index index)
620 {
621         clk_disable(asrc->asrc_clk);
622         return;
623 }
624 EXPORT_SYMBOL(asrc_finish_conv);
625
626 #define SET_OVERLOAD_ERR(index, err) \
627         do {asrc->asrc_pair[index].overload_error |= err; } while (0)
628
629 static irqreturn_t asrc_isr(int irq, void *dev_id)
630 {
631         enum asrc_pair_index index;
632         u32 status;
633
634         asrc_regmap_read(asrc->regmap, REG_ASRSTR, &status);
635
636         for (index = ASRC_PAIR_A; index < ASRC_PAIR_MAX_NUM; index++) {
637                 if (asrc->asrc_pair[index].active == 0)
638                         continue;
639                 if (status & ASRSTR_ATQOL)
640                         SET_OVERLOAD_ERR(index, ASRC_TASK_Q_OVERLOAD);
641                 if (status & ASRSTR_AOOL(index))
642                         SET_OVERLOAD_ERR(index, ASRC_OUTPUT_TASK_OVERLOAD);
643                 if (status & ASRSTR_AIOL(index))
644                         SET_OVERLOAD_ERR(index, ASRC_INPUT_TASK_OVERLOAD);
645                 if (status & ASRSTR_AODO(index))
646                         SET_OVERLOAD_ERR(index, ASRC_OUTPUT_BUFFER_OVERFLOW);
647                 if (status & ASRSTR_AIDU(index))
648                         SET_OVERLOAD_ERR(index, ASRC_INPUT_BUFFER_UNDERRUN);
649         }
650
651         /* Clean overload error  */
652         asrc_regmap_update_bits(asrc->regmap, REG_ASRSTR,
653                         ASRSTR_AOLE_MASK, ASRSTR_AOLE);
654
655         return IRQ_HANDLED;
656 }
657
658 void asrc_get_status(struct asrc_status_flags *flags)
659 {
660         enum asrc_pair_index index = flags->index;
661         unsigned long lock_flags;
662
663         spin_lock_irqsave(&data_lock, lock_flags);
664
665         flags->overload_error = asrc->asrc_pair[index].overload_error;
666
667         spin_unlock_irqrestore(&data_lock, lock_flags);
668
669         return;
670 }
671 EXPORT_SYMBOL(asrc_get_status);
672
673 u32 asrc_get_per_addr(enum asrc_pair_index index, bool in)
674 {
675         u32 addr = in ? REG_ASRDI(index) : REG_ASRDO(index);
676
677         return asrc->paddr + addr;
678 }
679 EXPORT_SYMBOL(asrc_get_per_addr);
680
681 static int mxc_init_asrc(void)
682 {
683         /* Halt ASRC internal FP when input FIFO needs data for pair A, B, C */
684         asrc_regmap_write(asrc->regmap, REG_ASRCTR, ASRCTR_ASRCEN);
685
686         /* Disable interrupt by default */
687         asrc_regmap_write(asrc->regmap, REG_ASRIER, 0x0);
688
689         /* Default 2: 6: 2 channel assignment */
690         asrc_set_channel_number(ASRC_PAIR_A, 2);
691         asrc_set_channel_number(ASRC_PAIR_B, 6);
692         asrc_set_channel_number(ASRC_PAIR_C, 2);
693
694         /* Parameter Registers recommended settings */
695         asrc_regmap_write(asrc->regmap, REG_ASRPM1, 0x7fffff);
696         asrc_regmap_write(asrc->regmap, REG_ASRPM2, 0x255555);
697         asrc_regmap_write(asrc->regmap, REG_ASRPM3, 0xff7280);
698         asrc_regmap_write(asrc->regmap, REG_ASRPM4, 0xff7280);
699         asrc_regmap_write(asrc->regmap, REG_ASRPM5, 0xff7280);
700
701         /* Base address for task queue FIFO. Set to 0x7C */
702         asrc_regmap_update_bits(asrc->regmap, REG_ASRTFR1,
703                         ASRTFR1_TF_BASE_MASK, ASRTFR1_TF_BASE(0xfc));
704
705         /* Set the processing clock for 76KHz, 133M */
706         asrc_regmap_write(asrc->regmap, REG_ASR76K, 0x06D6);
707
708         /* Set the processing clock for 56KHz, 133M */
709         asrc_regmap_write(asrc->regmap, REG_ASR56K, 0x0947);
710
711         return 0;
712 }
713
714 #define ASRC_xPUT_DMA_CALLBACK(in) \
715         ((in) ? asrc_input_dma_callback : asrc_output_dma_callback)
716
717 static void asrc_input_dma_callback(void *data)
718 {
719         struct asrc_pair_params *params = (struct asrc_pair_params *)data;
720         unsigned long lock_flags;
721
722         dma_unmap_sg(NULL, params->input_sg, params->input_sg_nodes,
723                         DMA_MEM_TO_DEV);
724
725         spin_lock_irqsave(&input_int_lock, lock_flags);
726
727         params->input_counter++;
728         wake_up_interruptible(&params->input_wait_queue);
729
730         spin_unlock_irqrestore(&input_int_lock, lock_flags);
731
732         schedule_work(&params->task_output_work);
733
734         return;
735 }
736
737 static void asrc_output_dma_callback(void *data)
738 {
739         struct asrc_pair_params *params = (struct asrc_pair_params *)data;
740
741         dma_unmap_sg(NULL, params->output_sg, params->output_sg_nodes,
742                         DMA_MEM_TO_DEV);
743 }
744
745 static unsigned int asrc_get_output_FIFO_size(enum asrc_pair_index index)
746 {
747         u32 val;
748
749         asrc_regmap_read(asrc->regmap, REG_ASRFST(index), &val);
750
751         val &= ASRFSTx_OUTPUT_FIFO_MASK;
752
753         return val >> ASRFSTx_OUTPUT_FIFO_SHIFT;
754 }
755
756 static unsigned int asrc_get_input_FIFO_size(enum asrc_pair_index index)
757 {
758         u32 val;
759
760         asrc_regmap_read(asrc->regmap, REG_ASRFST(index), &val);
761
762         val &= ASRFSTx_INPUT_FIFO_MASK;
763
764         return val >> ASRFSTx_INPUT_FIFO_SHIFT;
765 }
766
767 static u32 asrc_read_one_from_output_FIFO(enum asrc_pair_index index)
768 {
769         u32 val;
770
771         asrc_regmap_read(asrc->regmap, REG_ASRDO(index), &val);
772
773         return val;
774 }
775
776 static void asrc_write_one_to_output_FIFO(enum asrc_pair_index index, u32 val)
777 {
778         asrc_regmap_write(asrc->regmap, REG_ASRDI(index), val);
779 }
780
781 static void asrc_read_output_FIFO(struct asrc_pair_params *params)
782 {
783         u32 *reg24 = params->output_last_period.dma_vaddr;
784         u16 *reg16 = params->output_last_period.dma_vaddr;
785         enum asrc_pair_index index = params->index;
786         u32 d, i, j, reg, size, t_size;
787         bool bit24 = false;
788
789         if (params->output_word_width == ASRC_WIDTH_24_BIT)
790                 bit24 = true;
791
792         /* Delay for last period data output */
793         d = 1000000 / params->output_sample_rate * params->last_period_sample;
794
795         t_size = 0;
796         do {
797                 mdelay(1);
798                 size = asrc_get_output_FIFO_size(index);
799                 for (i = 0; i < size; i++) {
800                         for (j = 0; j < params->channel_nums; j++) {
801                                 reg = asrc_read_one_from_output_FIFO(index);
802                                 if (bit24) {
803                                         *(reg24) = reg;
804                                         reg24++;
805                                 } else {
806                                         *(reg16) = (u16)reg;
807                                         reg16++;
808                                 }
809                         }
810                 }
811                 t_size += size;
812         } while (size);
813
814         if (t_size > params->last_period_sample)
815                 t_size = params->last_period_sample;
816
817         params->output_last_period.length = t_size * params->channel_nums * 2;
818         if (bit24)
819                 params->output_last_period.length *= 2;
820 }
821
822 static void asrc_output_task_worker(struct work_struct *w)
823 {
824         struct asrc_pair_params *params =
825                 container_of(w, struct asrc_pair_params, task_output_work);
826         unsigned long lock_flags;
827
828         if (!params->pair_hold)
829                 return;
830
831         spin_lock_irqsave(&pair_lock, lock_flags);
832         asrc_read_output_FIFO(params);
833         spin_unlock_irqrestore(&pair_lock, lock_flags);
834
835         /* Finish receiving all output data */
836         spin_lock_irqsave(&output_int_lock, lock_flags);
837
838         params->output_counter++;
839         wake_up_interruptible(&params->output_wait_queue);
840
841         spin_unlock_irqrestore(&output_int_lock, lock_flags);
842 }
843
844 static void mxc_free_dma_buf(struct asrc_pair_params *params)
845 {
846         if (params->input_dma_total.dma_vaddr != NULL) {
847                 kfree(params->input_dma_total.dma_vaddr);
848                 params->input_dma_total.dma_vaddr = NULL;
849         }
850
851         if (params->output_dma_total.dma_vaddr != NULL) {
852                 kfree(params->output_dma_total.dma_vaddr);
853                 params->output_dma_total.dma_vaddr = NULL;
854         }
855
856         if (params->output_last_period.dma_vaddr) {
857                 dma_free_coherent(asrc->dev, 1024 * params->last_period_sample,
858                                 params->output_last_period.dma_vaddr,
859                                 params->output_last_period.dma_paddr);
860                 params->output_last_period.dma_vaddr = NULL;
861         }
862
863         return;
864 }
865
866 static int mxc_allocate_dma_buf(struct asrc_pair_params *params)
867 {
868         struct dma_block *input_a, *output_a, *last_period;
869
870         input_a = &params->input_dma_total;
871         output_a = &params->output_dma_total;
872         last_period = &params->output_last_period;
873
874         input_a->dma_vaddr = kzalloc(input_a->length, GFP_KERNEL);
875         if (!input_a->dma_vaddr) {
876                 dev_err(asrc->dev, "failed to allocate input dma buffer!\n");
877                 goto exit;
878         }
879         input_a->dma_paddr = virt_to_dma(NULL, input_a->dma_vaddr);
880
881         output_a->dma_vaddr = kzalloc(output_a->length, GFP_KERNEL);
882         if (!output_a->dma_vaddr) {
883                 dev_err(asrc->dev, "failed to allocate output dma buffer!\n");
884                 goto exit;
885         }
886         output_a->dma_paddr = virt_to_dma(NULL, output_a->dma_vaddr);
887
888         last_period->dma_vaddr = dma_alloc_coherent(NULL,
889                         1024 * params->last_period_sample,
890                         &last_period->dma_paddr, GFP_KERNEL);
891
892         return 0;
893
894 exit:
895         mxc_free_dma_buf(params);
896         dev_err(asrc->dev, "failed to allocate buffer.\n");
897
898         return -ENOBUFS;
899 }
900
901 static struct dma_chan *imx_asrc_dma_request_channel(
902                 struct asrc_pair_params *params, bool in)
903 {
904         char name[4];
905
906         sprintf(name, "%cx%c", in ? 'r' : 't', params->index + 'a');
907
908         return dma_request_slave_channel(asrc->dev, name);
909 }
910
911 static int imx_asrc_dma_config(struct asrc_pair_params *params,
912                 struct dma_chan *chan, u32 dma_addr, void *buf_addr,
913                 u32 buf_len, bool in, enum asrc_word_width word_width)
914 {
915         struct dma_async_tx_descriptor *desc;
916         struct dma_slave_config slave_config;
917         enum dma_slave_buswidth buswidth;
918         struct scatterlist *sg;
919         unsigned int sg_nent, i;
920         int ret;
921
922         if (in) {
923                 sg = params->input_sg;
924                 sg_nent = params->input_sg_nodes;
925                 desc = params->desc_in;
926         } else {
927                 sg = params->output_sg;
928                 sg_nent = params->output_sg_nodes;
929                 desc = params->desc_out;
930         }
931
932         switch (word_width) {
933         case ASRC_WIDTH_16_BIT:
934                 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
935                 break;
936         case ASRC_WIDTH_24_BIT:
937                 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
938                 break;
939         default:
940                 dev_err(asrc->dev, "Error word_width.\n");
941                 return -EINVAL;
942         }
943
944         slave_config.dma_request0 = 0;
945         slave_config.dma_request1 = 0;
946
947         if (in) {
948                 slave_config.direction = DMA_MEM_TO_DEV;
949                 slave_config.dst_addr = dma_addr;
950                 slave_config.dst_addr_width = buswidth;
951                 slave_config.dst_maxburst =
952                         params->input_wm * params->channel_nums / buswidth;
953         } else {
954                 slave_config.direction = DMA_DEV_TO_MEM;
955                 slave_config.src_addr = dma_addr;
956                 slave_config.src_addr_width = buswidth;
957                 slave_config.src_maxburst =
958                         params->output_wm * params->channel_nums / buswidth;
959         }
960         ret = dmaengine_slave_config(chan, &slave_config);
961         if (ret) {
962                 dev_err(asrc->dev, "imx_asrc_dma_config(%d) failed: %d\r\n", in, ret);
963                 return -EINVAL;
964         }
965
966         sg_init_table(sg, sg_nent);
967         switch (sg_nent) {
968         case 1:
969                 sg_init_one(sg, buf_addr, buf_len);
970                 break;
971         case 2:
972         case 3:
973         case 4:
974                 for (i = 0; i < (sg_nent - 1); i++)
975                         sg_set_buf(&sg[i], buf_addr + i * ASRC_MAX_BUFFER_SIZE,
976                                         ASRC_MAX_BUFFER_SIZE);
977
978                 sg_set_buf(&sg[i], buf_addr + i * ASRC_MAX_BUFFER_SIZE,
979                                 buf_len - ASRC_MAX_BUFFER_SIZE * i);
980                 break;
981         default:
982                 dev_err(asrc->dev, "Error Input DMA nodes number[%d]!\n", sg_nent);
983                 return -EINVAL;
984         }
985
986         ret = dma_map_sg(NULL, sg, sg_nent, slave_config.direction);
987         if (ret != sg_nent) {
988                 dev_err(asrc->dev, "DMA mapping error!\n");
989                 return -EINVAL;
990         }
991
992         desc = chan->device->device_prep_slave_sg(chan, sg, sg_nent,
993                         slave_config.direction, 1, NULL);
994
995         if (in) {
996                 params->desc_in = desc;
997                 params->desc_in->callback = asrc_input_dma_callback;
998         } else {
999                 params->desc_out = desc;
1000                 params->desc_out->callback = asrc_output_dma_callback;
1001         }
1002
1003         if (desc) {
1004                 desc->callback = ASRC_xPUT_DMA_CALLBACK(in);
1005                 desc->callback_param = params;
1006         } else {
1007                 return -EINVAL;
1008         }
1009
1010         return 0;
1011 }
1012
1013 static int mxc_asrc_prepare_io_buffer(struct asrc_pair_params *params,
1014                 struct asrc_convert_buffer *pbuf, bool in)
1015 {
1016         struct dma_chan *dma_channel;
1017         enum asrc_word_width width;
1018         unsigned int *dma_len, *sg_nodes, buf_len, wm;
1019         void __user *buf_vaddr;
1020         void *dma_vaddr;
1021         u32 word_size, fifo_addr;
1022
1023         if (in) {
1024                 dma_channel = params->input_dma_channel;
1025                 dma_vaddr = params->input_dma_total.dma_vaddr;
1026                 dma_len = &params->input_dma_total.length;
1027                 width = params->input_word_width;
1028                 sg_nodes = &params->input_sg_nodes;
1029                 wm = params->input_wm;
1030                 buf_vaddr = (void __user *)pbuf->input_buffer_vaddr;
1031                 buf_len = pbuf->input_buffer_length;
1032         } else {
1033                 dma_channel = params->output_dma_channel;
1034                 dma_vaddr = params->output_dma_total.dma_vaddr;
1035                 dma_len = &params->output_dma_total.length;
1036                 width = params->output_word_width;
1037                 sg_nodes = &params->output_sg_nodes;
1038                 wm = params->last_period_sample;
1039                 buf_vaddr = (void __user *)pbuf->output_buffer_vaddr;
1040                 buf_len = pbuf->output_buffer_length;
1041         }
1042
1043         switch (width) {
1044         case ASRC_WIDTH_24_BIT:
1045                 word_size = 4;
1046                 break;
1047         case ASRC_WIDTH_16_BIT:
1048         case ASRC_WIDTH_8_BIT:
1049                 word_size = 2;
1050                 break;
1051         default:
1052                 dev_err(asrc->dev, "error %s word size!\n", in ? "input" : "output");
1053                 return -EINVAL;
1054         }
1055
1056         if (buf_len < word_size * params->channel_nums * wm) {
1057                 dev_err(asrc->dev, "%s buffer size[%d] is too small!\n",
1058                                 in ? "input" : "output", buf_len);
1059                 return -EINVAL;
1060         }
1061
1062         /* Copy origin data into input buffer */
1063         if (in && copy_from_user(dma_vaddr, buf_vaddr, buf_len))
1064                 return -EFAULT;
1065
1066         *dma_len = buf_len;
1067         if (!in)
1068                 *dma_len -= wm * word_size * params->channel_nums;
1069
1070         *sg_nodes = *dma_len / ASRC_MAX_BUFFER_SIZE + 1;
1071
1072         fifo_addr = asrc_get_per_addr(params->index, in);
1073
1074         return imx_asrc_dma_config(params, dma_channel, fifo_addr, dma_vaddr,
1075                         *dma_len, in, width);
1076 }
1077
1078 static int mxc_asrc_prepare_buffer(struct asrc_pair_params *params,
1079                 struct asrc_convert_buffer *pbuf)
1080 {
1081         int ret;
1082
1083         ret = mxc_asrc_prepare_io_buffer(params, pbuf, true);
1084         if (ret) {
1085                 dev_err(asrc->dev, "failed to prepare input buffer: %d\n", ret);
1086                 return ret;
1087         }
1088
1089         ret = mxc_asrc_prepare_io_buffer(params, pbuf, false);
1090         if (ret) {
1091                 dev_err(asrc->dev, "failed to prepare output buffer: %d\n", ret);
1092                 return ret;
1093         }
1094
1095         return 0;
1096 }
1097
1098 int mxc_asrc_process_io_buffer(struct asrc_pair_params *params,
1099                 struct asrc_convert_buffer *pbuf, bool in)
1100 {
1101         void *last_vaddr = params->output_last_period.dma_vaddr;
1102         unsigned int *last_len = &params->output_last_period.length;
1103         unsigned int *counter, dma_len, *buf_len;
1104         unsigned long lock_flags;
1105         void __user *buf_vaddr;
1106         void *dma_vaddr;
1107         wait_queue_head_t *q;
1108         spinlock_t *lock;
1109
1110         if (in) {
1111                 dma_vaddr = params->input_dma_total.dma_vaddr;
1112                 dma_len = params->input_dma_total.length;
1113                 q = &params->input_wait_queue;
1114                 counter = &params->input_counter;
1115                 buf_len = &pbuf->input_buffer_length;
1116                 lock = &input_int_lock;
1117                 buf_vaddr = (void __user *)pbuf->input_buffer_vaddr;
1118         } else {
1119                 dma_vaddr = params->output_dma_total.dma_vaddr;
1120                 dma_len = params->output_dma_total.length;
1121                 q = &params->output_wait_queue;
1122                 counter = &params->output_counter;
1123                 buf_len = &pbuf->output_buffer_length;
1124                 lock = &output_int_lock;
1125                 buf_vaddr = (void __user *)pbuf->output_buffer_vaddr;
1126         }
1127
1128         if (!wait_event_interruptible_timeout(*q, *counter != 0, 10 * HZ)) {
1129                 dev_err(asrc->dev, "ASRC_DQ_OUTBUF timeout counter %x\n", *counter);
1130                 return -ETIME;
1131         } else if (signal_pending(current)) {
1132                 dev_err(asrc->dev, "ASRC_DQ_INBUF interrupt received.\n");
1133                 return -ERESTARTSYS;
1134         }
1135
1136         spin_lock_irqsave(lock, lock_flags);
1137         (*counter)--;
1138         spin_unlock_irqrestore(lock, lock_flags);
1139
1140         *buf_len = dma_len;
1141
1142         /* Only output need return data to user space */
1143         if (!in) {
1144                 if (copy_to_user(buf_vaddr, dma_vaddr, dma_len))
1145                         return -EFAULT;
1146
1147                 *buf_len += *last_len;
1148
1149                 if (copy_to_user(buf_vaddr + dma_len, last_vaddr, *last_len))
1150                         return -EFAULT;
1151         }
1152
1153         return 0;
1154 }
1155
1156 int mxc_asrc_process_buffer(struct asrc_pair_params *params,
1157                 struct asrc_convert_buffer *pbuf)
1158 {
1159         int ret;
1160
1161         ret = mxc_asrc_process_io_buffer(params, pbuf, true);
1162         if (ret) {
1163                 dev_err(asrc->dev, "failed to process input buffer: %d\n", ret);
1164                 return ret;
1165         }
1166
1167         ret = mxc_asrc_process_io_buffer(params, pbuf, false);
1168         if (ret) {
1169                 dev_err(asrc->dev, "failed to process output buffer: %d\n", ret);
1170                 return ret;
1171         }
1172
1173         return 0;
1174 }
1175
1176 static void mxc_asrc_submit_dma(struct asrc_pair_params *params)
1177 {
1178         enum asrc_pair_index index = params->index;
1179         u32 size = asrc_get_output_FIFO_size(params->index);
1180         int i, j;
1181
1182         /* Read all data in OUTPUT FIFO */
1183         while (size) {
1184                 for (j = 0; j < size; j++)
1185                         for (i = 0; i < params->channel_nums; i++)
1186                                 asrc_read_one_from_output_FIFO(index);
1187                 /*
1188                  * delay before size-getting should be:
1189                  * 1s / output_sample_rate * last_period_sample
1190                  * but we don't know last time's output_sample_rate and
1191                  * last_period_sample, and in order to cover max case
1192                  * so use min(sample_rate)=32kHz and max(last_period_sample)=32
1193                  * Thus 1s / 32k * 32 = 1ms
1194                  */
1195                 mdelay(1);
1196
1197                 size = asrc_get_output_FIFO_size(index);
1198         }
1199
1200         /* Fill the input FIFO until reach the stall level */
1201         size = asrc_get_input_FIFO_size(index);
1202         while (size < 3) {
1203                 for (i = 0; i < params->channel_nums; i++)
1204                         asrc_write_one_to_output_FIFO(index, 0);
1205                 size = asrc_get_input_FIFO_size(index);
1206         }
1207
1208         /* Submit dma request */
1209         dmaengine_submit(params->desc_in);
1210         dma_async_issue_pending(params->desc_in->chan);
1211
1212         dmaengine_submit(params->desc_out);
1213         dma_async_issue_pending(params->desc_out->chan);
1214
1215         sdma_set_event_pending(params->input_dma_channel);
1216 }
1217
1218 static long asrc_ioctl_req_pair(struct asrc_pair_params *params,
1219                 void __user *user)
1220 {
1221         struct asrc_req req;
1222         long ret;
1223
1224         ret = copy_from_user(&req, user, sizeof(req));
1225         if (ret) {
1226                 dev_err(asrc->dev, "failed to get req from user space: %ld\n", ret);
1227                 return ret;
1228         }
1229
1230         ret = asrc_req_pair(req.chn_num, &req.index);
1231         if (ret) {
1232                 dev_err(asrc->dev, "failed to request pair: %ld\n", ret);
1233                 return ret;
1234         }
1235
1236         params->pair_hold = 1;
1237         params->index = req.index;
1238         params->channel_nums = req.chn_num;
1239
1240         ret = copy_to_user(user, &req, sizeof(req));
1241         if (ret) {
1242                 dev_err(asrc->dev, "failed to send req to user space: %ld\n", ret);
1243                 return ret;
1244         }
1245
1246         return 0;
1247 }
1248
1249 static long asrc_ioctl_config_pair(struct asrc_pair_params *params,
1250                 void __user *user)
1251 {
1252         struct asrc_config config;
1253         enum asrc_pair_index index;
1254         long ret;
1255
1256         ret = copy_from_user(&config, user, sizeof(config));
1257         if (ret) {
1258                 dev_err(asrc->dev, "failed to get config from user space: %ld\n", ret);
1259                 return ret;
1260         }
1261
1262         ret = asrc_config_pair(&config);
1263         if (ret) {
1264                 dev_err(asrc->dev, "failed to config pair: %ld\n", ret);
1265                 return ret;
1266         }
1267
1268         index = config.pair;
1269
1270         params->input_wm = 4;
1271         params->output_wm = 2;
1272
1273         ret = asrc_set_watermark(index, params->input_wm, params->output_wm);
1274         if (ret)
1275                 return ret;
1276
1277         params->output_buffer_size = config.dma_buffer_size;
1278         params->input_buffer_size = config.dma_buffer_size;
1279         if (config.buffer_num > ASRC_DMA_BUFFER_NUM)
1280                 params->buffer_num = ASRC_DMA_BUFFER_NUM;
1281         else
1282                 params->buffer_num = config.buffer_num;
1283
1284         params->input_dma_total.length = ASRC_DMA_BUFFER_SIZE;
1285         params->output_dma_total.length = ASRC_DMA_BUFFER_SIZE;
1286
1287         params->input_word_width = config.input_word_width;
1288         params->output_word_width = config.output_word_width;
1289
1290         params->input_sample_rate = config.input_sample_rate;
1291         params->output_sample_rate = config.output_sample_rate;
1292
1293         params->last_period_sample = ASRC_OUTPUT_LAST_SAMPLE_DEFAULT;
1294
1295         /* Expand last period buffer if output_sample_rate is much bigger */
1296         if (params->output_sample_rate / params->input_sample_rate > 2)
1297                 params->last_period_sample *= 5;
1298         else if (params->output_sample_rate / params->input_sample_rate > 1)
1299                 params->last_period_sample *= 3;
1300
1301         ret = mxc_allocate_dma_buf(params);
1302         if (ret) {
1303                 dev_err(asrc->dev, "failed to allocate dma buffer: %ld\n", ret);
1304                 return ret;
1305         }
1306
1307         /* Request DMA channel for both input and output */
1308         params->input_dma_channel =
1309                         imx_asrc_dma_request_channel(params, true);
1310         if (params->input_dma_channel == NULL) {
1311                 dev_err(asrc->dev, "failed to request rx channel for Pair %c\n",
1312                                 'A' + index);
1313                 return  -EBUSY;
1314         }
1315
1316         params->output_dma_channel =
1317                         imx_asrc_dma_request_channel(params, false);
1318         if (params->output_dma_channel == NULL) {
1319                 dev_err(asrc->dev, "failed to request tx channel for Pair %c\n",
1320                                 'A' + index);
1321                 return  -EBUSY;
1322         }
1323
1324         init_waitqueue_head(&params->input_wait_queue);
1325         init_waitqueue_head(&params->output_wait_queue);
1326
1327         /* Add work struct to receive last period of output data */
1328         INIT_WORK(&params->task_output_work, asrc_output_task_worker);
1329
1330         ret = copy_to_user(user, &config, sizeof(config));
1331         if (ret) {
1332                 dev_err(asrc->dev, "failed to send config to user space: %ld\n", ret);
1333                 return ret;
1334         }
1335
1336         return 0;
1337 }
1338
1339 static long asrc_ioctl_release_pair(struct asrc_pair_params *params,
1340                 void __user *user)
1341 {
1342         enum asrc_pair_index index;
1343         unsigned long lock_flags;
1344         long ret;
1345
1346         ret = copy_from_user(&index, user, sizeof(index));
1347         if (ret) {
1348                 dev_err(asrc->dev, "failed to get index from user space: %ld\n", ret);
1349                 return ret;
1350         }
1351
1352         /* index might be not valid due to some application failure. */
1353         if (index < 0)
1354                 return -EINVAL;
1355
1356         params->asrc_active = 0;
1357
1358         spin_lock_irqsave(&pair_lock, lock_flags);
1359         params->pair_hold = 0;
1360         spin_unlock_irqrestore(&pair_lock, lock_flags);
1361
1362         if (params->input_dma_channel)
1363                 dma_release_channel(params->input_dma_channel);
1364         if (params->output_dma_channel)
1365                 dma_release_channel(params->output_dma_channel);
1366         mxc_free_dma_buf(params);
1367         asrc_release_pair(index);
1368         asrc_finish_conv(index);
1369
1370         return 0;
1371 }
1372
1373 static long asrc_ioctl_convert(struct asrc_pair_params *params,
1374                 void __user *user)
1375 {
1376         struct asrc_convert_buffer buf;
1377         long ret;
1378
1379         ret = copy_from_user(&buf, user, sizeof(buf));
1380         if (ret) {
1381                 dev_err(asrc->dev, "failed to get buf from user space: %ld\n", ret);
1382                 return ret;
1383         }
1384
1385         ret = mxc_asrc_prepare_buffer(params, &buf);
1386         if (ret) {
1387                 dev_err(asrc->dev, "failed to prepare buffer: %ld\n", ret);
1388                 return ret;
1389         }
1390
1391         mxc_asrc_submit_dma(params);
1392
1393         ret = mxc_asrc_process_buffer(params, &buf);
1394         if (ret) {
1395                 dev_err(asrc->dev, "failed to process buffer: %ld\n", ret);
1396                 return ret;
1397         }
1398
1399         ret = copy_to_user(user, &buf, sizeof(buf));
1400         if (ret) {
1401                 dev_err(asrc->dev, "failed to send buf to user space: %ld\n", ret);
1402                 return ret;
1403         }
1404
1405         return 0;
1406 }
1407
1408 static long asrc_ioctl_start_conv(struct asrc_pair_params *params,
1409                 void __user *user)
1410 {
1411         enum asrc_pair_index index;
1412         long ret;
1413
1414         ret = copy_from_user(&index, user, sizeof(index));
1415         if (ret) {
1416                 dev_err(asrc->dev, "failed to get index from user space: %ld\n", ret);
1417                 return ret;
1418         }
1419
1420         params->asrc_active = 1;
1421         asrc_start_conv(index);
1422
1423         return 0;
1424 }
1425
1426 static long asrc_ioctl_stop_conv(struct asrc_pair_params *params,
1427                 void __user *user)
1428 {
1429         enum asrc_pair_index index;
1430         long ret;
1431
1432         ret = copy_from_user(&index, user, sizeof(index));
1433         if (ret) {
1434                 dev_err(asrc->dev, "failed to get index from user space: %ld\n", ret);
1435                 return ret;
1436         }
1437
1438         dmaengine_terminate_all(params->input_dma_channel);
1439         dmaengine_terminate_all(params->output_dma_channel);
1440
1441         asrc_stop_conv(index);
1442         params->asrc_active = 0;
1443
1444         return 0;
1445 }
1446
1447 static long asrc_ioctl_status(struct asrc_pair_params *params,
1448                 void __user *user)
1449 {
1450         struct asrc_status_flags flags;
1451         long ret;
1452
1453         ret = copy_from_user(&flags, user, sizeof(flags));
1454         if (ret) {
1455                 dev_err(asrc->dev, "failed to get flags from user space: %ld\n", ret);
1456                 return ret;
1457         }
1458
1459         asrc_get_status(&flags);
1460
1461         ret = copy_to_user(user, &flags, sizeof(flags));
1462         if (ret) {
1463                 dev_err(asrc->dev, "failed to send flags to user space: %ld\n", ret);
1464                 return ret;
1465         }
1466
1467         return 0;
1468 }
1469
1470 static long asrc_ioctl_flush(struct asrc_pair_params *params,
1471                 void __user *user)
1472 {
1473         enum asrc_pair_index index = params->index;
1474         unsigned long lock_flags;
1475
1476         /* Flush input dma buffer */
1477         spin_lock_irqsave(&input_int_lock, lock_flags);
1478         params->input_counter = 0;
1479         spin_unlock_irqrestore(&input_int_lock, lock_flags);
1480
1481         /* Flush output dma buffer */
1482         spin_lock_irqsave(&output_int_lock, lock_flags);
1483         params->output_counter = 0;
1484         spin_unlock_irqrestore(&output_int_lock, lock_flags);
1485
1486         /* Release DMA and request again */
1487         dma_release_channel(params->input_dma_channel);
1488         dma_release_channel(params->output_dma_channel);
1489
1490         params->input_dma_channel = imx_asrc_dma_request_channel(params, true);
1491         if (params->input_dma_channel == NULL) {
1492                 dev_err(asrc->dev, "failed to request rx channel for Pair %c\n",
1493                                 'A' + index);
1494                 return -EBUSY;
1495         }
1496
1497         params->output_dma_channel =
1498                         imx_asrc_dma_request_channel(params, false);
1499         if (params->output_dma_channel == NULL) {
1500                 dev_err(asrc->dev, "failed to request tx channel for Pair %c\n",
1501                                 'A' + index);
1502                 return -EBUSY;
1503         }
1504
1505         return 0;
1506 }
1507
1508 static long asrc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1509 {
1510         struct asrc_pair_params *params = file->private_data;
1511         void __user *user = (void __user *)arg;
1512         long ret = 0;
1513
1514         switch (cmd) {
1515         case ASRC_REQ_PAIR:
1516                 ret = asrc_ioctl_req_pair(params, user);
1517                 break;
1518         case ASRC_CONFIG_PAIR:
1519                 ret = asrc_ioctl_config_pair(params, user);
1520                 break;
1521         case ASRC_RELEASE_PAIR:
1522                 ret = asrc_ioctl_release_pair(params, user);
1523                 break;
1524         case ASRC_CONVERT:
1525                 ret = asrc_ioctl_convert(params, user);
1526                 break;
1527         case ASRC_START_CONV:
1528                 ret = asrc_ioctl_start_conv(params, user);
1529                 dump_regs();
1530                 break;
1531         case ASRC_STOP_CONV:
1532                 ret = asrc_ioctl_stop_conv(params, user);
1533                 break;
1534         case ASRC_STATUS:
1535                 ret = asrc_ioctl_status(params, user);
1536                 break;
1537         case ASRC_FLUSH:
1538                 ret = asrc_ioctl_flush(params, user);
1539                 break;
1540         default:
1541                 dev_err(asrc->dev, "Unsupported ioctl cmd!\n");
1542                 break;
1543         }
1544
1545         return ret;
1546 }
1547
1548 static int mxc_asrc_open(struct inode *inode, struct file *file)
1549 {
1550         struct asrc_pair_params *pair_params;
1551         int ret = 0;
1552
1553         ret = signal_pending(current);
1554         if (ret) {
1555                 dev_err(asrc->dev, "Current process has a signal pending.\n");
1556                 return ret;
1557         }
1558
1559         pair_params = kzalloc(sizeof(struct asrc_pair_params), GFP_KERNEL);
1560         if (pair_params == NULL) {
1561                 dev_err(asrc->dev, "failed to allocate pair_params.\n");
1562                 return -ENOBUFS;
1563         }
1564
1565         file->private_data = pair_params;
1566
1567         return ret;
1568 }
1569
1570 static int mxc_asrc_close(struct inode *inode, struct file *file)
1571 {
1572         struct asrc_pair_params *pair_params;
1573         unsigned long lock_flags;
1574
1575         pair_params = file->private_data;
1576
1577         if (!pair_params)
1578                 return 0;
1579
1580         if (pair_params->asrc_active) {
1581                 pair_params->asrc_active = 0;
1582
1583                 dmaengine_terminate_all(pair_params->input_dma_channel);
1584                 dmaengine_terminate_all(pair_params->output_dma_channel);
1585
1586                 asrc_stop_conv(pair_params->index);
1587
1588                 wake_up_interruptible(&pair_params->input_wait_queue);
1589                 wake_up_interruptible(&pair_params->output_wait_queue);
1590         }
1591         if (pair_params->pair_hold) {
1592                 spin_lock_irqsave(&pair_lock, lock_flags);
1593                 pair_params->pair_hold = 0;
1594                 spin_unlock_irqrestore(&pair_lock, lock_flags);
1595
1596                 if (pair_params->input_dma_channel)
1597                         dma_release_channel(pair_params->input_dma_channel);
1598                 if (pair_params->output_dma_channel)
1599                         dma_release_channel(pair_params->output_dma_channel);
1600
1601                 mxc_free_dma_buf(pair_params);
1602
1603                 asrc_release_pair(pair_params->index);
1604                 asrc_finish_conv(pair_params->index);
1605         }
1606
1607         kfree(pair_params);
1608         file->private_data = NULL;
1609
1610         return 0;
1611 }
1612
1613 static int mxc_asrc_mmap(struct file *file, struct vm_area_struct *vma)
1614 {
1615         unsigned long size = vma->vm_end - vma->vm_start;
1616         int ret;
1617
1618         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1619
1620         ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1621                         size, vma->vm_page_prot);
1622         if (ret) {
1623                 dev_err(asrc->dev, "failed to memory map!\n");
1624                 return ret;
1625         }
1626
1627         vma->vm_flags &= ~VM_IO;
1628
1629         return ret;
1630 }
1631
1632 static const struct file_operations asrc_fops = {
1633         .owner          = THIS_MODULE,
1634         .unlocked_ioctl = asrc_ioctl,
1635         .mmap           = mxc_asrc_mmap,
1636         .open           = mxc_asrc_open,
1637         .release        = mxc_asrc_close,
1638 };
1639
1640 static struct miscdevice asrc_miscdev = {
1641         .name   = "mxc_asrc",
1642         .fops   = &asrc_fops,
1643         .minor  = MISC_DYNAMIC_MINOR,
1644 };
1645
1646 static int asrc_read_proc_attr(struct file *file, char __user *buf,
1647                                 size_t count, loff_t *off)
1648 {
1649         char tmpbuf[80];
1650         int len = 0;
1651         u32 reg;
1652
1653         if (*off)
1654                 return 0;
1655
1656         asrc_regmap_read(asrc->regmap, REG_ASRCNCR, &reg);
1657
1658         len += sprintf(tmpbuf, "ANCA: %d\nANCB: %d\nANCC: %d\n",
1659                         (int)ASRCNCR_ANCA_get(reg, asrc->channel_bits),
1660                         (int)ASRCNCR_ANCB_get(reg, asrc->channel_bits),
1661                         (int)ASRCNCR_ANCC_get(reg, asrc->channel_bits));
1662
1663         if (len > count)
1664                 return 0;
1665
1666         if (copy_to_user(buf, &tmpbuf, len))
1667                 return -EFAULT;
1668
1669         *off += len;
1670
1671         return len;
1672 }
1673
1674 #define ASRC_MAX_PROC_BUFFER_SIZE 63
1675
1676 static int asrc_write_proc_attr(struct file *file, const char __user *buffer,
1677                                 size_t count, loff_t *data)
1678 {
1679         char buf[ASRC_MAX_PROC_BUFFER_SIZE];
1680         int na, nb, nc;
1681         int total;
1682
1683         if (count > ASRC_MAX_PROC_BUFFER_SIZE) {
1684                 dev_err(asrc->dev, "Attr proc write: The input string was too long.\n");
1685                 return -EINVAL;
1686         }
1687
1688         if (copy_from_user(buf, buffer, count)) {
1689                 dev_err(asrc->dev, "Attr proc write: Failed to copy buffer from user.\n");
1690                 return -EFAULT;
1691         }
1692
1693         sscanf(buf, "ANCA: %d\nANCB: %d\nANCC: %d", &na, &nb, &nc);
1694
1695         total = asrc->channel_bits > 3 ? 10 : 5;
1696
1697         if (na + nb + nc > total) {
1698                 dev_err(asrc->dev, "Don't surpass %d for total.\n", total);
1699                 return -EINVAL;
1700         } else if (na % 2 != 0 || nb % 2 != 0 || nc % 2 != 0) {
1701                 dev_err(asrc->dev, "Please set an even number for each pair.\n");
1702                 return -EINVAL;
1703         } else if (na < 0 || nb < 0 || nc < 0) {
1704                 dev_err(asrc->dev, "Please set an positive number for each pair.\n");
1705                 return -EINVAL;
1706         }
1707
1708
1709         asrc->asrc_pair[ASRC_PAIR_A].chn_max = na;
1710         asrc->asrc_pair[ASRC_PAIR_B].chn_max = nb;
1711         asrc->asrc_pair[ASRC_PAIR_C].chn_max = nc;
1712
1713         asrc_set_channel_number(ASRC_PAIR_A, na);
1714         asrc_set_channel_number(ASRC_PAIR_B, nb);
1715         asrc_set_channel_number(ASRC_PAIR_C, nc);
1716
1717         return count;
1718 }
1719
1720 static const struct file_operations asrc_proc_fops = {
1721         .read           = asrc_read_proc_attr,
1722         .write          = asrc_write_proc_attr,
1723 };
1724
1725 static void asrc_proc_create(void)
1726 {
1727         struct proc_dir_entry *proc_attr;
1728
1729         asrc->proc_asrc = proc_mkdir(ASRC_PROC_PATH, NULL);
1730         if (!asrc->proc_asrc) {
1731                 dev_err(asrc->dev, "failed to create proc entry %s\n", ASRC_PROC_PATH);
1732                 return;
1733         }
1734
1735         proc_attr = proc_create("ChSettings", S_IFREG | S_IRUGO | S_IWUSR,
1736                         asrc->proc_asrc, &asrc_proc_fops);
1737         if (!proc_attr) {
1738                 remove_proc_entry(ASRC_PROC_PATH, NULL);
1739                 dev_err(asrc->dev, "failed to create proc attribute entry.\n");
1740         }
1741 }
1742
1743 static void asrc_proc_remove(void)
1744 {
1745         remove_proc_entry("ChSettings", asrc->proc_asrc);
1746         remove_proc_entry(ASRC_PROC_PATH, NULL);
1747 }
1748
1749
1750 #ifdef ASRC_USE_REGMAP
1751 /* ============= ASRC REGMAP ============= */
1752
1753 static bool asrc_readable_reg(struct device *dev, unsigned int reg)
1754 {
1755         switch (reg) {
1756         case REG_ASRCTR:
1757         case REG_ASRIER:
1758         case REG_ASRCNCR:
1759         case REG_ASRCFG:
1760         case REG_ASRCSR:
1761         case REG_ASRCDR1:
1762         case REG_ASRCDR2:
1763         case REG_ASRSTR:
1764         case REG_ASRPM1:
1765         case REG_ASRPM2:
1766         case REG_ASRPM3:
1767         case REG_ASRPM4:
1768         case REG_ASRPM5:
1769         case REG_ASRTFR1:
1770         case REG_ASRCCR:
1771         case REG_ASRDOA:
1772         case REG_ASRDOB:
1773         case REG_ASRDOC:
1774         case REG_ASRIDRHA:
1775         case REG_ASRIDRLA:
1776         case REG_ASRIDRHB:
1777         case REG_ASRIDRLB:
1778         case REG_ASRIDRHC:
1779         case REG_ASRIDRLC:
1780         case REG_ASR76K:
1781         case REG_ASR56K:
1782         case REG_ASRMCRA:
1783         case REG_ASRFSTA:
1784         case REG_ASRMCRB:
1785         case REG_ASRFSTB:
1786         case REG_ASRMCRC:
1787         case REG_ASRFSTC:
1788         case REG_ASRMCR1A:
1789         case REG_ASRMCR1B:
1790         case REG_ASRMCR1C:
1791                 return true;
1792         default:
1793                 return false;
1794         }
1795 }
1796
1797 static bool asrc_writeable_reg(struct device *dev, unsigned int reg)
1798 {
1799         switch (reg) {
1800         case REG_ASRCTR:
1801         case REG_ASRIER:
1802         case REG_ASRCNCR:
1803         case REG_ASRCFG:
1804         case REG_ASRCSR:
1805         case REG_ASRCDR1:
1806         case REG_ASRCDR2:
1807         case REG_ASRPM1:
1808         case REG_ASRPM2:
1809         case REG_ASRPM3:
1810         case REG_ASRPM4:
1811         case REG_ASRPM5:
1812         case REG_ASRTFR1:
1813         case REG_ASRCCR:
1814         case REG_ASRDIA:
1815         case REG_ASRDIB:
1816         case REG_ASRDIC:
1817         case REG_ASRIDRHA:
1818         case REG_ASRIDRLA:
1819         case REG_ASRIDRHB:
1820         case REG_ASRIDRLB:
1821         case REG_ASRIDRHC:
1822         case REG_ASRIDRLC:
1823         case REG_ASR76K:
1824         case REG_ASR56K:
1825         case REG_ASRMCRA:
1826         case REG_ASRMCRB:
1827         case REG_ASRMCRC:
1828         case REG_ASRMCR1A:
1829         case REG_ASRMCR1B:
1830         case REG_ASRMCR1C:
1831                 return true;
1832         default:
1833                 return false;
1834         }
1835 }
1836
1837 static bool asrc_volatile_reg(struct device *dev, unsigned int reg)
1838 {
1839         /* Sync all registers after reset */
1840         return true;
1841 }
1842
1843 static const struct regmap_config asrc_regmap_config = {
1844         .reg_bits = 32,
1845         .reg_stride = 4,
1846         .val_bits = 32,
1847
1848         .max_register = REG_ASRMCR1C,
1849         .readable_reg = asrc_readable_reg,
1850         .writeable_reg = asrc_writeable_reg,
1851         .volatile_reg = asrc_volatile_reg,
1852         .cache_type = REGCACHE_RBTREE,
1853 };
1854 #endif
1855
1856 static int mxc_asrc_probe(struct platform_device *pdev)
1857 {
1858         struct device_node *np = pdev->dev.of_node;
1859         struct resource res;
1860         void __iomem *regs;
1861         int ret;
1862
1863         /* Check if the device is existed */
1864         if (!of_device_is_available(np)) {
1865                 dev_err(&pdev->dev, "improper devicetree status.\n");
1866                 return -ENODEV;
1867         }
1868
1869         asrc = devm_kzalloc(&pdev->dev, sizeof(struct asrc_data), GFP_KERNEL);
1870         if (asrc == NULL) {
1871                 dev_err(&pdev->dev, "failed to allocate asrc.\n");
1872                 return -ENOMEM;
1873         }
1874
1875         asrc->dev = &pdev->dev;
1876         asrc->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1877
1878         asrc->asrc_pair[ASRC_PAIR_A].chn_max = 2;
1879         asrc->asrc_pair[ASRC_PAIR_B].chn_max = 6;
1880         asrc->asrc_pair[ASRC_PAIR_C].chn_max = 2;
1881         asrc->asrc_pair[ASRC_PAIR_A].overload_error = 0;
1882         asrc->asrc_pair[ASRC_PAIR_B].overload_error = 0;
1883         asrc->asrc_pair[ASRC_PAIR_C].overload_error = 0;
1884
1885         /* Map the address */
1886         ret = of_address_to_resource(np, 0, &res);
1887         if (ret) {
1888                 dev_err(&pdev->dev, "failed to map address: %d\n", ret);
1889                 return ret;
1890         }
1891         asrc->paddr = res.start;
1892
1893         regs = of_iomap(np, 0);
1894         if (IS_ERR(regs)) {
1895                 dev_err(&pdev->dev, "failed to map io resources.\n");
1896                 return IS_ERR(regs);
1897         }
1898         asrc->vaddr = (unsigned long)regs;
1899
1900 #ifdef ASRC_USE_REGMAP
1901         asrc->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1902                         "core", regs, &asrc_regmap_config);
1903         if (IS_ERR(asrc->regmap)) {
1904                 dev_err(&pdev->dev, "regmap init failed\n");
1905                 ret = PTR_ERR(asrc->regmap);
1906         }
1907
1908         regcache_cache_only(asrc->regmap, false);
1909 #endif
1910
1911         asrc->irq = irq_of_parse_and_map(np, 0);
1912         if (asrc->irq == NO_IRQ) {
1913                 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
1914                 goto err_iomap;
1915         }
1916
1917         ret = devm_request_irq(&pdev->dev, asrc->irq,
1918                         asrc_isr, 0, "asrc", NULL);
1919         if (ret) {
1920                 dev_err(&pdev->dev, "failed to request IRQ: %d\n", ret);
1921                 goto err_iomap;
1922         }
1923
1924         asrc->asrc_clk = devm_clk_get(&pdev->dev, NULL);
1925         if (IS_ERR(asrc->asrc_clk)) {
1926                 ret = PTR_ERR(asrc->asrc_clk);
1927                 goto err_iomap;
1928         }
1929 #ifndef ASRC_USE_REGMAP
1930         clk_prepare_enable(asrc->asrc_clk);
1931 #endif
1932
1933         ret = of_property_read_u32_array(pdev->dev.of_node,
1934                         "fsl,clk-channel-bits", &asrc->channel_bits, 1);
1935         if (ret) {
1936                 dev_err(&pdev->dev, "failed to get clk-channel-bits.\n");
1937                 goto err_iomap;
1938         }
1939
1940         ret = of_property_read_u32_array(pdev->dev.of_node,
1941                         "fsl,clk-map-version", &asrc->clk_map_ver, 1);
1942         if (ret) {
1943                 dev_err(&pdev->dev, "failed to get clk-map-version.\n");
1944                 goto err_iomap;
1945         }
1946
1947         switch (asrc->clk_map_ver) {
1948         case 1:
1949                 input_clk_map = input_clk_map_v1;
1950                 output_clk_map = output_clk_map_v1;
1951                 break;
1952         case 2:
1953         default:
1954                 input_clk_map = input_clk_map_v2;
1955                 output_clk_map = output_clk_map_v2;
1956                 break;
1957         }
1958
1959         ret = misc_register(&asrc_miscdev);
1960         if (ret) {
1961                 dev_err(&pdev->dev, "failed to register char device %d\n", ret);
1962                 goto err_iomap;
1963         }
1964
1965         asrc_proc_create();
1966
1967         ret = mxc_init_asrc();
1968         if (ret)
1969                 goto err_misc;
1970
1971         dev_info(&pdev->dev, "mxc_asrc registered.\n");
1972
1973         return ret;
1974
1975 err_misc:
1976         misc_deregister(&asrc_miscdev);
1977 err_iomap:
1978         iounmap(regs);
1979
1980         dev_err(&pdev->dev, "mxc_asrc register failed: err %d\n", ret);
1981
1982         return ret;
1983 }
1984
1985 static int mxc_asrc_remove(struct platform_device *pdev)
1986 {
1987         asrc_proc_remove();
1988         misc_deregister(&asrc_miscdev);
1989
1990         return 0;
1991 }
1992
1993 static const struct of_device_id fsl_asrc_ids[] = {
1994         { .compatible = "fsl,imx6q-asrc", },
1995         {}
1996 };
1997
1998 static struct platform_driver mxc_asrc_driver = {
1999         .driver = {
2000                 .name = "mxc_asrc",
2001                 .of_match_table = fsl_asrc_ids,
2002         },
2003         .probe = mxc_asrc_probe,
2004         .remove = mxc_asrc_remove,
2005 };
2006
2007 module_platform_driver(mxc_asrc_driver);
2008
2009 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2010 MODULE_DESCRIPTION("Asynchronous Sample Rate Converter");
2011 MODULE_LICENSE("GPL");