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) ? -ENODEV : \
107 priv->info->func(param))
113 u32 rsnd_read(struct rsnd_priv *priv,
114 struct rsnd_mod *mod, enum rsnd_reg reg)
116 void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
120 return ioread32(base);
123 void rsnd_write(struct rsnd_priv *priv,
124 struct rsnd_mod *mod,
125 enum rsnd_reg reg, u32 data)
127 void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
128 struct device *dev = rsnd_priv_to_dev(priv);
132 dev_dbg(dev, "w %p : %08x\n", base, data);
134 iowrite32(data, base);
137 void rsnd_bset(struct rsnd_priv *priv, struct rsnd_mod *mod,
138 enum rsnd_reg reg, u32 mask, u32 data)
140 void __iomem *base = rsnd_gen_reg_get(priv, mod, reg);
141 struct device *dev = rsnd_priv_to_dev(priv);
146 val = ioread32(base);
149 iowrite32(val, base);
151 dev_dbg(dev, "s %p : %08x\n", base, val);
157 char *rsnd_mod_name(struct rsnd_mod *mod)
159 if (!mod || !mod->ops)
162 return mod->ops->name;
165 void rsnd_mod_init(struct rsnd_priv *priv,
166 struct rsnd_mod *mod,
167 struct rsnd_mod_ops *ops,
173 INIT_LIST_HEAD(&mod->list);
179 static void rsnd_dma_continue(struct rsnd_dma *dma)
181 /* push next A or B plane */
182 dma->submit_loop = 1;
183 schedule_work(&dma->work);
186 void rsnd_dma_start(struct rsnd_dma *dma)
188 /* push both A and B plane*/
189 dma->submit_loop = 2;
190 schedule_work(&dma->work);
193 void rsnd_dma_stop(struct rsnd_dma *dma)
195 dma->submit_loop = 0;
196 cancel_work_sync(&dma->work);
197 dmaengine_terminate_all(dma->chan);
200 static void rsnd_dma_complete(void *data)
202 struct rsnd_dma *dma = (struct rsnd_dma *)data;
203 struct rsnd_priv *priv = dma->priv;
206 rsnd_lock(priv, flags);
210 if (dma->submit_loop)
211 rsnd_dma_continue(dma);
213 rsnd_unlock(priv, flags);
216 static void rsnd_dma_do_work(struct work_struct *work)
218 struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
219 struct rsnd_priv *priv = dma->priv;
220 struct device *dev = rsnd_priv_to_dev(priv);
221 struct dma_async_tx_descriptor *desc;
226 for (i = 0; i < dma->submit_loop; i++) {
228 if (dma->inquiry(dma, &buf, &len) < 0)
231 desc = dmaengine_prep_slave_single(
232 dma->chan, buf, len, dma->dir,
233 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
235 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
239 desc->callback = rsnd_dma_complete;
240 desc->callback_param = dma;
242 if (dmaengine_submit(desc) < 0) {
243 dev_err(dev, "dmaengine_submit() fail\n");
249 dma_async_issue_pending(dma->chan);
252 int rsnd_dma_available(struct rsnd_dma *dma)
257 static bool rsnd_dma_filter(struct dma_chan *chan, void *param)
259 chan->private = param;
264 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
266 int (*inquiry)(struct rsnd_dma *dma,
267 dma_addr_t *buf, int *len),
268 int (*complete)(struct rsnd_dma *dma))
270 struct device *dev = rsnd_priv_to_dev(priv);
274 dev_err(dev, "it already has dma channel\n");
279 dma_cap_set(DMA_SLAVE, mask);
281 dma->slave.shdma_slave.slave_id = id;
283 dma->chan = dma_request_channel(mask, rsnd_dma_filter,
284 &dma->slave.shdma_slave);
286 dev_err(dev, "can't get dma channel\n");
290 dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
292 dma->inquiry = inquiry;
293 dma->complete = complete;
294 INIT_WORK(&dma->work, rsnd_dma_do_work);
299 void rsnd_dma_quit(struct rsnd_priv *priv,
300 struct rsnd_dma *dma)
303 dma_release_channel(dma->chan);
311 #define rsnd_dai_call(rdai, io, fn) \
313 struct rsnd_mod *mod, *n; \
315 for_each_rsnd_mod(mod, n, io) { \
316 ret = rsnd_mod_call(mod, fn, rdai, io); \
323 int rsnd_dai_connect(struct rsnd_dai *rdai,
324 struct rsnd_mod *mod,
325 struct rsnd_dai_stream *io)
327 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
328 struct device *dev = rsnd_priv_to_dev(priv);
331 dev_err(dev, "NULL mod\n");
335 if (!list_empty(&mod->list)) {
336 dev_err(dev, "%s%d is not empty\n",
342 list_add_tail(&mod->list, &io->head);
347 int rsnd_dai_disconnect(struct rsnd_mod *mod)
349 list_del_init(&mod->list);
354 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
356 int id = rdai - priv->rdai;
358 if ((id < 0) || (id >= rsnd_dai_nr(priv)))
364 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
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 if (!list_empty(&io->head))
424 INIT_LIST_HEAD(&io->head);
425 io->substream = substream;
428 io->byte_per_period = runtime->period_size *
430 samples_to_bytes(runtime, 1);
431 io->next_period_byte = io->byte_per_period;
437 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
439 struct snd_soc_pcm_runtime *rtd = substream->private_data;
445 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
446 struct snd_pcm_substream *substream)
448 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
449 return &rdai->playback;
451 return &rdai->capture;
454 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
455 struct snd_soc_dai *dai)
457 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
458 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
459 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
460 struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
461 rsnd_dai_id(priv, rdai),
462 rsnd_dai_is_play(rdai, io));
463 int ssi_id = rsnd_mod_id(mod);
467 rsnd_lock(priv, flags);
470 case SNDRV_PCM_TRIGGER_START:
471 ret = rsnd_dai_stream_init(io, substream);
473 goto dai_trigger_end;
475 ret = rsnd_platform_call(priv, dai, start, ssi_id);
477 goto dai_trigger_end;
479 ret = rsnd_gen_path_init(priv, rdai, io);
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_gen_path_exit(priv, rdai, io);
502 goto dai_trigger_end;
504 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
506 goto dai_trigger_end;
513 rsnd_unlock(priv, flags);
518 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
520 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
522 /* set master/slave audio interface */
523 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
524 case SND_SOC_DAIFMT_CBM_CFM:
525 rdai->clk_master = 1;
527 case SND_SOC_DAIFMT_CBS_CFS:
528 rdai->clk_master = 0;
534 /* set clock inversion */
535 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
536 case SND_SOC_DAIFMT_NB_IF:
537 rdai->bit_clk_inv = 0;
538 rdai->frm_clk_inv = 1;
540 case SND_SOC_DAIFMT_IB_NF:
541 rdai->bit_clk_inv = 1;
542 rdai->frm_clk_inv = 0;
544 case SND_SOC_DAIFMT_IB_IF:
545 rdai->bit_clk_inv = 1;
546 rdai->frm_clk_inv = 1;
548 case SND_SOC_DAIFMT_NB_NF:
550 rdai->bit_clk_inv = 0;
551 rdai->frm_clk_inv = 0;
556 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
557 case SND_SOC_DAIFMT_I2S:
559 rdai->data_alignment = 0;
561 case SND_SOC_DAIFMT_LEFT_J:
563 rdai->data_alignment = 0;
565 case SND_SOC_DAIFMT_RIGHT_J:
567 rdai->data_alignment = 1;
574 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
575 .trigger = rsnd_soc_dai_trigger,
576 .set_fmt = rsnd_soc_dai_set_fmt,
579 static int rsnd_dai_probe(struct platform_device *pdev,
580 struct rcar_snd_info *info,
581 struct rsnd_priv *priv)
583 struct snd_soc_dai_driver *drv;
584 struct rsnd_dai *rdai;
585 struct rsnd_mod *pmod, *cmod;
586 struct device *dev = rsnd_priv_to_dev(priv);
591 for (dai_nr = 0; dai_nr < 32; dai_nr++) {
592 pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
593 cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
600 dev_err(dev, "no dai\n");
604 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
605 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
607 dev_err(dev, "dai allocate failed\n");
611 for (i = 0; i < dai_nr; i++) {
613 pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
614 cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
619 INIT_LIST_HEAD(&rdai[i].playback.head);
620 INIT_LIST_HEAD(&rdai[i].capture.head);
622 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
625 * init snd_soc_dai_driver
627 drv[i].name = rdai[i].name;
628 drv[i].ops = &rsnd_soc_dai_ops;
630 drv[i].playback.rates = RSND_RATES;
631 drv[i].playback.formats = RSND_FMTS;
632 drv[i].playback.channels_min = 2;
633 drv[i].playback.channels_max = 2;
636 drv[i].capture.rates = RSND_RATES;
637 drv[i].capture.formats = RSND_FMTS;
638 drv[i].capture.channels_min = 2;
639 drv[i].capture.channels_max = 2;
642 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
643 pmod ? "play" : " -- ",
644 cmod ? "capture" : " -- ");
647 priv->dai_nr = dai_nr;
654 static void rsnd_dai_remove(struct platform_device *pdev,
655 struct rsnd_priv *priv)
662 static struct snd_pcm_hardware rsnd_pcm_hardware = {
663 .info = SNDRV_PCM_INFO_INTERLEAVED |
664 SNDRV_PCM_INFO_MMAP |
665 SNDRV_PCM_INFO_MMAP_VALID |
666 SNDRV_PCM_INFO_PAUSE,
667 .formats = RSND_FMTS,
673 .buffer_bytes_max = 64 * 1024,
674 .period_bytes_min = 32,
675 .period_bytes_max = 8192,
681 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
683 struct snd_pcm_runtime *runtime = substream->runtime;
686 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
688 ret = snd_pcm_hw_constraint_integer(runtime,
689 SNDRV_PCM_HW_PARAM_PERIODS);
694 static int rsnd_hw_params(struct snd_pcm_substream *substream,
695 struct snd_pcm_hw_params *hw_params)
697 return snd_pcm_lib_malloc_pages(substream,
698 params_buffer_bytes(hw_params));
701 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
703 struct snd_pcm_runtime *runtime = substream->runtime;
704 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
705 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
706 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
708 return bytes_to_frames(runtime, io->byte_pos);
711 static struct snd_pcm_ops rsnd_pcm_ops = {
712 .open = rsnd_pcm_open,
713 .ioctl = snd_pcm_lib_ioctl,
714 .hw_params = rsnd_hw_params,
715 .hw_free = snd_pcm_lib_free_pages,
716 .pointer = rsnd_pointer,
723 #define PREALLOC_BUFFER (32 * 1024)
724 #define PREALLOC_BUFFER_MAX (32 * 1024)
726 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
728 return snd_pcm_lib_preallocate_pages_for_all(
731 rtd->card->snd_card->dev,
732 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
735 static void rsnd_pcm_free(struct snd_pcm *pcm)
737 snd_pcm_lib_preallocate_free_for_all(pcm);
740 static struct snd_soc_platform_driver rsnd_soc_platform = {
741 .ops = &rsnd_pcm_ops,
742 .pcm_new = rsnd_pcm_new,
743 .pcm_free = rsnd_pcm_free,
746 static const struct snd_soc_component_driver rsnd_soc_component = {
753 static int rsnd_probe(struct platform_device *pdev)
755 struct rcar_snd_info *info;
756 struct rsnd_priv *priv;
757 struct device *dev = &pdev->dev;
760 info = pdev->dev.platform_data;
762 dev_err(dev, "driver needs R-Car sound information\n");
769 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
771 dev_err(dev, "priv allocate failed\n");
777 spin_lock_init(&priv->lock);
782 ret = rsnd_gen_probe(pdev, info, priv);
786 ret = rsnd_scu_probe(pdev, info, priv);
790 ret = rsnd_adg_probe(pdev, info, priv);
794 ret = rsnd_ssi_probe(pdev, info, priv);
798 ret = rsnd_dai_probe(pdev, info, priv);
805 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
807 dev_err(dev, "cannot snd soc register\n");
811 ret = snd_soc_register_component(dev, &rsnd_soc_component,
812 priv->daidrv, rsnd_dai_nr(priv));
814 dev_err(dev, "cannot snd dai register\n");
818 dev_set_drvdata(dev, priv);
820 pm_runtime_enable(dev);
822 dev_info(dev, "probed\n");
826 snd_soc_unregister_platform(dev);
831 static int rsnd_remove(struct platform_device *pdev)
833 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
835 pm_runtime_disable(&pdev->dev);
840 rsnd_ssi_remove(pdev, priv);
841 rsnd_adg_remove(pdev, priv);
842 rsnd_scu_remove(pdev, priv);
843 rsnd_dai_remove(pdev, priv);
844 rsnd_gen_remove(pdev, priv);
849 static struct platform_driver rsnd_driver = {
851 .name = "rcar_sound",
854 .remove = rsnd_remove,
856 module_platform_driver(rsnd_driver);
858 MODULE_LICENSE("GPL");
859 MODULE_DESCRIPTION("Renesas R-Car audio driver");
860 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
861 MODULE_ALIAS("platform:rcar-pcm-audio");