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