]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/sh/rcar/core.c
ASoC: add Renesas R-Car SCU feature
[karo-tx-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 scu
77  *   |
78  *   +- scu
79  *      |
80  *      +- scu[0]
81  *      +- scu[1]
82  *      +- scu[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_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
101
102 /*
103  *      rsnd_platform functions
104  */
105 #define rsnd_platform_call(priv, dai, func, param...)   \
106         (!(priv->info->func) ? -ENODEV :                \
107          priv->info->func(param))
108
109
110 /*
111  *      basic function
112  */
113 u32 rsnd_read(struct rsnd_priv *priv,
114               struct rsnd_mod *mod, enum rsnd_reg reg)
115 {
116         void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
117
118         BUG_ON(!base);
119
120         return ioread32(base);
121 }
122
123 void rsnd_write(struct rsnd_priv *priv,
124                 struct rsnd_mod *mod,
125                 enum rsnd_reg reg, u32 data)
126 {
127         void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
128         struct device *dev = rsnd_priv_to_dev(priv);
129
130         BUG_ON(!base);
131
132         dev_dbg(dev, "w %p : %08x\n", base, data);
133
134         iowrite32(data, base);
135 }
136
137 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod,
138                enum rsnd_reg reg, u32 mask, u32 data)
139 {
140         void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
141         struct device *dev = rsnd_priv_to_dev(priv);
142         u32 val;
143
144         BUG_ON(!base);
145
146         val = ioread32(base);
147         val &= ~mask;
148         val |= data & mask;
149         iowrite32(val, base);
150
151         dev_dbg(dev, "s %p : %08x\n", base, val);
152 }
153
154 /*
155  *      rsnd_mod functions
156  */
157 char *rsnd_mod_name(struct rsnd_mod *mod)
158 {
159         if (!mod || !mod->ops)
160                 return "unknown";
161
162         return mod->ops->name;
163 }
164
165 void rsnd_mod_init(struct rsnd_priv *priv,
166                    struct rsnd_mod *mod,
167                    struct rsnd_mod_ops *ops,
168                    int id)
169 {
170         mod->priv       = priv;
171         mod->id         = id;
172         mod->ops        = ops;
173         INIT_LIST_HEAD(&mod->list);
174 }
175
176 /*
177  *      rsnd_dai functions
178  */
179 #define rsnd_dai_call(rdai, io, fn)                     \
180 ({                                                      \
181         struct rsnd_mod *mod, *n;                       \
182         int ret = 0;                                    \
183         for_each_rsnd_mod(mod, n, io) {                 \
184                 ret = rsnd_mod_call(mod, fn, rdai, io); \
185                 if (ret < 0)                            \
186                         break;                          \
187         }                                               \
188         ret;                                            \
189 })
190
191 int rsnd_dai_connect(struct rsnd_dai *rdai,
192                      struct rsnd_mod *mod,
193                      struct rsnd_dai_stream *io)
194 {
195         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
196         struct device *dev = rsnd_priv_to_dev(priv);
197
198         if (!mod) {
199                 dev_err(dev, "NULL mod\n");
200                 return -EIO;
201         }
202
203         if (!list_empty(&mod->list)) {
204                 dev_err(dev, "%s%d is not empty\n",
205                         rsnd_mod_name(mod),
206                         rsnd_mod_id(mod));
207                 return -EIO;
208         }
209
210         list_add_tail(&mod->list, &io->head);
211
212         return 0;
213 }
214
215 int rsnd_dai_disconnect(struct rsnd_mod *mod)
216 {
217         list_del_init(&mod->list);
218
219         return 0;
220 }
221
222 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
223 {
224         return priv->rdai + id;
225 }
226
227 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
228 {
229         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
230
231         return rsnd_dai_get(priv, dai->id);
232 }
233
234 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
235 {
236         return &rdai->playback == io;
237 }
238
239 /*
240  *      rsnd_soc_dai functions
241  */
242 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
243 {
244         struct snd_pcm_substream *substream = io->substream;
245         struct snd_pcm_runtime *runtime = substream->runtime;
246         int pos = io->byte_pos + additional;
247
248         pos %= (runtime->periods * io->byte_per_period);
249
250         return pos;
251 }
252
253 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
254 {
255         io->byte_pos += byte;
256
257         if (io->byte_pos >= io->next_period_byte) {
258                 struct snd_pcm_substream *substream = io->substream;
259                 struct snd_pcm_runtime *runtime = substream->runtime;
260
261                 io->period_pos++;
262                 io->next_period_byte += io->byte_per_period;
263
264                 if (io->period_pos >= runtime->periods) {
265                         io->byte_pos = 0;
266                         io->period_pos = 0;
267                         io->next_period_byte = io->byte_per_period;
268                 }
269
270                 snd_pcm_period_elapsed(substream);
271         }
272 }
273
274 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
275                                 struct snd_pcm_substream *substream)
276 {
277         struct snd_pcm_runtime *runtime = substream->runtime;
278
279         if (!list_empty(&io->head))
280                 return -EIO;
281
282         INIT_LIST_HEAD(&io->head);
283         io->substream           = substream;
284         io->byte_pos            = 0;
285         io->period_pos          = 0;
286         io->byte_per_period     = runtime->period_size *
287                                   runtime->channels *
288                                   samples_to_bytes(runtime, 1);
289         io->next_period_byte    = io->byte_per_period;
290
291         return 0;
292 }
293
294 static
295 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
296 {
297         struct snd_soc_pcm_runtime *rtd = substream->private_data;
298
299         return  rtd->cpu_dai;
300 }
301
302 static
303 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
304                                         struct snd_pcm_substream *substream)
305 {
306         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
307                 return &rdai->playback;
308         else
309                 return &rdai->capture;
310 }
311
312 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
313                             struct snd_soc_dai *dai)
314 {
315         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
316         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
317         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
318         struct rsnd_dai_platform_info *info = rsnd_dai_get_platform_info(rdai);
319         int ssi_id = rsnd_dai_is_play(rdai, io) ?       info->ssi_id_playback :
320                                                         info->ssi_id_capture;
321         int ret;
322         unsigned long flags;
323
324         rsnd_lock(priv, flags);
325
326         switch (cmd) {
327         case SNDRV_PCM_TRIGGER_START:
328                 ret = rsnd_dai_stream_init(io, substream);
329                 if (ret < 0)
330                         goto dai_trigger_end;
331
332                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
333                 if (ret < 0)
334                         goto dai_trigger_end;
335
336                 ret = rsnd_gen_path_init(priv, rdai, io);
337                 if (ret < 0)
338                         goto dai_trigger_end;
339
340                 ret = rsnd_dai_call(rdai, io, init);
341                 if (ret < 0)
342                         goto dai_trigger_end;
343
344                 ret = rsnd_dai_call(rdai, io, start);
345                 if (ret < 0)
346                         goto dai_trigger_end;
347                 break;
348         case SNDRV_PCM_TRIGGER_STOP:
349                 ret = rsnd_dai_call(rdai, io, stop);
350                 if (ret < 0)
351                         goto dai_trigger_end;
352
353                 ret = rsnd_dai_call(rdai, io, quit);
354                 if (ret < 0)
355                         goto dai_trigger_end;
356
357                 ret = rsnd_gen_path_exit(priv, rdai, io);
358                 if (ret < 0)
359                         goto dai_trigger_end;
360
361                 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
362                 if (ret < 0)
363                         goto dai_trigger_end;
364                 break;
365         default:
366                 ret = -EINVAL;
367         }
368
369 dai_trigger_end:
370         rsnd_unlock(priv, flags);
371
372         return ret;
373 }
374
375 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
376 {
377         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
378
379         /* set master/slave audio interface */
380         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
381         case SND_SOC_DAIFMT_CBM_CFM:
382                 rdai->clk_master = 1;
383                 break;
384         case SND_SOC_DAIFMT_CBS_CFS:
385                 rdai->clk_master = 0;
386                 break;
387         default:
388                 return -EINVAL;
389         }
390
391         /* set clock inversion */
392         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
393         case SND_SOC_DAIFMT_NB_IF:
394                 rdai->bit_clk_inv = 0;
395                 rdai->frm_clk_inv = 1;
396                 break;
397         case SND_SOC_DAIFMT_IB_NF:
398                 rdai->bit_clk_inv = 1;
399                 rdai->frm_clk_inv = 0;
400                 break;
401         case SND_SOC_DAIFMT_IB_IF:
402                 rdai->bit_clk_inv = 1;
403                 rdai->frm_clk_inv = 1;
404                 break;
405         case SND_SOC_DAIFMT_NB_NF:
406         default:
407                 rdai->bit_clk_inv = 0;
408                 rdai->frm_clk_inv = 0;
409                 break;
410         }
411
412         /* set format */
413         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
414         case SND_SOC_DAIFMT_I2S:
415                 rdai->sys_delay = 0;
416                 rdai->data_alignment = 0;
417                 break;
418         case SND_SOC_DAIFMT_LEFT_J:
419                 rdai->sys_delay = 1;
420                 rdai->data_alignment = 0;
421                 break;
422         case SND_SOC_DAIFMT_RIGHT_J:
423                 rdai->sys_delay = 1;
424                 rdai->data_alignment = 1;
425                 break;
426         }
427
428         return 0;
429 }
430
431 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
432         .trigger        = rsnd_soc_dai_trigger,
433         .set_fmt        = rsnd_soc_dai_set_fmt,
434 };
435
436 static int rsnd_dai_probe(struct platform_device *pdev,
437                           struct rcar_snd_info *info,
438                           struct rsnd_priv *priv)
439 {
440         struct snd_soc_dai_driver *drv;
441         struct rsnd_dai *rdai;
442         struct device *dev = rsnd_priv_to_dev(priv);
443         struct rsnd_dai_platform_info *dai_info;
444         int dai_nr = info->dai_info_nr;
445         int i, pid, cid;
446
447         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
448         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
449         if (!drv || !rdai) {
450                 dev_err(dev, "dai allocate failed\n");
451                 return -ENOMEM;
452         }
453
454         for (i = 0; i < dai_nr; i++) {
455                 dai_info = &info->dai_info[i];
456
457                 pid = dai_info->ssi_id_playback;
458                 cid = dai_info->ssi_id_capture;
459
460                 /*
461                  *      init rsnd_dai
462                  */
463                 INIT_LIST_HEAD(&rdai[i].playback.head);
464                 INIT_LIST_HEAD(&rdai[i].capture.head);
465
466                 rdai[i].info = dai_info;
467
468                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
469
470                 /*
471                  *      init snd_soc_dai_driver
472                  */
473                 drv[i].name     = rdai[i].name;
474                 drv[i].ops      = &rsnd_soc_dai_ops;
475                 if (pid >= 0) {
476                         drv[i].playback.rates           = RSND_RATES;
477                         drv[i].playback.formats         = RSND_FMTS;
478                         drv[i].playback.channels_min    = 2;
479                         drv[i].playback.channels_max    = 2;
480                 }
481                 if (cid >= 0) {
482                         drv[i].capture.rates            = RSND_RATES;
483                         drv[i].capture.formats          = RSND_FMTS;
484                         drv[i].capture.channels_min     = 2;
485                         drv[i].capture.channels_max     = 2;
486                 }
487
488                 dev_dbg(dev, "%s (%d, %d) probed", rdai[i].name, pid, cid);
489         }
490
491         priv->dai_nr    = dai_nr;
492         priv->daidrv    = drv;
493         priv->rdai      = rdai;
494
495         return 0;
496 }
497
498 static void rsnd_dai_remove(struct platform_device *pdev,
499                           struct rsnd_priv *priv)
500 {
501 }
502
503 /*
504  *              pcm ops
505  */
506 static struct snd_pcm_hardware rsnd_pcm_hardware = {
507         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
508                         SNDRV_PCM_INFO_MMAP             |
509                         SNDRV_PCM_INFO_MMAP_VALID       |
510                         SNDRV_PCM_INFO_PAUSE,
511         .formats                = RSND_FMTS,
512         .rates                  = RSND_RATES,
513         .rate_min               = 8000,
514         .rate_max               = 192000,
515         .channels_min           = 2,
516         .channels_max           = 2,
517         .buffer_bytes_max       = 64 * 1024,
518         .period_bytes_min       = 32,
519         .period_bytes_max       = 8192,
520         .periods_min            = 1,
521         .periods_max            = 32,
522         .fifo_size              = 256,
523 };
524
525 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
526 {
527         struct snd_pcm_runtime *runtime = substream->runtime;
528         int ret = 0;
529
530         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
531
532         ret = snd_pcm_hw_constraint_integer(runtime,
533                                             SNDRV_PCM_HW_PARAM_PERIODS);
534
535         return ret;
536 }
537
538 static int rsnd_hw_params(struct snd_pcm_substream *substream,
539                          struct snd_pcm_hw_params *hw_params)
540 {
541         return snd_pcm_lib_malloc_pages(substream,
542                                         params_buffer_bytes(hw_params));
543 }
544
545 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
546 {
547         struct snd_pcm_runtime *runtime = substream->runtime;
548         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
549         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
550         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
551
552         return bytes_to_frames(runtime, io->byte_pos);
553 }
554
555 static struct snd_pcm_ops rsnd_pcm_ops = {
556         .open           = rsnd_pcm_open,
557         .ioctl          = snd_pcm_lib_ioctl,
558         .hw_params      = rsnd_hw_params,
559         .hw_free        = snd_pcm_lib_free_pages,
560         .pointer        = rsnd_pointer,
561 };
562
563 /*
564  *              snd_soc_platform
565  */
566
567 #define PREALLOC_BUFFER         (32 * 1024)
568 #define PREALLOC_BUFFER_MAX     (32 * 1024)
569
570 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
571 {
572         return snd_pcm_lib_preallocate_pages_for_all(
573                 rtd->pcm,
574                 SNDRV_DMA_TYPE_DEV,
575                 rtd->card->snd_card->dev,
576                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
577 }
578
579 static void rsnd_pcm_free(struct snd_pcm *pcm)
580 {
581         snd_pcm_lib_preallocate_free_for_all(pcm);
582 }
583
584 static struct snd_soc_platform_driver rsnd_soc_platform = {
585         .ops            = &rsnd_pcm_ops,
586         .pcm_new        = rsnd_pcm_new,
587         .pcm_free       = rsnd_pcm_free,
588 };
589
590 static const struct snd_soc_component_driver rsnd_soc_component = {
591         .name           = "rsnd",
592 };
593
594 /*
595  *      rsnd probe
596  */
597 static int rsnd_probe(struct platform_device *pdev)
598 {
599         struct rcar_snd_info *info;
600         struct rsnd_priv *priv;
601         struct device *dev = &pdev->dev;
602         int ret;
603
604         info = pdev->dev.platform_data;
605         if (!info) {
606                 dev_err(dev, "driver needs R-Car sound information\n");
607                 return -ENODEV;
608         }
609
610         /*
611          *      init priv data
612          */
613         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
614         if (!priv) {
615                 dev_err(dev, "priv allocate failed\n");
616                 return -ENODEV;
617         }
618
619         priv->dev       = dev;
620         priv->info      = info;
621         spin_lock_init(&priv->lock);
622
623         /*
624          *      init each module
625          */
626         ret = rsnd_gen_probe(pdev, info, priv);
627         if (ret < 0)
628                 return ret;
629
630         ret = rsnd_dai_probe(pdev, info, priv);
631         if (ret < 0)
632                 return ret;
633
634         ret = rsnd_scu_probe(pdev, info, priv);
635         if (ret < 0)
636                 return ret;
637
638         /*
639          *      asoc register
640          */
641         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
642         if (ret < 0) {
643                 dev_err(dev, "cannot snd soc register\n");
644                 return ret;
645         }
646
647         ret = snd_soc_register_component(dev, &rsnd_soc_component,
648                                          priv->daidrv, rsnd_dai_nr(priv));
649         if (ret < 0) {
650                 dev_err(dev, "cannot snd dai register\n");
651                 goto exit_snd_soc;
652         }
653
654         dev_set_drvdata(dev, priv);
655
656         pm_runtime_enable(dev);
657
658         dev_info(dev, "probed\n");
659         return ret;
660
661 exit_snd_soc:
662         snd_soc_unregister_platform(dev);
663
664         return ret;
665 }
666
667 static int rsnd_remove(struct platform_device *pdev)
668 {
669         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
670
671         pm_runtime_disable(&pdev->dev);
672
673         /*
674          *      remove each module
675          */
676         rsnd_scu_remove(pdev, priv);
677         rsnd_dai_remove(pdev, priv);
678         rsnd_gen_remove(pdev, priv);
679
680         return 0;
681 }
682
683 static struct platform_driver rsnd_driver = {
684         .driver = {
685                 .name   = "rcar_sound",
686         },
687         .probe          = rsnd_probe,
688         .remove         = rsnd_remove,
689 };
690 module_platform_driver(rsnd_driver);
691
692 MODULE_LICENSE("GPL");
693 MODULE_DESCRIPTION("Renesas R-Car audio driver");
694 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
695 MODULE_ALIAS("platform:rcar-pcm-audio");