1 // SPDX-License-Identifier: GPL-2.0
3 // Renesas R-Car SRU/SCU/SSIU/SSI support
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
12 * Renesas R-Car sound device structure
16 * SRU : Sound Routing Unit
17 * - SRC : Sampling Rate Converter
19 * - CTU : Channel Count Conversion Unit
21 * - DVC : Digital Volume and Mute Function
22 * - SSI : Serial Sound Interface
26 * SCU : Sampling Rate Converter Unit
27 * - SRC : Sampling Rate Converter
29 * - CTU : Channel Count Conversion Unit
31 * - DVC : Digital Volume and Mute Function
32 * SSIU : Serial Sound Interface Unit
33 * - SSI : Serial Sound Interface
41 * | ** this depends on Gen1/Gen2
45 * | ** these depend on data path
46 * | ** gen and platform data control it
50 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
53 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
57 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
60 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
63 * | ** these control ssi
72 * | ** these control src
82 * for_each_rsnd_dai(xx, priv, xx)
83 * rdai[0] => rdai[1] => rdai[2] => ...
85 * for_each_rsnd_mod(xx, rdai, xx)
86 * [mod] => [mod] => [mod] => ...
88 * rsnd_dai_call(xxx, fn )
89 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
93 #include <linux/pm_runtime.h>
96 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
97 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
98 SNDRV_PCM_FMTBIT_S16_LE |\
99 SNDRV_PCM_FMTBIT_S24_LE)
101 static const struct of_device_id rsnd_of_match[] = {
102 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
103 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
104 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
105 /* Special Handling */
106 { .compatible = "renesas,rcar_sound-r8a77990", .data = (void *)(RSND_GEN3 | RSND_SOC_E) },
109 MODULE_DEVICE_TABLE(of, rsnd_of_match);
114 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
116 if (mod->type != type) {
117 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
118 struct device *dev = rsnd_priv_to_dev(priv);
120 dev_warn(dev, "%s is not your expected module\n",
125 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
126 struct rsnd_mod *mod)
128 if (!mod || !mod->ops || !mod->ops->dma_req)
131 return mod->ops->dma_req(io, mod);
134 #define MOD_NAME_NUM 5
135 #define MOD_NAME_SIZE 16
136 char *rsnd_mod_name(struct rsnd_mod *mod)
138 static char names[MOD_NAME_NUM][MOD_NAME_SIZE];
140 char *name = names[num];
143 if (num >= MOD_NAME_NUM)
147 * Let's use same char to avoid pointlessness memory
148 * Thus, rsnd_mod_name() should be used immediately
151 if ((mod)->ops->id_sub) {
152 snprintf(name, MOD_NAME_SIZE, "%s[%d%d]",
155 rsnd_mod_id_sub(mod));
157 snprintf(name, MOD_NAME_SIZE, "%s[%d]",
165 u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
166 struct rsnd_dai_stream *io,
167 enum rsnd_mod_type type)
172 int rsnd_mod_id_raw(struct rsnd_mod *mod)
177 int rsnd_mod_id(struct rsnd_mod *mod)
180 return (mod)->ops->id(mod);
182 return rsnd_mod_id_raw(mod);
185 int rsnd_mod_id_sub(struct rsnd_mod *mod)
187 if ((mod)->ops->id_sub)
188 return (mod)->ops->id_sub(mod);
193 int rsnd_mod_init(struct rsnd_priv *priv,
194 struct rsnd_mod *mod,
195 struct rsnd_mod_ops *ops,
197 enum rsnd_mod_type type,
200 int ret = clk_prepare(clk);
214 void rsnd_mod_quit(struct rsnd_mod *mod)
216 clk_unprepare(mod->clk);
220 void rsnd_mod_interrupt(struct rsnd_mod *mod,
221 void (*callback)(struct rsnd_mod *mod,
222 struct rsnd_dai_stream *io))
224 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
225 struct rsnd_dai *rdai;
228 for_each_rsnd_dai(rdai, priv, i) {
229 struct rsnd_dai_stream *io = &rdai->playback;
231 if (mod == io->mod[mod->type])
235 if (mod == io->mod[mod->type])
240 int rsnd_io_is_working(struct rsnd_dai_stream *io)
242 /* see rsnd_dai_stream_init/quit() */
244 return snd_pcm_running(io->substream);
249 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
250 struct snd_pcm_hw_params *params)
252 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
255 * params will be added when refine
257 * __rsnd_soc_hw_rule_rate()
258 * __rsnd_soc_hw_rule_channels()
261 return params_channels(params);
263 return runtime->channels;
267 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
268 struct snd_pcm_hw_params *params)
270 int chan = rsnd_runtime_channel_original_with_params(io, params);
271 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
274 u32 converted_chan = rsnd_io_converted_chan(io);
279 * converted_chan will be used for CTU,
281 * User shouldn't use CTU with TDM Split mode.
283 if (rsnd_runtime_is_tdm_split(io)) {
284 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
286 dev_err(dev, "CTU and TDM Split should be used\n");
290 return converted_chan;
296 int rsnd_channel_normalization(int chan)
298 if (WARN_ON((chan > 8) || (chan < 0)))
301 /* TDM Extend Mode needs 8ch */
308 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
309 struct snd_pcm_hw_params *params)
311 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
312 int chan = rsnd_io_is_play(io) ?
313 rsnd_runtime_channel_after_ctu_with_params(io, params) :
314 rsnd_runtime_channel_original_with_params(io, params);
317 if (rsnd_runtime_is_multi_ssi(io))
318 chan /= rsnd_rdai_ssi_lane_get(rdai);
320 return rsnd_channel_normalization(chan);
323 int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io)
325 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
326 int lane = rsnd_rdai_ssi_lane_get(rdai);
327 int chan = rsnd_io_is_play(io) ?
328 rsnd_runtime_channel_after_ctu(io) :
329 rsnd_runtime_channel_original(io);
331 return (chan > 2) && (lane > 1);
334 int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io)
336 return rsnd_runtime_channel_for_ssi(io) >= 6;
339 int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io)
341 return !!rsnd_flags_has(io, RSND_STREAM_TDM_SPLIT);
347 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
349 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
350 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
351 struct device *dev = rsnd_priv_to_dev(priv);
353 switch (snd_pcm_format_width(runtime->format)) {
362 dev_warn(dev, "not supported sample bits\n");
370 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
372 static const u32 dalign_values[8] = {
373 0x76543210, 0x00000032, 0x00007654, 0x00000076,
374 0xfedcba98, 0x000000ba, 0x0000fedc, 0x000000fe,
377 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
378 struct rsnd_mod *target;
379 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
383 * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
387 * We need to care about inversion timing to control
388 * Playback/Capture correctly.
389 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
391 * sL/R : software L/R
392 * hL/R : hardware L/R
393 * (*) : conversion timing
396 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
397 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
400 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
401 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
403 if (rsnd_io_is_play(io)) {
404 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
406 target = src ? src : ssiu;
408 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
410 target = cmd ? cmd : ssiu;
414 id = rsnd_mod_id_sub(mod);
416 dalign = dalign_values[id];
418 if (mod == target && snd_pcm_format_width(runtime->format) == 16) {
419 /* Target mod needs inverted DALIGN when 16bit */
420 dalign = (dalign & 0xf0f0f0f0) >> 4 |
421 (dalign & 0x0f0f0f0f) << 4;
427 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
429 static const enum rsnd_mod_type playback_mods[] = {
434 static const enum rsnd_mod_type capture_mods[] = {
439 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
440 struct rsnd_mod *tmod = NULL;
441 const enum rsnd_mod_type *mods =
442 rsnd_io_is_play(io) ?
443 playback_mods : capture_mods;
447 * This is needed for 24bit data
448 * We need to shift 8bit
450 * Linux 24bit data is located as 0x00******
451 * HW 24bit data is located as 0x******00
454 if (snd_pcm_format_width(runtime->format) != 24)
457 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
458 tmod = rsnd_io_to_mod(io, mods[i]);
466 if (rsnd_io_is_play(io))
467 return (0 << 20) | /* shift to Left */
468 (8 << 16); /* 8bit */
470 return (1 << 20) | /* shift to Right */
471 (8 << 16); /* 8bit */
477 struct rsnd_mod *rsnd_mod_next(int *iterator,
478 struct rsnd_dai_stream *io,
479 enum rsnd_mod_type *array,
482 int max = array ? array_size : RSND_MOD_MAX;
484 for (; *iterator < max; (*iterator)++) {
485 enum rsnd_mod_type type = (array) ? array[*iterator] : *iterator;
486 struct rsnd_mod *mod = rsnd_io_to_mod(io, type);
495 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
529 static int rsnd_status_update(struct rsnd_dai_stream *io,
530 struct rsnd_mod *mod, enum rsnd_mod_type type,
531 int shift, int add, int timing)
533 u32 *status = mod->ops->get_status(mod, io, type);
534 u32 mask = 0xF << shift;
535 u8 val = (*status >> shift) & 0xF;
536 u8 next_val = (val + add) & 0xF;
537 int func_call = (val == timing);
539 /* no status update */
540 if (add == 0 || shift == 28)
543 if (next_val == 0xF) /* underflow case */
546 *status = (*status & ~mask) + (next_val << shift);
551 #define rsnd_dai_call(fn, io, param...) \
553 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
554 struct rsnd_mod *mod; \
555 int is_play = rsnd_io_is_play(io); \
557 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
558 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
560 int func_call = rsnd_status_update(io, mod, types[i], \
561 __rsnd_mod_shift_##fn, \
562 __rsnd_mod_add_##fn, \
563 __rsnd_mod_call_##fn); \
564 if (func_call > 0 && (mod)->ops->fn) \
565 tmp = (mod)->ops->fn(mod, io, param); \
566 if (unlikely(func_call < 0) || \
567 unlikely(tmp && (tmp != -EPROBE_DEFER))) \
568 dev_err(dev, "%s : %s error (%d, %d)\n", \
569 rsnd_mod_name(mod), #fn, tmp, func_call);\
575 int rsnd_dai_connect(struct rsnd_mod *mod,
576 struct rsnd_dai_stream *io,
577 enum rsnd_mod_type type)
579 struct rsnd_priv *priv;
585 if (io->mod[type] == mod)
591 priv = rsnd_mod_to_priv(mod);
592 dev = rsnd_priv_to_dev(priv);
596 dev_dbg(dev, "%s is connected to io (%s)\n",
598 rsnd_io_is_play(io) ? "Playback" : "Capture");
603 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
604 struct rsnd_dai_stream *io,
605 enum rsnd_mod_type type)
607 io->mod[type] = NULL;
610 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
613 if (max_channels > 0)
614 rdai->max_channels = max_channels;
616 return rdai->max_channels;
619 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
623 rdai->ssi_lane = ssi_lane;
625 return rdai->ssi_lane;
628 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
631 rdai->chan_width = width;
633 return rdai->chan_width;
636 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
638 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
641 return priv->rdai + id;
644 static struct snd_soc_dai_driver
645 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
647 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
650 return priv->daidrv + id;
653 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
654 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
656 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
658 return rsnd_rdai_get(priv, dai->id);
662 * rsnd_soc_dai functions
664 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
666 struct snd_pcm_substream *substream = io->substream;
669 * this function should be called...
671 * - if rsnd_dai_pointer_update() returns true
672 * - without spin lock
675 snd_pcm_period_elapsed(substream);
678 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
679 struct snd_pcm_substream *substream)
681 io->substream = substream;
684 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
686 io->substream = NULL;
690 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
692 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
694 return asoc_rtd_to_cpu(rtd, 0);
698 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
699 struct snd_pcm_substream *substream)
701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
702 return &rdai->playback;
704 return &rdai->capture;
707 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
708 struct snd_soc_dai *dai)
710 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
711 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
712 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
716 spin_lock_irqsave(&priv->lock, flags);
719 case SNDRV_PCM_TRIGGER_START:
720 case SNDRV_PCM_TRIGGER_RESUME:
721 ret = rsnd_dai_call(init, io, priv);
723 goto dai_trigger_end;
725 ret = rsnd_dai_call(start, io, priv);
727 goto dai_trigger_end;
729 ret = rsnd_dai_call(irq, io, priv, 1);
731 goto dai_trigger_end;
734 case SNDRV_PCM_TRIGGER_STOP:
735 case SNDRV_PCM_TRIGGER_SUSPEND:
736 ret = rsnd_dai_call(irq, io, priv, 0);
738 ret |= rsnd_dai_call(stop, io, priv);
740 ret |= rsnd_dai_call(quit, io, priv);
748 spin_unlock_irqrestore(&priv->lock, flags);
753 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
755 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
757 /* set clock master for audio interface */
758 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
759 case SND_SOC_DAIFMT_BC_FC:
760 rdai->clk_master = 0;
762 case SND_SOC_DAIFMT_BP_FP:
763 rdai->clk_master = 1; /* cpu is master */
770 rdai->bit_clk_inv = 0;
771 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
772 case SND_SOC_DAIFMT_I2S:
774 rdai->data_alignment = 0;
775 rdai->frm_clk_inv = 0;
777 case SND_SOC_DAIFMT_LEFT_J:
778 case SND_SOC_DAIFMT_DSP_B:
780 rdai->data_alignment = 0;
781 rdai->frm_clk_inv = 1;
783 case SND_SOC_DAIFMT_RIGHT_J:
785 rdai->data_alignment = 1;
786 rdai->frm_clk_inv = 1;
788 case SND_SOC_DAIFMT_DSP_A:
790 rdai->data_alignment = 0;
791 rdai->frm_clk_inv = 1;
795 /* set clock inversion */
796 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
797 case SND_SOC_DAIFMT_NB_IF:
798 rdai->frm_clk_inv = !rdai->frm_clk_inv;
800 case SND_SOC_DAIFMT_IB_NF:
801 rdai->bit_clk_inv = !rdai->bit_clk_inv;
803 case SND_SOC_DAIFMT_IB_IF:
804 rdai->bit_clk_inv = !rdai->bit_clk_inv;
805 rdai->frm_clk_inv = !rdai->frm_clk_inv;
807 case SND_SOC_DAIFMT_NB_NF:
815 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
816 u32 tx_mask, u32 rx_mask,
817 int slots, int slot_width)
819 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
820 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
821 struct device *dev = rsnd_priv_to_dev(priv);
823 switch (slot_width) {
838 /* TDM Extend Mode */
839 rsnd_rdai_channels_set(rdai, slots);
840 rsnd_rdai_ssi_lane_set(rdai, 1);
841 rsnd_rdai_width_set(rdai, slot_width);
844 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
851 static unsigned int rsnd_soc_hw_channels_list[] = {
855 static unsigned int rsnd_soc_hw_rate_list[] = {
870 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
871 unsigned int *list, int list_num,
872 struct snd_interval *baseline, struct snd_interval *iv)
874 struct snd_interval p;
878 snd_interval_any(&p);
882 for (i = 0; i < list_num; i++) {
884 if (!snd_interval_test(iv, list[i]))
887 rate = rsnd_ssi_clk_query(rdai,
888 baseline->min, list[i], NULL);
890 p.min = min(p.min, list[i]);
891 p.max = max(p.max, list[i]);
894 rate = rsnd_ssi_clk_query(rdai,
895 baseline->max, list[i], NULL);
897 p.min = min(p.min, list[i]);
898 p.max = max(p.max, list[i]);
902 return snd_interval_refine(iv, &p);
905 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
906 struct snd_pcm_hw_rule *rule)
908 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
909 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
910 struct snd_interval ic;
911 struct rsnd_dai_stream *io = rule->private;
912 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
915 * possible sampling rate limitation is same as
916 * 2ch if it supports multi ssi
917 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
921 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
923 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
924 ARRAY_SIZE(rsnd_soc_hw_rate_list),
928 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
929 struct snd_pcm_hw_rule *rule)
931 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
932 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
933 struct snd_interval ic;
934 struct rsnd_dai_stream *io = rule->private;
935 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
938 * possible sampling rate limitation is same as
939 * 2ch if it supports multi ssi
940 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
944 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
946 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
947 ARRAY_SIZE(rsnd_soc_hw_channels_list),
951 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
952 .info = SNDRV_PCM_INFO_INTERLEAVED |
953 SNDRV_PCM_INFO_MMAP |
954 SNDRV_PCM_INFO_MMAP_VALID,
955 .buffer_bytes_max = 64 * 1024,
956 .period_bytes_min = 32,
957 .period_bytes_max = 8192,
963 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
964 struct snd_soc_dai *dai)
966 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
967 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
968 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
969 struct snd_pcm_runtime *runtime = substream->runtime;
970 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
973 rsnd_dai_stream_init(io, substream);
977 * It depends on Platform design
979 constraint->list = rsnd_soc_hw_channels_list;
980 constraint->count = 0;
981 constraint->mask = 0;
983 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
984 if (rsnd_soc_hw_channels_list[i] > max_channels)
986 constraint->count = i + 1;
989 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
991 snd_pcm_hw_constraint_list(runtime, 0,
992 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
994 snd_pcm_hw_constraint_integer(runtime,
995 SNDRV_PCM_HW_PARAM_PERIODS);
998 * Sampling Rate / Channel Limitation
999 * It depends on Clock Master Mode
1001 if (rsnd_rdai_is_clk_master(rdai)) {
1002 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1004 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1005 rsnd_soc_hw_rule_rate,
1006 is_play ? &rdai->playback : &rdai->capture,
1007 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1008 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1009 rsnd_soc_hw_rule_channels,
1010 is_play ? &rdai->playback : &rdai->capture,
1011 SNDRV_PCM_HW_PARAM_RATE, -1);
1017 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1018 struct snd_soc_dai *dai)
1020 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1021 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1022 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1025 * call rsnd_dai_call without spinlock
1027 rsnd_dai_call(cleanup, io, priv);
1029 rsnd_dai_stream_quit(io);
1032 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1033 struct snd_soc_dai *dai)
1035 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1036 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1037 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1039 return rsnd_dai_call(prepare, io, priv);
1042 static u64 rsnd_soc_dai_formats[] = {
1046 * Well tested formats.
1047 * Select below from Sound Card, not auto
1048 * SND_SOC_DAIFMT_CBC_CFC
1049 * SND_SOC_DAIFMT_CBP_CFP
1051 SND_SOC_POSSIBLE_DAIFMT_I2S |
1052 SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
1053 SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
1054 SND_SOC_POSSIBLE_DAIFMT_NB_NF |
1055 SND_SOC_POSSIBLE_DAIFMT_NB_IF |
1056 SND_SOC_POSSIBLE_DAIFMT_IB_NF |
1057 SND_SOC_POSSIBLE_DAIFMT_IB_IF,
1061 * Supported, but not well tested
1063 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
1064 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
1067 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
1068 .startup = rsnd_soc_dai_startup,
1069 .shutdown = rsnd_soc_dai_shutdown,
1070 .trigger = rsnd_soc_dai_trigger,
1071 .set_fmt = rsnd_soc_dai_set_fmt,
1072 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
1073 .prepare = rsnd_soc_dai_prepare,
1074 .auto_selectable_formats = rsnd_soc_dai_formats,
1075 .num_auto_selectable_formats = ARRAY_SIZE(rsnd_soc_dai_formats),
1078 static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
1079 struct rsnd_dai_stream *io,
1080 struct device_node *dai_np)
1082 struct device *dev = rsnd_priv_to_dev(priv);
1083 struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1084 struct device_node *np;
1085 int is_play = rsnd_io_is_play(io);
1092 * This driver assumes that it is TDM Split mode
1093 * if it includes ssiu node
1096 struct device_node *node = is_play ?
1097 of_parse_phandle(dai_np, "playback", i) :
1098 of_parse_phandle(dai_np, "capture", i);
1103 for_each_child_of_node(ssiu_np, np) {
1105 rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1106 dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1113 of_node_put(ssiu_np);
1116 static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1117 struct rsnd_dai_stream *io,
1118 struct device_node *dai_np)
1120 if (!rsnd_io_to_mod_ssi(io))
1123 rsnd_parse_tdm_split_mode(priv, io, dai_np);
1126 static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1127 struct rsnd_dai_stream *io,
1128 struct device_node *endpoint)
1130 struct device *dev = rsnd_priv_to_dev(priv);
1131 struct device_node *remote_node;
1133 if (!rsnd_io_to_mod_ssi(io))
1136 remote_node = of_graph_get_remote_port_parent(endpoint);
1139 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1140 rsnd_flags_set(io, RSND_STREAM_HDMI0);
1141 dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1145 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1146 rsnd_flags_set(io, RSND_STREAM_HDMI1);
1147 dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1150 rsnd_parse_tdm_split_mode(priv, io, endpoint);
1152 of_node_put(remote_node);
1155 void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
1156 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1157 struct device_node *node,
1158 struct device_node *playback,
1159 struct device_node *capture)
1161 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1162 struct device *dev = rsnd_priv_to_dev(priv);
1163 struct device_node *np;
1170 for_each_child_of_node(node, np) {
1171 struct rsnd_mod *mod;
1173 i = rsnd_node_fixed_index(dev, np, name, i);
1179 mod = mod_get(priv, i);
1182 rsnd_dai_connect(mod, &rdai->playback, mod->type);
1184 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1191 int rsnd_node_fixed_index(struct device *dev, struct device_node *node, char *name, int idx)
1196 * rsnd is assuming each device nodes are sequential numbering,
1197 * but some of them are not.
1198 * This function adjusts index for it.
1201 * Normal case, special case
1209 * assume Max 64 node
1211 for (; idx < 64; idx++) {
1212 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1214 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1218 dev_err(dev, "strange node numbering (%s)",
1219 of_node_full_name(node));
1223 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1225 struct device *dev = rsnd_priv_to_dev(priv);
1226 struct device_node *np;
1230 for_each_child_of_node(node, np) {
1231 i = rsnd_node_fixed_index(dev, np, name, i);
1242 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1245 struct device *dev = rsnd_priv_to_dev(priv);
1246 struct device_node *np = dev->of_node;
1247 struct device_node *dai_node;
1248 struct device_node *ret;
1253 * parse both previous dai (= rcar_sound,dai), and
1254 * graph dai (= ports/port)
1256 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1259 goto of_node_compatible;
1264 dai_node = of_graph_get_next_endpoint(np, NULL);
1273 of_node_put(dai_node);
1279 #define PREALLOC_BUFFER (32 * 1024)
1280 #define PREALLOC_BUFFER_MAX (32 * 1024)
1282 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1283 struct rsnd_dai_stream *io,
1286 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1287 struct device *dev = rsnd_priv_to_dev(priv);
1288 struct snd_pcm_substream *substream;
1291 * use Audio-DMAC dev if we can use IPMMU
1293 * rsnd_dmaen_attach()
1298 for (substream = rtd->pcm->streams[stream].substream;
1300 substream = substream->next) {
1301 snd_pcm_set_managed_buffer(substream,
1304 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1310 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd,
1311 struct snd_soc_dai *dai)
1313 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1316 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1320 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1324 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1325 SNDRV_PCM_STREAM_PLAYBACK);
1329 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1330 SNDRV_PCM_STREAM_CAPTURE);
1337 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1338 struct device_node *dai_np,
1341 struct rsnd_dai_stream *io_playback;
1342 struct rsnd_dai_stream *io_capture;
1343 struct snd_soc_dai_driver *drv;
1344 struct rsnd_dai *rdai;
1345 struct device *dev = rsnd_priv_to_dev(priv);
1348 rdai = rsnd_rdai_get(priv, dai_i);
1349 drv = rsnd_daidrv_get(priv, dai_i);
1350 io_playback = &rdai->playback;
1351 io_capture = &rdai->capture;
1353 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1356 drv->name = rdai->name;
1357 drv->ops = &rsnd_soc_dai_ops;
1358 drv->pcm_new = rsnd_pcm_new;
1360 snprintf(io_playback->name, RSND_DAI_NAME_SIZE,
1361 "DAI%d Playback", dai_i);
1362 drv->playback.rates = RSND_RATES;
1363 drv->playback.formats = RSND_FMTS;
1364 drv->playback.channels_min = 2;
1365 drv->playback.channels_max = 8;
1366 drv->playback.stream_name = io_playback->name;
1368 snprintf(io_capture->name, RSND_DAI_NAME_SIZE,
1369 "DAI%d Capture", dai_i);
1370 drv->capture.rates = RSND_RATES;
1371 drv->capture.formats = RSND_FMTS;
1372 drv->capture.channels_min = 2;
1373 drv->capture.channels_max = 8;
1374 drv->capture.stream_name = io_capture->name;
1376 io_playback->rdai = rdai;
1377 io_capture->rdai = rdai;
1378 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1379 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1380 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
1382 for (io_i = 0;; io_i++) {
1383 struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1384 struct device_node *capture = of_parse_phandle(dai_np, "capture", io_i);
1386 if (!playback && !capture)
1389 rsnd_parse_connect_ssi(rdai, playback, capture);
1390 rsnd_parse_connect_ssiu(rdai, playback, capture);
1391 rsnd_parse_connect_src(rdai, playback, capture);
1392 rsnd_parse_connect_ctu(rdai, playback, capture);
1393 rsnd_parse_connect_mix(rdai, playback, capture);
1394 rsnd_parse_connect_dvc(rdai, playback, capture);
1396 of_node_put(playback);
1397 of_node_put(capture);
1400 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1401 rsnd_ssi_is_pin_sharing(io_playback)) {
1402 /* should have symmetric_rate if pin sharing */
1403 drv->symmetric_rate = 1;
1406 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1407 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1408 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1411 static int rsnd_dai_probe(struct rsnd_priv *priv)
1413 struct device_node *dai_node;
1414 struct device_node *dai_np;
1415 struct snd_soc_dai_driver *rdrv;
1416 struct device *dev = rsnd_priv_to_dev(priv);
1417 struct rsnd_dai *rdai;
1422 dai_node = rsnd_dai_of_node(priv, &is_graph);
1424 nr = of_graph_get_endpoint_count(dai_node);
1426 nr = of_get_child_count(dai_node);
1431 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1432 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1437 priv->daidrv = rdrv;
1445 for_each_endpoint_of_node(dai_node, dai_np) {
1446 __rsnd_dai_probe(priv, dai_np, dai_i);
1447 if (rsnd_is_gen3(priv)) {
1448 rdai = rsnd_rdai_get(priv, dai_i);
1450 rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1451 rsnd_parse_connect_graph(priv, &rdai->capture, dai_np);
1456 for_each_child_of_node(dai_node, dai_np) {
1457 __rsnd_dai_probe(priv, dai_np, dai_i);
1458 if (rsnd_is_gen3(priv)) {
1459 rdai = rsnd_rdai_get(priv, dai_i);
1461 rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1462 rsnd_parse_connect_simple(priv, &rdai->capture, dai_np);
1474 static int rsnd_hw_update(struct snd_pcm_substream *substream,
1475 struct snd_pcm_hw_params *hw_params)
1477 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1478 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1479 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1480 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1481 unsigned long flags;
1484 spin_lock_irqsave(&priv->lock, flags);
1486 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1488 ret = rsnd_dai_call(hw_free, io, substream);
1489 spin_unlock_irqrestore(&priv->lock, flags);
1494 static int rsnd_hw_params(struct snd_soc_component *component,
1495 struct snd_pcm_substream *substream,
1496 struct snd_pcm_hw_params *hw_params)
1498 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1499 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1500 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1501 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1504 * rsnd assumes that it might be used under DPCM if user want to use
1505 * channel / rate convert. Then, rsnd should be FE.
1506 * And then, this function will be called *after* BE settings.
1507 * this means, each BE already has fixuped hw_params.
1509 * dpcm_fe_dai_hw_params()
1510 * dpcm_be_dai_hw_params()
1512 io->converted_rate = 0;
1513 io->converted_chan = 0;
1514 if (fe->dai_link->dynamic) {
1515 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1516 struct device *dev = rsnd_priv_to_dev(priv);
1517 struct snd_soc_dpcm *dpcm;
1518 int stream = substream->stream;
1520 for_each_dpcm_be(fe, stream, dpcm) {
1521 struct snd_pcm_hw_params *be_params = &dpcm->hw_params;
1523 if (params_channels(hw_params) != params_channels(be_params))
1524 io->converted_chan = params_channels(be_params);
1525 if (params_rate(hw_params) != params_rate(be_params))
1526 io->converted_rate = params_rate(be_params);
1528 if (io->converted_chan)
1529 dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1530 if (io->converted_rate) {
1532 * SRC supports convert rates from params_rate(hw_params)/k_down
1533 * to params_rate(hw_params)*k_up, where k_up is always 6, and
1534 * k_down depends on number of channels and SRC unit.
1535 * So all SRC units can upsample audio up to 6 times regardless
1536 * its number of channels. And all SRC units can downsample
1537 * 2 channel audio up to 6 times too.
1542 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1544 dev_dbg(dev, "convert rate = %d\n", io->converted_rate);
1546 channel = io->converted_chan ? io->converted_chan :
1547 params_channels(hw_params);
1549 switch (rsnd_mod_id(src_mod)) {
1551 * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times.
1552 * SRC1, SRC3 and SRC4 can downsample 4 channel audio
1554 * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio
1555 * no more than twice.
1570 /* Other SRC units do not support more than 2 channels */
1576 if (params_rate(hw_params) > io->converted_rate * k_down) {
1577 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1578 io->converted_rate * k_down;
1579 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1580 io->converted_rate * k_down;
1581 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1582 } else if (params_rate(hw_params) * k_up < io->converted_rate) {
1583 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1584 (io->converted_rate + k_up - 1) / k_up;
1585 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1586 (io->converted_rate + k_up - 1) / k_up;
1587 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1591 * TBD: Max SRC input and output rates also depend on number
1592 * of channels and SRC unit:
1593 * SRC1, SRC3 and SRC4 do not support more than 128kHz
1594 * for 6 channel and 96kHz for 8 channel audio.
1595 * Perhaps this function should return EINVAL if the input or
1596 * the output rate exceeds the limitation.
1601 return rsnd_hw_update(substream, hw_params);
1604 static int rsnd_hw_free(struct snd_soc_component *component,
1605 struct snd_pcm_substream *substream)
1607 return rsnd_hw_update(substream, NULL);
1610 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1611 struct snd_pcm_substream *substream)
1613 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1614 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1615 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1616 snd_pcm_uframes_t pointer = 0;
1618 rsnd_dai_call(pointer, io, &pointer);
1626 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1627 struct snd_ctl_elem_info *uinfo)
1629 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1632 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1633 uinfo->count = cfg->size;
1634 uinfo->value.enumerated.items = cfg->max;
1635 if (uinfo->value.enumerated.item >= cfg->max)
1636 uinfo->value.enumerated.item = cfg->max - 1;
1637 strscpy(uinfo->value.enumerated.name,
1638 cfg->texts[uinfo->value.enumerated.item],
1639 sizeof(uinfo->value.enumerated.name));
1641 uinfo->count = cfg->size;
1642 uinfo->value.integer.min = 0;
1643 uinfo->value.integer.max = cfg->max;
1644 uinfo->type = (cfg->max == 1) ?
1645 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1646 SNDRV_CTL_ELEM_TYPE_INTEGER;
1652 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1653 struct snd_ctl_elem_value *uc)
1655 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1658 for (i = 0; i < cfg->size; i++)
1660 uc->value.enumerated.item[i] = cfg->val[i];
1662 uc->value.integer.value[i] = cfg->val[i];
1667 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1668 struct snd_ctl_elem_value *uc)
1670 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1673 if (!cfg->accept(cfg->io))
1676 for (i = 0; i < cfg->size; i++) {
1678 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1679 cfg->val[i] = uc->value.enumerated.item[i];
1681 change |= (uc->value.integer.value[i] != cfg->val[i]);
1682 cfg->val[i] = uc->value.integer.value[i];
1686 if (change && cfg->update)
1687 cfg->update(cfg->io, cfg->mod);
1692 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1697 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1699 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1700 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1701 struct device *dev = rsnd_priv_to_dev(priv);
1704 dev_warn(dev, "Can't update kctrl when idle\n");
1711 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1713 cfg->cfg.val = cfg->val;
1718 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1720 cfg->cfg.val = &cfg->val;
1725 const char * const volume_ramp_rate[] = {
1726 "128 dB/1 step", /* 00000 */
1727 "64 dB/1 step", /* 00001 */
1728 "32 dB/1 step", /* 00010 */
1729 "16 dB/1 step", /* 00011 */
1730 "8 dB/1 step", /* 00100 */
1731 "4 dB/1 step", /* 00101 */
1732 "2 dB/1 step", /* 00110 */
1733 "1 dB/1 step", /* 00111 */
1734 "0.5 dB/1 step", /* 01000 */
1735 "0.25 dB/1 step", /* 01001 */
1736 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1737 "0.125 dB/2 steps", /* 01011 */
1738 "0.125 dB/4 steps", /* 01100 */
1739 "0.125 dB/8 steps", /* 01101 */
1740 "0.125 dB/16 steps", /* 01110 */
1741 "0.125 dB/32 steps", /* 01111 */
1742 "0.125 dB/64 steps", /* 10000 */
1743 "0.125 dB/128 steps", /* 10001 */
1744 "0.125 dB/256 steps", /* 10010 */
1745 "0.125 dB/512 steps", /* 10011 */
1746 "0.125 dB/1024 steps", /* 10100 */
1747 "0.125 dB/2048 steps", /* 10101 */
1748 "0.125 dB/4096 steps", /* 10110 */
1749 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1752 int rsnd_kctrl_new(struct rsnd_mod *mod,
1753 struct rsnd_dai_stream *io,
1754 struct snd_soc_pcm_runtime *rtd,
1755 const unsigned char *name,
1756 int (*accept)(struct rsnd_dai_stream *io),
1757 void (*update)(struct rsnd_dai_stream *io,
1758 struct rsnd_mod *mod),
1759 struct rsnd_kctrl_cfg *cfg,
1760 const char * const *texts,
1764 struct snd_card *card = rtd->card->snd_card;
1765 struct snd_kcontrol *kctrl;
1766 struct snd_kcontrol_new knew = {
1767 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1769 .info = rsnd_kctrl_info,
1771 .get = rsnd_kctrl_get,
1772 .put = rsnd_kctrl_put,
1777 * 1) Avoid duplicate register for DVC with MIX case
1778 * 2) Allow duplicate register for MIX
1779 * 3) re-register if card was rebinded
1781 list_for_each_entry(kctrl, &card->controls, list) {
1782 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1788 if (size > RSND_MAX_CHANNELS)
1791 kctrl = snd_ctl_new1(&knew, cfg);
1795 ret = snd_ctl_add(card, kctrl);
1802 cfg->accept = accept;
1803 cfg->update = update;
1815 static const struct snd_soc_component_driver rsnd_soc_component = {
1817 .probe = rsnd_debugfs_probe,
1818 .hw_params = rsnd_hw_params,
1819 .hw_free = rsnd_hw_free,
1820 .pointer = rsnd_pointer,
1821 .legacy_dai_naming = 1,
1824 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1825 struct rsnd_dai_stream *io)
1829 ret = rsnd_dai_call(probe, io, priv);
1830 if (ret == -EAGAIN) {
1831 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1832 struct rsnd_mod *mod;
1836 * Fallback to PIO mode
1840 * call "remove" for SSI/SRC/DVC
1841 * SSI will be switch to PIO mode if it was DMA mode
1844 * rsnd_ssi_fallback()
1846 rsnd_dai_call(remove, io, priv);
1849 * remove all mod from io
1850 * and, re connect ssi
1852 for_each_rsnd_mod(i, mod, io)
1853 rsnd_dai_disconnect(mod, io, i);
1854 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1859 rsnd_dai_call(fallback, io, priv);
1863 * DAI has SSI which is PIO mode only now.
1865 ret = rsnd_dai_call(probe, io, priv);
1874 static int rsnd_probe(struct platform_device *pdev)
1876 struct rsnd_priv *priv;
1877 struct device *dev = &pdev->dev;
1878 struct rsnd_dai *rdai;
1879 int (*probe_func[])(struct rsnd_priv *priv) = {
1897 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1902 priv->flags = (unsigned long)of_device_get_match_data(dev);
1903 spin_lock_init(&priv->lock);
1908 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1909 ret = probe_func[i](priv);
1914 for_each_rsnd_dai(rdai, priv, i) {
1915 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1917 goto exit_snd_probe;
1919 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1921 goto exit_snd_probe;
1924 dev_set_drvdata(dev, priv);
1929 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1930 priv->daidrv, rsnd_rdai_nr(priv));
1932 dev_err(dev, "cannot snd dai register\n");
1933 goto exit_snd_probe;
1936 pm_runtime_enable(dev);
1938 dev_info(dev, "probed\n");
1942 for_each_rsnd_dai(rdai, priv, i) {
1943 rsnd_dai_call(remove, &rdai->playback, priv);
1944 rsnd_dai_call(remove, &rdai->capture, priv);
1948 * adg is very special mod which can't use rsnd_dai_call(remove),
1949 * and it registers ADG clock on probe.
1950 * It should be unregister if probe failed.
1951 * Mainly it is assuming -EPROBE_DEFER case
1953 rsnd_adg_remove(priv);
1958 static int rsnd_remove(struct platform_device *pdev)
1960 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1961 struct rsnd_dai *rdai;
1962 void (*remove_func[])(struct rsnd_priv *priv) = {
1974 pm_runtime_disable(&pdev->dev);
1976 for_each_rsnd_dai(rdai, priv, i) {
1979 ret = rsnd_dai_call(remove, &rdai->playback, priv);
1981 dev_warn(&pdev->dev, "Failed to remove playback dai #%d\n", i);
1983 ret = rsnd_dai_call(remove, &rdai->capture, priv);
1985 dev_warn(&pdev->dev, "Failed to remove capture dai #%d\n", i);
1988 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1989 remove_func[i](priv);
1994 static int __maybe_unused rsnd_suspend(struct device *dev)
1996 struct rsnd_priv *priv = dev_get_drvdata(dev);
1998 rsnd_adg_clk_disable(priv);
2003 static int __maybe_unused rsnd_resume(struct device *dev)
2005 struct rsnd_priv *priv = dev_get_drvdata(dev);
2007 rsnd_adg_clk_enable(priv);
2012 static const struct dev_pm_ops rsnd_pm_ops = {
2013 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2016 static struct platform_driver rsnd_driver = {
2018 .name = "rcar_sound",
2020 .of_match_table = rsnd_of_match,
2022 .probe = rsnd_probe,
2023 .remove = rsnd_remove,
2025 module_platform_driver(rsnd_driver);
2027 MODULE_LICENSE("GPL v2");
2028 MODULE_DESCRIPTION("Renesas R-Car audio driver");
2029 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2030 MODULE_ALIAS("platform:rcar-pcm-audio");