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