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