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 enum rsnd_mod_type playback_mods[] = {
434 enum rsnd_mod_type capture_mods[] = {
439 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
440 struct rsnd_mod *tmod = NULL;
441 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_MASTER_MASK) {
759 case SND_SOC_DAIFMT_CBP_CFP:
760 rdai->clk_master = 0;
762 case SND_SOC_DAIFMT_CBC_CFC:
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_node *np;
1169 for_each_child_of_node(node, np) {
1170 struct rsnd_mod *mod;
1172 i = rsnd_node_fixed_index(np, name, i);
1174 mod = mod_get(priv, i);
1177 rsnd_dai_connect(mod, &rdai->playback, mod->type);
1179 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1186 int rsnd_node_fixed_index(struct device_node *node, char *name, int idx)
1191 * rsnd is assuming each device nodes are sequential numbering,
1192 * but some of them are not.
1193 * This function adjusts index for it.
1196 * Normal case, special case
1204 * assume Max 64 node
1206 for (; idx < 64; idx++) {
1207 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1209 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1216 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1218 struct device *dev = rsnd_priv_to_dev(priv);
1219 struct device_node *np;
1223 for_each_child_of_node(node, np) {
1224 i = rsnd_node_fixed_index(np, name, i);
1226 dev_err(dev, "strange node numbering (%s)",
1227 of_node_full_name(node));
1236 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1239 struct device *dev = rsnd_priv_to_dev(priv);
1240 struct device_node *np = dev->of_node;
1241 struct device_node *dai_node;
1242 struct device_node *ret;
1247 * parse both previous dai (= rcar_sound,dai), and
1248 * graph dai (= ports/port)
1250 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1253 goto of_node_compatible;
1258 dai_node = of_graph_get_next_endpoint(np, NULL);
1267 of_node_put(dai_node);
1273 #define PREALLOC_BUFFER (32 * 1024)
1274 #define PREALLOC_BUFFER_MAX (32 * 1024)
1276 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1277 struct rsnd_dai_stream *io,
1280 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1281 struct device *dev = rsnd_priv_to_dev(priv);
1282 struct snd_pcm_substream *substream;
1285 * use Audio-DMAC dev if we can use IPMMU
1287 * rsnd_dmaen_attach()
1292 for (substream = rtd->pcm->streams[stream].substream;
1294 substream = substream->next) {
1295 snd_pcm_set_managed_buffer(substream,
1298 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1304 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd,
1305 struct snd_soc_dai *dai)
1307 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1310 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1314 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1318 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1319 SNDRV_PCM_STREAM_PLAYBACK);
1323 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1324 SNDRV_PCM_STREAM_CAPTURE);
1331 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1332 struct device_node *dai_np,
1335 struct rsnd_dai_stream *io_playback;
1336 struct rsnd_dai_stream *io_capture;
1337 struct snd_soc_dai_driver *drv;
1338 struct rsnd_dai *rdai;
1339 struct device *dev = rsnd_priv_to_dev(priv);
1342 rdai = rsnd_rdai_get(priv, dai_i);
1343 drv = rsnd_daidrv_get(priv, dai_i);
1344 io_playback = &rdai->playback;
1345 io_capture = &rdai->capture;
1347 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1350 drv->name = rdai->name;
1351 drv->ops = &rsnd_soc_dai_ops;
1352 drv->pcm_new = rsnd_pcm_new;
1354 snprintf(io_playback->name, RSND_DAI_NAME_SIZE,
1355 "DAI%d Playback", dai_i);
1356 drv->playback.rates = RSND_RATES;
1357 drv->playback.formats = RSND_FMTS;
1358 drv->playback.channels_min = 2;
1359 drv->playback.channels_max = 8;
1360 drv->playback.stream_name = io_playback->name;
1362 snprintf(io_capture->name, RSND_DAI_NAME_SIZE,
1363 "DAI%d Capture", dai_i);
1364 drv->capture.rates = RSND_RATES;
1365 drv->capture.formats = RSND_FMTS;
1366 drv->capture.channels_min = 2;
1367 drv->capture.channels_max = 8;
1368 drv->capture.stream_name = io_capture->name;
1370 io_playback->rdai = rdai;
1371 io_capture->rdai = rdai;
1372 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1373 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1374 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
1376 for (io_i = 0;; io_i++) {
1377 struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1378 struct device_node *capture = of_parse_phandle(dai_np, "capture", io_i);
1380 if (!playback && !capture)
1383 rsnd_parse_connect_ssi(rdai, playback, capture);
1384 rsnd_parse_connect_ssiu(rdai, playback, capture);
1385 rsnd_parse_connect_src(rdai, playback, capture);
1386 rsnd_parse_connect_ctu(rdai, playback, capture);
1387 rsnd_parse_connect_mix(rdai, playback, capture);
1388 rsnd_parse_connect_dvc(rdai, playback, capture);
1390 of_node_put(playback);
1391 of_node_put(capture);
1394 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1395 rsnd_ssi_is_pin_sharing(io_playback)) {
1396 /* should have symmetric_rate if pin sharing */
1397 drv->symmetric_rate = 1;
1400 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1401 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1402 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1405 static int rsnd_dai_probe(struct rsnd_priv *priv)
1407 struct device_node *dai_node;
1408 struct device_node *dai_np;
1409 struct snd_soc_dai_driver *rdrv;
1410 struct device *dev = rsnd_priv_to_dev(priv);
1411 struct rsnd_dai *rdai;
1416 dai_node = rsnd_dai_of_node(priv, &is_graph);
1418 nr = of_graph_get_endpoint_count(dai_node);
1420 nr = of_get_child_count(dai_node);
1425 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1426 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1431 priv->daidrv = rdrv;
1439 for_each_endpoint_of_node(dai_node, dai_np) {
1440 __rsnd_dai_probe(priv, dai_np, dai_i);
1441 if (rsnd_is_gen3(priv)) {
1442 rdai = rsnd_rdai_get(priv, dai_i);
1444 rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1445 rsnd_parse_connect_graph(priv, &rdai->capture, dai_np);
1450 for_each_child_of_node(dai_node, dai_np) {
1451 __rsnd_dai_probe(priv, dai_np, dai_i);
1452 if (rsnd_is_gen3(priv)) {
1453 rdai = rsnd_rdai_get(priv, dai_i);
1455 rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1456 rsnd_parse_connect_simple(priv, &rdai->capture, dai_np);
1468 static int rsnd_hw_update(struct snd_pcm_substream *substream,
1469 struct snd_pcm_hw_params *hw_params)
1471 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1472 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1473 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1474 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1475 unsigned long flags;
1478 spin_lock_irqsave(&priv->lock, flags);
1480 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1482 ret = rsnd_dai_call(hw_free, io, substream);
1483 spin_unlock_irqrestore(&priv->lock, flags);
1488 static int rsnd_hw_params(struct snd_soc_component *component,
1489 struct snd_pcm_substream *substream,
1490 struct snd_pcm_hw_params *hw_params)
1492 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1493 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1494 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1495 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1498 * rsnd assumes that it might be used under DPCM if user want to use
1499 * channel / rate convert. Then, rsnd should be FE.
1500 * And then, this function will be called *after* BE settings.
1501 * this means, each BE already has fixuped hw_params.
1503 * dpcm_fe_dai_hw_params()
1504 * dpcm_be_dai_hw_params()
1506 io->converted_rate = 0;
1507 io->converted_chan = 0;
1508 if (fe->dai_link->dynamic) {
1509 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1510 struct device *dev = rsnd_priv_to_dev(priv);
1511 struct snd_soc_dpcm *dpcm;
1512 int stream = substream->stream;
1514 for_each_dpcm_be(fe, stream, dpcm) {
1515 struct snd_pcm_hw_params *be_params = &dpcm->hw_params;
1517 if (params_channels(hw_params) != params_channels(be_params))
1518 io->converted_chan = params_channels(be_params);
1519 if (params_rate(hw_params) != params_rate(be_params))
1520 io->converted_rate = params_rate(be_params);
1522 if (io->converted_chan)
1523 dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1524 if (io->converted_rate) {
1526 * SRC supports convert rates from params_rate(hw_params)/k_down
1527 * to params_rate(hw_params)*k_up, where k_up is always 6, and
1528 * k_down depends on number of channels and SRC unit.
1529 * So all SRC units can upsample audio up to 6 times regardless
1530 * its number of channels. And all SRC units can downsample
1531 * 2 channel audio up to 6 times too.
1536 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1538 dev_dbg(dev, "convert rate = %d\n", io->converted_rate);
1540 channel = io->converted_chan ? io->converted_chan :
1541 params_channels(hw_params);
1543 switch (rsnd_mod_id(src_mod)) {
1545 * SRC0 can downsample 4, 6 and 8 channel audio up to 4 times.
1546 * SRC1, SRC3 and SRC4 can downsample 4 channel audio
1548 * SRC1, SRC3 and SRC4 can downsample 6 and 8 channel audio
1549 * no more than twice.
1564 /* Other SRC units do not support more than 2 channels */
1570 if (params_rate(hw_params) > io->converted_rate * k_down) {
1571 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1572 io->converted_rate * k_down;
1573 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1574 io->converted_rate * k_down;
1575 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1576 } else if (params_rate(hw_params) * k_up < io->converted_rate) {
1577 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1578 (io->converted_rate + k_up - 1) / k_up;
1579 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1580 (io->converted_rate + k_up - 1) / k_up;
1581 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1585 * TBD: Max SRC input and output rates also depend on number
1586 * of channels and SRC unit:
1587 * SRC1, SRC3 and SRC4 do not support more than 128kHz
1588 * for 6 channel and 96kHz for 8 channel audio.
1589 * Perhaps this function should return EINVAL if the input or
1590 * the output rate exceeds the limitation.
1595 return rsnd_hw_update(substream, hw_params);
1598 static int rsnd_hw_free(struct snd_soc_component *component,
1599 struct snd_pcm_substream *substream)
1601 return rsnd_hw_update(substream, NULL);
1604 static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1605 struct snd_pcm_substream *substream)
1607 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1608 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1609 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1610 snd_pcm_uframes_t pointer = 0;
1612 rsnd_dai_call(pointer, io, &pointer);
1620 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1621 struct snd_ctl_elem_info *uinfo)
1623 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1626 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1627 uinfo->count = cfg->size;
1628 uinfo->value.enumerated.items = cfg->max;
1629 if (uinfo->value.enumerated.item >= cfg->max)
1630 uinfo->value.enumerated.item = cfg->max - 1;
1631 strscpy(uinfo->value.enumerated.name,
1632 cfg->texts[uinfo->value.enumerated.item],
1633 sizeof(uinfo->value.enumerated.name));
1635 uinfo->count = cfg->size;
1636 uinfo->value.integer.min = 0;
1637 uinfo->value.integer.max = cfg->max;
1638 uinfo->type = (cfg->max == 1) ?
1639 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1640 SNDRV_CTL_ELEM_TYPE_INTEGER;
1646 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1647 struct snd_ctl_elem_value *uc)
1649 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1652 for (i = 0; i < cfg->size; i++)
1654 uc->value.enumerated.item[i] = cfg->val[i];
1656 uc->value.integer.value[i] = cfg->val[i];
1661 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1662 struct snd_ctl_elem_value *uc)
1664 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1667 if (!cfg->accept(cfg->io))
1670 for (i = 0; i < cfg->size; i++) {
1672 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1673 cfg->val[i] = uc->value.enumerated.item[i];
1675 change |= (uc->value.integer.value[i] != cfg->val[i]);
1676 cfg->val[i] = uc->value.integer.value[i];
1680 if (change && cfg->update)
1681 cfg->update(cfg->io, cfg->mod);
1686 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1691 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1693 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1694 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1695 struct device *dev = rsnd_priv_to_dev(priv);
1698 dev_warn(dev, "Can't update kctrl when idle\n");
1705 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1707 cfg->cfg.val = cfg->val;
1712 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1714 cfg->cfg.val = &cfg->val;
1719 const char * const volume_ramp_rate[] = {
1720 "128 dB/1 step", /* 00000 */
1721 "64 dB/1 step", /* 00001 */
1722 "32 dB/1 step", /* 00010 */
1723 "16 dB/1 step", /* 00011 */
1724 "8 dB/1 step", /* 00100 */
1725 "4 dB/1 step", /* 00101 */
1726 "2 dB/1 step", /* 00110 */
1727 "1 dB/1 step", /* 00111 */
1728 "0.5 dB/1 step", /* 01000 */
1729 "0.25 dB/1 step", /* 01001 */
1730 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1731 "0.125 dB/2 steps", /* 01011 */
1732 "0.125 dB/4 steps", /* 01100 */
1733 "0.125 dB/8 steps", /* 01101 */
1734 "0.125 dB/16 steps", /* 01110 */
1735 "0.125 dB/32 steps", /* 01111 */
1736 "0.125 dB/64 steps", /* 10000 */
1737 "0.125 dB/128 steps", /* 10001 */
1738 "0.125 dB/256 steps", /* 10010 */
1739 "0.125 dB/512 steps", /* 10011 */
1740 "0.125 dB/1024 steps", /* 10100 */
1741 "0.125 dB/2048 steps", /* 10101 */
1742 "0.125 dB/4096 steps", /* 10110 */
1743 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1746 int rsnd_kctrl_new(struct rsnd_mod *mod,
1747 struct rsnd_dai_stream *io,
1748 struct snd_soc_pcm_runtime *rtd,
1749 const unsigned char *name,
1750 int (*accept)(struct rsnd_dai_stream *io),
1751 void (*update)(struct rsnd_dai_stream *io,
1752 struct rsnd_mod *mod),
1753 struct rsnd_kctrl_cfg *cfg,
1754 const char * const *texts,
1758 struct snd_card *card = rtd->card->snd_card;
1759 struct snd_kcontrol *kctrl;
1760 struct snd_kcontrol_new knew = {
1761 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1763 .info = rsnd_kctrl_info,
1765 .get = rsnd_kctrl_get,
1766 .put = rsnd_kctrl_put,
1771 * 1) Avoid duplicate register for DVC with MIX case
1772 * 2) Allow duplicate register for MIX
1773 * 3) re-register if card was rebinded
1775 list_for_each_entry(kctrl, &card->controls, list) {
1776 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1782 if (size > RSND_MAX_CHANNELS)
1785 kctrl = snd_ctl_new1(&knew, cfg);
1789 ret = snd_ctl_add(card, kctrl);
1796 cfg->accept = accept;
1797 cfg->update = update;
1809 static const struct snd_soc_component_driver rsnd_soc_component = {
1811 .probe = rsnd_debugfs_probe,
1812 .hw_params = rsnd_hw_params,
1813 .hw_free = rsnd_hw_free,
1814 .pointer = rsnd_pointer,
1817 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1818 struct rsnd_dai_stream *io)
1822 ret = rsnd_dai_call(probe, io, priv);
1823 if (ret == -EAGAIN) {
1824 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1825 struct rsnd_mod *mod;
1829 * Fallback to PIO mode
1833 * call "remove" for SSI/SRC/DVC
1834 * SSI will be switch to PIO mode if it was DMA mode
1837 * rsnd_ssi_fallback()
1839 rsnd_dai_call(remove, io, priv);
1842 * remove all mod from io
1843 * and, re connect ssi
1845 for_each_rsnd_mod(i, mod, io)
1846 rsnd_dai_disconnect(mod, io, i);
1847 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1852 rsnd_dai_call(fallback, io, priv);
1856 * DAI has SSI which is PIO mode only now.
1858 ret = rsnd_dai_call(probe, io, priv);
1867 static int rsnd_probe(struct platform_device *pdev)
1869 struct rsnd_priv *priv;
1870 struct device *dev = &pdev->dev;
1871 struct rsnd_dai *rdai;
1872 int (*probe_func[])(struct rsnd_priv *priv) = {
1890 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1895 priv->flags = (unsigned long)of_device_get_match_data(dev);
1896 spin_lock_init(&priv->lock);
1901 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1902 ret = probe_func[i](priv);
1907 for_each_rsnd_dai(rdai, priv, i) {
1908 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1910 goto exit_snd_probe;
1912 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1914 goto exit_snd_probe;
1917 dev_set_drvdata(dev, priv);
1922 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1923 priv->daidrv, rsnd_rdai_nr(priv));
1925 dev_err(dev, "cannot snd dai register\n");
1926 goto exit_snd_probe;
1929 pm_runtime_enable(dev);
1931 dev_info(dev, "probed\n");
1935 for_each_rsnd_dai(rdai, priv, i) {
1936 rsnd_dai_call(remove, &rdai->playback, priv);
1937 rsnd_dai_call(remove, &rdai->capture, priv);
1941 * adg is very special mod which can't use rsnd_dai_call(remove),
1942 * and it registers ADG clock on probe.
1943 * It should be unregister if probe failed.
1944 * Mainly it is assuming -EPROBE_DEFER case
1946 rsnd_adg_remove(priv);
1951 static int rsnd_remove(struct platform_device *pdev)
1953 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1954 struct rsnd_dai *rdai;
1955 void (*remove_func[])(struct rsnd_priv *priv) = {
1967 pm_runtime_disable(&pdev->dev);
1969 for_each_rsnd_dai(rdai, priv, i) {
1970 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1971 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1974 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1975 remove_func[i](priv);
1980 static int __maybe_unused rsnd_suspend(struct device *dev)
1982 struct rsnd_priv *priv = dev_get_drvdata(dev);
1984 rsnd_adg_clk_disable(priv);
1989 static int __maybe_unused rsnd_resume(struct device *dev)
1991 struct rsnd_priv *priv = dev_get_drvdata(dev);
1993 rsnd_adg_clk_enable(priv);
1998 static const struct dev_pm_ops rsnd_pm_ops = {
1999 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2002 static struct platform_driver rsnd_driver = {
2004 .name = "rcar_sound",
2006 .of_match_table = rsnd_of_match,
2008 .probe = rsnd_probe,
2009 .remove = rsnd_remove,
2011 module_platform_driver(rsnd_driver);
2013 MODULE_LICENSE("GPL v2");
2014 MODULE_DESCRIPTION("Renesas R-Car audio driver");
2015 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2016 MODULE_ALIAS("platform:rcar-pcm-audio");