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