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