Merge remote-tracking branches 'asoc/topic/link-param', 'asoc/topic/max98090', 'asoc...
[linux-drm-fsl-dcu.git] / sound / soc / sh / rcar / core.c
1 /*
2  * Renesas R-Car SRU/SCU/SSIU/SSI support
3  *
4  * Copyright (C) 2013 Renesas Solutions Corp.
5  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * Based on fsi.c
8  * Kuninori Morimoto <morimoto.kuninori@renesas.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 /*
16  * Renesas R-Car sound device structure
17  *
18  * Gen1
19  *
20  * SRU          : Sound Routing Unit
21  *  - SRC       : Sampling Rate Converter
22  *  - CMD
23  *    - CTU     : Channel Count Conversion Unit
24  *    - MIX     : Mixer
25  *    - DVC     : Digital Volume and Mute Function
26  *  - SSI       : Serial Sound Interface
27  *
28  * Gen2
29  *
30  * SCU          : Sampling Rate Converter Unit
31  *  - SRC       : Sampling Rate Converter
32  *  - CMD
33  *   - CTU      : Channel Count Conversion Unit
34  *   - MIX      : Mixer
35  *   - DVC      : Digital Volume and Mute Function
36  * SSIU         : Serial Sound Interface Unit
37  *  - SSI       : Serial Sound Interface
38  */
39
40 /*
41  *      driver data Image
42  *
43  * rsnd_priv
44  *   |
45  *   | ** this depends on Gen1/Gen2
46  *   |
47  *   +- gen
48  *   |
49  *   | ** these depend on data path
50  *   | ** gen and platform data control it
51  *   |
52  *   +- rdai[0]
53  *   |   |               sru     ssiu      ssi
54  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
55  *   |   |
56  *   |   |               sru     ssiu      ssi
57  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
58  *   |
59  *   +- rdai[1]
60  *   |   |               sru     ssiu      ssi
61  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
62  *   |   |
63  *   |   |               sru     ssiu      ssi
64  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
65  *   ...
66  *   |
67  *   | ** these control ssi
68  *   |
69  *   +- ssi
70  *   |  |
71  *   |  +- ssi[0]
72  *   |  +- ssi[1]
73  *   |  +- ssi[2]
74  *   |  ...
75  *   |
76  *   | ** these control src
77  *   |
78  *   +- src
79  *      |
80  *      +- src[0]
81  *      +- src[1]
82  *      +- src[2]
83  *      ...
84  *
85  *
86  * for_each_rsnd_dai(xx, priv, xx)
87  *  rdai[0] => rdai[1] => rdai[2] => ...
88  *
89  * for_each_rsnd_mod(xx, rdai, xx)
90  *  [mod] => [mod] => [mod] => ...
91  *
92  * rsnd_dai_call(xxx, fn )
93  *  [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
94  *
95  */
96 #include <linux/pm_runtime.h>
97 #include <linux/shdma-base.h>
98 #include "rsnd.h"
99
100 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
101 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102
103 static struct rsnd_of_data rsnd_of_data_gen1 = {
104         .flags = RSND_GEN1,
105 };
106
107 static struct rsnd_of_data rsnd_of_data_gen2 = {
108         .flags = RSND_GEN2,
109 };
110
111 static struct of_device_id rsnd_of_match[] = {
112         { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113         { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
114         {},
115 };
116 MODULE_DEVICE_TABLE(of, rsnd_of_match);
117
118 /*
119  *      rsnd_platform functions
120  */
121 #define rsnd_platform_call(priv, dai, func, param...)   \
122         (!(priv->info->func) ? 0 :              \
123          priv->info->func(param))
124
125 #define rsnd_is_enable_path(io, name) \
126         ((io)->info ? (io)->info->name : NULL)
127 #define rsnd_info_id(priv, io, name) \
128         ((io)->info->name - priv->info->name##_info)
129
130 /*
131  *      rsnd_mod functions
132  */
133 char *rsnd_mod_name(struct rsnd_mod *mod)
134 {
135         if (!mod || !mod->ops)
136                 return "unknown";
137
138         return mod->ops->name;
139 }
140
141 char *rsnd_mod_dma_name(struct rsnd_mod *mod)
142 {
143         if (!mod || !mod->ops)
144                 return "unknown";
145
146         if (!mod->ops->dma_name)
147                 return mod->ops->name;
148
149         return mod->ops->dma_name(mod);
150 }
151
152 int rsnd_mod_init(struct rsnd_mod *mod,
153                    struct rsnd_mod_ops *ops,
154                    struct clk *clk,
155                    enum rsnd_mod_type type,
156                    int id)
157 {
158         int ret = clk_prepare(clk);
159
160         if (ret)
161                 return ret;
162
163         mod->id         = id;
164         mod->ops        = ops;
165         mod->type       = type;
166         mod->clk        = clk;
167
168         return ret;
169 }
170
171 void rsnd_mod_quit(struct rsnd_mod *mod)
172 {
173         if (mod->clk)
174                 clk_unprepare(mod->clk);
175 }
176
177 /*
178  *      rsnd_dma functions
179  */
180 void rsnd_dma_stop(struct rsnd_dma *dma)
181 {
182         dmaengine_terminate_all(dma->chan);
183 }
184
185 static void rsnd_dma_complete(void *data)
186 {
187         struct rsnd_dma *dma = (struct rsnd_dma *)data;
188         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
189         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
190
191         /*
192          * Renesas sound Gen1 needs 1 DMAC,
193          * Gen2 needs 2 DMAC.
194          * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
195          * But, Audio-DMAC-peri-peri doesn't have interrupt,
196          * and this driver is assuming that here.
197          *
198          * If Audio-DMAC-peri-peri has interrpt,
199          * rsnd_dai_pointer_update() will be called twice,
200          * ant it will breaks io->byte_pos
201          */
202
203         rsnd_dai_pointer_update(io, io->byte_per_period);
204 }
205
206 void rsnd_dma_start(struct rsnd_dma *dma)
207 {
208         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
209         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
210         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
211         struct snd_pcm_substream *substream = io->substream;
212         struct device *dev = rsnd_priv_to_dev(priv);
213         struct dma_async_tx_descriptor *desc;
214
215         desc = dmaengine_prep_dma_cyclic(dma->chan,
216                                          (dma->addr) ? dma->addr :
217                                          substream->runtime->dma_addr,
218                                          snd_pcm_lib_buffer_bytes(substream),
219                                          snd_pcm_lib_period_bytes(substream),
220                                          dma->dir,
221                                          DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
222
223         if (!desc) {
224                 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
225                 return;
226         }
227
228         desc->callback          = rsnd_dma_complete;
229         desc->callback_param    = dma;
230
231         if (dmaengine_submit(desc) < 0) {
232                 dev_err(dev, "dmaengine_submit() fail\n");
233                 return;
234         }
235
236         dma_async_issue_pending(dma->chan);
237 }
238
239 int rsnd_dma_available(struct rsnd_dma *dma)
240 {
241         return !!dma->chan;
242 }
243
244 #define DMA_NAME_SIZE 16
245 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
246 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
247 {
248         if (mod)
249                 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
250                          rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
251         else
252                 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
253
254 }
255
256 static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
257                              struct rsnd_mod *mod_to,
258                              char *dma_name)
259 {
260         int index = 0;
261
262         index = _rsnd_dma_of_name(dma_name + index, mod_from);
263         *(dma_name + index++) = '_';
264         index = _rsnd_dma_of_name(dma_name + index, mod_to);
265 }
266
267 static void rsnd_dma_of_path(struct rsnd_dma *dma,
268                              int is_play,
269                              struct rsnd_mod **mod_from,
270                              struct rsnd_mod **mod_to)
271 {
272         struct rsnd_mod *this = rsnd_dma_to_mod(dma);
273         struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
274         struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
275         struct rsnd_mod *src = rsnd_io_to_mod_src(io);
276         struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
277         struct rsnd_mod *mod[MOD_MAX];
278         int i, index;
279
280
281         for (i = 0; i < MOD_MAX; i++)
282                 mod[i] = NULL;
283
284         /*
285          * in play case...
286          *
287          * src -> dst
288          *
289          * mem -> SSI
290          * mem -> SRC -> SSI
291          * mem -> SRC -> DVC -> SSI
292          */
293         mod[0] = NULL; /* for "mem" */
294         index = 1;
295         for (i = 1; i < MOD_MAX; i++) {
296                 if (!src) {
297                         mod[i] = ssi;
298                 } else if (!dvc) {
299                         mod[i] = src;
300                         src = NULL;
301                 } else {
302                         if ((!is_play) && (this == src))
303                                 this = dvc;
304
305                         mod[i] = (is_play) ? src : dvc;
306                         i++;
307                         mod[i] = (is_play) ? dvc : src;
308                         src = NULL;
309                         dvc = NULL;
310                 }
311
312                 if (mod[i] == this)
313                         index = i;
314
315                 if (mod[i] == ssi)
316                         break;
317         }
318
319         if (is_play) {
320                 *mod_from = mod[index - 1];
321                 *mod_to   = mod[index];
322         } else {
323                 *mod_from = mod[index];
324                 *mod_to   = mod[index - 1];
325         }
326 }
327
328 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
329                   int is_play, int id)
330 {
331         struct device *dev = rsnd_priv_to_dev(priv);
332         struct dma_slave_config cfg;
333         struct rsnd_mod *mod_from;
334         struct rsnd_mod *mod_to;
335         char dma_name[DMA_NAME_SIZE];
336         dma_cap_mask_t mask;
337         int ret;
338
339         if (dma->chan) {
340                 dev_err(dev, "it already has dma channel\n");
341                 return -EIO;
342         }
343
344         dma_cap_zero(mask);
345         dma_cap_set(DMA_SLAVE, mask);
346
347         rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
348         rsnd_dma_of_name(mod_from, mod_to, dma_name);
349
350         cfg.slave_id    = id;
351         cfg.direction   = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
352         cfg.src_addr    = rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
353         cfg.dst_addr    = rsnd_gen_dma_addr(priv, mod_to,   is_play, 0);
354         cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
355         cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
356
357         dev_dbg(dev, "dma : %s %pad -> %pad\n",
358                 dma_name, &cfg.src_addr, &cfg.dst_addr);
359
360         dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
361                                                      (void *)id, dev,
362                                                      dma_name);
363         if (!dma->chan) {
364                 dev_err(dev, "can't get dma channel\n");
365                 goto rsnd_dma_channel_err;
366         }
367
368         ret = dmaengine_slave_config(dma->chan, &cfg);
369         if (ret < 0)
370                 goto rsnd_dma_init_err;
371
372         dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
373         dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
374
375         return 0;
376
377 rsnd_dma_init_err:
378         rsnd_dma_quit(priv, dma);
379 rsnd_dma_channel_err:
380
381         /*
382          * DMA failed. try to PIO mode
383          * see
384          *      rsnd_ssi_fallback()
385          *      rsnd_rdai_continuance_probe()
386          */
387         return -EAGAIN;
388 }
389
390 void  rsnd_dma_quit(struct rsnd_priv *priv,
391                     struct rsnd_dma *dma)
392 {
393         if (dma->chan)
394                 dma_release_channel(dma->chan);
395
396         dma->chan = NULL;
397 }
398
399 /*
400  *      settting function
401  */
402 u32 rsnd_get_adinr(struct rsnd_mod *mod)
403 {
404         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
405         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
406         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
407         struct device *dev = rsnd_priv_to_dev(priv);
408         u32 adinr = runtime->channels;
409
410         switch (runtime->sample_bits) {
411         case 16:
412                 adinr |= (8 << 16);
413                 break;
414         case 32:
415                 adinr |= (0 << 16);
416                 break;
417         default:
418                 dev_warn(dev, "not supported sample bits\n");
419                 return 0;
420         }
421
422         return adinr;
423 }
424
425 /*
426  *      rsnd_dai functions
427  */
428 #define __rsnd_mod_call(mod, func, param...)                    \
429 ({                                                              \
430         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
431         struct device *dev = rsnd_priv_to_dev(priv);            \
432         u32 mask = 1 << __rsnd_mod_shift_##func;                        \
433         u32 call = __rsnd_mod_call_##func << __rsnd_mod_shift_##func;   \
434         int ret = 0;                                                    \
435         if ((mod->status & mask) == call) {                             \
436                 dev_dbg(dev, "%s[%d] %s\n",                             \
437                         rsnd_mod_name(mod), rsnd_mod_id(mod), #func);   \
438                 ret = (mod)->ops->func(mod, param);                     \
439                 mod->status = (mod->status & ~mask) | (~call & mask);   \
440         }                                                               \
441         ret;                                                            \
442 })
443
444 #define rsnd_mod_call(mod, func, param...)      \
445         (!(mod) ? -ENODEV :                     \
446          !((mod)->ops->func) ? 0 :              \
447          __rsnd_mod_call(mod, func, param))
448
449 #define rsnd_dai_call(fn, io, param...)                         \
450 ({                                                              \
451         struct rsnd_mod *mod;                                   \
452         int ret = 0, i;                                         \
453         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
454                 mod = (io)->mod[i];                             \
455                 if (!mod)                                       \
456                         continue;                               \
457                 ret = rsnd_mod_call(mod, fn, param);            \
458                 if (ret < 0)                                    \
459                         break;                                  \
460         }                                                       \
461         ret;                                                    \
462 })
463
464 static int rsnd_dai_connect(struct rsnd_mod *mod,
465                             struct rsnd_dai_stream *io)
466 {
467         if (!mod)
468                 return -EIO;
469
470         if (io->mod[mod->type]) {
471                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
472                 struct device *dev = rsnd_priv_to_dev(priv);
473
474                 dev_err(dev, "%s%d is not empty\n",
475                         rsnd_mod_name(mod),
476                         rsnd_mod_id(mod));
477                 return -EIO;
478         }
479
480         io->mod[mod->type] = mod;
481         mod->io = io;
482
483         return 0;
484 }
485
486 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
487                                 struct rsnd_dai_stream *io)
488 {
489         mod->io = NULL;
490         io->mod[mod->type] = NULL;
491 }
492
493 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
494 {
495         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
496                 return NULL;
497
498         return priv->rdai + id;
499 }
500
501 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
502 {
503         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
504
505         return rsnd_rdai_get(priv, dai->id);
506 }
507
508 /*
509  *      rsnd_soc_dai functions
510  */
511 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
512 {
513         struct snd_pcm_substream *substream = io->substream;
514         struct snd_pcm_runtime *runtime = substream->runtime;
515         int pos = io->byte_pos + additional;
516
517         pos %= (runtime->periods * io->byte_per_period);
518
519         return pos;
520 }
521
522 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
523 {
524         io->byte_pos += byte;
525
526         if (io->byte_pos >= io->next_period_byte) {
527                 struct snd_pcm_substream *substream = io->substream;
528                 struct snd_pcm_runtime *runtime = substream->runtime;
529
530                 io->period_pos++;
531                 io->next_period_byte += io->byte_per_period;
532
533                 if (io->period_pos >= runtime->periods) {
534                         io->byte_pos = 0;
535                         io->period_pos = 0;
536                         io->next_period_byte = io->byte_per_period;
537                 }
538
539                 snd_pcm_period_elapsed(substream);
540         }
541 }
542
543 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
544                                 struct snd_pcm_substream *substream)
545 {
546         struct snd_pcm_runtime *runtime = substream->runtime;
547
548         io->substream           = substream;
549         io->byte_pos            = 0;
550         io->period_pos          = 0;
551         io->byte_per_period     = runtime->period_size *
552                                   runtime->channels *
553                                   samples_to_bytes(runtime, 1);
554         io->next_period_byte    = io->byte_per_period;
555
556         return 0;
557 }
558
559 static
560 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
561 {
562         struct snd_soc_pcm_runtime *rtd = substream->private_data;
563
564         return  rtd->cpu_dai;
565 }
566
567 static
568 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
569                                         struct snd_pcm_substream *substream)
570 {
571         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
572                 return &rdai->playback;
573         else
574                 return &rdai->capture;
575 }
576
577 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
578                             struct snd_soc_dai *dai)
579 {
580         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
581         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
582         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
583         int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
584         int ret;
585         unsigned long flags;
586
587         rsnd_lock(priv, flags);
588
589         switch (cmd) {
590         case SNDRV_PCM_TRIGGER_START:
591                 ret = rsnd_dai_stream_init(io, substream);
592                 if (ret < 0)
593                         goto dai_trigger_end;
594
595                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
596                 if (ret < 0)
597                         goto dai_trigger_end;
598
599                 ret = rsnd_dai_call(init, io, priv);
600                 if (ret < 0)
601                         goto dai_trigger_end;
602
603                 ret = rsnd_dai_call(start, io, priv);
604                 if (ret < 0)
605                         goto dai_trigger_end;
606                 break;
607         case SNDRV_PCM_TRIGGER_STOP:
608                 ret = rsnd_dai_call(stop, io, priv);
609                 if (ret < 0)
610                         goto dai_trigger_end;
611
612                 ret = rsnd_dai_call(quit, io, priv);
613                 if (ret < 0)
614                         goto dai_trigger_end;
615
616                 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
617                 if (ret < 0)
618                         goto dai_trigger_end;
619                 break;
620         default:
621                 ret = -EINVAL;
622         }
623
624 dai_trigger_end:
625         rsnd_unlock(priv, flags);
626
627         return ret;
628 }
629
630 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
631 {
632         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
633
634         /* set master/slave audio interface */
635         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
636         case SND_SOC_DAIFMT_CBM_CFM:
637                 rdai->clk_master = 0;
638                 break;
639         case SND_SOC_DAIFMT_CBS_CFS:
640                 rdai->clk_master = 1; /* codec is slave, cpu is master */
641                 break;
642         default:
643                 return -EINVAL;
644         }
645
646         /* set format */
647         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
648         case SND_SOC_DAIFMT_I2S:
649                 rdai->sys_delay = 0;
650                 rdai->data_alignment = 0;
651                 rdai->frm_clk_inv = 0;
652                 break;
653         case SND_SOC_DAIFMT_LEFT_J:
654                 rdai->sys_delay = 1;
655                 rdai->data_alignment = 0;
656                 rdai->frm_clk_inv = 1;
657                 break;
658         case SND_SOC_DAIFMT_RIGHT_J:
659                 rdai->sys_delay = 1;
660                 rdai->data_alignment = 1;
661                 rdai->frm_clk_inv = 1;
662                 break;
663         }
664
665         /* set clock inversion */
666         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
667         case SND_SOC_DAIFMT_NB_IF:
668                 rdai->bit_clk_inv =  rdai->bit_clk_inv;
669                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
670                 break;
671         case SND_SOC_DAIFMT_IB_NF:
672                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
673                 rdai->frm_clk_inv =  rdai->frm_clk_inv;
674                 break;
675         case SND_SOC_DAIFMT_IB_IF:
676                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
677                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
678                 break;
679         case SND_SOC_DAIFMT_NB_NF:
680         default:
681                 break;
682         }
683
684         return 0;
685 }
686
687 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
688         .trigger        = rsnd_soc_dai_trigger,
689         .set_fmt        = rsnd_soc_dai_set_fmt,
690 };
691
692 #define rsnd_path_parse(priv, io, type)                         \
693 ({                                                              \
694         struct rsnd_mod *mod;                                   \
695         int ret = 0;                                            \
696         int id = -1;                                            \
697                                                                 \
698         if (rsnd_is_enable_path(io, type)) {                    \
699                 id = rsnd_info_id(priv, io, type);              \
700                 if (id >= 0) {                                  \
701                         mod = rsnd_##type##_mod_get(priv, id);  \
702                         ret = rsnd_dai_connect(mod, io);        \
703                 }                                               \
704         }                                                       \
705         ret;                                                    \
706 })
707
708 #define rsnd_path_break(priv, io, type)                         \
709 {                                                               \
710         struct rsnd_mod *mod;                                   \
711         int id = -1;                                            \
712                                                                 \
713         if (rsnd_is_enable_path(io, type)) {                    \
714                 id = rsnd_info_id(priv, io, type);              \
715                 if (id >= 0) {                                  \
716                         mod = rsnd_##type##_mod_get(priv, id);  \
717                         rsnd_dai_disconnect(mod, io);           \
718                 }                                               \
719         }                                                       \
720 }
721
722 static int rsnd_path_init(struct rsnd_priv *priv,
723                           struct rsnd_dai *rdai,
724                           struct rsnd_dai_stream *io)
725 {
726         int ret;
727
728         /*
729          * Gen1 is created by SRU/SSI, and this SRU is base module of
730          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
731          *
732          * Easy image is..
733          *      Gen1 SRU = Gen2 SCU + SSIU + etc
734          *
735          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
736          * using fixed path.
737          */
738
739         /* SRC */
740         ret = rsnd_path_parse(priv, io, src);
741         if (ret < 0)
742                 return ret;
743
744         /* SSI */
745         ret = rsnd_path_parse(priv, io, ssi);
746         if (ret < 0)
747                 return ret;
748
749         /* DVC */
750         ret = rsnd_path_parse(priv, io, dvc);
751         if (ret < 0)
752                 return ret;
753
754         return ret;
755 }
756
757 static void rsnd_of_parse_dai(struct platform_device *pdev,
758                               const struct rsnd_of_data *of_data,
759                               struct rsnd_priv *priv)
760 {
761         struct device_node *dai_node,   *dai_np;
762         struct device_node *ssi_node,   *ssi_np;
763         struct device_node *src_node,   *src_np;
764         struct device_node *dvc_node,   *dvc_np;
765         struct device_node *playback, *capture;
766         struct rsnd_dai_platform_info *dai_info;
767         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
768         struct device *dev = &pdev->dev;
769         int nr, i;
770         int dai_i, ssi_i, src_i, dvc_i;
771
772         if (!of_data)
773                 return;
774
775         dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
776         if (!dai_node)
777                 return;
778
779         nr = of_get_child_count(dai_node);
780         if (!nr)
781                 return;
782
783         dai_info = devm_kzalloc(dev,
784                                 sizeof(struct rsnd_dai_platform_info) * nr,
785                                 GFP_KERNEL);
786         if (!dai_info) {
787                 dev_err(dev, "dai info allocation error\n");
788                 return;
789         }
790
791         info->dai_info_nr       = nr;
792         info->dai_info          = dai_info;
793
794         ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
795         src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
796         dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
797
798 #define mod_parse(name)                                                 \
799 if (name##_node) {                                                      \
800         struct rsnd_##name##_platform_info *name##_info;                \
801                                                                         \
802         name##_i = 0;                                                   \
803         for_each_child_of_node(name##_node, name##_np) {                \
804                 name##_info = info->name##_info + name##_i;             \
805                                                                         \
806                 if (name##_np == playback)                              \
807                         dai_info->playback.name = name##_info;          \
808                 if (name##_np == capture)                               \
809                         dai_info->capture.name = name##_info;           \
810                                                                         \
811                 name##_i++;                                             \
812         }                                                               \
813 }
814
815         /*
816          * parse all dai
817          */
818         dai_i = 0;
819         for_each_child_of_node(dai_node, dai_np) {
820                 dai_info = info->dai_info + dai_i;
821
822                 for (i = 0;; i++) {
823
824                         playback = of_parse_phandle(dai_np, "playback", i);
825                         capture  = of_parse_phandle(dai_np, "capture", i);
826
827                         if (!playback && !capture)
828                                 break;
829
830                         mod_parse(ssi);
831                         mod_parse(src);
832                         mod_parse(dvc);
833
834                         of_node_put(playback);
835                         of_node_put(capture);
836                 }
837
838                 dai_i++;
839         }
840 }
841
842 static int rsnd_dai_probe(struct platform_device *pdev,
843                           const struct rsnd_of_data *of_data,
844                           struct rsnd_priv *priv)
845 {
846         struct snd_soc_dai_driver *drv;
847         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
848         struct rsnd_dai *rdai;
849         struct rsnd_ssi_platform_info *pmod, *cmod;
850         struct device *dev = rsnd_priv_to_dev(priv);
851         int dai_nr;
852         int i;
853
854         rsnd_of_parse_dai(pdev, of_data, priv);
855
856         dai_nr = info->dai_info_nr;
857         if (!dai_nr) {
858                 dev_err(dev, "no dai\n");
859                 return -EIO;
860         }
861
862         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
863         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
864         if (!drv || !rdai) {
865                 dev_err(dev, "dai allocate failed\n");
866                 return -ENOMEM;
867         }
868
869         priv->rdai_nr   = dai_nr;
870         priv->daidrv    = drv;
871         priv->rdai      = rdai;
872
873         for (i = 0; i < dai_nr; i++) {
874
875                 pmod = info->dai_info[i].playback.ssi;
876                 cmod = info->dai_info[i].capture.ssi;
877
878                 /*
879                  *      init rsnd_dai
880                  */
881                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
882                 rdai[i].priv = priv;
883
884                 /*
885                  *      init snd_soc_dai_driver
886                  */
887                 drv[i].name     = rdai[i].name;
888                 drv[i].ops      = &rsnd_soc_dai_ops;
889                 if (pmod) {
890                         drv[i].playback.rates           = RSND_RATES;
891                         drv[i].playback.formats         = RSND_FMTS;
892                         drv[i].playback.channels_min    = 2;
893                         drv[i].playback.channels_max    = 2;
894
895                         rdai[i].playback.info = &info->dai_info[i].playback;
896                         rdai[i].playback.rdai = rdai + i;
897                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
898                 }
899                 if (cmod) {
900                         drv[i].capture.rates            = RSND_RATES;
901                         drv[i].capture.formats          = RSND_FMTS;
902                         drv[i].capture.channels_min     = 2;
903                         drv[i].capture.channels_max     = 2;
904
905                         rdai[i].capture.info = &info->dai_info[i].capture;
906                         rdai[i].capture.rdai = rdai + i;
907                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
908                 }
909
910                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
911                         pmod ? "play"    : " -- ",
912                         cmod ? "capture" : "  --   ");
913         }
914
915         return 0;
916 }
917
918 /*
919  *              pcm ops
920  */
921 static struct snd_pcm_hardware rsnd_pcm_hardware = {
922         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
923                         SNDRV_PCM_INFO_MMAP             |
924                         SNDRV_PCM_INFO_MMAP_VALID,
925         .buffer_bytes_max       = 64 * 1024,
926         .period_bytes_min       = 32,
927         .period_bytes_max       = 8192,
928         .periods_min            = 1,
929         .periods_max            = 32,
930         .fifo_size              = 256,
931 };
932
933 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
934 {
935         struct snd_pcm_runtime *runtime = substream->runtime;
936         int ret = 0;
937
938         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
939
940         ret = snd_pcm_hw_constraint_integer(runtime,
941                                             SNDRV_PCM_HW_PARAM_PERIODS);
942
943         return ret;
944 }
945
946 static int rsnd_hw_params(struct snd_pcm_substream *substream,
947                          struct snd_pcm_hw_params *hw_params)
948 {
949         return snd_pcm_lib_malloc_pages(substream,
950                                         params_buffer_bytes(hw_params));
951 }
952
953 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
954 {
955         struct snd_pcm_runtime *runtime = substream->runtime;
956         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
957         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
958         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
959
960         return bytes_to_frames(runtime, io->byte_pos);
961 }
962
963 static struct snd_pcm_ops rsnd_pcm_ops = {
964         .open           = rsnd_pcm_open,
965         .ioctl          = snd_pcm_lib_ioctl,
966         .hw_params      = rsnd_hw_params,
967         .hw_free        = snd_pcm_lib_free_pages,
968         .pointer        = rsnd_pointer,
969 };
970
971 /*
972  *              snd_kcontrol
973  */
974 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
975 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
976                            struct snd_ctl_elem_info *uinfo)
977 {
978         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
979
980         if (cfg->texts) {
981                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
982                 uinfo->count = cfg->size;
983                 uinfo->value.enumerated.items = cfg->max;
984                 if (uinfo->value.enumerated.item >= cfg->max)
985                         uinfo->value.enumerated.item = cfg->max - 1;
986                 strlcpy(uinfo->value.enumerated.name,
987                         cfg->texts[uinfo->value.enumerated.item],
988                         sizeof(uinfo->value.enumerated.name));
989         } else {
990                 uinfo->count = cfg->size;
991                 uinfo->value.integer.min = 0;
992                 uinfo->value.integer.max = cfg->max;
993                 uinfo->type = (cfg->max == 1) ?
994                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
995                         SNDRV_CTL_ELEM_TYPE_INTEGER;
996         }
997
998         return 0;
999 }
1000
1001 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1002                           struct snd_ctl_elem_value *uc)
1003 {
1004         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1005         int i;
1006
1007         for (i = 0; i < cfg->size; i++)
1008                 if (cfg->texts)
1009                         uc->value.enumerated.item[i] = cfg->val[i];
1010                 else
1011                         uc->value.integer.value[i] = cfg->val[i];
1012
1013         return 0;
1014 }
1015
1016 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1017                           struct snd_ctl_elem_value *uc)
1018 {
1019         struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1020         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1021         int i, change = 0;
1022
1023         for (i = 0; i < cfg->size; i++) {
1024                 if (cfg->texts) {
1025                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1026                         cfg->val[i] = uc->value.enumerated.item[i];
1027                 } else {
1028                         change |= (uc->value.integer.value[i] != cfg->val[i]);
1029                         cfg->val[i] = uc->value.integer.value[i];
1030                 }
1031         }
1032
1033         if (change)
1034                 cfg->update(mod);
1035
1036         return change;
1037 }
1038
1039 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
1040                             struct snd_soc_pcm_runtime *rtd,
1041                             const unsigned char *name,
1042                             struct rsnd_kctrl_cfg *cfg,
1043                             void (*update)(struct rsnd_mod *mod))
1044 {
1045         struct snd_card *card = rtd->card->snd_card;
1046         struct snd_kcontrol *kctrl;
1047         struct snd_kcontrol_new knew = {
1048                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
1049                 .name           = name,
1050                 .info           = rsnd_kctrl_info,
1051                 .get            = rsnd_kctrl_get,
1052                 .put            = rsnd_kctrl_put,
1053                 .private_value  = (unsigned long)cfg,
1054         };
1055         int ret;
1056
1057         kctrl = snd_ctl_new1(&knew, mod);
1058         if (!kctrl)
1059                 return -ENOMEM;
1060
1061         ret = snd_ctl_add(card, kctrl);
1062         if (ret < 0) {
1063                 snd_ctl_free_one(kctrl);
1064                 return ret;
1065         }
1066
1067         cfg->update = update;
1068         cfg->card = card;
1069         cfg->kctrl = kctrl;
1070
1071         return 0;
1072 }
1073
1074 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1075 {
1076         snd_ctl_remove(cfg->card, cfg->kctrl);
1077 }
1078
1079 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1080                      struct snd_soc_pcm_runtime *rtd,
1081                      const unsigned char *name,
1082                      void (*update)(struct rsnd_mod *mod),
1083                      struct rsnd_kctrl_cfg_m *_cfg,
1084                      u32 max)
1085 {
1086         _cfg->cfg.max   = max;
1087         _cfg->cfg.size  = RSND_DVC_CHANNELS;
1088         _cfg->cfg.val   = _cfg->val;
1089         return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1090 }
1091
1092 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1093                      struct snd_soc_pcm_runtime *rtd,
1094                      const unsigned char *name,
1095                      void (*update)(struct rsnd_mod *mod),
1096                      struct rsnd_kctrl_cfg_s *_cfg,
1097                      u32 max)
1098 {
1099         _cfg->cfg.max   = max;
1100         _cfg->cfg.size  = 1;
1101         _cfg->cfg.val   = &_cfg->val;
1102         return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1103 }
1104
1105 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1106                      struct snd_soc_pcm_runtime *rtd,
1107                      const unsigned char *name,
1108                      struct rsnd_kctrl_cfg_s *_cfg,
1109                      void (*update)(struct rsnd_mod *mod),
1110                      const char * const *texts,
1111                      u32 max)
1112 {
1113         _cfg->cfg.max   = max;
1114         _cfg->cfg.size  = 1;
1115         _cfg->cfg.val   = &_cfg->val;
1116         _cfg->cfg.texts = texts;
1117         return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1118 }
1119
1120 /*
1121  *              snd_soc_platform
1122  */
1123
1124 #define PREALLOC_BUFFER         (32 * 1024)
1125 #define PREALLOC_BUFFER_MAX     (32 * 1024)
1126
1127 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1128 {
1129         struct snd_soc_dai *dai = rtd->cpu_dai;
1130         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1131         int ret;
1132
1133         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1134         if (ret)
1135                 return ret;
1136
1137         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1138         if (ret)
1139                 return ret;
1140
1141         return snd_pcm_lib_preallocate_pages_for_all(
1142                 rtd->pcm,
1143                 SNDRV_DMA_TYPE_DEV,
1144                 rtd->card->snd_card->dev,
1145                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1146 }
1147
1148 static struct snd_soc_platform_driver rsnd_soc_platform = {
1149         .ops            = &rsnd_pcm_ops,
1150         .pcm_new        = rsnd_pcm_new,
1151 };
1152
1153 static const struct snd_soc_component_driver rsnd_soc_component = {
1154         .name           = "rsnd",
1155 };
1156
1157 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1158                                        struct rsnd_dai_stream *io)
1159 {
1160         int ret;
1161
1162         ret = rsnd_dai_call(probe, io, priv);
1163         if (ret == -EAGAIN) {
1164                 /*
1165                  * Fallback to PIO mode
1166                  */
1167
1168                 /*
1169                  * call "remove" for SSI/SRC/DVC
1170                  * SSI will be switch to PIO mode if it was DMA mode
1171                  * see
1172                  *      rsnd_dma_init()
1173                  *      rsnd_ssi_fallback()
1174                  */
1175                 rsnd_dai_call(remove, io, priv);
1176
1177                 /*
1178                  * remove SRC/DVC from DAI,
1179                  */
1180                 rsnd_path_break(priv, io, src);
1181                 rsnd_path_break(priv, io, dvc);
1182
1183                 /*
1184                  * fallback
1185                  */
1186                 rsnd_dai_call(fallback, io, priv);
1187
1188                 /*
1189                  * retry to "probe".
1190                  * DAI has SSI which is PIO mode only now.
1191                  */
1192                 ret = rsnd_dai_call(probe, io, priv);
1193         }
1194
1195         return ret;
1196 }
1197
1198 /*
1199  *      rsnd probe
1200  */
1201 static int rsnd_probe(struct platform_device *pdev)
1202 {
1203         struct rcar_snd_info *info;
1204         struct rsnd_priv *priv;
1205         struct device *dev = &pdev->dev;
1206         struct rsnd_dai *rdai;
1207         const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1208         const struct rsnd_of_data *of_data;
1209         int (*probe_func[])(struct platform_device *pdev,
1210                             const struct rsnd_of_data *of_data,
1211                             struct rsnd_priv *priv) = {
1212                 rsnd_gen_probe,
1213                 rsnd_ssi_probe,
1214                 rsnd_src_probe,
1215                 rsnd_dvc_probe,
1216                 rsnd_adg_probe,
1217                 rsnd_dai_probe,
1218         };
1219         int ret, i;
1220
1221         info = NULL;
1222         of_data = NULL;
1223         if (of_id) {
1224                 info = devm_kzalloc(&pdev->dev,
1225                                     sizeof(struct rcar_snd_info), GFP_KERNEL);
1226                 of_data = of_id->data;
1227         } else {
1228                 info = pdev->dev.platform_data;
1229         }
1230
1231         if (!info) {
1232                 dev_err(dev, "driver needs R-Car sound information\n");
1233                 return -ENODEV;
1234         }
1235
1236         /*
1237          *      init priv data
1238          */
1239         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1240         if (!priv) {
1241                 dev_err(dev, "priv allocate failed\n");
1242                 return -ENODEV;
1243         }
1244
1245         priv->pdev      = pdev;
1246         priv->info      = info;
1247         spin_lock_init(&priv->lock);
1248
1249         /*
1250          *      init each module
1251          */
1252         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1253                 ret = probe_func[i](pdev, of_data, priv);
1254                 if (ret)
1255                         return ret;
1256         }
1257
1258         for_each_rsnd_dai(rdai, priv, i) {
1259                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1260                 if (ret)
1261                         goto exit_snd_probe;
1262
1263                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1264                 if (ret)
1265                         goto exit_snd_probe;
1266         }
1267
1268         dev_set_drvdata(dev, priv);
1269
1270         /*
1271          *      asoc register
1272          */
1273         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1274         if (ret < 0) {
1275                 dev_err(dev, "cannot snd soc register\n");
1276                 return ret;
1277         }
1278
1279         ret = snd_soc_register_component(dev, &rsnd_soc_component,
1280                                          priv->daidrv, rsnd_rdai_nr(priv));
1281         if (ret < 0) {
1282                 dev_err(dev, "cannot snd dai register\n");
1283                 goto exit_snd_soc;
1284         }
1285
1286         pm_runtime_enable(dev);
1287
1288         dev_info(dev, "probed\n");
1289         return ret;
1290
1291 exit_snd_soc:
1292         snd_soc_unregister_platform(dev);
1293 exit_snd_probe:
1294         for_each_rsnd_dai(rdai, priv, i) {
1295                 rsnd_dai_call(remove, &rdai->playback, priv);
1296                 rsnd_dai_call(remove, &rdai->capture, priv);
1297         }
1298
1299         return ret;
1300 }
1301
1302 static int rsnd_remove(struct platform_device *pdev)
1303 {
1304         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1305         struct rsnd_dai *rdai;
1306         void (*remove_func[])(struct platform_device *pdev,
1307                               struct rsnd_priv *priv) = {
1308                 rsnd_ssi_remove,
1309                 rsnd_src_remove,
1310                 rsnd_dvc_remove,
1311         };
1312         int ret = 0, i;
1313
1314         pm_runtime_disable(&pdev->dev);
1315
1316         for_each_rsnd_dai(rdai, priv, i) {
1317                 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1318                 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1319         }
1320
1321         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1322                 remove_func[i](pdev, priv);
1323
1324         snd_soc_unregister_component(&pdev->dev);
1325         snd_soc_unregister_platform(&pdev->dev);
1326
1327         return ret;
1328 }
1329
1330 static struct platform_driver rsnd_driver = {
1331         .driver = {
1332                 .name   = "rcar_sound",
1333                 .of_match_table = rsnd_of_match,
1334         },
1335         .probe          = rsnd_probe,
1336         .remove         = rsnd_remove,
1337 };
1338 module_platform_driver(rsnd_driver);
1339
1340 MODULE_LICENSE("GPL");
1341 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1342 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1343 MODULE_ALIAS("platform:rcar-pcm-audio");