ASoC: rsnd: get ssi/scu from rsnd_dai_stream
[platform/kernel/linux-stable.git] / sound / soc / sh / rcar / scu.c
1 /*
2  * Renesas R-Car SCU 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 struct rsnd_scu {
14         struct rsnd_scu_platform_info *info; /* rcar_snd.h */
15         struct rsnd_mod mod;
16         struct clk *clk;
17 };
18
19 #define RSND_SCU_NAME_SIZE 16
20
21 /*
22  * ADINR
23  */
24 #define OTBL_24         (0 << 16)
25 #define OTBL_22         (2 << 16)
26 #define OTBL_20         (4 << 16)
27 #define OTBL_18         (6 << 16)
28 #define OTBL_16         (8 << 16)
29
30 #define rsnd_scu_mode_flags(p) ((p)->info->flags)
31 #define rsnd_scu_convert_rate(p) ((p)->info->convert_rate)
32 #define rsnd_mod_to_scu(_mod)                           \
33         container_of((_mod), struct rsnd_scu, mod)
34 #define rsnd_scu_hpbif_is_enable(scu)   \
35         (rsnd_scu_mode_flags(scu) & RSND_SCU_USE_HPBIF)
36 #define rsnd_scu_dma_available(scu) \
37         rsnd_dma_available(rsnd_mod_to_dma(&(scu)->mod))
38
39 #define for_each_rsnd_scu(pos, priv, i)                         \
40         for ((i) = 0;                                           \
41              ((i) < rsnd_scu_nr(priv)) &&                       \
42              ((pos) = (struct rsnd_scu *)(priv)->scu + i);      \
43              i++)
44
45
46 /*
47  *              image of SRC (Sampling Rate Converter)
48  *
49  * 96kHz   <-> +-----+  48kHz   +-----+  48kHz  +-------+
50  * 48kHz   <-> | SRC | <------> | SSI | <-----> | codec |
51  * 44.1kHz <-> +-----+          +-----+         +-------+
52  * ...
53  *
54  */
55
56 /*
57  * scu.c is caring...
58  *
59  * Gen1
60  *
61  * [mem] -> [SRU] -> [SSI]
62  *        |--------|
63  *
64  * Gen2
65  *
66  * [mem] -> [SCU] -> [SSIU] -> [SSI]
67  *        |-----------------|
68  */
69
70 /*
71  *      How to use SRC bypass mode for debugging
72  *
73  * SRC has bypass mode, and it is useful for debugging.
74  * In Gen2 case,
75  * SRCm_MODE controls whether SRC is used or not
76  * SSI_MODE0 controls whether SSIU which receives SRC data
77  * is used or not.
78  * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
79  * but SRC bypass mode needs SSI_MODE0 only.
80  *
81  * This driver request
82  * struct rsnd_scu_platform_info {
83  *      u32 flags;
84  *      u32 convert_rate;
85  * }
86  *
87  * rsnd_scu_hpbif_is_enable() will be true
88  * if flags had RSND_SCU_USE_HPBIF,
89  * and it controls whether SSIU is used or not.
90  *
91  * rsnd_scu_convert_rate() indicates
92  * above convert_rate, and it controls
93  * whether SRC is used or not.
94  *
95  * ex) doesn't use SRC
96  * struct rsnd_scu_platform_info info = {
97  *      .flags = 0,
98  *      .convert_rate = 0,
99  * };
100  *
101  * ex) uses SRC
102  * struct rsnd_scu_platform_info info = {
103  *      .flags = RSND_SCU_USE_HPBIF,
104  *      .convert_rate = 48000,
105  * };
106  *
107  * ex) uses SRC bypass mode
108  * struct rsnd_scu_platform_info info = {
109  *      .flags = RSND_SCU_USE_HPBIF,
110  *      .convert_rate = 0,
111  * };
112  *
113  */
114
115 /*
116  *              Gen1/Gen2 common functions
117  */
118 static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod,
119                                   struct rsnd_dai *rdai,
120                                   struct rsnd_dai_stream *io)
121 {
122         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
123         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
124         struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
125         int ssi_id = rsnd_mod_id(ssi_mod);
126         u32 convert_rate = rsnd_scu_convert_rate(scu);
127
128         if (convert_rate && !rsnd_dai_is_clk_master(rdai)) {
129                 struct device *dev = rsnd_priv_to_dev(priv);
130
131                 dev_err(dev, "rsnd should be clk master when you rate convert\n");
132                 return -EINVAL;
133         }
134
135         /*
136          * SSI_MODE0
137          */
138         rsnd_mod_bset(mod, SSI_MODE0, (1 << ssi_id),
139                       rsnd_scu_hpbif_is_enable(scu) ? 0 : (1 << ssi_id));
140
141         /*
142          * SSI_MODE1
143          */
144         if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
145                 int shift = -1;
146                 switch (ssi_id) {
147                 case 1:
148                         shift = 0;
149                         break;
150                 case 2:
151                         shift = 2;
152                         break;
153                 case 4:
154                         shift = 16;
155                         break;
156                 }
157
158                 if (shift >= 0)
159                         rsnd_mod_bset(mod, SSI_MODE1,
160                                       0x3 << shift,
161                                       rsnd_dai_is_clk_master(rdai) ?
162                                       0x2 << shift : 0x1 << shift);
163         }
164
165         return 0;
166 }
167
168 unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
169                                    struct rsnd_dai_stream *io,
170                                    struct snd_pcm_runtime *runtime)
171 {
172         struct rsnd_scu *scu;
173         unsigned int rate;
174
175         scu = rsnd_mod_to_scu(rsnd_io_to_mod_scu(io));
176
177         /*
178          * return convert rate if SRC is used,
179          * otherwise, return runtime->rate as usual
180          */
181         rate = rsnd_scu_convert_rate(scu);
182         if (!rate)
183                 rate = runtime->rate;
184
185         return rate;
186 }
187
188 static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
189                                      struct rsnd_dai *rdai,
190                                      struct rsnd_dai_stream *io)
191 {
192         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
193         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
194         u32 convert_rate = rsnd_scu_convert_rate(scu);
195         u32 adinr = runtime->channels;
196         u32 fsrate = 0;
197
198         if (convert_rate)
199                 fsrate = 0x0400000 / convert_rate * runtime->rate;
200
201         /* set/clear soft reset */
202         rsnd_mod_write(mod, SRC_SWRSR, 0);
203         rsnd_mod_write(mod, SRC_SWRSR, 1);
204
205         /*
206          * Initialize the operation of the SRC internal circuits
207          * see rsnd_scu_start()
208          */
209         rsnd_mod_write(mod, SRC_SRCIR, 1);
210
211         /* Set channel number and output bit length */
212         switch (runtime->sample_bits) {
213         case 16:
214                 adinr |= OTBL_16;
215                 break;
216         case 32:
217                 adinr |= OTBL_24;
218                 break;
219         default:
220                 return -EIO;
221         }
222         rsnd_mod_write(mod, SRC_ADINR, adinr);
223
224         /* Enable the initial value of IFS */
225         if (fsrate) {
226                 rsnd_mod_write(mod, SRC_IFSCR, 1);
227
228                 /* Set initial value of IFS */
229                 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
230         }
231
232         /* use DMA transfer */
233         rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
234
235         return 0;
236 }
237
238 static int rsnd_scu_init(struct rsnd_mod *mod,
239                          struct rsnd_dai *rdai,
240                          struct rsnd_dai_stream *io)
241 {
242         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
243         int ret;
244
245         clk_enable(scu->clk);
246
247         ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
248         if (ret < 0)
249                 return ret;
250
251         return 0;
252 }
253
254 static int rsnd_scu_quit(struct rsnd_mod *mod,
255                          struct rsnd_dai *rdai,
256                          struct rsnd_dai_stream *io)
257 {
258         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
259
260         clk_disable(scu->clk);
261
262         return 0;
263 }
264
265 static int rsnd_scu_start(struct rsnd_mod *mod,
266                           struct rsnd_dai *rdai,
267                           struct rsnd_dai_stream *io)
268 {
269         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
270
271         /*
272          * Cancel the initialization and operate the SRC function
273          * see rsnd_scu_set_convert_rate()
274          */
275         rsnd_mod_write(mod, SRC_SRCIR, 0);
276
277         if (rsnd_scu_convert_rate(scu))
278                 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
279
280         return 0;
281 }
282
283
284 static int rsnd_scu_stop(struct rsnd_mod *mod,
285                          struct rsnd_dai *rdai,
286                          struct rsnd_dai_stream *io)
287 {
288         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
289
290         if (rsnd_scu_convert_rate(scu))
291                 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
292
293         return 0;
294 }
295
296 static struct rsnd_mod_ops rsnd_scu_non_ops = {
297         .name   = "scu (non)",
298 };
299
300 /*
301  *              Gen1 functions
302  */
303 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
304                                    struct rsnd_dai *rdai,
305                                    struct rsnd_dai_stream *io)
306 {
307         struct scu_route_config {
308                 u32 mask;
309                 int shift;
310         } routes[] = {
311                 { 0xF,  0, }, /* 0 */
312                 { 0xF,  4, }, /* 1 */
313                 { 0xF,  8, }, /* 2 */
314                 { 0x7, 12, }, /* 3 */
315                 { 0x7, 16, }, /* 4 */
316                 { 0x7, 20, }, /* 5 */
317                 { 0x7, 24, }, /* 6 */
318                 { 0x3, 28, }, /* 7 */
319                 { 0x3, 30, }, /* 8 */
320         };
321         u32 mask;
322         u32 val;
323         int id;
324
325         id = rsnd_mod_id(mod);
326         if (id < 0 || id >= ARRAY_SIZE(routes))
327                 return -EIO;
328
329         /*
330          * SRC_ROUTE_SELECT
331          */
332         val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
333         val = val               << routes[id].shift;
334         mask = routes[id].mask  << routes[id].shift;
335
336         rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
337
338         return 0;
339 }
340
341 static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
342                                             struct rsnd_dai *rdai,
343                                             struct rsnd_dai_stream *io)
344 {
345         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
346         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
347         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
348         u32 convert_rate = rsnd_scu_convert_rate(scu);
349         u32 mask;
350         u32 val;
351         int shift;
352         int id = rsnd_mod_id(mod);
353         int ret;
354
355         /*
356          * SRC_TIMING_SELECT
357          */
358         shift   = (id % 4) * 8;
359         mask    = 0x1F << shift;
360
361         /*
362          * ADG is used as source clock if SRC was used,
363          * then, SSI WS is used as destination clock.
364          * SSI WS is used as source clock if SRC is not used
365          * (when playback, source/destination become reverse when capture)
366          */
367         ret = 0;
368         if (convert_rate) {
369                 /* use ADG */
370                 val = 0;
371                 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
372                                                     runtime->rate,
373                                                     convert_rate);
374         } else if (8 == id) {
375                 /* use SSI WS, but SRU8 is special */
376                 val = id << shift;
377         } else {
378                 /* use SSI WS */
379                 val = (id + 1) << shift;
380         }
381
382         if (ret < 0)
383                 return ret;
384
385         switch (id / 4) {
386         case 0:
387                 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
388                 break;
389         case 1:
390                 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
391                 break;
392         case 2:
393                 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
394                 break;
395         }
396
397         return 0;
398 }
399
400 static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod,
401                                           struct rsnd_dai *rdai,
402                                           struct rsnd_dai_stream *io)
403 {
404         int ret;
405
406         ret = rsnd_scu_set_convert_rate(mod, rdai, io);
407         if (ret < 0)
408                 return ret;
409
410         /* Select SRC mode (fixed value) */
411         rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
412
413         /* Set the restriction value of the FS ratio (98%) */
414         rsnd_mod_write(mod, SRC_MNFSR,
415                        rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
416
417         /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
418
419         return 0;
420 }
421
422 static int rsnd_scu_init_gen1(struct rsnd_mod *mod,
423                               struct rsnd_dai *rdai,
424                               struct rsnd_dai_stream *io)
425 {
426         int ret;
427
428         ret = rsnd_scu_init(mod, rdai, io);
429         if (ret < 0)
430                 return ret;
431
432         ret = rsnd_src_set_route_gen1(mod, rdai, io);
433         if (ret < 0)
434                 return ret;
435
436         ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io);
437         if (ret < 0)
438                 return ret;
439
440         ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
441         if (ret < 0)
442                 return ret;
443
444         return 0;
445 }
446
447 static int rsnd_scu_start_gen1(struct rsnd_mod *mod,
448                                struct rsnd_dai *rdai,
449                                struct rsnd_dai_stream *io)
450 {
451         int id = rsnd_mod_id(mod);
452
453         rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
454
455         return rsnd_scu_start(mod, rdai, io);
456 }
457
458 static int rsnd_scu_stop_gen1(struct rsnd_mod *mod,
459                               struct rsnd_dai *rdai,
460                               struct rsnd_dai_stream *io)
461 {
462         int id = rsnd_mod_id(mod);
463
464         rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
465
466         return rsnd_scu_stop(mod, rdai, io);
467 }
468
469 static struct rsnd_mod_ops rsnd_scu_gen1_ops = {
470         .name   = "sru (gen1)",
471         .init   = rsnd_scu_init_gen1,
472         .quit   = rsnd_scu_quit,
473         .start  = rsnd_scu_start_gen1,
474         .stop   = rsnd_scu_stop_gen1,
475 };
476
477 static struct rsnd_mod_ops rsnd_scu_non_gen1_ops = {
478         .name   = "non-sru (gen1)",
479         .init   = rsnd_scu_ssi_mode_init,
480 };
481
482 /*
483  *              Gen2 functions
484  */
485 static int rsnd_scu_set_convert_rate_gen2(struct rsnd_mod *mod,
486                                           struct rsnd_dai *rdai,
487                                           struct rsnd_dai_stream *io)
488 {
489         int ret;
490
491         ret = rsnd_scu_set_convert_rate(mod, rdai, io);
492         if (ret < 0)
493                 return ret;
494
495         rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR));
496         rsnd_mod_write(mod, SSI_BUSIF_MODE,  rsnd_mod_read(mod, SRC_BUSIF_MODE));
497
498         rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
499
500         rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
501         rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
502
503         return 0;
504 }
505
506 static int rsnd_scu_set_convert_timing_gen2(struct rsnd_mod *mod,
507                                             struct rsnd_dai *rdai,
508                                             struct rsnd_dai_stream *io)
509 {
510         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
511         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
512         u32 convert_rate = rsnd_scu_convert_rate(scu);
513         int ret;
514
515         if (convert_rate)
516                 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
517                                                     runtime->rate,
518                                                     convert_rate);
519         else
520                 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
521
522         return ret;
523 }
524
525 static int rsnd_scu_init_gen2(struct rsnd_mod *mod,
526                               struct rsnd_dai *rdai,
527                               struct rsnd_dai_stream *io)
528 {
529         int ret;
530
531         ret = rsnd_scu_init(mod, rdai, io);
532         if (ret < 0)
533                 return ret;
534
535         ret = rsnd_scu_set_convert_rate_gen2(mod, rdai, io);
536         if (ret < 0)
537                 return ret;
538
539         ret = rsnd_scu_set_convert_timing_gen2(mod, rdai, io);
540         if (ret < 0)
541                 return ret;
542
543         return 0;
544 }
545
546 static int rsnd_scu_start_gen2(struct rsnd_mod *mod,
547                                struct rsnd_dai *rdai,
548                                struct rsnd_dai_stream *io)
549 {
550         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
551
552         rsnd_dma_start(rsnd_mod_to_dma(&scu->mod));
553
554         rsnd_mod_write(mod, SSI_CTRL, 0x1);
555         rsnd_mod_write(mod, SRC_CTRL, 0x11);
556
557         return rsnd_scu_start(mod, rdai, io);
558 }
559
560 static int rsnd_scu_stop_gen2(struct rsnd_mod *mod,
561                               struct rsnd_dai *rdai,
562                               struct rsnd_dai_stream *io)
563 {
564         struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
565
566         rsnd_mod_write(mod, SSI_CTRL, 0);
567         rsnd_mod_write(mod, SRC_CTRL, 0);
568
569         rsnd_dma_stop(rsnd_mod_to_dma(&scu->mod));
570
571         return rsnd_scu_stop(mod, rdai, io);
572 }
573
574 static struct rsnd_mod_ops rsnd_scu_gen2_ops = {
575         .name   = "scu (gen2)",
576         .init   = rsnd_scu_init_gen2,
577         .quit   = rsnd_scu_quit,
578         .start  = rsnd_scu_start_gen2,
579         .stop   = rsnd_scu_stop_gen2,
580 };
581
582 static int rsnd_scu_start_non_gen2(struct rsnd_mod *mod,
583                                    struct rsnd_dai *rdai,
584                                    struct rsnd_dai_stream *io)
585 {
586         struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
587
588         /* enable PIO interrupt */
589         rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
590
591         return 0;
592 }
593
594 static struct rsnd_mod_ops rsnd_scu_non_gen2_ops = {
595         .name   = "non-scu (gen2)",
596         .init   = rsnd_scu_ssi_mode_init,
597         .start  = rsnd_scu_start_non_gen2,
598 };
599
600 struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
601 {
602         if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
603                 id = 0;
604
605         return &((struct rsnd_scu *)(priv->scu) + id)->mod;
606 }
607
608 int rsnd_scu_probe(struct platform_device *pdev,
609                    struct rsnd_priv *priv)
610 {
611         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
612         struct device *dev = rsnd_priv_to_dev(priv);
613         struct rsnd_scu *scu;
614         struct rsnd_mod_ops *ops;
615         struct clk *clk;
616         char name[RSND_SCU_NAME_SIZE];
617         int i, nr;
618
619         /*
620          * init SCU
621          */
622         nr      = info->scu_info_nr;
623         scu     = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
624         if (!scu) {
625                 dev_err(dev, "SCU allocate failed\n");
626                 return -ENOMEM;
627         }
628
629         priv->scu_nr    = nr;
630         priv->scu       = scu;
631
632         for_each_rsnd_scu(scu, priv, i) {
633                 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
634
635                 clk = devm_clk_get(dev, name);
636                 if (IS_ERR(clk))
637                         return PTR_ERR(clk);
638
639                 scu->info = &info->scu_info[i];
640                 scu->clk = clk;
641
642                 ops = &rsnd_scu_non_ops;
643                 if (rsnd_scu_hpbif_is_enable(scu)) {
644                         if (rsnd_is_gen1(priv))
645                                 ops = &rsnd_scu_gen1_ops;
646                         if (rsnd_is_gen2(priv)) {
647                                 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, i);
648                                 int ret = rsnd_dma_init(priv,
649                                                 rsnd_mod_to_dma(&scu->mod),
650                                                 rsnd_ssi_is_play(ssi),
651                                                 scu->info->dma_id);
652                                 if (ret < 0)
653                                         return ret;
654
655                                 ops = &rsnd_scu_gen2_ops;
656                         }
657                 } else {
658                         if (rsnd_is_gen1(priv))
659                                 ops = &rsnd_scu_non_gen1_ops;
660                         if (rsnd_is_gen2(priv))
661                                 ops = &rsnd_scu_non_gen2_ops;
662                 }
663
664                 rsnd_mod_init(priv, &scu->mod, ops, RSND_MOD_SCU, i);
665
666                 dev_dbg(dev, "SCU%d probed\n", i);
667         }
668
669         return 0;
670 }
671
672 void rsnd_scu_remove(struct platform_device *pdev,
673                      struct rsnd_priv *priv)
674 {
675         struct rsnd_scu *scu;
676         int i;
677
678         for_each_rsnd_scu(scu, priv, i) {
679                 if (rsnd_scu_dma_available(scu))
680                         rsnd_dma_quit(priv, rsnd_mod_to_dma(&scu->mod));
681         }
682 }