2 * Renesas R-Car SRU/SCU/SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
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.
16 * Renesas R-Car sound device structure
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
23 * - CTU : Channel Count Conversion Unit
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
33 * - CTU : Channel Count Conversion Unit
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
45 * | ** this depends on Gen1/Gen2
49 * | ** these depend on data path
50 * | ** gen and platform data control it
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
67 * | ** these control ssi
76 * | ** these control scu
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
96 #include <linux/pm_runtime.h>
97 #include <linux/shdma-base.h>
100 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
101 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
104 * rsnd_platform functions
106 #define rsnd_platform_call(priv, dai, func, param...) \
107 (!(priv->info->func) ? 0 : \
108 priv->info->func(param))
113 char *rsnd_mod_name(struct rsnd_mod *mod)
115 if (!mod || !mod->ops)
118 return mod->ops->name;
121 void rsnd_mod_init(struct rsnd_priv *priv,
122 struct rsnd_mod *mod,
123 struct rsnd_mod_ops *ops,
124 enum rsnd_mod_type type,
136 static void __rsnd_dma_start(struct rsnd_dma *dma);
137 static void rsnd_dma_continue(struct rsnd_dma *dma)
139 /* push next A or B plane */
140 dma->submit_loop = 1;
141 schedule_work(&dma->work);
144 void rsnd_dma_start(struct rsnd_dma *dma)
146 /* push both A and B plane*/
148 dma->submit_loop = 2;
149 __rsnd_dma_start(dma);
152 void rsnd_dma_stop(struct rsnd_dma *dma)
154 dma->submit_loop = 0;
155 cancel_work_sync(&dma->work);
156 dmaengine_terminate_all(dma->chan);
159 static void rsnd_dma_complete(void *data)
161 struct rsnd_dma *dma = (struct rsnd_dma *)data;
162 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
163 struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
164 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
167 rsnd_lock(priv, flags);
170 * Renesas sound Gen1 needs 1 DMAC,
172 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
173 * But, Audio-DMAC-peri-peri doesn't have interrupt,
174 * and this driver is assuming that here.
176 * If Audio-DMAC-peri-peri has interrpt,
177 * rsnd_dai_pointer_update() will be called twice,
178 * ant it will breaks io->byte_pos
181 rsnd_dai_pointer_update(io, io->byte_per_period);
183 if (dma->submit_loop)
184 rsnd_dma_continue(dma);
186 rsnd_unlock(priv, flags);
189 static void __rsnd_dma_start(struct rsnd_dma *dma)
191 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
192 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
193 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
194 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
195 struct device *dev = rsnd_priv_to_dev(priv);
196 struct dma_async_tx_descriptor *desc;
198 size_t len = io->byte_per_period;
201 for (i = 0; i < dma->submit_loop; i++) {
203 buf = runtime->dma_addr +
204 rsnd_dai_pointer_offset(io, dma->offset + len);
207 desc = dmaengine_prep_slave_single(
208 dma->chan, buf, len, dma->dir,
209 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
211 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
215 desc->callback = rsnd_dma_complete;
216 desc->callback_param = dma;
218 if (dmaengine_submit(desc) < 0) {
219 dev_err(dev, "dmaengine_submit() fail\n");
223 dma_async_issue_pending(dma->chan);
227 static void rsnd_dma_do_work(struct work_struct *work)
229 struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
231 __rsnd_dma_start(dma);
234 int rsnd_dma_available(struct rsnd_dma *dma)
239 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
242 struct device *dev = rsnd_priv_to_dev(priv);
243 struct dma_slave_config cfg;
248 dev_err(dev, "it already has dma channel\n");
253 dma_cap_set(DMA_SLAVE, mask);
255 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
257 is_play ? "tx" : "rx");
259 dev_err(dev, "can't get dma channel\n");
264 cfg.dst_addr = 0; /* use default addr when playback */
265 cfg.src_addr = 0; /* use default addr when capture */
266 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
268 ret = dmaengine_slave_config(dma->chan, &cfg);
270 goto rsnd_dma_init_err;
272 dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
273 INIT_WORK(&dma->work, rsnd_dma_do_work);
278 rsnd_dma_quit(priv, dma);
283 void rsnd_dma_quit(struct rsnd_priv *priv,
284 struct rsnd_dma *dma)
287 dma_release_channel(dma->chan);
295 #define __rsnd_mod_call(mod, func, rdai, io) \
297 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
298 struct device *dev = rsnd_priv_to_dev(priv); \
299 dev_dbg(dev, "%s [%d] %s\n", \
300 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
301 (mod)->ops->func(mod, rdai, io); \
304 #define rsnd_mod_call(mod, func, rdai, io) \
305 (!(mod) ? -ENODEV : \
306 !((mod)->ops->func) ? 0 : \
307 __rsnd_mod_call(mod, func, (rdai), (io)))
309 #define rsnd_dai_call(rdai, io, fn) \
311 struct rsnd_mod *mod; \
313 for (i = 0; i < RSND_MOD_MAX; i++) { \
314 mod = (io)->mod[i]; \
317 ret = rsnd_mod_call(mod, fn, (rdai), (io)); \
324 static int rsnd_dai_connect(struct rsnd_mod *mod,
325 struct rsnd_dai_stream *io)
330 if (io->mod[mod->type]) {
331 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
332 struct device *dev = rsnd_priv_to_dev(priv);
334 dev_err(dev, "%s%d is not empty\n",
340 io->mod[mod->type] = mod;
346 static int rsnd_dai_disconnect(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
348 io->mod[mod->type] = NULL;
354 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
356 int id = rdai - priv->rdai;
358 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
364 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
366 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
369 return priv->rdai + id;
372 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
374 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
376 return rsnd_dai_get(priv, dai->id);
379 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
381 return &rdai->playback == io;
385 * rsnd_soc_dai functions
387 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
389 struct snd_pcm_substream *substream = io->substream;
390 struct snd_pcm_runtime *runtime = substream->runtime;
391 int pos = io->byte_pos + additional;
393 pos %= (runtime->periods * io->byte_per_period);
398 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
400 io->byte_pos += byte;
402 if (io->byte_pos >= io->next_period_byte) {
403 struct snd_pcm_substream *substream = io->substream;
404 struct snd_pcm_runtime *runtime = substream->runtime;
407 io->next_period_byte += io->byte_per_period;
409 if (io->period_pos >= runtime->periods) {
412 io->next_period_byte = io->byte_per_period;
415 snd_pcm_period_elapsed(substream);
419 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
420 struct snd_pcm_substream *substream)
422 struct snd_pcm_runtime *runtime = substream->runtime;
424 io->substream = substream;
427 io->byte_per_period = runtime->period_size *
429 samples_to_bytes(runtime, 1);
430 io->next_period_byte = io->byte_per_period;
436 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
438 struct snd_soc_pcm_runtime *rtd = substream->private_data;
444 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
445 struct snd_pcm_substream *substream)
447 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
448 return &rdai->playback;
450 return &rdai->capture;
453 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
454 struct snd_soc_dai *dai)
456 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
457 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
458 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
459 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
460 rsnd_dai_id(priv, rdai),
461 rsnd_dai_is_play(rdai, io));
462 int ssi_id = rsnd_mod_id(mod);
466 rsnd_lock(priv, flags);
469 case SNDRV_PCM_TRIGGER_START:
470 ret = rsnd_dai_stream_init(io, substream);
472 goto dai_trigger_end;
474 ret = rsnd_platform_call(priv, dai, start, ssi_id);
476 goto dai_trigger_end;
478 ret = rsnd_dai_call(rdai, io, init);
480 goto dai_trigger_end;
482 ret = rsnd_dai_call(rdai, io, start);
484 goto dai_trigger_end;
486 case SNDRV_PCM_TRIGGER_STOP:
487 ret = rsnd_dai_call(rdai, io, stop);
489 goto dai_trigger_end;
491 ret = rsnd_dai_call(rdai, io, quit);
493 goto dai_trigger_end;
495 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
497 goto dai_trigger_end;
504 rsnd_unlock(priv, flags);
509 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
511 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
513 /* set master/slave audio interface */
514 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
515 case SND_SOC_DAIFMT_CBM_CFM:
516 rdai->clk_master = 1;
518 case SND_SOC_DAIFMT_CBS_CFS:
519 rdai->clk_master = 0;
525 /* set clock inversion */
526 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
527 case SND_SOC_DAIFMT_NB_IF:
528 rdai->bit_clk_inv = 0;
529 rdai->frm_clk_inv = 1;
531 case SND_SOC_DAIFMT_IB_NF:
532 rdai->bit_clk_inv = 1;
533 rdai->frm_clk_inv = 0;
535 case SND_SOC_DAIFMT_IB_IF:
536 rdai->bit_clk_inv = 1;
537 rdai->frm_clk_inv = 1;
539 case SND_SOC_DAIFMT_NB_NF:
541 rdai->bit_clk_inv = 0;
542 rdai->frm_clk_inv = 0;
547 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
548 case SND_SOC_DAIFMT_I2S:
550 rdai->data_alignment = 0;
552 case SND_SOC_DAIFMT_LEFT_J:
554 rdai->data_alignment = 0;
556 case SND_SOC_DAIFMT_RIGHT_J:
558 rdai->data_alignment = 1;
565 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
566 .trigger = rsnd_soc_dai_trigger,
567 .set_fmt = rsnd_soc_dai_set_fmt,
570 static int rsnd_path_init(struct rsnd_priv *priv,
571 struct rsnd_dai *rdai,
572 struct rsnd_dai_stream *io)
574 struct rsnd_mod *mod;
579 * Gen1 is created by SRU/SSI, and this SRU is base module of
580 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
583 * Gen1 SRU = Gen2 SCU + SSIU + etc
585 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
588 * Then, SSI id = SCU id here
591 mod = rsnd_ssi_mod_get_frm_dai(priv,
592 rsnd_dai_id(priv, rdai),
593 rsnd_dai_is_play(rdai, io));
596 id = rsnd_mod_id(mod);
600 mod = rsnd_scu_mod_get(priv, id);
602 ret = rsnd_dai_connect(mod, io);
608 mod = rsnd_ssi_mod_get(priv, id);
610 ret = rsnd_dai_connect(mod, io);
618 static int rsnd_path_exit(struct rsnd_priv *priv,
619 struct rsnd_dai *rdai,
620 struct rsnd_dai_stream *io)
622 struct rsnd_mod *mod;
626 * remove all mod from rdai
628 for (i = 0; i < RSND_MOD_MAX; i++) {
632 ret |= rsnd_dai_disconnect(mod, io);
638 static int rsnd_dai_probe(struct platform_device *pdev,
639 struct rsnd_priv *priv)
641 struct snd_soc_dai_driver *drv;
642 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
643 struct rsnd_dai *rdai;
644 struct rsnd_mod *pmod, *cmod;
645 struct device *dev = rsnd_priv_to_dev(priv);
646 int dai_nr = info->dai_info_nr;
650 * dai_nr should be set via dai_info_nr,
651 * but allow it to keeping compatible
655 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
656 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
657 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
665 dev_err(dev, "no dai\n");
669 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
670 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
672 dev_err(dev, "dai allocate failed\n");
676 priv->rdai_nr = dai_nr;
680 for (i = 0; i < dai_nr; i++) {
682 rdai[i].info = &info->dai_info[i];
684 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
685 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
690 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
693 * init snd_soc_dai_driver
695 drv[i].name = rdai[i].name;
696 drv[i].ops = &rsnd_soc_dai_ops;
698 drv[i].playback.rates = RSND_RATES;
699 drv[i].playback.formats = RSND_FMTS;
700 drv[i].playback.channels_min = 2;
701 drv[i].playback.channels_max = 2;
702 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
705 drv[i].capture.rates = RSND_RATES;
706 drv[i].capture.formats = RSND_FMTS;
707 drv[i].capture.channels_min = 2;
708 drv[i].capture.channels_max = 2;
709 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
712 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
713 pmod ? "play" : " -- ",
714 cmod ? "capture" : " -- ");
720 static void rsnd_dai_remove(struct platform_device *pdev,
721 struct rsnd_priv *priv)
723 struct rsnd_dai *rdai;
726 for (i = 0; i < rsnd_rdai_nr(priv); i++) {
727 rdai = rsnd_dai_get(priv, i);
728 rsnd_path_exit(priv, rdai, &rdai->playback);
729 rsnd_path_exit(priv, rdai, &rdai->capture);
736 static struct snd_pcm_hardware rsnd_pcm_hardware = {
737 .info = SNDRV_PCM_INFO_INTERLEAVED |
738 SNDRV_PCM_INFO_MMAP |
739 SNDRV_PCM_INFO_MMAP_VALID |
740 SNDRV_PCM_INFO_PAUSE,
741 .buffer_bytes_max = 64 * 1024,
742 .period_bytes_min = 32,
743 .period_bytes_max = 8192,
749 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
751 struct snd_pcm_runtime *runtime = substream->runtime;
754 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
756 ret = snd_pcm_hw_constraint_integer(runtime,
757 SNDRV_PCM_HW_PARAM_PERIODS);
762 static int rsnd_hw_params(struct snd_pcm_substream *substream,
763 struct snd_pcm_hw_params *hw_params)
765 return snd_pcm_lib_malloc_pages(substream,
766 params_buffer_bytes(hw_params));
769 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
771 struct snd_pcm_runtime *runtime = substream->runtime;
772 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
773 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
774 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
776 return bytes_to_frames(runtime, io->byte_pos);
779 static struct snd_pcm_ops rsnd_pcm_ops = {
780 .open = rsnd_pcm_open,
781 .ioctl = snd_pcm_lib_ioctl,
782 .hw_params = rsnd_hw_params,
783 .hw_free = snd_pcm_lib_free_pages,
784 .pointer = rsnd_pointer,
791 #define PREALLOC_BUFFER (32 * 1024)
792 #define PREALLOC_BUFFER_MAX (32 * 1024)
794 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
796 return snd_pcm_lib_preallocate_pages_for_all(
799 rtd->card->snd_card->dev,
800 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
803 static void rsnd_pcm_free(struct snd_pcm *pcm)
805 snd_pcm_lib_preallocate_free_for_all(pcm);
808 static struct snd_soc_platform_driver rsnd_soc_platform = {
809 .ops = &rsnd_pcm_ops,
810 .pcm_new = rsnd_pcm_new,
811 .pcm_free = rsnd_pcm_free,
814 static const struct snd_soc_component_driver rsnd_soc_component = {
821 static int rsnd_probe(struct platform_device *pdev)
823 struct rcar_snd_info *info;
824 struct rsnd_priv *priv;
825 struct device *dev = &pdev->dev;
826 int (*probe_func[])(struct platform_device *pdev,
827 struct rsnd_priv *priv) = {
836 info = pdev->dev.platform_data;
838 dev_err(dev, "driver needs R-Car sound information\n");
845 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
847 dev_err(dev, "priv allocate failed\n");
853 spin_lock_init(&priv->lock);
858 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
859 ret = probe_func[i](pdev, priv);
867 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
869 dev_err(dev, "cannot snd soc register\n");
873 ret = snd_soc_register_component(dev, &rsnd_soc_component,
874 priv->daidrv, rsnd_rdai_nr(priv));
876 dev_err(dev, "cannot snd dai register\n");
880 dev_set_drvdata(dev, priv);
882 pm_runtime_enable(dev);
884 dev_info(dev, "probed\n");
888 snd_soc_unregister_platform(dev);
893 static int rsnd_remove(struct platform_device *pdev)
895 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
897 pm_runtime_disable(&pdev->dev);
902 rsnd_ssi_remove(pdev, priv);
903 rsnd_adg_remove(pdev, priv);
904 rsnd_scu_remove(pdev, priv);
905 rsnd_dai_remove(pdev, priv);
906 rsnd_gen_remove(pdev, priv);
911 static struct platform_driver rsnd_driver = {
913 .name = "rcar_sound",
916 .remove = rsnd_remove,
918 module_platform_driver(rsnd_driver);
920 MODULE_LICENSE("GPL");
921 MODULE_DESCRIPTION("Renesas R-Car audio driver");
922 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
923 MODULE_ALIAS("platform:rcar-pcm-audio");