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_start(struct rsnd_dma
*dma
);
136 static void rsnd_dma_continue(struct rsnd_dma
*dma
)
138 /* push next A or B plane */
139 dma
->submit_loop
= 1;
140 schedule_work(&dma
->work
);
143 void rsnd_dma_start(struct rsnd_dma
*dma
)
145 /* push both A and B plane*/
147 dma
->submit_loop
= 2;
148 __rsnd_dma_start(dma
);
151 void rsnd_dma_stop(struct rsnd_dma
*dma
)
153 dma
->submit_loop
= 0;
154 cancel_work_sync(&dma
->work
);
155 dmaengine_terminate_all(dma
->chan
);
158 static void rsnd_dma_complete(void *data
)
160 struct rsnd_dma
*dma
= (struct rsnd_dma
*)data
;
161 struct rsnd_mod
*mod
= rsnd_dma_to_mod(dma
);
162 struct rsnd_priv
*priv
= rsnd_mod_to_priv(rsnd_dma_to_mod(dma
));
163 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
166 rsnd_lock(priv
, flags
);
169 * Renesas sound Gen1 needs 1 DMAC,
171 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
172 * But, Audio-DMAC-peri-peri doesn't have interrupt,
173 * and this driver is assuming that here.
175 * If Audio-DMAC-peri-peri has interrpt,
176 * rsnd_dai_pointer_update() will be called twice,
177 * ant it will breaks io->byte_pos
180 rsnd_dai_pointer_update(io
, io
->byte_per_period
);
182 if (dma
->submit_loop
)
183 rsnd_dma_continue(dma
);
185 rsnd_unlock(priv
, flags
);
188 static void __rsnd_dma_start(struct rsnd_dma
*dma
)
190 struct rsnd_mod
*mod
= rsnd_dma_to_mod(dma
);
191 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
192 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
193 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
194 struct device
*dev
= rsnd_priv_to_dev(priv
);
195 struct dma_async_tx_descriptor
*desc
;
197 size_t len
= io
->byte_per_period
;
200 for (i
= 0; i
< dma
->submit_loop
; i
++) {
202 buf
= runtime
->dma_addr
+
203 rsnd_dai_pointer_offset(io
, dma
->offset
+ len
);
206 desc
= dmaengine_prep_slave_single(
207 dma
->chan
, buf
, len
, dma
->dir
,
208 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
210 dev_err(dev
, "dmaengine_prep_slave_sg() fail\n");
214 desc
->callback
= rsnd_dma_complete
;
215 desc
->callback_param
= dma
;
217 if (dmaengine_submit(desc
) < 0) {
218 dev_err(dev
, "dmaengine_submit() fail\n");
222 dma_async_issue_pending(dma
->chan
);
226 static void rsnd_dma_do_work(struct work_struct
*work
)
228 struct rsnd_dma
*dma
= container_of(work
, struct rsnd_dma
, work
);
230 __rsnd_dma_start(dma
);
233 int rsnd_dma_available(struct rsnd_dma
*dma
)
238 int rsnd_dma_init(struct rsnd_priv
*priv
, struct rsnd_dma
*dma
,
241 struct device
*dev
= rsnd_priv_to_dev(priv
);
242 struct dma_slave_config cfg
;
247 dev_err(dev
, "it already has dma channel\n");
252 dma_cap_set(DMA_SLAVE
, mask
);
254 dma
->chan
= dma_request_slave_channel_compat(mask
, shdma_chan_filter
,
256 is_play
? "tx" : "rx");
258 dev_err(dev
, "can't get dma channel\n");
263 cfg
.dst_addr
= 0; /* use default addr when playback */
264 cfg
.src_addr
= 0; /* use default addr when capture */
265 cfg
.direction
= is_play
? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
;
267 ret
= dmaengine_slave_config(dma
->chan
, &cfg
);
269 goto rsnd_dma_init_err
;
271 dma
->dir
= is_play
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
272 INIT_WORK(&dma
->work
, rsnd_dma_do_work
);
277 rsnd_dma_quit(priv
, dma
);
282 void rsnd_dma_quit(struct rsnd_priv
*priv
,
283 struct rsnd_dma
*dma
)
286 dma_release_channel(dma
->chan
);
294 #define __rsnd_mod_call(mod, func, rdai, io) \
296 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
297 struct device *dev = rsnd_priv_to_dev(priv); \
298 dev_dbg(dev, "%s [%d] %s\n", \
299 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
300 (mod)->ops->func(mod, rdai, io); \
303 #define rsnd_mod_call(mod, func, rdai, io) \
304 (!(mod) ? -ENODEV : \
305 !((mod)->ops->func) ? 0 : \
306 __rsnd_mod_call(mod, func, (rdai), (io)))
308 #define rsnd_dai_call(rdai, io, fn) \
310 struct rsnd_mod *mod, *n; \
312 for_each_rsnd_mod(mod, n, (io)) { \
313 ret = rsnd_mod_call(mod, fn, (rdai), (io)); \
320 int rsnd_dai_connect(struct rsnd_dai
*rdai
,
321 struct rsnd_mod
*mod
,
322 struct rsnd_dai_stream
*io
)
327 if (!list_empty(&mod
->list
)) {
328 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
329 struct device
*dev
= rsnd_priv_to_dev(priv
);
331 dev_err(dev
, "%s%d is not empty\n",
337 list_add_tail(&mod
->list
, &io
->head
);
343 int rsnd_dai_disconnect(struct rsnd_mod
*mod
)
345 list_del_init(&mod
->list
);
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 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 rsnd_priv
*priv
)
582 struct snd_soc_dai_driver
*drv
;
583 struct rsnd_dai
*rdai
;
584 struct rsnd_mod
*pmod
, *cmod
;
585 struct device
*dev
= rsnd_priv_to_dev(priv
);
590 for (dai_nr
= 0; dai_nr
< 32; dai_nr
++) {
591 pmod
= rsnd_ssi_mod_get_frm_dai(priv
, dai_nr
, 1);
592 cmod
= rsnd_ssi_mod_get_frm_dai(priv
, dai_nr
, 0);
599 dev_err(dev
, "no dai\n");
603 drv
= devm_kzalloc(dev
, sizeof(*drv
) * dai_nr
, GFP_KERNEL
);
604 rdai
= devm_kzalloc(dev
, sizeof(*rdai
) * dai_nr
, GFP_KERNEL
);
606 dev_err(dev
, "dai allocate failed\n");
610 priv
->rdai_nr
= dai_nr
;
614 for (i
= 0; i
< dai_nr
; i
++) {
616 pmod
= rsnd_ssi_mod_get_frm_dai(priv
, i
, 1);
617 cmod
= rsnd_ssi_mod_get_frm_dai(priv
, i
, 0);
622 INIT_LIST_HEAD(&rdai
[i
].playback
.head
);
623 INIT_LIST_HEAD(&rdai
[i
].capture
.head
);
625 snprintf(rdai
[i
].name
, RSND_DAI_NAME_SIZE
, "rsnd-dai.%d", i
);
628 * init snd_soc_dai_driver
630 drv
[i
].name
= rdai
[i
].name
;
631 drv
[i
].ops
= &rsnd_soc_dai_ops
;
633 drv
[i
].playback
.rates
= RSND_RATES
;
634 drv
[i
].playback
.formats
= RSND_FMTS
;
635 drv
[i
].playback
.channels_min
= 2;
636 drv
[i
].playback
.channels_max
= 2;
639 drv
[i
].capture
.rates
= RSND_RATES
;
640 drv
[i
].capture
.formats
= RSND_FMTS
;
641 drv
[i
].capture
.channels_min
= 2;
642 drv
[i
].capture
.channels_max
= 2;
645 dev_dbg(dev
, "%s (%s/%s)\n", rdai
[i
].name
,
646 pmod
? "play" : " -- ",
647 cmod
? "capture" : " -- ");
653 static void rsnd_dai_remove(struct platform_device
*pdev
,
654 struct rsnd_priv
*priv
)
661 static struct snd_pcm_hardware rsnd_pcm_hardware
= {
662 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
663 SNDRV_PCM_INFO_MMAP
|
664 SNDRV_PCM_INFO_MMAP_VALID
|
665 SNDRV_PCM_INFO_PAUSE
,
666 .buffer_bytes_max
= 64 * 1024,
667 .period_bytes_min
= 32,
668 .period_bytes_max
= 8192,
674 static int rsnd_pcm_open(struct snd_pcm_substream
*substream
)
676 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
679 snd_soc_set_runtime_hwparams(substream
, &rsnd_pcm_hardware
);
681 ret
= snd_pcm_hw_constraint_integer(runtime
,
682 SNDRV_PCM_HW_PARAM_PERIODS
);
687 static int rsnd_hw_params(struct snd_pcm_substream
*substream
,
688 struct snd_pcm_hw_params
*hw_params
)
690 return snd_pcm_lib_malloc_pages(substream
,
691 params_buffer_bytes(hw_params
));
694 static snd_pcm_uframes_t
rsnd_pointer(struct snd_pcm_substream
*substream
)
696 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
697 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
698 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
699 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
701 return bytes_to_frames(runtime
, io
->byte_pos
);
704 static struct snd_pcm_ops rsnd_pcm_ops
= {
705 .open
= rsnd_pcm_open
,
706 .ioctl
= snd_pcm_lib_ioctl
,
707 .hw_params
= rsnd_hw_params
,
708 .hw_free
= snd_pcm_lib_free_pages
,
709 .pointer
= rsnd_pointer
,
716 #define PREALLOC_BUFFER (32 * 1024)
717 #define PREALLOC_BUFFER_MAX (32 * 1024)
719 static int rsnd_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
721 return snd_pcm_lib_preallocate_pages_for_all(
724 rtd
->card
->snd_card
->dev
,
725 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
728 static void rsnd_pcm_free(struct snd_pcm
*pcm
)
730 snd_pcm_lib_preallocate_free_for_all(pcm
);
733 static struct snd_soc_platform_driver rsnd_soc_platform
= {
734 .ops
= &rsnd_pcm_ops
,
735 .pcm_new
= rsnd_pcm_new
,
736 .pcm_free
= rsnd_pcm_free
,
739 static const struct snd_soc_component_driver rsnd_soc_component
= {
746 static int rsnd_probe(struct platform_device
*pdev
)
748 struct rcar_snd_info
*info
;
749 struct rsnd_priv
*priv
;
750 struct device
*dev
= &pdev
->dev
;
753 info
= pdev
->dev
.platform_data
;
755 dev_err(dev
, "driver needs R-Car sound information\n");
762 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
764 dev_err(dev
, "priv allocate failed\n");
770 spin_lock_init(&priv
->lock
);
775 ret
= rsnd_gen_probe(pdev
, priv
);
779 ret
= rsnd_ssi_probe(pdev
, priv
);
783 ret
= rsnd_scu_probe(pdev
, priv
);
787 ret
= rsnd_adg_probe(pdev
, priv
);
791 ret
= rsnd_dai_probe(pdev
, priv
);
798 ret
= snd_soc_register_platform(dev
, &rsnd_soc_platform
);
800 dev_err(dev
, "cannot snd soc register\n");
804 ret
= snd_soc_register_component(dev
, &rsnd_soc_component
,
805 priv
->daidrv
, rsnd_rdai_nr(priv
));
807 dev_err(dev
, "cannot snd dai register\n");
811 dev_set_drvdata(dev
, priv
);
813 pm_runtime_enable(dev
);
815 dev_info(dev
, "probed\n");
819 snd_soc_unregister_platform(dev
);
824 static int rsnd_remove(struct platform_device
*pdev
)
826 struct rsnd_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
828 pm_runtime_disable(&pdev
->dev
);
833 rsnd_ssi_remove(pdev
, priv
);
834 rsnd_adg_remove(pdev
, priv
);
835 rsnd_scu_remove(pdev
, priv
);
836 rsnd_dai_remove(pdev
, priv
);
837 rsnd_gen_remove(pdev
, priv
);
842 static struct platform_driver rsnd_driver
= {
844 .name
= "rcar_sound",
847 .remove
= rsnd_remove
,
849 module_platform_driver(rsnd_driver
);
851 MODULE_LICENSE("GPL");
852 MODULE_DESCRIPTION("Renesas R-Car audio driver");
853 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
854 MODULE_ALIAS("platform:rcar-pcm-audio");