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