]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/sh/rcar/core.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux-tile
[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 of_device_id rsnd_of_match[] = {
103         { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
104         { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
105         { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN2 }, /* gen2 compatible */
106         {},
107 };
108 MODULE_DEVICE_TABLE(of, rsnd_of_match);
109
110 /*
111  *      rsnd_mod functions
112  */
113 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
114 {
115         if (mod->type != type) {
116                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
117                 struct device *dev = rsnd_priv_to_dev(priv);
118
119                 dev_warn(dev, "%s[%d] is not your expected module\n",
120                          rsnd_mod_name(mod), rsnd_mod_id(mod));
121         }
122 }
123
124 char *rsnd_mod_name(struct rsnd_mod *mod)
125 {
126         if (!mod || !mod->ops)
127                 return "unknown";
128
129         return mod->ops->name;
130 }
131
132 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
133                                   struct rsnd_mod *mod)
134 {
135         if (!mod || !mod->ops || !mod->ops->dma_req)
136                 return NULL;
137
138         return mod->ops->dma_req(io, mod);
139 }
140
141 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
142                          struct rsnd_mod *mod,
143                          enum rsnd_mod_type type)
144 {
145         return &mod->status;
146 }
147
148 int rsnd_mod_init(struct rsnd_priv *priv,
149                   struct rsnd_mod *mod,
150                   struct rsnd_mod_ops *ops,
151                   struct clk *clk,
152                   u32* (*get_status)(struct rsnd_dai_stream *io,
153                                      struct rsnd_mod *mod,
154                                      enum rsnd_mod_type type),
155                   enum rsnd_mod_type type,
156                   int id)
157 {
158         int ret = clk_prepare(clk);
159
160         if (ret)
161                 return ret;
162
163         mod->id         = id;
164         mod->ops        = ops;
165         mod->type       = type;
166         mod->clk        = clk;
167         mod->priv       = priv;
168         mod->get_status = get_status;
169
170         return ret;
171 }
172
173 void rsnd_mod_quit(struct rsnd_mod *mod)
174 {
175         if (mod->clk)
176                 clk_unprepare(mod->clk);
177         mod->clk = NULL;
178 }
179
180 void rsnd_mod_interrupt(struct rsnd_mod *mod,
181                         void (*callback)(struct rsnd_mod *mod,
182                                          struct rsnd_dai_stream *io))
183 {
184         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
185         struct rsnd_dai_stream *io;
186         struct rsnd_dai *rdai;
187         int i;
188
189         for_each_rsnd_dai(rdai, priv, i) {
190                 io = &rdai->playback;
191                 if (mod == io->mod[mod->type])
192                         callback(mod, io);
193
194                 io = &rdai->capture;
195                 if (mod == io->mod[mod->type])
196                         callback(mod, io);
197         }
198 }
199
200 int rsnd_io_is_working(struct rsnd_dai_stream *io)
201 {
202         /* see rsnd_dai_stream_init/quit() */
203         return !!io->substream;
204 }
205
206 void rsnd_set_slot(struct rsnd_dai *rdai,
207                    int slots, int num)
208 {
209         rdai->slots     = slots;
210         rdai->slots_num = num;
211 }
212
213 int rsnd_get_slot(struct rsnd_dai_stream *io)
214 {
215         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
216
217         return rdai->slots;
218 }
219
220 int rsnd_get_slot_num(struct rsnd_dai_stream *io)
221 {
222         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
223
224         return rdai->slots_num;
225 }
226
227 int rsnd_runtime_channel_original(struct rsnd_dai_stream *io)
228 {
229         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
230
231         return runtime->channels;
232 }
233
234 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io)
235 {
236         int chan = rsnd_runtime_channel_original(io);
237         struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
238
239         if (ctu_mod) {
240                 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
241
242                 if (converted_chan)
243                         return converted_chan;
244         }
245
246         return chan;
247 }
248
249 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io)
250 {
251         int chan = rsnd_io_is_play(io) ?
252                 rsnd_runtime_channel_after_ctu(io) :
253                 rsnd_runtime_channel_original(io);
254
255         /* Use Multi SSI */
256         if (rsnd_runtime_is_ssi_multi(io))
257                 chan /= rsnd_get_slot_num(io);
258
259         /* TDM Extend Mode needs 8ch */
260         if (chan == 6)
261                 chan = 8;
262
263         return chan;
264 }
265
266 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
267 {
268         int slots = rsnd_get_slot_num(io);
269         int chan = rsnd_io_is_play(io) ?
270                 rsnd_runtime_channel_after_ctu(io) :
271                 rsnd_runtime_channel_original(io);
272
273         return (chan >= 6) && (slots > 1);
274 }
275
276 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
277 {
278         return rsnd_runtime_channel_for_ssi(io) >= 6;
279 }
280
281 /*
282  *      ADINR function
283  */
284 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
285 {
286         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
287         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
288         struct device *dev = rsnd_priv_to_dev(priv);
289
290         switch (runtime->sample_bits) {
291         case 16:
292                 return 8 << 16;
293         case 32:
294                 return 0 << 16;
295         }
296
297         dev_warn(dev, "not supported sample bits\n");
298
299         return 0;
300 }
301
302 /*
303  *      DALIGN function
304  */
305 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
306 {
307         struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
308         struct rsnd_mod *target;
309         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
310         u32 val = 0x76543210;
311         u32 mask = ~0;
312
313         if (rsnd_io_is_play(io)) {
314                 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
315
316                 target = src ? src : ssi;
317         } else {
318                 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
319
320                 target = cmd ? cmd : ssi;
321         }
322
323         mask <<= runtime->channels * 4;
324         val = val & mask;
325
326         switch (runtime->sample_bits) {
327         case 16:
328                 val |= 0x67452301 & ~mask;
329                 break;
330         case 32:
331                 val |= 0x76543210 & ~mask;
332                 break;
333         }
334
335         /*
336          * exchange channeles on SRC if possible,
337          * otherwise, R/L volume settings on DVC
338          * changes inverted channels
339          */
340         if (mod == target)
341                 return val;
342         else
343                 return 0x76543210;
344 }
345
346 /*
347  *      rsnd_dai functions
348  */
349 #define rsnd_mod_call(idx, io, func, param...)                  \
350 ({                                                              \
351         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
352         struct rsnd_mod *mod = (io)->mod[idx];                  \
353         struct device *dev = rsnd_priv_to_dev(priv);            \
354         u32 *status = mod->get_status(io, mod, idx);                    \
355         u32 mask = 0xF << __rsnd_mod_shift_##func;                      \
356         u8 val  = (*status >> __rsnd_mod_shift_##func) & 0xF;           \
357         u8 add  = ((val + __rsnd_mod_add_##func) & 0xF);                \
358         int ret = 0;                                                    \
359         int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
360         if (add == 0xF)                                                 \
361                 call = 0;                                               \
362         else                                                            \
363                 *status = (*status & ~mask) +                           \
364                         (add << __rsnd_mod_shift_##func);               \
365         dev_dbg(dev, "%s[%d]\t0x%08x %s\n",                             \
366                 rsnd_mod_name(mod), rsnd_mod_id(mod),                   \
367                 *status, call ? #func : "");                            \
368         if (call)                                                       \
369                 ret = (mod)->ops->func(mod, io, param);                 \
370         if (ret)                                                        \
371                 dev_dbg(dev, "%s[%d] : rsnd_mod_call error %d\n",       \
372                         rsnd_mod_name(mod), rsnd_mod_id(mod), ret);     \
373         ret;                                                            \
374 })
375
376 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
377         {
378                 /* CAPTURE */
379                 RSND_MOD_AUDMAPP,
380                 RSND_MOD_AUDMA,
381                 RSND_MOD_DVC,
382                 RSND_MOD_MIX,
383                 RSND_MOD_CTU,
384                 RSND_MOD_CMD,
385                 RSND_MOD_SRC,
386                 RSND_MOD_SSIU,
387                 RSND_MOD_SSIM3,
388                 RSND_MOD_SSIM2,
389                 RSND_MOD_SSIM1,
390                 RSND_MOD_SSIP,
391                 RSND_MOD_SSI,
392         }, {
393                 /* PLAYBACK */
394                 RSND_MOD_AUDMAPP,
395                 RSND_MOD_AUDMA,
396                 RSND_MOD_SSIM3,
397                 RSND_MOD_SSIM2,
398                 RSND_MOD_SSIM1,
399                 RSND_MOD_SSIP,
400                 RSND_MOD_SSI,
401                 RSND_MOD_SSIU,
402                 RSND_MOD_DVC,
403                 RSND_MOD_MIX,
404                 RSND_MOD_CTU,
405                 RSND_MOD_CMD,
406                 RSND_MOD_SRC,
407         },
408 };
409
410 #define rsnd_dai_call(fn, io, param...)                         \
411 ({                                                              \
412         struct rsnd_mod *mod;                                   \
413         int type, is_play = rsnd_io_is_play(io);                \
414         int ret = 0, i;                                         \
415         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
416                 type = rsnd_mod_sequence[is_play][i];           \
417                 mod = (io)->mod[type];                          \
418                 if (!mod)                                       \
419                         continue;                               \
420                 ret |= rsnd_mod_call(type, io, fn, param);      \
421         }                                                       \
422         ret;                                                    \
423 })
424
425 int rsnd_dai_connect(struct rsnd_mod *mod,
426                      struct rsnd_dai_stream *io,
427                      enum rsnd_mod_type type)
428 {
429         struct rsnd_priv *priv;
430         struct device *dev;
431
432         if (!mod)
433                 return -EIO;
434
435         if (io->mod[type] == mod)
436                 return 0;
437
438         if (io->mod[type])
439                 return -EINVAL;
440
441         priv = rsnd_mod_to_priv(mod);
442         dev = rsnd_priv_to_dev(priv);
443
444         io->mod[type] = mod;
445
446         dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
447                 rsnd_mod_name(mod), rsnd_mod_id(mod),
448                 rsnd_io_is_play(io) ? "Playback" : "Capture");
449
450         return 0;
451 }
452
453 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
454                                 struct rsnd_dai_stream *io,
455                                 enum rsnd_mod_type type)
456 {
457         io->mod[type] = NULL;
458 }
459
460 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
461 {
462         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
463                 return NULL;
464
465         return priv->rdai + id;
466 }
467
468 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
469 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
470 {
471         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
472
473         return rsnd_rdai_get(priv, dai->id);
474 }
475
476 /*
477  *      rsnd_soc_dai functions
478  */
479 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
480 {
481         struct snd_pcm_substream *substream = io->substream;
482         struct snd_pcm_runtime *runtime = substream->runtime;
483         int pos = io->byte_pos + additional;
484
485         pos %= (runtime->periods * io->byte_per_period);
486
487         return pos;
488 }
489
490 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
491 {
492         io->byte_pos += byte;
493
494         if (io->byte_pos >= io->next_period_byte) {
495                 struct snd_pcm_substream *substream = io->substream;
496                 struct snd_pcm_runtime *runtime = substream->runtime;
497
498                 io->period_pos++;
499                 io->next_period_byte += io->byte_per_period;
500
501                 if (io->period_pos >= runtime->periods) {
502                         io->byte_pos = 0;
503                         io->period_pos = 0;
504                         io->next_period_byte = io->byte_per_period;
505                 }
506
507                 return true;
508         }
509
510         return false;
511 }
512
513 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
514 {
515         struct snd_pcm_substream *substream = io->substream;
516
517         /*
518          * this function should be called...
519          *
520          * - if rsnd_dai_pointer_update() returns true
521          * - without spin lock
522          */
523
524         snd_pcm_period_elapsed(substream);
525 }
526
527 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
528                                 struct snd_pcm_substream *substream)
529 {
530         struct snd_pcm_runtime *runtime = substream->runtime;
531
532         io->substream           = substream;
533         io->byte_pos            = 0;
534         io->period_pos          = 0;
535         io->byte_per_period     = runtime->period_size *
536                                   runtime->channels *
537                                   samples_to_bytes(runtime, 1);
538         io->next_period_byte    = io->byte_per_period;
539 }
540
541 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
542 {
543         io->substream           = NULL;
544 }
545
546 static
547 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
548 {
549         struct snd_soc_pcm_runtime *rtd = substream->private_data;
550
551         return  rtd->cpu_dai;
552 }
553
554 static
555 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
556                                         struct snd_pcm_substream *substream)
557 {
558         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
559                 return &rdai->playback;
560         else
561                 return &rdai->capture;
562 }
563
564 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
565                             struct snd_soc_dai *dai)
566 {
567         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
568         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
569         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
570         int ret;
571         unsigned long flags;
572
573         spin_lock_irqsave(&priv->lock, flags);
574
575         switch (cmd) {
576         case SNDRV_PCM_TRIGGER_START:
577                 rsnd_dai_stream_init(io, substream);
578
579                 ret = rsnd_dai_call(init, io, priv);
580                 if (ret < 0)
581                         goto dai_trigger_end;
582
583                 ret = rsnd_dai_call(start, io, priv);
584                 if (ret < 0)
585                         goto dai_trigger_end;
586
587                 ret = rsnd_dai_call(irq, io, priv, 1);
588                 if (ret < 0)
589                         goto dai_trigger_end;
590
591                 break;
592         case SNDRV_PCM_TRIGGER_STOP:
593                 ret = rsnd_dai_call(irq, io, priv, 0);
594
595                 ret |= rsnd_dai_call(stop, io, priv);
596
597                 ret |= rsnd_dai_call(quit, io, priv);
598
599                 rsnd_dai_stream_quit(io);
600                 break;
601         default:
602                 ret = -EINVAL;
603         }
604
605 dai_trigger_end:
606         spin_unlock_irqrestore(&priv->lock, flags);
607
608         return ret;
609 }
610
611 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
612 {
613         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
614
615         /* set master/slave audio interface */
616         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
617         case SND_SOC_DAIFMT_CBM_CFM:
618                 rdai->clk_master = 0;
619                 break;
620         case SND_SOC_DAIFMT_CBS_CFS:
621                 rdai->clk_master = 1; /* codec is slave, cpu is master */
622                 break;
623         default:
624                 return -EINVAL;
625         }
626
627         /* set format */
628         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
629         case SND_SOC_DAIFMT_I2S:
630                 rdai->sys_delay = 0;
631                 rdai->data_alignment = 0;
632                 rdai->frm_clk_inv = 0;
633                 break;
634         case SND_SOC_DAIFMT_LEFT_J:
635                 rdai->sys_delay = 1;
636                 rdai->data_alignment = 0;
637                 rdai->frm_clk_inv = 1;
638                 break;
639         case SND_SOC_DAIFMT_RIGHT_J:
640                 rdai->sys_delay = 1;
641                 rdai->data_alignment = 1;
642                 rdai->frm_clk_inv = 1;
643                 break;
644         }
645
646         /* set clock inversion */
647         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
648         case SND_SOC_DAIFMT_NB_IF:
649                 rdai->bit_clk_inv =  rdai->bit_clk_inv;
650                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
651                 break;
652         case SND_SOC_DAIFMT_IB_NF:
653                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
654                 rdai->frm_clk_inv =  rdai->frm_clk_inv;
655                 break;
656         case SND_SOC_DAIFMT_IB_IF:
657                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
658                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
659                 break;
660         case SND_SOC_DAIFMT_NB_NF:
661         default:
662                 break;
663         }
664
665         return 0;
666 }
667
668 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
669                                      u32 tx_mask, u32 rx_mask,
670                                      int slots, int slot_width)
671 {
672         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
673         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
674         struct device *dev = rsnd_priv_to_dev(priv);
675
676         switch (slots) {
677         case 6:
678                 /* TDM Extend Mode */
679                 rsnd_set_slot(rdai, slots, 1);
680                 break;
681         default:
682                 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
683                 return -EINVAL;
684         }
685
686         return 0;
687 }
688
689 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
690         .trigger        = rsnd_soc_dai_trigger,
691         .set_fmt        = rsnd_soc_dai_set_fmt,
692         .set_tdm_slot   = rsnd_soc_set_dai_tdm_slot,
693 };
694
695 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
696                 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
697                 struct device_node *node,
698                 struct device_node *playback,
699                 struct device_node *capture)
700 {
701         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
702         struct device_node *np;
703         struct rsnd_mod *mod;
704         int i;
705
706         if (!node)
707                 return;
708
709         i = 0;
710         for_each_child_of_node(node, np) {
711                 mod = mod_get(priv, i);
712                 if (np == playback)
713                         rsnd_dai_connect(mod, &rdai->playback, mod->type);
714                 if (np == capture)
715                         rsnd_dai_connect(mod, &rdai->capture, mod->type);
716                 i++;
717         }
718
719         of_node_put(node);
720 }
721
722 static int rsnd_dai_probe(struct rsnd_priv *priv)
723 {
724         struct device_node *dai_node;
725         struct device_node *dai_np;
726         struct device_node *playback, *capture;
727         struct rsnd_dai_stream *io_playback;
728         struct rsnd_dai_stream *io_capture;
729         struct snd_soc_dai_driver *rdrv, *drv;
730         struct rsnd_dai *rdai;
731         struct device *dev = rsnd_priv_to_dev(priv);
732         int nr, dai_i, io_i;
733         int ret;
734
735         dai_node = rsnd_dai_of_node(priv);
736         nr = of_get_child_count(dai_node);
737         if (!nr) {
738                 ret = -EINVAL;
739                 goto rsnd_dai_probe_done;
740         }
741
742         rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
743         rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
744         if (!rdrv || !rdai) {
745                 ret = -ENOMEM;
746                 goto rsnd_dai_probe_done;
747         }
748
749         priv->rdai_nr   = nr;
750         priv->daidrv    = rdrv;
751         priv->rdai      = rdai;
752
753         /*
754          * parse all dai
755          */
756         dai_i = 0;
757         for_each_child_of_node(dai_node, dai_np) {
758                 rdai            = rsnd_rdai_get(priv, dai_i);
759                 drv             = rdrv + dai_i;
760                 io_playback     = &rdai->playback;
761                 io_capture      = &rdai->capture;
762
763                 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
764
765                 rdai->priv      = priv;
766                 drv->name       = rdai->name;
767                 drv->ops        = &rsnd_soc_dai_ops;
768
769                 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
770                          "DAI%d Playback", dai_i);
771                 drv->playback.rates             = RSND_RATES;
772                 drv->playback.formats           = RSND_FMTS;
773                 drv->playback.channels_min      = 2;
774                 drv->playback.channels_max      = 6;
775                 drv->playback.stream_name       = rdai->playback.name;
776
777                 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
778                          "DAI%d Capture", dai_i);
779                 drv->capture.rates              = RSND_RATES;
780                 drv->capture.formats            = RSND_FMTS;
781                 drv->capture.channels_min       = 2;
782                 drv->capture.channels_max       = 6;
783                 drv->capture.stream_name        = rdai->capture.name;
784
785                 rdai->playback.rdai             = rdai;
786                 rdai->capture.rdai              = rdai;
787                 rsnd_set_slot(rdai, 2, 1); /* default */
788
789                 for (io_i = 0;; io_i++) {
790                         playback = of_parse_phandle(dai_np, "playback", io_i);
791                         capture  = of_parse_phandle(dai_np, "capture", io_i);
792
793                         if (!playback && !capture)
794                                 break;
795
796                         rsnd_parse_connect_ssi(rdai, playback, capture);
797                         rsnd_parse_connect_src(rdai, playback, capture);
798                         rsnd_parse_connect_ctu(rdai, playback, capture);
799                         rsnd_parse_connect_mix(rdai, playback, capture);
800                         rsnd_parse_connect_dvc(rdai, playback, capture);
801
802                         of_node_put(playback);
803                         of_node_put(capture);
804                 }
805
806                 dai_i++;
807
808                 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
809                         rsnd_io_to_mod_ssi(io_playback) ? "play"    : " -- ",
810                         rsnd_io_to_mod_ssi(io_capture) ? "capture" : "  --   ");
811         }
812
813         ret = 0;
814
815 rsnd_dai_probe_done:
816         of_node_put(dai_node);
817
818         return ret;
819 }
820
821 /*
822  *              pcm ops
823  */
824 static struct snd_pcm_hardware rsnd_pcm_hardware = {
825         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
826                         SNDRV_PCM_INFO_MMAP             |
827                         SNDRV_PCM_INFO_MMAP_VALID,
828         .buffer_bytes_max       = 64 * 1024,
829         .period_bytes_min       = 32,
830         .period_bytes_max       = 8192,
831         .periods_min            = 1,
832         .periods_max            = 32,
833         .fifo_size              = 256,
834 };
835
836 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
837 {
838         struct snd_pcm_runtime *runtime = substream->runtime;
839         int ret = 0;
840
841         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
842
843         ret = snd_pcm_hw_constraint_integer(runtime,
844                                             SNDRV_PCM_HW_PARAM_PERIODS);
845
846         return ret;
847 }
848
849 static int rsnd_hw_params(struct snd_pcm_substream *substream,
850                          struct snd_pcm_hw_params *hw_params)
851 {
852         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
853         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
854         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
855         int ret;
856
857         ret = rsnd_dai_call(hw_params, io, substream, hw_params);
858         if (ret)
859                 return ret;
860
861         return snd_pcm_lib_malloc_pages(substream,
862                                         params_buffer_bytes(hw_params));
863 }
864
865 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
866 {
867         struct snd_pcm_runtime *runtime = substream->runtime;
868         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
869         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
870         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
871
872         return bytes_to_frames(runtime, io->byte_pos);
873 }
874
875 static struct snd_pcm_ops rsnd_pcm_ops = {
876         .open           = rsnd_pcm_open,
877         .ioctl          = snd_pcm_lib_ioctl,
878         .hw_params      = rsnd_hw_params,
879         .hw_free        = snd_pcm_lib_free_pages,
880         .pointer        = rsnd_pointer,
881 };
882
883 /*
884  *              snd_kcontrol
885  */
886 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
887 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
888                            struct snd_ctl_elem_info *uinfo)
889 {
890         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
891
892         if (cfg->texts) {
893                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
894                 uinfo->count = cfg->size;
895                 uinfo->value.enumerated.items = cfg->max;
896                 if (uinfo->value.enumerated.item >= cfg->max)
897                         uinfo->value.enumerated.item = cfg->max - 1;
898                 strlcpy(uinfo->value.enumerated.name,
899                         cfg->texts[uinfo->value.enumerated.item],
900                         sizeof(uinfo->value.enumerated.name));
901         } else {
902                 uinfo->count = cfg->size;
903                 uinfo->value.integer.min = 0;
904                 uinfo->value.integer.max = cfg->max;
905                 uinfo->type = (cfg->max == 1) ?
906                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
907                         SNDRV_CTL_ELEM_TYPE_INTEGER;
908         }
909
910         return 0;
911 }
912
913 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
914                           struct snd_ctl_elem_value *uc)
915 {
916         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
917         int i;
918
919         for (i = 0; i < cfg->size; i++)
920                 if (cfg->texts)
921                         uc->value.enumerated.item[i] = cfg->val[i];
922                 else
923                         uc->value.integer.value[i] = cfg->val[i];
924
925         return 0;
926 }
927
928 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
929                           struct snd_ctl_elem_value *uc)
930 {
931         struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
932         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
933         int i, change = 0;
934
935         for (i = 0; i < cfg->size; i++) {
936                 if (cfg->texts) {
937                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
938                         cfg->val[i] = uc->value.enumerated.item[i];
939                 } else {
940                         change |= (uc->value.integer.value[i] != cfg->val[i]);
941                         cfg->val[i] = uc->value.integer.value[i];
942                 }
943         }
944
945         if (change && cfg->update)
946                 cfg->update(cfg->io, mod);
947
948         return change;
949 }
950
951 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
952                             struct rsnd_dai_stream *io,
953                             struct snd_soc_pcm_runtime *rtd,
954                             const unsigned char *name,
955                             struct rsnd_kctrl_cfg *cfg,
956                             void (*update)(struct rsnd_dai_stream *io,
957                                            struct rsnd_mod *mod))
958 {
959         struct snd_card *card = rtd->card->snd_card;
960         struct snd_kcontrol *kctrl;
961         struct snd_kcontrol_new knew = {
962                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
963                 .name           = name,
964                 .info           = rsnd_kctrl_info,
965                 .index          = rtd->num,
966                 .get            = rsnd_kctrl_get,
967                 .put            = rsnd_kctrl_put,
968                 .private_value  = (unsigned long)cfg,
969         };
970         int ret;
971
972         kctrl = snd_ctl_new1(&knew, mod);
973         if (!kctrl)
974                 return -ENOMEM;
975
976         ret = snd_ctl_add(card, kctrl);
977         if (ret < 0) {
978                 snd_ctl_free_one(kctrl);
979                 return ret;
980         }
981
982         cfg->update = update;
983         cfg->card = card;
984         cfg->kctrl = kctrl;
985         cfg->io = io;
986
987         return 0;
988 }
989
990 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
991 {
992         snd_ctl_remove(cfg->card, cfg->kctrl);
993 }
994
995 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
996                      struct rsnd_dai_stream *io,
997                      struct snd_soc_pcm_runtime *rtd,
998                      const unsigned char *name,
999                      void (*update)(struct rsnd_dai_stream *io,
1000                                     struct rsnd_mod *mod),
1001                      struct rsnd_kctrl_cfg_m *_cfg,
1002                      int ch_size,
1003                      u32 max)
1004 {
1005         if (ch_size > RSND_MAX_CHANNELS)
1006                 return -EINVAL;
1007
1008         _cfg->cfg.max   = max;
1009         _cfg->cfg.size  = ch_size;
1010         _cfg->cfg.val   = _cfg->val;
1011         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1012 }
1013
1014 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1015                      struct rsnd_dai_stream *io,
1016                      struct snd_soc_pcm_runtime *rtd,
1017                      const unsigned char *name,
1018                      void (*update)(struct rsnd_dai_stream *io,
1019                                     struct rsnd_mod *mod),
1020                      struct rsnd_kctrl_cfg_s *_cfg,
1021                      u32 max)
1022 {
1023         _cfg->cfg.max   = max;
1024         _cfg->cfg.size  = 1;
1025         _cfg->cfg.val   = &_cfg->val;
1026         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1027 }
1028
1029 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1030                      struct rsnd_dai_stream *io,
1031                      struct snd_soc_pcm_runtime *rtd,
1032                      const unsigned char *name,
1033                      struct rsnd_kctrl_cfg_s *_cfg,
1034                      void (*update)(struct rsnd_dai_stream *io,
1035                                     struct rsnd_mod *mod),
1036                      const char * const *texts,
1037                      u32 max)
1038 {
1039         _cfg->cfg.max   = max;
1040         _cfg->cfg.size  = 1;
1041         _cfg->cfg.val   = &_cfg->val;
1042         _cfg->cfg.texts = texts;
1043         return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1044 }
1045
1046 /*
1047  *              snd_soc_platform
1048  */
1049
1050 #define PREALLOC_BUFFER         (32 * 1024)
1051 #define PREALLOC_BUFFER_MAX     (32 * 1024)
1052
1053 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1054 {
1055         struct snd_soc_dai *dai = rtd->cpu_dai;
1056         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1057         int ret;
1058
1059         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1060         if (ret)
1061                 return ret;
1062
1063         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1064         if (ret)
1065                 return ret;
1066
1067         return snd_pcm_lib_preallocate_pages_for_all(
1068                 rtd->pcm,
1069                 SNDRV_DMA_TYPE_DEV,
1070                 rtd->card->snd_card->dev,
1071                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1072 }
1073
1074 static struct snd_soc_platform_driver rsnd_soc_platform = {
1075         .ops            = &rsnd_pcm_ops,
1076         .pcm_new        = rsnd_pcm_new,
1077 };
1078
1079 static const struct snd_soc_component_driver rsnd_soc_component = {
1080         .name           = "rsnd",
1081 };
1082
1083 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1084                                        struct rsnd_dai_stream *io)
1085 {
1086         int ret;
1087
1088         ret = rsnd_dai_call(probe, io, priv);
1089         if (ret == -EAGAIN) {
1090                 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1091                 int i;
1092
1093                 /*
1094                  * Fallback to PIO mode
1095                  */
1096
1097                 /*
1098                  * call "remove" for SSI/SRC/DVC
1099                  * SSI will be switch to PIO mode if it was DMA mode
1100                  * see
1101                  *      rsnd_dma_init()
1102                  *      rsnd_ssi_fallback()
1103                  */
1104                 rsnd_dai_call(remove, io, priv);
1105
1106                 /*
1107                  * remove all mod from io
1108                  * and, re connect ssi
1109                  */
1110                 for (i = 0; i < RSND_MOD_MAX; i++)
1111                         rsnd_dai_disconnect((io)->mod[i], io, i);
1112                 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1113
1114                 /*
1115                  * fallback
1116                  */
1117                 rsnd_dai_call(fallback, io, priv);
1118
1119                 /*
1120                  * retry to "probe".
1121                  * DAI has SSI which is PIO mode only now.
1122                  */
1123                 ret = rsnd_dai_call(probe, io, priv);
1124         }
1125
1126         return ret;
1127 }
1128
1129 /*
1130  *      rsnd probe
1131  */
1132 static int rsnd_probe(struct platform_device *pdev)
1133 {
1134         struct rsnd_priv *priv;
1135         struct device *dev = &pdev->dev;
1136         struct rsnd_dai *rdai;
1137         int (*probe_func[])(struct rsnd_priv *priv) = {
1138                 rsnd_gen_probe,
1139                 rsnd_dma_probe,
1140                 rsnd_ssi_probe,
1141                 rsnd_ssiu_probe,
1142                 rsnd_src_probe,
1143                 rsnd_ctu_probe,
1144                 rsnd_mix_probe,
1145                 rsnd_dvc_probe,
1146                 rsnd_cmd_probe,
1147                 rsnd_adg_probe,
1148                 rsnd_dai_probe,
1149         };
1150         int ret, i;
1151
1152         /*
1153          *      init priv data
1154          */
1155         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1156         if (!priv) {
1157                 dev_err(dev, "priv allocate failed\n");
1158                 return -ENODEV;
1159         }
1160
1161         priv->pdev      = pdev;
1162         priv->flags     = (unsigned long)of_device_get_match_data(dev);
1163         spin_lock_init(&priv->lock);
1164
1165         /*
1166          *      init each module
1167          */
1168         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1169                 ret = probe_func[i](priv);
1170                 if (ret)
1171                         return ret;
1172         }
1173
1174         for_each_rsnd_dai(rdai, priv, i) {
1175                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1176                 if (ret)
1177                         goto exit_snd_probe;
1178
1179                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1180                 if (ret)
1181                         goto exit_snd_probe;
1182         }
1183
1184         dev_set_drvdata(dev, priv);
1185
1186         /*
1187          *      asoc register
1188          */
1189         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1190         if (ret < 0) {
1191                 dev_err(dev, "cannot snd soc register\n");
1192                 return ret;
1193         }
1194
1195         ret = snd_soc_register_component(dev, &rsnd_soc_component,
1196                                          priv->daidrv, rsnd_rdai_nr(priv));
1197         if (ret < 0) {
1198                 dev_err(dev, "cannot snd dai register\n");
1199                 goto exit_snd_soc;
1200         }
1201
1202         pm_runtime_enable(dev);
1203
1204         dev_info(dev, "probed\n");
1205         return ret;
1206
1207 exit_snd_soc:
1208         snd_soc_unregister_platform(dev);
1209 exit_snd_probe:
1210         for_each_rsnd_dai(rdai, priv, i) {
1211                 rsnd_dai_call(remove, &rdai->playback, priv);
1212                 rsnd_dai_call(remove, &rdai->capture, priv);
1213         }
1214
1215         return ret;
1216 }
1217
1218 static int rsnd_remove(struct platform_device *pdev)
1219 {
1220         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1221         struct rsnd_dai *rdai;
1222         void (*remove_func[])(struct rsnd_priv *priv) = {
1223                 rsnd_ssi_remove,
1224                 rsnd_ssiu_remove,
1225                 rsnd_src_remove,
1226                 rsnd_ctu_remove,
1227                 rsnd_mix_remove,
1228                 rsnd_dvc_remove,
1229                 rsnd_cmd_remove,
1230                 rsnd_adg_remove,
1231         };
1232         int ret = 0, i;
1233
1234         pm_runtime_disable(&pdev->dev);
1235
1236         for_each_rsnd_dai(rdai, priv, i) {
1237                 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1238                 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1239         }
1240
1241         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1242                 remove_func[i](priv);
1243
1244         snd_soc_unregister_component(&pdev->dev);
1245         snd_soc_unregister_platform(&pdev->dev);
1246
1247         return ret;
1248 }
1249
1250 static struct platform_driver rsnd_driver = {
1251         .driver = {
1252                 .name   = "rcar_sound",
1253                 .of_match_table = rsnd_of_match,
1254         },
1255         .probe          = rsnd_probe,
1256         .remove         = rsnd_remove,
1257 };
1258 module_platform_driver(rsnd_driver);
1259
1260 MODULE_LICENSE("GPL");
1261 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1262 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1263 MODULE_ALIAS("platform:rcar-pcm-audio");