powerpc/mm: Avoid calling arch_enter/leave_lazy_mmu() in set_ptes
[platform/kernel/linux-starfive.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 #include <linux/pm_runtime.h>
94 #include "rsnd.h"
95
96 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
97 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
98                    SNDRV_PCM_FMTBIT_S16_LE |\
99                    SNDRV_PCM_FMTBIT_S24_LE)
100
101 static const struct of_device_id rsnd_of_match[] = {
102         { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
103         { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
104         { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
105         { .compatible = "renesas,rcar_sound-gen4", .data = (void *)RSND_GEN4 },
106         /* Special Handling */
107         { .compatible = "renesas,rcar_sound-r8a77990", .data = (void *)(RSND_GEN3 | RSND_SOC_E) },
108         {},
109 };
110 MODULE_DEVICE_TABLE(of, rsnd_of_match);
111
112 /*
113  *      rsnd_mod functions
114  */
115 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
116 {
117         if (mod->type != type) {
118                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
119                 struct device *dev = rsnd_priv_to_dev(priv);
120
121                 dev_warn(dev, "%s is not your expected module\n",
122                          rsnd_mod_name(mod));
123         }
124 }
125
126 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
127                                   struct rsnd_mod *mod)
128 {
129         if (!mod || !mod->ops || !mod->ops->dma_req)
130                 return NULL;
131
132         return mod->ops->dma_req(io, mod);
133 }
134
135 #define MOD_NAME_NUM   5
136 #define MOD_NAME_SIZE 16
137 char *rsnd_mod_name(struct rsnd_mod *mod)
138 {
139         static char names[MOD_NAME_NUM][MOD_NAME_SIZE];
140         static int num;
141         char *name = names[num];
142
143         num++;
144         if (num >= MOD_NAME_NUM)
145                 num = 0;
146
147         /*
148          * Let's use same char to avoid pointlessness memory
149          * Thus, rsnd_mod_name() should be used immediately
150          * Don't keep pointer
151          */
152         if ((mod)->ops->id_sub) {
153                 snprintf(name, MOD_NAME_SIZE, "%s[%d%d]",
154                          mod->ops->name,
155                          rsnd_mod_id(mod),
156                          rsnd_mod_id_sub(mod));
157         } else {
158                 snprintf(name, MOD_NAME_SIZE, "%s[%d]",
159                          mod->ops->name,
160                          rsnd_mod_id(mod));
161         }
162
163         return name;
164 }
165
166 u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
167                          struct rsnd_dai_stream *io,
168                          enum rsnd_mod_type type)
169 {
170         return &mod->status;
171 }
172
173 int rsnd_mod_id_raw(struct rsnd_mod *mod)
174 {
175         return mod->id;
176 }
177
178 int rsnd_mod_id(struct rsnd_mod *mod)
179 {
180         if ((mod)->ops->id)
181                 return (mod)->ops->id(mod);
182
183         return rsnd_mod_id_raw(mod);
184 }
185
186 int rsnd_mod_id_sub(struct rsnd_mod *mod)
187 {
188         if ((mod)->ops->id_sub)
189                 return (mod)->ops->id_sub(mod);
190
191         return 0;
192 }
193
194 int rsnd_mod_init(struct rsnd_priv *priv,
195                   struct rsnd_mod *mod,
196                   struct rsnd_mod_ops *ops,
197                   struct clk *clk,
198                   enum rsnd_mod_type type,
199                   int id)
200 {
201         int ret = clk_prepare(clk);
202
203         if (ret)
204                 return ret;
205
206         mod->id         = id;
207         mod->ops        = ops;
208         mod->type       = type;
209         mod->clk        = clk;
210         mod->priv       = priv;
211
212         return 0;
213 }
214
215 void rsnd_mod_quit(struct rsnd_mod *mod)
216 {
217         clk_unprepare(mod->clk);
218         mod->clk = NULL;
219 }
220
221 void rsnd_mod_interrupt(struct rsnd_mod *mod,
222                         void (*callback)(struct rsnd_mod *mod,
223                                          struct rsnd_dai_stream *io))
224 {
225         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
226         struct rsnd_dai *rdai;
227         int i;
228
229         for_each_rsnd_dai(rdai, priv, i) {
230                 struct rsnd_dai_stream *io = &rdai->playback;
231
232                 if (mod == io->mod[mod->type])
233                         callback(mod, io);
234
235                 io = &rdai->capture;
236                 if (mod == io->mod[mod->type])
237                         callback(mod, io);
238         }
239 }
240
241 int rsnd_io_is_working(struct rsnd_dai_stream *io)
242 {
243         /* see rsnd_dai_stream_init/quit() */
244         if (io->substream)
245                 return snd_pcm_running(io->substream);
246
247         return 0;
248 }
249
250 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
251                                               struct snd_pcm_hw_params *params)
252 {
253         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
254
255         /*
256          * params will be added when refine
257          * see
258          *      __rsnd_soc_hw_rule_rate()
259          *      __rsnd_soc_hw_rule_channels()
260          */
261         if (params)
262                 return params_channels(params);
263         else if (runtime)
264                 return runtime->channels;
265         return 0;
266 }
267
268 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
269                                                struct snd_pcm_hw_params *params)
270 {
271         int chan = rsnd_runtime_channel_original_with_params(io, params);
272         struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
273
274         if (ctu_mod) {
275                 u32 converted_chan = rsnd_io_converted_chan(io);
276
277                 /*
278                  * !! Note !!
279                  *
280                  * converted_chan will be used for CTU,
281                  * or TDM Split mode.
282                  * User shouldn't use CTU with TDM Split mode.
283                  */
284                 if (rsnd_runtime_is_tdm_split(io)) {
285                         struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
286
287                         dev_err(dev, "CTU and TDM Split should be used\n");
288                 }
289
290                 if (converted_chan)
291                         return converted_chan;
292         }
293
294         return chan;
295 }
296
297 int rsnd_channel_normalization(int chan)
298 {
299         if (WARN_ON((chan > 8) || (chan < 0)))
300                 return 0;
301
302         /* TDM Extend Mode needs 8ch */
303         if (chan == 6)
304                 chan = 8;
305
306         return chan;
307 }
308
309 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
310                                              struct snd_pcm_hw_params *params)
311 {
312         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
313         int chan = rsnd_io_is_play(io) ?
314                 rsnd_runtime_channel_after_ctu_with_params(io, params) :
315                 rsnd_runtime_channel_original_with_params(io, params);
316
317         /* Use Multi SSI */
318         if (rsnd_runtime_is_multi_ssi(io))
319                 chan /= rsnd_rdai_ssi_lane_get(rdai);
320
321         return rsnd_channel_normalization(chan);
322 }
323
324 int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io)
325 {
326         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
327         int lane = rsnd_rdai_ssi_lane_get(rdai);
328         int chan = rsnd_io_is_play(io) ?
329                 rsnd_runtime_channel_after_ctu(io) :
330                 rsnd_runtime_channel_original(io);
331
332         return (chan > 2) && (lane > 1);
333 }
334
335 int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io)
336 {
337         return rsnd_runtime_channel_for_ssi(io) >= 6;
338 }
339
340 int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io)
341 {
342         return !!rsnd_flags_has(io, RSND_STREAM_TDM_SPLIT);
343 }
344
345 /*
346  *      ADINR function
347  */
348 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
349 {
350         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
351         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
352         struct device *dev = rsnd_priv_to_dev(priv);
353
354         switch (snd_pcm_format_width(runtime->format)) {
355         case 8:
356                 return 16 << 16;
357         case 16:
358                 return 8 << 16;
359         case 24:
360                 return 0 << 16;
361         }
362
363         dev_warn(dev, "not supported sample bits\n");
364
365         return 0;
366 }
367
368 /*
369  *      DALIGN function
370  */
371 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
372 {
373         static const u32 dalign_values[8] = {
374                 0x76543210, 0x00000032, 0x00007654, 0x00000076,
375                 0xfedcba98, 0x000000ba, 0x0000fedc, 0x000000fe,
376         };
377         int id = 0;
378         struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
379         struct rsnd_mod *target;
380         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
381         u32 dalign;
382
383         /*
384          * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
385          *          31..16 15...0
386          *      HW: [L ch] [R ch]
387          *      SW: [R ch] [L ch]
388          * We need to care about inversion timing to control
389          * Playback/Capture correctly.
390          * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
391          *
392          * sL/R : software L/R
393          * hL/R : hardware L/R
394          * (*)  : conversion timing
395          *
396          * Playback
397          *           sL/R (*) hL/R     hL/R     hL/R      hL/R     hL/R
398          *      [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
399          *
400          * Capture
401          *           hL/R     hL/R      hL/R     hL/R     hL/R (*) sL/R
402          *      codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
403          */
404         if (rsnd_io_is_play(io)) {
405                 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
406
407                 target = src ? src : ssiu;
408         } else {
409                 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
410
411                 target = cmd ? cmd : ssiu;
412         }
413
414         if (mod == ssiu)
415                 id = rsnd_mod_id_sub(mod);
416
417         dalign = dalign_values[id];
418
419         if (mod == target && snd_pcm_format_width(runtime->format) == 16) {
420                 /* Target mod needs inverted DALIGN when 16bit */
421                 dalign = (dalign & 0xf0f0f0f0) >> 4 |
422                          (dalign & 0x0f0f0f0f) << 4;
423         }
424
425         return dalign;
426 }
427
428 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
429 {
430         static const enum rsnd_mod_type playback_mods[] = {
431                 RSND_MOD_SRC,
432                 RSND_MOD_CMD,
433                 RSND_MOD_SSIU,
434         };
435         static const enum rsnd_mod_type capture_mods[] = {
436                 RSND_MOD_CMD,
437                 RSND_MOD_SRC,
438                 RSND_MOD_SSIU,
439         };
440         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
441         struct rsnd_mod *tmod = NULL;
442         const enum rsnd_mod_type *mods =
443                 rsnd_io_is_play(io) ?
444                 playback_mods : capture_mods;
445         int i;
446
447         /*
448          * This is needed for 24bit data
449          * We need to shift 8bit
450          *
451          * Linux 24bit data is located as 0x00******
452          * HW    24bit data is located as 0x******00
453          *
454          */
455         if (snd_pcm_format_width(runtime->format) != 24)
456                 return 0;
457
458         for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
459                 tmod = rsnd_io_to_mod(io, mods[i]);
460                 if (tmod)
461                         break;
462         }
463
464         if (tmod != mod)
465                 return 0;
466
467         if (rsnd_io_is_play(io))
468                 return  (0 << 20) | /* shift to Left */
469                         (8 << 16);  /* 8bit */
470         else
471                 return  (1 << 20) | /* shift to Right */
472                         (8 << 16);  /* 8bit */
473 }
474
475 /*
476  *      rsnd_dai functions
477  */
478 struct rsnd_mod *rsnd_mod_next(int *iterator,
479                                struct rsnd_dai_stream *io,
480                                enum rsnd_mod_type *array,
481                                int array_size)
482 {
483         int max = array ? array_size : RSND_MOD_MAX;
484
485         for (; *iterator < max; (*iterator)++) {
486                 enum rsnd_mod_type type = (array) ? array[*iterator] : *iterator;
487                 struct rsnd_mod *mod = rsnd_io_to_mod(io, type);
488
489                 if (mod)
490                         return mod;
491         }
492
493         return NULL;
494 }
495
496 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
497         {
498                 /* CAPTURE */
499                 RSND_MOD_AUDMAPP,
500                 RSND_MOD_AUDMA,
501                 RSND_MOD_DVC,
502                 RSND_MOD_MIX,
503                 RSND_MOD_CTU,
504                 RSND_MOD_CMD,
505                 RSND_MOD_SRC,
506                 RSND_MOD_SSIU,
507                 RSND_MOD_SSIM3,
508                 RSND_MOD_SSIM2,
509                 RSND_MOD_SSIM1,
510                 RSND_MOD_SSIP,
511                 RSND_MOD_SSI,
512         }, {
513                 /* PLAYBACK */
514                 RSND_MOD_AUDMAPP,
515                 RSND_MOD_AUDMA,
516                 RSND_MOD_SSIM3,
517                 RSND_MOD_SSIM2,
518                 RSND_MOD_SSIM1,
519                 RSND_MOD_SSIP,
520                 RSND_MOD_SSI,
521                 RSND_MOD_SSIU,
522                 RSND_MOD_DVC,
523                 RSND_MOD_MIX,
524                 RSND_MOD_CTU,
525                 RSND_MOD_CMD,
526                 RSND_MOD_SRC,
527         },
528 };
529
530 static int rsnd_status_update(struct rsnd_dai_stream *io,
531                               struct rsnd_mod *mod, enum rsnd_mod_type type,
532                               int shift, int add, int timing)
533 {
534         u32 *status     = mod->ops->get_status(mod, io, type);
535         u32 mask        = 0xF << shift;
536         u8 val          = (*status >> shift) & 0xF;
537         u8 next_val     = (val + add) & 0xF;
538         int func_call   = (val == timing);
539
540         /* no status update */
541         if (add == 0 || shift == 28)
542                 return 1;
543
544         if (next_val == 0xF) /* underflow case */
545                 func_call = -1;
546         else
547                 *status = (*status & ~mask) + (next_val << shift);
548
549         return func_call;
550 }
551
552 #define rsnd_dai_call(fn, io, param...)                                 \
553 ({                                                                      \
554         struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));     \
555         struct rsnd_mod *mod;                                           \
556         int is_play = rsnd_io_is_play(io);                              \
557         int ret = 0, i;                                                 \
558         enum rsnd_mod_type *types = rsnd_mod_sequence[is_play];         \
559         for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) {     \
560                 int tmp = 0;                                            \
561                 int func_call = rsnd_status_update(io, mod, types[i],   \
562                                                 __rsnd_mod_shift_##fn,  \
563                                                 __rsnd_mod_add_##fn,    \
564                                                 __rsnd_mod_call_##fn);  \
565                 if (func_call > 0 && (mod)->ops->fn)                    \
566                         tmp = (mod)->ops->fn(mod, io, param);           \
567                 if (unlikely(func_call < 0) ||                          \
568                     unlikely(tmp && (tmp != -EPROBE_DEFER)))            \
569                         dev_err(dev, "%s : %s error (%d, %d)\n",        \
570                                 rsnd_mod_name(mod), #fn, tmp, func_call);\
571                 ret |= tmp;                                             \
572         }                                                               \
573         ret;                                                            \
574 })
575
576 int rsnd_dai_connect(struct rsnd_mod *mod,
577                      struct rsnd_dai_stream *io,
578                      enum rsnd_mod_type type)
579 {
580         struct rsnd_priv *priv;
581         struct device *dev;
582
583         if (!mod)
584                 return -EIO;
585
586         if (io->mod[type] == mod)
587                 return 0;
588
589         if (io->mod[type])
590                 return -EINVAL;
591
592         priv = rsnd_mod_to_priv(mod);
593         dev = rsnd_priv_to_dev(priv);
594
595         io->mod[type] = mod;
596
597         dev_dbg(dev, "%s is connected to io (%s)\n",
598                 rsnd_mod_name(mod),
599                 rsnd_io_is_play(io) ? "Playback" : "Capture");
600
601         return 0;
602 }
603
604 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
605                                 struct rsnd_dai_stream *io,
606                                 enum rsnd_mod_type type)
607 {
608         io->mod[type] = NULL;
609 }
610
611 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
612                             int max_channels)
613 {
614         if (max_channels > 0)
615                 rdai->max_channels = max_channels;
616
617         return rdai->max_channels;
618 }
619
620 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
621                             int ssi_lane)
622 {
623         if (ssi_lane > 0)
624                 rdai->ssi_lane = ssi_lane;
625
626         return rdai->ssi_lane;
627 }
628
629 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
630 {
631         if (width > 0)
632                 rdai->chan_width = width;
633
634         return rdai->chan_width;
635 }
636
637 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
638 {
639         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
640                 return NULL;
641
642         return priv->rdai + id;
643 }
644
645 static struct snd_soc_dai_driver
646 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
647 {
648         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
649                 return NULL;
650
651         return priv->daidrv + id;
652 }
653
654 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
655 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
656 {
657         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
658
659         return rsnd_rdai_get(priv, dai->id);
660 }
661
662 /*
663  *      rsnd_soc_dai functions
664  */
665 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
666 {
667         struct snd_pcm_substream *substream = io->substream;
668
669         /*
670          * this function should be called...
671          *
672          * - if rsnd_dai_pointer_update() returns true
673          * - without spin lock
674          */
675
676         snd_pcm_period_elapsed(substream);
677 }
678
679 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
680                                 struct snd_pcm_substream *substream)
681 {
682         io->substream           = substream;
683 }
684
685 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
686 {
687         io->substream           = NULL;
688 }
689
690 static
691 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
692 {
693         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
694
695         return  asoc_rtd_to_cpu(rtd, 0);
696 }
697
698 static
699 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
700                                         struct snd_pcm_substream *substream)
701 {
702         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
703                 return &rdai->playback;
704         else
705                 return &rdai->capture;
706 }
707
708 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
709                             struct snd_soc_dai *dai)
710 {
711         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
712         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
713         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
714         int ret;
715         unsigned long flags;
716
717         spin_lock_irqsave(&priv->lock, flags);
718
719         switch (cmd) {
720         case SNDRV_PCM_TRIGGER_START:
721         case SNDRV_PCM_TRIGGER_RESUME:
722                 ret = rsnd_dai_call(init, io, priv);
723                 if (ret < 0)
724                         goto dai_trigger_end;
725
726                 ret = rsnd_dai_call(start, io, priv);
727                 if (ret < 0)
728                         goto dai_trigger_end;
729
730                 ret = rsnd_dai_call(irq, io, priv, 1);
731                 if (ret < 0)
732                         goto dai_trigger_end;
733
734                 break;
735         case SNDRV_PCM_TRIGGER_STOP:
736         case SNDRV_PCM_TRIGGER_SUSPEND:
737                 ret = rsnd_dai_call(irq, io, priv, 0);
738
739                 ret |= rsnd_dai_call(stop, io, priv);
740
741                 ret |= rsnd_dai_call(quit, io, priv);
742
743                 break;
744         default:
745                 ret = -EINVAL;
746         }
747
748 dai_trigger_end:
749         spin_unlock_irqrestore(&priv->lock, flags);
750
751         return ret;
752 }
753
754 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
755 {
756         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
757
758         /* set clock master for audio interface */
759         switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
760         case SND_SOC_DAIFMT_BC_FC:
761                 rdai->clk_master = 0;
762                 break;
763         case SND_SOC_DAIFMT_BP_FP:
764                 rdai->clk_master = 1; /* cpu is master */
765                 break;
766         default:
767                 return -EINVAL;
768         }
769
770         /* set format */
771         rdai->bit_clk_inv = 0;
772         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
773         case SND_SOC_DAIFMT_I2S:
774                 rdai->sys_delay = 0;
775                 rdai->data_alignment = 0;
776                 rdai->frm_clk_inv = 0;
777                 break;
778         case SND_SOC_DAIFMT_LEFT_J:
779         case SND_SOC_DAIFMT_DSP_B:
780                 rdai->sys_delay = 1;
781                 rdai->data_alignment = 0;
782                 rdai->frm_clk_inv = 1;
783                 break;
784         case SND_SOC_DAIFMT_RIGHT_J:
785                 rdai->sys_delay = 1;
786                 rdai->data_alignment = 1;
787                 rdai->frm_clk_inv = 1;
788                 break;
789         case SND_SOC_DAIFMT_DSP_A:
790                 rdai->sys_delay = 0;
791                 rdai->data_alignment = 0;
792                 rdai->frm_clk_inv = 1;
793                 break;
794         }
795
796         /* set clock inversion */
797         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
798         case SND_SOC_DAIFMT_NB_IF:
799                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
800                 break;
801         case SND_SOC_DAIFMT_IB_NF:
802                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
803                 break;
804         case SND_SOC_DAIFMT_IB_IF:
805                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
806                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
807                 break;
808         case SND_SOC_DAIFMT_NB_NF:
809         default:
810                 break;
811         }
812
813         return 0;
814 }
815
816 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
817                                      u32 tx_mask, u32 rx_mask,
818                                      int slots, int slot_width)
819 {
820         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
821         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
822         struct device *dev = rsnd_priv_to_dev(priv);
823
824         switch (slot_width) {
825         case 16:
826         case 24:
827         case 32:
828                 break;
829         default:
830                 /* use default */
831                 /*
832                  * Indicate warning if DT has "dai-tdm-slot-width"
833                  * but the value was not expected.
834                  */
835                 if (slot_width)
836                         dev_warn(dev, "unsupported TDM slot width (%d), force to use default 32\n",
837                                  slot_width);
838                 slot_width = 32;
839         }
840
841         switch (slots) {
842         case 2:
843                 /* TDM Split Mode */
844         case 6:
845         case 8:
846                 /* TDM Extend Mode */
847                 rsnd_rdai_channels_set(rdai, slots);
848                 rsnd_rdai_ssi_lane_set(rdai, 1);
849                 rsnd_rdai_width_set(rdai, slot_width);
850                 break;
851         default:
852                 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
853                 return -EINVAL;
854         }
855
856         return 0;
857 }
858
859 static unsigned int rsnd_soc_hw_channels_list[] = {
860         2, 6, 8,
861 };
862
863 static unsigned int rsnd_soc_hw_rate_list[] = {
864           8000,
865          11025,
866          16000,
867          22050,
868          32000,
869          44100,
870          48000,
871          64000,
872          88200,
873          96000,
874         176400,
875         192000,
876 };
877
878 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
879                             unsigned int *list, int list_num,
880                             struct snd_interval *baseline, struct snd_interval *iv,
881                             struct rsnd_dai_stream *io, char *unit)
882 {
883         struct snd_interval p;
884         unsigned int rate;
885         int i;
886
887         snd_interval_any(&p);
888         p.min = UINT_MAX;
889         p.max = 0;
890
891         for (i = 0; i < list_num; i++) {
892
893                 if (!snd_interval_test(iv, list[i]))
894                         continue;
895
896                 rate = rsnd_ssi_clk_query(rdai,
897                                           baseline->min, list[i], NULL);
898                 if (rate > 0) {
899                         p.min = min(p.min, list[i]);
900                         p.max = max(p.max, list[i]);
901                 }
902
903                 rate = rsnd_ssi_clk_query(rdai,
904                                           baseline->max, list[i], NULL);
905                 if (rate > 0) {
906                         p.min = min(p.min, list[i]);
907                         p.max = max(p.max, list[i]);
908                 }
909         }
910
911         /* Indicate error once if it can't handle */
912         if (!rsnd_flags_has(io, RSND_HW_RULE_ERR) && (p.min > p.max)) {
913                 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
914                 struct device *dev = rsnd_priv_to_dev(priv);
915
916                 dev_warn(dev, "It can't handle %d %s <-> %d %s\n",
917                          baseline->min, unit, baseline->max, unit);
918                 rsnd_flags_set(io, RSND_HW_RULE_ERR);
919         }
920
921         return snd_interval_refine(iv, &p);
922 }
923
924 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
925                                  struct snd_pcm_hw_rule *rule)
926 {
927         struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
928         struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
929         struct snd_interval ic;
930         struct rsnd_dai_stream *io = rule->private;
931         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
932
933         /*
934          * possible sampling rate limitation is same as
935          * 2ch if it supports multi ssi
936          * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
937          */
938         ic = *ic_;
939         ic.min =
940         ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
941
942         return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
943                                 ARRAY_SIZE(rsnd_soc_hw_rate_list),
944                                 &ic, ir, io, "ch");
945 }
946
947 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
948                                      struct snd_pcm_hw_rule *rule)
949 {
950         struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
951         struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
952         struct snd_interval ic;
953         struct rsnd_dai_stream *io = rule->private;
954         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
955
956         /*
957          * possible sampling rate limitation is same as
958          * 2ch if it supports multi ssi
959          * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
960          */
961         ic = *ic_;
962         ic.min =
963         ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
964
965         return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
966                                 ARRAY_SIZE(rsnd_soc_hw_channels_list),
967                                 ir, &ic, io, "Hz");
968 }
969
970 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
971         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
972                         SNDRV_PCM_INFO_MMAP             |
973                         SNDRV_PCM_INFO_MMAP_VALID,
974         .buffer_bytes_max       = 64 * 1024,
975         .period_bytes_min       = 32,
976         .period_bytes_max       = 8192,
977         .periods_min            = 1,
978         .periods_max            = 32,
979         .fifo_size              = 256,
980 };
981
982 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
983                                 struct snd_soc_dai *dai)
984 {
985         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
986         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
987         struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
988         struct snd_pcm_runtime *runtime = substream->runtime;
989         unsigned int max_channels = rsnd_rdai_channels_get(rdai);
990         int i;
991
992         rsnd_flags_del(io, RSND_HW_RULE_ERR);
993
994         rsnd_dai_stream_init(io, substream);
995
996         /*
997          * Channel Limitation
998          * It depends on Platform design
999          */
1000         constraint->list        = rsnd_soc_hw_channels_list;
1001         constraint->count       = 0;
1002         constraint->mask        = 0;
1003
1004         for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
1005                 if (rsnd_soc_hw_channels_list[i] > max_channels)
1006                         break;
1007                 constraint->count = i + 1;
1008         }
1009
1010         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
1011
1012         snd_pcm_hw_constraint_list(runtime, 0,
1013                                    SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
1014
1015         snd_pcm_hw_constraint_integer(runtime,
1016                                       SNDRV_PCM_HW_PARAM_PERIODS);
1017
1018         /*
1019          * Sampling Rate / Channel Limitation
1020          * It depends on Clock Master Mode
1021          */
1022         if (rsnd_rdai_is_clk_master(rdai)) {
1023                 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1024
1025                 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1026                                     rsnd_soc_hw_rule_rate,
1027                                     is_play ? &rdai->playback : &rdai->capture,
1028                                     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1029                 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1030                                     rsnd_soc_hw_rule_channels,
1031                                     is_play ? &rdai->playback : &rdai->capture,
1032                                     SNDRV_PCM_HW_PARAM_RATE, -1);
1033         }
1034
1035         return 0;
1036 }
1037
1038 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1039                                   struct snd_soc_dai *dai)
1040 {
1041         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1042         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1043         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1044
1045         /*
1046          * call rsnd_dai_call without spinlock
1047          */
1048         rsnd_dai_call(cleanup, io, priv);
1049
1050         rsnd_dai_stream_quit(io);
1051 }
1052
1053 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1054                                 struct snd_soc_dai *dai)
1055 {
1056         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1057         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1058         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1059
1060         return rsnd_dai_call(prepare, io, priv);
1061 }
1062
1063 static u64 rsnd_soc_dai_formats[] = {
1064         /*
1065          * 1st Priority
1066          *
1067          * Well tested formats.
1068          * Select below from Sound Card, not auto
1069          *      SND_SOC_DAIFMT_CBC_CFC
1070          *      SND_SOC_DAIFMT_CBP_CFP
1071          */
1072         SND_SOC_POSSIBLE_DAIFMT_I2S     |
1073         SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
1074         SND_SOC_POSSIBLE_DAIFMT_LEFT_J  |
1075         SND_SOC_POSSIBLE_DAIFMT_NB_NF   |
1076         SND_SOC_POSSIBLE_DAIFMT_NB_IF   |
1077         SND_SOC_POSSIBLE_DAIFMT_IB_NF   |
1078         SND_SOC_POSSIBLE_DAIFMT_IB_IF,
1079         /*
1080          * 2nd Priority
1081          *
1082          * Supported, but not well tested
1083          */
1084         SND_SOC_POSSIBLE_DAIFMT_DSP_A   |
1085         SND_SOC_POSSIBLE_DAIFMT_DSP_B,
1086 };
1087
1088 static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
1089                                       struct rsnd_dai_stream *io,
1090                                       struct device_node *dai_np)
1091 {
1092         struct device *dev = rsnd_priv_to_dev(priv);
1093         struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1094         struct device_node *np;
1095         int is_play = rsnd_io_is_play(io);
1096         int i;
1097
1098         if (!ssiu_np)
1099                 return;
1100
1101         /*
1102          * This driver assumes that it is TDM Split mode
1103          * if it includes ssiu node
1104          */
1105         for (i = 0;; i++) {
1106                 struct device_node *node = is_play ?
1107                         of_parse_phandle(dai_np, "playback", i) :
1108                         of_parse_phandle(dai_np, "capture",  i);
1109
1110                 if (!node)
1111                         break;
1112
1113                 for_each_child_of_node(ssiu_np, np) {
1114                         if (np == node) {
1115                                 rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1116                                 dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1117                         }
1118                 }
1119
1120                 of_node_put(node);
1121         }
1122
1123         of_node_put(ssiu_np);
1124 }
1125
1126 static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1127                                       struct rsnd_dai_stream *io,
1128                                       struct device_node *dai_np)
1129 {
1130         if (!rsnd_io_to_mod_ssi(io))
1131                 return;
1132
1133         rsnd_parse_tdm_split_mode(priv, io, dai_np);
1134 }
1135
1136 static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1137                                      struct rsnd_dai_stream *io,
1138                                      struct device_node *endpoint)
1139 {
1140         struct device *dev = rsnd_priv_to_dev(priv);
1141         struct device_node *remote_node;
1142
1143         if (!rsnd_io_to_mod_ssi(io))
1144                 return;
1145
1146         remote_node = of_graph_get_remote_port_parent(endpoint);
1147
1148         /* HDMI0 */
1149         if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1150                 rsnd_flags_set(io, RSND_STREAM_HDMI0);
1151                 dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1152         }
1153
1154         /* HDMI1 */
1155         if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1156                 rsnd_flags_set(io, RSND_STREAM_HDMI1);
1157                 dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1158         }
1159
1160         rsnd_parse_tdm_split_mode(priv, io, endpoint);
1161
1162         of_node_put(remote_node);
1163 }
1164
1165 void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
1166                 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1167                 struct device_node *node,
1168                 struct device_node *playback,
1169                 struct device_node *capture)
1170 {
1171         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1172         struct device *dev = rsnd_priv_to_dev(priv);
1173         struct device_node *np;
1174         int i;
1175
1176         if (!node)
1177                 return;
1178
1179         i = 0;
1180         for_each_child_of_node(node, np) {
1181                 struct rsnd_mod *mod;
1182
1183                 i = rsnd_node_fixed_index(dev, np, name, i);
1184                 if (i < 0) {
1185                         of_node_put(np);
1186                         break;
1187                 }
1188
1189                 mod = mod_get(priv, i);
1190
1191                 if (np == playback)
1192                         rsnd_dai_connect(mod, &rdai->playback, mod->type);
1193                 if (np == capture)
1194                         rsnd_dai_connect(mod, &rdai->capture, mod->type);
1195                 i++;
1196         }
1197
1198         of_node_put(node);
1199 }
1200
1201 int rsnd_node_fixed_index(struct device *dev, struct device_node *node, char *name, int idx)
1202 {
1203         char node_name[16];
1204
1205         /*
1206          * rsnd is assuming each device nodes are sequential numbering,
1207          * but some of them are not.
1208          * This function adjusts index for it.
1209          *
1210          * ex)
1211          * Normal case,         special case
1212          *      ssi-0
1213          *      ssi-1
1214          *      ssi-2
1215          *      ssi-3           ssi-3
1216          *      ssi-4           ssi-4
1217          *      ...
1218          *
1219          * assume Max 64 node
1220          */
1221         for (; idx < 64; idx++) {
1222                 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1223
1224                 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1225                         return idx;
1226         }
1227
1228         dev_err(dev, "strange node numbering (%s)",
1229                 of_node_full_name(node));
1230         return -EINVAL;
1231 }
1232
1233 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1234 {
1235         struct device *dev = rsnd_priv_to_dev(priv);
1236         struct device_node *np;
1237         int i;
1238
1239         i = 0;
1240         for_each_child_of_node(node, np) {
1241                 i = rsnd_node_fixed_index(dev, np, name, i);
1242                 if (i < 0) {
1243                         of_node_put(np);
1244                         return 0;
1245                 }
1246                 i++;
1247         }
1248
1249         return i;
1250 }
1251
1252 static int rsnd_dai_of_node(struct rsnd_priv *priv, int *is_graph)
1253 {
1254         struct device *dev = rsnd_priv_to_dev(priv);
1255         struct device_node *np = dev->of_node;
1256         struct device_node *ports, *node;
1257         int nr = 0;
1258         int i = 0;
1259
1260         *is_graph = 0;
1261
1262         /*
1263          * parse both previous dai (= rcar_sound,dai), and
1264          * graph dai (= ports/port)
1265          */
1266
1267         /*
1268          * Simple-Card
1269          */
1270         node = of_get_child_by_name(np, RSND_NODE_DAI);
1271         if (!node)
1272                 goto audio_graph;
1273
1274         of_node_put(node);
1275
1276         for_each_child_of_node(np, node) {
1277                 if (!of_node_name_eq(node, RSND_NODE_DAI))
1278                         continue;
1279
1280                 priv->component_dais[i] = of_get_child_count(node);
1281                 nr += priv->component_dais[i];
1282                 i++;
1283                 if (i >= RSND_MAX_COMPONENT) {
1284                         dev_info(dev, "reach to max component\n");
1285                         of_node_put(node);
1286                         break;
1287                 }
1288         }
1289
1290         return nr;
1291
1292 audio_graph:
1293         /*
1294          * Audio-Graph-Card
1295          */
1296         for_each_child_of_node(np, ports) {
1297                 if (!of_node_name_eq(ports, "ports") &&
1298                     !of_node_name_eq(ports, "port"))
1299                         continue;
1300                 priv->component_dais[i] = of_graph_get_endpoint_count(ports);
1301                 nr += priv->component_dais[i];
1302                 i++;
1303                 if (i >= RSND_MAX_COMPONENT) {
1304                         dev_info(dev, "reach to max component\n");
1305                         of_node_put(node);
1306                         break;
1307                 }
1308         }
1309
1310         *is_graph = 1;
1311
1312         return nr;
1313 }
1314
1315
1316 #define PREALLOC_BUFFER         (32 * 1024)
1317 #define PREALLOC_BUFFER_MAX     (32 * 1024)
1318
1319 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1320                                   struct rsnd_dai_stream *io,
1321                                   int stream)
1322 {
1323         struct rsnd_priv *priv = rsnd_io_to_priv(io);
1324         struct device *dev = rsnd_priv_to_dev(priv);
1325         struct snd_pcm_substream *substream;
1326
1327         /*
1328          * use Audio-DMAC dev if we can use IPMMU
1329          * see
1330          *      rsnd_dmaen_attach()
1331          */
1332         if (io->dmac_dev)
1333                 dev = io->dmac_dev;
1334
1335         for (substream = rtd->pcm->streams[stream].substream;
1336              substream;
1337              substream = substream->next) {
1338                 snd_pcm_set_managed_buffer(substream,
1339                                            SNDRV_DMA_TYPE_DEV,
1340                                            dev,
1341                                            PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1342         }
1343
1344         return 0;
1345 }
1346
1347 static int rsnd_soc_dai_pcm_new(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
1348 {
1349         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1350         int ret;
1351
1352         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1353         if (ret)
1354                 return ret;
1355
1356         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1357         if (ret)
1358                 return ret;
1359
1360         ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1361                                      SNDRV_PCM_STREAM_PLAYBACK);
1362         if (ret)
1363                 return ret;
1364
1365         ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1366                                      SNDRV_PCM_STREAM_CAPTURE);
1367         if (ret)
1368                 return ret;
1369
1370         return 0;
1371 }
1372
1373 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
1374         .pcm_new                        = rsnd_soc_dai_pcm_new,
1375         .startup                        = rsnd_soc_dai_startup,
1376         .shutdown                       = rsnd_soc_dai_shutdown,
1377         .trigger                        = rsnd_soc_dai_trigger,
1378         .set_fmt                        = rsnd_soc_dai_set_fmt,
1379         .set_tdm_slot                   = rsnd_soc_set_dai_tdm_slot,
1380         .prepare                        = rsnd_soc_dai_prepare,
1381         .auto_selectable_formats        = rsnd_soc_dai_formats,
1382         .num_auto_selectable_formats    = ARRAY_SIZE(rsnd_soc_dai_formats),
1383 };
1384
1385 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1386                              struct device_node *dai_np,
1387                              struct device_node *node_np,
1388                              uint32_t node_arg,
1389                              int dai_i)
1390 {
1391         struct rsnd_dai_stream *io_playback;
1392         struct rsnd_dai_stream *io_capture;
1393         struct snd_soc_dai_driver *drv;
1394         struct rsnd_dai *rdai;
1395         struct device *dev = rsnd_priv_to_dev(priv);
1396         int playback_exist = 0, capture_exist = 0;
1397         int io_i;
1398
1399         rdai            = rsnd_rdai_get(priv, dai_i);
1400         drv             = rsnd_daidrv_get(priv, dai_i);
1401         io_playback     = &rdai->playback;
1402         io_capture      = &rdai->capture;
1403
1404         snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1405
1406         /* for multi Component */
1407         rdai->dai_args.np               = node_np;
1408         rdai->dai_args.args_count       = 1;
1409         rdai->dai_args.args[0]          = node_arg;
1410
1411         rdai->priv      = priv;
1412         drv->name       = rdai->name;
1413         drv->ops        = &rsnd_soc_dai_ops;
1414         drv->id         = dai_i;
1415         drv->dai_args   = &rdai->dai_args;
1416
1417         io_playback->rdai               = rdai;
1418         io_capture->rdai                = rdai;
1419         rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1420         rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1421         rsnd_rdai_width_set(rdai, 32);   /* default 32bit width */
1422
1423         for (io_i = 0;; io_i++) {
1424                 struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1425                 struct device_node *capture  = of_parse_phandle(dai_np, "capture", io_i);
1426
1427                 if (!playback && !capture)
1428                         break;
1429
1430                 if (io_i == 0) {
1431                         /* check whether playback/capture property exists */
1432                         if (playback)
1433                                 playback_exist = 1;
1434                         if (capture)
1435                                 capture_exist = 1;
1436                 }
1437
1438                 rsnd_parse_connect_ssi(rdai, playback, capture);
1439                 rsnd_parse_connect_ssiu(rdai, playback, capture);
1440                 rsnd_parse_connect_src(rdai, playback, capture);
1441                 rsnd_parse_connect_ctu(rdai, playback, capture);
1442                 rsnd_parse_connect_mix(rdai, playback, capture);
1443                 rsnd_parse_connect_dvc(rdai, playback, capture);
1444
1445                 of_node_put(playback);
1446                 of_node_put(capture);
1447         }
1448
1449         if (playback_exist) {
1450                 snprintf(io_playback->name, RSND_DAI_NAME_SIZE, "DAI%d Playback", dai_i);
1451                 drv->playback.rates             = RSND_RATES;
1452                 drv->playback.formats           = RSND_FMTS;
1453                 drv->playback.channels_min      = 2;
1454                 drv->playback.channels_max      = 8;
1455                 drv->playback.stream_name       = io_playback->name;
1456         }
1457         if (capture_exist) {
1458                 snprintf(io_capture->name, RSND_DAI_NAME_SIZE, "DAI%d Capture", dai_i);
1459                 drv->capture.rates              = RSND_RATES;
1460                 drv->capture.formats            = RSND_FMTS;
1461                 drv->capture.channels_min       = 2;
1462                 drv->capture.channels_max       = 8;
1463                 drv->capture.stream_name        = io_capture->name;
1464         }
1465
1466         if (rsnd_ssi_is_pin_sharing(io_capture) ||
1467             rsnd_ssi_is_pin_sharing(io_playback)) {
1468                 /* should have symmetric_rate if pin sharing */
1469                 drv->symmetric_rate = 1;
1470         }
1471
1472         dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1473                 rsnd_io_to_mod_ssi(io_playback) ? "play"    : " -- ",
1474                 rsnd_io_to_mod_ssi(io_capture) ? "capture" : "  --   ");
1475 }
1476
1477 static int rsnd_dai_probe(struct rsnd_priv *priv)
1478 {
1479         struct snd_soc_dai_driver *rdrv;
1480         struct device *dev = rsnd_priv_to_dev(priv);
1481         struct device_node *np = dev->of_node;
1482         struct rsnd_dai *rdai;
1483         int nr = 0;
1484         int is_graph;
1485         int dai_i;
1486
1487         nr = rsnd_dai_of_node(priv, &is_graph);
1488         if (!nr)
1489                 return -EINVAL;
1490
1491         rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1492         rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1493         if (!rdrv || !rdai)
1494                 return -ENOMEM;
1495
1496         priv->rdai_nr   = nr;
1497         priv->daidrv    = rdrv;
1498         priv->rdai      = rdai;
1499
1500         /*
1501          * parse all dai
1502          */
1503         dai_i = 0;
1504         if (is_graph) {
1505                 struct device_node *ports;
1506                 struct device_node *dai_np;
1507
1508                 for_each_child_of_node(np, ports) {
1509                         if (!of_node_name_eq(ports, "ports") &&
1510                             !of_node_name_eq(ports, "port"))
1511                                 continue;
1512                         for_each_endpoint_of_node(ports, dai_np) {
1513                                 __rsnd_dai_probe(priv, dai_np, dai_np, 0, dai_i);
1514                                 if (rsnd_is_gen3(priv) || rsnd_is_gen4(priv)) {
1515                                         rdai = rsnd_rdai_get(priv, dai_i);
1516
1517                                         rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1518                                         rsnd_parse_connect_graph(priv, &rdai->capture,  dai_np);
1519                                 }
1520                                 dai_i++;
1521                         }
1522                 }
1523         } else {
1524                 struct device_node *node;
1525                 struct device_node *dai_np;
1526
1527                 for_each_child_of_node(np, node) {
1528                         if (!of_node_name_eq(node, RSND_NODE_DAI))
1529                                 continue;
1530
1531                         for_each_child_of_node(node, dai_np) {
1532                                 __rsnd_dai_probe(priv, dai_np, np, dai_i, dai_i);
1533                                 if (rsnd_is_gen3(priv) || rsnd_is_gen4(priv)) {
1534                                         rdai = rsnd_rdai_get(priv, dai_i);
1535
1536                                         rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1537                                         rsnd_parse_connect_simple(priv, &rdai->capture,  dai_np);
1538                                 }
1539                                 dai_i++;
1540                         }
1541                 }
1542         }
1543
1544         return 0;
1545 }
1546
1547 /*
1548  *              pcm ops
1549  */
1550 static int rsnd_hw_update(struct snd_pcm_substream *substream,
1551                           struct snd_pcm_hw_params *hw_params)
1552 {
1553         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1554         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1555         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1556         struct rsnd_priv *priv = rsnd_io_to_priv(io);
1557         unsigned long flags;
1558         int ret;
1559
1560         spin_lock_irqsave(&priv->lock, flags);
1561         if (hw_params)
1562                 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1563         else
1564                 ret = rsnd_dai_call(hw_free, io, substream);
1565         spin_unlock_irqrestore(&priv->lock, flags);
1566
1567         return ret;
1568 }
1569
1570 static int rsnd_hw_params(struct snd_soc_component *component,
1571                           struct snd_pcm_substream *substream,
1572                           struct snd_pcm_hw_params *hw_params)
1573 {
1574         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1575         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1576         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1577         struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1578
1579         /*
1580          * rsnd assumes that it might be used under DPCM if user want to use
1581          * channel / rate convert. Then, rsnd should be FE.
1582          * And then, this function will be called *after* BE settings.
1583          * this means, each BE already has fixuped hw_params.
1584          * see
1585          *      dpcm_fe_dai_hw_params()
1586          *      dpcm_be_dai_hw_params()
1587          */
1588         io->converted_rate = 0;
1589         io->converted_chan = 0;
1590         if (fe->dai_link->dynamic) {
1591                 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1592                 struct device *dev = rsnd_priv_to_dev(priv);
1593                 struct snd_soc_dpcm *dpcm;
1594                 int stream = substream->stream;
1595
1596                 for_each_dpcm_be(fe, stream, dpcm) {
1597                         struct snd_soc_pcm_runtime *be = dpcm->be;
1598                         struct snd_pcm_hw_params *be_params = &be->dpcm[stream].hw_params;
1599
1600                         if (params_channels(hw_params) != params_channels(be_params))
1601                                 io->converted_chan = params_channels(be_params);
1602                         if (params_rate(hw_params) != params_rate(be_params))
1603                                 io->converted_rate = params_rate(be_params);
1604                 }
1605                 if (io->converted_chan)
1606                         dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1607                 if (io->converted_rate) {
1608                         /*
1609                          * SRC supports convert rates from params_rate(hw_params)/k_down
1610                          * to params_rate(hw_params)*k_up, where k_up is always 6, and
1611                          * k_down depends on number of channels and SRC unit.
1612                          * So all SRC units can upsample audio up to 6 times regardless
1613                          * its number of channels. And all SRC units can downsample
1614                          * 2 channel audio up to 6 times too.
1615                          */
1616                         int k_up = 6;
1617                         int k_down = 6;
1618                         int channel;
1619                         struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1620
1621                         dev_dbg(dev, "convert rate     = %d\n", io->converted_rate);
1622
1623                         channel = io->converted_chan ? io->converted_chan :
1624                                   params_channels(hw_params);
1625
1626                         switch (rsnd_mod_id(src_mod)) {
1627                         /*
1628                          * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times.
1629                          * SRC1, SRC3 and SRC4 can downsample 4 channel audio
1630                          * up to 4 times.
1631                          * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio
1632                          * no more than twice.
1633                          */
1634                         case 1:
1635                         case 3:
1636                         case 4:
1637                                 if (channel > 4) {
1638                                         k_down = 2;
1639                                         break;
1640                                 }
1641                                 fallthrough;
1642                         case 0:
1643                                 if (channel > 2)
1644                                         k_down = 4;
1645                                 break;
1646
1647                         /* Other SRC units do not support more than 2 channels */
1648                         default:
1649                                 if (channel > 2)
1650                                         return -EINVAL;
1651                         }
1652
1653                         if (params_rate(hw_params) > io->converted_rate * k_down) {
1654                                 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1655                                         io->converted_rate * k_down;
1656                                 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1657                                         io->converted_rate * k_down;
1658                                 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1659                         } else if (params_rate(hw_params) * k_up < io->converted_rate) {
1660                                 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1661                                         DIV_ROUND_UP(io->converted_rate, k_up);
1662                                 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1663                                         DIV_ROUND_UP(io->converted_rate, k_up);
1664                                 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1665                         }
1666
1667                         /*
1668                          * TBD: Max SRC input and output rates also depend on number
1669                          * of channels and SRC unit:
1670                          * SRC1, SRC3 and SRC4 do not support more than 128kHz
1671                          * for 6 channel and 96kHz for 8 channel audio.
1672                          * Perhaps this function should return EINVAL if the input or
1673                          * the output rate exceeds the limitation.
1674                          */
1675                 }
1676         }
1677
1678         return rsnd_hw_update(substream, hw_params);
1679 }
1680
1681 static int rsnd_hw_free(struct snd_soc_component *component,
1682                         struct snd_pcm_substream *substream)
1683 {
1684         return rsnd_hw_update(substream, NULL);
1685 }
1686
1687 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1688                                       struct snd_pcm_substream *substream)
1689 {
1690         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1691         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1692         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1693         snd_pcm_uframes_t pointer = 0;
1694
1695         rsnd_dai_call(pointer, io, &pointer);
1696
1697         return pointer;
1698 }
1699
1700 /*
1701  *              snd_kcontrol
1702  */
1703 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1704                            struct snd_ctl_elem_info *uinfo)
1705 {
1706         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1707
1708         if (cfg->texts) {
1709                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1710                 uinfo->count = cfg->size;
1711                 uinfo->value.enumerated.items = cfg->max;
1712                 if (uinfo->value.enumerated.item >= cfg->max)
1713                         uinfo->value.enumerated.item = cfg->max - 1;
1714                 strscpy(uinfo->value.enumerated.name,
1715                         cfg->texts[uinfo->value.enumerated.item],
1716                         sizeof(uinfo->value.enumerated.name));
1717         } else {
1718                 uinfo->count = cfg->size;
1719                 uinfo->value.integer.min = 0;
1720                 uinfo->value.integer.max = cfg->max;
1721                 uinfo->type = (cfg->max == 1) ?
1722                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1723                         SNDRV_CTL_ELEM_TYPE_INTEGER;
1724         }
1725
1726         return 0;
1727 }
1728
1729 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1730                           struct snd_ctl_elem_value *uc)
1731 {
1732         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1733         int i;
1734
1735         for (i = 0; i < cfg->size; i++)
1736                 if (cfg->texts)
1737                         uc->value.enumerated.item[i] = cfg->val[i];
1738                 else
1739                         uc->value.integer.value[i] = cfg->val[i];
1740
1741         return 0;
1742 }
1743
1744 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1745                           struct snd_ctl_elem_value *uc)
1746 {
1747         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1748         int i, change = 0;
1749
1750         if (!cfg->accept(cfg->io))
1751                 return 0;
1752
1753         for (i = 0; i < cfg->size; i++) {
1754                 if (cfg->texts) {
1755                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1756                         cfg->val[i] = uc->value.enumerated.item[i];
1757                 } else {
1758                         change |= (uc->value.integer.value[i] != cfg->val[i]);
1759                         cfg->val[i] = uc->value.integer.value[i];
1760                 }
1761         }
1762
1763         if (change && cfg->update)
1764                 cfg->update(cfg->io, cfg->mod);
1765
1766         return change;
1767 }
1768
1769 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1770 {
1771         return 1;
1772 }
1773
1774 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1775 {
1776         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1777         struct rsnd_priv *priv = rsnd_io_to_priv(io);
1778         struct device *dev = rsnd_priv_to_dev(priv);
1779
1780         if (!runtime) {
1781                 dev_warn(dev, "Can't update kctrl when idle\n");
1782                 return 0;
1783         }
1784
1785         return 1;
1786 }
1787
1788 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1789 {
1790         cfg->cfg.val = cfg->val;
1791
1792         return &cfg->cfg;
1793 }
1794
1795 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1796 {
1797         cfg->cfg.val = &cfg->val;
1798
1799         return &cfg->cfg;
1800 }
1801
1802 const char * const volume_ramp_rate[] = {
1803         "128 dB/1 step",         /* 00000 */
1804         "64 dB/1 step",          /* 00001 */
1805         "32 dB/1 step",          /* 00010 */
1806         "16 dB/1 step",          /* 00011 */
1807         "8 dB/1 step",           /* 00100 */
1808         "4 dB/1 step",           /* 00101 */
1809         "2 dB/1 step",           /* 00110 */
1810         "1 dB/1 step",           /* 00111 */
1811         "0.5 dB/1 step",         /* 01000 */
1812         "0.25 dB/1 step",        /* 01001 */
1813         "0.125 dB/1 step",       /* 01010 = VOLUME_RAMP_MAX_MIX */
1814         "0.125 dB/2 steps",      /* 01011 */
1815         "0.125 dB/4 steps",      /* 01100 */
1816         "0.125 dB/8 steps",      /* 01101 */
1817         "0.125 dB/16 steps",     /* 01110 */
1818         "0.125 dB/32 steps",     /* 01111 */
1819         "0.125 dB/64 steps",     /* 10000 */
1820         "0.125 dB/128 steps",    /* 10001 */
1821         "0.125 dB/256 steps",    /* 10010 */
1822         "0.125 dB/512 steps",    /* 10011 */
1823         "0.125 dB/1024 steps",   /* 10100 */
1824         "0.125 dB/2048 steps",   /* 10101 */
1825         "0.125 dB/4096 steps",   /* 10110 */
1826         "0.125 dB/8192 steps",   /* 10111 = VOLUME_RAMP_MAX_DVC */
1827 };
1828
1829 int rsnd_kctrl_new(struct rsnd_mod *mod,
1830                    struct rsnd_dai_stream *io,
1831                    struct snd_soc_pcm_runtime *rtd,
1832                    const unsigned char *name,
1833                    int (*accept)(struct rsnd_dai_stream *io),
1834                    void (*update)(struct rsnd_dai_stream *io,
1835                                   struct rsnd_mod *mod),
1836                    struct rsnd_kctrl_cfg *cfg,
1837                    const char * const *texts,
1838                    int size,
1839                    u32 max)
1840 {
1841         struct snd_card *card = rtd->card->snd_card;
1842         struct snd_kcontrol *kctrl;
1843         struct snd_kcontrol_new knew = {
1844                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
1845                 .name           = name,
1846                 .info           = rsnd_kctrl_info,
1847                 .index          = rtd->num,
1848                 .get            = rsnd_kctrl_get,
1849                 .put            = rsnd_kctrl_put,
1850         };
1851         int ret;
1852
1853         /*
1854          * 1) Avoid duplicate register for DVC with MIX case
1855          * 2) Allow duplicate register for MIX
1856          * 3) re-register if card was rebinded
1857          */
1858         list_for_each_entry(kctrl, &card->controls, list) {
1859                 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1860
1861                 if (c == cfg)
1862                         return 0;
1863         }
1864
1865         if (size > RSND_MAX_CHANNELS)
1866                 return -EINVAL;
1867
1868         kctrl = snd_ctl_new1(&knew, cfg);
1869         if (!kctrl)
1870                 return -ENOMEM;
1871
1872         ret = snd_ctl_add(card, kctrl);
1873         if (ret < 0)
1874                 return ret;
1875
1876         cfg->texts      = texts;
1877         cfg->max        = max;
1878         cfg->size       = size;
1879         cfg->accept     = accept;
1880         cfg->update     = update;
1881         cfg->card       = card;
1882         cfg->kctrl      = kctrl;
1883         cfg->io         = io;
1884         cfg->mod        = mod;
1885
1886         return 0;
1887 }
1888
1889 /*
1890  *              snd_soc_component
1891  */
1892 static const struct snd_soc_component_driver rsnd_soc_component = {
1893         .name                   = "rsnd",
1894         .probe                  = rsnd_debugfs_probe,
1895         .hw_params              = rsnd_hw_params,
1896         .hw_free                = rsnd_hw_free,
1897         .pointer                = rsnd_pointer,
1898         .legacy_dai_naming      = 1,
1899 };
1900
1901 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1902                                        struct rsnd_dai_stream *io)
1903 {
1904         int ret;
1905
1906         ret = rsnd_dai_call(probe, io, priv);
1907         if (ret == -EAGAIN) {
1908                 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1909                 struct rsnd_mod *mod;
1910                 int i;
1911
1912                 /*
1913                  * Fallback to PIO mode
1914                  */
1915
1916                 /*
1917                  * call "remove" for SSI/SRC/DVC
1918                  * SSI will be switch to PIO mode if it was DMA mode
1919                  * see
1920                  *      rsnd_dma_init()
1921                  *      rsnd_ssi_fallback()
1922                  */
1923                 rsnd_dai_call(remove, io, priv);
1924
1925                 /*
1926                  * remove all mod from io
1927                  * and, re connect ssi
1928                  */
1929                 for_each_rsnd_mod(i, mod, io)
1930                         rsnd_dai_disconnect(mod, io, i);
1931                 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1932
1933                 /*
1934                  * fallback
1935                  */
1936                 rsnd_dai_call(fallback, io, priv);
1937
1938                 /*
1939                  * retry to "probe".
1940                  * DAI has SSI which is PIO mode only now.
1941                  */
1942                 ret = rsnd_dai_call(probe, io, priv);
1943         }
1944
1945         return ret;
1946 }
1947
1948 /*
1949  *      rsnd probe
1950  */
1951 static int rsnd_probe(struct platform_device *pdev)
1952 {
1953         struct rsnd_priv *priv;
1954         struct device *dev = &pdev->dev;
1955         struct rsnd_dai *rdai;
1956         int (*probe_func[])(struct rsnd_priv *priv) = {
1957                 rsnd_gen_probe,
1958                 rsnd_dma_probe,
1959                 rsnd_ssi_probe,
1960                 rsnd_ssiu_probe,
1961                 rsnd_src_probe,
1962                 rsnd_ctu_probe,
1963                 rsnd_mix_probe,
1964                 rsnd_dvc_probe,
1965                 rsnd_cmd_probe,
1966                 rsnd_adg_probe,
1967                 rsnd_dai_probe,
1968         };
1969         int ret, i;
1970         int ci;
1971
1972         /*
1973          *      init priv data
1974          */
1975         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1976         if (!priv)
1977                 return -ENODEV;
1978
1979         priv->pdev      = pdev;
1980         priv->flags     = (unsigned long)of_device_get_match_data(dev);
1981         spin_lock_init(&priv->lock);
1982
1983         /*
1984          *      init each module
1985          */
1986         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1987                 ret = probe_func[i](priv);
1988                 if (ret)
1989                         return ret;
1990         }
1991
1992         for_each_rsnd_dai(rdai, priv, i) {
1993                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1994                 if (ret)
1995                         goto exit_snd_probe;
1996
1997                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1998                 if (ret)
1999                         goto exit_snd_probe;
2000         }
2001
2002         dev_set_drvdata(dev, priv);
2003
2004         /*
2005          *      asoc register
2006          */
2007         ci = 0;
2008         for (i = 0; priv->component_dais[i] > 0; i++) {
2009                 int nr = priv->component_dais[i];
2010
2011                 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
2012                                                       priv->daidrv + ci, nr);
2013                 if (ret < 0) {
2014                         dev_err(dev, "cannot snd component register\n");
2015                         goto exit_snd_probe;
2016                 }
2017
2018                 ci += nr;
2019         }
2020
2021         pm_runtime_enable(dev);
2022
2023         dev_info(dev, "probed\n");
2024         return ret;
2025
2026 exit_snd_probe:
2027         for_each_rsnd_dai(rdai, priv, i) {
2028                 rsnd_dai_call(remove, &rdai->playback, priv);
2029                 rsnd_dai_call(remove, &rdai->capture, priv);
2030         }
2031
2032         /*
2033          * adg is very special mod which can't use rsnd_dai_call(remove),
2034          * and it registers ADG clock on probe.
2035          * It should be unregister if probe failed.
2036          * Mainly it is assuming -EPROBE_DEFER case
2037          */
2038         rsnd_adg_remove(priv);
2039
2040         return ret;
2041 }
2042
2043 static void rsnd_remove(struct platform_device *pdev)
2044 {
2045         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
2046         struct rsnd_dai *rdai;
2047         void (*remove_func[])(struct rsnd_priv *priv) = {
2048                 rsnd_ssi_remove,
2049                 rsnd_ssiu_remove,
2050                 rsnd_src_remove,
2051                 rsnd_ctu_remove,
2052                 rsnd_mix_remove,
2053                 rsnd_dvc_remove,
2054                 rsnd_cmd_remove,
2055                 rsnd_adg_remove,
2056         };
2057         int i;
2058
2059         pm_runtime_disable(&pdev->dev);
2060
2061         for_each_rsnd_dai(rdai, priv, i) {
2062                 int ret;
2063
2064                 ret = rsnd_dai_call(remove, &rdai->playback, priv);
2065                 if (ret)
2066                         dev_warn(&pdev->dev, "Failed to remove playback dai #%d\n", i);
2067
2068                 ret = rsnd_dai_call(remove, &rdai->capture, priv);
2069                 if (ret)
2070                         dev_warn(&pdev->dev, "Failed to remove capture dai #%d\n", i);
2071         }
2072
2073         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
2074                 remove_func[i](priv);
2075 }
2076
2077 static int __maybe_unused rsnd_suspend(struct device *dev)
2078 {
2079         struct rsnd_priv *priv = dev_get_drvdata(dev);
2080
2081         rsnd_adg_clk_disable(priv);
2082
2083         return 0;
2084 }
2085
2086 static int __maybe_unused rsnd_resume(struct device *dev)
2087 {
2088         struct rsnd_priv *priv = dev_get_drvdata(dev);
2089
2090         rsnd_adg_clk_enable(priv);
2091
2092         return 0;
2093 }
2094
2095 static const struct dev_pm_ops rsnd_pm_ops = {
2096         SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2097 };
2098
2099 static struct platform_driver rsnd_driver = {
2100         .driver = {
2101                 .name   = "rcar_sound",
2102                 .pm     = &rsnd_pm_ops,
2103                 .of_match_table = rsnd_of_match,
2104         },
2105         .probe          = rsnd_probe,
2106         .remove_new     = rsnd_remove,
2107 };
2108 module_platform_driver(rsnd_driver);
2109
2110 MODULE_LICENSE("GPL v2");
2111 MODULE_DESCRIPTION("Renesas R-Car audio driver");
2112 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2113 MODULE_ALIAS("platform:rcar-pcm-audio");