ASoC: rsnd: SSI + DMA can select BUSIF
[firefly-linux-kernel-4.4.55.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 <linux/shdma-base.h>
98 #include "rsnd.h"
99
100 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
101 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102
103 static struct rsnd_of_data rsnd_of_data_gen1 = {
104         .flags = RSND_GEN1,
105 };
106
107 static struct rsnd_of_data rsnd_of_data_gen2 = {
108         .flags = RSND_GEN2,
109 };
110
111 static struct of_device_id rsnd_of_match[] = {
112         { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113         { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
114         {},
115 };
116 MODULE_DEVICE_TABLE(of, rsnd_of_match);
117
118 /*
119  *      rsnd_platform functions
120  */
121 #define rsnd_platform_call(priv, dai, func, param...)   \
122         (!(priv->info->func) ? 0 :              \
123          priv->info->func(param))
124
125 #define rsnd_is_enable_path(io, name) \
126         ((io)->info ? (io)->info->name : NULL)
127 #define rsnd_info_id(priv, io, name) \
128         ((io)->info->name - priv->info->name##_info)
129
130 /*
131  *      rsnd_mod functions
132  */
133 char *rsnd_mod_name(struct rsnd_mod *mod)
134 {
135         if (!mod || !mod->ops)
136                 return "unknown";
137
138         return mod->ops->name;
139 }
140
141 char *rsnd_mod_dma_name(struct rsnd_mod *mod)
142 {
143         if (!mod || !mod->ops)
144                 return "unknown";
145
146         if (!mod->ops->dma_name)
147                 return mod->ops->name;
148
149         return mod->ops->dma_name(mod);
150 }
151
152 void rsnd_mod_init(struct rsnd_priv *priv,
153                    struct rsnd_mod *mod,
154                    struct rsnd_mod_ops *ops,
155                    enum rsnd_mod_type type,
156                    int id)
157 {
158         mod->priv       = priv;
159         mod->id         = id;
160         mod->ops        = ops;
161         mod->type       = type;
162 }
163
164 /*
165  *      rsnd_dma functions
166  */
167 static void __rsnd_dma_start(struct rsnd_dma *dma);
168 static void rsnd_dma_continue(struct rsnd_dma *dma)
169 {
170         /* push next A or B plane */
171         dma->submit_loop = 1;
172         schedule_work(&dma->work);
173 }
174
175 void rsnd_dma_start(struct rsnd_dma *dma)
176 {
177         /* push both A and B plane*/
178         dma->offset = 0;
179         dma->submit_loop = 2;
180         __rsnd_dma_start(dma);
181 }
182
183 void rsnd_dma_stop(struct rsnd_dma *dma)
184 {
185         dma->submit_loop = 0;
186         cancel_work_sync(&dma->work);
187         dmaengine_terminate_all(dma->chan);
188 }
189
190 static void rsnd_dma_complete(void *data)
191 {
192         struct rsnd_dma *dma = (struct rsnd_dma *)data;
193         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
194         struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
195         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
196         unsigned long flags;
197
198         rsnd_lock(priv, flags);
199
200         /*
201          * Renesas sound Gen1 needs 1 DMAC,
202          * Gen2 needs 2 DMAC.
203          * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
204          * But, Audio-DMAC-peri-peri doesn't have interrupt,
205          * and this driver is assuming that here.
206          *
207          * If Audio-DMAC-peri-peri has interrpt,
208          * rsnd_dai_pointer_update() will be called twice,
209          * ant it will breaks io->byte_pos
210          */
211         if (dma->submit_loop)
212                 rsnd_dma_continue(dma);
213
214         rsnd_unlock(priv, flags);
215
216         rsnd_dai_pointer_update(io, io->byte_per_period);
217 }
218
219 static void __rsnd_dma_start(struct rsnd_dma *dma)
220 {
221         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
222         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
223         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
224         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
225         struct device *dev = rsnd_priv_to_dev(priv);
226         struct dma_async_tx_descriptor *desc;
227         dma_addr_t buf;
228         size_t len = io->byte_per_period;
229         int i;
230
231         for (i = 0; i < dma->submit_loop; i++) {
232
233                 buf = runtime->dma_addr +
234                         rsnd_dai_pointer_offset(io, dma->offset + len);
235                 dma->offset = len;
236
237                 desc = dmaengine_prep_slave_single(
238                         dma->chan, buf, len, dma->dir,
239                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
240                 if (!desc) {
241                         dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
242                         return;
243                 }
244
245                 desc->callback          = rsnd_dma_complete;
246                 desc->callback_param    = dma;
247
248                 if (dmaengine_submit(desc) < 0) {
249                         dev_err(dev, "dmaengine_submit() fail\n");
250                         return;
251                 }
252
253                 dma_async_issue_pending(dma->chan);
254         }
255 }
256
257 static void rsnd_dma_do_work(struct work_struct *work)
258 {
259         struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
260
261         __rsnd_dma_start(dma);
262 }
263
264 int rsnd_dma_available(struct rsnd_dma *dma)
265 {
266         return !!dma->chan;
267 }
268
269 #define DMA_NAME_SIZE 16
270 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
271 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
272 {
273         if (mod)
274                 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
275                          rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
276         else
277                 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
278
279 }
280
281 static void rsnd_dma_of_name(struct rsnd_dma *dma,
282                              int is_play, char *dma_name)
283 {
284         struct rsnd_mod *this = rsnd_dma_to_mod(dma);
285         struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
286         struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
287         struct rsnd_mod *src = rsnd_io_to_mod_src(io);
288         struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
289         struct rsnd_mod *mod[MOD_MAX];
290         struct rsnd_mod *src_mod, *dst_mod;
291         int i, index;
292
293
294         for (i = 0; i < MOD_MAX; i++)
295                 mod[i] = NULL;
296
297         /*
298          * in play case...
299          *
300          * src -> dst
301          *
302          * mem -> SSI
303          * mem -> SRC -> SSI
304          * mem -> SRC -> DVC -> SSI
305          */
306         mod[0] = NULL; /* for "mem" */
307         index = 1;
308         for (i = 1; i < MOD_MAX; i++) {
309                 if (!src) {
310                         mod[i] = ssi;
311                 } else if (!dvc) {
312                         mod[i] = src;
313                         src = NULL;
314                 } else {
315                         mod[i] = dvc;
316                         dvc = NULL;
317                 }
318
319                 if (mod[i] == this)
320                         index = i;
321
322                 if (mod[i] == ssi)
323                         break;
324         }
325
326         if (is_play) {
327                 src_mod = mod[index - 1];
328                 dst_mod = mod[index];
329         } else {
330                 src_mod = mod[index];
331                 dst_mod = mod[index - 1];
332         }
333
334         index = 0;
335         index = _rsnd_dma_of_name(dma_name + index, src_mod);
336         *(dma_name + index++) = '_';
337         index = _rsnd_dma_of_name(dma_name + index, dst_mod);
338 }
339
340 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
341                   int is_play, int id)
342 {
343         struct device *dev = rsnd_priv_to_dev(priv);
344         struct dma_slave_config cfg;
345         char dma_name[DMA_NAME_SIZE];
346         dma_cap_mask_t mask;
347         int ret;
348
349         if (dma->chan) {
350                 dev_err(dev, "it already has dma channel\n");
351                 return -EIO;
352         }
353
354         dma_cap_zero(mask);
355         dma_cap_set(DMA_SLAVE, mask);
356
357         rsnd_dma_of_name(dma, is_play, dma_name);
358         rsnd_gen_dma_addr(priv, dma, &cfg, is_play, id);
359
360         dev_dbg(dev, "dma name : %s\n", dma_name);
361
362         dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
363                                                      (void *)id, dev,
364                                                      dma_name);
365         if (!dma->chan) {
366                 dev_err(dev, "can't get dma channel\n");
367                 return -EIO;
368         }
369
370         ret = dmaengine_slave_config(dma->chan, &cfg);
371         if (ret < 0)
372                 goto rsnd_dma_init_err;
373
374         dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
375         INIT_WORK(&dma->work, rsnd_dma_do_work);
376
377         return 0;
378
379 rsnd_dma_init_err:
380         rsnd_dma_quit(priv, dma);
381
382         return ret;
383 }
384
385 void  rsnd_dma_quit(struct rsnd_priv *priv,
386                     struct rsnd_dma *dma)
387 {
388         if (dma->chan)
389                 dma_release_channel(dma->chan);
390
391         dma->chan = NULL;
392 }
393
394 /*
395  *      settting function
396  */
397 u32 rsnd_get_adinr(struct rsnd_mod *mod)
398 {
399         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
400         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
401         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
402         struct device *dev = rsnd_priv_to_dev(priv);
403         u32 adinr = runtime->channels;
404
405         switch (runtime->sample_bits) {
406         case 16:
407                 adinr |= (8 << 16);
408                 break;
409         case 32:
410                 adinr |= (0 << 16);
411                 break;
412         default:
413                 dev_warn(dev, "not supported sample bits\n");
414                 return 0;
415         }
416
417         return adinr;
418 }
419
420 /*
421  *      rsnd_dai functions
422  */
423 #define __rsnd_mod_call(mod, func, rdai...)                     \
424 ({                                                              \
425         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
426         struct device *dev = rsnd_priv_to_dev(priv);            \
427         dev_dbg(dev, "%s [%d] %s\n",                            \
428                 rsnd_mod_name(mod), rsnd_mod_id(mod), #func);   \
429         (mod)->ops->func(mod, rdai);                            \
430 })
431
432 #define rsnd_mod_call(mod, func, rdai...)       \
433         (!(mod) ? -ENODEV :                     \
434          !((mod)->ops->func) ? 0 :              \
435          __rsnd_mod_call(mod, func, rdai))
436
437 #define rsnd_dai_call(fn, io, rdai...)                          \
438 ({                                                              \
439         struct rsnd_mod *mod;                                   \
440         int ret = 0, i;                                         \
441         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
442                 mod = (io)->mod[i];                             \
443                 if (!mod)                                       \
444                         continue;                               \
445                 ret = rsnd_mod_call(mod, fn, rdai);             \
446                 if (ret < 0)                                    \
447                         break;                                  \
448         }                                                       \
449         ret;                                                    \
450 })
451
452 static int rsnd_dai_connect(struct rsnd_mod *mod,
453                             struct rsnd_dai_stream *io)
454 {
455         if (!mod)
456                 return -EIO;
457
458         if (io->mod[mod->type]) {
459                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
460                 struct device *dev = rsnd_priv_to_dev(priv);
461
462                 dev_err(dev, "%s%d is not empty\n",
463                         rsnd_mod_name(mod),
464                         rsnd_mod_id(mod));
465                 return -EIO;
466         }
467
468         io->mod[mod->type] = mod;
469         mod->io = io;
470
471         return 0;
472 }
473
474 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
475 {
476         int id = rdai - priv->rdai;
477
478         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
479                 return -EINVAL;
480
481         return id;
482 }
483
484 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
485 {
486         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
487                 return NULL;
488
489         return priv->rdai + id;
490 }
491
492 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
493 {
494         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
495
496         return rsnd_dai_get(priv, dai->id);
497 }
498
499 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
500 {
501         return &rdai->playback == io;
502 }
503
504 /*
505  *      rsnd_soc_dai functions
506  */
507 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
508 {
509         struct snd_pcm_substream *substream = io->substream;
510         struct snd_pcm_runtime *runtime = substream->runtime;
511         int pos = io->byte_pos + additional;
512
513         pos %= (runtime->periods * io->byte_per_period);
514
515         return pos;
516 }
517
518 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
519 {
520         io->byte_pos += byte;
521
522         if (io->byte_pos >= io->next_period_byte) {
523                 struct snd_pcm_substream *substream = io->substream;
524                 struct snd_pcm_runtime *runtime = substream->runtime;
525
526                 io->period_pos++;
527                 io->next_period_byte += io->byte_per_period;
528
529                 if (io->period_pos >= runtime->periods) {
530                         io->byte_pos = 0;
531                         io->period_pos = 0;
532                         io->next_period_byte = io->byte_per_period;
533                 }
534
535                 snd_pcm_period_elapsed(substream);
536         }
537 }
538
539 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
540                                 struct snd_pcm_substream *substream)
541 {
542         struct snd_pcm_runtime *runtime = substream->runtime;
543
544         io->substream           = substream;
545         io->byte_pos            = 0;
546         io->period_pos          = 0;
547         io->byte_per_period     = runtime->period_size *
548                                   runtime->channels *
549                                   samples_to_bytes(runtime, 1);
550         io->next_period_byte    = io->byte_per_period;
551
552         return 0;
553 }
554
555 static
556 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
557 {
558         struct snd_soc_pcm_runtime *rtd = substream->private_data;
559
560         return  rtd->cpu_dai;
561 }
562
563 static
564 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
565                                         struct snd_pcm_substream *substream)
566 {
567         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
568                 return &rdai->playback;
569         else
570                 return &rdai->capture;
571 }
572
573 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
574                             struct snd_soc_dai *dai)
575 {
576         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
577         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
578         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
579         int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
580         int ret;
581         unsigned long flags;
582
583         rsnd_lock(priv, flags);
584
585         switch (cmd) {
586         case SNDRV_PCM_TRIGGER_START:
587                 ret = rsnd_dai_stream_init(io, substream);
588                 if (ret < 0)
589                         goto dai_trigger_end;
590
591                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
592                 if (ret < 0)
593                         goto dai_trigger_end;
594
595                 ret = rsnd_dai_call(init, io, rdai);
596                 if (ret < 0)
597                         goto dai_trigger_end;
598
599                 ret = rsnd_dai_call(start, io, rdai);
600                 if (ret < 0)
601                         goto dai_trigger_end;
602                 break;
603         case SNDRV_PCM_TRIGGER_STOP:
604                 ret = rsnd_dai_call(stop, io, rdai);
605                 if (ret < 0)
606                         goto dai_trigger_end;
607
608                 ret = rsnd_dai_call(quit, io, rdai);
609                 if (ret < 0)
610                         goto dai_trigger_end;
611
612                 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
613                 if (ret < 0)
614                         goto dai_trigger_end;
615                 break;
616         default:
617                 ret = -EINVAL;
618         }
619
620 dai_trigger_end:
621         rsnd_unlock(priv, flags);
622
623         return ret;
624 }
625
626 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
627 {
628         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
629
630         /* set master/slave audio interface */
631         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
632         case SND_SOC_DAIFMT_CBM_CFM:
633                 rdai->clk_master = 0;
634                 break;
635         case SND_SOC_DAIFMT_CBS_CFS:
636                 rdai->clk_master = 1; /* codec is slave, cpu is master */
637                 break;
638         default:
639                 return -EINVAL;
640         }
641
642         /* set clock inversion */
643         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
644         case SND_SOC_DAIFMT_NB_IF:
645                 rdai->bit_clk_inv = 0;
646                 rdai->frm_clk_inv = 1;
647                 break;
648         case SND_SOC_DAIFMT_IB_NF:
649                 rdai->bit_clk_inv = 1;
650                 rdai->frm_clk_inv = 0;
651                 break;
652         case SND_SOC_DAIFMT_IB_IF:
653                 rdai->bit_clk_inv = 1;
654                 rdai->frm_clk_inv = 1;
655                 break;
656         case SND_SOC_DAIFMT_NB_NF:
657         default:
658                 rdai->bit_clk_inv = 0;
659                 rdai->frm_clk_inv = 0;
660                 break;
661         }
662
663         /* set format */
664         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
665         case SND_SOC_DAIFMT_I2S:
666                 rdai->sys_delay = 0;
667                 rdai->data_alignment = 0;
668                 break;
669         case SND_SOC_DAIFMT_LEFT_J:
670                 rdai->sys_delay = 1;
671                 rdai->data_alignment = 0;
672                 break;
673         case SND_SOC_DAIFMT_RIGHT_J:
674                 rdai->sys_delay = 1;
675                 rdai->data_alignment = 1;
676                 break;
677         }
678
679         return 0;
680 }
681
682 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
683         .trigger        = rsnd_soc_dai_trigger,
684         .set_fmt        = rsnd_soc_dai_set_fmt,
685 };
686
687 #define rsnd_path_parse(priv, io, type)                         \
688 ({                                                              \
689         struct rsnd_mod *mod;                                   \
690         int ret = 0;                                            \
691         int id = -1;                                            \
692                                                                 \
693         if (rsnd_is_enable_path(io, type)) {                    \
694                 id = rsnd_info_id(priv, io, type);              \
695                 if (id >= 0) {                                  \
696                         mod = rsnd_##type##_mod_get(priv, id);  \
697                         ret = rsnd_dai_connect(mod, io);        \
698                 }                                               \
699         }                                                       \
700         ret;                                                    \
701 })
702
703 static int rsnd_path_init(struct rsnd_priv *priv,
704                           struct rsnd_dai *rdai,
705                           struct rsnd_dai_stream *io)
706 {
707         int ret;
708
709         /*
710          * Gen1 is created by SRU/SSI, and this SRU is base module of
711          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
712          *
713          * Easy image is..
714          *      Gen1 SRU = Gen2 SCU + SSIU + etc
715          *
716          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
717          * using fixed path.
718          */
719
720         /* SRC */
721         ret = rsnd_path_parse(priv, io, src);
722         if (ret < 0)
723                 return ret;
724
725         /* SSI */
726         ret = rsnd_path_parse(priv, io, ssi);
727         if (ret < 0)
728                 return ret;
729
730         /* DVC */
731         ret = rsnd_path_parse(priv, io, dvc);
732         if (ret < 0)
733                 return ret;
734
735         return ret;
736 }
737
738 static void rsnd_of_parse_dai(struct platform_device *pdev,
739                               const struct rsnd_of_data *of_data,
740                               struct rsnd_priv *priv)
741 {
742         struct device_node *dai_node,   *dai_np;
743         struct device_node *ssi_node,   *ssi_np;
744         struct device_node *src_node,   *src_np;
745         struct device_node *playback, *capture;
746         struct rsnd_dai_platform_info *dai_info;
747         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
748         struct device *dev = &pdev->dev;
749         int nr, i;
750         int dai_i, ssi_i, src_i;
751
752         if (!of_data)
753                 return;
754
755         dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
756         if (!dai_node)
757                 return;
758
759         nr = of_get_child_count(dai_node);
760         if (!nr)
761                 return;
762
763         dai_info = devm_kzalloc(dev,
764                                 sizeof(struct rsnd_dai_platform_info) * nr,
765                                 GFP_KERNEL);
766         if (!dai_info) {
767                 dev_err(dev, "dai info allocation error\n");
768                 return;
769         }
770
771         info->dai_info_nr       = nr;
772         info->dai_info          = dai_info;
773
774         ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
775         src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
776
777 #define mod_parse(name)                                                 \
778 if (name##_node) {                                                      \
779         struct rsnd_##name##_platform_info *name##_info;                \
780                                                                         \
781         name##_i = 0;                                                   \
782         for_each_child_of_node(name##_node, name##_np) {                \
783                 name##_info = info->name##_info + name##_i;             \
784                                                                         \
785                 if (name##_np == playback)                              \
786                         dai_info->playback.name = name##_info;          \
787                 if (name##_np == capture)                               \
788                         dai_info->capture.name = name##_info;           \
789                                                                         \
790                 name##_i++;                                             \
791         }                                                               \
792 }
793
794         /*
795          * parse all dai
796          */
797         dai_i = 0;
798         for_each_child_of_node(dai_node, dai_np) {
799                 dai_info = info->dai_info + dai_i;
800
801                 for (i = 0;; i++) {
802
803                         playback = of_parse_phandle(dai_np, "playback", i);
804                         capture  = of_parse_phandle(dai_np, "capture", i);
805
806                         if (!playback && !capture)
807                                 break;
808
809                         mod_parse(ssi);
810                         mod_parse(src);
811
812                         if (playback)
813                                 of_node_put(playback);
814                         if (capture)
815                                 of_node_put(capture);
816                 }
817
818                 dai_i++;
819         }
820 }
821
822 static int rsnd_dai_probe(struct platform_device *pdev,
823                           const struct rsnd_of_data *of_data,
824                           struct rsnd_priv *priv)
825 {
826         struct snd_soc_dai_driver *drv;
827         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
828         struct rsnd_dai *rdai;
829         struct rsnd_ssi_platform_info *pmod, *cmod;
830         struct device *dev = rsnd_priv_to_dev(priv);
831         int dai_nr;
832         int i;
833
834         rsnd_of_parse_dai(pdev, of_data, priv);
835
836         dai_nr = info->dai_info_nr;
837         if (!dai_nr) {
838                 dev_err(dev, "no dai\n");
839                 return -EIO;
840         }
841
842         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
843         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
844         if (!drv || !rdai) {
845                 dev_err(dev, "dai allocate failed\n");
846                 return -ENOMEM;
847         }
848
849         priv->rdai_nr   = dai_nr;
850         priv->daidrv    = drv;
851         priv->rdai      = rdai;
852
853         for (i = 0; i < dai_nr; i++) {
854                 rdai[i].info = &info->dai_info[i];
855
856                 pmod = rdai[i].info->playback.ssi;
857                 cmod = rdai[i].info->capture.ssi;
858
859                 /*
860                  *      init rsnd_dai
861                  */
862                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
863
864                 /*
865                  *      init snd_soc_dai_driver
866                  */
867                 drv[i].name     = rdai[i].name;
868                 drv[i].ops      = &rsnd_soc_dai_ops;
869                 if (pmod) {
870                         drv[i].playback.rates           = RSND_RATES;
871                         drv[i].playback.formats         = RSND_FMTS;
872                         drv[i].playback.channels_min    = 2;
873                         drv[i].playback.channels_max    = 2;
874
875                         rdai[i].playback.info = &info->dai_info[i].playback;
876                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
877                 }
878                 if (cmod) {
879                         drv[i].capture.rates            = RSND_RATES;
880                         drv[i].capture.formats          = RSND_FMTS;
881                         drv[i].capture.channels_min     = 2;
882                         drv[i].capture.channels_max     = 2;
883
884                         rdai[i].capture.info = &info->dai_info[i].capture;
885                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
886                 }
887
888                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
889                         pmod ? "play"    : " -- ",
890                         cmod ? "capture" : "  --   ");
891         }
892
893         return 0;
894 }
895
896 /*
897  *              pcm ops
898  */
899 static struct snd_pcm_hardware rsnd_pcm_hardware = {
900         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
901                         SNDRV_PCM_INFO_MMAP             |
902                         SNDRV_PCM_INFO_MMAP_VALID       |
903                         SNDRV_PCM_INFO_PAUSE,
904         .buffer_bytes_max       = 64 * 1024,
905         .period_bytes_min       = 32,
906         .period_bytes_max       = 8192,
907         .periods_min            = 1,
908         .periods_max            = 32,
909         .fifo_size              = 256,
910 };
911
912 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
913 {
914         struct snd_pcm_runtime *runtime = substream->runtime;
915         int ret = 0;
916
917         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
918
919         ret = snd_pcm_hw_constraint_integer(runtime,
920                                             SNDRV_PCM_HW_PARAM_PERIODS);
921
922         return ret;
923 }
924
925 static int rsnd_hw_params(struct snd_pcm_substream *substream,
926                          struct snd_pcm_hw_params *hw_params)
927 {
928         return snd_pcm_lib_malloc_pages(substream,
929                                         params_buffer_bytes(hw_params));
930 }
931
932 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
933 {
934         struct snd_pcm_runtime *runtime = substream->runtime;
935         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
936         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
937         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
938
939         return bytes_to_frames(runtime, io->byte_pos);
940 }
941
942 static struct snd_pcm_ops rsnd_pcm_ops = {
943         .open           = rsnd_pcm_open,
944         .ioctl          = snd_pcm_lib_ioctl,
945         .hw_params      = rsnd_hw_params,
946         .hw_free        = snd_pcm_lib_free_pages,
947         .pointer        = rsnd_pointer,
948 };
949
950 /*
951  *              snd_soc_platform
952  */
953
954 #define PREALLOC_BUFFER         (32 * 1024)
955 #define PREALLOC_BUFFER_MAX     (32 * 1024)
956
957 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
958 {
959         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
960         struct rsnd_dai *rdai;
961         int i, ret;
962
963         for_each_rsnd_dai(rdai, priv, i) {
964                 ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd);
965                 if (ret)
966                         return ret;
967
968                 ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd);
969                 if (ret)
970                         return ret;
971         }
972
973         return snd_pcm_lib_preallocate_pages_for_all(
974                 rtd->pcm,
975                 SNDRV_DMA_TYPE_DEV,
976                 rtd->card->snd_card->dev,
977                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
978 }
979
980 static void rsnd_pcm_free(struct snd_pcm *pcm)
981 {
982         snd_pcm_lib_preallocate_free_for_all(pcm);
983 }
984
985 static struct snd_soc_platform_driver rsnd_soc_platform = {
986         .ops            = &rsnd_pcm_ops,
987         .pcm_new        = rsnd_pcm_new,
988         .pcm_free       = rsnd_pcm_free,
989 };
990
991 static const struct snd_soc_component_driver rsnd_soc_component = {
992         .name           = "rsnd",
993 };
994
995 /*
996  *      rsnd probe
997  */
998 static int rsnd_probe(struct platform_device *pdev)
999 {
1000         struct rcar_snd_info *info;
1001         struct rsnd_priv *priv;
1002         struct device *dev = &pdev->dev;
1003         struct rsnd_dai *rdai;
1004         const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1005         const struct rsnd_of_data *of_data;
1006         int (*probe_func[])(struct platform_device *pdev,
1007                             const struct rsnd_of_data *of_data,
1008                             struct rsnd_priv *priv) = {
1009                 rsnd_gen_probe,
1010                 rsnd_ssi_probe,
1011                 rsnd_src_probe,
1012                 rsnd_dvc_probe,
1013                 rsnd_adg_probe,
1014                 rsnd_dai_probe,
1015         };
1016         int ret, i;
1017
1018         info = NULL;
1019         of_data = NULL;
1020         if (of_id) {
1021                 info = devm_kzalloc(&pdev->dev,
1022                                     sizeof(struct rcar_snd_info), GFP_KERNEL);
1023                 of_data = of_id->data;
1024         } else {
1025                 info = pdev->dev.platform_data;
1026         }
1027
1028         if (!info) {
1029                 dev_err(dev, "driver needs R-Car sound information\n");
1030                 return -ENODEV;
1031         }
1032
1033         /*
1034          *      init priv data
1035          */
1036         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1037         if (!priv) {
1038                 dev_err(dev, "priv allocate failed\n");
1039                 return -ENODEV;
1040         }
1041
1042         priv->pdev      = pdev;
1043         priv->info      = info;
1044         spin_lock_init(&priv->lock);
1045
1046         /*
1047          *      init each module
1048          */
1049         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1050                 ret = probe_func[i](pdev, of_data, priv);
1051                 if (ret)
1052                         return ret;
1053         }
1054
1055         for_each_rsnd_dai(rdai, priv, i) {
1056                 ret = rsnd_dai_call(probe, &rdai->playback, rdai);
1057                 if (ret)
1058                         return ret;
1059
1060                 ret = rsnd_dai_call(probe, &rdai->capture, rdai);
1061                 if (ret)
1062                         return ret;
1063         }
1064
1065         /*
1066          *      asoc register
1067          */
1068         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1069         if (ret < 0) {
1070                 dev_err(dev, "cannot snd soc register\n");
1071                 return ret;
1072         }
1073
1074         ret = snd_soc_register_component(dev, &rsnd_soc_component,
1075                                          priv->daidrv, rsnd_rdai_nr(priv));
1076         if (ret < 0) {
1077                 dev_err(dev, "cannot snd dai register\n");
1078                 goto exit_snd_soc;
1079         }
1080
1081         dev_set_drvdata(dev, priv);
1082
1083         pm_runtime_enable(dev);
1084
1085         dev_info(dev, "probed\n");
1086         return ret;
1087
1088 exit_snd_soc:
1089         snd_soc_unregister_platform(dev);
1090
1091         return ret;
1092 }
1093
1094 static int rsnd_remove(struct platform_device *pdev)
1095 {
1096         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1097         struct rsnd_dai *rdai;
1098         int ret, i;
1099
1100         pm_runtime_disable(&pdev->dev);
1101
1102         for_each_rsnd_dai(rdai, priv, i) {
1103                 ret = rsnd_dai_call(remove, &rdai->playback, rdai);
1104                 if (ret)
1105                         return ret;
1106
1107                 ret = rsnd_dai_call(remove, &rdai->capture, rdai);
1108                 if (ret)
1109                         return ret;
1110         }
1111
1112         return 0;
1113 }
1114
1115 static struct platform_driver rsnd_driver = {
1116         .driver = {
1117                 .name   = "rcar_sound",
1118                 .of_match_table = rsnd_of_match,
1119         },
1120         .probe          = rsnd_probe,
1121         .remove         = rsnd_remove,
1122 };
1123 module_platform_driver(rsnd_driver);
1124
1125 MODULE_LICENSE("GPL");
1126 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1127 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1128 MODULE_ALIAS("platform:rcar-pcm-audio");