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