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