ASoC: rsnd: replace rsnd_dai_is_play() to rsnd_io_is_play()
[cascardo/linux.git] / sound / soc / sh / rcar / src.c
1 /*
2  * Renesas R-Car SRC support
3  *
4  * Copyright (C) 2013 Renesas Solutions Corp.
5  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include "rsnd.h"
12
13 #define SRC_NAME "src"
14
15 /* SRCx_STATUS */
16 #define OUF_SRCO        ((1 << 12) | (1 << 13))
17 #define OUF_SRCI        ((1 <<  9) | (1 <<  8))
18
19 /* SCU_SYSTEM_STATUS0/1 */
20 #define OUF_SRC(id)     ((1 << (id + 16)) | (1 << id))
21
22 struct rsnd_src {
23         struct rsnd_src_platform_info *info; /* rcar_snd.h */
24         struct rsnd_mod mod;
25         int err;
26 };
27
28 #define RSND_SRC_NAME_SIZE 16
29
30 #define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
31 #define rsnd_mod_to_src(_mod)                           \
32         container_of((_mod), struct rsnd_src, mod)
33 #define rsnd_src_dma_available(src) \
34         rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
35
36 #define for_each_rsnd_src(pos, priv, i)                         \
37         for ((i) = 0;                                           \
38              ((i) < rsnd_src_nr(priv)) &&                       \
39              ((pos) = (struct rsnd_src *)(priv)->src + i);      \
40              i++)
41
42
43 /*
44  *              image of SRC (Sampling Rate Converter)
45  *
46  * 96kHz   <-> +-----+  48kHz   +-----+  48kHz  +-------+
47  * 48kHz   <-> | SRC | <------> | SSI | <-----> | codec |
48  * 44.1kHz <-> +-----+          +-----+         +-------+
49  * ...
50  *
51  */
52
53 /*
54  * src.c is caring...
55  *
56  * Gen1
57  *
58  * [mem] -> [SRU] -> [SSI]
59  *        |--------|
60  *
61  * Gen2
62  *
63  * [mem] -> [SRC] -> [SSIU] -> [SSI]
64  *        |-----------------|
65  */
66
67 /*
68  *      How to use SRC bypass mode for debugging
69  *
70  * SRC has bypass mode, and it is useful for debugging.
71  * In Gen2 case,
72  * SRCm_MODE controls whether SRC is used or not
73  * SSI_MODE0 controls whether SSIU which receives SRC data
74  * is used or not.
75  * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
76  * but SRC bypass mode needs SSI_MODE0 only.
77  *
78  * This driver request
79  * struct rsnd_src_platform_info {
80  *      u32 convert_rate;
81  *      int dma_id;
82  * }
83  *
84  * rsnd_src_convert_rate() indicates
85  * above convert_rate, and it controls
86  * whether SRC is used or not.
87  *
88  * ex) doesn't use SRC
89  * static struct rsnd_dai_platform_info rsnd_dai = {
90  *      .playback = { .ssi = &rsnd_ssi[0], },
91  * };
92  *
93  * ex) uses SRC
94  * static struct rsnd_src_platform_info rsnd_src[] = {
95  *      RSND_SCU(48000, 0),
96  *      ...
97  * };
98  * static struct rsnd_dai_platform_info rsnd_dai = {
99  *      .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
100  * };
101  *
102  * ex) uses SRC bypass mode
103  * static struct rsnd_src_platform_info rsnd_src[] = {
104  *      RSND_SCU(0, 0),
105  *      ...
106  * };
107  * static struct rsnd_dai_platform_info rsnd_dai = {
108  *      .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
109  * };
110  *
111  */
112
113 /*
114  *              Gen1/Gen2 common functions
115  */
116 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
117                         struct rsnd_dai *rdai,
118                         int use_busif)
119 {
120         struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
121         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
122         int ssi_id = rsnd_mod_id(ssi_mod);
123
124         /*
125          * SSI_MODE0
126          */
127         rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
128                       !use_busif << ssi_id);
129
130         /*
131          * SSI_MODE1
132          */
133         if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
134                 int shift = -1;
135                 switch (ssi_id) {
136                 case 1:
137                         shift = 0;
138                         break;
139                 case 2:
140                         shift = 2;
141                         break;
142                 case 4:
143                         shift = 16;
144                         break;
145                 }
146
147                 if (shift >= 0)
148                         rsnd_mod_bset(ssi_mod, SSI_MODE1,
149                                       0x3 << shift,
150                                       rsnd_rdai_is_clk_master(rdai) ?
151                                       0x2 << shift : 0x1 << shift);
152         }
153
154         /*
155          * DMA settings for SSIU
156          */
157         if (use_busif) {
158                 u32 val = 0x76543210;
159                 u32 mask = ~0;
160
161                 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
162                                rsnd_get_adinr(ssi_mod));
163                 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE,  1);
164                 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
165
166                 mask <<= runtime->channels * 4;
167                 val = val & mask;
168
169                 switch (runtime->sample_bits) {
170                 case 16:
171                         val |= 0x67452301 & ~mask;
172                         break;
173                 case 32:
174                         val |= 0x76543210 & ~mask;
175                         break;
176                 }
177                 rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val);
178
179         }
180
181         return 0;
182 }
183
184 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
185                        struct rsnd_dai *rdai)
186 {
187         /*
188          * DMA settings for SSIU
189          */
190         rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
191
192         return 0;
193 }
194
195 int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod,
196                             struct rsnd_dai *rdai)
197 {
198         struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
199
200         if (rsnd_is_gen1(priv))
201                 return 0;
202
203         /* enable SSI interrupt if Gen2 */
204         if (rsnd_ssi_is_dma_mode(ssi_mod))
205                 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0e000000);
206         else
207                 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
208
209         return 0;
210 }
211
212 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod,
213                             struct rsnd_dai *rdai)
214 {
215         struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
216
217         if (rsnd_is_gen1(priv))
218                 return 0;
219
220         /* disable SSI interrupt if Gen2 */
221         rsnd_mod_write(ssi_mod, INT_ENABLE, 0x00000000);
222
223         return 0;
224 }
225
226 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
227                                    struct rsnd_dai_stream *io,
228                                    struct snd_pcm_runtime *runtime)
229 {
230         struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
231         struct rsnd_src *src;
232         unsigned int rate = 0;
233
234         if (src_mod) {
235                 src = rsnd_mod_to_src(src_mod);
236
237                 /*
238                  * return convert rate if SRC is used,
239                  * otherwise, return runtime->rate as usual
240                  */
241                 rate = rsnd_src_convert_rate(src);
242         }
243
244         if (!rate)
245                 rate = runtime->rate;
246
247         return rate;
248 }
249
250 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
251                                      struct rsnd_dai *rdai)
252 {
253         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
254         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
255         struct rsnd_src *src = rsnd_mod_to_src(mod);
256         u32 convert_rate = rsnd_src_convert_rate(src);
257         u32 fsrate = 0;
258
259         if (convert_rate)
260                 fsrate = 0x0400000 / convert_rate * runtime->rate;
261
262         /* set/clear soft reset */
263         rsnd_mod_write(mod, SRC_SWRSR, 0);
264         rsnd_mod_write(mod, SRC_SWRSR, 1);
265
266         /* Set channel number and output bit length */
267         rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
268
269         /* Enable the initial value of IFS */
270         if (fsrate) {
271                 rsnd_mod_write(mod, SRC_IFSCR, 1);
272
273                 /* Set initial value of IFS */
274                 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
275         }
276
277         /* use DMA transfer */
278         rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
279
280         return 0;
281 }
282
283 static int rsnd_src_init(struct rsnd_mod *mod,
284                          struct rsnd_dai *rdai)
285 {
286         struct rsnd_src *src = rsnd_mod_to_src(mod);
287
288         rsnd_mod_hw_start(mod);
289
290         src->err = 0;
291
292         /*
293          * Initialize the operation of the SRC internal circuits
294          * see rsnd_src_start()
295          */
296         rsnd_mod_write(mod, SRC_SRCIR, 1);
297
298         return 0;
299 }
300
301 static int rsnd_src_quit(struct rsnd_mod *mod,
302                          struct rsnd_dai *rdai)
303 {
304         struct rsnd_src *src = rsnd_mod_to_src(mod);
305         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
306         struct device *dev = rsnd_priv_to_dev(priv);
307
308         rsnd_mod_hw_stop(mod);
309
310         if (src->err)
311                 dev_warn(dev, "src under/over flow err = %d\n", src->err);
312
313         return 0;
314 }
315
316 static int rsnd_src_start(struct rsnd_mod *mod)
317 {
318         /*
319          * Cancel the initialization and operate the SRC function
320          * see rsnd_src_init()
321          */
322         rsnd_mod_write(mod, SRC_SRCIR, 0);
323
324         return 0;
325 }
326
327 static int rsnd_src_stop(struct rsnd_mod *mod)
328 {
329         /* nothing to do */
330         return 0;
331 }
332
333 /*
334  *              Gen1 functions
335  */
336 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
337                                    struct rsnd_dai *rdai)
338 {
339         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
340         struct src_route_config {
341                 u32 mask;
342                 int shift;
343         } routes[] = {
344                 { 0xF,  0, }, /* 0 */
345                 { 0xF,  4, }, /* 1 */
346                 { 0xF,  8, }, /* 2 */
347                 { 0x7, 12, }, /* 3 */
348                 { 0x7, 16, }, /* 4 */
349                 { 0x7, 20, }, /* 5 */
350                 { 0x7, 24, }, /* 6 */
351                 { 0x3, 28, }, /* 7 */
352                 { 0x3, 30, }, /* 8 */
353         };
354         u32 mask;
355         u32 val;
356         int id;
357
358         id = rsnd_mod_id(mod);
359         if (id < 0 || id >= ARRAY_SIZE(routes))
360                 return -EIO;
361
362         /*
363          * SRC_ROUTE_SELECT
364          */
365         val = rsnd_io_is_play(io) ? 0x1 : 0x2;
366         val = val               << routes[id].shift;
367         mask = routes[id].mask  << routes[id].shift;
368
369         rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
370
371         return 0;
372 }
373
374 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
375                                             struct rsnd_dai *rdai)
376 {
377         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
378         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
379         struct rsnd_src *src = rsnd_mod_to_src(mod);
380         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
381         u32 convert_rate = rsnd_src_convert_rate(src);
382         u32 mask;
383         u32 val;
384         int shift;
385         int id = rsnd_mod_id(mod);
386         int ret;
387
388         /*
389          * SRC_TIMING_SELECT
390          */
391         shift   = (id % 4) * 8;
392         mask    = 0x1F << shift;
393
394         /*
395          * ADG is used as source clock if SRC was used,
396          * then, SSI WS is used as destination clock.
397          * SSI WS is used as source clock if SRC is not used
398          * (when playback, source/destination become reverse when capture)
399          */
400         ret = 0;
401         if (convert_rate) {
402                 /* use ADG */
403                 val = 0;
404                 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
405                                                     runtime->rate,
406                                                     convert_rate);
407         } else if (8 == id) {
408                 /* use SSI WS, but SRU8 is special */
409                 val = id << shift;
410         } else {
411                 /* use SSI WS */
412                 val = (id + 1) << shift;
413         }
414
415         if (ret < 0)
416                 return ret;
417
418         switch (id / 4) {
419         case 0:
420                 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
421                 break;
422         case 1:
423                 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
424                 break;
425         case 2:
426                 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
427                 break;
428         }
429
430         return 0;
431 }
432
433 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
434                                           struct rsnd_dai *rdai)
435 {
436         struct rsnd_src *src = rsnd_mod_to_src(mod);
437         int ret;
438
439         ret = rsnd_src_set_convert_rate(mod, rdai);
440         if (ret < 0)
441                 return ret;
442
443         /* Select SRC mode (fixed value) */
444         rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
445
446         /* Set the restriction value of the FS ratio (98%) */
447         rsnd_mod_write(mod, SRC_MNFSR,
448                        rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
449
450         /* Gen1/Gen2 are not compatible */
451         if (rsnd_src_convert_rate(src))
452                 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
453
454         /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
455
456         return 0;
457 }
458
459 static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
460                               struct rsnd_dai *rdai)
461 {
462         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
463         struct device *dev = rsnd_priv_to_dev(priv);
464
465         dev_dbg(dev, "%s[%d] (Gen1) is probed\n",
466                 rsnd_mod_name(mod), rsnd_mod_id(mod));
467
468         return 0;
469 }
470
471 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
472                               struct rsnd_dai *rdai)
473 {
474         int ret;
475
476         ret = rsnd_src_init(mod, rdai);
477         if (ret < 0)
478                 return ret;
479
480         ret = rsnd_src_set_route_gen1(mod, rdai);
481         if (ret < 0)
482                 return ret;
483
484         ret = rsnd_src_set_convert_rate_gen1(mod, rdai);
485         if (ret < 0)
486                 return ret;
487
488         ret = rsnd_src_set_convert_timing_gen1(mod, rdai);
489         if (ret < 0)
490                 return ret;
491
492         return 0;
493 }
494
495 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
496                                struct rsnd_dai *rdai)
497 {
498         int id = rsnd_mod_id(mod);
499
500         rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
501
502         return rsnd_src_start(mod);
503 }
504
505 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
506                               struct rsnd_dai *rdai)
507 {
508         int id = rsnd_mod_id(mod);
509
510         rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
511
512         return rsnd_src_stop(mod);
513 }
514
515 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
516         .name   = SRC_NAME,
517         .probe  = rsnd_src_probe_gen1,
518         .init   = rsnd_src_init_gen1,
519         .quit   = rsnd_src_quit,
520         .start  = rsnd_src_start_gen1,
521         .stop   = rsnd_src_stop_gen1,
522 };
523
524 /*
525  *              Gen2 functions
526  */
527 #define rsnd_src_irq_enable_gen2(mod)  rsnd_src_irq_ctrol_gen2(mod, 1)
528 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
529 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
530 {
531         struct rsnd_src *src = rsnd_mod_to_src(mod);
532         u32 sys_int_val, int_val, sys_int_mask;
533         int irq = src->info->irq;
534         int id = rsnd_mod_id(mod);
535
536         sys_int_val =
537         sys_int_mask = OUF_SRC(id);
538         int_val = 0x3300;
539
540         /*
541          * IRQ is not supported on non-DT
542          * see
543          *      rsnd_src_probe_gen2()
544          */
545         if ((irq <= 0) || !enable) {
546                 sys_int_val = 0;
547                 int_val = 0;
548         }
549
550         rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
551         rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
552         rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
553 }
554
555 static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
556 {
557         u32 val = OUF_SRC(rsnd_mod_id(mod));
558
559         rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
560         rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
561 }
562
563 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
564 {
565         u32 val = OUF_SRC(rsnd_mod_id(mod));
566         bool ret = false;
567
568         if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val) ||
569             (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val)) {
570                 struct rsnd_src *src = rsnd_mod_to_src(mod);
571
572                 src->err++;
573                 ret = true;
574         }
575
576         /* clear error static */
577         rsnd_src_error_clear_gen2(mod);
578
579         return ret;
580 }
581
582 static int _rsnd_src_start_gen2(struct rsnd_mod *mod)
583 {
584         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
585         u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
586
587         rsnd_mod_write(mod, SRC_CTRL, val);
588
589         rsnd_src_error_clear_gen2(mod);
590
591         rsnd_src_start(mod);
592
593         rsnd_src_irq_enable_gen2(mod);
594
595         return 0;
596 }
597
598 static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
599 {
600         rsnd_src_irq_disable_gen2(mod);
601
602         rsnd_mod_write(mod, SRC_CTRL, 0);
603
604         rsnd_src_error_record_gen2(mod);
605
606         return rsnd_src_stop(mod);
607 }
608
609 static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
610 {
611         struct rsnd_mod *mod = data;
612         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
613
614         if (!io)
615                 return IRQ_NONE;
616
617         if (rsnd_src_error_record_gen2(mod)) {
618                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
619                 struct device *dev = rsnd_priv_to_dev(priv);
620
621                 _rsnd_src_stop_gen2(mod);
622                 _rsnd_src_start_gen2(mod);
623
624                 dev_dbg(dev, "%s[%d] restart\n",
625                         rsnd_mod_name(mod), rsnd_mod_id(mod));
626         }
627
628         return IRQ_HANDLED;
629 }
630
631 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
632                                           struct rsnd_dai *rdai)
633 {
634         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
635         struct device *dev = rsnd_priv_to_dev(priv);
636         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
637         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
638         struct rsnd_src *src = rsnd_mod_to_src(mod);
639         u32 convert_rate = rsnd_src_convert_rate(src);
640         uint ratio;
641         int ret;
642
643         /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
644         if (!convert_rate)
645                 ratio = 0;
646         else if (convert_rate > runtime->rate)
647                 ratio = 100 * convert_rate / runtime->rate;
648         else
649                 ratio = 100 * runtime->rate / convert_rate;
650
651         if (ratio > 600) {
652                 dev_err(dev, "FSO/FSI ratio error\n");
653                 return -EINVAL;
654         }
655
656         ret = rsnd_src_set_convert_rate(mod, rdai);
657         if (ret < 0)
658                 return ret;
659
660         rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
661
662         if (convert_rate) {
663                 /* Gen1/Gen2 are not compatible */
664                 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
665         }
666
667         switch (rsnd_mod_id(mod)) {
668         case 5:
669         case 6:
670         case 7:
671         case 8:
672                 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
673                 break;
674         default:
675                 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
676                 break;
677         }
678
679         rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
680
681         return 0;
682 }
683
684 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
685                                             struct rsnd_dai *rdai)
686 {
687         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
688         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
689         struct rsnd_src *src = rsnd_mod_to_src(mod);
690         u32 convert_rate = rsnd_src_convert_rate(src);
691         int ret;
692
693         if (convert_rate)
694                 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
695                                                     runtime->rate,
696                                                     convert_rate);
697         else
698                 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
699
700         return ret;
701 }
702
703 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
704                                struct rsnd_dai *rdai)
705 {
706         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
707         struct rsnd_src *src = rsnd_mod_to_src(mod);
708         struct device *dev = rsnd_priv_to_dev(priv);
709         int irq = src->info->irq;
710         int ret;
711
712         if (irq > 0) {
713                 /*
714                  * IRQ is not supported on non-DT
715                  * see
716                  *      rsnd_src_irq_enable_gen2()
717                  */
718                 ret = devm_request_irq(dev, irq,
719                                        rsnd_src_interrupt_gen2,
720                                        IRQF_SHARED,
721                                        dev_name(dev), mod);
722                 if (ret)
723                         goto rsnd_src_probe_gen2_fail;
724         }
725
726         ret = rsnd_dma_init(priv,
727                             rsnd_mod_to_dma(mod),
728                             rsnd_info_is_playback(priv, src),
729                             src->info->dma_id);
730         if (ret)
731                 goto rsnd_src_probe_gen2_fail;
732
733         dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
734                 rsnd_mod_name(mod), rsnd_mod_id(mod));
735
736         return ret;
737
738 rsnd_src_probe_gen2_fail:
739         dev_err(dev, "%s[%d] (Gen2) failed\n",
740                 rsnd_mod_name(mod), rsnd_mod_id(mod));
741
742         return ret;
743 }
744
745 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
746                                 struct rsnd_dai *rdai)
747 {
748         rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
749
750         return 0;
751 }
752
753 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
754                               struct rsnd_dai *rdai)
755 {
756         int ret;
757
758         ret = rsnd_src_init(mod, rdai);
759         if (ret < 0)
760                 return ret;
761
762         ret = rsnd_src_set_convert_rate_gen2(mod, rdai);
763         if (ret < 0)
764                 return ret;
765
766         ret = rsnd_src_set_convert_timing_gen2(mod, rdai);
767         if (ret < 0)
768                 return ret;
769
770         return 0;
771 }
772
773 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
774                                struct rsnd_dai *rdai)
775 {
776         rsnd_dma_start(rsnd_mod_to_dma(mod));
777
778         return _rsnd_src_start_gen2(mod);
779 }
780
781 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
782                               struct rsnd_dai *rdai)
783 {
784         int ret;
785
786         ret = _rsnd_src_stop_gen2(mod);
787
788         rsnd_dma_stop(rsnd_mod_to_dma(mod));
789
790         return ret;
791 }
792
793 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
794         .name   = SRC_NAME,
795         .probe  = rsnd_src_probe_gen2,
796         .remove = rsnd_src_remove_gen2,
797         .init   = rsnd_src_init_gen2,
798         .quit   = rsnd_src_quit,
799         .start  = rsnd_src_start_gen2,
800         .stop   = rsnd_src_stop_gen2,
801 };
802
803 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
804 {
805         if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
806                 id = 0;
807
808         return &((struct rsnd_src *)(priv->src) + id)->mod;
809 }
810
811 static void rsnd_of_parse_src(struct platform_device *pdev,
812                               const struct rsnd_of_data *of_data,
813                               struct rsnd_priv *priv)
814 {
815         struct device_node *src_node;
816         struct device_node *np;
817         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
818         struct rsnd_src_platform_info *src_info;
819         struct device *dev = &pdev->dev;
820         int nr, i;
821
822         if (!of_data)
823                 return;
824
825         src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
826         if (!src_node)
827                 return;
828
829         nr = of_get_child_count(src_node);
830         if (!nr)
831                 goto rsnd_of_parse_src_end;
832
833         src_info = devm_kzalloc(dev,
834                                 sizeof(struct rsnd_src_platform_info) * nr,
835                                 GFP_KERNEL);
836         if (!src_info) {
837                 dev_err(dev, "src info allocation error\n");
838                 goto rsnd_of_parse_src_end;
839         }
840
841         info->src_info          = src_info;
842         info->src_info_nr       = nr;
843
844         i = 0;
845         for_each_child_of_node(src_node, np) {
846                 src_info[i].irq = irq_of_parse_and_map(np, 0);
847
848                 i++;
849         }
850
851 rsnd_of_parse_src_end:
852         of_node_put(src_node);
853 }
854
855 int rsnd_src_probe(struct platform_device *pdev,
856                    const struct rsnd_of_data *of_data,
857                    struct rsnd_priv *priv)
858 {
859         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
860         struct device *dev = rsnd_priv_to_dev(priv);
861         struct rsnd_src *src;
862         struct rsnd_mod_ops *ops;
863         struct clk *clk;
864         char name[RSND_SRC_NAME_SIZE];
865         int i, nr;
866
867         ops = NULL;
868         if (rsnd_is_gen1(priv))
869                 ops = &rsnd_src_gen1_ops;
870         if (rsnd_is_gen2(priv))
871                 ops = &rsnd_src_gen2_ops;
872         if (!ops) {
873                 dev_err(dev, "unknown Generation\n");
874                 return -EIO;
875         }
876
877         rsnd_of_parse_src(pdev, of_data, priv);
878
879         /*
880          * init SRC
881          */
882         nr      = info->src_info_nr;
883         if (!nr)
884                 return 0;
885
886         src     = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
887         if (!src) {
888                 dev_err(dev, "SRC allocate failed\n");
889                 return -ENOMEM;
890         }
891
892         priv->src_nr    = nr;
893         priv->src       = src;
894
895         for_each_rsnd_src(src, priv, i) {
896                 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
897                          SRC_NAME, i);
898
899                 clk = devm_clk_get(dev, name);
900                 if (IS_ERR(clk))
901                         return PTR_ERR(clk);
902
903                 src->info = &info->src_info[i];
904
905                 rsnd_mod_init(priv, &src->mod, ops, clk, RSND_MOD_SRC, i);
906
907                 dev_dbg(dev, "SRC%d probed\n", i);
908         }
909
910         return 0;
911 }