]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/mxc/asrc/mxc_asrc.c
ENGR00274286-2 mxc: asrc: Add asrc m2m driver
[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         if (in) {
945                 slave_config.direction = DMA_MEM_TO_DEV;
946                 slave_config.dst_addr = dma_addr;
947                 slave_config.dst_addr_width = buswidth;
948                 slave_config.dst_maxburst =
949                         params->input_wm * params->channel_nums / buswidth;
950         } else {
951                 slave_config.direction = DMA_DEV_TO_MEM;
952                 slave_config.src_addr = dma_addr;
953                 slave_config.src_addr_width = buswidth;
954                 slave_config.src_maxburst =
955                         params->output_wm * params->channel_nums / buswidth;
956         }
957         ret = dmaengine_slave_config(chan, &slave_config);
958         if (ret) {
959                 dev_err(asrc->dev, "imx_asrc_dma_config(%d) failed: %d\r\n", in, ret);
960                 return -EINVAL;
961         }
962
963         sg_init_table(sg, sg_nent);
964         switch (sg_nent) {
965         case 1:
966                 sg_init_one(sg, buf_addr, buf_len);
967                 break;
968         case 2:
969         case 3:
970         case 4:
971                 for (i = 0; i < (sg_nent - 1); i++)
972                         sg_set_buf(&sg[i], buf_addr + i * ASRC_MAX_BUFFER_SIZE,
973                                         ASRC_MAX_BUFFER_SIZE);
974
975                 sg_set_buf(&sg[i], buf_addr + i * ASRC_MAX_BUFFER_SIZE,
976                                 buf_len - ASRC_MAX_BUFFER_SIZE * i);
977                 break;
978         default:
979                 dev_err(asrc->dev, "Error Input DMA nodes number[%d]!\n", sg_nent);
980                 return -EINVAL;
981         }
982
983         ret = dma_map_sg(NULL, sg, sg_nent, slave_config.direction);
984         if (ret != sg_nent) {
985                 dev_err(asrc->dev, "DMA mapping error!\n");
986                 return -EINVAL;
987         }
988
989         desc = chan->device->device_prep_slave_sg(chan, sg, sg_nent,
990                         slave_config.direction, 1, NULL);
991
992         if (in) {
993                 params->desc_in = desc;
994                 params->desc_in->callback = asrc_input_dma_callback;
995         } else {
996                 params->desc_out = desc;
997                 params->desc_out->callback = asrc_output_dma_callback;
998         }
999
1000         if (desc) {
1001                 desc->callback = ASRC_xPUT_DMA_CALLBACK(in);
1002                 desc->callback_param = params;
1003         } else {
1004                 return -EINVAL;
1005         }
1006
1007         return 0;
1008 }
1009
1010 static int mxc_asrc_prepare_io_buffer(struct asrc_pair_params *params,
1011                 struct asrc_convert_buffer *pbuf, bool in)
1012 {
1013         struct dma_chan *dma_channel;
1014         enum asrc_word_width width;
1015         unsigned int *dma_len, *sg_nodes, buf_len, wm;
1016         void __user *buf_vaddr;
1017         void *dma_vaddr;
1018         u32 word_size, fifo_addr;
1019
1020         if (in) {
1021                 dma_channel = params->input_dma_channel;
1022                 dma_vaddr = params->input_dma_total.dma_vaddr;
1023                 dma_len = &params->input_dma_total.length;
1024                 width = params->input_word_width;
1025                 sg_nodes = &params->input_sg_nodes;
1026                 wm = params->input_wm;
1027                 buf_vaddr = (void __user *)pbuf->input_buffer_vaddr;
1028                 buf_len = pbuf->input_buffer_length;
1029         } else {
1030                 dma_channel = params->output_dma_channel;
1031                 dma_vaddr = params->output_dma_total.dma_vaddr;
1032                 dma_len = &params->output_dma_total.length;
1033                 width = params->output_word_width;
1034                 sg_nodes = &params->output_sg_nodes;
1035                 wm = params->last_period_sample;
1036                 buf_vaddr = (void __user *)pbuf->output_buffer_vaddr;
1037                 buf_len = pbuf->output_buffer_length;
1038         }
1039
1040         switch (width) {
1041         case ASRC_WIDTH_24_BIT:
1042                 word_size = 4;
1043                 break;
1044         case ASRC_WIDTH_16_BIT:
1045         case ASRC_WIDTH_8_BIT:
1046                 word_size = 2;
1047                 break;
1048         default:
1049                 dev_err(asrc->dev, "error %s word size!\n", in ? "input" : "output");
1050                 return -EINVAL;
1051         }
1052
1053         if (buf_len < word_size * params->channel_nums * wm) {
1054                 dev_err(asrc->dev, "%s buffer size[%d] is too small!\n",
1055                                 in ? "input" : "output", buf_len);
1056                 return -EINVAL;
1057         }
1058
1059         /* Copy origin data into input buffer */
1060         if (in && copy_from_user(dma_vaddr, buf_vaddr, buf_len))
1061                 return -EFAULT;
1062
1063         *dma_len = buf_len;
1064         if (!in)
1065                 *dma_len -= wm * word_size * params->channel_nums;
1066
1067         *sg_nodes = *dma_len / ASRC_MAX_BUFFER_SIZE + 1;
1068
1069         fifo_addr = asrc_get_per_addr(params->index, in);
1070
1071         return imx_asrc_dma_config(params, dma_channel, fifo_addr, dma_vaddr,
1072                         *dma_len, in, width);
1073 }
1074
1075 static int mxc_asrc_prepare_buffer(struct asrc_pair_params *params,
1076                 struct asrc_convert_buffer *pbuf)
1077 {
1078         int ret;
1079
1080         ret = mxc_asrc_prepare_io_buffer(params, pbuf, true);
1081         if (ret) {
1082                 dev_err(asrc->dev, "failed to prepare input buffer: %d\n", ret);
1083                 return ret;
1084         }
1085
1086         ret = mxc_asrc_prepare_io_buffer(params, pbuf, false);
1087         if (ret) {
1088                 dev_err(asrc->dev, "failed to prepare output buffer: %d\n", ret);
1089                 return ret;
1090         }
1091
1092         return 0;
1093 }
1094
1095 int mxc_asrc_process_io_buffer(struct asrc_pair_params *params,
1096                 struct asrc_convert_buffer *pbuf, bool in)
1097 {
1098         void *last_vaddr = params->output_last_period.dma_vaddr;
1099         unsigned int *last_len = &params->output_last_period.length;
1100         unsigned int *counter, dma_len, *buf_len;
1101         unsigned long lock_flags;
1102         void __user *buf_vaddr;
1103         void *dma_vaddr;
1104         wait_queue_head_t *q;
1105         spinlock_t *lock;
1106
1107         if (in) {
1108                 dma_vaddr = params->input_dma_total.dma_vaddr;
1109                 dma_len = params->input_dma_total.length;
1110                 q = &params->input_wait_queue;
1111                 counter = &params->input_counter;
1112                 buf_len = &pbuf->input_buffer_length;
1113                 lock = &input_int_lock;
1114                 buf_vaddr = (void __user *)pbuf->input_buffer_vaddr;
1115         } else {
1116                 dma_vaddr = params->output_dma_total.dma_vaddr;
1117                 dma_len = params->output_dma_total.length;
1118                 q = &params->output_wait_queue;
1119                 counter = &params->output_counter;
1120                 buf_len = &pbuf->output_buffer_length;
1121                 lock = &output_int_lock;
1122                 buf_vaddr = (void __user *)pbuf->output_buffer_vaddr;
1123         }
1124
1125         if (!wait_event_interruptible_timeout(*q, *counter != 0, 10 * HZ)) {
1126                 dev_err(asrc->dev, "ASRC_DQ_OUTBUF timeout counter %x\n", *counter);
1127                 return -ETIME;
1128         } else if (signal_pending(current)) {
1129                 dev_err(asrc->dev, "ASRC_DQ_INBUF interrupt received.\n");
1130                 return -ERESTARTSYS;
1131         }
1132
1133         spin_lock_irqsave(lock, lock_flags);
1134         (*counter)--;
1135         spin_unlock_irqrestore(lock, lock_flags);
1136
1137         *buf_len = dma_len;
1138
1139         /* Only output need return data to user space */
1140         if (!in) {
1141                 if (copy_to_user(buf_vaddr, dma_vaddr, dma_len))
1142                         return -EFAULT;
1143
1144                 *buf_len += *last_len;
1145
1146                 if (copy_to_user(buf_vaddr + dma_len, last_vaddr, *last_len))
1147                         return -EFAULT;
1148         }
1149
1150         return 0;
1151 }
1152
1153 int mxc_asrc_process_buffer(struct asrc_pair_params *params,
1154                 struct asrc_convert_buffer *pbuf)
1155 {
1156         int ret;
1157
1158         ret = mxc_asrc_process_io_buffer(params, pbuf, true);
1159         if (ret) {
1160                 dev_err(asrc->dev, "failed to process input buffer: %d\n", ret);
1161                 return ret;
1162         }
1163
1164         ret = mxc_asrc_process_io_buffer(params, pbuf, false);
1165         if (ret) {
1166                 dev_err(asrc->dev, "failed to process output buffer: %d\n", ret);
1167                 return ret;
1168         }
1169
1170         return 0;
1171 }
1172
1173 static void mxc_asrc_submit_dma(struct asrc_pair_params *params)
1174 {
1175         enum asrc_pair_index index = params->index;
1176         u32 size = asrc_get_output_FIFO_size(params->index);
1177         int i, j;
1178
1179         /* Read all data in OUTPUT FIFO */
1180         while (size) {
1181                 for (j = 0; j < size; j++)
1182                         for (i = 0; i < params->channel_nums; i++)
1183                                 asrc_read_one_from_output_FIFO(index);
1184                 /*
1185                  * delay before size-getting should be:
1186                  * 1s / output_sample_rate * last_period_sample
1187                  * but we don't know last time's output_sample_rate and
1188                  * last_period_sample, and in order to cover max case
1189                  * so use min(sample_rate)=32kHz and max(last_period_sample)=32
1190                  * Thus 1s / 32k * 32 = 1ms
1191                  */
1192                 mdelay(1);
1193
1194                 size = asrc_get_output_FIFO_size(index);
1195         }
1196
1197         /* Fill the input FIFO until reach the stall level */
1198         size = asrc_get_input_FIFO_size(index);
1199         while (size < 3) {
1200                 for (i = 0; i < params->channel_nums; i++)
1201                         asrc_write_one_to_output_FIFO(index, 0);
1202                 size = asrc_get_input_FIFO_size(index);
1203         }
1204
1205         /* Submit dma request */
1206         dmaengine_submit(params->desc_in);
1207         dma_async_issue_pending(params->desc_in->chan);
1208
1209         dmaengine_submit(params->desc_out);
1210         dma_async_issue_pending(params->desc_out->chan);
1211
1212         sdma_set_event_pending(params->input_dma_channel);
1213 }
1214
1215 static long asrc_ioctl_req_pair(struct asrc_pair_params *params,
1216                 void __user *user)
1217 {
1218         struct asrc_req req;
1219         long ret;
1220
1221         ret = copy_from_user(&req, user, sizeof(req));
1222         if (ret) {
1223                 dev_err(asrc->dev, "failed to get req from user space: %ld\n", ret);
1224                 return ret;
1225         }
1226
1227         ret = asrc_req_pair(req.chn_num, &req.index);
1228         if (ret) {
1229                 dev_err(asrc->dev, "failed to request pair: %ld\n", ret);
1230                 return ret;
1231         }
1232
1233         params->pair_hold = 1;
1234         params->index = req.index;
1235         params->channel_nums = req.chn_num;
1236
1237         ret = copy_to_user(user, &req, sizeof(req));
1238         if (ret) {
1239                 dev_err(asrc->dev, "failed to send req to user space: %ld\n", ret);
1240                 return ret;
1241         }
1242
1243         return 0;
1244 }
1245
1246 static long asrc_ioctl_config_pair(struct asrc_pair_params *params,
1247                 void __user *user)
1248 {
1249         struct asrc_config config;
1250         enum asrc_pair_index index;
1251         long ret;
1252
1253         ret = copy_from_user(&config, user, sizeof(config));
1254         if (ret) {
1255                 dev_err(asrc->dev, "failed to get config from user space: %ld\n", ret);
1256                 return ret;
1257         }
1258
1259         ret = asrc_config_pair(&config);
1260         if (ret) {
1261                 dev_err(asrc->dev, "failed to config pair: %ld\n", ret);
1262                 return ret;
1263         }
1264
1265         index = config.pair;
1266
1267         params->input_wm = 4;
1268         params->output_wm = 2;
1269
1270         ret = asrc_set_watermark(index, params->input_wm, params->output_wm);
1271         if (ret)
1272                 return ret;
1273
1274         params->output_buffer_size = config.dma_buffer_size;
1275         params->input_buffer_size = config.dma_buffer_size;
1276         if (config.buffer_num > ASRC_DMA_BUFFER_NUM)
1277                 params->buffer_num = ASRC_DMA_BUFFER_NUM;
1278         else
1279                 params->buffer_num = config.buffer_num;
1280
1281         params->input_dma_total.length = ASRC_DMA_BUFFER_SIZE;
1282         params->output_dma_total.length = ASRC_DMA_BUFFER_SIZE;
1283
1284         params->input_word_width = config.input_word_width;
1285         params->output_word_width = config.output_word_width;
1286
1287         params->input_sample_rate = config.input_sample_rate;
1288         params->output_sample_rate = config.output_sample_rate;
1289
1290         params->last_period_sample = ASRC_OUTPUT_LAST_SAMPLE_DEFAULT;
1291
1292         /* Expand last period buffer if output_sample_rate is much bigger */
1293         if (params->output_sample_rate / params->input_sample_rate > 2)
1294                 params->last_period_sample *= 5;
1295         else if (params->output_sample_rate / params->input_sample_rate > 1)
1296                 params->last_period_sample *= 3;
1297
1298         ret = mxc_allocate_dma_buf(params);
1299         if (ret) {
1300                 dev_err(asrc->dev, "failed to allocate dma buffer: %ld\n", ret);
1301                 return ret;
1302         }
1303
1304         /* Request DMA channel for both input and output */
1305         params->input_dma_channel =
1306                         imx_asrc_dma_request_channel(params, true);
1307         if (params->input_dma_channel == NULL) {
1308                 dev_err(asrc->dev, "failed to request rx channel for Pair %c\n",
1309                                 'A' + index);
1310                 return  -EBUSY;
1311         }
1312
1313         params->output_dma_channel =
1314                         imx_asrc_dma_request_channel(params, false);
1315         if (params->output_dma_channel == NULL) {
1316                 dev_err(asrc->dev, "failed to request tx channel for Pair %c\n",
1317                                 'A' + index);
1318                 return  -EBUSY;
1319         }
1320
1321         init_waitqueue_head(&params->input_wait_queue);
1322         init_waitqueue_head(&params->output_wait_queue);
1323
1324         /* Add work struct to receive last period of output data */
1325         INIT_WORK(&params->task_output_work, asrc_output_task_worker);
1326
1327         ret = copy_to_user(user, &config, sizeof(config));
1328         if (ret) {
1329                 dev_err(asrc->dev, "failed to send config to user space: %ld\n", ret);
1330                 return ret;
1331         }
1332
1333         return 0;
1334 }
1335
1336 static long asrc_ioctl_release_pair(struct asrc_pair_params *params,
1337                 void __user *user)
1338 {
1339         enum asrc_pair_index index;
1340         unsigned long lock_flags;
1341         long ret;
1342
1343         ret = copy_from_user(&index, user, sizeof(index));
1344         if (ret) {
1345                 dev_err(asrc->dev, "failed to get index from user space: %ld\n", ret);
1346                 return ret;
1347         }
1348
1349         /* index might be not valid due to some application failure. */
1350         if (index < 0)
1351                 return -EINVAL;
1352
1353         params->asrc_active = 0;
1354
1355         spin_lock_irqsave(&pair_lock, lock_flags);
1356         params->pair_hold = 0;
1357         spin_unlock_irqrestore(&pair_lock, lock_flags);
1358
1359         if (params->input_dma_channel)
1360                 dma_release_channel(params->input_dma_channel);
1361         if (params->output_dma_channel)
1362                 dma_release_channel(params->output_dma_channel);
1363         mxc_free_dma_buf(params);
1364         asrc_release_pair(index);
1365         asrc_finish_conv(index);
1366
1367         return 0;
1368 }
1369
1370 static long asrc_ioctl_convert(struct asrc_pair_params *params,
1371                 void __user *user)
1372 {
1373         struct asrc_convert_buffer buf;
1374         long ret;
1375
1376         ret = copy_from_user(&buf, user, sizeof(buf));
1377         if (ret) {
1378                 dev_err(asrc->dev, "failed to get buf from user space: %ld\n", ret);
1379                 return ret;
1380         }
1381
1382         ret = mxc_asrc_prepare_buffer(params, &buf);
1383         if (ret) {
1384                 dev_err(asrc->dev, "failed to prepare buffer: %ld\n", ret);
1385                 return ret;
1386         }
1387
1388         mxc_asrc_submit_dma(params);
1389
1390         ret = mxc_asrc_process_buffer(params, &buf);
1391         if (ret) {
1392                 dev_err(asrc->dev, "failed to process buffer: %ld\n", ret);
1393                 return ret;
1394         }
1395
1396         ret = copy_to_user(user, &buf, sizeof(buf));
1397         if (ret) {
1398                 dev_err(asrc->dev, "failed to send buf to user space: %ld\n", ret);
1399                 return ret;
1400         }
1401
1402         return 0;
1403 }
1404
1405 static long asrc_ioctl_start_conv(struct asrc_pair_params *params,
1406                 void __user *user)
1407 {
1408         enum asrc_pair_index index;
1409         long ret;
1410
1411         ret = copy_from_user(&index, user, sizeof(index));
1412         if (ret) {
1413                 dev_err(asrc->dev, "failed to get index from user space: %ld\n", ret);
1414                 return ret;
1415         }
1416
1417         params->asrc_active = 1;
1418         asrc_start_conv(index);
1419
1420         return 0;
1421 }
1422
1423 static long asrc_ioctl_stop_conv(struct asrc_pair_params *params,
1424                 void __user *user)
1425 {
1426         enum asrc_pair_index index;
1427         long ret;
1428
1429         ret = copy_from_user(&index, user, sizeof(index));
1430         if (ret) {
1431                 dev_err(asrc->dev, "failed to get index from user space: %ld\n", ret);
1432                 return ret;
1433         }
1434
1435         dmaengine_terminate_all(params->input_dma_channel);
1436         dmaengine_terminate_all(params->output_dma_channel);
1437
1438         asrc_stop_conv(index);
1439         params->asrc_active = 0;
1440
1441         return 0;
1442 }
1443
1444 static long asrc_ioctl_status(struct asrc_pair_params *params,
1445                 void __user *user)
1446 {
1447         struct asrc_status_flags flags;
1448         long ret;
1449
1450         ret = copy_from_user(&flags, user, sizeof(flags));
1451         if (ret) {
1452                 dev_err(asrc->dev, "failed to get flags from user space: %ld\n", ret);
1453                 return ret;
1454         }
1455
1456         asrc_get_status(&flags);
1457
1458         ret = copy_to_user(user, &flags, sizeof(flags));
1459         if (ret) {
1460                 dev_err(asrc->dev, "failed to send flags to user space: %ld\n", ret);
1461                 return ret;
1462         }
1463
1464         return 0;
1465 }
1466
1467 static long asrc_ioctl_flush(struct asrc_pair_params *params,
1468                 void __user *user)
1469 {
1470         enum asrc_pair_index index = params->index;
1471         unsigned long lock_flags;
1472
1473         /* Flush input dma buffer */
1474         spin_lock_irqsave(&input_int_lock, lock_flags);
1475         params->input_counter = 0;
1476         spin_unlock_irqrestore(&input_int_lock, lock_flags);
1477
1478         /* Flush output dma buffer */
1479         spin_lock_irqsave(&output_int_lock, lock_flags);
1480         params->output_counter = 0;
1481         spin_unlock_irqrestore(&output_int_lock, lock_flags);
1482
1483         /* Release DMA and request again */
1484         dma_release_channel(params->input_dma_channel);
1485         dma_release_channel(params->output_dma_channel);
1486
1487         params->input_dma_channel = imx_asrc_dma_request_channel(params, true);
1488         if (params->input_dma_channel == NULL) {
1489                 dev_err(asrc->dev, "failed to request rx channel for Pair %c\n",
1490                                 'A' + index);
1491                 return -EBUSY;
1492         }
1493
1494         params->output_dma_channel =
1495                         imx_asrc_dma_request_channel(params, false);
1496         if (params->output_dma_channel == NULL) {
1497                 dev_err(asrc->dev, "failed to request tx channel for Pair %c\n",
1498                                 'A' + index);
1499                 return -EBUSY;
1500         }
1501
1502         return 0;
1503 }
1504
1505 static long asrc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1506 {
1507         struct asrc_pair_params *params = file->private_data;
1508         void __user *user = (void __user *)arg;
1509         long ret = 0;
1510
1511         switch (cmd) {
1512         case ASRC_REQ_PAIR:
1513                 ret = asrc_ioctl_req_pair(params, user);
1514                 break;
1515         case ASRC_CONFIG_PAIR:
1516                 ret = asrc_ioctl_config_pair(params, user);
1517                 break;
1518         case ASRC_RELEASE_PAIR:
1519                 ret = asrc_ioctl_release_pair(params, user);
1520                 break;
1521         case ASRC_CONVERT:
1522                 ret = asrc_ioctl_convert(params, user);
1523                 break;
1524         case ASRC_START_CONV:
1525                 ret = asrc_ioctl_start_conv(params, user);
1526                 dump_regs();
1527                 break;
1528         case ASRC_STOP_CONV:
1529                 ret = asrc_ioctl_stop_conv(params, user);
1530                 break;
1531         case ASRC_STATUS:
1532                 ret = asrc_ioctl_status(params, user);
1533                 break;
1534         case ASRC_FLUSH:
1535                 ret = asrc_ioctl_flush(params, user);
1536                 break;
1537         default:
1538                 dev_err(asrc->dev, "Unsupported ioctl cmd!\n");
1539                 break;
1540         }
1541
1542         return ret;
1543 }
1544
1545 static int mxc_asrc_open(struct inode *inode, struct file *file)
1546 {
1547         struct asrc_pair_params *pair_params;
1548         int ret = 0;
1549
1550         ret = signal_pending(current);
1551         if (ret) {
1552                 dev_err(asrc->dev, "Current process has a signal pending.\n");
1553                 return ret;
1554         }
1555
1556         pair_params = kzalloc(sizeof(struct asrc_pair_params), GFP_KERNEL);
1557         if (pair_params == NULL) {
1558                 dev_err(asrc->dev, "failed to allocate pair_params.\n");
1559                 return -ENOBUFS;
1560         }
1561
1562         file->private_data = pair_params;
1563
1564         return ret;
1565 }
1566
1567 static int mxc_asrc_close(struct inode *inode, struct file *file)
1568 {
1569         struct asrc_pair_params *pair_params;
1570         unsigned long lock_flags;
1571
1572         pair_params = file->private_data;
1573
1574         if (!pair_params)
1575                 return 0;
1576
1577         if (pair_params->asrc_active) {
1578                 pair_params->asrc_active = 0;
1579
1580                 dmaengine_terminate_all(pair_params->input_dma_channel);
1581                 dmaengine_terminate_all(pair_params->output_dma_channel);
1582
1583                 asrc_stop_conv(pair_params->index);
1584
1585                 wake_up_interruptible(&pair_params->input_wait_queue);
1586                 wake_up_interruptible(&pair_params->output_wait_queue);
1587         }
1588         if (pair_params->pair_hold) {
1589                 spin_lock_irqsave(&pair_lock, lock_flags);
1590                 pair_params->pair_hold = 0;
1591                 spin_unlock_irqrestore(&pair_lock, lock_flags);
1592
1593                 if (pair_params->input_dma_channel)
1594                         dma_release_channel(pair_params->input_dma_channel);
1595                 if (pair_params->output_dma_channel)
1596                         dma_release_channel(pair_params->output_dma_channel);
1597
1598                 mxc_free_dma_buf(pair_params);
1599
1600                 asrc_release_pair(pair_params->index);
1601                 asrc_finish_conv(pair_params->index);
1602         }
1603
1604         kfree(pair_params);
1605         file->private_data = NULL;
1606
1607         return 0;
1608 }
1609
1610 static int mxc_asrc_mmap(struct file *file, struct vm_area_struct *vma)
1611 {
1612         unsigned long size = vma->vm_end - vma->vm_start;
1613         int ret;
1614
1615         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1616
1617         ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1618                         size, vma->vm_page_prot);
1619         if (ret) {
1620                 dev_err(asrc->dev, "failed to memory map!\n");
1621                 return ret;
1622         }
1623
1624         vma->vm_flags &= ~VM_IO;
1625
1626         return ret;
1627 }
1628
1629 static const struct file_operations asrc_fops = {
1630         .owner          = THIS_MODULE,
1631         .unlocked_ioctl = asrc_ioctl,
1632         .mmap           = mxc_asrc_mmap,
1633         .open           = mxc_asrc_open,
1634         .release        = mxc_asrc_close,
1635 };
1636
1637 static struct miscdevice asrc_miscdev = {
1638         .name   = "mxc_asrc",
1639         .fops   = &asrc_fops,
1640         .minor  = MISC_DYNAMIC_MINOR,
1641 };
1642
1643 static int asrc_read_proc_attr(struct file *file, char __user *buf,
1644                                 size_t count, loff_t *off)
1645 {
1646         char tmpbuf[80];
1647         int len = 0;
1648         u32 reg;
1649
1650         if (*off)
1651                 return 0;
1652
1653         asrc_regmap_read(asrc->regmap, REG_ASRCNCR, &reg);
1654
1655         len += sprintf(tmpbuf, "ANCA: %d\nANCB: %d\nANCC: %d\n",
1656                         (int)ASRCNCR_ANCA_get(reg, asrc->channel_bits),
1657                         (int)ASRCNCR_ANCB_get(reg, asrc->channel_bits),
1658                         (int)ASRCNCR_ANCC_get(reg, asrc->channel_bits));
1659
1660         if (len > count)
1661                 return 0;
1662
1663         if (copy_to_user(buf, &tmpbuf, len))
1664                 return -EFAULT;
1665
1666         *off += len;
1667
1668         return len;
1669 }
1670
1671 #define ASRC_MAX_PROC_BUFFER_SIZE 63
1672
1673 static int asrc_write_proc_attr(struct file *file, const char __user *buffer,
1674                                 size_t count, loff_t *data)
1675 {
1676         char buf[ASRC_MAX_PROC_BUFFER_SIZE];
1677         int na, nb, nc;
1678         int total;
1679
1680         if (count > ASRC_MAX_PROC_BUFFER_SIZE) {
1681                 dev_err(asrc->dev, "Attr proc write: The input string was too long.\n");
1682                 return -EINVAL;
1683         }
1684
1685         if (copy_from_user(buf, buffer, count)) {
1686                 dev_err(asrc->dev, "Attr proc write: Failed to copy buffer from user.\n");
1687                 return -EFAULT;
1688         }
1689
1690         sscanf(buf, "ANCA: %d\nANCB: %d\nANCC: %d", &na, &nb, &nc);
1691
1692         total = asrc->channel_bits > 3 ? 10 : 5;
1693
1694         if (na + nb + nc > total) {
1695                 dev_err(asrc->dev, "Don't surpass %d for total.\n", total);
1696                 return -EINVAL;
1697         } else if (na % 2 != 0 || nb % 2 != 0 || nc % 2 != 0) {
1698                 dev_err(asrc->dev, "Please set an even number for each pair.\n");
1699                 return -EINVAL;
1700         } else if (na < 0 || nb < 0 || nc < 0) {
1701                 dev_err(asrc->dev, "Please set an positive number for each pair.\n");
1702                 return -EINVAL;
1703         }
1704
1705
1706         asrc->asrc_pair[ASRC_PAIR_A].chn_max = na;
1707         asrc->asrc_pair[ASRC_PAIR_B].chn_max = nb;
1708         asrc->asrc_pair[ASRC_PAIR_C].chn_max = nc;
1709
1710         asrc_set_channel_number(ASRC_PAIR_A, na);
1711         asrc_set_channel_number(ASRC_PAIR_B, nb);
1712         asrc_set_channel_number(ASRC_PAIR_C, nc);
1713
1714         return count;
1715 }
1716
1717 static const struct file_operations asrc_proc_fops = {
1718         .read           = asrc_read_proc_attr,
1719         .write          = asrc_write_proc_attr,
1720 };
1721
1722 static void asrc_proc_create(void)
1723 {
1724         struct proc_dir_entry *proc_attr;
1725
1726         asrc->proc_asrc = proc_mkdir(ASRC_PROC_PATH, NULL);
1727         if (!asrc->proc_asrc) {
1728                 dev_err(asrc->dev, "failed to create proc entry %s\n", ASRC_PROC_PATH);
1729                 return;
1730         }
1731
1732         proc_attr = proc_create("ChSettings", S_IFREG | S_IRUGO | S_IWUSR,
1733                         asrc->proc_asrc, &asrc_proc_fops);
1734         if (!proc_attr) {
1735                 remove_proc_entry(ASRC_PROC_PATH, NULL);
1736                 dev_err(asrc->dev, "failed to create proc attribute entry.\n");
1737         }
1738 }
1739
1740 static void asrc_proc_remove(void)
1741 {
1742         remove_proc_entry("ChSettings", asrc->proc_asrc);
1743         remove_proc_entry(ASRC_PROC_PATH, NULL);
1744 }
1745
1746
1747 #ifdef ASRC_USE_REGMAP
1748 /* ============= ASRC REGMAP ============= */
1749
1750 static bool asrc_readable_reg(struct device *dev, unsigned int reg)
1751 {
1752         switch (reg) {
1753         case REG_ASRCTR:
1754         case REG_ASRIER:
1755         case REG_ASRCNCR:
1756         case REG_ASRCFG:
1757         case REG_ASRCSR:
1758         case REG_ASRCDR1:
1759         case REG_ASRCDR2:
1760         case REG_ASRSTR:
1761         case REG_ASRPM1:
1762         case REG_ASRPM2:
1763         case REG_ASRPM3:
1764         case REG_ASRPM4:
1765         case REG_ASRPM5:
1766         case REG_ASRTFR1:
1767         case REG_ASRCCR:
1768         case REG_ASRDOA:
1769         case REG_ASRDOB:
1770         case REG_ASRDOC:
1771         case REG_ASRIDRHA:
1772         case REG_ASRIDRLA:
1773         case REG_ASRIDRHB:
1774         case REG_ASRIDRLB:
1775         case REG_ASRIDRHC:
1776         case REG_ASRIDRLC:
1777         case REG_ASR76K:
1778         case REG_ASR56K:
1779         case REG_ASRMCRA:
1780         case REG_ASRFSTA:
1781         case REG_ASRMCRB:
1782         case REG_ASRFSTB:
1783         case REG_ASRMCRC:
1784         case REG_ASRFSTC:
1785         case REG_ASRMCR1A:
1786         case REG_ASRMCR1B:
1787         case REG_ASRMCR1C:
1788                 return true;
1789         default:
1790                 return false;
1791         }
1792 }
1793
1794 static bool asrc_writeable_reg(struct device *dev, unsigned int reg)
1795 {
1796         switch (reg) {
1797         case REG_ASRCTR:
1798         case REG_ASRIER:
1799         case REG_ASRCNCR:
1800         case REG_ASRCFG:
1801         case REG_ASRCSR:
1802         case REG_ASRCDR1:
1803         case REG_ASRCDR2:
1804         case REG_ASRPM1:
1805         case REG_ASRPM2:
1806         case REG_ASRPM3:
1807         case REG_ASRPM4:
1808         case REG_ASRPM5:
1809         case REG_ASRTFR1:
1810         case REG_ASRCCR:
1811         case REG_ASRDIA:
1812         case REG_ASRDIB:
1813         case REG_ASRDIC:
1814         case REG_ASRIDRHA:
1815         case REG_ASRIDRLA:
1816         case REG_ASRIDRHB:
1817         case REG_ASRIDRLB:
1818         case REG_ASRIDRHC:
1819         case REG_ASRIDRLC:
1820         case REG_ASR76K:
1821         case REG_ASR56K:
1822         case REG_ASRMCRA:
1823         case REG_ASRMCRB:
1824         case REG_ASRMCRC:
1825         case REG_ASRMCR1A:
1826         case REG_ASRMCR1B:
1827         case REG_ASRMCR1C:
1828                 return true;
1829         default:
1830                 return false;
1831         }
1832 }
1833
1834 static bool asrc_volatile_reg(struct device *dev, unsigned int reg)
1835 {
1836         /* Sync all registers after reset */
1837         return true;
1838 }
1839
1840 static const struct regmap_config asrc_regmap_config = {
1841         .reg_bits = 32,
1842         .reg_stride = 4,
1843         .val_bits = 32,
1844
1845         .max_register = REG_ASRMCR1C,
1846         .readable_reg = asrc_readable_reg,
1847         .writeable_reg = asrc_writeable_reg,
1848         .volatile_reg = asrc_volatile_reg,
1849         .cache_type = REGCACHE_RBTREE,
1850 };
1851 #endif
1852
1853 static int mxc_asrc_probe(struct platform_device *pdev)
1854 {
1855         struct device_node *np = pdev->dev.of_node;
1856         struct resource res;
1857         void __iomem *regs;
1858         int ret;
1859
1860         /* Check if the device is existed */
1861         if (!of_device_is_available(np)) {
1862                 dev_err(&pdev->dev, "improper devicetree status.\n");
1863                 return -ENODEV;
1864         }
1865
1866         asrc = devm_kzalloc(&pdev->dev, sizeof(struct asrc_data), GFP_KERNEL);
1867         if (asrc == NULL) {
1868                 dev_err(&pdev->dev, "failed to allocate asrc.\n");
1869                 return -ENOMEM;
1870         }
1871
1872         asrc->dev = &pdev->dev;
1873         asrc->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1874
1875         asrc->asrc_pair[ASRC_PAIR_A].chn_max = 2;
1876         asrc->asrc_pair[ASRC_PAIR_B].chn_max = 6;
1877         asrc->asrc_pair[ASRC_PAIR_C].chn_max = 2;
1878         asrc->asrc_pair[ASRC_PAIR_A].overload_error = 0;
1879         asrc->asrc_pair[ASRC_PAIR_B].overload_error = 0;
1880         asrc->asrc_pair[ASRC_PAIR_C].overload_error = 0;
1881
1882         /* Map the address */
1883         ret = of_address_to_resource(np, 0, &res);
1884         if (ret) {
1885                 dev_err(&pdev->dev, "failed to map address: %d\n", ret);
1886                 return ret;
1887         }
1888         asrc->paddr = res.start;
1889
1890         regs = of_iomap(np, 0);
1891         if (IS_ERR(regs)) {
1892                 dev_err(&pdev->dev, "failed to map io resources.\n");
1893                 return IS_ERR(regs);
1894         }
1895         asrc->vaddr = (unsigned long)regs;
1896
1897 #ifdef ASRC_USE_REGMAP
1898         asrc->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1899                         "core", regs, &asrc_regmap_config);
1900         if (IS_ERR(asrc->regmap)) {
1901                 dev_err(&pdev->dev, "regmap init failed\n");
1902                 ret = PTR_ERR(asrc->regmap);
1903         }
1904
1905         regcache_cache_only(asrc->regmap, false);
1906 #endif
1907
1908         asrc->irq = irq_of_parse_and_map(np, 0);
1909         if (asrc->irq == NO_IRQ) {
1910                 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
1911                 goto err_iomap;
1912         }
1913
1914         ret = devm_request_irq(&pdev->dev, asrc->irq,
1915                         asrc_isr, 0, "asrc", NULL);
1916         if (ret) {
1917                 dev_err(&pdev->dev, "failed to request IRQ: %d\n", ret);
1918                 goto err_iomap;
1919         }
1920
1921         asrc->asrc_clk = devm_clk_get(&pdev->dev, NULL);
1922         if (IS_ERR(asrc->asrc_clk)) {
1923                 ret = PTR_ERR(asrc->asrc_clk);
1924                 goto err_iomap;
1925         }
1926 #ifndef ASRC_USE_REGMAP
1927         clk_prepare_enable(asrc->asrc_clk);
1928 #endif
1929
1930         ret = of_property_read_u32_array(pdev->dev.of_node,
1931                         "fsl,clk-channel-bits", &asrc->channel_bits, 1);
1932         if (ret) {
1933                 dev_err(&pdev->dev, "failed to get clk-channel-bits.\n");
1934                 goto err_iomap;
1935         }
1936
1937         ret = of_property_read_u32_array(pdev->dev.of_node,
1938                         "fsl,clk-map-version", &asrc->clk_map_ver, 1);
1939         if (ret) {
1940                 dev_err(&pdev->dev, "failed to get clk-map-version.\n");
1941                 goto err_iomap;
1942         }
1943
1944         switch (asrc->clk_map_ver) {
1945         case 1:
1946                 input_clk_map = input_clk_map_v1;
1947                 output_clk_map = output_clk_map_v1;
1948                 break;
1949         case 2:
1950         default:
1951                 input_clk_map = input_clk_map_v2;
1952                 output_clk_map = output_clk_map_v2;
1953                 break;
1954         }
1955
1956         ret = misc_register(&asrc_miscdev);
1957         if (ret) {
1958                 dev_err(&pdev->dev, "failed to register char device %d\n", ret);
1959                 goto err_iomap;
1960         }
1961
1962         asrc_proc_create();
1963
1964         ret = mxc_init_asrc();
1965         if (ret)
1966                 goto err_misc;
1967
1968         dev_info(&pdev->dev, "mxc_asrc registered.\n");
1969
1970         return ret;
1971
1972 err_misc:
1973         misc_deregister(&asrc_miscdev);
1974 err_iomap:
1975         iounmap(regs);
1976
1977         dev_err(&pdev->dev, "mxc_asrc register failed: err %d\n", ret);
1978
1979         return ret;
1980 }
1981
1982 static int mxc_asrc_remove(struct platform_device *pdev)
1983 {
1984         asrc_proc_remove();
1985         misc_deregister(&asrc_miscdev);
1986
1987         return 0;
1988 }
1989
1990 static const struct of_device_id fsl_asrc_ids[] = {
1991         { .compatible = "fsl,imx6q-asrc", },
1992         {}
1993 };
1994
1995 static struct platform_driver mxc_asrc_driver = {
1996         .driver = {
1997                 .name = "mxc_asrc",
1998                 .of_match_table = fsl_asrc_ids,
1999         },
2000         .probe = mxc_asrc_probe,
2001         .remove = mxc_asrc_remove,
2002 };
2003
2004 module_platform_driver(mxc_asrc_driver);
2005
2006 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2007 MODULE_DESCRIPTION("Asynchronous Sample Rate Converter");
2008 MODULE_LICENSE("GPL");