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");
204 dma_async_issue_pending(dma->chan);
207 int rsnd_dma_available(struct rsnd_dma *dma)
212 static bool rsnd_dma_filter(struct dma_chan *chan, void *param)
214 chan->private = param;
219 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
221 int (*inquiry)(struct rsnd_dma *dma,
222 dma_addr_t *buf, int *len),
223 int (*complete)(struct rsnd_dma *dma))
225 struct device *dev = rsnd_priv_to_dev(priv);
229 dev_err(dev, "it already has dma channel\n");
234 dma_cap_set(DMA_SLAVE, mask);
236 dma->slave.shdma_slave.slave_id = id;
238 dma->chan = dma_request_channel(mask, rsnd_dma_filter,
239 &dma->slave.shdma_slave);
241 dev_err(dev, "can't get dma channel\n");
245 dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
247 dma->inquiry = inquiry;
248 dma->complete = complete;
249 INIT_WORK(&dma->work, rsnd_dma_do_work);
254 void rsnd_dma_quit(struct rsnd_priv *priv,
255 struct rsnd_dma *dma)
258 dma_release_channel(dma->chan);
266 #define rsnd_dai_call(rdai, io, fn) \
268 struct rsnd_mod *mod, *n; \
270 for_each_rsnd_mod(mod, n, io) { \
271 ret = rsnd_mod_call(mod, fn, rdai, io); \
278 int rsnd_dai_connect(struct rsnd_dai *rdai,
279 struct rsnd_mod *mod,
280 struct rsnd_dai_stream *io)
285 if (!list_empty(&mod->list)) {
286 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
287 struct device *dev = rsnd_priv_to_dev(priv);
289 dev_err(dev, "%s%d is not empty\n",
295 list_add_tail(&mod->list, &io->head);
300 int rsnd_dai_disconnect(struct rsnd_mod *mod)
302 list_del_init(&mod->list);
307 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
309 int id = rdai - priv->rdai;
311 if ((id < 0) || (id >= rsnd_dai_nr(priv)))
317 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
319 if ((id < 0) || (id >= rsnd_dai_nr(priv)))
322 return priv->rdai + id;
325 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
327 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
329 return rsnd_dai_get(priv, dai->id);
332 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
334 return &rdai->playback == io;
338 * rsnd_soc_dai functions
340 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
342 struct snd_pcm_substream *substream = io->substream;
343 struct snd_pcm_runtime *runtime = substream->runtime;
344 int pos = io->byte_pos + additional;
346 pos %= (runtime->periods * io->byte_per_period);
351 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
353 io->byte_pos += byte;
355 if (io->byte_pos >= io->next_period_byte) {
356 struct snd_pcm_substream *substream = io->substream;
357 struct snd_pcm_runtime *runtime = substream->runtime;
360 io->next_period_byte += io->byte_per_period;
362 if (io->period_pos >= runtime->periods) {
365 io->next_period_byte = io->byte_per_period;
368 snd_pcm_period_elapsed(substream);
372 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
373 struct snd_pcm_substream *substream)
375 struct snd_pcm_runtime *runtime = substream->runtime;
377 if (!list_empty(&io->head))
380 INIT_LIST_HEAD(&io->head);
381 io->substream = substream;
384 io->byte_per_period = runtime->period_size *
386 samples_to_bytes(runtime, 1);
387 io->next_period_byte = io->byte_per_period;
393 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
395 struct snd_soc_pcm_runtime *rtd = substream->private_data;
401 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
402 struct snd_pcm_substream *substream)
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
405 return &rdai->playback;
407 return &rdai->capture;
410 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
411 struct snd_soc_dai *dai)
413 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
414 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
415 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
416 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
417 rsnd_dai_id(priv, rdai),
418 rsnd_dai_is_play(rdai, io));
419 int ssi_id = rsnd_mod_id(mod);
423 rsnd_lock(priv, flags);
426 case SNDRV_PCM_TRIGGER_START:
427 ret = rsnd_dai_stream_init(io, substream);
429 goto dai_trigger_end;
431 ret = rsnd_platform_call(priv, dai, start, ssi_id);
433 goto dai_trigger_end;
435 ret = rsnd_gen_path_init(priv, rdai, io);
437 goto dai_trigger_end;
439 ret = rsnd_dai_call(rdai, io, init);
441 goto dai_trigger_end;
443 ret = rsnd_dai_call(rdai, io, start);
445 goto dai_trigger_end;
447 case SNDRV_PCM_TRIGGER_STOP:
448 ret = rsnd_dai_call(rdai, io, stop);
450 goto dai_trigger_end;
452 ret = rsnd_dai_call(rdai, io, quit);
454 goto dai_trigger_end;
456 ret = rsnd_gen_path_exit(priv, rdai, io);
458 goto dai_trigger_end;
460 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
462 goto dai_trigger_end;
469 rsnd_unlock(priv, flags);
474 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
476 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
478 /* set master/slave audio interface */
479 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
480 case SND_SOC_DAIFMT_CBM_CFM:
481 rdai->clk_master = 1;
483 case SND_SOC_DAIFMT_CBS_CFS:
484 rdai->clk_master = 0;
490 /* set clock inversion */
491 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
492 case SND_SOC_DAIFMT_NB_IF:
493 rdai->bit_clk_inv = 0;
494 rdai->frm_clk_inv = 1;
496 case SND_SOC_DAIFMT_IB_NF:
497 rdai->bit_clk_inv = 1;
498 rdai->frm_clk_inv = 0;
500 case SND_SOC_DAIFMT_IB_IF:
501 rdai->bit_clk_inv = 1;
502 rdai->frm_clk_inv = 1;
504 case SND_SOC_DAIFMT_NB_NF:
506 rdai->bit_clk_inv = 0;
507 rdai->frm_clk_inv = 0;
512 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
513 case SND_SOC_DAIFMT_I2S:
515 rdai->data_alignment = 0;
517 case SND_SOC_DAIFMT_LEFT_J:
519 rdai->data_alignment = 0;
521 case SND_SOC_DAIFMT_RIGHT_J:
523 rdai->data_alignment = 1;
530 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
531 .trigger = rsnd_soc_dai_trigger,
532 .set_fmt = rsnd_soc_dai_set_fmt,
535 static int rsnd_dai_probe(struct platform_device *pdev,
536 struct rcar_snd_info *info,
537 struct rsnd_priv *priv)
539 struct snd_soc_dai_driver *drv;
540 struct rsnd_dai *rdai;
541 struct rsnd_mod *pmod, *cmod;
542 struct device *dev = rsnd_priv_to_dev(priv);
547 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
548 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
549 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
556 dev_err(dev, "no dai\n");
560 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
561 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
563 dev_err(dev, "dai allocate failed\n");
567 for (i = 0; i < dai_nr; i++) {
569 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
570 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
575 INIT_LIST_HEAD(&rdai[i].playback.head);
576 INIT_LIST_HEAD(&rdai[i].capture.head);
578 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
581 * init snd_soc_dai_driver
583 drv[i].name = rdai[i].name;
584 drv[i].ops = &rsnd_soc_dai_ops;
586 drv[i].playback.rates = RSND_RATES;
587 drv[i].playback.formats = RSND_FMTS;
588 drv[i].playback.channels_min = 2;
589 drv[i].playback.channels_max = 2;
592 drv[i].capture.rates = RSND_RATES;
593 drv[i].capture.formats = RSND_FMTS;
594 drv[i].capture.channels_min = 2;
595 drv[i].capture.channels_max = 2;
598 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
599 pmod ? "play" : " -- ",
600 cmod ? "capture" : " -- ");
603 priv->dai_nr = dai_nr;
610 static void rsnd_dai_remove(struct platform_device *pdev,
611 struct rsnd_priv *priv)
618 static struct snd_pcm_hardware rsnd_pcm_hardware = {
619 .info = SNDRV_PCM_INFO_INTERLEAVED |
620 SNDRV_PCM_INFO_MMAP |
621 SNDRV_PCM_INFO_MMAP_VALID |
622 SNDRV_PCM_INFO_PAUSE,
623 .formats = RSND_FMTS,
629 .buffer_bytes_max = 64 * 1024,
630 .period_bytes_min = 32,
631 .period_bytes_max = 8192,
637 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
639 struct snd_pcm_runtime *runtime = substream->runtime;
642 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
644 ret = snd_pcm_hw_constraint_integer(runtime,
645 SNDRV_PCM_HW_PARAM_PERIODS);
650 static int rsnd_hw_params(struct snd_pcm_substream *substream,
651 struct snd_pcm_hw_params *hw_params)
653 return snd_pcm_lib_malloc_pages(substream,
654 params_buffer_bytes(hw_params));
657 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
659 struct snd_pcm_runtime *runtime = substream->runtime;
660 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
661 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
662 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
664 return bytes_to_frames(runtime, io->byte_pos);
667 static struct snd_pcm_ops rsnd_pcm_ops = {
668 .open = rsnd_pcm_open,
669 .ioctl = snd_pcm_lib_ioctl,
670 .hw_params = rsnd_hw_params,
671 .hw_free = snd_pcm_lib_free_pages,
672 .pointer = rsnd_pointer,
679 #define PREALLOC_BUFFER (32 * 1024)
680 #define PREALLOC_BUFFER_MAX (32 * 1024)
682 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
684 return snd_pcm_lib_preallocate_pages_for_all(
687 rtd->card->snd_card->dev,
688 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
691 static void rsnd_pcm_free(struct snd_pcm *pcm)
693 snd_pcm_lib_preallocate_free_for_all(pcm);
696 static struct snd_soc_platform_driver rsnd_soc_platform = {
697 .ops = &rsnd_pcm_ops,
698 .pcm_new = rsnd_pcm_new,
699 .pcm_free = rsnd_pcm_free,
702 static const struct snd_soc_component_driver rsnd_soc_component = {
709 static int rsnd_probe(struct platform_device *pdev)
711 struct rcar_snd_info *info;
712 struct rsnd_priv *priv;
713 struct device *dev = &pdev->dev;
716 info = pdev->dev.platform_data;
718 dev_err(dev, "driver needs R-Car sound information\n");
725 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
727 dev_err(dev, "priv allocate failed\n");
733 spin_lock_init(&priv->lock);
738 ret = rsnd_gen_probe(pdev, info, priv);
742 ret = rsnd_scu_probe(pdev, info, priv);
746 ret = rsnd_adg_probe(pdev, info, priv);
750 ret = rsnd_ssi_probe(pdev, info, priv);
754 ret = rsnd_dai_probe(pdev, info, priv);
761 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
763 dev_err(dev, "cannot snd soc register\n");
767 ret = snd_soc_register_component(dev, &rsnd_soc_component,
768 priv->daidrv, rsnd_dai_nr(priv));
770 dev_err(dev, "cannot snd dai register\n");
774 dev_set_drvdata(dev, priv);
776 pm_runtime_enable(dev);
778 dev_info(dev, "probed\n");
782 snd_soc_unregister_platform(dev);
787 static int rsnd_remove(struct platform_device *pdev)
789 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
791 pm_runtime_disable(&pdev->dev);
796 rsnd_ssi_remove(pdev, priv);
797 rsnd_adg_remove(pdev, priv);
798 rsnd_scu_remove(pdev, priv);
799 rsnd_dai_remove(pdev, priv);
800 rsnd_gen_remove(pdev, priv);
805 static struct platform_driver rsnd_driver = {
807 .name = "rcar_sound",
810 .remove = rsnd_remove,
812 module_platform_driver(rsnd_driver);
814 MODULE_LICENSE("GPL");
815 MODULE_DESCRIPTION("Renesas R-Car audio driver");
816 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
817 MODULE_ALIAS("platform:rcar-pcm-audio");