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 src
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)
102 static const struct of_device_id rsnd_of_match
[] = {
103 { .compatible
= "renesas,rcar_sound-gen1", .data
= (void *)RSND_GEN1
},
104 { .compatible
= "renesas,rcar_sound-gen2", .data
= (void *)RSND_GEN2
},
105 { .compatible
= "renesas,rcar_sound-gen3", .data
= (void *)RSND_GEN2
}, /* gen2 compatible */
108 MODULE_DEVICE_TABLE(of
, rsnd_of_match
);
113 void rsnd_mod_make_sure(struct rsnd_mod
*mod
, enum rsnd_mod_type type
)
115 if (mod
->type
!= type
) {
116 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
117 struct device
*dev
= rsnd_priv_to_dev(priv
);
119 dev_warn(dev
, "%s[%d] is not your expected module\n",
120 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
124 char *rsnd_mod_name(struct rsnd_mod
*mod
)
126 if (!mod
|| !mod
->ops
)
129 return mod
->ops
->name
;
132 struct dma_chan
*rsnd_mod_dma_req(struct rsnd_dai_stream
*io
,
133 struct rsnd_mod
*mod
)
135 if (!mod
|| !mod
->ops
|| !mod
->ops
->dma_req
)
138 return mod
->ops
->dma_req(io
, mod
);
141 u32
*rsnd_mod_get_status(struct rsnd_dai_stream
*io
,
142 struct rsnd_mod
*mod
,
143 enum rsnd_mod_type type
)
148 int rsnd_mod_init(struct rsnd_priv
*priv
,
149 struct rsnd_mod
*mod
,
150 struct rsnd_mod_ops
*ops
,
152 u32
* (*get_status
)(struct rsnd_dai_stream
*io
,
153 struct rsnd_mod
*mod
,
154 enum rsnd_mod_type type
),
155 enum rsnd_mod_type type
,
158 int ret
= clk_prepare(clk
);
168 mod
->get_status
= get_status
;
173 void rsnd_mod_quit(struct rsnd_mod
*mod
)
176 clk_unprepare(mod
->clk
);
180 void rsnd_mod_interrupt(struct rsnd_mod
*mod
,
181 void (*callback
)(struct rsnd_mod
*mod
,
182 struct rsnd_dai_stream
*io
))
184 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
185 struct rsnd_dai_stream
*io
;
186 struct rsnd_dai
*rdai
;
189 for_each_rsnd_dai(rdai
, priv
, i
) {
190 io
= &rdai
->playback
;
191 if (mod
== io
->mod
[mod
->type
])
195 if (mod
== io
->mod
[mod
->type
])
200 int rsnd_io_is_working(struct rsnd_dai_stream
*io
)
202 /* see rsnd_dai_stream_init/quit() */
203 return !!io
->substream
;
206 void rsnd_set_slot(struct rsnd_dai
*rdai
,
210 rdai
->slots_num
= num
;
213 int rsnd_get_slot(struct rsnd_dai_stream
*io
)
215 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
220 int rsnd_get_slot_num(struct rsnd_dai_stream
*io
)
222 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
224 return rdai
->slots_num
;
227 int rsnd_runtime_channel_original(struct rsnd_dai_stream
*io
)
229 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
231 return runtime
->channels
;
234 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream
*io
)
236 int chan
= rsnd_runtime_channel_original(io
);
237 struct rsnd_mod
*ctu_mod
= rsnd_io_to_mod_ctu(io
);
240 u32 converted_chan
= rsnd_ctu_converted_channel(ctu_mod
);
243 return converted_chan
;
249 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream
*io
)
251 int chan
= rsnd_io_is_play(io
) ?
252 rsnd_runtime_channel_after_ctu(io
) :
253 rsnd_runtime_channel_original(io
);
256 if (rsnd_runtime_is_ssi_multi(io
))
257 chan
/= rsnd_get_slot_num(io
);
259 /* TDM Extend Mode needs 8ch */
266 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream
*io
)
268 int slots
= rsnd_get_slot_num(io
);
269 int chan
= rsnd_io_is_play(io
) ?
270 rsnd_runtime_channel_after_ctu(io
) :
271 rsnd_runtime_channel_original(io
);
273 return (chan
>= 6) && (slots
> 1);
276 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream
*io
)
278 return rsnd_runtime_channel_for_ssi(io
) >= 6;
284 u32
rsnd_get_adinr_bit(struct rsnd_mod
*mod
, struct rsnd_dai_stream
*io
)
286 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
287 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
288 struct device
*dev
= rsnd_priv_to_dev(priv
);
290 switch (runtime
->sample_bits
) {
297 dev_warn(dev
, "not supported sample bits\n");
305 u32
rsnd_get_dalign(struct rsnd_mod
*mod
, struct rsnd_dai_stream
*io
)
307 struct rsnd_mod
*ssi
= rsnd_io_to_mod_ssi(io
);
308 struct rsnd_mod
*target
;
309 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
310 u32 val
= 0x76543210;
313 if (rsnd_io_is_play(io
)) {
314 struct rsnd_mod
*src
= rsnd_io_to_mod_src(io
);
316 target
= src
? src
: ssi
;
318 struct rsnd_mod
*cmd
= rsnd_io_to_mod_cmd(io
);
320 target
= cmd
? cmd
: ssi
;
323 mask
<<= runtime
->channels
* 4;
326 switch (runtime
->sample_bits
) {
328 val
|= 0x67452301 & ~mask
;
331 val
|= 0x76543210 & ~mask
;
336 * exchange channeles on SRC if possible,
337 * otherwise, R/L volume settings on DVC
338 * changes inverted channels
349 #define rsnd_mod_call(idx, io, func, param...) \
351 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
352 struct rsnd_mod *mod = (io)->mod[idx]; \
353 struct device *dev = rsnd_priv_to_dev(priv); \
354 u32 *status = mod->get_status(io, mod, idx); \
355 u32 mask = 0xF << __rsnd_mod_shift_##func; \
356 u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \
357 u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
359 int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
363 *status = (*status & ~mask) + \
364 (add << __rsnd_mod_shift_##func); \
365 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
366 rsnd_mod_name(mod), rsnd_mod_id(mod), \
367 *status, call ? #func : ""); \
369 ret = (mod)->ops->func(mod, io, param); \
371 dev_dbg(dev, "%s[%d] : rsnd_mod_call error %d\n", \
372 rsnd_mod_name(mod), rsnd_mod_id(mod), ret); \
376 static enum rsnd_mod_type rsnd_mod_sequence
[][RSND_MOD_MAX
] = {
410 #define rsnd_dai_call(fn, io, param...) \
412 struct rsnd_mod *mod; \
413 int type, is_play = rsnd_io_is_play(io); \
415 for (i = 0; i < RSND_MOD_MAX; i++) { \
416 type = rsnd_mod_sequence[is_play][i]; \
417 mod = (io)->mod[type]; \
420 ret |= rsnd_mod_call(type, io, fn, param); \
425 int rsnd_dai_connect(struct rsnd_mod
*mod
,
426 struct rsnd_dai_stream
*io
,
427 enum rsnd_mod_type type
)
429 struct rsnd_priv
*priv
;
435 if (io
->mod
[type
] == mod
)
441 priv
= rsnd_mod_to_priv(mod
);
442 dev
= rsnd_priv_to_dev(priv
);
446 dev_dbg(dev
, "%s[%d] is connected to io (%s)\n",
447 rsnd_mod_name(mod
), rsnd_mod_id(mod
),
448 rsnd_io_is_play(io
) ? "Playback" : "Capture");
453 static void rsnd_dai_disconnect(struct rsnd_mod
*mod
,
454 struct rsnd_dai_stream
*io
,
455 enum rsnd_mod_type type
)
457 io
->mod
[type
] = NULL
;
460 struct rsnd_dai
*rsnd_rdai_get(struct rsnd_priv
*priv
, int id
)
462 if ((id
< 0) || (id
>= rsnd_rdai_nr(priv
)))
465 return priv
->rdai
+ id
;
468 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
469 static struct rsnd_dai
*rsnd_dai_to_rdai(struct snd_soc_dai
*dai
)
471 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
473 return rsnd_rdai_get(priv
, dai
->id
);
477 * rsnd_soc_dai functions
479 int rsnd_dai_pointer_offset(struct rsnd_dai_stream
*io
, int additional
)
481 struct snd_pcm_substream
*substream
= io
->substream
;
482 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
483 int pos
= io
->byte_pos
+ additional
;
485 pos
%= (runtime
->periods
* io
->byte_per_period
);
490 bool rsnd_dai_pointer_update(struct rsnd_dai_stream
*io
, int byte
)
492 io
->byte_pos
+= byte
;
494 if (io
->byte_pos
>= io
->next_period_byte
) {
495 struct snd_pcm_substream
*substream
= io
->substream
;
496 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
499 io
->next_period_byte
+= io
->byte_per_period
;
501 if (io
->period_pos
>= runtime
->periods
) {
504 io
->next_period_byte
= io
->byte_per_period
;
513 void rsnd_dai_period_elapsed(struct rsnd_dai_stream
*io
)
515 struct snd_pcm_substream
*substream
= io
->substream
;
518 * this function should be called...
520 * - if rsnd_dai_pointer_update() returns true
521 * - without spin lock
524 snd_pcm_period_elapsed(substream
);
527 static void rsnd_dai_stream_init(struct rsnd_dai_stream
*io
,
528 struct snd_pcm_substream
*substream
)
530 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
532 io
->substream
= substream
;
535 io
->byte_per_period
= runtime
->period_size
*
537 samples_to_bytes(runtime
, 1);
538 io
->next_period_byte
= io
->byte_per_period
;
541 static void rsnd_dai_stream_quit(struct rsnd_dai_stream
*io
)
543 io
->substream
= NULL
;
547 struct snd_soc_dai
*rsnd_substream_to_dai(struct snd_pcm_substream
*substream
)
549 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
555 struct rsnd_dai_stream
*rsnd_rdai_to_io(struct rsnd_dai
*rdai
,
556 struct snd_pcm_substream
*substream
)
558 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
559 return &rdai
->playback
;
561 return &rdai
->capture
;
564 static int rsnd_soc_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
565 struct snd_soc_dai
*dai
)
567 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
568 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
569 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
573 spin_lock_irqsave(&priv
->lock
, flags
);
576 case SNDRV_PCM_TRIGGER_START
:
577 rsnd_dai_stream_init(io
, substream
);
579 ret
= rsnd_dai_call(init
, io
, priv
);
581 goto dai_trigger_end
;
583 ret
= rsnd_dai_call(start
, io
, priv
);
585 goto dai_trigger_end
;
587 ret
= rsnd_dai_call(irq
, io
, priv
, 1);
589 goto dai_trigger_end
;
592 case SNDRV_PCM_TRIGGER_STOP
:
593 ret
= rsnd_dai_call(irq
, io
, priv
, 0);
595 ret
|= rsnd_dai_call(stop
, io
, priv
);
597 ret
|= rsnd_dai_call(quit
, io
, priv
);
599 rsnd_dai_stream_quit(io
);
606 spin_unlock_irqrestore(&priv
->lock
, flags
);
611 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
613 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
615 /* set master/slave audio interface */
616 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
617 case SND_SOC_DAIFMT_CBM_CFM
:
618 rdai
->clk_master
= 0;
620 case SND_SOC_DAIFMT_CBS_CFS
:
621 rdai
->clk_master
= 1; /* codec is slave, cpu is master */
628 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
629 case SND_SOC_DAIFMT_I2S
:
631 rdai
->data_alignment
= 0;
632 rdai
->frm_clk_inv
= 0;
634 case SND_SOC_DAIFMT_LEFT_J
:
636 rdai
->data_alignment
= 0;
637 rdai
->frm_clk_inv
= 1;
639 case SND_SOC_DAIFMT_RIGHT_J
:
641 rdai
->data_alignment
= 1;
642 rdai
->frm_clk_inv
= 1;
646 /* set clock inversion */
647 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
648 case SND_SOC_DAIFMT_NB_IF
:
649 rdai
->bit_clk_inv
= rdai
->bit_clk_inv
;
650 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
652 case SND_SOC_DAIFMT_IB_NF
:
653 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
654 rdai
->frm_clk_inv
= rdai
->frm_clk_inv
;
656 case SND_SOC_DAIFMT_IB_IF
:
657 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
658 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
660 case SND_SOC_DAIFMT_NB_NF
:
668 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
669 u32 tx_mask
, u32 rx_mask
,
670 int slots
, int slot_width
)
672 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
673 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
674 struct device
*dev
= rsnd_priv_to_dev(priv
);
678 /* TDM Extend Mode */
679 rsnd_set_slot(rdai
, slots
, 1);
682 dev_err(dev
, "unsupported TDM slots (%d)\n", slots
);
689 static const struct snd_soc_dai_ops rsnd_soc_dai_ops
= {
690 .trigger
= rsnd_soc_dai_trigger
,
691 .set_fmt
= rsnd_soc_dai_set_fmt
,
692 .set_tdm_slot
= rsnd_soc_set_dai_tdm_slot
,
695 void rsnd_parse_connect_common(struct rsnd_dai
*rdai
,
696 struct rsnd_mod
* (*mod_get
)(struct rsnd_priv
*priv
, int id
),
697 struct device_node
*node
,
698 struct device_node
*playback
,
699 struct device_node
*capture
)
701 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
702 struct device_node
*np
;
703 struct rsnd_mod
*mod
;
710 for_each_child_of_node(node
, np
) {
711 mod
= mod_get(priv
, i
);
713 rsnd_dai_connect(mod
, &rdai
->playback
, mod
->type
);
715 rsnd_dai_connect(mod
, &rdai
->capture
, mod
->type
);
722 static int rsnd_dai_probe(struct rsnd_priv
*priv
)
724 struct device_node
*dai_node
;
725 struct device_node
*dai_np
;
726 struct device_node
*playback
, *capture
;
727 struct rsnd_dai_stream
*io_playback
;
728 struct rsnd_dai_stream
*io_capture
;
729 struct snd_soc_dai_driver
*rdrv
, *drv
;
730 struct rsnd_dai
*rdai
;
731 struct device
*dev
= rsnd_priv_to_dev(priv
);
735 dai_node
= rsnd_dai_of_node(priv
);
736 nr
= of_get_child_count(dai_node
);
739 goto rsnd_dai_probe_done
;
742 rdrv
= devm_kzalloc(dev
, sizeof(*rdrv
) * nr
, GFP_KERNEL
);
743 rdai
= devm_kzalloc(dev
, sizeof(*rdai
) * nr
, GFP_KERNEL
);
744 if (!rdrv
|| !rdai
) {
746 goto rsnd_dai_probe_done
;
757 for_each_child_of_node(dai_node
, dai_np
) {
758 rdai
= rsnd_rdai_get(priv
, dai_i
);
760 io_playback
= &rdai
->playback
;
761 io_capture
= &rdai
->capture
;
763 snprintf(rdai
->name
, RSND_DAI_NAME_SIZE
, "rsnd-dai.%d", dai_i
);
766 drv
->name
= rdai
->name
;
767 drv
->ops
= &rsnd_soc_dai_ops
;
769 snprintf(rdai
->playback
.name
, RSND_DAI_NAME_SIZE
,
770 "DAI%d Playback", dai_i
);
771 drv
->playback
.rates
= RSND_RATES
;
772 drv
->playback
.formats
= RSND_FMTS
;
773 drv
->playback
.channels_min
= 2;
774 drv
->playback
.channels_max
= 6;
775 drv
->playback
.stream_name
= rdai
->playback
.name
;
777 snprintf(rdai
->capture
.name
, RSND_DAI_NAME_SIZE
,
778 "DAI%d Capture", dai_i
);
779 drv
->capture
.rates
= RSND_RATES
;
780 drv
->capture
.formats
= RSND_FMTS
;
781 drv
->capture
.channels_min
= 2;
782 drv
->capture
.channels_max
= 6;
783 drv
->capture
.stream_name
= rdai
->capture
.name
;
785 rdai
->playback
.rdai
= rdai
;
786 rdai
->capture
.rdai
= rdai
;
787 rsnd_set_slot(rdai
, 2, 1); /* default */
789 for (io_i
= 0;; io_i
++) {
790 playback
= of_parse_phandle(dai_np
, "playback", io_i
);
791 capture
= of_parse_phandle(dai_np
, "capture", io_i
);
793 if (!playback
&& !capture
)
796 rsnd_parse_connect_ssi(rdai
, playback
, capture
);
797 rsnd_parse_connect_src(rdai
, playback
, capture
);
798 rsnd_parse_connect_ctu(rdai
, playback
, capture
);
799 rsnd_parse_connect_mix(rdai
, playback
, capture
);
800 rsnd_parse_connect_dvc(rdai
, playback
, capture
);
802 of_node_put(playback
);
803 of_node_put(capture
);
808 dev_dbg(dev
, "%s (%s/%s)\n", rdai
->name
,
809 rsnd_io_to_mod_ssi(io_playback
) ? "play" : " -- ",
810 rsnd_io_to_mod_ssi(io_capture
) ? "capture" : " -- ");
816 of_node_put(dai_node
);
824 static struct snd_pcm_hardware rsnd_pcm_hardware
= {
825 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
826 SNDRV_PCM_INFO_MMAP
|
827 SNDRV_PCM_INFO_MMAP_VALID
,
828 .buffer_bytes_max
= 64 * 1024,
829 .period_bytes_min
= 32,
830 .period_bytes_max
= 8192,
836 static int rsnd_pcm_open(struct snd_pcm_substream
*substream
)
838 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
841 snd_soc_set_runtime_hwparams(substream
, &rsnd_pcm_hardware
);
843 ret
= snd_pcm_hw_constraint_integer(runtime
,
844 SNDRV_PCM_HW_PARAM_PERIODS
);
849 static int rsnd_hw_params(struct snd_pcm_substream
*substream
,
850 struct snd_pcm_hw_params
*hw_params
)
852 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
853 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
854 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
857 ret
= rsnd_dai_call(hw_params
, io
, substream
, hw_params
);
861 return snd_pcm_lib_malloc_pages(substream
,
862 params_buffer_bytes(hw_params
));
865 static snd_pcm_uframes_t
rsnd_pointer(struct snd_pcm_substream
*substream
)
867 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
868 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
869 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
870 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
872 return bytes_to_frames(runtime
, io
->byte_pos
);
875 static struct snd_pcm_ops rsnd_pcm_ops
= {
876 .open
= rsnd_pcm_open
,
877 .ioctl
= snd_pcm_lib_ioctl
,
878 .hw_params
= rsnd_hw_params
,
879 .hw_free
= snd_pcm_lib_free_pages
,
880 .pointer
= rsnd_pointer
,
886 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
887 static int rsnd_kctrl_info(struct snd_kcontrol
*kctrl
,
888 struct snd_ctl_elem_info
*uinfo
)
890 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
893 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
894 uinfo
->count
= cfg
->size
;
895 uinfo
->value
.enumerated
.items
= cfg
->max
;
896 if (uinfo
->value
.enumerated
.item
>= cfg
->max
)
897 uinfo
->value
.enumerated
.item
= cfg
->max
- 1;
898 strlcpy(uinfo
->value
.enumerated
.name
,
899 cfg
->texts
[uinfo
->value
.enumerated
.item
],
900 sizeof(uinfo
->value
.enumerated
.name
));
902 uinfo
->count
= cfg
->size
;
903 uinfo
->value
.integer
.min
= 0;
904 uinfo
->value
.integer
.max
= cfg
->max
;
905 uinfo
->type
= (cfg
->max
== 1) ?
906 SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
907 SNDRV_CTL_ELEM_TYPE_INTEGER
;
913 static int rsnd_kctrl_get(struct snd_kcontrol
*kctrl
,
914 struct snd_ctl_elem_value
*uc
)
916 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
919 for (i
= 0; i
< cfg
->size
; i
++)
921 uc
->value
.enumerated
.item
[i
] = cfg
->val
[i
];
923 uc
->value
.integer
.value
[i
] = cfg
->val
[i
];
928 static int rsnd_kctrl_put(struct snd_kcontrol
*kctrl
,
929 struct snd_ctl_elem_value
*uc
)
931 struct rsnd_mod
*mod
= snd_kcontrol_chip(kctrl
);
932 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
935 for (i
= 0; i
< cfg
->size
; i
++) {
937 change
|= (uc
->value
.enumerated
.item
[i
] != cfg
->val
[i
]);
938 cfg
->val
[i
] = uc
->value
.enumerated
.item
[i
];
940 change
|= (uc
->value
.integer
.value
[i
] != cfg
->val
[i
]);
941 cfg
->val
[i
] = uc
->value
.integer
.value
[i
];
945 if (change
&& cfg
->update
)
946 cfg
->update(cfg
->io
, mod
);
951 static int __rsnd_kctrl_new(struct rsnd_mod
*mod
,
952 struct rsnd_dai_stream
*io
,
953 struct snd_soc_pcm_runtime
*rtd
,
954 const unsigned char *name
,
955 struct rsnd_kctrl_cfg
*cfg
,
956 void (*update
)(struct rsnd_dai_stream
*io
,
957 struct rsnd_mod
*mod
))
959 struct snd_card
*card
= rtd
->card
->snd_card
;
960 struct snd_kcontrol
*kctrl
;
961 struct snd_kcontrol_new knew
= {
962 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
964 .info
= rsnd_kctrl_info
,
966 .get
= rsnd_kctrl_get
,
967 .put
= rsnd_kctrl_put
,
968 .private_value
= (unsigned long)cfg
,
972 kctrl
= snd_ctl_new1(&knew
, mod
);
976 ret
= snd_ctl_add(card
, kctrl
);
978 snd_ctl_free_one(kctrl
);
982 cfg
->update
= update
;
990 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg
*cfg
)
992 snd_ctl_remove(cfg
->card
, cfg
->kctrl
);
995 int rsnd_kctrl_new_m(struct rsnd_mod
*mod
,
996 struct rsnd_dai_stream
*io
,
997 struct snd_soc_pcm_runtime
*rtd
,
998 const unsigned char *name
,
999 void (*update
)(struct rsnd_dai_stream
*io
,
1000 struct rsnd_mod
*mod
),
1001 struct rsnd_kctrl_cfg_m
*_cfg
,
1005 if (ch_size
> RSND_MAX_CHANNELS
)
1008 _cfg
->cfg
.max
= max
;
1009 _cfg
->cfg
.size
= ch_size
;
1010 _cfg
->cfg
.val
= _cfg
->val
;
1011 return __rsnd_kctrl_new(mod
, io
, rtd
, name
, &_cfg
->cfg
, update
);
1014 int rsnd_kctrl_new_s(struct rsnd_mod
*mod
,
1015 struct rsnd_dai_stream
*io
,
1016 struct snd_soc_pcm_runtime
*rtd
,
1017 const unsigned char *name
,
1018 void (*update
)(struct rsnd_dai_stream
*io
,
1019 struct rsnd_mod
*mod
),
1020 struct rsnd_kctrl_cfg_s
*_cfg
,
1023 _cfg
->cfg
.max
= max
;
1025 _cfg
->cfg
.val
= &_cfg
->val
;
1026 return __rsnd_kctrl_new(mod
, io
, rtd
, name
, &_cfg
->cfg
, update
);
1029 int rsnd_kctrl_new_e(struct rsnd_mod
*mod
,
1030 struct rsnd_dai_stream
*io
,
1031 struct snd_soc_pcm_runtime
*rtd
,
1032 const unsigned char *name
,
1033 struct rsnd_kctrl_cfg_s
*_cfg
,
1034 void (*update
)(struct rsnd_dai_stream
*io
,
1035 struct rsnd_mod
*mod
),
1036 const char * const *texts
,
1039 _cfg
->cfg
.max
= max
;
1041 _cfg
->cfg
.val
= &_cfg
->val
;
1042 _cfg
->cfg
.texts
= texts
;
1043 return __rsnd_kctrl_new(mod
, io
, rtd
, name
, &_cfg
->cfg
, update
);
1050 #define PREALLOC_BUFFER (32 * 1024)
1051 #define PREALLOC_BUFFER_MAX (32 * 1024)
1053 static int rsnd_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
1055 struct snd_soc_dai
*dai
= rtd
->cpu_dai
;
1056 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
1059 ret
= rsnd_dai_call(pcm_new
, &rdai
->playback
, rtd
);
1063 ret
= rsnd_dai_call(pcm_new
, &rdai
->capture
, rtd
);
1067 return snd_pcm_lib_preallocate_pages_for_all(
1070 rtd
->card
->snd_card
->dev
,
1071 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1074 static struct snd_soc_platform_driver rsnd_soc_platform
= {
1075 .ops
= &rsnd_pcm_ops
,
1076 .pcm_new
= rsnd_pcm_new
,
1079 static const struct snd_soc_component_driver rsnd_soc_component
= {
1083 static int rsnd_rdai_continuance_probe(struct rsnd_priv
*priv
,
1084 struct rsnd_dai_stream
*io
)
1088 ret
= rsnd_dai_call(probe
, io
, priv
);
1089 if (ret
== -EAGAIN
) {
1090 struct rsnd_mod
*ssi_mod
= rsnd_io_to_mod_ssi(io
);
1094 * Fallback to PIO mode
1098 * call "remove" for SSI/SRC/DVC
1099 * SSI will be switch to PIO mode if it was DMA mode
1102 * rsnd_ssi_fallback()
1104 rsnd_dai_call(remove
, io
, priv
);
1107 * remove all mod from io
1108 * and, re connect ssi
1110 for (i
= 0; i
< RSND_MOD_MAX
; i
++)
1111 rsnd_dai_disconnect((io
)->mod
[i
], io
, i
);
1112 rsnd_dai_connect(ssi_mod
, io
, RSND_MOD_SSI
);
1117 rsnd_dai_call(fallback
, io
, priv
);
1121 * DAI has SSI which is PIO mode only now.
1123 ret
= rsnd_dai_call(probe
, io
, priv
);
1132 static int rsnd_probe(struct platform_device
*pdev
)
1134 struct rsnd_priv
*priv
;
1135 struct device
*dev
= &pdev
->dev
;
1136 struct rsnd_dai
*rdai
;
1137 int (*probe_func
[])(struct rsnd_priv
*priv
) = {
1155 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
1157 dev_err(dev
, "priv allocate failed\n");
1162 priv
->flags
= (unsigned long)of_device_get_match_data(dev
);
1163 spin_lock_init(&priv
->lock
);
1168 for (i
= 0; i
< ARRAY_SIZE(probe_func
); i
++) {
1169 ret
= probe_func
[i
](priv
);
1174 for_each_rsnd_dai(rdai
, priv
, i
) {
1175 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->playback
);
1177 goto exit_snd_probe
;
1179 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->capture
);
1181 goto exit_snd_probe
;
1184 dev_set_drvdata(dev
, priv
);
1189 ret
= snd_soc_register_platform(dev
, &rsnd_soc_platform
);
1191 dev_err(dev
, "cannot snd soc register\n");
1195 ret
= snd_soc_register_component(dev
, &rsnd_soc_component
,
1196 priv
->daidrv
, rsnd_rdai_nr(priv
));
1198 dev_err(dev
, "cannot snd dai register\n");
1202 pm_runtime_enable(dev
);
1204 dev_info(dev
, "probed\n");
1208 snd_soc_unregister_platform(dev
);
1210 for_each_rsnd_dai(rdai
, priv
, i
) {
1211 rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1212 rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1218 static int rsnd_remove(struct platform_device
*pdev
)
1220 struct rsnd_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
1221 struct rsnd_dai
*rdai
;
1222 void (*remove_func
[])(struct rsnd_priv
*priv
) = {
1234 pm_runtime_disable(&pdev
->dev
);
1236 for_each_rsnd_dai(rdai
, priv
, i
) {
1237 ret
|= rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1238 ret
|= rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1241 for (i
= 0; i
< ARRAY_SIZE(remove_func
); i
++)
1242 remove_func
[i
](priv
);
1244 snd_soc_unregister_component(&pdev
->dev
);
1245 snd_soc_unregister_platform(&pdev
->dev
);
1250 static struct platform_driver rsnd_driver
= {
1252 .name
= "rcar_sound",
1253 .of_match_table
= rsnd_of_match
,
1255 .probe
= rsnd_probe
,
1256 .remove
= rsnd_remove
,
1258 module_platform_driver(rsnd_driver
);
1260 MODULE_LICENSE("GPL");
1261 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1262 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1263 MODULE_ALIAS("platform:rcar-pcm-audio");