]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/sh/rcar/core.c
ASoC: rsnd: avoid pointless loop in rsnd_mod_interrupt()
[karo-tx-linux.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(idx, io, func, param...)                  \
301 ({                                                              \
302         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
303         struct rsnd_mod *mod = (io)->mod[idx];                  \
304         struct device *dev = rsnd_priv_to_dev(priv);            \
305         u32 *status = (io)->mod_status + idx;                   \
306         u32 mask = 0xF << __rsnd_mod_shift_##func;                      \
307         u8 val  = (*status >> __rsnd_mod_shift_##func) & 0xF;           \
308         u8 add  = ((val + __rsnd_mod_add_##func) & 0xF);                \
309         int ret = 0;                                                    \
310         int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
311         *status = (*status & ~mask) +                                   \
312                 (add << __rsnd_mod_shift_##func);                       \
313         dev_dbg(dev, "%s[%d]\t0x%08x %s\n",                             \
314                 rsnd_mod_name(mod), rsnd_mod_id(mod),                   \
315                 *status, call ? #func : "");                            \
316         if (call)                                                       \
317                 ret = (mod)->ops->func(mod, io, param);                 \
318         ret;                                                            \
319 })
320
321 #define rsnd_dai_call(fn, io, param...)                         \
322 ({                                                              \
323         struct rsnd_mod *mod;                                   \
324         int ret = 0, i;                                         \
325         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
326                 mod = (io)->mod[i];                             \
327                 if (!mod)                                       \
328                         continue;                               \
329                 ret |= rsnd_mod_call(i, io, fn, param);         \
330         }                                                       \
331         ret;                                                    \
332 })
333
334 int rsnd_dai_connect(struct rsnd_mod *mod,
335                      struct rsnd_dai_stream *io,
336                      enum rsnd_mod_type type)
337 {
338         struct rsnd_priv *priv;
339         struct device *dev;
340
341         if (!mod)
342                 return -EIO;
343
344         priv = rsnd_mod_to_priv(mod);
345         dev = rsnd_priv_to_dev(priv);
346
347         io->mod[type] = mod;
348
349         dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
350                 rsnd_mod_name(mod), rsnd_mod_id(mod),
351                 rsnd_io_is_play(io) ? "Playback" : "Capture");
352
353         return 0;
354 }
355
356 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
357                                 struct rsnd_dai_stream *io,
358                                 enum rsnd_mod_type type)
359 {
360         io->mod[type] = NULL;
361 }
362
363 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
364 {
365         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
366                 return NULL;
367
368         return priv->rdai + id;
369 }
370
371 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
372 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
373 {
374         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
375
376         return rsnd_rdai_get(priv, dai->id);
377 }
378
379 /*
380  *      rsnd_soc_dai functions
381  */
382 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
383 {
384         struct snd_pcm_substream *substream = io->substream;
385         struct snd_pcm_runtime *runtime = substream->runtime;
386         int pos = io->byte_pos + additional;
387
388         pos %= (runtime->periods * io->byte_per_period);
389
390         return pos;
391 }
392
393 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
394 {
395         io->byte_pos += byte;
396
397         if (io->byte_pos >= io->next_period_byte) {
398                 struct snd_pcm_substream *substream = io->substream;
399                 struct snd_pcm_runtime *runtime = substream->runtime;
400
401                 io->period_pos++;
402                 io->next_period_byte += io->byte_per_period;
403
404                 if (io->period_pos >= runtime->periods) {
405                         io->byte_pos = 0;
406                         io->period_pos = 0;
407                         io->next_period_byte = io->byte_per_period;
408                 }
409
410                 return true;
411         }
412
413         return false;
414 }
415
416 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
417 {
418         struct snd_pcm_substream *substream = io->substream;
419
420         /*
421          * this function should be called...
422          *
423          * - if rsnd_dai_pointer_update() returns true
424          * - without spin lock
425          */
426
427         snd_pcm_period_elapsed(substream);
428 }
429
430 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
431                                 struct snd_pcm_substream *substream)
432 {
433         struct snd_pcm_runtime *runtime = substream->runtime;
434
435         io->substream           = substream;
436         io->byte_pos            = 0;
437         io->period_pos          = 0;
438         io->byte_per_period     = runtime->period_size *
439                                   runtime->channels *
440                                   samples_to_bytes(runtime, 1);
441         io->next_period_byte    = io->byte_per_period;
442 }
443
444 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
445 {
446         io->substream           = NULL;
447 }
448
449 static
450 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
451 {
452         struct snd_soc_pcm_runtime *rtd = substream->private_data;
453
454         return  rtd->cpu_dai;
455 }
456
457 static
458 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
459                                         struct snd_pcm_substream *substream)
460 {
461         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
462                 return &rdai->playback;
463         else
464                 return &rdai->capture;
465 }
466
467 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
468                             struct snd_soc_dai *dai)
469 {
470         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
471         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
472         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
473         int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
474         int ret;
475         unsigned long flags;
476
477         spin_lock_irqsave(&priv->lock, flags);
478
479         switch (cmd) {
480         case SNDRV_PCM_TRIGGER_START:
481                 rsnd_dai_stream_init(io, substream);
482
483                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
484                 if (ret < 0)
485                         goto dai_trigger_end;
486
487                 ret = rsnd_dai_call(init, io, priv);
488                 if (ret < 0)
489                         goto dai_trigger_end;
490
491                 ret = rsnd_dai_call(start, io, priv);
492                 if (ret < 0)
493                         goto dai_trigger_end;
494                 break;
495         case SNDRV_PCM_TRIGGER_STOP:
496                 ret = rsnd_dai_call(stop, io, priv);
497
498                 ret |= rsnd_dai_call(quit, io, priv);
499
500                 ret |= rsnd_platform_call(priv, dai, stop, ssi_id);
501
502                 rsnd_dai_stream_quit(io);
503                 break;
504         default:
505                 ret = -EINVAL;
506         }
507
508 dai_trigger_end:
509         spin_unlock_irqrestore(&priv->lock, flags);
510
511         return ret;
512 }
513
514 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
515 {
516         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
517
518         /* set master/slave audio interface */
519         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
520         case SND_SOC_DAIFMT_CBM_CFM:
521                 rdai->clk_master = 0;
522                 break;
523         case SND_SOC_DAIFMT_CBS_CFS:
524                 rdai->clk_master = 1; /* codec is slave, cpu is master */
525                 break;
526         default:
527                 return -EINVAL;
528         }
529
530         /* set format */
531         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
532         case SND_SOC_DAIFMT_I2S:
533                 rdai->sys_delay = 0;
534                 rdai->data_alignment = 0;
535                 rdai->frm_clk_inv = 0;
536                 break;
537         case SND_SOC_DAIFMT_LEFT_J:
538                 rdai->sys_delay = 1;
539                 rdai->data_alignment = 0;
540                 rdai->frm_clk_inv = 1;
541                 break;
542         case SND_SOC_DAIFMT_RIGHT_J:
543                 rdai->sys_delay = 1;
544                 rdai->data_alignment = 1;
545                 rdai->frm_clk_inv = 1;
546                 break;
547         }
548
549         /* set clock inversion */
550         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
551         case SND_SOC_DAIFMT_NB_IF:
552                 rdai->bit_clk_inv =  rdai->bit_clk_inv;
553                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
554                 break;
555         case SND_SOC_DAIFMT_IB_NF:
556                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
557                 rdai->frm_clk_inv =  rdai->frm_clk_inv;
558                 break;
559         case SND_SOC_DAIFMT_IB_IF:
560                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
561                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
562                 break;
563         case SND_SOC_DAIFMT_NB_NF:
564         default:
565                 break;
566         }
567
568         return 0;
569 }
570
571 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
572         .trigger        = rsnd_soc_dai_trigger,
573         .set_fmt        = rsnd_soc_dai_set_fmt,
574 };
575
576 #define rsnd_path_add(priv, io, _type)                          \
577 ({                                                              \
578         struct rsnd_mod *mod;                                   \
579         int ret = 0;                                            \
580         int id = -1;                                            \
581                                                                 \
582         if (rsnd_is_enable_path(io, _type)) {                   \
583                 id = rsnd_info_id(priv, io, _type);             \
584                 if (id >= 0) {                                  \
585                         mod = rsnd_##_type##_mod_get(priv, id); \
586                         ret = rsnd_dai_connect(mod, io, mod->type);\
587                 }                                               \
588         }                                                       \
589         ret;                                                    \
590 })
591
592 void rsnd_path_parse(struct rsnd_priv *priv,
593                      struct rsnd_dai_stream *io)
594 {
595         struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
596         struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
597         struct rsnd_mod *src = rsnd_io_to_mod_src(io);
598         struct rsnd_mod *cmd;
599         struct device *dev = rsnd_priv_to_dev(priv);
600         u32 data;
601
602         /* Gen1 is not supported */
603         if (rsnd_is_gen1(priv))
604                 return;
605
606         if (!mix && !dvc)
607                 return;
608
609         if (mix) {
610                 struct rsnd_dai *rdai;
611                 int i;
612                 u32 path[] = {
613                         [0] = 0,
614                         [1] = 1 << 0,
615                         [2] = 0,
616                         [3] = 0,
617                         [4] = 0,
618                         [5] = 1 << 8
619                 };
620
621                 /*
622                  * it is assuming that integrater is well understanding about
623                  * data path. Here doesn't check impossible connection,
624                  * like src2 + src5
625                  */
626                 data = 0;
627                 for_each_rsnd_dai(rdai, priv, i) {
628                         io = &rdai->playback;
629                         if (mix == rsnd_io_to_mod_mix(io))
630                                 data |= path[rsnd_mod_id(src)];
631
632                         io = &rdai->capture;
633                         if (mix == rsnd_io_to_mod_mix(io))
634                                 data |= path[rsnd_mod_id(src)];
635                 }
636
637                 /*
638                  * We can't use ctu = rsnd_io_ctu() here.
639                  * Since, ID of dvc/mix are 0 or 1 (= same as CMD number)
640                  * but ctu IDs are 0 - 7 (= CTU00 - CTU13)
641                  */
642                 cmd = mix;
643         } else {
644                 u32 path[] = {
645                         [0] = 0x30000,
646                         [1] = 0x30001,
647                         [2] = 0x40000,
648                         [3] = 0x10000,
649                         [4] = 0x20000,
650                         [5] = 0x40100
651                 };
652
653                 data = path[rsnd_mod_id(src)];
654
655                 cmd = dvc;
656         }
657
658         dev_dbg(dev, "ctu/mix path = 0x%08x", data);
659
660         rsnd_mod_write(cmd, CMD_ROUTE_SLCT, data);
661
662         rsnd_mod_write(cmd, CMD_CTRL, 0x10);
663 }
664
665 static int rsnd_path_init(struct rsnd_priv *priv,
666                           struct rsnd_dai *rdai,
667                           struct rsnd_dai_stream *io)
668 {
669         int ret;
670
671         /*
672          * Gen1 is created by SRU/SSI, and this SRU is base module of
673          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
674          *
675          * Easy image is..
676          *      Gen1 SRU = Gen2 SCU + SSIU + etc
677          *
678          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
679          * using fixed path.
680          */
681
682         /* SSI */
683         ret = rsnd_path_add(priv, io, ssi);
684         if (ret < 0)
685                 return ret;
686
687         /* SRC */
688         ret = rsnd_path_add(priv, io, src);
689         if (ret < 0)
690                 return ret;
691
692         /* CTU */
693         ret = rsnd_path_add(priv, io, ctu);
694         if (ret < 0)
695                 return ret;
696
697         /* MIX */
698         ret = rsnd_path_add(priv, io, mix);
699         if (ret < 0)
700                 return ret;
701
702         /* DVC */
703         ret = rsnd_path_add(priv, io, dvc);
704         if (ret < 0)
705                 return ret;
706
707         return ret;
708 }
709
710 static void rsnd_of_parse_dai(struct platform_device *pdev,
711                               const struct rsnd_of_data *of_data,
712                               struct rsnd_priv *priv)
713 {
714         struct device_node *dai_node,   *dai_np;
715         struct device_node *ssi_node,   *ssi_np;
716         struct device_node *src_node,   *src_np;
717         struct device_node *ctu_node,   *ctu_np;
718         struct device_node *mix_node,   *mix_np;
719         struct device_node *dvc_node,   *dvc_np;
720         struct device_node *playback, *capture;
721         struct rsnd_dai_platform_info *dai_info;
722         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
723         struct device *dev = &pdev->dev;
724         int nr, i;
725         int dai_i, ssi_i, src_i, ctu_i, mix_i, dvc_i;
726
727         if (!of_data)
728                 return;
729
730         dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
731         if (!dai_node)
732                 return;
733
734         nr = of_get_child_count(dai_node);
735         if (!nr)
736                 return;
737
738         dai_info = devm_kzalloc(dev,
739                                 sizeof(struct rsnd_dai_platform_info) * nr,
740                                 GFP_KERNEL);
741         if (!dai_info) {
742                 dev_err(dev, "dai info allocation error\n");
743                 return;
744         }
745
746         info->dai_info_nr       = nr;
747         info->dai_info          = dai_info;
748
749         ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
750         src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
751         ctu_node = of_get_child_by_name(dev->of_node, "rcar_sound,ctu");
752         mix_node = of_get_child_by_name(dev->of_node, "rcar_sound,mix");
753         dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
754
755 #define mod_parse(name)                                                 \
756 if (name##_node) {                                                      \
757         struct rsnd_##name##_platform_info *name##_info;                \
758                                                                         \
759         name##_i = 0;                                                   \
760         for_each_child_of_node(name##_node, name##_np) {                \
761                 name##_info = info->name##_info + name##_i;             \
762                                                                         \
763                 if (name##_np == playback)                              \
764                         dai_info->playback.name = name##_info;          \
765                 if (name##_np == capture)                               \
766                         dai_info->capture.name = name##_info;           \
767                                                                         \
768                 name##_i++;                                             \
769         }                                                               \
770 }
771
772         /*
773          * parse all dai
774          */
775         dai_i = 0;
776         for_each_child_of_node(dai_node, dai_np) {
777                 dai_info = info->dai_info + dai_i;
778
779                 for (i = 0;; i++) {
780
781                         playback = of_parse_phandle(dai_np, "playback", i);
782                         capture  = of_parse_phandle(dai_np, "capture", i);
783
784                         if (!playback && !capture)
785                                 break;
786
787                         mod_parse(ssi);
788                         mod_parse(src);
789                         mod_parse(ctu);
790                         mod_parse(mix);
791                         mod_parse(dvc);
792
793                         of_node_put(playback);
794                         of_node_put(capture);
795                 }
796
797                 dai_i++;
798         }
799 }
800
801 static int rsnd_dai_probe(struct platform_device *pdev,
802                           const struct rsnd_of_data *of_data,
803                           struct rsnd_priv *priv)
804 {
805         struct snd_soc_dai_driver *drv;
806         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
807         struct rsnd_dai *rdai;
808         struct rsnd_ssi_platform_info *pmod, *cmod;
809         struct device *dev = rsnd_priv_to_dev(priv);
810         int dai_nr;
811         int i;
812
813         rsnd_of_parse_dai(pdev, of_data, priv);
814
815         dai_nr = info->dai_info_nr;
816         if (!dai_nr) {
817                 dev_err(dev, "no dai\n");
818                 return -EIO;
819         }
820
821         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
822         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
823         if (!drv || !rdai) {
824                 dev_err(dev, "dai allocate failed\n");
825                 return -ENOMEM;
826         }
827
828         priv->rdai_nr   = dai_nr;
829         priv->daidrv    = drv;
830         priv->rdai      = rdai;
831
832         for (i = 0; i < dai_nr; i++) {
833
834                 pmod = info->dai_info[i].playback.ssi;
835                 cmod = info->dai_info[i].capture.ssi;
836
837                 /*
838                  *      init rsnd_dai
839                  */
840                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
841                 rdai[i].priv = priv;
842
843                 /*
844                  *      init snd_soc_dai_driver
845                  */
846                 drv[i].name     = rdai[i].name;
847                 drv[i].ops      = &rsnd_soc_dai_ops;
848                 if (pmod) {
849                         snprintf(rdai[i].playback.name, RSND_DAI_NAME_SIZE,
850                                  "DAI%d Playback", i);
851
852                         drv[i].playback.rates           = RSND_RATES;
853                         drv[i].playback.formats         = RSND_FMTS;
854                         drv[i].playback.channels_min    = 2;
855                         drv[i].playback.channels_max    = 2;
856                         drv[i].playback.stream_name     = rdai[i].playback.name;
857
858                         rdai[i].playback.info = &info->dai_info[i].playback;
859                         rdai[i].playback.rdai = rdai + i;
860                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
861                 }
862                 if (cmod) {
863                         snprintf(rdai[i].capture.name, RSND_DAI_NAME_SIZE,
864                                  "DAI%d Capture", i);
865
866                         drv[i].capture.rates            = RSND_RATES;
867                         drv[i].capture.formats          = RSND_FMTS;
868                         drv[i].capture.channels_min     = 2;
869                         drv[i].capture.channels_max     = 2;
870                         drv[i].capture.stream_name      = rdai[i].capture.name;
871
872                         rdai[i].capture.info = &info->dai_info[i].capture;
873                         rdai[i].capture.rdai = rdai + i;
874                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
875                 }
876
877                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
878                         pmod ? "play"    : " -- ",
879                         cmod ? "capture" : "  --   ");
880         }
881
882         return 0;
883 }
884
885 /*
886  *              pcm ops
887  */
888 static struct snd_pcm_hardware rsnd_pcm_hardware = {
889         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
890                         SNDRV_PCM_INFO_MMAP             |
891                         SNDRV_PCM_INFO_MMAP_VALID,
892         .buffer_bytes_max       = 64 * 1024,
893         .period_bytes_min       = 32,
894         .period_bytes_max       = 8192,
895         .periods_min            = 1,
896         .periods_max            = 32,
897         .fifo_size              = 256,
898 };
899
900 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
901 {
902         struct snd_pcm_runtime *runtime = substream->runtime;
903         int ret = 0;
904
905         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
906
907         ret = snd_pcm_hw_constraint_integer(runtime,
908                                             SNDRV_PCM_HW_PARAM_PERIODS);
909
910         return ret;
911 }
912
913 static int rsnd_hw_params(struct snd_pcm_substream *substream,
914                          struct snd_pcm_hw_params *hw_params)
915 {
916         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
917         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
918         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
919         int ret;
920
921         ret = rsnd_dai_call(hw_params, io, substream, hw_params);
922         if (ret)
923                 return ret;
924
925         return snd_pcm_lib_malloc_pages(substream,
926                                         params_buffer_bytes(hw_params));
927 }
928
929 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
930 {
931         struct snd_pcm_runtime *runtime = substream->runtime;
932         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
933         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
934         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
935
936         return bytes_to_frames(runtime, io->byte_pos);
937 }
938
939 static struct snd_pcm_ops rsnd_pcm_ops = {
940         .open           = rsnd_pcm_open,
941         .ioctl          = snd_pcm_lib_ioctl,
942         .hw_params      = rsnd_hw_params,
943         .hw_free        = snd_pcm_lib_free_pages,
944         .pointer        = rsnd_pointer,
945 };
946
947 /*
948  *              snd_kcontrol
949  */
950 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
951 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
952                            struct snd_ctl_elem_info *uinfo)
953 {
954         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
955
956         if (cfg->texts) {
957                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
958                 uinfo->count = cfg->size;
959                 uinfo->value.enumerated.items = cfg->max;
960                 if (uinfo->value.enumerated.item >= cfg->max)
961                         uinfo->value.enumerated.item = cfg->max - 1;
962                 strlcpy(uinfo->value.enumerated.name,
963                         cfg->texts[uinfo->value.enumerated.item],
964                         sizeof(uinfo->value.enumerated.name));
965         } else {
966                 uinfo->count = cfg->size;
967                 uinfo->value.integer.min = 0;
968                 uinfo->value.integer.max = cfg->max;
969                 uinfo->type = (cfg->max == 1) ?
970                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
971                         SNDRV_CTL_ELEM_TYPE_INTEGER;
972         }
973
974         return 0;
975 }
976
977 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
978                           struct snd_ctl_elem_value *uc)
979 {
980         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
981         int i;
982
983         for (i = 0; i < cfg->size; i++)
984                 if (cfg->texts)
985                         uc->value.enumerated.item[i] = cfg->val[i];
986                 else
987                         uc->value.integer.value[i] = cfg->val[i];
988
989         return 0;
990 }
991
992 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
993                           struct snd_ctl_elem_value *uc)
994 {
995         struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
996         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
997         int i, change = 0;
998
999         for (i = 0; i < cfg->size; i++) {
1000                 if (cfg->texts) {
1001                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1002                         cfg->val[i] = uc->value.enumerated.item[i];
1003                 } else {
1004                         change |= (uc->value.integer.value[i] != cfg->val[i]);
1005                         cfg->val[i] = uc->value.integer.value[i];
1006                 }
1007         }
1008
1009         if (change)
1010                 cfg->update(cfg->io, mod);
1011
1012         return change;
1013 }
1014
1015 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
1016                             struct rsnd_dai_stream *io,
1017                             struct snd_soc_pcm_runtime *rtd,
1018                             const unsigned char *name,
1019                             struct rsnd_kctrl_cfg *cfg,
1020                             void (*update)(struct rsnd_dai_stream *io,
1021                                            struct rsnd_mod *mod))
1022 {
1023         struct snd_soc_card *soc_card = rtd->card;
1024         struct snd_card *card = rtd->card->snd_card;
1025         struct snd_kcontrol *kctrl;
1026         struct snd_kcontrol_new knew = {
1027                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
1028                 .name           = name,
1029                 .info           = rsnd_kctrl_info,
1030                 .index          = rtd - soc_card->rtd,
1031                 .get            = rsnd_kctrl_get,
1032                 .put            = rsnd_kctrl_put,
1033                 .private_value  = (unsigned long)cfg,
1034         };
1035         int ret;
1036
1037         kctrl = snd_ctl_new1(&knew, mod);
1038         if (!kctrl)
1039                 return -ENOMEM;
1040
1041         ret = snd_ctl_add(card, kctrl);
1042         if (ret < 0) {
1043                 snd_ctl_free_one(kctrl);
1044                 return ret;
1045         }
1046
1047         cfg->update = update;
1048         cfg->card = card;
1049         cfg->kctrl = kctrl;
1050         cfg->io = io;
1051
1052         return 0;
1053 }
1054
1055 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1056 {
1057         snd_ctl_remove(cfg->card, cfg->kctrl);
1058 }
1059
1060 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1061                      struct rsnd_dai_stream *io,
1062                      struct snd_soc_pcm_runtime *rtd,
1063                      const unsigned char *name,
1064                      void (*update)(struct rsnd_dai_stream *io,
1065                                     struct rsnd_mod *mod),
1066                      struct rsnd_kctrl_cfg_m *_cfg,
1067                      u32 max)
1068 {
1069         _cfg->cfg.max   = max;
1070         _cfg->cfg.size  = RSND_DVC_CHANNELS;
1071         _cfg->cfg.val   = _cfg->val;
1072         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1073 }
1074
1075 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1076                      struct rsnd_dai_stream *io,
1077                      struct snd_soc_pcm_runtime *rtd,
1078                      const unsigned char *name,
1079                      void (*update)(struct rsnd_dai_stream *io,
1080                                     struct rsnd_mod *mod),
1081                      struct rsnd_kctrl_cfg_s *_cfg,
1082                      u32 max)
1083 {
1084         _cfg->cfg.max   = max;
1085         _cfg->cfg.size  = 1;
1086         _cfg->cfg.val   = &_cfg->val;
1087         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1088 }
1089
1090 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1091                      struct rsnd_dai_stream *io,
1092                      struct snd_soc_pcm_runtime *rtd,
1093                      const unsigned char *name,
1094                      struct rsnd_kctrl_cfg_s *_cfg,
1095                      void (*update)(struct rsnd_dai_stream *io,
1096                                     struct rsnd_mod *mod),
1097                      const char * const *texts,
1098                      u32 max)
1099 {
1100         _cfg->cfg.max   = max;
1101         _cfg->cfg.size  = 1;
1102         _cfg->cfg.val   = &_cfg->val;
1103         _cfg->cfg.texts = texts;
1104         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1105 }
1106
1107 /*
1108  *              snd_soc_platform
1109  */
1110
1111 #define PREALLOC_BUFFER         (32 * 1024)
1112 #define PREALLOC_BUFFER_MAX     (32 * 1024)
1113
1114 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1115 {
1116         struct snd_soc_dai *dai = rtd->cpu_dai;
1117         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1118         int ret;
1119
1120         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1121         if (ret)
1122                 return ret;
1123
1124         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1125         if (ret)
1126                 return ret;
1127
1128         return snd_pcm_lib_preallocate_pages_for_all(
1129                 rtd->pcm,
1130                 SNDRV_DMA_TYPE_DEV,
1131                 rtd->card->snd_card->dev,
1132                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1133 }
1134
1135 static struct snd_soc_platform_driver rsnd_soc_platform = {
1136         .ops            = &rsnd_pcm_ops,
1137         .pcm_new        = rsnd_pcm_new,
1138 };
1139
1140 static const struct snd_soc_component_driver rsnd_soc_component = {
1141         .name           = "rsnd",
1142 };
1143
1144 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1145                                        struct rsnd_dai_stream *io)
1146 {
1147         int ret;
1148
1149         ret = rsnd_dai_call(probe, io, priv);
1150         if (ret == -EAGAIN) {
1151                 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1152                 int i;
1153
1154                 /*
1155                  * Fallback to PIO mode
1156                  */
1157
1158                 /*
1159                  * call "remove" for SSI/SRC/DVC
1160                  * SSI will be switch to PIO mode if it was DMA mode
1161                  * see
1162                  *      rsnd_dma_init()
1163                  *      rsnd_ssi_fallback()
1164                  */
1165                 rsnd_dai_call(remove, io, priv);
1166
1167                 /*
1168                  * remove all mod from io
1169                  * and, re connect ssi
1170                  */
1171                 for (i = 0; i < RSND_MOD_MAX; i++)
1172                         rsnd_dai_disconnect((io)->mod[i], io, i);
1173                 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1174
1175                 /*
1176                  * fallback
1177                  */
1178                 rsnd_dai_call(fallback, io, priv);
1179
1180                 /*
1181                  * retry to "probe".
1182                  * DAI has SSI which is PIO mode only now.
1183                  */
1184                 ret = rsnd_dai_call(probe, io, priv);
1185         }
1186
1187         return ret;
1188 }
1189
1190 /*
1191  *      rsnd probe
1192  */
1193 static int rsnd_probe(struct platform_device *pdev)
1194 {
1195         struct rcar_snd_info *info;
1196         struct rsnd_priv *priv;
1197         struct device *dev = &pdev->dev;
1198         struct rsnd_dai *rdai;
1199         const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1200         const struct rsnd_of_data *of_data;
1201         int (*probe_func[])(struct platform_device *pdev,
1202                             const struct rsnd_of_data *of_data,
1203                             struct rsnd_priv *priv) = {
1204                 rsnd_gen_probe,
1205                 rsnd_dma_probe,
1206                 rsnd_ssi_probe,
1207                 rsnd_src_probe,
1208                 rsnd_ctu_probe,
1209                 rsnd_mix_probe,
1210                 rsnd_dvc_probe,
1211                 rsnd_adg_probe,
1212                 rsnd_dai_probe,
1213         };
1214         int ret, i;
1215
1216         info = devm_kzalloc(&pdev->dev, sizeof(struct rcar_snd_info),
1217                             GFP_KERNEL);
1218         if (!info)
1219                 return -ENOMEM;
1220         of_data = of_id->data;
1221
1222         /*
1223          *      init priv data
1224          */
1225         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1226         if (!priv) {
1227                 dev_err(dev, "priv allocate failed\n");
1228                 return -ENODEV;
1229         }
1230
1231         priv->pdev      = pdev;
1232         priv->info      = info;
1233         spin_lock_init(&priv->lock);
1234
1235         /*
1236          *      init each module
1237          */
1238         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1239                 ret = probe_func[i](pdev, of_data, priv);
1240                 if (ret)
1241                         return ret;
1242         }
1243
1244         for_each_rsnd_dai(rdai, priv, i) {
1245                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1246                 if (ret)
1247                         goto exit_snd_probe;
1248
1249                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1250                 if (ret)
1251                         goto exit_snd_probe;
1252         }
1253
1254         dev_set_drvdata(dev, priv);
1255
1256         /*
1257          *      asoc register
1258          */
1259         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1260         if (ret < 0) {
1261                 dev_err(dev, "cannot snd soc register\n");
1262                 return ret;
1263         }
1264
1265         ret = snd_soc_register_component(dev, &rsnd_soc_component,
1266                                          priv->daidrv, rsnd_rdai_nr(priv));
1267         if (ret < 0) {
1268                 dev_err(dev, "cannot snd dai register\n");
1269                 goto exit_snd_soc;
1270         }
1271
1272         pm_runtime_enable(dev);
1273
1274         dev_info(dev, "probed\n");
1275         return ret;
1276
1277 exit_snd_soc:
1278         snd_soc_unregister_platform(dev);
1279 exit_snd_probe:
1280         for_each_rsnd_dai(rdai, priv, i) {
1281                 rsnd_dai_call(remove, &rdai->playback, priv);
1282                 rsnd_dai_call(remove, &rdai->capture, priv);
1283         }
1284
1285         return ret;
1286 }
1287
1288 static int rsnd_remove(struct platform_device *pdev)
1289 {
1290         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1291         struct rsnd_dai *rdai;
1292         void (*remove_func[])(struct platform_device *pdev,
1293                               struct rsnd_priv *priv) = {
1294                 rsnd_ssi_remove,
1295                 rsnd_src_remove,
1296                 rsnd_ctu_remove,
1297                 rsnd_mix_remove,
1298                 rsnd_dvc_remove,
1299         };
1300         int ret = 0, i;
1301
1302         pm_runtime_disable(&pdev->dev);
1303
1304         for_each_rsnd_dai(rdai, priv, i) {
1305                 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1306                 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1307         }
1308
1309         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1310                 remove_func[i](pdev, priv);
1311
1312         snd_soc_unregister_component(&pdev->dev);
1313         snd_soc_unregister_platform(&pdev->dev);
1314
1315         return ret;
1316 }
1317
1318 static struct platform_driver rsnd_driver = {
1319         .driver = {
1320                 .name   = "rcar_sound",
1321                 .of_match_table = rsnd_of_match,
1322         },
1323         .probe          = rsnd_probe,
1324         .remove         = rsnd_remove,
1325 };
1326 module_platform_driver(rsnd_driver);
1327
1328 MODULE_LICENSE("GPL");
1329 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1330 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1331 MODULE_ALIAS("platform:rcar-pcm-audio");