Merge remote-tracking branches 'asoc/topic/wm8961', 'asoc/topic/wm8962', 'asoc/topic...
[linux-2.6-microblaze.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 /*
104  *      rsnd_platform functions
105  */
106 #define rsnd_platform_call(priv, dai, func, param...)   \
107         (!(priv->info->func) ? 0 :              \
108          priv->info->func(param))
109
110 #define rsnd_is_enable_path(io, name) \
111         ((io)->info ? (io)->info->name : NULL)
112 #define rsnd_info_id(priv, io, name) \
113         ((io)->info->name - priv->info->name##_info)
114
115 /*
116  *      rsnd_mod functions
117  */
118 char *rsnd_mod_name(struct rsnd_mod *mod)
119 {
120         if (!mod || !mod->ops)
121                 return "unknown";
122
123         return mod->ops->name;
124 }
125
126 void rsnd_mod_init(struct rsnd_priv *priv,
127                    struct rsnd_mod *mod,
128                    struct rsnd_mod_ops *ops,
129                    enum rsnd_mod_type type,
130                    int id)
131 {
132         mod->priv       = priv;
133         mod->id         = id;
134         mod->ops        = ops;
135         mod->type       = type;
136 }
137
138 /*
139  *      rsnd_dma functions
140  */
141 static void __rsnd_dma_start(struct rsnd_dma *dma);
142 static void rsnd_dma_continue(struct rsnd_dma *dma)
143 {
144         /* push next A or B plane */
145         dma->submit_loop = 1;
146         schedule_work(&dma->work);
147 }
148
149 void rsnd_dma_start(struct rsnd_dma *dma)
150 {
151         /* push both A and B plane*/
152         dma->offset = 0;
153         dma->submit_loop = 2;
154         __rsnd_dma_start(dma);
155 }
156
157 void rsnd_dma_stop(struct rsnd_dma *dma)
158 {
159         dma->submit_loop = 0;
160         cancel_work_sync(&dma->work);
161         dmaengine_terminate_all(dma->chan);
162 }
163
164 static void rsnd_dma_complete(void *data)
165 {
166         struct rsnd_dma *dma = (struct rsnd_dma *)data;
167         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
168         struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
169         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
170         unsigned long flags;
171
172         rsnd_lock(priv, flags);
173
174         /*
175          * Renesas sound Gen1 needs 1 DMAC,
176          * Gen2 needs 2 DMAC.
177          * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
178          * But, Audio-DMAC-peri-peri doesn't have interrupt,
179          * and this driver is assuming that here.
180          *
181          * If Audio-DMAC-peri-peri has interrpt,
182          * rsnd_dai_pointer_update() will be called twice,
183          * ant it will breaks io->byte_pos
184          */
185
186         rsnd_dai_pointer_update(io, io->byte_per_period);
187
188         if (dma->submit_loop)
189                 rsnd_dma_continue(dma);
190
191         rsnd_unlock(priv, flags);
192 }
193
194 static void __rsnd_dma_start(struct rsnd_dma *dma)
195 {
196         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
197         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
198         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
199         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
200         struct device *dev = rsnd_priv_to_dev(priv);
201         struct dma_async_tx_descriptor *desc;
202         dma_addr_t buf;
203         size_t len = io->byte_per_period;
204         int i;
205
206         for (i = 0; i < dma->submit_loop; i++) {
207
208                 buf = runtime->dma_addr +
209                         rsnd_dai_pointer_offset(io, dma->offset + len);
210                 dma->offset = len;
211
212                 desc = dmaengine_prep_slave_single(
213                         dma->chan, buf, len, dma->dir,
214                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
215                 if (!desc) {
216                         dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
217                         return;
218                 }
219
220                 desc->callback          = rsnd_dma_complete;
221                 desc->callback_param    = dma;
222
223                 if (dmaengine_submit(desc) < 0) {
224                         dev_err(dev, "dmaengine_submit() fail\n");
225                         return;
226                 }
227
228                 dma_async_issue_pending(dma->chan);
229         }
230 }
231
232 static void rsnd_dma_do_work(struct work_struct *work)
233 {
234         struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
235
236         __rsnd_dma_start(dma);
237 }
238
239 int rsnd_dma_available(struct rsnd_dma *dma)
240 {
241         return !!dma->chan;
242 }
243
244 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
245                   int is_play, int id)
246 {
247         struct device *dev = rsnd_priv_to_dev(priv);
248         struct dma_slave_config cfg;
249         dma_cap_mask_t mask;
250         int ret;
251
252         if (dma->chan) {
253                 dev_err(dev, "it already has dma channel\n");
254                 return -EIO;
255         }
256
257         dma_cap_zero(mask);
258         dma_cap_set(DMA_SLAVE, mask);
259
260         dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
261                                                      (void *)id, dev,
262                                                      is_play ? "tx" : "rx");
263         if (!dma->chan) {
264                 dev_err(dev, "can't get dma channel\n");
265                 return -EIO;
266         }
267
268         cfg.slave_id    = id;
269         cfg.dst_addr    = 0; /* use default addr when playback */
270         cfg.src_addr    = 0; /* use default addr when capture */
271         cfg.direction   = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
272
273         ret = dmaengine_slave_config(dma->chan, &cfg);
274         if (ret < 0)
275                 goto rsnd_dma_init_err;
276
277         dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
278         INIT_WORK(&dma->work, rsnd_dma_do_work);
279
280         return 0;
281
282 rsnd_dma_init_err:
283         rsnd_dma_quit(priv, dma);
284
285         return ret;
286 }
287
288 void  rsnd_dma_quit(struct rsnd_priv *priv,
289                     struct rsnd_dma *dma)
290 {
291         if (dma->chan)
292                 dma_release_channel(dma->chan);
293
294         dma->chan = NULL;
295 }
296
297 /*
298  *      rsnd_dai functions
299  */
300 #define __rsnd_mod_call(mod, func, rdai, io)                    \
301 ({                                                              \
302         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
303         struct device *dev = rsnd_priv_to_dev(priv);            \
304         dev_dbg(dev, "%s [%d] %s\n",                            \
305                 rsnd_mod_name(mod), rsnd_mod_id(mod), #func);   \
306         (mod)->ops->func(mod, rdai, io);                        \
307 })
308
309 #define rsnd_mod_call(mod, func, rdai, io)      \
310         (!(mod) ? -ENODEV :                     \
311          !((mod)->ops->func) ? 0 :              \
312          __rsnd_mod_call(mod, func, (rdai), (io)))
313
314 #define rsnd_dai_call(rdai, io, fn)                             \
315 ({                                                              \
316         struct rsnd_mod *mod;                                   \
317         int ret = 0, i;                                         \
318         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
319                 mod = (io)->mod[i];                             \
320                 if (!mod)                                       \
321                         continue;                               \
322                 ret = rsnd_mod_call(mod, fn, (rdai), (io));     \
323                 if (ret < 0)                                    \
324                         break;                                  \
325         }                                                       \
326         ret;                                                    \
327 })
328
329 static int rsnd_dai_connect(struct rsnd_mod *mod,
330                             struct rsnd_dai_stream *io)
331 {
332         if (!mod)
333                 return -EIO;
334
335         if (io->mod[mod->type]) {
336                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
337                 struct device *dev = rsnd_priv_to_dev(priv);
338
339                 dev_err(dev, "%s%d is not empty\n",
340                         rsnd_mod_name(mod),
341                         rsnd_mod_id(mod));
342                 return -EIO;
343         }
344
345         io->mod[mod->type] = mod;
346         mod->io = io;
347
348         return 0;
349 }
350
351 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
352 {
353         int id = rdai - priv->rdai;
354
355         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
356                 return -EINVAL;
357
358         return id;
359 }
360
361 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
362 {
363         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
364                 return NULL;
365
366         return priv->rdai + id;
367 }
368
369 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
370 {
371         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
372
373         return rsnd_dai_get(priv, dai->id);
374 }
375
376 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
377 {
378         return &rdai->playback == io;
379 }
380
381 /*
382  *      rsnd_soc_dai functions
383  */
384 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
385 {
386         struct snd_pcm_substream *substream = io->substream;
387         struct snd_pcm_runtime *runtime = substream->runtime;
388         int pos = io->byte_pos + additional;
389
390         pos %= (runtime->periods * io->byte_per_period);
391
392         return pos;
393 }
394
395 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
396 {
397         io->byte_pos += byte;
398
399         if (io->byte_pos >= io->next_period_byte) {
400                 struct snd_pcm_substream *substream = io->substream;
401                 struct snd_pcm_runtime *runtime = substream->runtime;
402
403                 io->period_pos++;
404                 io->next_period_byte += io->byte_per_period;
405
406                 if (io->period_pos >= runtime->periods) {
407                         io->byte_pos = 0;
408                         io->period_pos = 0;
409                         io->next_period_byte = io->byte_per_period;
410                 }
411
412                 snd_pcm_period_elapsed(substream);
413         }
414 }
415
416 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
417                                 struct snd_pcm_substream *substream)
418 {
419         struct snd_pcm_runtime *runtime = substream->runtime;
420
421         io->substream           = substream;
422         io->byte_pos            = 0;
423         io->period_pos          = 0;
424         io->byte_per_period     = runtime->period_size *
425                                   runtime->channels *
426                                   samples_to_bytes(runtime, 1);
427         io->next_period_byte    = io->byte_per_period;
428
429         return 0;
430 }
431
432 static
433 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
434 {
435         struct snd_soc_pcm_runtime *rtd = substream->private_data;
436
437         return  rtd->cpu_dai;
438 }
439
440 static
441 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
442                                         struct snd_pcm_substream *substream)
443 {
444         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
445                 return &rdai->playback;
446         else
447                 return &rdai->capture;
448 }
449
450 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
451                             struct snd_soc_dai *dai)
452 {
453         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
454         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
455         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
456         struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
457                                                 rsnd_dai_id(priv, rdai),
458                                                 rsnd_dai_is_play(rdai, io));
459         int ssi_id = rsnd_mod_id(mod);
460         int ret;
461         unsigned long flags;
462
463         rsnd_lock(priv, flags);
464
465         switch (cmd) {
466         case SNDRV_PCM_TRIGGER_START:
467                 ret = rsnd_dai_stream_init(io, substream);
468                 if (ret < 0)
469                         goto dai_trigger_end;
470
471                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
472                 if (ret < 0)
473                         goto dai_trigger_end;
474
475                 ret = rsnd_dai_call(rdai, io, init);
476                 if (ret < 0)
477                         goto dai_trigger_end;
478
479                 ret = rsnd_dai_call(rdai, io, start);
480                 if (ret < 0)
481                         goto dai_trigger_end;
482                 break;
483         case SNDRV_PCM_TRIGGER_STOP:
484                 ret = rsnd_dai_call(rdai, io, stop);
485                 if (ret < 0)
486                         goto dai_trigger_end;
487
488                 ret = rsnd_dai_call(rdai, io, quit);
489                 if (ret < 0)
490                         goto dai_trigger_end;
491
492                 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
493                 if (ret < 0)
494                         goto dai_trigger_end;
495                 break;
496         default:
497                 ret = -EINVAL;
498         }
499
500 dai_trigger_end:
501         rsnd_unlock(priv, flags);
502
503         return ret;
504 }
505
506 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
507 {
508         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
509
510         /* set master/slave audio interface */
511         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
512         case SND_SOC_DAIFMT_CBM_CFM:
513                 rdai->clk_master = 1;
514                 break;
515         case SND_SOC_DAIFMT_CBS_CFS:
516                 rdai->clk_master = 0;
517                 break;
518         default:
519                 return -EINVAL;
520         }
521
522         /* set clock inversion */
523         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
524         case SND_SOC_DAIFMT_NB_IF:
525                 rdai->bit_clk_inv = 0;
526                 rdai->frm_clk_inv = 1;
527                 break;
528         case SND_SOC_DAIFMT_IB_NF:
529                 rdai->bit_clk_inv = 1;
530                 rdai->frm_clk_inv = 0;
531                 break;
532         case SND_SOC_DAIFMT_IB_IF:
533                 rdai->bit_clk_inv = 1;
534                 rdai->frm_clk_inv = 1;
535                 break;
536         case SND_SOC_DAIFMT_NB_NF:
537         default:
538                 rdai->bit_clk_inv = 0;
539                 rdai->frm_clk_inv = 0;
540                 break;
541         }
542
543         /* set format */
544         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
545         case SND_SOC_DAIFMT_I2S:
546                 rdai->sys_delay = 0;
547                 rdai->data_alignment = 0;
548                 break;
549         case SND_SOC_DAIFMT_LEFT_J:
550                 rdai->sys_delay = 1;
551                 rdai->data_alignment = 0;
552                 break;
553         case SND_SOC_DAIFMT_RIGHT_J:
554                 rdai->sys_delay = 1;
555                 rdai->data_alignment = 1;
556                 break;
557         }
558
559         return 0;
560 }
561
562 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
563         .trigger        = rsnd_soc_dai_trigger,
564         .set_fmt        = rsnd_soc_dai_set_fmt,
565 };
566
567 static int rsnd_path_init(struct rsnd_priv *priv,
568                           struct rsnd_dai *rdai,
569                           struct rsnd_dai_stream *io)
570 {
571         struct rsnd_mod *mod;
572         struct rsnd_dai_platform_info *dai_info = rdai->info;
573         int ret;
574         int ssi_id = -1;
575         int src_id = -1;
576
577         /*
578          * Gen1 is created by SRU/SSI, and this SRU is base module of
579          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
580          *
581          * Easy image is..
582          *      Gen1 SRU = Gen2 SCU + SSIU + etc
583          *
584          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
585          * using fixed path.
586          */
587         if (dai_info) {
588                 if (rsnd_is_enable_path(io, ssi))
589                         ssi_id = rsnd_info_id(priv, io, ssi);
590                 if (rsnd_is_enable_path(io, src))
591                         src_id = rsnd_info_id(priv, io, src);
592         } else {
593                 /* get SSI's ID */
594                 mod = rsnd_ssi_mod_get_frm_dai(priv,
595                                                rsnd_dai_id(priv, rdai),
596                                                rsnd_dai_is_play(rdai, io));
597                 if (!mod)
598                         return 0;
599                 ssi_id = src_id = rsnd_mod_id(mod);
600         }
601
602         ret = 0;
603
604         /* SRC */
605         if (src_id >= 0) {
606                 mod = rsnd_src_mod_get(priv, src_id);
607                 ret = rsnd_dai_connect(mod, io);
608                 if (ret < 0)
609                         return ret;
610         }
611
612         /* SSI */
613         if (ssi_id >= 0) {
614                 mod = rsnd_ssi_mod_get(priv, ssi_id);
615                 ret = rsnd_dai_connect(mod, io);
616                 if (ret < 0)
617                         return ret;
618         }
619
620         return ret;
621 }
622
623 static int rsnd_dai_probe(struct platform_device *pdev,
624                           struct rsnd_priv *priv)
625 {
626         struct snd_soc_dai_driver *drv;
627         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
628         struct rsnd_dai *rdai;
629         struct rsnd_mod *pmod, *cmod;
630         struct device *dev = rsnd_priv_to_dev(priv);
631         int dai_nr = info->dai_info_nr;
632         int i;
633
634         /*
635          * dai_nr should be set via dai_info_nr,
636          * but allow it to keeping compatible
637          */
638         if (!dai_nr) {
639                 /* get max dai nr */
640                 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
641                         pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
642                         cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
643
644                         if (!pmod && !cmod)
645                                 break;
646                 }
647         }
648
649         if (!dai_nr) {
650                 dev_err(dev, "no dai\n");
651                 return -EIO;
652         }
653
654         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
655         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
656         if (!drv || !rdai) {
657                 dev_err(dev, "dai allocate failed\n");
658                 return -ENOMEM;
659         }
660
661         priv->rdai_nr   = dai_nr;
662         priv->daidrv    = drv;
663         priv->rdai      = rdai;
664
665         for (i = 0; i < dai_nr; i++) {
666                 if (info->dai_info)
667                         rdai[i].info = &info->dai_info[i];
668
669                 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
670                 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
671
672                 /*
673                  *      init rsnd_dai
674                  */
675                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
676
677                 /*
678                  *      init snd_soc_dai_driver
679                  */
680                 drv[i].name     = rdai[i].name;
681                 drv[i].ops      = &rsnd_soc_dai_ops;
682                 if (pmod) {
683                         drv[i].playback.rates           = RSND_RATES;
684                         drv[i].playback.formats         = RSND_FMTS;
685                         drv[i].playback.channels_min    = 2;
686                         drv[i].playback.channels_max    = 2;
687
688                         if (info->dai_info)
689                                 rdai[i].playback.info = &info->dai_info[i].playback;
690                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
691                 }
692                 if (cmod) {
693                         drv[i].capture.rates            = RSND_RATES;
694                         drv[i].capture.formats          = RSND_FMTS;
695                         drv[i].capture.channels_min     = 2;
696                         drv[i].capture.channels_max     = 2;
697
698                         if (info->dai_info)
699                                 rdai[i].capture.info = &info->dai_info[i].capture;
700                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
701                 }
702
703                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
704                         pmod ? "play"    : " -- ",
705                         cmod ? "capture" : "  --   ");
706         }
707
708         return 0;
709 }
710
711 /*
712  *              pcm ops
713  */
714 static struct snd_pcm_hardware rsnd_pcm_hardware = {
715         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
716                         SNDRV_PCM_INFO_MMAP             |
717                         SNDRV_PCM_INFO_MMAP_VALID       |
718                         SNDRV_PCM_INFO_PAUSE,
719         .buffer_bytes_max       = 64 * 1024,
720         .period_bytes_min       = 32,
721         .period_bytes_max       = 8192,
722         .periods_min            = 1,
723         .periods_max            = 32,
724         .fifo_size              = 256,
725 };
726
727 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
728 {
729         struct snd_pcm_runtime *runtime = substream->runtime;
730         int ret = 0;
731
732         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
733
734         ret = snd_pcm_hw_constraint_integer(runtime,
735                                             SNDRV_PCM_HW_PARAM_PERIODS);
736
737         return ret;
738 }
739
740 static int rsnd_hw_params(struct snd_pcm_substream *substream,
741                          struct snd_pcm_hw_params *hw_params)
742 {
743         return snd_pcm_lib_malloc_pages(substream,
744                                         params_buffer_bytes(hw_params));
745 }
746
747 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
748 {
749         struct snd_pcm_runtime *runtime = substream->runtime;
750         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
751         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
752         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
753
754         return bytes_to_frames(runtime, io->byte_pos);
755 }
756
757 static struct snd_pcm_ops rsnd_pcm_ops = {
758         .open           = rsnd_pcm_open,
759         .ioctl          = snd_pcm_lib_ioctl,
760         .hw_params      = rsnd_hw_params,
761         .hw_free        = snd_pcm_lib_free_pages,
762         .pointer        = rsnd_pointer,
763 };
764
765 /*
766  *              snd_soc_platform
767  */
768
769 #define PREALLOC_BUFFER         (32 * 1024)
770 #define PREALLOC_BUFFER_MAX     (32 * 1024)
771
772 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
773 {
774         return snd_pcm_lib_preallocate_pages_for_all(
775                 rtd->pcm,
776                 SNDRV_DMA_TYPE_DEV,
777                 rtd->card->snd_card->dev,
778                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
779 }
780
781 static void rsnd_pcm_free(struct snd_pcm *pcm)
782 {
783         snd_pcm_lib_preallocate_free_for_all(pcm);
784 }
785
786 static struct snd_soc_platform_driver rsnd_soc_platform = {
787         .ops            = &rsnd_pcm_ops,
788         .pcm_new        = rsnd_pcm_new,
789         .pcm_free       = rsnd_pcm_free,
790 };
791
792 static const struct snd_soc_component_driver rsnd_soc_component = {
793         .name           = "rsnd",
794 };
795
796 /*
797  *      rsnd probe
798  */
799 static int rsnd_probe(struct platform_device *pdev)
800 {
801         struct rcar_snd_info *info;
802         struct rsnd_priv *priv;
803         struct device *dev = &pdev->dev;
804         struct rsnd_dai *rdai;
805         int (*probe_func[])(struct platform_device *pdev,
806                             struct rsnd_priv *priv) = {
807                 rsnd_gen_probe,
808                 rsnd_ssi_probe,
809                 rsnd_src_probe,
810                 rsnd_adg_probe,
811                 rsnd_dai_probe,
812         };
813         int ret, i;
814
815         info = pdev->dev.platform_data;
816         if (!info) {
817                 dev_err(dev, "driver needs R-Car sound information\n");
818                 return -ENODEV;
819         }
820
821         /*
822          *      init priv data
823          */
824         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
825         if (!priv) {
826                 dev_err(dev, "priv allocate failed\n");
827                 return -ENODEV;
828         }
829
830         priv->dev       = dev;
831         priv->info      = info;
832         spin_lock_init(&priv->lock);
833
834         /*
835          *      init each module
836          */
837         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
838                 ret = probe_func[i](pdev, priv);
839                 if (ret)
840                         return ret;
841         }
842
843         for_each_rsnd_dai(rdai, priv, i) {
844                 ret = rsnd_dai_call(rdai, &rdai->playback, probe);
845                 if (ret)
846                         return ret;
847
848                 ret = rsnd_dai_call(rdai, &rdai->capture, probe);
849                 if (ret)
850                         return ret;
851         }
852
853         /*
854          *      asoc register
855          */
856         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
857         if (ret < 0) {
858                 dev_err(dev, "cannot snd soc register\n");
859                 return ret;
860         }
861
862         ret = snd_soc_register_component(dev, &rsnd_soc_component,
863                                          priv->daidrv, rsnd_rdai_nr(priv));
864         if (ret < 0) {
865                 dev_err(dev, "cannot snd dai register\n");
866                 goto exit_snd_soc;
867         }
868
869         dev_set_drvdata(dev, priv);
870
871         pm_runtime_enable(dev);
872
873         dev_info(dev, "probed\n");
874         return ret;
875
876 exit_snd_soc:
877         snd_soc_unregister_platform(dev);
878
879         return ret;
880 }
881
882 static int rsnd_remove(struct platform_device *pdev)
883 {
884         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
885         struct rsnd_dai *rdai;
886         int ret, i;
887
888         pm_runtime_disable(&pdev->dev);
889
890         for_each_rsnd_dai(rdai, priv, i) {
891                 ret = rsnd_dai_call(rdai, &rdai->playback, remove);
892                 if (ret)
893                         return ret;
894
895                 ret = rsnd_dai_call(rdai, &rdai->capture, remove);
896                 if (ret)
897                         return ret;
898         }
899
900         return 0;
901 }
902
903 static struct platform_driver rsnd_driver = {
904         .driver = {
905                 .name   = "rcar_sound",
906         },
907         .probe          = rsnd_probe,
908         .remove         = rsnd_remove,
909 };
910 module_platform_driver(rsnd_driver);
911
912 MODULE_LICENSE("GPL");
913 MODULE_DESCRIPTION("Renesas R-Car audio driver");
914 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
915 MODULE_ALIAS("platform:rcar-pcm-audio");