ASoC: rsnd: count each mod (SSI/SRC/DVC)
[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 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 "rsnd.h"
98
99 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
101
102 static const struct rsnd_of_data rsnd_of_data_gen1 = {
103         .flags = RSND_GEN1,
104 };
105
106 static const struct rsnd_of_data rsnd_of_data_gen2 = {
107         .flags = RSND_GEN2,
108 };
109
110 static const struct of_device_id rsnd_of_match[] = {
111         { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
112         { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
113         {},
114 };
115 MODULE_DEVICE_TABLE(of, rsnd_of_match);
116
117 /*
118  *      rsnd_platform functions
119  */
120 #define rsnd_platform_call(priv, dai, func, param...)   \
121         (!(priv->info->func) ? 0 :              \
122          priv->info->func(param))
123
124 #define rsnd_is_enable_path(io, name) \
125         ((io)->info ? (io)->info->name : NULL)
126 #define rsnd_info_id(priv, io, name) \
127         ((io)->info->name - priv->info->name##_info)
128
129 /*
130  *      rsnd_mod functions
131  */
132 char *rsnd_mod_name(struct rsnd_mod *mod)
133 {
134         if (!mod || !mod->ops)
135                 return "unknown";
136
137         return mod->ops->name;
138 }
139
140 struct dma_chan *rsnd_mod_dma_req(struct rsnd_mod *mod)
141 {
142         if (!mod || !mod->ops || !mod->ops->dma_req)
143                 return NULL;
144
145         return mod->ops->dma_req(mod);
146 }
147
148 int rsnd_mod_init(struct rsnd_mod *mod,
149                    struct rsnd_mod_ops *ops,
150                    struct clk *clk,
151                    enum rsnd_mod_type type,
152                    int id)
153 {
154         int ret = clk_prepare(clk);
155
156         if (ret)
157                 return ret;
158
159         mod->id         = id;
160         mod->ops        = ops;
161         mod->type       = type;
162         mod->clk        = clk;
163
164         return ret;
165 }
166
167 void rsnd_mod_quit(struct rsnd_mod *mod)
168 {
169         if (mod->clk)
170                 clk_unprepare(mod->clk);
171 }
172
173 int rsnd_mod_is_working(struct rsnd_mod *mod)
174 {
175         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
176
177         /* see rsnd_dai_stream_init/quit() */
178         return !!io->substream;
179 }
180
181 /*
182  *      settting function
183  */
184 u32 rsnd_get_adinr(struct rsnd_mod *mod)
185 {
186         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
187         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
188         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
189         struct device *dev = rsnd_priv_to_dev(priv);
190         u32 adinr = runtime->channels;
191
192         switch (runtime->sample_bits) {
193         case 16:
194                 adinr |= (8 << 16);
195                 break;
196         case 32:
197                 adinr |= (0 << 16);
198                 break;
199         default:
200                 dev_warn(dev, "not supported sample bits\n");
201                 return 0;
202         }
203
204         return adinr;
205 }
206
207 /*
208  *      rsnd_dai functions
209  */
210 #define __rsnd_mod_call(mod, func, param...)                    \
211 ({                                                              \
212         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
213         struct device *dev = rsnd_priv_to_dev(priv);            \
214         u32 mask = 0xF << __rsnd_mod_shift_##func;                      \
215         u8 val  = (mod->status >> __rsnd_mod_shift_##func) & 0xF;       \
216         u8 add  = ((val + __rsnd_mod_add_##func) & 0xF);                \
217         int ret = 0;                                                    \
218         int called = 0;                                                 \
219         if (val == __rsnd_mod_call_##func) {                            \
220                 called = 1;                                             \
221                 ret = (mod)->ops->func(mod, param);                     \
222                 mod->status = (mod->status & ~mask) +                   \
223                         (add << __rsnd_mod_shift_##func);               \
224         }                                                               \
225         dev_dbg(dev, "%s[%d] 0x%08x %s\n",                              \
226                 rsnd_mod_name(mod), rsnd_mod_id(mod), mod->status,      \
227                 called ? #func : "");                                   \
228         ret;                                                            \
229 })
230
231 #define rsnd_mod_call(mod, func, param...)      \
232         (!(mod) ? -ENODEV :                     \
233          !((mod)->ops->func) ? 0 :              \
234          __rsnd_mod_call(mod, func, param))
235
236 #define rsnd_dai_call(fn, io, param...)                         \
237 ({                                                              \
238         struct rsnd_mod *mod;                                   \
239         int ret = 0, i;                                         \
240         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
241                 mod = (io)->mod[i];                             \
242                 if (!mod)                                       \
243                         continue;                               \
244                 ret = rsnd_mod_call(mod, fn, param);            \
245                 if (ret < 0)                                    \
246                         break;                                  \
247         }                                                       \
248         ret;                                                    \
249 })
250
251 static int rsnd_dai_connect(struct rsnd_mod *mod,
252                             struct rsnd_dai_stream *io)
253 {
254         if (!mod)
255                 return -EIO;
256
257         if (io->mod[mod->type]) {
258                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
259                 struct device *dev = rsnd_priv_to_dev(priv);
260
261                 dev_err(dev, "%s[%d] is not empty\n",
262                         rsnd_mod_name(mod),
263                         rsnd_mod_id(mod));
264                 return -EIO;
265         }
266
267         io->mod[mod->type] = mod;
268         mod->io = io;
269
270         return 0;
271 }
272
273 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
274                                 struct rsnd_dai_stream *io)
275 {
276         mod->io = NULL;
277         io->mod[mod->type] = NULL;
278 }
279
280 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
281 {
282         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
283                 return NULL;
284
285         return priv->rdai + id;
286 }
287
288 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
289 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
290 {
291         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
292
293         return rsnd_rdai_get(priv, dai->id);
294 }
295
296 /*
297  *      rsnd_soc_dai functions
298  */
299 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
300 {
301         struct snd_pcm_substream *substream = io->substream;
302         struct snd_pcm_runtime *runtime = substream->runtime;
303         int pos = io->byte_pos + additional;
304
305         pos %= (runtime->periods * io->byte_per_period);
306
307         return pos;
308 }
309
310 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
311 {
312         io->byte_pos += byte;
313
314         if (io->byte_pos >= io->next_period_byte) {
315                 struct snd_pcm_substream *substream = io->substream;
316                 struct snd_pcm_runtime *runtime = substream->runtime;
317
318                 io->period_pos++;
319                 io->next_period_byte += io->byte_per_period;
320
321                 if (io->period_pos >= runtime->periods) {
322                         io->byte_pos = 0;
323                         io->period_pos = 0;
324                         io->next_period_byte = io->byte_per_period;
325                 }
326
327                 return true;
328         }
329
330         return false;
331 }
332
333 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
334 {
335         struct snd_pcm_substream *substream = io->substream;
336
337         /*
338          * this function should be called...
339          *
340          * - if rsnd_dai_pointer_update() returns true
341          * - without spin lock
342          */
343
344         snd_pcm_period_elapsed(substream);
345 }
346
347 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
348                                 struct snd_pcm_substream *substream)
349 {
350         struct snd_pcm_runtime *runtime = substream->runtime;
351
352         io->substream           = substream;
353         io->byte_pos            = 0;
354         io->period_pos          = 0;
355         io->byte_per_period     = runtime->period_size *
356                                   runtime->channels *
357                                   samples_to_bytes(runtime, 1);
358         io->next_period_byte    = io->byte_per_period;
359 }
360
361 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
362 {
363         io->substream           = NULL;
364 }
365
366 static
367 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
368 {
369         struct snd_soc_pcm_runtime *rtd = substream->private_data;
370
371         return  rtd->cpu_dai;
372 }
373
374 static
375 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
376                                         struct snd_pcm_substream *substream)
377 {
378         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
379                 return &rdai->playback;
380         else
381                 return &rdai->capture;
382 }
383
384 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
385                             struct snd_soc_dai *dai)
386 {
387         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
388         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
389         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
390         int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
391         int ret;
392         unsigned long flags;
393
394         spin_lock_irqsave(&priv->lock, flags);
395
396         switch (cmd) {
397         case SNDRV_PCM_TRIGGER_START:
398                 rsnd_dai_stream_init(io, substream);
399
400                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
401                 if (ret < 0)
402                         goto dai_trigger_end;
403
404                 ret = rsnd_dai_call(init, io, priv);
405                 if (ret < 0)
406                         goto dai_trigger_end;
407
408                 ret = rsnd_dai_call(start, io, priv);
409                 if (ret < 0)
410                         goto dai_trigger_end;
411                 break;
412         case SNDRV_PCM_TRIGGER_STOP:
413                 ret = rsnd_dai_call(stop, io, priv);
414                 if (ret < 0)
415                         goto dai_trigger_end;
416
417                 ret = rsnd_dai_call(quit, io, priv);
418                 if (ret < 0)
419                         goto dai_trigger_end;
420
421                 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
422                 if (ret < 0)
423                         goto dai_trigger_end;
424
425                 rsnd_dai_stream_quit(io);
426                 break;
427         default:
428                 ret = -EINVAL;
429         }
430
431 dai_trigger_end:
432         spin_unlock_irqrestore(&priv->lock, flags);
433
434         return ret;
435 }
436
437 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
438 {
439         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
440
441         /* set master/slave audio interface */
442         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
443         case SND_SOC_DAIFMT_CBM_CFM:
444                 rdai->clk_master = 0;
445                 break;
446         case SND_SOC_DAIFMT_CBS_CFS:
447                 rdai->clk_master = 1; /* codec is slave, cpu is master */
448                 break;
449         default:
450                 return -EINVAL;
451         }
452
453         /* set format */
454         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
455         case SND_SOC_DAIFMT_I2S:
456                 rdai->sys_delay = 0;
457                 rdai->data_alignment = 0;
458                 rdai->frm_clk_inv = 0;
459                 break;
460         case SND_SOC_DAIFMT_LEFT_J:
461                 rdai->sys_delay = 1;
462                 rdai->data_alignment = 0;
463                 rdai->frm_clk_inv = 1;
464                 break;
465         case SND_SOC_DAIFMT_RIGHT_J:
466                 rdai->sys_delay = 1;
467                 rdai->data_alignment = 1;
468                 rdai->frm_clk_inv = 1;
469                 break;
470         }
471
472         /* set clock inversion */
473         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
474         case SND_SOC_DAIFMT_NB_IF:
475                 rdai->bit_clk_inv =  rdai->bit_clk_inv;
476                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
477                 break;
478         case SND_SOC_DAIFMT_IB_NF:
479                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
480                 rdai->frm_clk_inv =  rdai->frm_clk_inv;
481                 break;
482         case SND_SOC_DAIFMT_IB_IF:
483                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
484                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
485                 break;
486         case SND_SOC_DAIFMT_NB_NF:
487         default:
488                 break;
489         }
490
491         return 0;
492 }
493
494 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
495         .trigger        = rsnd_soc_dai_trigger,
496         .set_fmt        = rsnd_soc_dai_set_fmt,
497 };
498
499 #define rsnd_path_parse(priv, io, type)                         \
500 ({                                                              \
501         struct rsnd_mod *mod;                                   \
502         int ret = 0;                                            \
503         int id = -1;                                            \
504                                                                 \
505         if (rsnd_is_enable_path(io, type)) {                    \
506                 id = rsnd_info_id(priv, io, type);              \
507                 if (id >= 0) {                                  \
508                         mod = rsnd_##type##_mod_get(priv, id);  \
509                         ret = rsnd_dai_connect(mod, io);        \
510                 }                                               \
511         }                                                       \
512         ret;                                                    \
513 })
514
515 #define rsnd_path_break(priv, io, type)                         \
516 {                                                               \
517         struct rsnd_mod *mod;                                   \
518         int id = -1;                                            \
519                                                                 \
520         if (rsnd_is_enable_path(io, type)) {                    \
521                 id = rsnd_info_id(priv, io, type);              \
522                 if (id >= 0) {                                  \
523                         mod = rsnd_##type##_mod_get(priv, id);  \
524                         rsnd_dai_disconnect(mod, io);           \
525                 }                                               \
526         }                                                       \
527 }
528
529 static int rsnd_path_init(struct rsnd_priv *priv,
530                           struct rsnd_dai *rdai,
531                           struct rsnd_dai_stream *io)
532 {
533         int ret;
534
535         /*
536          * Gen1 is created by SRU/SSI, and this SRU is base module of
537          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
538          *
539          * Easy image is..
540          *      Gen1 SRU = Gen2 SCU + SSIU + etc
541          *
542          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
543          * using fixed path.
544          */
545
546         /* SRC */
547         ret = rsnd_path_parse(priv, io, src);
548         if (ret < 0)
549                 return ret;
550
551         /* SSI */
552         ret = rsnd_path_parse(priv, io, ssi);
553         if (ret < 0)
554                 return ret;
555
556         /* DVC */
557         ret = rsnd_path_parse(priv, io, dvc);
558         if (ret < 0)
559                 return ret;
560
561         return ret;
562 }
563
564 static void rsnd_of_parse_dai(struct platform_device *pdev,
565                               const struct rsnd_of_data *of_data,
566                               struct rsnd_priv *priv)
567 {
568         struct device_node *dai_node,   *dai_np;
569         struct device_node *ssi_node,   *ssi_np;
570         struct device_node *src_node,   *src_np;
571         struct device_node *dvc_node,   *dvc_np;
572         struct device_node *playback, *capture;
573         struct rsnd_dai_platform_info *dai_info;
574         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
575         struct device *dev = &pdev->dev;
576         int nr, i;
577         int dai_i, ssi_i, src_i, dvc_i;
578
579         if (!of_data)
580                 return;
581
582         dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
583         if (!dai_node)
584                 return;
585
586         nr = of_get_child_count(dai_node);
587         if (!nr)
588                 return;
589
590         dai_info = devm_kzalloc(dev,
591                                 sizeof(struct rsnd_dai_platform_info) * nr,
592                                 GFP_KERNEL);
593         if (!dai_info) {
594                 dev_err(dev, "dai info allocation error\n");
595                 return;
596         }
597
598         info->dai_info_nr       = nr;
599         info->dai_info          = dai_info;
600
601         ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
602         src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
603         dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
604
605 #define mod_parse(name)                                                 \
606 if (name##_node) {                                                      \
607         struct rsnd_##name##_platform_info *name##_info;                \
608                                                                         \
609         name##_i = 0;                                                   \
610         for_each_child_of_node(name##_node, name##_np) {                \
611                 name##_info = info->name##_info + name##_i;             \
612                                                                         \
613                 if (name##_np == playback)                              \
614                         dai_info->playback.name = name##_info;          \
615                 if (name##_np == capture)                               \
616                         dai_info->capture.name = name##_info;           \
617                                                                         \
618                 name##_i++;                                             \
619         }                                                               \
620 }
621
622         /*
623          * parse all dai
624          */
625         dai_i = 0;
626         for_each_child_of_node(dai_node, dai_np) {
627                 dai_info = info->dai_info + dai_i;
628
629                 for (i = 0;; i++) {
630
631                         playback = of_parse_phandle(dai_np, "playback", i);
632                         capture  = of_parse_phandle(dai_np, "capture", i);
633
634                         if (!playback && !capture)
635                                 break;
636
637                         mod_parse(ssi);
638                         mod_parse(src);
639                         mod_parse(dvc);
640
641                         of_node_put(playback);
642                         of_node_put(capture);
643                 }
644
645                 dai_i++;
646         }
647 }
648
649 static int rsnd_dai_probe(struct platform_device *pdev,
650                           const struct rsnd_of_data *of_data,
651                           struct rsnd_priv *priv)
652 {
653         struct snd_soc_dai_driver *drv;
654         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
655         struct rsnd_dai *rdai;
656         struct rsnd_ssi_platform_info *pmod, *cmod;
657         struct device *dev = rsnd_priv_to_dev(priv);
658         int dai_nr;
659         int i;
660
661         rsnd_of_parse_dai(pdev, of_data, priv);
662
663         dai_nr = info->dai_info_nr;
664         if (!dai_nr) {
665                 dev_err(dev, "no dai\n");
666                 return -EIO;
667         }
668
669         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
670         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
671         if (!drv || !rdai) {
672                 dev_err(dev, "dai allocate failed\n");
673                 return -ENOMEM;
674         }
675
676         priv->rdai_nr   = dai_nr;
677         priv->daidrv    = drv;
678         priv->rdai      = rdai;
679
680         for (i = 0; i < dai_nr; i++) {
681
682                 pmod = info->dai_info[i].playback.ssi;
683                 cmod = info->dai_info[i].capture.ssi;
684
685                 /*
686                  *      init rsnd_dai
687                  */
688                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
689                 rdai[i].priv = priv;
690
691                 /*
692                  *      init snd_soc_dai_driver
693                  */
694                 drv[i].name     = rdai[i].name;
695                 drv[i].ops      = &rsnd_soc_dai_ops;
696                 if (pmod) {
697                         snprintf(rdai[i].playback.name, RSND_DAI_NAME_SIZE,
698                                  "DAI%d Playback", i);
699
700                         drv[i].playback.rates           = RSND_RATES;
701                         drv[i].playback.formats         = RSND_FMTS;
702                         drv[i].playback.channels_min    = 2;
703                         drv[i].playback.channels_max    = 2;
704                         drv[i].playback.stream_name     = rdai[i].playback.name;
705
706                         rdai[i].playback.info = &info->dai_info[i].playback;
707                         rdai[i].playback.rdai = rdai + i;
708                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
709                 }
710                 if (cmod) {
711                         snprintf(rdai[i].capture.name, RSND_DAI_NAME_SIZE,
712                                  "DAI%d Capture", i);
713
714                         drv[i].capture.rates            = RSND_RATES;
715                         drv[i].capture.formats          = RSND_FMTS;
716                         drv[i].capture.channels_min     = 2;
717                         drv[i].capture.channels_max     = 2;
718                         drv[i].capture.stream_name      = rdai[i].capture.name;
719
720                         rdai[i].capture.info = &info->dai_info[i].capture;
721                         rdai[i].capture.rdai = rdai + i;
722                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
723                 }
724
725                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
726                         pmod ? "play"    : " -- ",
727                         cmod ? "capture" : "  --   ");
728         }
729
730         return 0;
731 }
732
733 /*
734  *              pcm ops
735  */
736 static struct snd_pcm_hardware rsnd_pcm_hardware = {
737         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
738                         SNDRV_PCM_INFO_MMAP             |
739                         SNDRV_PCM_INFO_MMAP_VALID,
740         .buffer_bytes_max       = 64 * 1024,
741         .period_bytes_min       = 32,
742         .period_bytes_max       = 8192,
743         .periods_min            = 1,
744         .periods_max            = 32,
745         .fifo_size              = 256,
746 };
747
748 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
749 {
750         struct snd_pcm_runtime *runtime = substream->runtime;
751         int ret = 0;
752
753         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
754
755         ret = snd_pcm_hw_constraint_integer(runtime,
756                                             SNDRV_PCM_HW_PARAM_PERIODS);
757
758         return ret;
759 }
760
761 static int rsnd_hw_params(struct snd_pcm_substream *substream,
762                          struct snd_pcm_hw_params *hw_params)
763 {
764         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
765         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
766         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
767         int ret;
768
769         ret = rsnd_dai_call(hw_params, io, substream, hw_params);
770         if (ret)
771                 return ret;
772
773         return snd_pcm_lib_malloc_pages(substream,
774                                         params_buffer_bytes(hw_params));
775 }
776
777 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
778 {
779         struct snd_pcm_runtime *runtime = substream->runtime;
780         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
781         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
782         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
783
784         return bytes_to_frames(runtime, io->byte_pos);
785 }
786
787 static struct snd_pcm_ops rsnd_pcm_ops = {
788         .open           = rsnd_pcm_open,
789         .ioctl          = snd_pcm_lib_ioctl,
790         .hw_params      = rsnd_hw_params,
791         .hw_free        = snd_pcm_lib_free_pages,
792         .pointer        = rsnd_pointer,
793 };
794
795 /*
796  *              snd_kcontrol
797  */
798 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
799 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
800                            struct snd_ctl_elem_info *uinfo)
801 {
802         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
803
804         if (cfg->texts) {
805                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
806                 uinfo->count = cfg->size;
807                 uinfo->value.enumerated.items = cfg->max;
808                 if (uinfo->value.enumerated.item >= cfg->max)
809                         uinfo->value.enumerated.item = cfg->max - 1;
810                 strlcpy(uinfo->value.enumerated.name,
811                         cfg->texts[uinfo->value.enumerated.item],
812                         sizeof(uinfo->value.enumerated.name));
813         } else {
814                 uinfo->count = cfg->size;
815                 uinfo->value.integer.min = 0;
816                 uinfo->value.integer.max = cfg->max;
817                 uinfo->type = (cfg->max == 1) ?
818                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
819                         SNDRV_CTL_ELEM_TYPE_INTEGER;
820         }
821
822         return 0;
823 }
824
825 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
826                           struct snd_ctl_elem_value *uc)
827 {
828         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
829         int i;
830
831         for (i = 0; i < cfg->size; i++)
832                 if (cfg->texts)
833                         uc->value.enumerated.item[i] = cfg->val[i];
834                 else
835                         uc->value.integer.value[i] = cfg->val[i];
836
837         return 0;
838 }
839
840 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
841                           struct snd_ctl_elem_value *uc)
842 {
843         struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
844         struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
845         int i, change = 0;
846
847         for (i = 0; i < cfg->size; i++) {
848                 if (cfg->texts) {
849                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
850                         cfg->val[i] = uc->value.enumerated.item[i];
851                 } else {
852                         change |= (uc->value.integer.value[i] != cfg->val[i]);
853                         cfg->val[i] = uc->value.integer.value[i];
854                 }
855         }
856
857         if (change)
858                 cfg->update(mod);
859
860         return change;
861 }
862
863 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
864                             struct snd_soc_pcm_runtime *rtd,
865                             const unsigned char *name,
866                             struct rsnd_kctrl_cfg *cfg,
867                             void (*update)(struct rsnd_mod *mod))
868 {
869         struct snd_soc_card *soc_card = rtd->card;
870         struct snd_card *card = rtd->card->snd_card;
871         struct snd_kcontrol *kctrl;
872         struct snd_kcontrol_new knew = {
873                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
874                 .name           = name,
875                 .info           = rsnd_kctrl_info,
876                 .index          = rtd - soc_card->rtd,
877                 .get            = rsnd_kctrl_get,
878                 .put            = rsnd_kctrl_put,
879                 .private_value  = (unsigned long)cfg,
880         };
881         int ret;
882
883         kctrl = snd_ctl_new1(&knew, mod);
884         if (!kctrl)
885                 return -ENOMEM;
886
887         ret = snd_ctl_add(card, kctrl);
888         if (ret < 0) {
889                 snd_ctl_free_one(kctrl);
890                 return ret;
891         }
892
893         cfg->update = update;
894         cfg->card = card;
895         cfg->kctrl = kctrl;
896
897         return 0;
898 }
899
900 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
901 {
902         snd_ctl_remove(cfg->card, cfg->kctrl);
903 }
904
905 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
906                      struct snd_soc_pcm_runtime *rtd,
907                      const unsigned char *name,
908                      void (*update)(struct rsnd_mod *mod),
909                      struct rsnd_kctrl_cfg_m *_cfg,
910                      u32 max)
911 {
912         _cfg->cfg.max   = max;
913         _cfg->cfg.size  = RSND_DVC_CHANNELS;
914         _cfg->cfg.val   = _cfg->val;
915         return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
916 }
917
918 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
919                      struct snd_soc_pcm_runtime *rtd,
920                      const unsigned char *name,
921                      void (*update)(struct rsnd_mod *mod),
922                      struct rsnd_kctrl_cfg_s *_cfg,
923                      u32 max)
924 {
925         _cfg->cfg.max   = max;
926         _cfg->cfg.size  = 1;
927         _cfg->cfg.val   = &_cfg->val;
928         return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
929 }
930
931 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
932                      struct snd_soc_pcm_runtime *rtd,
933                      const unsigned char *name,
934                      struct rsnd_kctrl_cfg_s *_cfg,
935                      void (*update)(struct rsnd_mod *mod),
936                      const char * const *texts,
937                      u32 max)
938 {
939         _cfg->cfg.max   = max;
940         _cfg->cfg.size  = 1;
941         _cfg->cfg.val   = &_cfg->val;
942         _cfg->cfg.texts = texts;
943         return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
944 }
945
946 /*
947  *              snd_soc_platform
948  */
949
950 #define PREALLOC_BUFFER         (32 * 1024)
951 #define PREALLOC_BUFFER_MAX     (32 * 1024)
952
953 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
954 {
955         struct snd_soc_dai *dai = rtd->cpu_dai;
956         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
957         int ret;
958
959         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
960         if (ret)
961                 return ret;
962
963         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
964         if (ret)
965                 return ret;
966
967         return snd_pcm_lib_preallocate_pages_for_all(
968                 rtd->pcm,
969                 SNDRV_DMA_TYPE_DEV,
970                 rtd->card->snd_card->dev,
971                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
972 }
973
974 static struct snd_soc_platform_driver rsnd_soc_platform = {
975         .ops            = &rsnd_pcm_ops,
976         .pcm_new        = rsnd_pcm_new,
977 };
978
979 static const struct snd_soc_component_driver rsnd_soc_component = {
980         .name           = "rsnd",
981 };
982
983 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
984                                        struct rsnd_dai_stream *io)
985 {
986         int ret;
987
988         ret = rsnd_dai_call(probe, io, priv);
989         if (ret == -EAGAIN) {
990                 /*
991                  * Fallback to PIO mode
992                  */
993
994                 /*
995                  * call "remove" for SSI/SRC/DVC
996                  * SSI will be switch to PIO mode if it was DMA mode
997                  * see
998                  *      rsnd_dma_init()
999                  *      rsnd_ssi_fallback()
1000                  */
1001                 rsnd_dai_call(remove, io, priv);
1002
1003                 /*
1004                  * remove SRC/DVC from DAI,
1005                  */
1006                 rsnd_path_break(priv, io, src);
1007                 rsnd_path_break(priv, io, dvc);
1008
1009                 /*
1010                  * fallback
1011                  */
1012                 rsnd_dai_call(fallback, io, priv);
1013
1014                 /*
1015                  * retry to "probe".
1016                  * DAI has SSI which is PIO mode only now.
1017                  */
1018                 ret = rsnd_dai_call(probe, io, priv);
1019         }
1020
1021         return ret;
1022 }
1023
1024 /*
1025  *      rsnd probe
1026  */
1027 static int rsnd_probe(struct platform_device *pdev)
1028 {
1029         struct rcar_snd_info *info;
1030         struct rsnd_priv *priv;
1031         struct device *dev = &pdev->dev;
1032         struct rsnd_dai *rdai;
1033         const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1034         const struct rsnd_of_data *of_data;
1035         int (*probe_func[])(struct platform_device *pdev,
1036                             const struct rsnd_of_data *of_data,
1037                             struct rsnd_priv *priv) = {
1038                 rsnd_gen_probe,
1039                 rsnd_dma_probe,
1040                 rsnd_ssi_probe,
1041                 rsnd_src_probe,
1042                 rsnd_dvc_probe,
1043                 rsnd_adg_probe,
1044                 rsnd_dai_probe,
1045         };
1046         int ret, i;
1047
1048         info = NULL;
1049         of_data = NULL;
1050         if (of_id) {
1051                 info = devm_kzalloc(&pdev->dev,
1052                                     sizeof(struct rcar_snd_info), GFP_KERNEL);
1053                 of_data = of_id->data;
1054         } else {
1055                 info = pdev->dev.platform_data;
1056         }
1057
1058         if (!info) {
1059                 dev_err(dev, "driver needs R-Car sound information\n");
1060                 return -ENODEV;
1061         }
1062
1063         /*
1064          *      init priv data
1065          */
1066         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1067         if (!priv) {
1068                 dev_err(dev, "priv allocate failed\n");
1069                 return -ENODEV;
1070         }
1071
1072         priv->pdev      = pdev;
1073         priv->info      = info;
1074         spin_lock_init(&priv->lock);
1075
1076         /*
1077          *      init each module
1078          */
1079         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1080                 ret = probe_func[i](pdev, of_data, priv);
1081                 if (ret)
1082                         return ret;
1083         }
1084
1085         for_each_rsnd_dai(rdai, priv, i) {
1086                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1087                 if (ret)
1088                         goto exit_snd_probe;
1089
1090                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1091                 if (ret)
1092                         goto exit_snd_probe;
1093         }
1094
1095         dev_set_drvdata(dev, priv);
1096
1097         /*
1098          *      asoc register
1099          */
1100         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1101         if (ret < 0) {
1102                 dev_err(dev, "cannot snd soc register\n");
1103                 return ret;
1104         }
1105
1106         ret = snd_soc_register_component(dev, &rsnd_soc_component,
1107                                          priv->daidrv, rsnd_rdai_nr(priv));
1108         if (ret < 0) {
1109                 dev_err(dev, "cannot snd dai register\n");
1110                 goto exit_snd_soc;
1111         }
1112
1113         pm_runtime_enable(dev);
1114
1115         dev_info(dev, "probed\n");
1116         return ret;
1117
1118 exit_snd_soc:
1119         snd_soc_unregister_platform(dev);
1120 exit_snd_probe:
1121         for_each_rsnd_dai(rdai, priv, i) {
1122                 rsnd_dai_call(remove, &rdai->playback, priv);
1123                 rsnd_dai_call(remove, &rdai->capture, priv);
1124         }
1125
1126         return ret;
1127 }
1128
1129 static int rsnd_remove(struct platform_device *pdev)
1130 {
1131         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1132         struct rsnd_dai *rdai;
1133         void (*remove_func[])(struct platform_device *pdev,
1134                               struct rsnd_priv *priv) = {
1135                 rsnd_ssi_remove,
1136                 rsnd_src_remove,
1137                 rsnd_dvc_remove,
1138         };
1139         int ret = 0, i;
1140
1141         pm_runtime_disable(&pdev->dev);
1142
1143         for_each_rsnd_dai(rdai, priv, i) {
1144                 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1145                 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1146         }
1147
1148         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1149                 remove_func[i](pdev, priv);
1150
1151         snd_soc_unregister_component(&pdev->dev);
1152         snd_soc_unregister_platform(&pdev->dev);
1153
1154         return ret;
1155 }
1156
1157 static struct platform_driver rsnd_driver = {
1158         .driver = {
1159                 .name   = "rcar_sound",
1160                 .of_match_table = rsnd_of_match,
1161         },
1162         .probe          = rsnd_probe,
1163         .remove         = rsnd_remove,
1164 };
1165 module_platform_driver(rsnd_driver);
1166
1167 MODULE_LICENSE("GPL");
1168 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1169 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1170 MODULE_ALIAS("platform:rcar-pcm-audio");