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 src
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 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
353 int id = rdai - priv->rdai;
355 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
361 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
363 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
366 return priv->rdai + id;
369 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
371 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
373 return rsnd_dai_get(priv, dai->id);
376 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
378 return &rdai->playback == io;
382 * rsnd_soc_dai functions
384 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
386 struct snd_pcm_substream *substream = io->substream;
387 struct snd_pcm_runtime *runtime = substream->runtime;
388 int pos = io->byte_pos + additional;
390 pos %= (runtime->periods * io->byte_per_period);
395 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
397 io->byte_pos += byte;
399 if (io->byte_pos >= io->next_period_byte) {
400 struct snd_pcm_substream *substream = io->substream;
401 struct snd_pcm_runtime *runtime = substream->runtime;
404 io->next_period_byte += io->byte_per_period;
406 if (io->period_pos >= runtime->periods) {
409 io->next_period_byte = io->byte_per_period;
412 snd_pcm_period_elapsed(substream);
416 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
417 struct snd_pcm_substream *substream)
419 struct snd_pcm_runtime *runtime = substream->runtime;
421 io->substream = substream;
424 io->byte_per_period = runtime->period_size *
426 samples_to_bytes(runtime, 1);
427 io->next_period_byte = io->byte_per_period;
433 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
435 struct snd_soc_pcm_runtime *rtd = substream->private_data;
441 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
442 struct snd_pcm_substream *substream)
444 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
445 return &rdai->playback;
447 return &rdai->capture;
450 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
451 struct snd_soc_dai *dai)
453 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
454 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
455 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
456 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
457 rsnd_dai_id(priv, rdai),
458 rsnd_dai_is_play(rdai, io));
459 int ssi_id = rsnd_mod_id(mod);
463 rsnd_lock(priv, flags);
466 case SNDRV_PCM_TRIGGER_START:
467 ret = rsnd_dai_stream_init(io, substream);
469 goto dai_trigger_end;
471 ret = rsnd_platform_call(priv, dai, start, ssi_id);
473 goto dai_trigger_end;
475 ret = rsnd_dai_call(rdai, io, init);
477 goto dai_trigger_end;
479 ret = rsnd_dai_call(rdai, io, start);
481 goto dai_trigger_end;
483 case SNDRV_PCM_TRIGGER_STOP:
484 ret = rsnd_dai_call(rdai, io, stop);
486 goto dai_trigger_end;
488 ret = rsnd_dai_call(rdai, io, quit);
490 goto dai_trigger_end;
492 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
494 goto dai_trigger_end;
501 rsnd_unlock(priv, flags);
506 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
508 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
510 /* set master/slave audio interface */
511 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
512 case SND_SOC_DAIFMT_CBM_CFM:
513 rdai->clk_master = 1;
515 case SND_SOC_DAIFMT_CBS_CFS:
516 rdai->clk_master = 0;
522 /* set clock inversion */
523 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
524 case SND_SOC_DAIFMT_NB_IF:
525 rdai->bit_clk_inv = 0;
526 rdai->frm_clk_inv = 1;
528 case SND_SOC_DAIFMT_IB_NF:
529 rdai->bit_clk_inv = 1;
530 rdai->frm_clk_inv = 0;
532 case SND_SOC_DAIFMT_IB_IF:
533 rdai->bit_clk_inv = 1;
534 rdai->frm_clk_inv = 1;
536 case SND_SOC_DAIFMT_NB_NF:
538 rdai->bit_clk_inv = 0;
539 rdai->frm_clk_inv = 0;
544 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
545 case SND_SOC_DAIFMT_I2S:
547 rdai->data_alignment = 0;
549 case SND_SOC_DAIFMT_LEFT_J:
551 rdai->data_alignment = 0;
553 case SND_SOC_DAIFMT_RIGHT_J:
555 rdai->data_alignment = 1;
562 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
563 .trigger = rsnd_soc_dai_trigger,
564 .set_fmt = rsnd_soc_dai_set_fmt,
567 static int rsnd_path_init(struct rsnd_priv *priv,
568 struct rsnd_dai *rdai,
569 struct rsnd_dai_stream *io)
571 struct rsnd_mod *mod;
572 struct rsnd_dai_platform_info *dai_info = rdai->info;
578 * Gen1 is created by SRU/SSI, and this SRU is base module of
579 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
582 * Gen1 SRU = Gen2 SCU + SSIU + etc
584 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
588 if (rsnd_is_enable_path(io, ssi))
589 ssi_id = rsnd_info_id(priv, io, ssi);
590 if (rsnd_is_enable_path(io, src))
591 src_id = rsnd_info_id(priv, io, src);
594 mod = rsnd_ssi_mod_get_frm_dai(priv,
595 rsnd_dai_id(priv, rdai),
596 rsnd_dai_is_play(rdai, io));
599 ssi_id = src_id = rsnd_mod_id(mod);
606 mod = rsnd_src_mod_get(priv, src_id);
607 ret = rsnd_dai_connect(mod, io);
614 mod = rsnd_ssi_mod_get(priv, ssi_id);
615 ret = rsnd_dai_connect(mod, io);
623 static int rsnd_dai_probe(struct platform_device *pdev,
624 struct rsnd_priv *priv)
626 struct snd_soc_dai_driver *drv;
627 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
628 struct rsnd_dai *rdai;
629 struct rsnd_mod *pmod, *cmod;
630 struct device *dev = rsnd_priv_to_dev(priv);
631 int dai_nr = info->dai_info_nr;
635 * dai_nr should be set via dai_info_nr,
636 * but allow it to keeping compatible
640 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
641 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
642 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
650 dev_err(dev, "no dai\n");
654 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
655 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
657 dev_err(dev, "dai allocate failed\n");
661 priv->rdai_nr = dai_nr;
665 for (i = 0; i < dai_nr; i++) {
667 rdai[i].info = &info->dai_info[i];
669 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
670 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
675 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
678 * init snd_soc_dai_driver
680 drv[i].name = rdai[i].name;
681 drv[i].ops = &rsnd_soc_dai_ops;
683 drv[i].playback.rates = RSND_RATES;
684 drv[i].playback.formats = RSND_FMTS;
685 drv[i].playback.channels_min = 2;
686 drv[i].playback.channels_max = 2;
689 rdai[i].playback.info = &info->dai_info[i].playback;
690 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
693 drv[i].capture.rates = RSND_RATES;
694 drv[i].capture.formats = RSND_FMTS;
695 drv[i].capture.channels_min = 2;
696 drv[i].capture.channels_max = 2;
699 rdai[i].capture.info = &info->dai_info[i].capture;
700 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
703 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
704 pmod ? "play" : " -- ",
705 cmod ? "capture" : " -- ");
714 static struct snd_pcm_hardware rsnd_pcm_hardware = {
715 .info = SNDRV_PCM_INFO_INTERLEAVED |
716 SNDRV_PCM_INFO_MMAP |
717 SNDRV_PCM_INFO_MMAP_VALID |
718 SNDRV_PCM_INFO_PAUSE,
719 .buffer_bytes_max = 64 * 1024,
720 .period_bytes_min = 32,
721 .period_bytes_max = 8192,
727 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
729 struct snd_pcm_runtime *runtime = substream->runtime;
732 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
734 ret = snd_pcm_hw_constraint_integer(runtime,
735 SNDRV_PCM_HW_PARAM_PERIODS);
740 static int rsnd_hw_params(struct snd_pcm_substream *substream,
741 struct snd_pcm_hw_params *hw_params)
743 return snd_pcm_lib_malloc_pages(substream,
744 params_buffer_bytes(hw_params));
747 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
749 struct snd_pcm_runtime *runtime = substream->runtime;
750 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
751 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
752 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
754 return bytes_to_frames(runtime, io->byte_pos);
757 static struct snd_pcm_ops rsnd_pcm_ops = {
758 .open = rsnd_pcm_open,
759 .ioctl = snd_pcm_lib_ioctl,
760 .hw_params = rsnd_hw_params,
761 .hw_free = snd_pcm_lib_free_pages,
762 .pointer = rsnd_pointer,
769 #define PREALLOC_BUFFER (32 * 1024)
770 #define PREALLOC_BUFFER_MAX (32 * 1024)
772 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
774 return snd_pcm_lib_preallocate_pages_for_all(
777 rtd->card->snd_card->dev,
778 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
781 static void rsnd_pcm_free(struct snd_pcm *pcm)
783 snd_pcm_lib_preallocate_free_for_all(pcm);
786 static struct snd_soc_platform_driver rsnd_soc_platform = {
787 .ops = &rsnd_pcm_ops,
788 .pcm_new = rsnd_pcm_new,
789 .pcm_free = rsnd_pcm_free,
792 static const struct snd_soc_component_driver rsnd_soc_component = {
799 static int rsnd_probe(struct platform_device *pdev)
801 struct rcar_snd_info *info;
802 struct rsnd_priv *priv;
803 struct device *dev = &pdev->dev;
804 struct rsnd_dai *rdai;
805 int (*probe_func[])(struct platform_device *pdev,
806 struct rsnd_priv *priv) = {
815 info = pdev->dev.platform_data;
817 dev_err(dev, "driver needs R-Car sound information\n");
824 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
826 dev_err(dev, "priv allocate failed\n");
832 spin_lock_init(&priv->lock);
837 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
838 ret = probe_func[i](pdev, priv);
843 for_each_rsnd_dai(rdai, priv, i) {
844 ret = rsnd_dai_call(rdai, &rdai->playback, probe);
848 ret = rsnd_dai_call(rdai, &rdai->capture, probe);
856 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
858 dev_err(dev, "cannot snd soc register\n");
862 ret = snd_soc_register_component(dev, &rsnd_soc_component,
863 priv->daidrv, rsnd_rdai_nr(priv));
865 dev_err(dev, "cannot snd dai register\n");
869 dev_set_drvdata(dev, priv);
871 pm_runtime_enable(dev);
873 dev_info(dev, "probed\n");
877 snd_soc_unregister_platform(dev);
882 static int rsnd_remove(struct platform_device *pdev)
884 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
885 struct rsnd_dai *rdai;
888 pm_runtime_disable(&pdev->dev);
890 for_each_rsnd_dai(rdai, priv, i) {
891 ret = rsnd_dai_call(rdai, &rdai->playback, remove);
895 ret = rsnd_dai_call(rdai, &rdai->capture, remove);
903 static struct platform_driver rsnd_driver = {
905 .name = "rcar_sound",
908 .remove = rsnd_remove,
910 module_platform_driver(rsnd_driver);
912 MODULE_LICENSE("GPL");
913 MODULE_DESCRIPTION("Renesas R-Car audio driver");
914 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
915 MODULE_ALIAS("platform:rcar-pcm-audio");