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))
110 #define rsnd_is_enable_path(io, name) \
111 ((io)->info ? (io)->info->name : NULL)
112 #define rsnd_info_id(priv, io, name) \
113 ((io)->info->name - priv->info->name##_info)
118 char *rsnd_mod_name(struct rsnd_mod *mod)
120 if (!mod || !mod->ops)
123 return mod->ops->name;
126 void rsnd_mod_init(struct rsnd_priv *priv,
127 struct rsnd_mod *mod,
128 struct rsnd_mod_ops *ops,
129 enum rsnd_mod_type type,
141 static void __rsnd_dma_start(struct rsnd_dma *dma);
142 static void rsnd_dma_continue(struct rsnd_dma *dma)
144 /* push next A or B plane */
145 dma->submit_loop = 1;
146 schedule_work(&dma->work);
149 void rsnd_dma_start(struct rsnd_dma *dma)
151 /* push both A and B plane*/
153 dma->submit_loop = 2;
154 __rsnd_dma_start(dma);
157 void rsnd_dma_stop(struct rsnd_dma *dma)
159 dma->submit_loop = 0;
160 cancel_work_sync(&dma->work);
161 dmaengine_terminate_all(dma->chan);
164 static void rsnd_dma_complete(void *data)
166 struct rsnd_dma *dma = (struct rsnd_dma *)data;
167 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
168 struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
169 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
172 rsnd_lock(priv, flags);
175 * Renesas sound Gen1 needs 1 DMAC,
177 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
178 * But, Audio-DMAC-peri-peri doesn't have interrupt,
179 * and this driver is assuming that here.
181 * If Audio-DMAC-peri-peri has interrpt,
182 * rsnd_dai_pointer_update() will be called twice,
183 * ant it will breaks io->byte_pos
186 rsnd_dai_pointer_update(io, io->byte_per_period);
188 if (dma->submit_loop)
189 rsnd_dma_continue(dma);
191 rsnd_unlock(priv, flags);
194 static void __rsnd_dma_start(struct rsnd_dma *dma)
196 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
197 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
198 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
199 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
200 struct device *dev = rsnd_priv_to_dev(priv);
201 struct dma_async_tx_descriptor *desc;
203 size_t len = io->byte_per_period;
206 for (i = 0; i < dma->submit_loop; i++) {
208 buf = runtime->dma_addr +
209 rsnd_dai_pointer_offset(io, dma->offset + len);
212 desc = dmaengine_prep_slave_single(
213 dma->chan, buf, len, dma->dir,
214 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
216 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
220 desc->callback = rsnd_dma_complete;
221 desc->callback_param = dma;
223 if (dmaengine_submit(desc) < 0) {
224 dev_err(dev, "dmaengine_submit() fail\n");
228 dma_async_issue_pending(dma->chan);
232 static void rsnd_dma_do_work(struct work_struct *work)
234 struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
236 __rsnd_dma_start(dma);
239 int rsnd_dma_available(struct rsnd_dma *dma)
244 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
247 struct device *dev = rsnd_priv_to_dev(priv);
248 struct dma_slave_config cfg;
253 dev_err(dev, "it already has dma channel\n");
258 dma_cap_set(DMA_SLAVE, mask);
260 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
262 is_play ? "tx" : "rx");
264 dev_err(dev, "can't get dma channel\n");
269 cfg.dst_addr = 0; /* use default addr when playback */
270 cfg.src_addr = 0; /* use default addr when capture */
271 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
273 ret = dmaengine_slave_config(dma->chan, &cfg);
275 goto rsnd_dma_init_err;
277 dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
278 INIT_WORK(&dma->work, rsnd_dma_do_work);
283 rsnd_dma_quit(priv, dma);
288 void rsnd_dma_quit(struct rsnd_priv *priv,
289 struct rsnd_dma *dma)
292 dma_release_channel(dma->chan);
300 #define __rsnd_mod_call(mod, func, rdai, io) \
302 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
303 struct device *dev = rsnd_priv_to_dev(priv); \
304 dev_dbg(dev, "%s [%d] %s\n", \
305 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
306 (mod)->ops->func(mod, rdai, io); \
309 #define rsnd_mod_call(mod, func, rdai, io) \
310 (!(mod) ? -ENODEV : \
311 !((mod)->ops->func) ? 0 : \
312 __rsnd_mod_call(mod, func, (rdai), (io)))
314 #define rsnd_dai_call(rdai, io, fn) \
316 struct rsnd_mod *mod; \
318 for (i = 0; i < RSND_MOD_MAX; i++) { \
319 mod = (io)->mod[i]; \
322 ret = rsnd_mod_call(mod, fn, (rdai), (io)); \
329 static int rsnd_dai_connect(struct rsnd_mod *mod,
330 struct rsnd_dai_stream *io)
335 if (io->mod[mod->type]) {
336 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
337 struct device *dev = rsnd_priv_to_dev(priv);
339 dev_err(dev, "%s%d is not empty\n",
345 io->mod[mod->type] = mod;
351 static int rsnd_dai_disconnect(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
353 io->mod[mod->type] = NULL;
359 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
361 int id = rdai - priv->rdai;
363 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
369 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
371 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
374 return priv->rdai + id;
377 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
379 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
381 return rsnd_dai_get(priv, dai->id);
384 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
386 return &rdai->playback == io;
390 * rsnd_soc_dai functions
392 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
394 struct snd_pcm_substream *substream = io->substream;
395 struct snd_pcm_runtime *runtime = substream->runtime;
396 int pos = io->byte_pos + additional;
398 pos %= (runtime->periods * io->byte_per_period);
403 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
405 io->byte_pos += byte;
407 if (io->byte_pos >= io->next_period_byte) {
408 struct snd_pcm_substream *substream = io->substream;
409 struct snd_pcm_runtime *runtime = substream->runtime;
412 io->next_period_byte += io->byte_per_period;
414 if (io->period_pos >= runtime->periods) {
417 io->next_period_byte = io->byte_per_period;
420 snd_pcm_period_elapsed(substream);
424 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
425 struct snd_pcm_substream *substream)
427 struct snd_pcm_runtime *runtime = substream->runtime;
429 io->substream = substream;
432 io->byte_per_period = runtime->period_size *
434 samples_to_bytes(runtime, 1);
435 io->next_period_byte = io->byte_per_period;
441 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
443 struct snd_soc_pcm_runtime *rtd = substream->private_data;
449 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
450 struct snd_pcm_substream *substream)
452 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
453 return &rdai->playback;
455 return &rdai->capture;
458 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
459 struct snd_soc_dai *dai)
461 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
462 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
463 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
464 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
465 rsnd_dai_id(priv, rdai),
466 rsnd_dai_is_play(rdai, io));
467 int ssi_id = rsnd_mod_id(mod);
471 rsnd_lock(priv, flags);
474 case SNDRV_PCM_TRIGGER_START:
475 ret = rsnd_dai_stream_init(io, substream);
477 goto dai_trigger_end;
479 ret = rsnd_platform_call(priv, dai, start, ssi_id);
481 goto dai_trigger_end;
483 ret = rsnd_dai_call(rdai, io, init);
485 goto dai_trigger_end;
487 ret = rsnd_dai_call(rdai, io, start);
489 goto dai_trigger_end;
491 case SNDRV_PCM_TRIGGER_STOP:
492 ret = rsnd_dai_call(rdai, io, stop);
494 goto dai_trigger_end;
496 ret = rsnd_dai_call(rdai, io, quit);
498 goto dai_trigger_end;
500 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
502 goto dai_trigger_end;
509 rsnd_unlock(priv, flags);
514 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
516 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
518 /* set master/slave audio interface */
519 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
520 case SND_SOC_DAIFMT_CBM_CFM:
521 rdai->clk_master = 1;
523 case SND_SOC_DAIFMT_CBS_CFS:
524 rdai->clk_master = 0;
530 /* set clock inversion */
531 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
532 case SND_SOC_DAIFMT_NB_IF:
533 rdai->bit_clk_inv = 0;
534 rdai->frm_clk_inv = 1;
536 case SND_SOC_DAIFMT_IB_NF:
537 rdai->bit_clk_inv = 1;
538 rdai->frm_clk_inv = 0;
540 case SND_SOC_DAIFMT_IB_IF:
541 rdai->bit_clk_inv = 1;
542 rdai->frm_clk_inv = 1;
544 case SND_SOC_DAIFMT_NB_NF:
546 rdai->bit_clk_inv = 0;
547 rdai->frm_clk_inv = 0;
552 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
553 case SND_SOC_DAIFMT_I2S:
555 rdai->data_alignment = 0;
557 case SND_SOC_DAIFMT_LEFT_J:
559 rdai->data_alignment = 0;
561 case SND_SOC_DAIFMT_RIGHT_J:
563 rdai->data_alignment = 1;
570 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
571 .trigger = rsnd_soc_dai_trigger,
572 .set_fmt = rsnd_soc_dai_set_fmt,
575 static int rsnd_path_init(struct rsnd_priv *priv,
576 struct rsnd_dai *rdai,
577 struct rsnd_dai_stream *io)
579 struct rsnd_mod *mod;
580 struct rsnd_dai_platform_info *dai_info = rdai->info;
586 * Gen1 is created by SRU/SSI, and this SRU is base module of
587 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
590 * Gen1 SRU = Gen2 SCU + SSIU + etc
592 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
596 if (rsnd_is_enable_path(io, ssi))
597 ssi_id = rsnd_info_id(priv, io, ssi);
598 if (rsnd_is_enable_path(io, scu))
599 scu_id = rsnd_info_id(priv, io, scu);
602 mod = rsnd_ssi_mod_get_frm_dai(priv,
603 rsnd_dai_id(priv, rdai),
604 rsnd_dai_is_play(rdai, io));
607 ssi_id = scu_id = rsnd_mod_id(mod);
614 mod = rsnd_scu_mod_get(priv, scu_id);
615 ret = rsnd_dai_connect(mod, io);
622 mod = rsnd_ssi_mod_get(priv, ssi_id);
623 ret = rsnd_dai_connect(mod, io);
631 static int rsnd_path_exit(struct rsnd_priv *priv,
632 struct rsnd_dai *rdai,
633 struct rsnd_dai_stream *io)
635 struct rsnd_mod *mod;
639 * remove all mod from rdai
641 for (i = 0; i < RSND_MOD_MAX; i++) {
645 ret |= rsnd_dai_disconnect(mod, io);
651 static int rsnd_dai_probe(struct platform_device *pdev,
652 struct rsnd_priv *priv)
654 struct snd_soc_dai_driver *drv;
655 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
656 struct rsnd_dai *rdai;
657 struct rsnd_mod *pmod, *cmod;
658 struct device *dev = rsnd_priv_to_dev(priv);
659 int dai_nr = info->dai_info_nr;
663 * dai_nr should be set via dai_info_nr,
664 * but allow it to keeping compatible
668 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
669 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
670 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
678 dev_err(dev, "no dai\n");
682 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
683 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
685 dev_err(dev, "dai allocate failed\n");
689 priv->rdai_nr = dai_nr;
693 for (i = 0; i < dai_nr; i++) {
695 rdai[i].info = &info->dai_info[i];
697 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
698 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
703 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
706 * init snd_soc_dai_driver
708 drv[i].name = rdai[i].name;
709 drv[i].ops = &rsnd_soc_dai_ops;
711 drv[i].playback.rates = RSND_RATES;
712 drv[i].playback.formats = RSND_FMTS;
713 drv[i].playback.channels_min = 2;
714 drv[i].playback.channels_max = 2;
717 rdai[i].playback.info = &info->dai_info[i].playback;
718 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
721 drv[i].capture.rates = RSND_RATES;
722 drv[i].capture.formats = RSND_FMTS;
723 drv[i].capture.channels_min = 2;
724 drv[i].capture.channels_max = 2;
727 rdai[i].capture.info = &info->dai_info[i].capture;
728 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
731 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
732 pmod ? "play" : " -- ",
733 cmod ? "capture" : " -- ");
739 static void rsnd_dai_remove(struct platform_device *pdev,
740 struct rsnd_priv *priv)
742 struct rsnd_dai *rdai;
745 for (i = 0; i < rsnd_rdai_nr(priv); i++) {
746 rdai = rsnd_dai_get(priv, i);
747 rsnd_path_exit(priv, rdai, &rdai->playback);
748 rsnd_path_exit(priv, rdai, &rdai->capture);
755 static struct snd_pcm_hardware rsnd_pcm_hardware = {
756 .info = SNDRV_PCM_INFO_INTERLEAVED |
757 SNDRV_PCM_INFO_MMAP |
758 SNDRV_PCM_INFO_MMAP_VALID |
759 SNDRV_PCM_INFO_PAUSE,
760 .buffer_bytes_max = 64 * 1024,
761 .period_bytes_min = 32,
762 .period_bytes_max = 8192,
768 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
770 struct snd_pcm_runtime *runtime = substream->runtime;
773 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
775 ret = snd_pcm_hw_constraint_integer(runtime,
776 SNDRV_PCM_HW_PARAM_PERIODS);
781 static int rsnd_hw_params(struct snd_pcm_substream *substream,
782 struct snd_pcm_hw_params *hw_params)
784 return snd_pcm_lib_malloc_pages(substream,
785 params_buffer_bytes(hw_params));
788 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
790 struct snd_pcm_runtime *runtime = substream->runtime;
791 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
792 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
793 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
795 return bytes_to_frames(runtime, io->byte_pos);
798 static struct snd_pcm_ops rsnd_pcm_ops = {
799 .open = rsnd_pcm_open,
800 .ioctl = snd_pcm_lib_ioctl,
801 .hw_params = rsnd_hw_params,
802 .hw_free = snd_pcm_lib_free_pages,
803 .pointer = rsnd_pointer,
810 #define PREALLOC_BUFFER (32 * 1024)
811 #define PREALLOC_BUFFER_MAX (32 * 1024)
813 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
815 return snd_pcm_lib_preallocate_pages_for_all(
818 rtd->card->snd_card->dev,
819 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
822 static void rsnd_pcm_free(struct snd_pcm *pcm)
824 snd_pcm_lib_preallocate_free_for_all(pcm);
827 static struct snd_soc_platform_driver rsnd_soc_platform = {
828 .ops = &rsnd_pcm_ops,
829 .pcm_new = rsnd_pcm_new,
830 .pcm_free = rsnd_pcm_free,
833 static const struct snd_soc_component_driver rsnd_soc_component = {
840 static int rsnd_probe(struct platform_device *pdev)
842 struct rcar_snd_info *info;
843 struct rsnd_priv *priv;
844 struct device *dev = &pdev->dev;
845 struct rsnd_dai *rdai;
846 int (*probe_func[])(struct platform_device *pdev,
847 struct rsnd_priv *priv) = {
856 info = pdev->dev.platform_data;
858 dev_err(dev, "driver needs R-Car sound information\n");
865 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
867 dev_err(dev, "priv allocate failed\n");
873 spin_lock_init(&priv->lock);
878 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
879 ret = probe_func[i](pdev, priv);
884 for_each_rsnd_dai(rdai, priv, i) {
885 ret = rsnd_dai_call(rdai, &rdai->playback, probe);
889 ret = rsnd_dai_call(rdai, &rdai->capture, probe);
897 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
899 dev_err(dev, "cannot snd soc register\n");
903 ret = snd_soc_register_component(dev, &rsnd_soc_component,
904 priv->daidrv, rsnd_rdai_nr(priv));
906 dev_err(dev, "cannot snd dai register\n");
910 dev_set_drvdata(dev, priv);
912 pm_runtime_enable(dev);
914 dev_info(dev, "probed\n");
918 snd_soc_unregister_platform(dev);
923 static int rsnd_remove(struct platform_device *pdev)
925 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
926 struct rsnd_dai *rdai;
929 pm_runtime_disable(&pdev->dev);
931 for_each_rsnd_dai(rdai, priv, i) {
932 ret = rsnd_dai_call(rdai, &rdai->playback, remove);
936 ret = rsnd_dai_call(rdai, &rdai->capture, remove);
944 rsnd_ssi_remove(pdev, priv);
945 rsnd_adg_remove(pdev, priv);
946 rsnd_scu_remove(pdev, priv);
947 rsnd_dai_remove(pdev, priv);
948 rsnd_gen_remove(pdev, priv);
953 static struct platform_driver rsnd_driver = {
955 .name = "rcar_sound",
958 .remove = rsnd_remove,
960 module_platform_driver(rsnd_driver);
962 MODULE_LICENSE("GPL");
963 MODULE_DESCRIPTION("Renesas R-Car audio driver");
964 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
965 MODULE_ALIAS("platform:rcar-pcm-audio");