Merge branch 'asoc-4.19' into asoc-4.20 for rcar dep
[muen/linux.git] / sound / soc / sh / rcar / core.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SRU/SCU/SSIU/SSI support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // Based on fsi.c
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
10
11 /*
12  * Renesas R-Car sound device structure
13  *
14  * Gen1
15  *
16  * SRU          : Sound Routing Unit
17  *  - SRC       : Sampling Rate Converter
18  *  - CMD
19  *    - CTU     : Channel Count Conversion Unit
20  *    - MIX     : Mixer
21  *    - DVC     : Digital Volume and Mute Function
22  *  - SSI       : Serial Sound Interface
23  *
24  * Gen2
25  *
26  * SCU          : Sampling Rate Converter Unit
27  *  - SRC       : Sampling Rate Converter
28  *  - CMD
29  *   - CTU      : Channel Count Conversion Unit
30  *   - MIX      : Mixer
31  *   - DVC      : Digital Volume and Mute Function
32  * SSIU         : Serial Sound Interface Unit
33  *  - SSI       : Serial Sound Interface
34  */
35
36 /*
37  *      driver data Image
38  *
39  * rsnd_priv
40  *   |
41  *   | ** this depends on Gen1/Gen2
42  *   |
43  *   +- gen
44  *   |
45  *   | ** these depend on data path
46  *   | ** gen and platform data control it
47  *   |
48  *   +- rdai[0]
49  *   |   |               sru     ssiu      ssi
50  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
51  *   |   |
52  *   |   |               sru     ssiu      ssi
53  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
54  *   |
55  *   +- rdai[1]
56  *   |   |               sru     ssiu      ssi
57  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
58  *   |   |
59  *   |   |               sru     ssiu      ssi
60  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
61  *   ...
62  *   |
63  *   | ** these control ssi
64  *   |
65  *   +- ssi
66  *   |  |
67  *   |  +- ssi[0]
68  *   |  +- ssi[1]
69  *   |  +- ssi[2]
70  *   |  ...
71  *   |
72  *   | ** these control src
73  *   |
74  *   +- src
75  *      |
76  *      +- src[0]
77  *      +- src[1]
78  *      +- src[2]
79  *      ...
80  *
81  *
82  * for_each_rsnd_dai(xx, priv, xx)
83  *  rdai[0] => rdai[1] => rdai[2] => ...
84  *
85  * for_each_rsnd_mod(xx, rdai, xx)
86  *  [mod] => [mod] => [mod] => ...
87  *
88  * rsnd_dai_call(xxx, fn )
89  *  [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
90  *
91  */
92
93 /*
94  * you can enable below define if you don't need
95  * DAI status debug message when debugging
96  * see rsnd_dbg_dai_call()
97  *
98  * #define RSND_DEBUG_NO_DAI_CALL 1
99  */
100
101 #include <linux/pm_runtime.h>
102 #include "rsnd.h"
103
104 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
105 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
106                    SNDRV_PCM_FMTBIT_S16_LE |\
107                    SNDRV_PCM_FMTBIT_S24_LE)
108
109 static const struct of_device_id rsnd_of_match[] = {
110         { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
111         { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
112         { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
113         {},
114 };
115 MODULE_DEVICE_TABLE(of, rsnd_of_match);
116
117 /*
118  *      rsnd_mod functions
119  */
120 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
121 {
122         if (mod->type != type) {
123                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
124                 struct device *dev = rsnd_priv_to_dev(priv);
125
126                 dev_warn(dev, "%s[%d] is not your expected module\n",
127                          rsnd_mod_name(mod), rsnd_mod_id(mod));
128         }
129 }
130
131 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
132                                   struct rsnd_mod *mod)
133 {
134         if (!mod || !mod->ops || !mod->ops->dma_req)
135                 return NULL;
136
137         return mod->ops->dma_req(io, mod);
138 }
139
140 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
141                          struct rsnd_mod *mod,
142                          enum rsnd_mod_type type)
143 {
144         return &mod->status;
145 }
146
147 int rsnd_mod_init(struct rsnd_priv *priv,
148                   struct rsnd_mod *mod,
149                   struct rsnd_mod_ops *ops,
150                   struct clk *clk,
151                   u32* (*get_status)(struct rsnd_dai_stream *io,
152                                      struct rsnd_mod *mod,
153                                      enum rsnd_mod_type type),
154                   enum rsnd_mod_type type,
155                   int id)
156 {
157         int ret = clk_prepare(clk);
158
159         if (ret)
160                 return ret;
161
162         mod->id         = id;
163         mod->ops        = ops;
164         mod->type       = type;
165         mod->clk        = clk;
166         mod->priv       = priv;
167         mod->get_status = get_status;
168
169         return ret;
170 }
171
172 void rsnd_mod_quit(struct rsnd_mod *mod)
173 {
174         clk_unprepare(mod->clk);
175         mod->clk = NULL;
176 }
177
178 void rsnd_mod_interrupt(struct rsnd_mod *mod,
179                         void (*callback)(struct rsnd_mod *mod,
180                                          struct rsnd_dai_stream *io))
181 {
182         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
183         struct rsnd_dai_stream *io;
184         struct rsnd_dai *rdai;
185         int i;
186
187         for_each_rsnd_dai(rdai, priv, i) {
188                 io = &rdai->playback;
189                 if (mod == io->mod[mod->type])
190                         callback(mod, io);
191
192                 io = &rdai->capture;
193                 if (mod == io->mod[mod->type])
194                         callback(mod, io);
195         }
196 }
197
198 int rsnd_io_is_working(struct rsnd_dai_stream *io)
199 {
200         /* see rsnd_dai_stream_init/quit() */
201         if (io->substream)
202                 return snd_pcm_running(io->substream);
203
204         return 0;
205 }
206
207 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
208                                               struct snd_pcm_hw_params *params)
209 {
210         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
211
212         /*
213          * params will be added when refine
214          * see
215          *      __rsnd_soc_hw_rule_rate()
216          *      __rsnd_soc_hw_rule_channels()
217          */
218         if (params)
219                 return params_channels(params);
220         else
221                 return runtime->channels;
222 }
223
224 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
225                                                struct snd_pcm_hw_params *params)
226 {
227         int chan = rsnd_runtime_channel_original_with_params(io, params);
228         struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
229
230         if (ctu_mod) {
231                 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
232
233                 if (converted_chan)
234                         return converted_chan;
235         }
236
237         return chan;
238 }
239
240 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
241                                              struct snd_pcm_hw_params *params)
242 {
243         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
244         int chan = rsnd_io_is_play(io) ?
245                 rsnd_runtime_channel_after_ctu_with_params(io, params) :
246                 rsnd_runtime_channel_original_with_params(io, params);
247
248         /* Use Multi SSI */
249         if (rsnd_runtime_is_ssi_multi(io))
250                 chan /= rsnd_rdai_ssi_lane_get(rdai);
251
252         /* TDM Extend Mode needs 8ch */
253         if (chan == 6)
254                 chan = 8;
255
256         return chan;
257 }
258
259 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
260 {
261         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
262         int lane = rsnd_rdai_ssi_lane_get(rdai);
263         int chan = rsnd_io_is_play(io) ?
264                 rsnd_runtime_channel_after_ctu(io) :
265                 rsnd_runtime_channel_original(io);
266
267         return (chan > 2) && (lane > 1);
268 }
269
270 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
271 {
272         return rsnd_runtime_channel_for_ssi(io) >= 6;
273 }
274
275 /*
276  *      ADINR function
277  */
278 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
279 {
280         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
281         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
282         struct device *dev = rsnd_priv_to_dev(priv);
283
284         switch (snd_pcm_format_width(runtime->format)) {
285         case 8:
286                 return 16 << 16;
287         case 16:
288                 return 8 << 16;
289         case 24:
290                 return 0 << 16;
291         }
292
293         dev_warn(dev, "not supported sample bits\n");
294
295         return 0;
296 }
297
298 /*
299  *      DALIGN function
300  */
301 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
302 {
303         struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
304         struct rsnd_mod *target;
305         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
306
307         /*
308          * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
309          *          31..16 15...0
310          *      HW: [L ch] [R ch]
311          *      SW: [R ch] [L ch]
312          * We need to care about inversion timing to control
313          * Playback/Capture correctly.
314          * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
315          *
316          * sL/R : software L/R
317          * hL/R : hardware L/R
318          * (*)  : conversion timing
319          *
320          * Playback
321          *           sL/R (*) hL/R     hL/R     hL/R      hL/R     hL/R
322          *      [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
323          *
324          * Capture
325          *           hL/R     hL/R      hL/R     hL/R     hL/R (*) sL/R
326          *      codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
327          */
328         if (rsnd_io_is_play(io)) {
329                 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
330
331                 target = src ? src : ssiu;
332         } else {
333                 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
334
335                 target = cmd ? cmd : ssiu;
336         }
337
338         /* Non target mod or non 16bit needs normal DALIGN */
339         if ((snd_pcm_format_width(runtime->format) != 16) ||
340             (mod != target))
341                 return 0x76543210;
342         /* Target mod needs inverted DALIGN when 16bit */
343         else
344                 return 0x67452301;
345 }
346
347 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
348 {
349         enum rsnd_mod_type playback_mods[] = {
350                 RSND_MOD_SRC,
351                 RSND_MOD_CMD,
352                 RSND_MOD_SSIU,
353         };
354         enum rsnd_mod_type capture_mods[] = {
355                 RSND_MOD_CMD,
356                 RSND_MOD_SRC,
357                 RSND_MOD_SSIU,
358         };
359         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
360         struct rsnd_mod *tmod = NULL;
361         enum rsnd_mod_type *mods =
362                 rsnd_io_is_play(io) ?
363                 playback_mods : capture_mods;
364         int i;
365
366         /*
367          * This is needed for 24bit data
368          * We need to shift 8bit
369          *
370          * Linux 24bit data is located as 0x00******
371          * HW    24bit data is located as 0x******00
372          *
373          */
374         if (snd_pcm_format_width(runtime->format) != 24)
375                 return 0;
376
377         for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
378                 tmod = rsnd_io_to_mod(io, mods[i]);
379                 if (tmod)
380                         break;
381         }
382
383         if (tmod != mod)
384                 return 0;
385
386         if (rsnd_io_is_play(io))
387                 return  (0 << 20) | /* shift to Left */
388                         (8 << 16);  /* 8bit */
389         else
390                 return  (1 << 20) | /* shift to Right */
391                         (8 << 16);  /* 8bit */
392 }
393
394 /*
395  *      rsnd_dai functions
396  */
397 struct rsnd_mod *rsnd_mod_next(int *iterator,
398                                struct rsnd_dai_stream *io,
399                                enum rsnd_mod_type *array,
400                                int array_size)
401 {
402         struct rsnd_mod *mod;
403         enum rsnd_mod_type type;
404         int max = array ? array_size : RSND_MOD_MAX;
405
406         for (; *iterator < max; (*iterator)++) {
407                 type = (array) ? array[*iterator] : *iterator;
408                 mod = rsnd_io_to_mod(io, type);
409                 if (mod)
410                         return mod;
411         }
412
413         return NULL;
414 }
415
416 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
417         {
418                 /* CAPTURE */
419                 RSND_MOD_AUDMAPP,
420                 RSND_MOD_AUDMA,
421                 RSND_MOD_DVC,
422                 RSND_MOD_MIX,
423                 RSND_MOD_CTU,
424                 RSND_MOD_CMD,
425                 RSND_MOD_SRC,
426                 RSND_MOD_SSIU,
427                 RSND_MOD_SSIM3,
428                 RSND_MOD_SSIM2,
429                 RSND_MOD_SSIM1,
430                 RSND_MOD_SSIP,
431                 RSND_MOD_SSI,
432         }, {
433                 /* PLAYBACK */
434                 RSND_MOD_AUDMAPP,
435                 RSND_MOD_AUDMA,
436                 RSND_MOD_SSIM3,
437                 RSND_MOD_SSIM2,
438                 RSND_MOD_SSIM1,
439                 RSND_MOD_SSIP,
440                 RSND_MOD_SSI,
441                 RSND_MOD_SSIU,
442                 RSND_MOD_DVC,
443                 RSND_MOD_MIX,
444                 RSND_MOD_CTU,
445                 RSND_MOD_CMD,
446                 RSND_MOD_SRC,
447         },
448 };
449
450 static int rsnd_status_update(u32 *status,
451                               int shift, int add, int timing)
452 {
453         u32 mask        = 0xF << shift;
454         u8 val          = (*status >> shift) & 0xF;
455         u8 next_val     = (val + add) & 0xF;
456         int func_call   = (val == timing);
457
458         if (next_val == 0xF) /* underflow case */
459                 func_call = 0;
460         else
461                 *status = (*status & ~mask) + (next_val << shift);
462
463         return func_call;
464 }
465
466 #define rsnd_dai_call(fn, io, param...)                                 \
467 ({                                                                      \
468         struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));     \
469         struct rsnd_mod *mod;                                           \
470         int is_play = rsnd_io_is_play(io);                              \
471         int ret = 0, i;                                                 \
472         enum rsnd_mod_type *types = rsnd_mod_sequence[is_play];         \
473         for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) {     \
474                 int tmp = 0;                                            \
475                 u32 *status = mod->get_status(io, mod, types[i]);       \
476                 int func_call = rsnd_status_update(status,              \
477                                                 __rsnd_mod_shift_##fn,  \
478                                                 __rsnd_mod_add_##fn,    \
479                                                 __rsnd_mod_call_##fn);  \
480                 rsnd_dbg_dai_call(dev, "%s[%d]\t0x%08x %s\n",           \
481                         rsnd_mod_name(mod), rsnd_mod_id(mod), *status,  \
482                         (func_call && (mod)->ops->fn) ? #fn : "");      \
483                 if (func_call && (mod)->ops->fn)                        \
484                         tmp = (mod)->ops->fn(mod, io, param);           \
485                 if (tmp)                                                \
486                         dev_err(dev, "%s[%d] : %s error %d\n",          \
487                                 rsnd_mod_name(mod), rsnd_mod_id(mod),   \
488                                                      #fn, tmp);         \
489                 ret |= tmp;                                             \
490         }                                                               \
491         ret;                                                            \
492 })
493
494 int rsnd_dai_connect(struct rsnd_mod *mod,
495                      struct rsnd_dai_stream *io,
496                      enum rsnd_mod_type type)
497 {
498         struct rsnd_priv *priv;
499         struct device *dev;
500
501         if (!mod)
502                 return -EIO;
503
504         if (io->mod[type] == mod)
505                 return 0;
506
507         if (io->mod[type])
508                 return -EINVAL;
509
510         priv = rsnd_mod_to_priv(mod);
511         dev = rsnd_priv_to_dev(priv);
512
513         io->mod[type] = mod;
514
515         dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
516                 rsnd_mod_name(mod), rsnd_mod_id(mod),
517                 rsnd_io_is_play(io) ? "Playback" : "Capture");
518
519         return 0;
520 }
521
522 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
523                                 struct rsnd_dai_stream *io,
524                                 enum rsnd_mod_type type)
525 {
526         io->mod[type] = NULL;
527 }
528
529 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
530                             int max_channels)
531 {
532         if (max_channels > 0)
533                 rdai->max_channels = max_channels;
534
535         return rdai->max_channels;
536 }
537
538 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
539                             int ssi_lane)
540 {
541         if (ssi_lane > 0)
542                 rdai->ssi_lane = ssi_lane;
543
544         return rdai->ssi_lane;
545 }
546
547 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
548 {
549         if (width > 0)
550                 rdai->chan_width = width;
551
552         return rdai->chan_width;
553 }
554
555 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
556 {
557         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
558                 return NULL;
559
560         return priv->rdai + id;
561 }
562
563 static struct snd_soc_dai_driver
564 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
565 {
566         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
567                 return NULL;
568
569         return priv->daidrv + id;
570 }
571
572 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
573 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
574 {
575         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
576
577         return rsnd_rdai_get(priv, dai->id);
578 }
579
580 /*
581  *      rsnd_soc_dai functions
582  */
583 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
584 {
585         struct snd_pcm_substream *substream = io->substream;
586
587         /*
588          * this function should be called...
589          *
590          * - if rsnd_dai_pointer_update() returns true
591          * - without spin lock
592          */
593
594         snd_pcm_period_elapsed(substream);
595 }
596
597 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
598                                 struct snd_pcm_substream *substream)
599 {
600         io->substream           = substream;
601 }
602
603 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
604 {
605         io->substream           = NULL;
606 }
607
608 static
609 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
610 {
611         struct snd_soc_pcm_runtime *rtd = substream->private_data;
612
613         return  rtd->cpu_dai;
614 }
615
616 static
617 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
618                                         struct snd_pcm_substream *substream)
619 {
620         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
621                 return &rdai->playback;
622         else
623                 return &rdai->capture;
624 }
625
626 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
627                             struct snd_soc_dai *dai)
628 {
629         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
630         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
631         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
632         int ret;
633         unsigned long flags;
634
635         spin_lock_irqsave(&priv->lock, flags);
636
637         switch (cmd) {
638         case SNDRV_PCM_TRIGGER_START:
639         case SNDRV_PCM_TRIGGER_RESUME:
640                 ret = rsnd_dai_call(init, io, priv);
641                 if (ret < 0)
642                         goto dai_trigger_end;
643
644                 ret = rsnd_dai_call(start, io, priv);
645                 if (ret < 0)
646                         goto dai_trigger_end;
647
648                 ret = rsnd_dai_call(irq, io, priv, 1);
649                 if (ret < 0)
650                         goto dai_trigger_end;
651
652                 break;
653         case SNDRV_PCM_TRIGGER_STOP:
654         case SNDRV_PCM_TRIGGER_SUSPEND:
655                 ret = rsnd_dai_call(irq, io, priv, 0);
656
657                 ret |= rsnd_dai_call(stop, io, priv);
658
659                 ret |= rsnd_dai_call(quit, io, priv);
660
661                 break;
662         default:
663                 ret = -EINVAL;
664         }
665
666 dai_trigger_end:
667         spin_unlock_irqrestore(&priv->lock, flags);
668
669         return ret;
670 }
671
672 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
673 {
674         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
675
676         /* set master/slave audio interface */
677         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
678         case SND_SOC_DAIFMT_CBM_CFM:
679                 rdai->clk_master = 0;
680                 break;
681         case SND_SOC_DAIFMT_CBS_CFS:
682                 rdai->clk_master = 1; /* codec is slave, cpu is master */
683                 break;
684         default:
685                 return -EINVAL;
686         }
687
688         /* set format */
689         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
690         case SND_SOC_DAIFMT_I2S:
691                 rdai->sys_delay = 0;
692                 rdai->data_alignment = 0;
693                 rdai->frm_clk_inv = 0;
694                 break;
695         case SND_SOC_DAIFMT_LEFT_J:
696         case SND_SOC_DAIFMT_DSP_B:
697                 rdai->sys_delay = 1;
698                 rdai->data_alignment = 0;
699                 rdai->frm_clk_inv = 1;
700                 break;
701         case SND_SOC_DAIFMT_RIGHT_J:
702                 rdai->sys_delay = 1;
703                 rdai->data_alignment = 1;
704                 rdai->frm_clk_inv = 1;
705                 break;
706         case SND_SOC_DAIFMT_DSP_A:
707                 rdai->sys_delay = 0;
708                 rdai->data_alignment = 0;
709                 rdai->frm_clk_inv = 1;
710                 break;
711         }
712
713         /* set clock inversion */
714         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
715         case SND_SOC_DAIFMT_NB_IF:
716                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
717                 break;
718         case SND_SOC_DAIFMT_IB_NF:
719                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
720                 break;
721         case SND_SOC_DAIFMT_IB_IF:
722                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
723                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
724                 break;
725         case SND_SOC_DAIFMT_NB_NF:
726         default:
727                 break;
728         }
729
730         return 0;
731 }
732
733 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
734                                      u32 tx_mask, u32 rx_mask,
735                                      int slots, int slot_width)
736 {
737         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
738         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
739         struct device *dev = rsnd_priv_to_dev(priv);
740
741         switch (slot_width) {
742         case 16:
743         case 24:
744         case 32:
745                 break;
746         default:
747                 dev_err(dev, "unsupported slot width value: %d\n", slot_width);
748                 return -EINVAL;
749         }
750
751         switch (slots) {
752         case 2:
753         case 6:
754         case 8:
755                 /* TDM Extend Mode */
756                 rsnd_rdai_channels_set(rdai, slots);
757                 rsnd_rdai_ssi_lane_set(rdai, 1);
758                 rsnd_rdai_width_set(rdai, slot_width);
759                 break;
760         default:
761                 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
762                 return -EINVAL;
763         }
764
765         return 0;
766 }
767
768 static unsigned int rsnd_soc_hw_channels_list[] = {
769         2, 6, 8,
770 };
771
772 static unsigned int rsnd_soc_hw_rate_list[] = {
773           8000,
774          11025,
775          16000,
776          22050,
777          32000,
778          44100,
779          48000,
780          64000,
781          88200,
782          96000,
783         176400,
784         192000,
785 };
786
787 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
788                             unsigned int *list, int list_num,
789                             struct snd_interval *baseline, struct snd_interval *iv)
790 {
791         struct snd_interval p;
792         unsigned int rate;
793         int i;
794
795         snd_interval_any(&p);
796         p.min = UINT_MAX;
797         p.max = 0;
798
799         for (i = 0; i < list_num; i++) {
800
801                 if (!snd_interval_test(iv, list[i]))
802                         continue;
803
804                 rate = rsnd_ssi_clk_query(rdai,
805                                           baseline->min, list[i], NULL);
806                 if (rate > 0) {
807                         p.min = min(p.min, list[i]);
808                         p.max = max(p.max, list[i]);
809                 }
810
811                 rate = rsnd_ssi_clk_query(rdai,
812                                           baseline->max, list[i], NULL);
813                 if (rate > 0) {
814                         p.min = min(p.min, list[i]);
815                         p.max = max(p.max, list[i]);
816                 }
817         }
818
819         return snd_interval_refine(iv, &p);
820 }
821
822 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
823                                  struct snd_pcm_hw_rule *rule)
824 {
825         struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
826         struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
827         struct snd_interval ic;
828         struct rsnd_dai_stream *io = rule->private;
829         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
830
831         /*
832          * possible sampling rate limitation is same as
833          * 2ch if it supports multi ssi
834          * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
835          */
836         ic = *ic_;
837         ic.min =
838         ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
839
840         return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
841                                 ARRAY_SIZE(rsnd_soc_hw_rate_list),
842                                 &ic, ir);
843 }
844
845 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
846                                      struct snd_pcm_hw_rule *rule)
847 {
848         struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
849         struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
850         struct snd_interval ic;
851         struct rsnd_dai_stream *io = rule->private;
852         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
853
854         /*
855          * possible sampling rate limitation is same as
856          * 2ch if it supports multi ssi
857          * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
858          */
859         ic = *ic_;
860         ic.min =
861         ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
862
863         return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
864                                 ARRAY_SIZE(rsnd_soc_hw_channels_list),
865                                 ir, &ic);
866 }
867
868 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
869         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
870                         SNDRV_PCM_INFO_MMAP             |
871                         SNDRV_PCM_INFO_MMAP_VALID,
872         .buffer_bytes_max       = 64 * 1024,
873         .period_bytes_min       = 32,
874         .period_bytes_max       = 8192,
875         .periods_min            = 1,
876         .periods_max            = 32,
877         .fifo_size              = 256,
878 };
879
880 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
881                                 struct snd_soc_dai *dai)
882 {
883         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
884         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
885         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
886         struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
887         struct snd_pcm_runtime *runtime = substream->runtime;
888         unsigned int max_channels = rsnd_rdai_channels_get(rdai);
889         int ret;
890         int i;
891
892         rsnd_dai_stream_init(io, substream);
893
894         /*
895          * Channel Limitation
896          * It depends on Platform design
897          */
898         constraint->list        = rsnd_soc_hw_channels_list;
899         constraint->count       = 0;
900         constraint->mask        = 0;
901
902         for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
903                 if (rsnd_soc_hw_channels_list[i] > max_channels)
904                         break;
905                 constraint->count = i + 1;
906         }
907
908         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
909
910         snd_pcm_hw_constraint_list(runtime, 0,
911                                    SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
912
913         snd_pcm_hw_constraint_integer(runtime,
914                                       SNDRV_PCM_HW_PARAM_PERIODS);
915
916         /*
917          * Sampling Rate / Channel Limitation
918          * It depends on Clock Master Mode
919          */
920         if (rsnd_rdai_is_clk_master(rdai)) {
921                 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
922
923                 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
924                                     rsnd_soc_hw_rule_rate,
925                                     is_play ? &rdai->playback : &rdai->capture,
926                                     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
927                 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
928                                     rsnd_soc_hw_rule_channels,
929                                     is_play ? &rdai->playback : &rdai->capture,
930                                     SNDRV_PCM_HW_PARAM_RATE, -1);
931         }
932
933         /*
934          * call rsnd_dai_call without spinlock
935          */
936         ret = rsnd_dai_call(nolock_start, io, priv);
937         if (ret < 0)
938                 rsnd_dai_call(nolock_stop, io, priv);
939
940         return ret;
941 }
942
943 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
944                                   struct snd_soc_dai *dai)
945 {
946         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
947         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
948         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
949
950         /*
951          * call rsnd_dai_call without spinlock
952          */
953         rsnd_dai_call(nolock_stop, io, priv);
954
955         rsnd_dai_stream_quit(io);
956 }
957
958 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
959                                 struct snd_soc_dai *dai)
960 {
961         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
962         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
963         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
964
965         return rsnd_dai_call(prepare, io, priv);
966 }
967
968 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
969         .startup        = rsnd_soc_dai_startup,
970         .shutdown       = rsnd_soc_dai_shutdown,
971         .trigger        = rsnd_soc_dai_trigger,
972         .set_fmt        = rsnd_soc_dai_set_fmt,
973         .set_tdm_slot   = rsnd_soc_set_dai_tdm_slot,
974         .prepare        = rsnd_soc_dai_prepare,
975 };
976
977 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
978                 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
979                 struct device_node *node,
980                 struct device_node *playback,
981                 struct device_node *capture)
982 {
983         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
984         struct device_node *np;
985         struct rsnd_mod *mod;
986         int i;
987
988         if (!node)
989                 return;
990
991         i = 0;
992         for_each_child_of_node(node, np) {
993                 mod = mod_get(priv, i);
994                 if (np == playback)
995                         rsnd_dai_connect(mod, &rdai->playback, mod->type);
996                 if (np == capture)
997                         rsnd_dai_connect(mod, &rdai->capture, mod->type);
998                 i++;
999         }
1000
1001         of_node_put(node);
1002 }
1003
1004 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1005                                             int *is_graph)
1006 {
1007         struct device *dev = rsnd_priv_to_dev(priv);
1008         struct device_node *np = dev->of_node;
1009         struct device_node *dai_node;
1010         struct device_node *ret;
1011
1012         *is_graph = 0;
1013
1014         /*
1015          * parse both previous dai (= rcar_sound,dai), and
1016          * graph dai (= ports/port)
1017          */
1018         dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1019         if (dai_node) {
1020                 ret = dai_node;
1021                 goto of_node_compatible;
1022         }
1023
1024         ret = np;
1025
1026         dai_node = of_graph_get_next_endpoint(np, NULL);
1027         if (dai_node)
1028                 goto of_node_graph;
1029
1030         return NULL;
1031
1032 of_node_graph:
1033         *is_graph = 1;
1034 of_node_compatible:
1035         of_node_put(dai_node);
1036
1037         return ret;
1038 }
1039
1040 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1041                              struct device_node *dai_np,
1042                              int dai_i)
1043 {
1044         struct device_node *playback, *capture;
1045         struct rsnd_dai_stream *io_playback;
1046         struct rsnd_dai_stream *io_capture;
1047         struct snd_soc_dai_driver *drv;
1048         struct rsnd_dai *rdai;
1049         struct device *dev = rsnd_priv_to_dev(priv);
1050         int io_i;
1051
1052         rdai            = rsnd_rdai_get(priv, dai_i);
1053         drv             = rsnd_daidrv_get(priv, dai_i);
1054         io_playback     = &rdai->playback;
1055         io_capture      = &rdai->capture;
1056
1057         snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1058
1059         rdai->priv      = priv;
1060         drv->name       = rdai->name;
1061         drv->ops        = &rsnd_soc_dai_ops;
1062
1063         snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1064                  "DAI%d Playback", dai_i);
1065         drv->playback.rates             = RSND_RATES;
1066         drv->playback.formats           = RSND_FMTS;
1067         drv->playback.channels_min      = 2;
1068         drv->playback.channels_max      = 8;
1069         drv->playback.stream_name       = rdai->playback.name;
1070
1071         snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1072                  "DAI%d Capture", dai_i);
1073         drv->capture.rates              = RSND_RATES;
1074         drv->capture.formats            = RSND_FMTS;
1075         drv->capture.channels_min       = 2;
1076         drv->capture.channels_max       = 8;
1077         drv->capture.stream_name        = rdai->capture.name;
1078
1079         rdai->playback.rdai             = rdai;
1080         rdai->capture.rdai              = rdai;
1081         rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1082         rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1083         rsnd_rdai_width_set(rdai, 32);   /* default 32bit width */
1084
1085         for (io_i = 0;; io_i++) {
1086                 playback = of_parse_phandle(dai_np, "playback", io_i);
1087                 capture  = of_parse_phandle(dai_np, "capture", io_i);
1088
1089                 if (!playback && !capture)
1090                         break;
1091
1092                 rsnd_parse_connect_ssi(rdai, playback, capture);
1093                 rsnd_parse_connect_src(rdai, playback, capture);
1094                 rsnd_parse_connect_ctu(rdai, playback, capture);
1095                 rsnd_parse_connect_mix(rdai, playback, capture);
1096                 rsnd_parse_connect_dvc(rdai, playback, capture);
1097
1098                 of_node_put(playback);
1099                 of_node_put(capture);
1100         }
1101
1102         if (rsnd_ssi_is_pin_sharing(io_capture) ||
1103             rsnd_ssi_is_pin_sharing(io_playback)) {
1104                 /* should have symmetric_rates if pin sharing */
1105                 drv->symmetric_rates = 1;
1106         }
1107
1108         dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1109                 rsnd_io_to_mod_ssi(io_playback) ? "play"    : " -- ",
1110                 rsnd_io_to_mod_ssi(io_capture) ? "capture" : "  --   ");
1111 }
1112
1113 static int rsnd_dai_probe(struct rsnd_priv *priv)
1114 {
1115         struct device_node *dai_node;
1116         struct device_node *dai_np;
1117         struct snd_soc_dai_driver *rdrv;
1118         struct device *dev = rsnd_priv_to_dev(priv);
1119         struct rsnd_dai *rdai;
1120         int nr;
1121         int is_graph;
1122         int dai_i;
1123
1124         dai_node = rsnd_dai_of_node(priv, &is_graph);
1125         if (is_graph)
1126                 nr = of_graph_get_endpoint_count(dai_node);
1127         else
1128                 nr = of_get_child_count(dai_node);
1129
1130         if (!nr)
1131                 return -EINVAL;
1132
1133         rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1134         rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1135         if (!rdrv || !rdai)
1136                 return -ENOMEM;
1137
1138         priv->rdai_nr   = nr;
1139         priv->daidrv    = rdrv;
1140         priv->rdai      = rdai;
1141
1142         /*
1143          * parse all dai
1144          */
1145         dai_i = 0;
1146         if (is_graph) {
1147                 for_each_endpoint_of_node(dai_node, dai_np) {
1148                         __rsnd_dai_probe(priv, dai_np, dai_i);
1149                         rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
1150                         dai_i++;
1151                 }
1152         } else {
1153                 for_each_child_of_node(dai_node, dai_np)
1154                         __rsnd_dai_probe(priv, dai_np, dai_i++);
1155         }
1156
1157         return 0;
1158 }
1159
1160 /*
1161  *              pcm ops
1162  */
1163 static int rsnd_hw_params(struct snd_pcm_substream *substream,
1164                          struct snd_pcm_hw_params *hw_params)
1165 {
1166         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1167         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1168         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1169         int ret;
1170
1171         ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1172         if (ret)
1173                 return ret;
1174
1175         return snd_pcm_lib_malloc_pages(substream,
1176                                         params_buffer_bytes(hw_params));
1177 }
1178
1179 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
1180 {
1181         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1182         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1183         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1184         snd_pcm_uframes_t pointer = 0;
1185
1186         rsnd_dai_call(pointer, io, &pointer);
1187
1188         return pointer;
1189 }
1190
1191 static const struct snd_pcm_ops rsnd_pcm_ops = {
1192         .ioctl          = snd_pcm_lib_ioctl,
1193         .hw_params      = rsnd_hw_params,
1194         .hw_free        = snd_pcm_lib_free_pages,
1195         .pointer        = rsnd_pointer,
1196 };
1197
1198 /*
1199  *              snd_kcontrol
1200  */
1201 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1202                            struct snd_ctl_elem_info *uinfo)
1203 {
1204         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1205
1206         if (cfg->texts) {
1207                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1208                 uinfo->count = cfg->size;
1209                 uinfo->value.enumerated.items = cfg->max;
1210                 if (uinfo->value.enumerated.item >= cfg->max)
1211                         uinfo->value.enumerated.item = cfg->max - 1;
1212                 strlcpy(uinfo->value.enumerated.name,
1213                         cfg->texts[uinfo->value.enumerated.item],
1214                         sizeof(uinfo->value.enumerated.name));
1215         } else {
1216                 uinfo->count = cfg->size;
1217                 uinfo->value.integer.min = 0;
1218                 uinfo->value.integer.max = cfg->max;
1219                 uinfo->type = (cfg->max == 1) ?
1220                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1221                         SNDRV_CTL_ELEM_TYPE_INTEGER;
1222         }
1223
1224         return 0;
1225 }
1226
1227 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1228                           struct snd_ctl_elem_value *uc)
1229 {
1230         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1231         int i;
1232
1233         for (i = 0; i < cfg->size; i++)
1234                 if (cfg->texts)
1235                         uc->value.enumerated.item[i] = cfg->val[i];
1236                 else
1237                         uc->value.integer.value[i] = cfg->val[i];
1238
1239         return 0;
1240 }
1241
1242 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1243                           struct snd_ctl_elem_value *uc)
1244 {
1245         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1246         int i, change = 0;
1247
1248         if (!cfg->accept(cfg->io))
1249                 return 0;
1250
1251         for (i = 0; i < cfg->size; i++) {
1252                 if (cfg->texts) {
1253                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1254                         cfg->val[i] = uc->value.enumerated.item[i];
1255                 } else {
1256                         change |= (uc->value.integer.value[i] != cfg->val[i]);
1257                         cfg->val[i] = uc->value.integer.value[i];
1258                 }
1259         }
1260
1261         if (change && cfg->update)
1262                 cfg->update(cfg->io, cfg->mod);
1263
1264         return change;
1265 }
1266
1267 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1268 {
1269         return 1;
1270 }
1271
1272 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1273 {
1274         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1275         struct rsnd_priv *priv = rsnd_io_to_priv(io);
1276         struct device *dev = rsnd_priv_to_dev(priv);
1277
1278         if (!runtime) {
1279                 dev_warn(dev, "Can't update kctrl when idle\n");
1280                 return 0;
1281         }
1282
1283         return 1;
1284 }
1285
1286 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1287 {
1288         cfg->cfg.val = cfg->val;
1289
1290         return &cfg->cfg;
1291 }
1292
1293 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1294 {
1295         cfg->cfg.val = &cfg->val;
1296
1297         return &cfg->cfg;
1298 }
1299
1300 const char * const volume_ramp_rate[] = {
1301         "128 dB/1 step",         /* 00000 */
1302         "64 dB/1 step",          /* 00001 */
1303         "32 dB/1 step",          /* 00010 */
1304         "16 dB/1 step",          /* 00011 */
1305         "8 dB/1 step",           /* 00100 */
1306         "4 dB/1 step",           /* 00101 */
1307         "2 dB/1 step",           /* 00110 */
1308         "1 dB/1 step",           /* 00111 */
1309         "0.5 dB/1 step",         /* 01000 */
1310         "0.25 dB/1 step",        /* 01001 */
1311         "0.125 dB/1 step",       /* 01010 = VOLUME_RAMP_MAX_MIX */
1312         "0.125 dB/2 steps",      /* 01011 */
1313         "0.125 dB/4 steps",      /* 01100 */
1314         "0.125 dB/8 steps",      /* 01101 */
1315         "0.125 dB/16 steps",     /* 01110 */
1316         "0.125 dB/32 steps",     /* 01111 */
1317         "0.125 dB/64 steps",     /* 10000 */
1318         "0.125 dB/128 steps",    /* 10001 */
1319         "0.125 dB/256 steps",    /* 10010 */
1320         "0.125 dB/512 steps",    /* 10011 */
1321         "0.125 dB/1024 steps",   /* 10100 */
1322         "0.125 dB/2048 steps",   /* 10101 */
1323         "0.125 dB/4096 steps",   /* 10110 */
1324         "0.125 dB/8192 steps",   /* 10111 = VOLUME_RAMP_MAX_DVC */
1325 };
1326
1327 int rsnd_kctrl_new(struct rsnd_mod *mod,
1328                    struct rsnd_dai_stream *io,
1329                    struct snd_soc_pcm_runtime *rtd,
1330                    const unsigned char *name,
1331                    int (*accept)(struct rsnd_dai_stream *io),
1332                    void (*update)(struct rsnd_dai_stream *io,
1333                                   struct rsnd_mod *mod),
1334                    struct rsnd_kctrl_cfg *cfg,
1335                    const char * const *texts,
1336                    int size,
1337                    u32 max)
1338 {
1339         struct snd_card *card = rtd->card->snd_card;
1340         struct snd_kcontrol *kctrl;
1341         struct snd_kcontrol_new knew = {
1342                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
1343                 .name           = name,
1344                 .info           = rsnd_kctrl_info,
1345                 .index          = rtd->num,
1346                 .get            = rsnd_kctrl_get,
1347                 .put            = rsnd_kctrl_put,
1348         };
1349         int ret;
1350
1351         if (size > RSND_MAX_CHANNELS)
1352                 return -EINVAL;
1353
1354         kctrl = snd_ctl_new1(&knew, cfg);
1355         if (!kctrl)
1356                 return -ENOMEM;
1357
1358         ret = snd_ctl_add(card, kctrl);
1359         if (ret < 0)
1360                 return ret;
1361
1362         cfg->texts      = texts;
1363         cfg->max        = max;
1364         cfg->size       = size;
1365         cfg->accept     = accept;
1366         cfg->update     = update;
1367         cfg->card       = card;
1368         cfg->kctrl      = kctrl;
1369         cfg->io         = io;
1370         cfg->mod        = mod;
1371
1372         return 0;
1373 }
1374
1375 /*
1376  *              snd_soc_component
1377  */
1378
1379 #define PREALLOC_BUFFER         (32 * 1024)
1380 #define PREALLOC_BUFFER_MAX     (32 * 1024)
1381
1382 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1383                                   struct rsnd_dai_stream *io,
1384                                   int stream)
1385 {
1386         struct rsnd_priv *priv = rsnd_io_to_priv(io);
1387         struct device *dev = rsnd_priv_to_dev(priv);
1388         struct snd_pcm_substream *substream;
1389         int err;
1390
1391         /*
1392          * use Audio-DMAC dev if we can use IPMMU
1393          * see
1394          *      rsnd_dmaen_attach()
1395          */
1396         if (io->dmac_dev)
1397                 dev = io->dmac_dev;
1398
1399         for (substream = rtd->pcm->streams[stream].substream;
1400              substream;
1401              substream = substream->next) {
1402                 err = snd_pcm_lib_preallocate_pages(substream,
1403                                         SNDRV_DMA_TYPE_DEV,
1404                                         dev,
1405                                         PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1406                 if (err < 0)
1407                         return err;
1408         }
1409
1410         return 0;
1411 }
1412
1413 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1414 {
1415         struct snd_soc_dai *dai = rtd->cpu_dai;
1416         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1417         int ret;
1418
1419         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1420         if (ret)
1421                 return ret;
1422
1423         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1424         if (ret)
1425                 return ret;
1426
1427         ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1428                                      SNDRV_PCM_STREAM_PLAYBACK);
1429         if (ret)
1430                 return ret;
1431
1432         ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1433                                      SNDRV_PCM_STREAM_CAPTURE);
1434         if (ret)
1435                 return ret;
1436
1437         return 0;
1438 }
1439
1440 static const struct snd_soc_component_driver rsnd_soc_component = {
1441         .ops            = &rsnd_pcm_ops,
1442         .pcm_new        = rsnd_pcm_new,
1443         .name           = "rsnd",
1444 };
1445
1446 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1447                                        struct rsnd_dai_stream *io)
1448 {
1449         int ret;
1450
1451         ret = rsnd_dai_call(probe, io, priv);
1452         if (ret == -EAGAIN) {
1453                 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1454                 struct rsnd_mod *mod;
1455                 int i;
1456
1457                 /*
1458                  * Fallback to PIO mode
1459                  */
1460
1461                 /*
1462                  * call "remove" for SSI/SRC/DVC
1463                  * SSI will be switch to PIO mode if it was DMA mode
1464                  * see
1465                  *      rsnd_dma_init()
1466                  *      rsnd_ssi_fallback()
1467                  */
1468                 rsnd_dai_call(remove, io, priv);
1469
1470                 /*
1471                  * remove all mod from io
1472                  * and, re connect ssi
1473                  */
1474                 for_each_rsnd_mod(i, mod, io)
1475                         rsnd_dai_disconnect(mod, io, i);
1476                 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1477
1478                 /*
1479                  * fallback
1480                  */
1481                 rsnd_dai_call(fallback, io, priv);
1482
1483                 /*
1484                  * retry to "probe".
1485                  * DAI has SSI which is PIO mode only now.
1486                  */
1487                 ret = rsnd_dai_call(probe, io, priv);
1488         }
1489
1490         return ret;
1491 }
1492
1493 /*
1494  *      rsnd probe
1495  */
1496 static int rsnd_probe(struct platform_device *pdev)
1497 {
1498         struct rsnd_priv *priv;
1499         struct device *dev = &pdev->dev;
1500         struct rsnd_dai *rdai;
1501         int (*probe_func[])(struct rsnd_priv *priv) = {
1502                 rsnd_gen_probe,
1503                 rsnd_dma_probe,
1504                 rsnd_ssi_probe,
1505                 rsnd_ssiu_probe,
1506                 rsnd_src_probe,
1507                 rsnd_ctu_probe,
1508                 rsnd_mix_probe,
1509                 rsnd_dvc_probe,
1510                 rsnd_cmd_probe,
1511                 rsnd_adg_probe,
1512                 rsnd_dai_probe,
1513         };
1514         int ret, i;
1515
1516         /*
1517          *      init priv data
1518          */
1519         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1520         if (!priv)
1521                 return -ENODEV;
1522
1523         priv->pdev      = pdev;
1524         priv->flags     = (unsigned long)of_device_get_match_data(dev);
1525         spin_lock_init(&priv->lock);
1526
1527         /*
1528          *      init each module
1529          */
1530         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1531                 ret = probe_func[i](priv);
1532                 if (ret)
1533                         return ret;
1534         }
1535
1536         for_each_rsnd_dai(rdai, priv, i) {
1537                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1538                 if (ret)
1539                         goto exit_snd_probe;
1540
1541                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1542                 if (ret)
1543                         goto exit_snd_probe;
1544         }
1545
1546         dev_set_drvdata(dev, priv);
1547
1548         /*
1549          *      asoc register
1550          */
1551         ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1552                                          priv->daidrv, rsnd_rdai_nr(priv));
1553         if (ret < 0) {
1554                 dev_err(dev, "cannot snd dai register\n");
1555                 goto exit_snd_probe;
1556         }
1557
1558         pm_runtime_enable(dev);
1559
1560         dev_info(dev, "probed\n");
1561         return ret;
1562
1563 exit_snd_probe:
1564         for_each_rsnd_dai(rdai, priv, i) {
1565                 rsnd_dai_call(remove, &rdai->playback, priv);
1566                 rsnd_dai_call(remove, &rdai->capture, priv);
1567         }
1568
1569         return ret;
1570 }
1571
1572 static int rsnd_remove(struct platform_device *pdev)
1573 {
1574         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1575         struct rsnd_dai *rdai;
1576         void (*remove_func[])(struct rsnd_priv *priv) = {
1577                 rsnd_ssi_remove,
1578                 rsnd_ssiu_remove,
1579                 rsnd_src_remove,
1580                 rsnd_ctu_remove,
1581                 rsnd_mix_remove,
1582                 rsnd_dvc_remove,
1583                 rsnd_cmd_remove,
1584                 rsnd_adg_remove,
1585         };
1586         int ret = 0, i;
1587
1588         snd_soc_disconnect_sync(&pdev->dev);
1589
1590         pm_runtime_disable(&pdev->dev);
1591
1592         for_each_rsnd_dai(rdai, priv, i) {
1593                 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1594                 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1595         }
1596
1597         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1598                 remove_func[i](priv);
1599
1600         return ret;
1601 }
1602
1603 static int __maybe_unused rsnd_suspend(struct device *dev)
1604 {
1605         struct rsnd_priv *priv = dev_get_drvdata(dev);
1606
1607         rsnd_adg_clk_disable(priv);
1608
1609         return 0;
1610 }
1611
1612 static int __maybe_unused rsnd_resume(struct device *dev)
1613 {
1614         struct rsnd_priv *priv = dev_get_drvdata(dev);
1615
1616         rsnd_adg_clk_enable(priv);
1617
1618         return 0;
1619 }
1620
1621 static const struct dev_pm_ops rsnd_pm_ops = {
1622         SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1623 };
1624
1625 static struct platform_driver rsnd_driver = {
1626         .driver = {
1627                 .name   = "rcar_sound",
1628                 .pm     = &rsnd_pm_ops,
1629                 .of_match_table = rsnd_of_match,
1630         },
1631         .probe          = rsnd_probe,
1632         .remove         = rsnd_remove,
1633 };
1634 module_platform_driver(rsnd_driver);
1635
1636 MODULE_LICENSE("GPL v2");
1637 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1638 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1639 MODULE_ALIAS("platform:rcar-pcm-audio");