ASoC: rsnd: run rsnd_path_init() when probe() timing
[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 scu
77  *   |
78  *   +- scu
79  *      |
80  *      +- scu[0]
81  *      +- scu[1]
82  *      +- scu[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 /*
111  *      rsnd_mod functions
112  */
113 char *rsnd_mod_name(struct rsnd_mod *mod)
114 {
115         if (!mod || !mod->ops)
116                 return "unknown";
117
118         return mod->ops->name;
119 }
120
121 void rsnd_mod_init(struct rsnd_priv *priv,
122                    struct rsnd_mod *mod,
123                    struct rsnd_mod_ops *ops,
124                    int id)
125 {
126         mod->priv       = priv;
127         mod->id         = id;
128         mod->ops        = ops;
129         INIT_LIST_HEAD(&mod->list);
130 }
131
132 /*
133  *      rsnd_dma functions
134  */
135 static void __rsnd_dma_start(struct rsnd_dma *dma);
136 static void rsnd_dma_continue(struct rsnd_dma *dma)
137 {
138         /* push next A or B plane */
139         dma->submit_loop = 1;
140         schedule_work(&dma->work);
141 }
142
143 void rsnd_dma_start(struct rsnd_dma *dma)
144 {
145         /* push both A and B plane*/
146         dma->offset = 0;
147         dma->submit_loop = 2;
148         __rsnd_dma_start(dma);
149 }
150
151 void rsnd_dma_stop(struct rsnd_dma *dma)
152 {
153         dma->submit_loop = 0;
154         cancel_work_sync(&dma->work);
155         dmaengine_terminate_all(dma->chan);
156 }
157
158 static void rsnd_dma_complete(void *data)
159 {
160         struct rsnd_dma *dma = (struct rsnd_dma *)data;
161         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
162         struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
163         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
164         unsigned long flags;
165
166         rsnd_lock(priv, flags);
167
168         /*
169          * Renesas sound Gen1 needs 1 DMAC,
170          * Gen2 needs 2 DMAC.
171          * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
172          * But, Audio-DMAC-peri-peri doesn't have interrupt,
173          * and this driver is assuming that here.
174          *
175          * If Audio-DMAC-peri-peri has interrpt,
176          * rsnd_dai_pointer_update() will be called twice,
177          * ant it will breaks io->byte_pos
178          */
179
180         rsnd_dai_pointer_update(io, io->byte_per_period);
181
182         if (dma->submit_loop)
183                 rsnd_dma_continue(dma);
184
185         rsnd_unlock(priv, flags);
186 }
187
188 static void __rsnd_dma_start(struct rsnd_dma *dma)
189 {
190         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
191         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
192         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
193         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
194         struct device *dev = rsnd_priv_to_dev(priv);
195         struct dma_async_tx_descriptor *desc;
196         dma_addr_t buf;
197         size_t len = io->byte_per_period;
198         int i;
199
200         for (i = 0; i < dma->submit_loop; i++) {
201
202                 buf = runtime->dma_addr +
203                         rsnd_dai_pointer_offset(io, dma->offset + len);
204                 dma->offset = len;
205
206                 desc = dmaengine_prep_slave_single(
207                         dma->chan, buf, len, dma->dir,
208                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
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
226 static void rsnd_dma_do_work(struct work_struct *work)
227 {
228         struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
229
230         __rsnd_dma_start(dma);
231 }
232
233 int rsnd_dma_available(struct rsnd_dma *dma)
234 {
235         return !!dma->chan;
236 }
237
238 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
239                   int is_play, int id)
240 {
241         struct device *dev = rsnd_priv_to_dev(priv);
242         struct dma_slave_config cfg;
243         dma_cap_mask_t mask;
244         int ret;
245
246         if (dma->chan) {
247                 dev_err(dev, "it already has dma channel\n");
248                 return -EIO;
249         }
250
251         dma_cap_zero(mask);
252         dma_cap_set(DMA_SLAVE, mask);
253
254         dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
255                                                      (void *)id, dev,
256                                                      is_play ? "tx" : "rx");
257         if (!dma->chan) {
258                 dev_err(dev, "can't get dma channel\n");
259                 return -EIO;
260         }
261
262         cfg.slave_id    = id;
263         cfg.dst_addr    = 0; /* use default addr when playback */
264         cfg.src_addr    = 0; /* use default addr when capture */
265         cfg.direction   = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
266
267         ret = dmaengine_slave_config(dma->chan, &cfg);
268         if (ret < 0)
269                 goto rsnd_dma_init_err;
270
271         dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
272         INIT_WORK(&dma->work, rsnd_dma_do_work);
273
274         return 0;
275
276 rsnd_dma_init_err:
277         rsnd_dma_quit(priv, dma);
278
279         return ret;
280 }
281
282 void  rsnd_dma_quit(struct rsnd_priv *priv,
283                     struct rsnd_dma *dma)
284 {
285         if (dma->chan)
286                 dma_release_channel(dma->chan);
287
288         dma->chan = NULL;
289 }
290
291 /*
292  *      rsnd_dai functions
293  */
294 #define __rsnd_mod_call(mod, func, rdai, io)                    \
295 ({                                                              \
296         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
297         struct device *dev = rsnd_priv_to_dev(priv);            \
298         dev_dbg(dev, "%s [%d] %s\n",                            \
299                 rsnd_mod_name(mod), rsnd_mod_id(mod), #func);   \
300         (mod)->ops->func(mod, rdai, io);                        \
301 })
302
303 #define rsnd_mod_call(mod, func, rdai, io)      \
304         (!(mod) ? -ENODEV :                     \
305          !((mod)->ops->func) ? 0 :              \
306          __rsnd_mod_call(mod, func, (rdai), (io)))
307
308 #define rsnd_dai_call(rdai, io, fn)                             \
309 ({                                                              \
310         struct rsnd_mod *mod, *n;                               \
311         int ret = 0;                                            \
312         for_each_rsnd_mod(mod, n, (io)) {                       \
313                 ret = rsnd_mod_call(mod, fn, (rdai), (io));     \
314                 if (ret < 0)                                    \
315                         break;                                  \
316         }                                                       \
317         ret;                                                    \
318 })
319
320 static int rsnd_dai_connect(struct rsnd_dai *rdai,
321                             struct rsnd_mod *mod,
322                             struct rsnd_dai_stream *io)
323 {
324         if (!mod)
325                 return -EIO;
326
327         if (!list_empty(&mod->list)) {
328                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
329                 struct device *dev = rsnd_priv_to_dev(priv);
330
331                 dev_err(dev, "%s%d is not empty\n",
332                         rsnd_mod_name(mod),
333                         rsnd_mod_id(mod));
334                 return -EIO;
335         }
336
337         list_add_tail(&mod->list, &io->head);
338         mod->io = io;
339
340         return 0;
341 }
342
343 static int rsnd_dai_disconnect(struct rsnd_mod *mod)
344 {
345         list_del_init(&mod->list);
346         mod->io = NULL;
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         int ret;
573         int id;
574
575         /*
576          * Gen1 is created by SRU/SSI, and this SRU is base module of
577          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
578          *
579          * Easy image is..
580          *      Gen1 SRU = Gen2 SCU + SSIU + etc
581          *
582          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
583          * using fixed path.
584          *
585          * Then, SSI id = SCU id here
586          */
587         /* get SSI's ID */
588         mod = rsnd_ssi_mod_get_frm_dai(priv,
589                                        rsnd_dai_id(priv, rdai),
590                                        rsnd_dai_is_play(rdai, io));
591         if (!mod)
592                 return 0;
593         id = rsnd_mod_id(mod);
594         ret = 0;
595
596         /* SCU */
597         mod = rsnd_scu_mod_get(priv, id);
598         if (mod) {
599                 ret = rsnd_dai_connect(rdai, mod, io);
600                 if (ret < 0)
601                         return ret;
602         }
603
604         /* SSI */
605         mod = rsnd_ssi_mod_get(priv, id);
606         if (mod) {
607                 ret = rsnd_dai_connect(rdai, mod, io);
608                 if (ret < 0)
609                         return ret;
610         }
611
612         return ret;
613 }
614
615 static int rsnd_path_exit(struct rsnd_priv *priv,
616                           struct rsnd_dai *rdai,
617                           struct rsnd_dai_stream *io)
618 {
619         struct rsnd_mod *mod, *n;
620         int ret = 0;
621
622         /*
623          * remove all mod from rdai
624          */
625         for_each_rsnd_mod(mod, n, io)
626                 ret |= rsnd_dai_disconnect(mod);
627
628         return ret;
629 }
630
631 static int rsnd_dai_probe(struct platform_device *pdev,
632                           struct rsnd_priv *priv)
633 {
634         struct snd_soc_dai_driver *drv;
635         struct rsnd_dai *rdai;
636         struct rsnd_mod *pmod, *cmod;
637         struct device *dev = rsnd_priv_to_dev(priv);
638         int dai_nr;
639         int i;
640
641         /* get max dai nr */
642         for (dai_nr = 0; dai_nr < 32; dai_nr++) {
643                 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
644                 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
645
646                 if (!pmod && !cmod)
647                         break;
648         }
649
650         if (!dai_nr) {
651                 dev_err(dev, "no dai\n");
652                 return -EIO;
653         }
654
655         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
656         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
657         if (!drv || !rdai) {
658                 dev_err(dev, "dai allocate failed\n");
659                 return -ENOMEM;
660         }
661
662         priv->rdai_nr   = dai_nr;
663         priv->daidrv    = drv;
664         priv->rdai      = rdai;
665
666         for (i = 0; i < dai_nr; i++) {
667
668                 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
669                 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
670
671                 /*
672                  *      init rsnd_dai
673                  */
674                 INIT_LIST_HEAD(&rdai[i].playback.head);
675                 INIT_LIST_HEAD(&rdai[i].capture.head);
676
677                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
678
679                 /*
680                  *      init snd_soc_dai_driver
681                  */
682                 drv[i].name     = rdai[i].name;
683                 drv[i].ops      = &rsnd_soc_dai_ops;
684                 if (pmod) {
685                         drv[i].playback.rates           = RSND_RATES;
686                         drv[i].playback.formats         = RSND_FMTS;
687                         drv[i].playback.channels_min    = 2;
688                         drv[i].playback.channels_max    = 2;
689                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
690                 }
691                 if (cmod) {
692                         drv[i].capture.rates            = RSND_RATES;
693                         drv[i].capture.formats          = RSND_FMTS;
694                         drv[i].capture.channels_min     = 2;
695                         drv[i].capture.channels_max     = 2;
696                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
697                 }
698
699                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
700                         pmod ? "play"    : " -- ",
701                         cmod ? "capture" : "  --   ");
702         }
703
704         return 0;
705 }
706
707 static void rsnd_dai_remove(struct platform_device *pdev,
708                           struct rsnd_priv *priv)
709 {
710         struct rsnd_dai *rdai;
711         int i;
712
713         for (i = 0; i < rsnd_rdai_nr(priv); i++) {
714                 rdai = rsnd_dai_get(priv, i);
715                 rsnd_path_exit(priv, rdai, &rdai->playback);
716                 rsnd_path_exit(priv, rdai, &rdai->capture);
717         }
718 }
719
720 /*
721  *              pcm ops
722  */
723 static struct snd_pcm_hardware rsnd_pcm_hardware = {
724         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
725                         SNDRV_PCM_INFO_MMAP             |
726                         SNDRV_PCM_INFO_MMAP_VALID       |
727                         SNDRV_PCM_INFO_PAUSE,
728         .buffer_bytes_max       = 64 * 1024,
729         .period_bytes_min       = 32,
730         .period_bytes_max       = 8192,
731         .periods_min            = 1,
732         .periods_max            = 32,
733         .fifo_size              = 256,
734 };
735
736 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
737 {
738         struct snd_pcm_runtime *runtime = substream->runtime;
739         int ret = 0;
740
741         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
742
743         ret = snd_pcm_hw_constraint_integer(runtime,
744                                             SNDRV_PCM_HW_PARAM_PERIODS);
745
746         return ret;
747 }
748
749 static int rsnd_hw_params(struct snd_pcm_substream *substream,
750                          struct snd_pcm_hw_params *hw_params)
751 {
752         return snd_pcm_lib_malloc_pages(substream,
753                                         params_buffer_bytes(hw_params));
754 }
755
756 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
757 {
758         struct snd_pcm_runtime *runtime = substream->runtime;
759         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
760         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
761         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
762
763         return bytes_to_frames(runtime, io->byte_pos);
764 }
765
766 static struct snd_pcm_ops rsnd_pcm_ops = {
767         .open           = rsnd_pcm_open,
768         .ioctl          = snd_pcm_lib_ioctl,
769         .hw_params      = rsnd_hw_params,
770         .hw_free        = snd_pcm_lib_free_pages,
771         .pointer        = rsnd_pointer,
772 };
773
774 /*
775  *              snd_soc_platform
776  */
777
778 #define PREALLOC_BUFFER         (32 * 1024)
779 #define PREALLOC_BUFFER_MAX     (32 * 1024)
780
781 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
782 {
783         return snd_pcm_lib_preallocate_pages_for_all(
784                 rtd->pcm,
785                 SNDRV_DMA_TYPE_DEV,
786                 rtd->card->snd_card->dev,
787                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
788 }
789
790 static void rsnd_pcm_free(struct snd_pcm *pcm)
791 {
792         snd_pcm_lib_preallocate_free_for_all(pcm);
793 }
794
795 static struct snd_soc_platform_driver rsnd_soc_platform = {
796         .ops            = &rsnd_pcm_ops,
797         .pcm_new        = rsnd_pcm_new,
798         .pcm_free       = rsnd_pcm_free,
799 };
800
801 static const struct snd_soc_component_driver rsnd_soc_component = {
802         .name           = "rsnd",
803 };
804
805 /*
806  *      rsnd probe
807  */
808 static int rsnd_probe(struct platform_device *pdev)
809 {
810         struct rcar_snd_info *info;
811         struct rsnd_priv *priv;
812         struct device *dev = &pdev->dev;
813         int ret;
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         ret = rsnd_gen_probe(pdev, priv);
838         if (ret)
839                 return ret;
840
841         ret = rsnd_ssi_probe(pdev, priv);
842         if (ret)
843                 return ret;
844
845         ret = rsnd_scu_probe(pdev, priv);
846         if (ret)
847                 return ret;
848
849         ret = rsnd_adg_probe(pdev, priv);
850         if (ret)
851                 return ret;
852
853         ret = rsnd_dai_probe(pdev, priv);
854         if (ret)
855                 return ret;
856
857         /*
858          *      asoc register
859          */
860         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
861         if (ret < 0) {
862                 dev_err(dev, "cannot snd soc register\n");
863                 return ret;
864         }
865
866         ret = snd_soc_register_component(dev, &rsnd_soc_component,
867                                          priv->daidrv, rsnd_rdai_nr(priv));
868         if (ret < 0) {
869                 dev_err(dev, "cannot snd dai register\n");
870                 goto exit_snd_soc;
871         }
872
873         dev_set_drvdata(dev, priv);
874
875         pm_runtime_enable(dev);
876
877         dev_info(dev, "probed\n");
878         return ret;
879
880 exit_snd_soc:
881         snd_soc_unregister_platform(dev);
882
883         return ret;
884 }
885
886 static int rsnd_remove(struct platform_device *pdev)
887 {
888         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
889
890         pm_runtime_disable(&pdev->dev);
891
892         /*
893          *      remove each module
894          */
895         rsnd_ssi_remove(pdev, priv);
896         rsnd_adg_remove(pdev, priv);
897         rsnd_scu_remove(pdev, priv);
898         rsnd_dai_remove(pdev, priv);
899         rsnd_gen_remove(pdev, priv);
900
901         return 0;
902 }
903
904 static struct platform_driver rsnd_driver = {
905         .driver = {
906                 .name   = "rcar_sound",
907         },
908         .probe          = rsnd_probe,
909         .remove         = rsnd_remove,
910 };
911 module_platform_driver(rsnd_driver);
912
913 MODULE_LICENSE("GPL");
914 MODULE_DESCRIPTION("Renesas R-Car audio driver");
915 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
916 MODULE_ALIAS("platform:rcar-pcm-audio");