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>
99 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
103 * rsnd_platform functions
105 #define rsnd_platform_call(priv, dai, func, param...) \
106 (!(priv->info->func) ? 0 : \
107 priv->info->func(param))
112 char *rsnd_mod_name(struct rsnd_mod *mod)
114 if (!mod || !mod->ops)
117 return mod->ops->name;
120 void rsnd_mod_init(struct rsnd_priv *priv,
121 struct rsnd_mod *mod,
122 struct rsnd_mod_ops *ops,
128 INIT_LIST_HEAD(&mod->list);
134 static void rsnd_dma_continue(struct rsnd_dma *dma)
136 /* push next A or B plane */
137 dma->submit_loop = 1;
138 schedule_work(&dma->work);
141 void rsnd_dma_start(struct rsnd_dma *dma)
143 /* push both A and B plane*/
144 dma->submit_loop = 2;
145 schedule_work(&dma->work);
148 void rsnd_dma_stop(struct rsnd_dma *dma)
150 dma->submit_loop = 0;
151 cancel_work_sync(&dma->work);
152 dmaengine_terminate_all(dma->chan);
155 static void rsnd_dma_complete(void *data)
157 struct rsnd_dma *dma = (struct rsnd_dma *)data;
158 struct rsnd_priv *priv = dma->priv;
161 rsnd_lock(priv, flags);
165 if (dma->submit_loop)
166 rsnd_dma_continue(dma);
168 rsnd_unlock(priv, flags);
171 static void rsnd_dma_do_work(struct work_struct *work)
173 struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
174 struct rsnd_priv *priv = dma->priv;
175 struct device *dev = rsnd_priv_to_dev(priv);
176 struct dma_async_tx_descriptor *desc;
181 for (i = 0; i < dma->submit_loop; i++) {
183 if (dma->inquiry(dma, &buf, &len) < 0)
186 desc = dmaengine_prep_slave_single(
187 dma->chan, buf, len, dma->dir,
188 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
190 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
194 desc->callback = rsnd_dma_complete;
195 desc->callback_param = dma;
197 if (dmaengine_submit(desc) < 0) {
198 dev_err(dev, "dmaengine_submit() fail\n");
202 dma_async_issue_pending(dma->chan);
206 int rsnd_dma_available(struct rsnd_dma *dma)
211 static bool rsnd_dma_filter(struct dma_chan *chan, void *param)
213 chan->private = param;
218 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
220 int (*inquiry)(struct rsnd_dma *dma,
221 dma_addr_t *buf, int *len),
222 int (*complete)(struct rsnd_dma *dma))
224 struct device *dev = rsnd_priv_to_dev(priv);
228 dev_err(dev, "it already has dma channel\n");
233 dma_cap_set(DMA_SLAVE, mask);
235 dma->slave.shdma_slave.slave_id = id;
237 dma->chan = dma_request_channel(mask, rsnd_dma_filter,
238 &dma->slave.shdma_slave);
240 dev_err(dev, "can't get dma channel\n");
244 dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
246 dma->inquiry = inquiry;
247 dma->complete = complete;
248 INIT_WORK(&dma->work, rsnd_dma_do_work);
253 void rsnd_dma_quit(struct rsnd_priv *priv,
254 struct rsnd_dma *dma)
257 dma_release_channel(dma->chan);
265 #define rsnd_dai_call(rdai, io, fn) \
267 struct rsnd_mod *mod, *n; \
269 for_each_rsnd_mod(mod, n, io) { \
270 ret = rsnd_mod_call(mod, fn, rdai, io); \
277 int rsnd_dai_connect(struct rsnd_dai *rdai,
278 struct rsnd_mod *mod,
279 struct rsnd_dai_stream *io)
284 if (!list_empty(&mod->list)) {
285 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
286 struct device *dev = rsnd_priv_to_dev(priv);
288 dev_err(dev, "%s%d is not empty\n",
294 list_add_tail(&mod->list, &io->head);
299 int rsnd_dai_disconnect(struct rsnd_mod *mod)
301 list_del_init(&mod->list);
306 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
308 int id = rdai - priv->rdai;
310 if ((id < 0) || (id >= rsnd_dai_nr(priv)))
316 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
318 if ((id < 0) || (id >= rsnd_dai_nr(priv)))
321 return priv->rdai + id;
324 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
326 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
328 return rsnd_dai_get(priv, dai->id);
331 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
333 return &rdai->playback == io;
337 * rsnd_soc_dai functions
339 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
341 struct snd_pcm_substream *substream = io->substream;
342 struct snd_pcm_runtime *runtime = substream->runtime;
343 int pos = io->byte_pos + additional;
345 pos %= (runtime->periods * io->byte_per_period);
350 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
352 io->byte_pos += byte;
354 if (io->byte_pos >= io->next_period_byte) {
355 struct snd_pcm_substream *substream = io->substream;
356 struct snd_pcm_runtime *runtime = substream->runtime;
359 io->next_period_byte += io->byte_per_period;
361 if (io->period_pos >= runtime->periods) {
364 io->next_period_byte = io->byte_per_period;
367 snd_pcm_period_elapsed(substream);
371 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
372 struct snd_pcm_substream *substream)
374 struct snd_pcm_runtime *runtime = substream->runtime;
376 if (!list_empty(&io->head))
379 INIT_LIST_HEAD(&io->head);
380 io->substream = substream;
383 io->byte_per_period = runtime->period_size *
385 samples_to_bytes(runtime, 1);
386 io->next_period_byte = io->byte_per_period;
392 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
394 struct snd_soc_pcm_runtime *rtd = substream->private_data;
400 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
401 struct snd_pcm_substream *substream)
403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
404 return &rdai->playback;
406 return &rdai->capture;
409 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
410 struct snd_soc_dai *dai)
412 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
413 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
414 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
415 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
416 rsnd_dai_id(priv, rdai),
417 rsnd_dai_is_play(rdai, io));
418 int ssi_id = rsnd_mod_id(mod);
422 rsnd_lock(priv, flags);
425 case SNDRV_PCM_TRIGGER_START:
426 ret = rsnd_dai_stream_init(io, substream);
428 goto dai_trigger_end;
430 ret = rsnd_platform_call(priv, dai, start, ssi_id);
432 goto dai_trigger_end;
434 ret = rsnd_gen_path_init(priv, rdai, io);
436 goto dai_trigger_end;
438 ret = rsnd_dai_call(rdai, io, init);
440 goto dai_trigger_end;
442 ret = rsnd_dai_call(rdai, io, start);
444 goto dai_trigger_end;
446 case SNDRV_PCM_TRIGGER_STOP:
447 ret = rsnd_dai_call(rdai, io, stop);
449 goto dai_trigger_end;
451 ret = rsnd_dai_call(rdai, io, quit);
453 goto dai_trigger_end;
455 ret = rsnd_gen_path_exit(priv, rdai, io);
457 goto dai_trigger_end;
459 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
461 goto dai_trigger_end;
468 rsnd_unlock(priv, flags);
473 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
475 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
477 /* set master/slave audio interface */
478 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
479 case SND_SOC_DAIFMT_CBM_CFM:
480 rdai->clk_master = 1;
482 case SND_SOC_DAIFMT_CBS_CFS:
483 rdai->clk_master = 0;
489 /* set clock inversion */
490 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
491 case SND_SOC_DAIFMT_NB_IF:
492 rdai->bit_clk_inv = 0;
493 rdai->frm_clk_inv = 1;
495 case SND_SOC_DAIFMT_IB_NF:
496 rdai->bit_clk_inv = 1;
497 rdai->frm_clk_inv = 0;
499 case SND_SOC_DAIFMT_IB_IF:
500 rdai->bit_clk_inv = 1;
501 rdai->frm_clk_inv = 1;
503 case SND_SOC_DAIFMT_NB_NF:
505 rdai->bit_clk_inv = 0;
506 rdai->frm_clk_inv = 0;
511 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
512 case SND_SOC_DAIFMT_I2S:
514 rdai->data_alignment = 0;
516 case SND_SOC_DAIFMT_LEFT_J:
518 rdai->data_alignment = 0;
520 case SND_SOC_DAIFMT_RIGHT_J:
522 rdai->data_alignment = 1;
529 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
530 .trigger = rsnd_soc_dai_trigger,
531 .set_fmt = rsnd_soc_dai_set_fmt,
534 static int rsnd_dai_probe(struct platform_device *pdev,
535 struct rcar_snd_info *info,
536 struct rsnd_priv *priv)
538 struct snd_soc_dai_driver *drv;
539 struct rsnd_dai *rdai;
540 struct rsnd_mod *pmod, *cmod;
541 struct device *dev = rsnd_priv_to_dev(priv);
546 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
547 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
548 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
555 dev_err(dev, "no dai\n");
559 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
560 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
562 dev_err(dev, "dai allocate failed\n");
566 for (i = 0; i < dai_nr; i++) {
568 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
569 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
574 INIT_LIST_HEAD(&rdai[i].playback.head);
575 INIT_LIST_HEAD(&rdai[i].capture.head);
577 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
580 * init snd_soc_dai_driver
582 drv[i].name = rdai[i].name;
583 drv[i].ops = &rsnd_soc_dai_ops;
585 drv[i].playback.rates = RSND_RATES;
586 drv[i].playback.formats = RSND_FMTS;
587 drv[i].playback.channels_min = 2;
588 drv[i].playback.channels_max = 2;
591 drv[i].capture.rates = RSND_RATES;
592 drv[i].capture.formats = RSND_FMTS;
593 drv[i].capture.channels_min = 2;
594 drv[i].capture.channels_max = 2;
597 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
598 pmod ? "play" : " -- ",
599 cmod ? "capture" : " -- ");
602 priv->dai_nr = dai_nr;
609 static void rsnd_dai_remove(struct platform_device *pdev,
610 struct rsnd_priv *priv)
617 static struct snd_pcm_hardware rsnd_pcm_hardware = {
618 .info = SNDRV_PCM_INFO_INTERLEAVED |
619 SNDRV_PCM_INFO_MMAP |
620 SNDRV_PCM_INFO_MMAP_VALID |
621 SNDRV_PCM_INFO_PAUSE,
622 .formats = RSND_FMTS,
628 .buffer_bytes_max = 64 * 1024,
629 .period_bytes_min = 32,
630 .period_bytes_max = 8192,
636 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
638 struct snd_pcm_runtime *runtime = substream->runtime;
641 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
643 ret = snd_pcm_hw_constraint_integer(runtime,
644 SNDRV_PCM_HW_PARAM_PERIODS);
649 static int rsnd_hw_params(struct snd_pcm_substream *substream,
650 struct snd_pcm_hw_params *hw_params)
652 return snd_pcm_lib_malloc_pages(substream,
653 params_buffer_bytes(hw_params));
656 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
658 struct snd_pcm_runtime *runtime = substream->runtime;
659 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
660 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
661 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
663 return bytes_to_frames(runtime, io->byte_pos);
666 static struct snd_pcm_ops rsnd_pcm_ops = {
667 .open = rsnd_pcm_open,
668 .ioctl = snd_pcm_lib_ioctl,
669 .hw_params = rsnd_hw_params,
670 .hw_free = snd_pcm_lib_free_pages,
671 .pointer = rsnd_pointer,
678 #define PREALLOC_BUFFER (32 * 1024)
679 #define PREALLOC_BUFFER_MAX (32 * 1024)
681 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
683 return snd_pcm_lib_preallocate_pages_for_all(
686 rtd->card->snd_card->dev,
687 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
690 static void rsnd_pcm_free(struct snd_pcm *pcm)
692 snd_pcm_lib_preallocate_free_for_all(pcm);
695 static struct snd_soc_platform_driver rsnd_soc_platform = {
696 .ops = &rsnd_pcm_ops,
697 .pcm_new = rsnd_pcm_new,
698 .pcm_free = rsnd_pcm_free,
701 static const struct snd_soc_component_driver rsnd_soc_component = {
708 static int rsnd_probe(struct platform_device *pdev)
710 struct rcar_snd_info *info;
711 struct rsnd_priv *priv;
712 struct device *dev = &pdev->dev;
715 info = pdev->dev.platform_data;
717 dev_err(dev, "driver needs R-Car sound information\n");
724 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
726 dev_err(dev, "priv allocate failed\n");
732 spin_lock_init(&priv->lock);
737 ret = rsnd_gen_probe(pdev, info, priv);
741 ret = rsnd_scu_probe(pdev, info, priv);
745 ret = rsnd_adg_probe(pdev, info, priv);
749 ret = rsnd_ssi_probe(pdev, info, priv);
753 ret = rsnd_dai_probe(pdev, info, priv);
760 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
762 dev_err(dev, "cannot snd soc register\n");
766 ret = snd_soc_register_component(dev, &rsnd_soc_component,
767 priv->daidrv, rsnd_dai_nr(priv));
769 dev_err(dev, "cannot snd dai register\n");
773 dev_set_drvdata(dev, priv);
775 pm_runtime_enable(dev);
777 dev_info(dev, "probed\n");
781 snd_soc_unregister_platform(dev);
786 static int rsnd_remove(struct platform_device *pdev)
788 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
790 pm_runtime_disable(&pdev->dev);
795 rsnd_ssi_remove(pdev, priv);
796 rsnd_adg_remove(pdev, priv);
797 rsnd_scu_remove(pdev, priv);
798 rsnd_dai_remove(pdev, priv);
799 rsnd_gen_remove(pdev, priv);
804 static struct platform_driver rsnd_driver = {
806 .name = "rcar_sound",
809 .remove = rsnd_remove,
811 module_platform_driver(rsnd_driver);
813 MODULE_LICENSE("GPL");
814 MODULE_DESCRIPTION("Renesas R-Car audio driver");
815 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
816 MODULE_ALIAS("platform:rcar-pcm-audio");