GNU Linux-libre 4.4.283-gnu1
[releases.git] / sound / soc / sh / rcar / core.c
1 /*
2  * Renesas R-Car SRU/SCU/SSIU/SSI support
3  *
4  * Copyright (C) 2013 Renesas Solutions Corp.
5  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * Based on fsi.c
8  * Kuninori Morimoto <morimoto.kuninori@renesas.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 /*
16  * Renesas R-Car sound device structure
17  *
18  * Gen1
19  *
20  * SRU          : Sound Routing Unit
21  *  - SRC       : Sampling Rate Converter
22  *  - CMD
23  *    - CTU     : Channel Count Conversion Unit
24  *    - MIX     : Mixer
25  *    - DVC     : Digital Volume and Mute Function
26  *  - SSI       : Serial Sound Interface
27  *
28  * Gen2
29  *
30  * SCU          : Sampling Rate Converter Unit
31  *  - SRC       : Sampling Rate Converter
32  *  - CMD
33  *   - CTU      : Channel Count Conversion Unit
34  *   - MIX      : Mixer
35  *   - DVC      : Digital Volume and Mute Function
36  * SSIU         : Serial Sound Interface Unit
37  *  - SSI       : Serial Sound Interface
38  */
39
40 /*
41  *      driver data Image
42  *
43  * rsnd_priv
44  *   |
45  *   | ** this depends on Gen1/Gen2
46  *   |
47  *   +- gen
48  *   |
49  *   | ** these depend on data path
50  *   | ** gen and platform data control it
51  *   |
52  *   +- rdai[0]
53  *   |   |               sru     ssiu      ssi
54  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
55  *   |   |
56  *   |   |               sru     ssiu      ssi
57  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
58  *   |
59  *   +- rdai[1]
60  *   |   |               sru     ssiu      ssi
61  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
62  *   |   |
63  *   |   |               sru     ssiu      ssi
64  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
65  *   ...
66  *   |
67  *   | ** these control ssi
68  *   |
69  *   +- ssi
70  *   |  |
71  *   |  +- ssi[0]
72  *   |  +- ssi[1]
73  *   |  +- ssi[2]
74  *   |  ...
75  *   |
76  *   | ** these control src
77  *   |
78  *   +- src
79  *      |
80  *      +- src[0]
81  *      +- src[1]
82  *      +- src[2]
83  *      ...
84  *
85  *
86  * for_each_rsnd_dai(xx, priv, xx)
87  *  rdai[0] => rdai[1] => rdai[2] => ...
88  *
89  * for_each_rsnd_mod(xx, rdai, xx)
90  *  [mod] => [mod] => [mod] => ...
91  *
92  * rsnd_dai_call(xxx, fn )
93  *  [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
94  *
95  */
96 #include <linux/pm_runtime.h>
97 #include "rsnd.h"
98
99 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
101
102 static const struct rsnd_of_data rsnd_of_data_gen1 = {
103         .flags = RSND_GEN1,
104 };
105
106 static const struct rsnd_of_data rsnd_of_data_gen2 = {
107         .flags = RSND_GEN2,
108 };
109
110 static const struct of_device_id rsnd_of_match[] = {
111         { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
112         { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
113         { .compatible = "renesas,rcar_sound-gen3", .data = &rsnd_of_data_gen2 }, /* gen2 compatible */
114         {},
115 };
116 MODULE_DEVICE_TABLE(of, rsnd_of_match);
117
118 /*
119  *      rsnd_platform functions
120  */
121 #define rsnd_platform_call(priv, dai, func, param...)   \
122         (!(priv->info->func) ? 0 :              \
123          priv->info->func(param))
124
125 #define rsnd_is_enable_path(io, name) \
126         ((io)->info ? (io)->info->name : NULL)
127 #define rsnd_info_id(priv, io, name) \
128         ((io)->info->name - priv->info->name##_info)
129
130 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
131 {
132         if (mod->type != type) {
133                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
134                 struct device *dev = rsnd_priv_to_dev(priv);
135
136                 dev_warn(dev, "%s[%d] is not your expected module\n",
137                          rsnd_mod_name(mod), rsnd_mod_id(mod));
138         }
139 }
140
141 /*
142  *      rsnd_mod functions
143  */
144 char *rsnd_mod_name(struct rsnd_mod *mod)
145 {
146         if (!mod || !mod->ops)
147                 return "unknown";
148
149         return mod->ops->name;
150 }
151
152 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
153                                   struct rsnd_mod *mod)
154 {
155         if (!mod || !mod->ops || !mod->ops->dma_req)
156                 return NULL;
157
158         return mod->ops->dma_req(io, mod);
159 }
160
161 int rsnd_mod_init(struct rsnd_priv *priv,
162                   struct rsnd_mod *mod,
163                    struct rsnd_mod_ops *ops,
164                    struct clk *clk,
165                    enum rsnd_mod_type type,
166                    int id)
167 {
168         int ret = clk_prepare(clk);
169
170         if (ret)
171                 return ret;
172
173         mod->id         = id;
174         mod->ops        = ops;
175         mod->type       = type;
176         mod->clk        = clk;
177         mod->priv       = priv;
178
179         return ret;
180 }
181
182 void rsnd_mod_quit(struct rsnd_mod *mod)
183 {
184         if (mod->clk)
185                 clk_unprepare(mod->clk);
186 }
187
188 void rsnd_mod_interrupt(struct rsnd_mod *mod,
189                         void (*callback)(struct rsnd_mod *mod,
190                                          struct rsnd_dai_stream *io))
191 {
192         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
193         struct rsnd_dai_stream *io;
194         struct rsnd_dai *rdai;
195         int i;
196
197         for_each_rsnd_dai(rdai, priv, i) {
198                 io = &rdai->playback;
199                 if (mod == io->mod[mod->type])
200                         callback(mod, io);
201
202                 io = &rdai->capture;
203                 if (mod == io->mod[mod->type])
204                         callback(mod, io);
205         }
206 }
207
208 int rsnd_io_is_working(struct rsnd_dai_stream *io)
209 {
210         /* see rsnd_dai_stream_init/quit() */
211         return !!io->substream;
212 }
213
214 /*
215  *      ADINR function
216  */
217 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
218 {
219         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
220         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
221         struct device *dev = rsnd_priv_to_dev(priv);
222         u32 adinr = runtime->channels;
223
224         switch (runtime->sample_bits) {
225         case 16:
226                 adinr |= (8 << 16);
227                 break;
228         case 32:
229                 adinr |= (0 << 16);
230                 break;
231         default:
232                 dev_warn(dev, "not supported sample bits\n");
233                 return 0;
234         }
235
236         return adinr;
237 }
238
239 u32 rsnd_get_adinr_chan(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
240 {
241         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
242         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
243         struct device *dev = rsnd_priv_to_dev(priv);
244         u32 chan = runtime->channels;
245
246         switch (chan) {
247         case 1:
248         case 2:
249         case 4:
250         case 6:
251         case 8:
252                 break;
253         default:
254                 dev_warn(dev, "not supported channel\n");
255                 chan = 0;
256                 break;
257         }
258
259         return chan;
260 }
261
262 /*
263  *      DALIGN function
264  */
265 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
266 {
267         struct rsnd_mod *src = rsnd_io_to_mod_src(io);
268         struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
269         struct rsnd_mod *target = src ? src : ssi;
270         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
271         u32 val = 0x76543210;
272         u32 mask = ~0;
273
274         mask <<= runtime->channels * 4;
275         val = val & mask;
276
277         switch (runtime->sample_bits) {
278         case 16:
279                 val |= 0x67452301 & ~mask;
280                 break;
281         case 32:
282                 val |= 0x76543210 & ~mask;
283                 break;
284         }
285
286         /*
287          * exchange channeles on SRC if possible,
288          * otherwise, R/L volume settings on DVC
289          * changes inverted channels
290          */
291         if (mod == target)
292                 return val;
293         else
294                 return 0x76543210;
295 }
296
297 /*
298  *      rsnd_dai functions
299  */
300 #define rsnd_mod_call(mod, io, func, param...)                  \
301 ({                                                              \
302         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
303         struct device *dev = rsnd_priv_to_dev(priv);            \
304         u32 mask = 0xF << __rsnd_mod_shift_##func;                      \
305         u8 val  = (mod->status >> __rsnd_mod_shift_##func) & 0xF;       \
306         u8 add  = ((val + __rsnd_mod_add_##func) & 0xF);                \
307         int ret = 0;                                                    \
308         int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
309         mod->status = (mod->status & ~mask) +                           \
310                 (add << __rsnd_mod_shift_##func);                       \
311         dev_dbg(dev, "%s[%d]\t0x%08x %s\n",                             \
312                 rsnd_mod_name(mod), rsnd_mod_id(mod),                   \
313                 mod->status, call ? #func : "");                        \
314         if (call)                                                       \
315                 ret = (mod)->ops->func(mod, io, param);                 \
316         ret;                                                            \
317 })
318
319 #define rsnd_dai_call(fn, io, param...)                         \
320 ({                                                              \
321         struct rsnd_mod *mod;                                   \
322         int ret = 0, i;                                         \
323         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
324                 mod = (io)->mod[i];                             \
325                 if (!mod)                                       \
326                         continue;                               \
327                 ret |= rsnd_mod_call(mod, io, fn, param);       \
328         }                                                       \
329         ret;                                                    \
330 })
331
332 static int rsnd_dai_connect(struct rsnd_mod *mod,
333                             struct rsnd_dai_stream *io)
334 {
335         struct rsnd_priv *priv;
336         struct device *dev;
337
338         if (!mod)
339                 return -EIO;
340
341         priv = rsnd_mod_to_priv(mod);
342         dev = rsnd_priv_to_dev(priv);
343
344         io->mod[mod->type] = mod;
345
346         dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
347                 rsnd_mod_name(mod), rsnd_mod_id(mod),
348                 rsnd_io_is_play(io) ? "Playback" : "Capture");
349
350         return 0;
351 }
352
353 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
354                                 struct rsnd_dai_stream *io)
355 {
356         io->mod[mod->type] = NULL;
357 }
358
359 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
360 {
361         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
362                 return NULL;
363
364         return priv->rdai + id;
365 }
366
367 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
368 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
369 {
370         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
371
372         return rsnd_rdai_get(priv, dai->id);
373 }
374
375 /*
376  *      rsnd_soc_dai functions
377  */
378 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
379 {
380         struct snd_pcm_substream *substream = io->substream;
381         struct snd_pcm_runtime *runtime = substream->runtime;
382         int pos = io->byte_pos + additional;
383
384         pos %= (runtime->periods * io->byte_per_period);
385
386         return pos;
387 }
388
389 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
390 {
391         io->byte_pos += byte;
392
393         if (io->byte_pos >= io->next_period_byte) {
394                 struct snd_pcm_substream *substream = io->substream;
395                 struct snd_pcm_runtime *runtime = substream->runtime;
396
397                 io->period_pos++;
398                 io->next_period_byte += io->byte_per_period;
399
400                 if (io->period_pos >= runtime->periods) {
401                         io->byte_pos = 0;
402                         io->period_pos = 0;
403                         io->next_period_byte = io->byte_per_period;
404                 }
405
406                 return true;
407         }
408
409         return false;
410 }
411
412 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
413 {
414         struct snd_pcm_substream *substream = io->substream;
415
416         /*
417          * this function should be called...
418          *
419          * - if rsnd_dai_pointer_update() returns true
420          * - without spin lock
421          */
422
423         snd_pcm_period_elapsed(substream);
424 }
425
426 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
427                                 struct snd_pcm_substream *substream)
428 {
429         struct snd_pcm_runtime *runtime = substream->runtime;
430
431         io->substream           = substream;
432         io->byte_pos            = 0;
433         io->period_pos          = 0;
434         io->byte_per_period     = runtime->period_size *
435                                   runtime->channels *
436                                   samples_to_bytes(runtime, 1);
437         io->next_period_byte    = io->byte_per_period;
438 }
439
440 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
441 {
442         io->substream           = NULL;
443 }
444
445 static
446 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
447 {
448         struct snd_soc_pcm_runtime *rtd = substream->private_data;
449
450         return  rtd->cpu_dai;
451 }
452
453 static
454 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
455                                         struct snd_pcm_substream *substream)
456 {
457         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
458                 return &rdai->playback;
459         else
460                 return &rdai->capture;
461 }
462
463 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
464                             struct snd_soc_dai *dai)
465 {
466         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
467         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
468         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
469         int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
470         int ret;
471         unsigned long flags;
472
473         spin_lock_irqsave(&priv->lock, flags);
474
475         switch (cmd) {
476         case SNDRV_PCM_TRIGGER_START:
477                 rsnd_dai_stream_init(io, substream);
478
479                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
480                 if (ret < 0)
481                         goto dai_trigger_end;
482
483                 ret = rsnd_dai_call(init, io, priv);
484                 if (ret < 0)
485                         goto dai_trigger_end;
486
487                 ret = rsnd_dai_call(start, io, priv);
488                 if (ret < 0)
489                         goto dai_trigger_end;
490                 break;
491         case SNDRV_PCM_TRIGGER_STOP:
492                 ret = rsnd_dai_call(stop, io, priv);
493
494                 ret |= rsnd_dai_call(quit, io, priv);
495
496                 ret |= rsnd_platform_call(priv, dai, stop, ssi_id);
497
498                 rsnd_dai_stream_quit(io);
499                 break;
500         default:
501                 ret = -EINVAL;
502         }
503
504 dai_trigger_end:
505         spin_unlock_irqrestore(&priv->lock, flags);
506
507         return ret;
508 }
509
510 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
511 {
512         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
513
514         /* set master/slave audio interface */
515         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
516         case SND_SOC_DAIFMT_CBM_CFM:
517                 rdai->clk_master = 0;
518                 break;
519         case SND_SOC_DAIFMT_CBS_CFS:
520                 rdai->clk_master = 1; /* codec is slave, cpu is master */
521                 break;
522         default:
523                 return -EINVAL;
524         }
525
526         /* set format */
527         rdai->bit_clk_inv = 0;
528         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
529         case SND_SOC_DAIFMT_I2S:
530                 rdai->sys_delay = 0;
531                 rdai->data_alignment = 0;
532                 rdai->frm_clk_inv = 0;
533                 break;
534         case SND_SOC_DAIFMT_LEFT_J:
535                 rdai->sys_delay = 1;
536                 rdai->data_alignment = 0;
537                 rdai->frm_clk_inv = 1;
538                 break;
539         case SND_SOC_DAIFMT_RIGHT_J:
540                 rdai->sys_delay = 1;
541                 rdai->data_alignment = 1;
542                 rdai->frm_clk_inv = 1;
543                 break;
544         }
545
546         /* set clock inversion */
547         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
548         case SND_SOC_DAIFMT_NB_IF:
549                 rdai->bit_clk_inv =  rdai->bit_clk_inv;
550                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
551                 break;
552         case SND_SOC_DAIFMT_IB_NF:
553                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
554                 rdai->frm_clk_inv =  rdai->frm_clk_inv;
555                 break;
556         case SND_SOC_DAIFMT_IB_IF:
557                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
558                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
559                 break;
560         case SND_SOC_DAIFMT_NB_NF:
561         default:
562                 break;
563         }
564
565         return 0;
566 }
567
568 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
569         .trigger        = rsnd_soc_dai_trigger,
570         .set_fmt        = rsnd_soc_dai_set_fmt,
571 };
572
573 #define rsnd_path_add(priv, io, type)                           \
574 ({                                                              \
575         struct rsnd_mod *mod;                                   \
576         int ret = 0;                                            \
577         int id = -1;                                            \
578                                                                 \
579         if (rsnd_is_enable_path(io, type)) {                    \
580                 id = rsnd_info_id(priv, io, type);              \
581                 if (id >= 0) {                                  \
582                         mod = rsnd_##type##_mod_get(priv, id);  \
583                         ret = rsnd_dai_connect(mod, io);        \
584                 }                                               \
585         }                                                       \
586         ret;                                                    \
587 })
588
589 #define rsnd_path_remove(priv, io, type)                        \
590 {                                                               \
591         struct rsnd_mod *mod;                                   \
592         int id = -1;                                            \
593                                                                 \
594         if (rsnd_is_enable_path(io, type)) {                    \
595                 id = rsnd_info_id(priv, io, type);              \
596                 if (id >= 0) {                                  \
597                         mod = rsnd_##type##_mod_get(priv, id);  \
598                         rsnd_dai_disconnect(mod, io);           \
599                 }                                               \
600         }                                                       \
601 }
602
603 void rsnd_path_parse(struct rsnd_priv *priv,
604                      struct rsnd_dai_stream *io)
605 {
606         struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
607         struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
608         struct rsnd_mod *src = rsnd_io_to_mod_src(io);
609         struct rsnd_mod *cmd;
610         struct device *dev = rsnd_priv_to_dev(priv);
611         u32 data;
612
613         /* Gen1 is not supported */
614         if (rsnd_is_gen1(priv))
615                 return;
616
617         if (!mix && !dvc)
618                 return;
619
620         if (mix) {
621                 struct rsnd_dai *rdai;
622                 int i;
623                 u32 path[] = {
624                         [0] = 0,
625                         [1] = 1 << 0,
626                         [2] = 0,
627                         [3] = 0,
628                         [4] = 0,
629                         [5] = 1 << 8
630                 };
631
632                 /*
633                  * it is assuming that integrater is well understanding about
634                  * data path. Here doesn't check impossible connection,
635                  * like src2 + src5
636                  */
637                 data = 0;
638                 for_each_rsnd_dai(rdai, priv, i) {
639                         io = &rdai->playback;
640                         if (mix == rsnd_io_to_mod_mix(io))
641                                 data |= path[rsnd_mod_id(src)];
642
643                         io = &rdai->capture;
644                         if (mix == rsnd_io_to_mod_mix(io))
645                                 data |= path[rsnd_mod_id(src)];
646                 }
647
648                 /*
649                  * We can't use ctu = rsnd_io_ctu() here.
650                  * Since, ID of dvc/mix are 0 or 1 (= same as CMD number)
651                  * but ctu IDs are 0 - 7 (= CTU00 - CTU13)
652                  */
653                 cmd = mix;
654         } else {
655                 u32 path[] = {
656                         [0] = 0x30000,
657                         [1] = 0x30001,
658                         [2] = 0x40000,
659                         [3] = 0x10000,
660                         [4] = 0x20000,
661                         [5] = 0x40100
662                 };
663
664                 data = path[rsnd_mod_id(src)];
665
666                 cmd = dvc;
667         }
668
669         dev_dbg(dev, "ctu/mix path = 0x%08x", data);
670
671         rsnd_mod_write(cmd, CMD_ROUTE_SLCT, data);
672
673         rsnd_mod_write(cmd, CMD_CTRL, 0x10);
674 }
675
676 static int rsnd_path_init(struct rsnd_priv *priv,
677                           struct rsnd_dai *rdai,
678                           struct rsnd_dai_stream *io)
679 {
680         int ret;
681
682         /*
683          * Gen1 is created by SRU/SSI, and this SRU is base module of
684          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
685          *
686          * Easy image is..
687          *      Gen1 SRU = Gen2 SCU + SSIU + etc
688          *
689          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
690          * using fixed path.
691          */
692
693         /* SSI */
694         ret = rsnd_path_add(priv, io, ssi);
695         if (ret < 0)
696                 return ret;
697
698         /* SRC */
699         ret = rsnd_path_add(priv, io, src);
700         if (ret < 0)
701                 return ret;
702
703         /* CTU */
704         ret = rsnd_path_add(priv, io, ctu);
705         if (ret < 0)
706                 return ret;
707
708         /* MIX */
709         ret = rsnd_path_add(priv, io, mix);
710         if (ret < 0)
711                 return ret;
712
713         /* DVC */
714         ret = rsnd_path_add(priv, io, dvc);
715         if (ret < 0)
716                 return ret;
717
718         return ret;
719 }
720
721 static void rsnd_of_parse_dai(struct platform_device *pdev,
722                               const struct rsnd_of_data *of_data,
723                               struct rsnd_priv *priv)
724 {
725         struct device_node *dai_node,   *dai_np;
726         struct device_node *ssi_node,   *ssi_np;
727         struct device_node *src_node,   *src_np;
728         struct device_node *ctu_node,   *ctu_np;
729         struct device_node *mix_node,   *mix_np;
730         struct device_node *dvc_node,   *dvc_np;
731         struct device_node *playback, *capture;
732         struct rsnd_dai_platform_info *dai_info;
733         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
734         struct device *dev = &pdev->dev;
735         int nr, i;
736         int dai_i, ssi_i, src_i, ctu_i, mix_i, dvc_i;
737
738         if (!of_data)
739                 return;
740
741         dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
742         if (!dai_node)
743                 return;
744
745         nr = of_get_child_count(dai_node);
746         if (!nr)
747                 return;
748
749         dai_info = devm_kzalloc(dev,
750                                 sizeof(struct rsnd_dai_platform_info) * nr,
751                                 GFP_KERNEL);
752         if (!dai_info) {
753                 dev_err(dev, "dai info allocation error\n");
754                 return;
755         }
756
757         info->dai_info_nr       = nr;
758         info->dai_info          = dai_info;
759
760         ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
761         src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
762         ctu_node = of_get_child_by_name(dev->of_node, "rcar_sound,ctu");
763         mix_node = of_get_child_by_name(dev->of_node, "rcar_sound,mix");
764         dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
765
766 #define mod_parse(name)                                                 \
767 if (name##_node) {                                                      \
768         struct rsnd_##name##_platform_info *name##_info;                \
769                                                                         \
770         name##_i = 0;                                                   \
771         for_each_child_of_node(name##_node, name##_np) {                \
772                 name##_info = info->name##_info + name##_i;             \
773                                                                         \
774                 if (name##_np == playback)                              \
775                         dai_info->playback.name = name##_info;          \
776                 if (name##_np == capture)                               \
777                         dai_info->capture.name = name##_info;           \
778                                                                         \
779                 name##_i++;                                             \
780         }                                                               \
781 }
782
783         /*
784          * parse all dai
785          */
786         dai_i = 0;
787         for_each_child_of_node(dai_node, dai_np) {
788                 dai_info = info->dai_info + dai_i;
789
790                 for (i = 0;; i++) {
791
792                         playback = of_parse_phandle(dai_np, "playback", i);
793                         capture  = of_parse_phandle(dai_np, "capture", i);
794
795                         if (!playback && !capture)
796                                 break;
797
798                         mod_parse(ssi);
799                         mod_parse(src);
800                         mod_parse(ctu);
801                         mod_parse(mix);
802                         mod_parse(dvc);
803
804                         of_node_put(playback);
805                         of_node_put(capture);
806                 }
807
808                 dai_i++;
809         }
810 }
811
812 static int rsnd_dai_probe(struct platform_device *pdev,
813                           const struct rsnd_of_data *of_data,
814                           struct rsnd_priv *priv)
815 {
816         struct snd_soc_dai_driver *drv;
817         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
818         struct rsnd_dai *rdai;
819         struct rsnd_ssi_platform_info *pmod, *cmod;
820         struct device *dev = rsnd_priv_to_dev(priv);
821         int dai_nr;
822         int i;
823
824         rsnd_of_parse_dai(pdev, of_data, priv);
825
826         dai_nr = info->dai_info_nr;
827         if (!dai_nr) {
828                 dev_err(dev, "no dai\n");
829                 return -EIO;
830         }
831
832         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
833         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
834         if (!drv || !rdai) {
835                 dev_err(dev, "dai allocate failed\n");
836                 return -ENOMEM;
837         }
838
839         priv->rdai_nr   = dai_nr;
840         priv->daidrv    = drv;
841         priv->rdai      = rdai;
842
843         for (i = 0; i < dai_nr; i++) {
844
845                 pmod = info->dai_info[i].playback.ssi;
846                 cmod = info->dai_info[i].capture.ssi;
847
848                 /*
849                  *      init rsnd_dai
850                  */
851                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
852                 rdai[i].priv = priv;
853
854                 /*
855                  *      init snd_soc_dai_driver
856                  */
857                 drv[i].name     = rdai[i].name;
858                 drv[i].ops      = &rsnd_soc_dai_ops;
859                 if (pmod) {
860                         snprintf(rdai[i].playback.name, RSND_DAI_NAME_SIZE,
861                                  "DAI%d Playback", i);
862
863                         drv[i].playback.rates           = RSND_RATES;
864                         drv[i].playback.formats         = RSND_FMTS;
865                         drv[i].playback.channels_min    = 2;
866                         drv[i].playback.channels_max    = 2;
867                         drv[i].playback.stream_name     = rdai[i].playback.name;
868
869                         rdai[i].playback.info = &info->dai_info[i].playback;
870                         rdai[i].playback.rdai = rdai + i;
871                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
872                 }
873                 if (cmod) {
874                         snprintf(rdai[i].capture.name, RSND_DAI_NAME_SIZE,
875                                  "DAI%d Capture", i);
876
877                         drv[i].capture.rates            = RSND_RATES;
878                         drv[i].capture.formats          = RSND_FMTS;
879                         drv[i].capture.channels_min     = 2;
880                         drv[i].capture.channels_max     = 2;
881                         drv[i].capture.stream_name      = rdai[i].capture.name;
882
883                         rdai[i].capture.info = &info->dai_info[i].capture;
884                         rdai[i].capture.rdai = rdai + i;
885                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
886                 }
887
888                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
889                         pmod ? "play"    : " -- ",
890                         cmod ? "capture" : "  --   ");
891         }
892
893         return 0;
894 }
895
896 /*
897  *              pcm ops
898  */
899 static struct snd_pcm_hardware rsnd_pcm_hardware = {
900         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
901                         SNDRV_PCM_INFO_MMAP             |
902                         SNDRV_PCM_INFO_MMAP_VALID,
903         .buffer_bytes_max       = 64 * 1024,
904         .period_bytes_min       = 32,
905         .period_bytes_max       = 8192,
906         .periods_min            = 1,
907         .periods_max            = 32,
908         .fifo_size              = 256,
909 };
910
911 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
912 {
913         struct snd_pcm_runtime *runtime = substream->runtime;
914         int ret = 0;
915
916         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
917
918         ret = snd_pcm_hw_constraint_integer(runtime,
919                                             SNDRV_PCM_HW_PARAM_PERIODS);
920
921         return ret;
922 }
923
924 static int rsnd_hw_params(struct snd_pcm_substream *substream,
925                          struct snd_pcm_hw_params *hw_params)
926 {
927         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
928         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
929         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
930         int ret;
931
932         ret = rsnd_dai_call(hw_params, io, substream, hw_params);
933         if (ret)
934                 return ret;
935
936         return snd_pcm_lib_malloc_pages(substream,
937                                         params_buffer_bytes(hw_params));
938 }
939
940 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
941 {
942         struct snd_pcm_runtime *runtime = substream->runtime;
943         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
944         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
945         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
946
947         return bytes_to_frames(runtime, io->byte_pos);
948 }
949
950 static struct snd_pcm_ops rsnd_pcm_ops = {
951         .open           = rsnd_pcm_open,
952         .ioctl          = snd_pcm_lib_ioctl,
953         .hw_params      = rsnd_hw_params,
954         .hw_free        = snd_pcm_lib_free_pages,
955         .pointer        = rsnd_pointer,
956 };
957
958 /*
959  *              snd_kcontrol
960  */
961 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
962 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
963                            struct snd_ctl_elem_info *uinfo)
964 {
965         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
966
967         if (cfg->texts) {
968                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
969                 uinfo->count = cfg->size;
970                 uinfo->value.enumerated.items = cfg->max;
971                 if (uinfo->value.enumerated.item >= cfg->max)
972                         uinfo->value.enumerated.item = cfg->max - 1;
973                 strlcpy(uinfo->value.enumerated.name,
974                         cfg->texts[uinfo->value.enumerated.item],
975                         sizeof(uinfo->value.enumerated.name));
976         } else {
977                 uinfo->count = cfg->size;
978                 uinfo->value.integer.min = 0;
979                 uinfo->value.integer.max = cfg->max;
980                 uinfo->type = (cfg->max == 1) ?
981                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
982                         SNDRV_CTL_ELEM_TYPE_INTEGER;
983         }
984
985         return 0;
986 }
987
988 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
989                           struct snd_ctl_elem_value *uc)
990 {
991         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
992         int i;
993
994         for (i = 0; i < cfg->size; i++)
995                 if (cfg->texts)
996                         uc->value.enumerated.item[i] = cfg->val[i];
997                 else
998                         uc->value.integer.value[i] = cfg->val[i];
999
1000         return 0;
1001 }
1002
1003 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1004                           struct snd_ctl_elem_value *uc)
1005 {
1006         struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1007         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1008         int i, change = 0;
1009
1010         for (i = 0; i < cfg->size; i++) {
1011                 if (cfg->texts) {
1012                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1013                         cfg->val[i] = uc->value.enumerated.item[i];
1014                 } else {
1015                         change |= (uc->value.integer.value[i] != cfg->val[i]);
1016                         cfg->val[i] = uc->value.integer.value[i];
1017                 }
1018         }
1019
1020         if (change && cfg->update)
1021                 cfg->update(cfg->io, mod);
1022
1023         return change;
1024 }
1025
1026 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
1027                             struct rsnd_dai_stream *io,
1028                             struct snd_soc_pcm_runtime *rtd,
1029                             const unsigned char *name,
1030                             struct rsnd_kctrl_cfg *cfg,
1031                             void (*update)(struct rsnd_dai_stream *io,
1032                                            struct rsnd_mod *mod))
1033 {
1034         struct snd_soc_card *soc_card = rtd->card;
1035         struct snd_card *card = rtd->card->snd_card;
1036         struct snd_kcontrol *kctrl;
1037         struct snd_kcontrol_new knew = {
1038                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
1039                 .name           = name,
1040                 .info           = rsnd_kctrl_info,
1041                 .index          = rtd - soc_card->rtd,
1042                 .get            = rsnd_kctrl_get,
1043                 .put            = rsnd_kctrl_put,
1044                 .private_value  = (unsigned long)cfg,
1045         };
1046         int ret;
1047
1048         kctrl = snd_ctl_new1(&knew, mod);
1049         if (!kctrl)
1050                 return -ENOMEM;
1051
1052         ret = snd_ctl_add(card, kctrl);
1053         if (ret < 0)
1054                 return ret;
1055
1056         cfg->update = update;
1057         cfg->card = card;
1058         cfg->kctrl = kctrl;
1059         cfg->io = io;
1060
1061         return 0;
1062 }
1063
1064 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1065 {
1066         snd_ctl_remove(cfg->card, cfg->kctrl);
1067 }
1068
1069 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1070                      struct rsnd_dai_stream *io,
1071                      struct snd_soc_pcm_runtime *rtd,
1072                      const unsigned char *name,
1073                      void (*update)(struct rsnd_dai_stream *io,
1074                                     struct rsnd_mod *mod),
1075                      struct rsnd_kctrl_cfg_m *_cfg,
1076                      u32 max)
1077 {
1078         _cfg->cfg.max   = max;
1079         _cfg->cfg.size  = RSND_DVC_CHANNELS;
1080         _cfg->cfg.val   = _cfg->val;
1081         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1082 }
1083
1084 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1085                      struct rsnd_dai_stream *io,
1086                      struct snd_soc_pcm_runtime *rtd,
1087                      const unsigned char *name,
1088                      void (*update)(struct rsnd_dai_stream *io,
1089                                     struct rsnd_mod *mod),
1090                      struct rsnd_kctrl_cfg_s *_cfg,
1091                      u32 max)
1092 {
1093         _cfg->cfg.max   = max;
1094         _cfg->cfg.size  = 1;
1095         _cfg->cfg.val   = &_cfg->val;
1096         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1097 }
1098
1099 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1100                      struct rsnd_dai_stream *io,
1101                      struct snd_soc_pcm_runtime *rtd,
1102                      const unsigned char *name,
1103                      struct rsnd_kctrl_cfg_s *_cfg,
1104                      void (*update)(struct rsnd_dai_stream *io,
1105                                     struct rsnd_mod *mod),
1106                      const char * const *texts,
1107                      u32 max)
1108 {
1109         _cfg->cfg.max   = max;
1110         _cfg->cfg.size  = 1;
1111         _cfg->cfg.val   = &_cfg->val;
1112         _cfg->cfg.texts = texts;
1113         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1114 }
1115
1116 /*
1117  *              snd_soc_platform
1118  */
1119
1120 #define PREALLOC_BUFFER         (32 * 1024)
1121 #define PREALLOC_BUFFER_MAX     (32 * 1024)
1122
1123 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1124 {
1125         struct snd_soc_dai *dai = rtd->cpu_dai;
1126         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1127         int ret;
1128
1129         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1130         if (ret)
1131                 return ret;
1132
1133         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1134         if (ret)
1135                 return ret;
1136
1137         return snd_pcm_lib_preallocate_pages_for_all(
1138                 rtd->pcm,
1139                 SNDRV_DMA_TYPE_DEV,
1140                 rtd->card->snd_card->dev,
1141                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1142 }
1143
1144 static struct snd_soc_platform_driver rsnd_soc_platform = {
1145         .ops            = &rsnd_pcm_ops,
1146         .pcm_new        = rsnd_pcm_new,
1147 };
1148
1149 static const struct snd_soc_component_driver rsnd_soc_component = {
1150         .name           = "rsnd",
1151 };
1152
1153 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1154                                        struct rsnd_dai_stream *io)
1155 {
1156         int ret;
1157
1158         ret = rsnd_dai_call(probe, io, priv);
1159         if (ret == -EAGAIN) {
1160                 /*
1161                  * Fallback to PIO mode
1162                  */
1163
1164                 /*
1165                  * call "remove" for SSI/SRC/DVC
1166                  * SSI will be switch to PIO mode if it was DMA mode
1167                  * see
1168                  *      rsnd_dma_init()
1169                  *      rsnd_ssi_fallback()
1170                  */
1171                 rsnd_dai_call(remove, io, priv);
1172
1173                 /*
1174                  * remove SRC/DVC from DAI,
1175                  */
1176                 rsnd_path_remove(priv, io, src);
1177                 rsnd_path_remove(priv, io, dvc);
1178
1179                 /*
1180                  * fallback
1181                  */
1182                 rsnd_dai_call(fallback, io, priv);
1183
1184                 /*
1185                  * retry to "probe".
1186                  * DAI has SSI which is PIO mode only now.
1187                  */
1188                 ret = rsnd_dai_call(probe, io, priv);
1189         }
1190
1191         return ret;
1192 }
1193
1194 /*
1195  *      rsnd probe
1196  */
1197 static int rsnd_probe(struct platform_device *pdev)
1198 {
1199         struct rcar_snd_info *info;
1200         struct rsnd_priv *priv;
1201         struct device *dev = &pdev->dev;
1202         struct rsnd_dai *rdai;
1203         const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1204         const struct rsnd_of_data *of_data;
1205         int (*probe_func[])(struct platform_device *pdev,
1206                             const struct rsnd_of_data *of_data,
1207                             struct rsnd_priv *priv) = {
1208                 rsnd_gen_probe,
1209                 rsnd_dma_probe,
1210                 rsnd_ssi_probe,
1211                 rsnd_src_probe,
1212                 rsnd_ctu_probe,
1213                 rsnd_mix_probe,
1214                 rsnd_dvc_probe,
1215                 rsnd_adg_probe,
1216                 rsnd_dai_probe,
1217         };
1218         int ret, i;
1219
1220         info = devm_kzalloc(&pdev->dev, sizeof(struct rcar_snd_info),
1221                             GFP_KERNEL);
1222         if (!info)
1223                 return -ENOMEM;
1224         of_data = of_id->data;
1225
1226         /*
1227          *      init priv data
1228          */
1229         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1230         if (!priv) {
1231                 dev_err(dev, "priv allocate failed\n");
1232                 return -ENODEV;
1233         }
1234
1235         priv->pdev      = pdev;
1236         priv->info      = info;
1237         spin_lock_init(&priv->lock);
1238
1239         /*
1240          *      init each module
1241          */
1242         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1243                 ret = probe_func[i](pdev, of_data, priv);
1244                 if (ret)
1245                         return ret;
1246         }
1247
1248         for_each_rsnd_dai(rdai, priv, i) {
1249                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1250                 if (ret)
1251                         goto exit_snd_probe;
1252
1253                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1254                 if (ret)
1255                         goto exit_snd_probe;
1256         }
1257
1258         dev_set_drvdata(dev, priv);
1259
1260         /*
1261          *      asoc register
1262          */
1263         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1264         if (ret < 0) {
1265                 dev_err(dev, "cannot snd soc register\n");
1266                 return ret;
1267         }
1268
1269         ret = snd_soc_register_component(dev, &rsnd_soc_component,
1270                                          priv->daidrv, rsnd_rdai_nr(priv));
1271         if (ret < 0) {
1272                 dev_err(dev, "cannot snd dai register\n");
1273                 goto exit_snd_soc;
1274         }
1275
1276         pm_runtime_enable(dev);
1277
1278         dev_info(dev, "probed\n");
1279         return ret;
1280
1281 exit_snd_soc:
1282         snd_soc_unregister_platform(dev);
1283 exit_snd_probe:
1284         for_each_rsnd_dai(rdai, priv, i) {
1285                 rsnd_dai_call(remove, &rdai->playback, priv);
1286                 rsnd_dai_call(remove, &rdai->capture, priv);
1287         }
1288
1289         return ret;
1290 }
1291
1292 static int rsnd_remove(struct platform_device *pdev)
1293 {
1294         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1295         struct rsnd_dai *rdai;
1296         void (*remove_func[])(struct platform_device *pdev,
1297                               struct rsnd_priv *priv) = {
1298                 rsnd_ssi_remove,
1299                 rsnd_src_remove,
1300                 rsnd_ctu_remove,
1301                 rsnd_mix_remove,
1302                 rsnd_dvc_remove,
1303         };
1304         int ret = 0, i;
1305
1306         pm_runtime_disable(&pdev->dev);
1307
1308         for_each_rsnd_dai(rdai, priv, i) {
1309                 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1310                 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1311         }
1312
1313         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1314                 remove_func[i](pdev, priv);
1315
1316         snd_soc_unregister_component(&pdev->dev);
1317         snd_soc_unregister_platform(&pdev->dev);
1318
1319         return ret;
1320 }
1321
1322 static struct platform_driver rsnd_driver = {
1323         .driver = {
1324                 .name   = "rcar_sound",
1325                 .of_match_table = rsnd_of_match,
1326         },
1327         .probe          = rsnd_probe,
1328         .remove         = rsnd_remove,
1329 };
1330 module_platform_driver(rsnd_driver);
1331
1332 MODULE_LICENSE("GPL");
1333 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1334 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1335 MODULE_ALIAS("platform:rcar-pcm-audio");