GNU Linux-libre 4.4.294-gnu1
[releases.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         struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
26         struct rsnd_kctrl_cfg_s sync; /* sync convert */
27         u32 convert_rate; /* sampling rate convert */
28         int err;
29 };
30
31 #define RSND_SRC_NAME_SIZE 16
32
33 #define rsnd_src_nr(priv) ((priv)->src_nr)
34 #define rsnd_enable_sync_convert(src) ((src)->sen.val)
35 #define rsnd_src_of_node(priv) \
36         of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
37
38 #define rsnd_mod_to_src(_mod)                           \
39         container_of((_mod), struct rsnd_src, mod)
40
41 #define for_each_rsnd_src(pos, priv, i)                         \
42         for ((i) = 0;                                           \
43              ((i) < rsnd_src_nr(priv)) &&                       \
44              ((pos) = (struct rsnd_src *)(priv)->src + i);      \
45              i++)
46
47
48 /*
49  *              image of SRC (Sampling Rate Converter)
50  *
51  * 96kHz   <-> +-----+  48kHz   +-----+  48kHz  +-------+
52  * 48kHz   <-> | SRC | <------> | SSI | <-----> | codec |
53  * 44.1kHz <-> +-----+          +-----+         +-------+
54  * ...
55  *
56  */
57
58 /*
59  * src.c is caring...
60  *
61  * Gen1
62  *
63  * [mem] -> [SRU] -> [SSI]
64  *        |--------|
65  *
66  * Gen2
67  *
68  * [mem] -> [SRC] -> [SSIU] -> [SSI]
69  *        |-----------------|
70  */
71
72 /*
73  *      How to use SRC bypass mode for debugging
74  *
75  * SRC has bypass mode, and it is useful for debugging.
76  * In Gen2 case,
77  * SRCm_MODE controls whether SRC is used or not
78  * SSI_MODE0 controls whether SSIU which receives SRC data
79  * is used or not.
80  * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
81  * but SRC bypass mode needs SSI_MODE0 only.
82  *
83  * This driver request
84  * struct rsnd_src_platform_info {
85  *      u32 convert_rate;
86  *      int dma_id;
87  * }
88  *
89  * rsnd_src_convert_rate() indicates
90  * above convert_rate, and it controls
91  * whether SRC is used or not.
92  *
93  * ex) doesn't use SRC
94  * static struct rsnd_dai_platform_info rsnd_dai = {
95  *      .playback = { .ssi = &rsnd_ssi[0], },
96  * };
97  *
98  * ex) uses SRC
99  * static struct rsnd_src_platform_info rsnd_src[] = {
100  *      RSND_SCU(48000, 0),
101  *      ...
102  * };
103  * static struct rsnd_dai_platform_info rsnd_dai = {
104  *      .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
105  * };
106  *
107  * ex) uses SRC bypass mode
108  * static struct rsnd_src_platform_info rsnd_src[] = {
109  *      RSND_SCU(0, 0),
110  *      ...
111  * };
112  * static struct rsnd_dai_platform_info rsnd_dai = {
113  *      .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
114  * };
115  *
116  */
117
118 /*
119  *              Gen1/Gen2 common functions
120  */
121 static void rsnd_src_soft_reset(struct rsnd_mod *mod)
122 {
123         rsnd_mod_write(mod, SRC_SWRSR, 0);
124         rsnd_mod_write(mod, SRC_SWRSR, 1);
125 }
126
127
128 #define rsnd_src_initialize_lock(mod)   __rsnd_src_initialize_lock(mod, 1)
129 #define rsnd_src_initialize_unlock(mod) __rsnd_src_initialize_lock(mod, 0)
130 static void __rsnd_src_initialize_lock(struct rsnd_mod *mod, u32 enable)
131 {
132         rsnd_mod_write(mod, SRC_SRCIR, enable);
133 }
134
135 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
136                                          struct rsnd_mod *mod)
137 {
138         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
139         int is_play = rsnd_io_is_play(io);
140
141         return rsnd_dma_request_channel(rsnd_src_of_node(priv),
142                                         mod,
143                                         is_play ? "rx" : "tx");
144 }
145
146 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
147                         struct rsnd_dai_stream *io,
148                         int use_busif)
149 {
150         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
151         int ssi_id = rsnd_mod_id(ssi_mod);
152
153         /*
154          * SSI_MODE0
155          */
156         rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
157                       !use_busif << ssi_id);
158
159         /*
160          * SSI_MODE1
161          */
162         if (rsnd_ssi_is_pin_sharing(io)) {
163                 int shift = -1;
164                 switch (ssi_id) {
165                 case 1:
166                         shift = 0;
167                         break;
168                 case 2:
169                         shift = 2;
170                         break;
171                 case 4:
172                         shift = 16;
173                         break;
174                 }
175
176                 if (shift >= 0)
177                         rsnd_mod_bset(ssi_mod, SSI_MODE1,
178                                       0x3 << shift,
179                                       rsnd_rdai_is_clk_master(rdai) ?
180                                       0x2 << shift : 0x1 << shift);
181         }
182
183         /*
184          * DMA settings for SSIU
185          */
186         if (use_busif) {
187                 u32 val = rsnd_get_dalign(ssi_mod, io);
188
189                 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
190                                rsnd_get_adinr_bit(ssi_mod, io));
191                 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE,  1);
192                 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
193
194                 rsnd_mod_write(ssi_mod, SSI_BUSIF_DALIGN, val);
195         }
196
197         return 0;
198 }
199
200 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
201                        struct rsnd_dai_stream *io)
202 {
203         /*
204          * DMA settings for SSIU
205          */
206         rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
207
208         return 0;
209 }
210
211 int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
212 {
213         struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
214
215         if (rsnd_is_gen1(priv))
216                 return 0;
217
218         /* enable SSI interrupt if Gen2 */
219         rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
220                        rsnd_ssi_is_dma_mode(ssi_mod) ?
221                        0x0e000000 : 0x0f000000);
222
223         return 0;
224 }
225
226 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
227 {
228         struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
229
230         if (rsnd_is_gen1(priv))
231                 return 0;
232
233         /* disable SSI interrupt if Gen2 */
234         rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
235
236         return 0;
237 }
238
239 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
240                                  struct rsnd_src *src)
241 {
242         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
243         u32 convert_rate;
244
245         if (!runtime)
246                 return 0;
247
248         if (!rsnd_enable_sync_convert(src))
249                 return src->convert_rate;
250
251         convert_rate = src->sync.val;
252
253         if (!convert_rate)
254                 convert_rate = src->convert_rate;
255
256         if (!convert_rate)
257                 convert_rate = runtime->rate;
258
259         return convert_rate;
260 }
261
262 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
263                                    struct rsnd_dai_stream *io,
264                                    struct snd_pcm_runtime *runtime)
265 {
266         struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
267         struct rsnd_src *src;
268         unsigned int rate = 0;
269
270         if (src_mod) {
271                 src = rsnd_mod_to_src(src_mod);
272
273                 /*
274                  * return convert rate if SRC is used,
275                  * otherwise, return runtime->rate as usual
276                  */
277                 rate = rsnd_src_convert_rate(io, src);
278         }
279
280         if (!rate)
281                 rate = runtime->rate;
282
283         return rate;
284 }
285
286 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
287                                      struct rsnd_dai_stream *io)
288 {
289         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
290         struct rsnd_src *src = rsnd_mod_to_src(mod);
291         u32 convert_rate = rsnd_src_convert_rate(io, src);
292         u32 fsrate = 0;
293
294         if (convert_rate)
295                 fsrate = 0x0400000 / convert_rate * runtime->rate;
296
297         /* Set channel number and output bit length */
298         rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr_bit(mod, io));
299
300         /* Enable the initial value of IFS */
301         if (fsrate) {
302                 rsnd_mod_write(mod, SRC_IFSCR, 1);
303
304                 /* Set initial value of IFS */
305                 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
306         }
307
308         /* use DMA transfer */
309         rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
310
311         return 0;
312 }
313
314 static int rsnd_src_hw_params(struct rsnd_mod *mod,
315                               struct rsnd_dai_stream *io,
316                               struct snd_pcm_substream *substream,
317                               struct snd_pcm_hw_params *fe_params)
318 {
319         struct rsnd_src *src = rsnd_mod_to_src(mod);
320         struct snd_soc_pcm_runtime *fe = substream->private_data;
321
322         /* default value (mainly for non-DT) */
323         src->convert_rate = src->info->convert_rate;
324
325         /*
326          * SRC assumes that it is used under DPCM if user want to use
327          * sampling rate convert. Then, SRC should be FE.
328          * And then, this function will be called *after* BE settings.
329          * this means, each BE already has fixuped hw_params.
330          * see
331          *      dpcm_fe_dai_hw_params()
332          *      dpcm_be_dai_hw_params()
333          */
334         if (fe->dai_link->dynamic) {
335                 int stream = substream->stream;
336                 struct snd_soc_dpcm *dpcm;
337                 struct snd_pcm_hw_params *be_params;
338
339                 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
340                         be_params = &dpcm->hw_params;
341
342                         if (params_rate(fe_params) != params_rate(be_params))
343                                 src->convert_rate = params_rate(be_params);
344                 }
345         }
346
347         return 0;
348 }
349
350 static int rsnd_src_init(struct rsnd_mod *mod,
351                          struct rsnd_priv *priv)
352 {
353         struct rsnd_src *src = rsnd_mod_to_src(mod);
354
355         rsnd_mod_power_on(mod);
356
357         rsnd_src_soft_reset(mod);
358
359         rsnd_src_initialize_lock(mod);
360
361         src->err = 0;
362
363         /* reset sync convert_rate */
364         src->sync.val = 0;
365
366         return 0;
367 }
368
369 static int rsnd_src_quit(struct rsnd_mod *mod,
370                          struct rsnd_dai_stream *io,
371                          struct rsnd_priv *priv)
372 {
373         struct rsnd_src *src = rsnd_mod_to_src(mod);
374         struct device *dev = rsnd_priv_to_dev(priv);
375
376         rsnd_mod_power_off(mod);
377
378         if (src->err)
379                 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
380                          rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
381
382         src->convert_rate = 0;
383
384         /* reset sync convert_rate */
385         src->sync.val = 0;
386
387         return 0;
388 }
389
390 static int rsnd_src_start(struct rsnd_mod *mod)
391 {
392         rsnd_src_initialize_unlock(mod);
393
394         return 0;
395 }
396
397 static int rsnd_src_stop(struct rsnd_mod *mod)
398 {
399         /* nothing to do */
400         return 0;
401 }
402
403 /*
404  *              Gen1 functions
405  */
406 static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
407                                    struct rsnd_mod *mod)
408 {
409         struct src_route_config {
410                 u32 mask;
411                 int shift;
412         } routes[] = {
413                 { 0xF,  0, }, /* 0 */
414                 { 0xF,  4, }, /* 1 */
415                 { 0xF,  8, }, /* 2 */
416                 { 0x7, 12, }, /* 3 */
417                 { 0x7, 16, }, /* 4 */
418                 { 0x7, 20, }, /* 5 */
419                 { 0x7, 24, }, /* 6 */
420                 { 0x3, 28, }, /* 7 */
421                 { 0x3, 30, }, /* 8 */
422         };
423         u32 mask;
424         u32 val;
425         int id;
426
427         id = rsnd_mod_id(mod);
428         if (id < 0 || id >= ARRAY_SIZE(routes))
429                 return -EIO;
430
431         /*
432          * SRC_ROUTE_SELECT
433          */
434         val = rsnd_io_is_play(io) ? 0x1 : 0x2;
435         val = val               << routes[id].shift;
436         mask = routes[id].mask  << routes[id].shift;
437
438         rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
439
440         return 0;
441 }
442
443 static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
444                                             struct rsnd_mod *mod)
445 {
446         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
447         struct rsnd_src *src = rsnd_mod_to_src(mod);
448         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
449         u32 convert_rate = rsnd_src_convert_rate(io, src);
450         u32 mask;
451         u32 val;
452         int shift;
453         int id = rsnd_mod_id(mod);
454         int ret;
455
456         /*
457          * SRC_TIMING_SELECT
458          */
459         shift   = (id % 4) * 8;
460         mask    = 0x1F << shift;
461
462         /*
463          * ADG is used as source clock if SRC was used,
464          * then, SSI WS is used as destination clock.
465          * SSI WS is used as source clock if SRC is not used
466          * (when playback, source/destination become reverse when capture)
467          */
468         ret = 0;
469         if (convert_rate) {
470                 /* use ADG */
471                 val = 0;
472                 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
473                                                     runtime->rate,
474                                                     convert_rate);
475         } else if (8 == id) {
476                 /* use SSI WS, but SRU8 is special */
477                 val = id << shift;
478         } else {
479                 /* use SSI WS */
480                 val = (id + 1) << shift;
481         }
482
483         if (ret < 0)
484                 return ret;
485
486         switch (id / 4) {
487         case 0:
488                 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
489                 break;
490         case 1:
491                 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
492                 break;
493         case 2:
494                 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
495                 break;
496         }
497
498         return 0;
499 }
500
501 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
502                                           struct rsnd_dai_stream *io)
503 {
504         struct rsnd_src *src = rsnd_mod_to_src(mod);
505         int ret;
506
507         ret = rsnd_src_set_convert_rate(mod, io);
508         if (ret < 0)
509                 return ret;
510
511         /* Select SRC mode (fixed value) */
512         rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
513
514         /* Set the restriction value of the FS ratio (98%) */
515         rsnd_mod_write(mod, SRC_MNFSR,
516                        rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
517
518         /* Gen1/Gen2 are not compatible */
519         if (rsnd_src_convert_rate(io, src))
520                 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
521
522         /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
523
524         return 0;
525 }
526
527 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
528                               struct rsnd_dai_stream *io,
529                               struct rsnd_priv *priv)
530 {
531         int ret;
532
533         ret = rsnd_src_init(mod, priv);
534         if (ret < 0)
535                 return ret;
536
537         ret = rsnd_src_set_route_gen1(io, mod);
538         if (ret < 0)
539                 return ret;
540
541         ret = rsnd_src_set_convert_rate_gen1(mod, io);
542         if (ret < 0)
543                 return ret;
544
545         ret = rsnd_src_set_convert_timing_gen1(io, mod);
546         if (ret < 0)
547                 return ret;
548
549         return 0;
550 }
551
552 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
553                                struct rsnd_dai_stream *io,
554                                struct rsnd_priv *priv)
555 {
556         int id = rsnd_mod_id(mod);
557
558         rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
559
560         return rsnd_src_start(mod);
561 }
562
563 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
564                               struct rsnd_dai_stream *io,
565                               struct rsnd_priv *priv)
566 {
567         int id = rsnd_mod_id(mod);
568
569         rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
570
571         return rsnd_src_stop(mod);
572 }
573
574 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
575         .name   = SRC_NAME,
576         .dma_req = rsnd_src_dma_req,
577         .init   = rsnd_src_init_gen1,
578         .quit   = rsnd_src_quit,
579         .start  = rsnd_src_start_gen1,
580         .stop   = rsnd_src_stop_gen1,
581         .hw_params = rsnd_src_hw_params,
582 };
583
584 /*
585  *              Gen2 functions
586  */
587 #define rsnd_src_irq_enable_gen2(mod)  rsnd_src_irq_ctrol_gen2(mod, 1)
588 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
589 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
590 {
591         struct rsnd_src *src = rsnd_mod_to_src(mod);
592         u32 sys_int_val, int_val, sys_int_mask;
593         int irq = src->info->irq;
594         int id = rsnd_mod_id(mod);
595
596         sys_int_val =
597         sys_int_mask = OUF_SRC(id);
598         int_val = 0x3300;
599
600         /*
601          * IRQ is not supported on non-DT
602          * see
603          *      rsnd_src_probe_gen2()
604          */
605         if ((irq <= 0) || !enable) {
606                 sys_int_val = 0;
607                 int_val = 0;
608         }
609
610         /*
611          * WORKAROUND
612          *
613          * ignore over flow error when rsnd_enable_sync_convert()
614          */
615         if (rsnd_enable_sync_convert(src))
616                 sys_int_val = sys_int_val & 0xffff;
617
618         rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
619         rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
620         rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
621 }
622
623 static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
624 {
625         u32 val = OUF_SRC(rsnd_mod_id(mod));
626
627         rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
628         rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
629 }
630
631 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
632 {
633         struct rsnd_src *src = rsnd_mod_to_src(mod);
634         u32 val0, val1;
635         bool ret = false;
636
637         val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
638
639         /*
640          * WORKAROUND
641          *
642          * ignore over flow error when rsnd_enable_sync_convert()
643          */
644         if (rsnd_enable_sync_convert(src))
645                 val0 = val0 & 0xffff;
646
647         if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
648             (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
649                 struct rsnd_src *src = rsnd_mod_to_src(mod);
650
651                 src->err++;
652                 ret = true;
653         }
654
655         /* clear error static */
656         rsnd_src_error_clear_gen2(mod);
657
658         return ret;
659 }
660
661 static int _rsnd_src_start_gen2(struct rsnd_mod *mod,
662                                 struct rsnd_dai_stream *io)
663 {
664         struct rsnd_src *src = rsnd_mod_to_src(mod);
665         u32 val;
666
667         val = rsnd_get_dalign(mod, io);
668
669         rsnd_mod_write(mod, SRC_BUSIF_DALIGN, val);
670
671         /*
672          * WORKAROUND
673          *
674          * Enable SRC output if you want to use sync convert together with DVC
675          */
676         val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
677                 0x01 : 0x11;
678
679         rsnd_mod_write(mod, SRC_CTRL, val);
680
681         rsnd_src_error_clear_gen2(mod);
682
683         rsnd_src_start(mod);
684
685         rsnd_src_irq_enable_gen2(mod);
686
687         return 0;
688 }
689
690 static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
691 {
692         rsnd_src_irq_disable_gen2(mod);
693
694         /*
695          * stop SRC output only
696          * see rsnd_src_quit_gen2
697          */
698         rsnd_mod_write(mod, SRC_CTRL, 0x01);
699
700         rsnd_src_error_record_gen2(mod);
701
702         return rsnd_src_stop(mod);
703 }
704
705 static int rsnd_src_quit_gen2(struct rsnd_mod *mod,
706                               struct rsnd_dai_stream *io,
707                               struct rsnd_priv *priv)
708 {
709         /* stop both out/in */
710         rsnd_mod_write(mod, SRC_CTRL, 0);
711
712         return 0;
713 }
714
715 static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
716                                       struct rsnd_dai_stream *io)
717 {
718         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
719
720         spin_lock(&priv->lock);
721
722         /* ignore all cases if not working */
723         if (!rsnd_io_is_working(io))
724                 goto rsnd_src_interrupt_gen2_out;
725
726         if (rsnd_src_error_record_gen2(mod)) {
727                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
728                 struct rsnd_src *src = rsnd_mod_to_src(mod);
729                 struct device *dev = rsnd_priv_to_dev(priv);
730
731                 dev_dbg(dev, "%s[%d] restart\n",
732                         rsnd_mod_name(mod), rsnd_mod_id(mod));
733
734                 _rsnd_src_stop_gen2(mod);
735                 if (src->err < 1024)
736                         _rsnd_src_start_gen2(mod, io);
737                 else
738                         dev_warn(dev, "no more SRC restart\n");
739         }
740
741 rsnd_src_interrupt_gen2_out:
742         spin_unlock(&priv->lock);
743 }
744
745 static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
746 {
747         struct rsnd_mod *mod = data;
748
749         rsnd_mod_interrupt(mod, __rsnd_src_interrupt_gen2);
750
751         return IRQ_HANDLED;
752 }
753
754 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
755                                           struct rsnd_dai_stream *io)
756 {
757         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
758         struct device *dev = rsnd_priv_to_dev(priv);
759         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
760         struct rsnd_src *src = rsnd_mod_to_src(mod);
761         u32 convert_rate = rsnd_src_convert_rate(io, src);
762         u32 cr, route;
763         uint ratio;
764         int ret;
765
766         /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
767         if (!convert_rate)
768                 ratio = 0;
769         else if (convert_rate > runtime->rate)
770                 ratio = 100 * convert_rate / runtime->rate;
771         else
772                 ratio = 100 * runtime->rate / convert_rate;
773
774         if (ratio > 600) {
775                 dev_err(dev, "FSO/FSI ratio error\n");
776                 return -EINVAL;
777         }
778
779         ret = rsnd_src_set_convert_rate(mod, io);
780         if (ret < 0)
781                 return ret;
782
783         cr      = 0x00011110;
784         route   = 0x0;
785         if (convert_rate) {
786                 route   = 0x1;
787
788                 if (rsnd_enable_sync_convert(src)) {
789                         cr |= 0x1;
790                         route |= rsnd_io_is_play(io) ?
791                                 (0x1 << 24) : (0x1 << 25);
792                 }
793         }
794
795         rsnd_mod_write(mod, SRC_SRCCR, cr);
796         rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
797
798         switch (rsnd_mod_id(mod)) {
799         case 5:
800         case 6:
801         case 7:
802         case 8:
803                 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
804                 break;
805         default:
806                 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
807                 break;
808         }
809
810         rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
811
812         return 0;
813 }
814
815 static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
816                                             struct rsnd_mod *mod)
817 {
818         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
819         struct rsnd_src *src = rsnd_mod_to_src(mod);
820         u32 convert_rate = rsnd_src_convert_rate(io, src);
821         int ret;
822
823         if (convert_rate)
824                 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
825                                                     runtime->rate,
826                                                     convert_rate);
827         else
828                 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
829
830         return ret;
831 }
832
833 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
834                                struct rsnd_dai_stream *io,
835                                struct rsnd_priv *priv)
836 {
837         struct rsnd_src *src = rsnd_mod_to_src(mod);
838         struct device *dev = rsnd_priv_to_dev(priv);
839         int irq = src->info->irq;
840         int ret;
841
842         if (irq > 0) {
843                 /*
844                  * IRQ is not supported on non-DT
845                  * see
846                  *      rsnd_src_irq_enable_gen2()
847                  */
848                 ret = devm_request_irq(dev, irq,
849                                        rsnd_src_interrupt_gen2,
850                                        IRQF_SHARED,
851                                        dev_name(dev), mod);
852                 if (ret)
853                         return ret;
854         }
855
856         ret = rsnd_dma_init(io,
857                             rsnd_mod_to_dma(mod),
858                             src->info->dma_id);
859
860         return ret;
861 }
862
863 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
864                                 struct rsnd_dai_stream *io,
865                                 struct rsnd_priv *priv)
866 {
867         rsnd_dma_quit(io, rsnd_mod_to_dma(mod));
868
869         return 0;
870 }
871
872 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
873                               struct rsnd_dai_stream *io,
874                               struct rsnd_priv *priv)
875 {
876         int ret;
877
878         ret = rsnd_src_init(mod, priv);
879         if (ret < 0)
880                 return ret;
881
882         ret = rsnd_src_set_convert_rate_gen2(mod, io);
883         if (ret < 0)
884                 return ret;
885
886         ret = rsnd_src_set_convert_timing_gen2(io, mod);
887         if (ret < 0)
888                 return ret;
889
890         return 0;
891 }
892
893 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
894                                struct rsnd_dai_stream *io,
895                                struct rsnd_priv *priv)
896 {
897         rsnd_dma_start(io, rsnd_mod_to_dma(mod));
898
899         return _rsnd_src_start_gen2(mod, io);
900 }
901
902 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
903                               struct rsnd_dai_stream *io,
904                               struct rsnd_priv *priv)
905 {
906         int ret;
907
908         ret = _rsnd_src_stop_gen2(mod);
909
910         rsnd_dma_stop(io, rsnd_mod_to_dma(mod));
911
912         return ret;
913 }
914
915 static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
916                                       struct rsnd_mod *mod)
917 {
918         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
919         struct rsnd_src *src = rsnd_mod_to_src(mod);
920         u32 convert_rate = rsnd_src_convert_rate(io, src);
921         u32 fsrate;
922
923         if (!runtime)
924                 return;
925
926         if (!convert_rate)
927                 convert_rate = runtime->rate;
928
929         fsrate = 0x0400000 / convert_rate * runtime->rate;
930
931         /* update IFS */
932         rsnd_mod_write(mod, SRC_IFSVR, fsrate);
933 }
934
935 static int rsnd_src_pcm_new_gen2(struct rsnd_mod *mod,
936                             struct rsnd_dai_stream *io,
937                             struct snd_soc_pcm_runtime *rtd)
938 {
939         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
940         struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
941         struct rsnd_src *src = rsnd_mod_to_src(mod);
942         int ret;
943
944         /*
945          * enable SRC sync convert if possible
946          */
947
948         /*
949          * SRC sync convert needs clock master
950          */
951         if (!rsnd_rdai_is_clk_master(rdai))
952                 return 0;
953
954         /*
955          * SRC In doesn't work if DVC was enabled
956          */
957         if (dvc && !rsnd_io_is_play(io))
958                 return 0;
959
960         /*
961          * enable sync convert
962          */
963         ret = rsnd_kctrl_new_s(mod, io, rtd,
964                                rsnd_io_is_play(io) ?
965                                "SRC Out Rate Switch" :
966                                "SRC In Rate Switch",
967                                rsnd_src_reconvert_update,
968                                &src->sen, 1);
969         if (ret < 0)
970                 return ret;
971
972         ret = rsnd_kctrl_new_s(mod, io, rtd,
973                                rsnd_io_is_play(io) ?
974                                "SRC Out Rate" :
975                                "SRC In Rate",
976                                rsnd_src_reconvert_update,
977                                &src->sync, 192000);
978
979         return ret;
980 }
981
982 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
983         .name   = SRC_NAME,
984         .dma_req = rsnd_src_dma_req,
985         .probe  = rsnd_src_probe_gen2,
986         .remove = rsnd_src_remove_gen2,
987         .init   = rsnd_src_init_gen2,
988         .quit   = rsnd_src_quit_gen2,
989         .start  = rsnd_src_start_gen2,
990         .stop   = rsnd_src_stop_gen2,
991         .hw_params = rsnd_src_hw_params,
992         .pcm_new = rsnd_src_pcm_new_gen2,
993 };
994
995 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
996 {
997         if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
998                 id = 0;
999
1000         return rsnd_mod_get((struct rsnd_src *)(priv->src) + id);
1001 }
1002
1003 static void rsnd_of_parse_src(struct platform_device *pdev,
1004                               const struct rsnd_of_data *of_data,
1005                               struct rsnd_priv *priv)
1006 {
1007         struct device_node *src_node;
1008         struct device_node *np;
1009         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
1010         struct rsnd_src_platform_info *src_info;
1011         struct device *dev = &pdev->dev;
1012         int nr, i;
1013
1014         if (!of_data)
1015                 return;
1016
1017         src_node = rsnd_src_of_node(priv);
1018         if (!src_node)
1019                 return;
1020
1021         nr = of_get_child_count(src_node);
1022         if (!nr)
1023                 goto rsnd_of_parse_src_end;
1024
1025         src_info = devm_kzalloc(dev,
1026                                 sizeof(struct rsnd_src_platform_info) * nr,
1027                                 GFP_KERNEL);
1028         if (!src_info) {
1029                 dev_err(dev, "src info allocation error\n");
1030                 goto rsnd_of_parse_src_end;
1031         }
1032
1033         info->src_info          = src_info;
1034         info->src_info_nr       = nr;
1035
1036         i = 0;
1037         for_each_child_of_node(src_node, np) {
1038                 src_info[i].irq = irq_of_parse_and_map(np, 0);
1039
1040                 i++;
1041         }
1042
1043 rsnd_of_parse_src_end:
1044         of_node_put(src_node);
1045 }
1046
1047 int rsnd_src_probe(struct platform_device *pdev,
1048                    const struct rsnd_of_data *of_data,
1049                    struct rsnd_priv *priv)
1050 {
1051         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
1052         struct device *dev = rsnd_priv_to_dev(priv);
1053         struct rsnd_src *src;
1054         struct rsnd_mod_ops *ops;
1055         struct clk *clk;
1056         char name[RSND_SRC_NAME_SIZE];
1057         int i, nr, ret;
1058
1059         ops = NULL;
1060         if (rsnd_is_gen1(priv)) {
1061                 ops = &rsnd_src_gen1_ops;
1062                 dev_warn(dev, "Gen1 support will be removed soon\n");
1063         }
1064         if (rsnd_is_gen2(priv))
1065                 ops = &rsnd_src_gen2_ops;
1066         if (!ops) {
1067                 dev_err(dev, "unknown Generation\n");
1068                 return -EIO;
1069         }
1070
1071         rsnd_of_parse_src(pdev, of_data, priv);
1072
1073         /*
1074          * init SRC
1075          */
1076         nr      = info->src_info_nr;
1077         if (!nr)
1078                 return 0;
1079
1080         src     = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
1081         if (!src)
1082                 return -ENOMEM;
1083
1084         priv->src_nr    = nr;
1085         priv->src       = src;
1086
1087         for_each_rsnd_src(src, priv, i) {
1088                 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1089                          SRC_NAME, i);
1090
1091                 clk = devm_clk_get(dev, name);
1092                 if (IS_ERR(clk))
1093                         return PTR_ERR(clk);
1094
1095                 src->info = &info->src_info[i];
1096
1097                 ret = rsnd_mod_init(priv, rsnd_mod_get(src), ops, clk, RSND_MOD_SRC, i);
1098                 if (ret)
1099                         return ret;
1100         }
1101
1102         return 0;
1103 }
1104
1105 void rsnd_src_remove(struct platform_device *pdev,
1106                      struct rsnd_priv *priv)
1107 {
1108         struct rsnd_src *src;
1109         int i;
1110
1111         for_each_rsnd_src(src, priv, i) {
1112                 rsnd_mod_quit(rsnd_mod_get(src));
1113         }
1114 }