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,
129 INIT_LIST_HEAD(&mod->list);
135 static void rsnd_dma_continue(struct rsnd_dma *dma)
137 /* push next A or B plane */
138 dma->submit_loop = 1;
139 schedule_work(&dma->work);
142 void rsnd_dma_start(struct rsnd_dma *dma)
144 /* push both A and B plane*/
145 dma->submit_loop = 2;
146 schedule_work(&dma->work);
149 void rsnd_dma_stop(struct rsnd_dma *dma)
151 dma->submit_loop = 0;
152 cancel_work_sync(&dma->work);
153 dmaengine_terminate_all(dma->chan);
156 static void rsnd_dma_complete(void *data)
158 struct rsnd_dma *dma = (struct rsnd_dma *)data;
159 struct rsnd_priv *priv = dma->priv;
162 rsnd_lock(priv, flags);
166 if (dma->submit_loop)
167 rsnd_dma_continue(dma);
169 rsnd_unlock(priv, flags);
172 static void rsnd_dma_do_work(struct work_struct *work)
174 struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
175 struct rsnd_priv *priv = dma->priv;
176 struct device *dev = rsnd_priv_to_dev(priv);
177 struct dma_async_tx_descriptor *desc;
182 for (i = 0; i < dma->submit_loop; i++) {
184 if (dma->inquiry(dma, &buf, &len) < 0)
187 desc = dmaengine_prep_slave_single(
188 dma->chan, buf, len, dma->dir,
189 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
191 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
195 desc->callback = rsnd_dma_complete;
196 desc->callback_param = dma;
198 if (dmaengine_submit(desc) < 0) {
199 dev_err(dev, "dmaengine_submit() fail\n");
203 dma_async_issue_pending(dma->chan);
207 int rsnd_dma_available(struct rsnd_dma *dma)
212 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
214 int (*inquiry)(struct rsnd_dma *dma,
215 dma_addr_t *buf, int *len),
216 int (*complete)(struct rsnd_dma *dma))
218 struct device *dev = rsnd_priv_to_dev(priv);
219 struct dma_slave_config cfg;
224 dev_err(dev, "it already has dma channel\n");
229 dma_cap_set(DMA_SLAVE, mask);
231 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
233 is_play ? "tx" : "rx");
235 dev_err(dev, "can't get dma channel\n");
240 cfg.dst_addr = 0; /* use default addr when playback */
241 cfg.src_addr = 0; /* use default addr when capture */
242 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
244 ret = dmaengine_slave_config(dma->chan, &cfg);
246 goto rsnd_dma_init_err;
248 dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
250 dma->inquiry = inquiry;
251 dma->complete = complete;
252 INIT_WORK(&dma->work, rsnd_dma_do_work);
257 rsnd_dma_quit(priv, dma);
262 void rsnd_dma_quit(struct rsnd_priv *priv,
263 struct rsnd_dma *dma)
266 dma_release_channel(dma->chan);
274 #define rsnd_dai_call(rdai, io, fn) \
276 struct rsnd_mod *mod, *n; \
278 for_each_rsnd_mod(mod, n, io) { \
279 ret = rsnd_mod_call(mod, fn, rdai, io); \
286 int rsnd_dai_connect(struct rsnd_dai *rdai,
287 struct rsnd_mod *mod,
288 struct rsnd_dai_stream *io)
293 if (!list_empty(&mod->list)) {
294 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
295 struct device *dev = rsnd_priv_to_dev(priv);
297 dev_err(dev, "%s%d is not empty\n",
303 list_add_tail(&mod->list, &io->head);
308 int rsnd_dai_disconnect(struct rsnd_mod *mod)
310 list_del_init(&mod->list);
315 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
317 int id = rdai - priv->rdai;
319 if ((id < 0) || (id >= rsnd_dai_nr(priv)))
325 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
327 if ((id < 0) || (id >= rsnd_dai_nr(priv)))
330 return priv->rdai + id;
333 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
335 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
337 return rsnd_dai_get(priv, dai->id);
340 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
342 return &rdai->playback == io;
346 * rsnd_soc_dai functions
348 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
350 struct snd_pcm_substream *substream = io->substream;
351 struct snd_pcm_runtime *runtime = substream->runtime;
352 int pos = io->byte_pos + additional;
354 pos %= (runtime->periods * io->byte_per_period);
359 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
361 io->byte_pos += byte;
363 if (io->byte_pos >= io->next_period_byte) {
364 struct snd_pcm_substream *substream = io->substream;
365 struct snd_pcm_runtime *runtime = substream->runtime;
368 io->next_period_byte += io->byte_per_period;
370 if (io->period_pos >= runtime->periods) {
373 io->next_period_byte = io->byte_per_period;
376 snd_pcm_period_elapsed(substream);
380 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
381 struct snd_pcm_substream *substream)
383 struct snd_pcm_runtime *runtime = substream->runtime;
385 if (!list_empty(&io->head))
388 INIT_LIST_HEAD(&io->head);
389 io->substream = substream;
392 io->byte_per_period = runtime->period_size *
394 samples_to_bytes(runtime, 1);
395 io->next_period_byte = io->byte_per_period;
401 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
403 struct snd_soc_pcm_runtime *rtd = substream->private_data;
409 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
410 struct snd_pcm_substream *substream)
412 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
413 return &rdai->playback;
415 return &rdai->capture;
418 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
419 struct snd_soc_dai *dai)
421 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
422 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
423 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
424 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
425 rsnd_dai_id(priv, rdai),
426 rsnd_dai_is_play(rdai, io));
427 int ssi_id = rsnd_mod_id(mod);
431 rsnd_lock(priv, flags);
434 case SNDRV_PCM_TRIGGER_START:
435 ret = rsnd_dai_stream_init(io, substream);
437 goto dai_trigger_end;
439 ret = rsnd_platform_call(priv, dai, start, ssi_id);
441 goto dai_trigger_end;
443 ret = rsnd_gen_path_init(priv, rdai, io);
445 goto dai_trigger_end;
447 ret = rsnd_dai_call(rdai, io, init);
449 goto dai_trigger_end;
451 ret = rsnd_dai_call(rdai, io, start);
453 goto dai_trigger_end;
455 case SNDRV_PCM_TRIGGER_STOP:
456 ret = rsnd_dai_call(rdai, io, stop);
458 goto dai_trigger_end;
460 ret = rsnd_dai_call(rdai, io, quit);
462 goto dai_trigger_end;
464 ret = rsnd_gen_path_exit(priv, rdai, io);
466 goto dai_trigger_end;
468 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
470 goto dai_trigger_end;
477 rsnd_unlock(priv, flags);
482 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
484 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
486 /* set master/slave audio interface */
487 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
488 case SND_SOC_DAIFMT_CBM_CFM:
489 rdai->clk_master = 1;
491 case SND_SOC_DAIFMT_CBS_CFS:
492 rdai->clk_master = 0;
498 /* set clock inversion */
499 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
500 case SND_SOC_DAIFMT_NB_IF:
501 rdai->bit_clk_inv = 0;
502 rdai->frm_clk_inv = 1;
504 case SND_SOC_DAIFMT_IB_NF:
505 rdai->bit_clk_inv = 1;
506 rdai->frm_clk_inv = 0;
508 case SND_SOC_DAIFMT_IB_IF:
509 rdai->bit_clk_inv = 1;
510 rdai->frm_clk_inv = 1;
512 case SND_SOC_DAIFMT_NB_NF:
514 rdai->bit_clk_inv = 0;
515 rdai->frm_clk_inv = 0;
520 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
521 case SND_SOC_DAIFMT_I2S:
523 rdai->data_alignment = 0;
525 case SND_SOC_DAIFMT_LEFT_J:
527 rdai->data_alignment = 0;
529 case SND_SOC_DAIFMT_RIGHT_J:
531 rdai->data_alignment = 1;
538 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
539 .trigger = rsnd_soc_dai_trigger,
540 .set_fmt = rsnd_soc_dai_set_fmt,
543 static int rsnd_dai_probe(struct platform_device *pdev,
544 struct rcar_snd_info *info,
545 struct rsnd_priv *priv)
547 struct snd_soc_dai_driver *drv;
548 struct rsnd_dai *rdai;
549 struct rsnd_mod *pmod, *cmod;
550 struct device *dev = rsnd_priv_to_dev(priv);
555 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
556 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
557 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
564 dev_err(dev, "no dai\n");
568 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
569 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
571 dev_err(dev, "dai allocate failed\n");
575 for (i = 0; i < dai_nr; i++) {
577 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
578 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
583 INIT_LIST_HEAD(&rdai[i].playback.head);
584 INIT_LIST_HEAD(&rdai[i].capture.head);
586 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
589 * init snd_soc_dai_driver
591 drv[i].name = rdai[i].name;
592 drv[i].ops = &rsnd_soc_dai_ops;
594 drv[i].playback.rates = RSND_RATES;
595 drv[i].playback.formats = RSND_FMTS;
596 drv[i].playback.channels_min = 2;
597 drv[i].playback.channels_max = 2;
600 drv[i].capture.rates = RSND_RATES;
601 drv[i].capture.formats = RSND_FMTS;
602 drv[i].capture.channels_min = 2;
603 drv[i].capture.channels_max = 2;
606 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
607 pmod ? "play" : " -- ",
608 cmod ? "capture" : " -- ");
611 priv->dai_nr = dai_nr;
618 static void rsnd_dai_remove(struct platform_device *pdev,
619 struct rsnd_priv *priv)
626 static struct snd_pcm_hardware rsnd_pcm_hardware = {
627 .info = SNDRV_PCM_INFO_INTERLEAVED |
628 SNDRV_PCM_INFO_MMAP |
629 SNDRV_PCM_INFO_MMAP_VALID |
630 SNDRV_PCM_INFO_PAUSE,
631 .buffer_bytes_max = 64 * 1024,
632 .period_bytes_min = 32,
633 .period_bytes_max = 8192,
639 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
641 struct snd_pcm_runtime *runtime = substream->runtime;
644 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
646 ret = snd_pcm_hw_constraint_integer(runtime,
647 SNDRV_PCM_HW_PARAM_PERIODS);
652 static int rsnd_hw_params(struct snd_pcm_substream *substream,
653 struct snd_pcm_hw_params *hw_params)
655 return snd_pcm_lib_malloc_pages(substream,
656 params_buffer_bytes(hw_params));
659 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
661 struct snd_pcm_runtime *runtime = substream->runtime;
662 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
663 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
664 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
666 return bytes_to_frames(runtime, io->byte_pos);
669 static struct snd_pcm_ops rsnd_pcm_ops = {
670 .open = rsnd_pcm_open,
671 .ioctl = snd_pcm_lib_ioctl,
672 .hw_params = rsnd_hw_params,
673 .hw_free = snd_pcm_lib_free_pages,
674 .pointer = rsnd_pointer,
681 #define PREALLOC_BUFFER (32 * 1024)
682 #define PREALLOC_BUFFER_MAX (32 * 1024)
684 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
686 return snd_pcm_lib_preallocate_pages_for_all(
689 rtd->card->snd_card->dev,
690 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
693 static void rsnd_pcm_free(struct snd_pcm *pcm)
695 snd_pcm_lib_preallocate_free_for_all(pcm);
698 static struct snd_soc_platform_driver rsnd_soc_platform = {
699 .ops = &rsnd_pcm_ops,
700 .pcm_new = rsnd_pcm_new,
701 .pcm_free = rsnd_pcm_free,
704 static const struct snd_soc_component_driver rsnd_soc_component = {
711 static int rsnd_probe(struct platform_device *pdev)
713 struct rcar_snd_info *info;
714 struct rsnd_priv *priv;
715 struct device *dev = &pdev->dev;
718 info = pdev->dev.platform_data;
720 dev_err(dev, "driver needs R-Car sound information\n");
727 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
729 dev_err(dev, "priv allocate failed\n");
735 spin_lock_init(&priv->lock);
740 ret = rsnd_gen_probe(pdev, info, priv);
744 ret = rsnd_scu_probe(pdev, info, priv);
748 ret = rsnd_adg_probe(pdev, info, priv);
752 ret = rsnd_ssi_probe(pdev, info, priv);
756 ret = rsnd_dai_probe(pdev, info, priv);
763 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
765 dev_err(dev, "cannot snd soc register\n");
769 ret = snd_soc_register_component(dev, &rsnd_soc_component,
770 priv->daidrv, rsnd_dai_nr(priv));
772 dev_err(dev, "cannot snd dai register\n");
776 dev_set_drvdata(dev, priv);
778 pm_runtime_enable(dev);
780 dev_info(dev, "probed\n");
784 snd_soc_unregister_platform(dev);
789 static int rsnd_remove(struct platform_device *pdev)
791 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
793 pm_runtime_disable(&pdev->dev);
798 rsnd_ssi_remove(pdev, priv);
799 rsnd_adg_remove(pdev, priv);
800 rsnd_scu_remove(pdev, priv);
801 rsnd_dai_remove(pdev, priv);
802 rsnd_gen_remove(pdev, priv);
807 static struct platform_driver rsnd_driver = {
809 .name = "rcar_sound",
812 .remove = rsnd_remove,
814 module_platform_driver(rsnd_driver);
816 MODULE_LICENSE("GPL");
817 MODULE_DESCRIPTION("Renesas R-Car audio driver");
818 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
819 MODULE_ALIAS("platform:rcar-pcm-audio");