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