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_dai_call(rdai, io, fn) \
296 struct rsnd_mod *mod, *n; \
298 for_each_rsnd_mod(mod, n, io) { \
299 ret = rsnd_mod_call(mod, fn, rdai, io); \
306 int rsnd_dai_connect(struct rsnd_dai
*rdai
,
307 struct rsnd_mod
*mod
,
308 struct rsnd_dai_stream
*io
)
313 if (!list_empty(&mod
->list
)) {
314 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
315 struct device
*dev
= rsnd_priv_to_dev(priv
);
317 dev_err(dev
, "%s%d is not empty\n",
323 list_add_tail(&mod
->list
, &io
->head
);
329 int rsnd_dai_disconnect(struct rsnd_mod
*mod
)
331 list_del_init(&mod
->list
);
337 int rsnd_dai_id(struct rsnd_priv
*priv
, struct rsnd_dai
*rdai
)
339 int id
= rdai
- priv
->rdai
;
341 if ((id
< 0) || (id
>= rsnd_dai_nr(priv
)))
347 struct rsnd_dai
*rsnd_dai_get(struct rsnd_priv
*priv
, int id
)
349 if ((id
< 0) || (id
>= rsnd_dai_nr(priv
)))
352 return priv
->rdai
+ id
;
355 static struct rsnd_dai
*rsnd_dai_to_rdai(struct snd_soc_dai
*dai
)
357 struct rsnd_priv
*priv
= snd_soc_dai_get_drvdata(dai
);
359 return rsnd_dai_get(priv
, dai
->id
);
362 int rsnd_dai_is_play(struct rsnd_dai
*rdai
, struct rsnd_dai_stream
*io
)
364 return &rdai
->playback
== io
;
368 * rsnd_soc_dai functions
370 int rsnd_dai_pointer_offset(struct rsnd_dai_stream
*io
, int additional
)
372 struct snd_pcm_substream
*substream
= io
->substream
;
373 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
374 int pos
= io
->byte_pos
+ additional
;
376 pos
%= (runtime
->periods
* io
->byte_per_period
);
381 void rsnd_dai_pointer_update(struct rsnd_dai_stream
*io
, int byte
)
383 io
->byte_pos
+= byte
;
385 if (io
->byte_pos
>= io
->next_period_byte
) {
386 struct snd_pcm_substream
*substream
= io
->substream
;
387 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
390 io
->next_period_byte
+= io
->byte_per_period
;
392 if (io
->period_pos
>= runtime
->periods
) {
395 io
->next_period_byte
= io
->byte_per_period
;
398 snd_pcm_period_elapsed(substream
);
402 static int rsnd_dai_stream_init(struct rsnd_dai_stream
*io
,
403 struct snd_pcm_substream
*substream
)
405 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
407 if (!list_empty(&io
->head
))
410 INIT_LIST_HEAD(&io
->head
);
411 io
->substream
= substream
;
414 io
->byte_per_period
= runtime
->period_size
*
416 samples_to_bytes(runtime
, 1);
417 io
->next_period_byte
= io
->byte_per_period
;
423 struct snd_soc_dai
*rsnd_substream_to_dai(struct snd_pcm_substream
*substream
)
425 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
431 struct rsnd_dai_stream
*rsnd_rdai_to_io(struct rsnd_dai
*rdai
,
432 struct snd_pcm_substream
*substream
)
434 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
435 return &rdai
->playback
;
437 return &rdai
->capture
;
440 static int rsnd_soc_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
441 struct snd_soc_dai
*dai
)
443 struct rsnd_priv
*priv
= snd_soc_dai_get_drvdata(dai
);
444 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
445 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
446 struct rsnd_mod
*mod
= rsnd_ssi_mod_get_frm_dai(priv
,
447 rsnd_dai_id(priv
, rdai
),
448 rsnd_dai_is_play(rdai
, io
));
449 int ssi_id
= rsnd_mod_id(mod
);
453 rsnd_lock(priv
, flags
);
456 case SNDRV_PCM_TRIGGER_START
:
457 ret
= rsnd_dai_stream_init(io
, substream
);
459 goto dai_trigger_end
;
461 ret
= rsnd_platform_call(priv
, dai
, start
, ssi_id
);
463 goto dai_trigger_end
;
465 ret
= rsnd_gen_path_init(priv
, rdai
, io
);
467 goto dai_trigger_end
;
469 ret
= rsnd_dai_call(rdai
, io
, init
);
471 goto dai_trigger_end
;
473 ret
= rsnd_dai_call(rdai
, io
, start
);
475 goto dai_trigger_end
;
477 case SNDRV_PCM_TRIGGER_STOP
:
478 ret
= rsnd_dai_call(rdai
, io
, stop
);
480 goto dai_trigger_end
;
482 ret
= rsnd_dai_call(rdai
, io
, quit
);
484 goto dai_trigger_end
;
486 ret
= rsnd_gen_path_exit(priv
, rdai
, io
);
488 goto dai_trigger_end
;
490 ret
= rsnd_platform_call(priv
, dai
, stop
, ssi_id
);
492 goto dai_trigger_end
;
499 rsnd_unlock(priv
, flags
);
504 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
506 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
508 /* set master/slave audio interface */
509 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
510 case SND_SOC_DAIFMT_CBM_CFM
:
511 rdai
->clk_master
= 1;
513 case SND_SOC_DAIFMT_CBS_CFS
:
514 rdai
->clk_master
= 0;
520 /* set clock inversion */
521 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
522 case SND_SOC_DAIFMT_NB_IF
:
523 rdai
->bit_clk_inv
= 0;
524 rdai
->frm_clk_inv
= 1;
526 case SND_SOC_DAIFMT_IB_NF
:
527 rdai
->bit_clk_inv
= 1;
528 rdai
->frm_clk_inv
= 0;
530 case SND_SOC_DAIFMT_IB_IF
:
531 rdai
->bit_clk_inv
= 1;
532 rdai
->frm_clk_inv
= 1;
534 case SND_SOC_DAIFMT_NB_NF
:
536 rdai
->bit_clk_inv
= 0;
537 rdai
->frm_clk_inv
= 0;
542 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
543 case SND_SOC_DAIFMT_I2S
:
545 rdai
->data_alignment
= 0;
547 case SND_SOC_DAIFMT_LEFT_J
:
549 rdai
->data_alignment
= 0;
551 case SND_SOC_DAIFMT_RIGHT_J
:
553 rdai
->data_alignment
= 1;
560 static const struct snd_soc_dai_ops rsnd_soc_dai_ops
= {
561 .trigger
= rsnd_soc_dai_trigger
,
562 .set_fmt
= rsnd_soc_dai_set_fmt
,
565 static int rsnd_dai_probe(struct platform_device
*pdev
,
566 struct rcar_snd_info
*info
,
567 struct rsnd_priv
*priv
)
569 struct snd_soc_dai_driver
*drv
;
570 struct rsnd_dai
*rdai
;
571 struct rsnd_mod
*pmod
, *cmod
;
572 struct device
*dev
= rsnd_priv_to_dev(priv
);
577 for (dai_nr
= 0; dai_nr
< 32; dai_nr
++) {
578 pmod
= rsnd_ssi_mod_get_frm_dai(priv
, dai_nr
, 1);
579 cmod
= rsnd_ssi_mod_get_frm_dai(priv
, dai_nr
, 0);
586 dev_err(dev
, "no dai\n");
590 drv
= devm_kzalloc(dev
, sizeof(*drv
) * dai_nr
, GFP_KERNEL
);
591 rdai
= devm_kzalloc(dev
, sizeof(*rdai
) * dai_nr
, GFP_KERNEL
);
593 dev_err(dev
, "dai allocate failed\n");
597 priv
->dai_nr
= dai_nr
;
601 for (i
= 0; i
< dai_nr
; i
++) {
603 pmod
= rsnd_ssi_mod_get_frm_dai(priv
, i
, 1);
604 cmod
= rsnd_ssi_mod_get_frm_dai(priv
, i
, 0);
609 INIT_LIST_HEAD(&rdai
[i
].playback
.head
);
610 INIT_LIST_HEAD(&rdai
[i
].capture
.head
);
612 snprintf(rdai
[i
].name
, RSND_DAI_NAME_SIZE
, "rsnd-dai.%d", i
);
615 * init snd_soc_dai_driver
617 drv
[i
].name
= rdai
[i
].name
;
618 drv
[i
].ops
= &rsnd_soc_dai_ops
;
620 drv
[i
].playback
.rates
= RSND_RATES
;
621 drv
[i
].playback
.formats
= RSND_FMTS
;
622 drv
[i
].playback
.channels_min
= 2;
623 drv
[i
].playback
.channels_max
= 2;
626 drv
[i
].capture
.rates
= RSND_RATES
;
627 drv
[i
].capture
.formats
= RSND_FMTS
;
628 drv
[i
].capture
.channels_min
= 2;
629 drv
[i
].capture
.channels_max
= 2;
632 dev_dbg(dev
, "%s (%s/%s)\n", rdai
[i
].name
,
633 pmod
? "play" : " -- ",
634 cmod
? "capture" : " -- ");
640 static void rsnd_dai_remove(struct platform_device
*pdev
,
641 struct rsnd_priv
*priv
)
648 static struct snd_pcm_hardware rsnd_pcm_hardware
= {
649 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
650 SNDRV_PCM_INFO_MMAP
|
651 SNDRV_PCM_INFO_MMAP_VALID
|
652 SNDRV_PCM_INFO_PAUSE
,
653 .buffer_bytes_max
= 64 * 1024,
654 .period_bytes_min
= 32,
655 .period_bytes_max
= 8192,
661 static int rsnd_pcm_open(struct snd_pcm_substream
*substream
)
663 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
666 snd_soc_set_runtime_hwparams(substream
, &rsnd_pcm_hardware
);
668 ret
= snd_pcm_hw_constraint_integer(runtime
,
669 SNDRV_PCM_HW_PARAM_PERIODS
);
674 static int rsnd_hw_params(struct snd_pcm_substream
*substream
,
675 struct snd_pcm_hw_params
*hw_params
)
677 return snd_pcm_lib_malloc_pages(substream
,
678 params_buffer_bytes(hw_params
));
681 static snd_pcm_uframes_t
rsnd_pointer(struct snd_pcm_substream
*substream
)
683 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
684 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
685 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
686 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
688 return bytes_to_frames(runtime
, io
->byte_pos
);
691 static struct snd_pcm_ops rsnd_pcm_ops
= {
692 .open
= rsnd_pcm_open
,
693 .ioctl
= snd_pcm_lib_ioctl
,
694 .hw_params
= rsnd_hw_params
,
695 .hw_free
= snd_pcm_lib_free_pages
,
696 .pointer
= rsnd_pointer
,
703 #define PREALLOC_BUFFER (32 * 1024)
704 #define PREALLOC_BUFFER_MAX (32 * 1024)
706 static int rsnd_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
708 return snd_pcm_lib_preallocate_pages_for_all(
711 rtd
->card
->snd_card
->dev
,
712 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
715 static void rsnd_pcm_free(struct snd_pcm
*pcm
)
717 snd_pcm_lib_preallocate_free_for_all(pcm
);
720 static struct snd_soc_platform_driver rsnd_soc_platform
= {
721 .ops
= &rsnd_pcm_ops
,
722 .pcm_new
= rsnd_pcm_new
,
723 .pcm_free
= rsnd_pcm_free
,
726 static const struct snd_soc_component_driver rsnd_soc_component
= {
733 static int rsnd_probe(struct platform_device
*pdev
)
735 struct rcar_snd_info
*info
;
736 struct rsnd_priv
*priv
;
737 struct device
*dev
= &pdev
->dev
;
740 info
= pdev
->dev
.platform_data
;
742 dev_err(dev
, "driver needs R-Car sound information\n");
749 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
751 dev_err(dev
, "priv allocate failed\n");
757 spin_lock_init(&priv
->lock
);
762 ret
= rsnd_gen_probe(pdev
, info
, priv
);
766 ret
= rsnd_ssi_probe(pdev
, info
, priv
);
770 ret
= rsnd_scu_probe(pdev
, info
, priv
);
774 ret
= rsnd_adg_probe(pdev
, info
, priv
);
778 ret
= rsnd_dai_probe(pdev
, info
, priv
);
785 ret
= snd_soc_register_platform(dev
, &rsnd_soc_platform
);
787 dev_err(dev
, "cannot snd soc register\n");
791 ret
= snd_soc_register_component(dev
, &rsnd_soc_component
,
792 priv
->daidrv
, rsnd_dai_nr(priv
));
794 dev_err(dev
, "cannot snd dai register\n");
798 dev_set_drvdata(dev
, priv
);
800 pm_runtime_enable(dev
);
802 dev_info(dev
, "probed\n");
806 snd_soc_unregister_platform(dev
);
811 static int rsnd_remove(struct platform_device
*pdev
)
813 struct rsnd_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
815 pm_runtime_disable(&pdev
->dev
);
820 rsnd_ssi_remove(pdev
, priv
);
821 rsnd_adg_remove(pdev
, priv
);
822 rsnd_scu_remove(pdev
, priv
);
823 rsnd_dai_remove(pdev
, priv
);
824 rsnd_gen_remove(pdev
, priv
);
829 static struct platform_driver rsnd_driver
= {
831 .name
= "rcar_sound",
834 .remove
= rsnd_remove
,
836 module_platform_driver(rsnd_driver
);
838 MODULE_LICENSE("GPL");
839 MODULE_DESCRIPTION("Renesas R-Car audio driver");
840 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
841 MODULE_ALIAS("platform:rcar-pcm-audio");