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