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()...
94 * you can enable below define if you don't need
95 * DAI status debug message when debugging
96 * see rsnd_dbg_dai_call()
98 * #define RSND_DEBUG_NO_DAI_CALL 1
101 #include <linux/pm_runtime.h>
104 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
105 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
107 static const struct of_device_id rsnd_of_match[] = {
108 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
109 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
110 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
113 MODULE_DEVICE_TABLE(of, rsnd_of_match);
118 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
120 if (mod->type != type) {
121 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
122 struct device *dev = rsnd_priv_to_dev(priv);
124 dev_warn(dev, "%s[%d] is not your expected module\n",
125 rsnd_mod_name(mod), rsnd_mod_id(mod));
129 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
130 struct rsnd_mod *mod)
132 if (!mod || !mod->ops || !mod->ops->dma_req)
135 return mod->ops->dma_req(io, mod);
138 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
139 struct rsnd_mod *mod,
140 enum rsnd_mod_type type)
145 int rsnd_mod_init(struct rsnd_priv *priv,
146 struct rsnd_mod *mod,
147 struct rsnd_mod_ops *ops,
149 u32* (*get_status)(struct rsnd_dai_stream *io,
150 struct rsnd_mod *mod,
151 enum rsnd_mod_type type),
152 enum rsnd_mod_type type,
155 int ret = clk_prepare(clk);
165 mod->get_status = get_status;
170 void rsnd_mod_quit(struct rsnd_mod *mod)
172 clk_unprepare(mod->clk);
176 void rsnd_mod_interrupt(struct rsnd_mod *mod,
177 void (*callback)(struct rsnd_mod *mod,
178 struct rsnd_dai_stream *io))
180 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
181 struct rsnd_dai_stream *io;
182 struct rsnd_dai *rdai;
185 for_each_rsnd_dai(rdai, priv, i) {
186 io = &rdai->playback;
187 if (mod == io->mod[mod->type])
191 if (mod == io->mod[mod->type])
196 int rsnd_io_is_working(struct rsnd_dai_stream *io)
198 /* see rsnd_dai_stream_init/quit() */
200 return snd_pcm_running(io->substream);
205 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
206 struct snd_pcm_hw_params *params)
208 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
211 * params will be added when refine
213 * __rsnd_soc_hw_rule_rate()
214 * __rsnd_soc_hw_rule_channels()
217 return params_channels(params);
219 return runtime->channels;
222 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
223 struct snd_pcm_hw_params *params)
225 int chan = rsnd_runtime_channel_original_with_params(io, params);
226 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
229 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
232 return converted_chan;
238 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
239 struct snd_pcm_hw_params *params)
241 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
242 int chan = rsnd_io_is_play(io) ?
243 rsnd_runtime_channel_after_ctu_with_params(io, params) :
244 rsnd_runtime_channel_original_with_params(io, params);
247 if (rsnd_runtime_is_ssi_multi(io))
248 chan /= rsnd_rdai_ssi_lane_get(rdai);
250 /* TDM Extend Mode needs 8ch */
257 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
259 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
260 int lane = rsnd_rdai_ssi_lane_get(rdai);
261 int chan = rsnd_io_is_play(io) ?
262 rsnd_runtime_channel_after_ctu(io) :
263 rsnd_runtime_channel_original(io);
265 return (chan > 2) && (lane > 1);
268 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
270 return rsnd_runtime_channel_for_ssi(io) >= 6;
276 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
278 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
279 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
280 struct device *dev = rsnd_priv_to_dev(priv);
282 switch (snd_pcm_format_width(runtime->format)) {
289 dev_warn(dev, "not supported sample bits\n");
297 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
299 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
300 struct rsnd_mod *target;
301 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
304 * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
308 * We need to care about inversion timing to control
309 * Playback/Capture correctly.
310 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
312 * sL/R : software L/R
313 * hL/R : hardware L/R
314 * (*) : conversion timing
317 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
318 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
321 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
322 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
324 if (rsnd_io_is_play(io)) {
325 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
327 target = src ? src : ssiu;
329 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
331 target = cmd ? cmd : ssiu;
334 /* Non target mod or 24bit data needs normal DALIGN */
335 if ((snd_pcm_format_width(runtime->format) != 16) ||
338 /* Target mod needs inverted DALIGN when 16bit */
343 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
345 enum rsnd_mod_type playback_mods[] = {
350 enum rsnd_mod_type capture_mods[] = {
355 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
356 struct rsnd_mod *tmod = NULL;
357 enum rsnd_mod_type *mods =
358 rsnd_io_is_play(io) ?
359 playback_mods : capture_mods;
363 * This is needed for 24bit data
364 * We need to shift 8bit
366 * Linux 24bit data is located as 0x00******
367 * HW 24bit data is located as 0x******00
370 if (snd_pcm_format_width(runtime->format) == 16)
373 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
374 tmod = rsnd_io_to_mod(io, mods[i]);
382 if (rsnd_io_is_play(io))
383 return (0 << 20) | /* shift to Left */
384 (8 << 16); /* 8bit */
386 return (1 << 20) | /* shift to Right */
387 (8 << 16); /* 8bit */
393 struct rsnd_mod *rsnd_mod_next(int *iterator,
394 struct rsnd_dai_stream *io,
395 enum rsnd_mod_type *array,
398 struct rsnd_mod *mod;
399 enum rsnd_mod_type type;
400 int max = array ? array_size : RSND_MOD_MAX;
402 for (; *iterator < max; (*iterator)++) {
403 type = (array) ? array[*iterator] : *iterator;
404 mod = rsnd_io_to_mod(io, type);
412 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
446 static int rsnd_status_update(u32 *status,
447 int shift, int add, int timing)
449 u32 mask = 0xF << shift;
450 u8 val = (*status >> shift) & 0xF;
451 u8 next_val = (val + add) & 0xF;
452 int func_call = (val == timing);
454 if (next_val == 0xF) /* underflow case */
457 *status = (*status & ~mask) + (next_val << shift);
462 #define rsnd_dai_call(fn, io, param...) \
464 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
465 struct rsnd_mod *mod; \
466 int is_play = rsnd_io_is_play(io); \
468 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
469 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
471 u32 *status = mod->get_status(io, mod, types[i]); \
472 int func_call = rsnd_status_update(status, \
473 __rsnd_mod_shift_##fn, \
474 __rsnd_mod_add_##fn, \
475 __rsnd_mod_call_##fn); \
476 rsnd_dbg_dai_call(dev, "%s[%d]\t0x%08x %s\n", \
477 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
478 (func_call && (mod)->ops->fn) ? #fn : ""); \
479 if (func_call && (mod)->ops->fn) \
480 tmp = (mod)->ops->fn(mod, io, param); \
482 dev_err(dev, "%s[%d] : %s error %d\n", \
483 rsnd_mod_name(mod), rsnd_mod_id(mod), \
490 int rsnd_dai_connect(struct rsnd_mod *mod,
491 struct rsnd_dai_stream *io,
492 enum rsnd_mod_type type)
494 struct rsnd_priv *priv;
500 if (io->mod[type] == mod)
506 priv = rsnd_mod_to_priv(mod);
507 dev = rsnd_priv_to_dev(priv);
511 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
512 rsnd_mod_name(mod), rsnd_mod_id(mod),
513 rsnd_io_is_play(io) ? "Playback" : "Capture");
518 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
519 struct rsnd_dai_stream *io,
520 enum rsnd_mod_type type)
522 io->mod[type] = NULL;
525 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
528 if (max_channels > 0)
529 rdai->max_channels = max_channels;
531 return rdai->max_channels;
534 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
538 rdai->ssi_lane = ssi_lane;
540 return rdai->ssi_lane;
543 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
545 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
548 return priv->rdai + id;
551 static struct snd_soc_dai_driver
552 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
554 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
557 return priv->daidrv + id;
560 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
561 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
563 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
565 return rsnd_rdai_get(priv, dai->id);
569 * rsnd_soc_dai functions
571 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
573 struct snd_pcm_substream *substream = io->substream;
576 * this function should be called...
578 * - if rsnd_dai_pointer_update() returns true
579 * - without spin lock
582 snd_pcm_period_elapsed(substream);
585 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
586 struct snd_pcm_substream *substream)
588 io->substream = substream;
591 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
593 io->substream = NULL;
597 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
599 struct snd_soc_pcm_runtime *rtd = substream->private_data;
605 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
606 struct snd_pcm_substream *substream)
608 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
609 return &rdai->playback;
611 return &rdai->capture;
614 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
615 struct snd_soc_dai *dai)
617 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
618 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
619 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
623 spin_lock_irqsave(&priv->lock, flags);
626 case SNDRV_PCM_TRIGGER_START:
627 case SNDRV_PCM_TRIGGER_RESUME:
628 ret = rsnd_dai_call(init, io, priv);
630 goto dai_trigger_end;
632 ret = rsnd_dai_call(start, io, priv);
634 goto dai_trigger_end;
636 ret = rsnd_dai_call(irq, io, priv, 1);
638 goto dai_trigger_end;
641 case SNDRV_PCM_TRIGGER_STOP:
642 case SNDRV_PCM_TRIGGER_SUSPEND:
643 ret = rsnd_dai_call(irq, io, priv, 0);
645 ret |= rsnd_dai_call(stop, io, priv);
647 ret |= rsnd_dai_call(quit, io, priv);
655 spin_unlock_irqrestore(&priv->lock, flags);
660 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
662 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
664 /* set master/slave audio interface */
665 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
666 case SND_SOC_DAIFMT_CBM_CFM:
667 rdai->clk_master = 0;
669 case SND_SOC_DAIFMT_CBS_CFS:
670 rdai->clk_master = 1; /* codec is slave, cpu is master */
677 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
678 case SND_SOC_DAIFMT_I2S:
680 rdai->data_alignment = 0;
681 rdai->frm_clk_inv = 0;
683 case SND_SOC_DAIFMT_LEFT_J:
685 rdai->data_alignment = 0;
686 rdai->frm_clk_inv = 1;
688 case SND_SOC_DAIFMT_RIGHT_J:
690 rdai->data_alignment = 1;
691 rdai->frm_clk_inv = 1;
695 /* set clock inversion */
696 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
697 case SND_SOC_DAIFMT_NB_IF:
698 rdai->frm_clk_inv = !rdai->frm_clk_inv;
700 case SND_SOC_DAIFMT_IB_NF:
701 rdai->bit_clk_inv = !rdai->bit_clk_inv;
703 case SND_SOC_DAIFMT_IB_IF:
704 rdai->bit_clk_inv = !rdai->bit_clk_inv;
705 rdai->frm_clk_inv = !rdai->frm_clk_inv;
707 case SND_SOC_DAIFMT_NB_NF:
715 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
716 u32 tx_mask, u32 rx_mask,
717 int slots, int slot_width)
719 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
720 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
721 struct device *dev = rsnd_priv_to_dev(priv);
727 /* TDM Extend Mode */
728 rsnd_rdai_channels_set(rdai, slots);
729 rsnd_rdai_ssi_lane_set(rdai, 1);
732 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
739 static unsigned int rsnd_soc_hw_channels_list[] = {
743 static unsigned int rsnd_soc_hw_rate_list[] = {
758 static int rsnd_soc_hw_rule(struct rsnd_priv *priv,
759 unsigned int *list, int list_num,
760 struct snd_interval *baseline, struct snd_interval *iv)
762 struct snd_interval p;
766 snd_interval_any(&p);
770 for (i = 0; i < list_num; i++) {
772 if (!snd_interval_test(iv, list[i]))
775 rate = rsnd_ssi_clk_query(priv,
776 baseline->min, list[i], NULL);
778 p.min = min(p.min, list[i]);
779 p.max = max(p.max, list[i]);
782 rate = rsnd_ssi_clk_query(priv,
783 baseline->max, list[i], NULL);
785 p.min = min(p.min, list[i]);
786 p.max = max(p.max, list[i]);
790 return snd_interval_refine(iv, &p);
793 static int __rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
794 struct snd_pcm_hw_rule *rule,
797 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
798 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
799 struct snd_interval ic;
800 struct snd_soc_dai *dai = rule->private;
801 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
802 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
803 struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
806 * possible sampling rate limitation is same as
807 * 2ch if it supports multi ssi
808 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
812 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
814 return rsnd_soc_hw_rule(priv, rsnd_soc_hw_rate_list,
815 ARRAY_SIZE(rsnd_soc_hw_rate_list),
819 static int rsnd_soc_hw_rule_rate_playback(struct snd_pcm_hw_params *params,
820 struct snd_pcm_hw_rule *rule)
822 return __rsnd_soc_hw_rule_rate(params, rule, 1);
825 static int rsnd_soc_hw_rule_rate_capture(struct snd_pcm_hw_params *params,
826 struct snd_pcm_hw_rule *rule)
828 return __rsnd_soc_hw_rule_rate(params, rule, 0);
831 static int __rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
832 struct snd_pcm_hw_rule *rule,
835 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
836 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
837 struct snd_interval ic;
838 struct snd_soc_dai *dai = rule->private;
839 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
840 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
841 struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
844 * possible sampling rate limitation is same as
845 * 2ch if it supports multi ssi
846 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
850 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
852 return rsnd_soc_hw_rule(priv, rsnd_soc_hw_channels_list,
853 ARRAY_SIZE(rsnd_soc_hw_channels_list),
857 static int rsnd_soc_hw_rule_channels_playback(struct snd_pcm_hw_params *params,
858 struct snd_pcm_hw_rule *rule)
860 return __rsnd_soc_hw_rule_channels(params, rule, 1);
863 static int rsnd_soc_hw_rule_channels_capture(struct snd_pcm_hw_params *params,
864 struct snd_pcm_hw_rule *rule)
866 return __rsnd_soc_hw_rule_channels(params, rule, 0);
869 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
870 .info = SNDRV_PCM_INFO_INTERLEAVED |
871 SNDRV_PCM_INFO_MMAP |
872 SNDRV_PCM_INFO_MMAP_VALID,
873 .buffer_bytes_max = 64 * 1024,
874 .period_bytes_min = 32,
875 .period_bytes_max = 8192,
881 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
882 struct snd_soc_dai *dai)
884 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
885 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
886 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
887 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
888 struct snd_pcm_runtime *runtime = substream->runtime;
889 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
893 rsnd_dai_stream_init(io, substream);
897 * It depends on Platform design
899 constraint->list = rsnd_soc_hw_channels_list;
900 constraint->count = 0;
901 constraint->mask = 0;
903 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
904 if (rsnd_soc_hw_channels_list[i] > max_channels)
906 constraint->count = i + 1;
909 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
911 snd_pcm_hw_constraint_list(runtime, 0,
912 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
914 snd_pcm_hw_constraint_integer(runtime,
915 SNDRV_PCM_HW_PARAM_PERIODS);
918 * Sampling Rate / Channel Limitation
919 * It depends on Clock Master Mode
921 if (rsnd_rdai_is_clk_master(rdai)) {
922 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
924 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
925 is_play ? rsnd_soc_hw_rule_rate_playback :
926 rsnd_soc_hw_rule_rate_capture,
928 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
929 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
930 is_play ? rsnd_soc_hw_rule_channels_playback :
931 rsnd_soc_hw_rule_channels_capture,
933 SNDRV_PCM_HW_PARAM_RATE, -1);
937 * call rsnd_dai_call without spinlock
939 ret = rsnd_dai_call(nolock_start, io, priv);
941 rsnd_dai_call(nolock_stop, io, priv);
946 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
947 struct snd_soc_dai *dai)
949 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
950 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
951 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
954 * call rsnd_dai_call without spinlock
956 rsnd_dai_call(nolock_stop, io, priv);
958 rsnd_dai_stream_quit(io);
961 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
962 .startup = rsnd_soc_dai_startup,
963 .shutdown = rsnd_soc_dai_shutdown,
964 .trigger = rsnd_soc_dai_trigger,
965 .set_fmt = rsnd_soc_dai_set_fmt,
966 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
969 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
970 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
971 struct device_node *node,
972 struct device_node *playback,
973 struct device_node *capture)
975 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
976 struct device_node *np;
977 struct rsnd_mod *mod;
984 for_each_child_of_node(node, np) {
985 mod = mod_get(priv, i);
987 rsnd_dai_connect(mod, &rdai->playback, mod->type);
989 rsnd_dai_connect(mod, &rdai->capture, mod->type);
996 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
999 struct device *dev = rsnd_priv_to_dev(priv);
1000 struct device_node *np = dev->of_node;
1001 struct device_node *dai_node;
1002 struct device_node *ret;
1007 * parse both previous dai (= rcar_sound,dai), and
1008 * graph dai (= ports/port)
1010 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1013 goto of_node_compatible;
1018 dai_node = of_graph_get_next_endpoint(np, NULL);
1027 of_node_put(dai_node);
1032 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1033 struct device_node *dai_np,
1036 struct device_node *playback, *capture;
1037 struct rsnd_dai_stream *io_playback;
1038 struct rsnd_dai_stream *io_capture;
1039 struct snd_soc_dai_driver *drv;
1040 struct rsnd_dai *rdai;
1041 struct device *dev = rsnd_priv_to_dev(priv);
1044 rdai = rsnd_rdai_get(priv, dai_i);
1045 drv = rsnd_daidrv_get(priv, dai_i);
1046 io_playback = &rdai->playback;
1047 io_capture = &rdai->capture;
1049 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1052 drv->name = rdai->name;
1053 drv->ops = &rsnd_soc_dai_ops;
1055 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1056 "DAI%d Playback", dai_i);
1057 drv->playback.rates = RSND_RATES;
1058 drv->playback.formats = RSND_FMTS;
1059 drv->playback.channels_min = 2;
1060 drv->playback.channels_max = 8;
1061 drv->playback.stream_name = rdai->playback.name;
1063 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1064 "DAI%d Capture", dai_i);
1065 drv->capture.rates = RSND_RATES;
1066 drv->capture.formats = RSND_FMTS;
1067 drv->capture.channels_min = 2;
1068 drv->capture.channels_max = 8;
1069 drv->capture.stream_name = rdai->capture.name;
1071 rdai->playback.rdai = rdai;
1072 rdai->capture.rdai = rdai;
1073 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1074 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1076 for (io_i = 0;; io_i++) {
1077 playback = of_parse_phandle(dai_np, "playback", io_i);
1078 capture = of_parse_phandle(dai_np, "capture", io_i);
1080 if (!playback && !capture)
1083 rsnd_parse_connect_ssi(rdai, playback, capture);
1084 rsnd_parse_connect_src(rdai, playback, capture);
1085 rsnd_parse_connect_ctu(rdai, playback, capture);
1086 rsnd_parse_connect_mix(rdai, playback, capture);
1087 rsnd_parse_connect_dvc(rdai, playback, capture);
1089 of_node_put(playback);
1090 of_node_put(capture);
1093 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1094 rsnd_ssi_is_pin_sharing(io_playback)) {
1095 /* should have symmetric_rates if pin sharing */
1096 drv->symmetric_rates = 1;
1099 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1100 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1101 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1104 static int rsnd_dai_probe(struct rsnd_priv *priv)
1106 struct device_node *dai_node;
1107 struct device_node *dai_np;
1108 struct snd_soc_dai_driver *rdrv;
1109 struct device *dev = rsnd_priv_to_dev(priv);
1110 struct rsnd_dai *rdai;
1115 dai_node = rsnd_dai_of_node(priv, &is_graph);
1117 nr = of_graph_get_endpoint_count(dai_node);
1119 nr = of_get_child_count(dai_node);
1124 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1125 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1130 priv->daidrv = rdrv;
1138 for_each_endpoint_of_node(dai_node, dai_np) {
1139 __rsnd_dai_probe(priv, dai_np, dai_i);
1140 rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
1144 for_each_child_of_node(dai_node, dai_np)
1145 __rsnd_dai_probe(priv, dai_np, dai_i++);
1154 static int rsnd_hw_params(struct snd_pcm_substream *substream,
1155 struct snd_pcm_hw_params *hw_params)
1157 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1158 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1159 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1162 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1166 return snd_pcm_lib_malloc_pages(substream,
1167 params_buffer_bytes(hw_params));
1170 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
1172 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1173 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1174 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1175 snd_pcm_uframes_t pointer = 0;
1177 rsnd_dai_call(pointer, io, &pointer);
1182 static const struct snd_pcm_ops rsnd_pcm_ops = {
1183 .ioctl = snd_pcm_lib_ioctl,
1184 .hw_params = rsnd_hw_params,
1185 .hw_free = snd_pcm_lib_free_pages,
1186 .pointer = rsnd_pointer,
1192 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1193 struct snd_ctl_elem_info *uinfo)
1195 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1198 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1199 uinfo->count = cfg->size;
1200 uinfo->value.enumerated.items = cfg->max;
1201 if (uinfo->value.enumerated.item >= cfg->max)
1202 uinfo->value.enumerated.item = cfg->max - 1;
1203 strlcpy(uinfo->value.enumerated.name,
1204 cfg->texts[uinfo->value.enumerated.item],
1205 sizeof(uinfo->value.enumerated.name));
1207 uinfo->count = cfg->size;
1208 uinfo->value.integer.min = 0;
1209 uinfo->value.integer.max = cfg->max;
1210 uinfo->type = (cfg->max == 1) ?
1211 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1212 SNDRV_CTL_ELEM_TYPE_INTEGER;
1218 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1219 struct snd_ctl_elem_value *uc)
1221 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1224 for (i = 0; i < cfg->size; i++)
1226 uc->value.enumerated.item[i] = cfg->val[i];
1228 uc->value.integer.value[i] = cfg->val[i];
1233 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1234 struct snd_ctl_elem_value *uc)
1236 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1239 if (!cfg->accept(cfg->io))
1242 for (i = 0; i < cfg->size; i++) {
1244 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1245 cfg->val[i] = uc->value.enumerated.item[i];
1247 change |= (uc->value.integer.value[i] != cfg->val[i]);
1248 cfg->val[i] = uc->value.integer.value[i];
1252 if (change && cfg->update)
1253 cfg->update(cfg->io, cfg->mod);
1258 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1263 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1265 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1270 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1272 cfg->cfg.val = cfg->val;
1277 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1279 cfg->cfg.val = &cfg->val;
1284 const char * const volume_ramp_rate[] = {
1285 "128 dB/1 step", /* 00000 */
1286 "64 dB/1 step", /* 00001 */
1287 "32 dB/1 step", /* 00010 */
1288 "16 dB/1 step", /* 00011 */
1289 "8 dB/1 step", /* 00100 */
1290 "4 dB/1 step", /* 00101 */
1291 "2 dB/1 step", /* 00110 */
1292 "1 dB/1 step", /* 00111 */
1293 "0.5 dB/1 step", /* 01000 */
1294 "0.25 dB/1 step", /* 01001 */
1295 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1296 "0.125 dB/2 steps", /* 01011 */
1297 "0.125 dB/4 steps", /* 01100 */
1298 "0.125 dB/8 steps", /* 01101 */
1299 "0.125 dB/16 steps", /* 01110 */
1300 "0.125 dB/32 steps", /* 01111 */
1301 "0.125 dB/64 steps", /* 10000 */
1302 "0.125 dB/128 steps", /* 10001 */
1303 "0.125 dB/256 steps", /* 10010 */
1304 "0.125 dB/512 steps", /* 10011 */
1305 "0.125 dB/1024 steps", /* 10100 */
1306 "0.125 dB/2048 steps", /* 10101 */
1307 "0.125 dB/4096 steps", /* 10110 */
1308 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1311 int rsnd_kctrl_new(struct rsnd_mod *mod,
1312 struct rsnd_dai_stream *io,
1313 struct snd_soc_pcm_runtime *rtd,
1314 const unsigned char *name,
1315 int (*accept)(struct rsnd_dai_stream *io),
1316 void (*update)(struct rsnd_dai_stream *io,
1317 struct rsnd_mod *mod),
1318 struct rsnd_kctrl_cfg *cfg,
1319 const char * const *texts,
1323 struct snd_card *card = rtd->card->snd_card;
1324 struct snd_kcontrol *kctrl;
1325 struct snd_kcontrol_new knew = {
1326 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1328 .info = rsnd_kctrl_info,
1330 .get = rsnd_kctrl_get,
1331 .put = rsnd_kctrl_put,
1335 if (size > RSND_MAX_CHANNELS)
1338 kctrl = snd_ctl_new1(&knew, cfg);
1342 ret = snd_ctl_add(card, kctrl);
1349 cfg->accept = accept;
1350 cfg->update = update;
1363 #define PREALLOC_BUFFER (32 * 1024)
1364 #define PREALLOC_BUFFER_MAX (32 * 1024)
1366 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1367 struct rsnd_dai_stream *io,
1370 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1371 struct device *dev = rsnd_priv_to_dev(priv);
1372 struct snd_pcm_substream *substream;
1376 * use Audio-DMAC dev if we can use IPMMU
1378 * rsnd_dmaen_attach()
1383 for (substream = rtd->pcm->streams[stream].substream;
1385 substream = substream->next) {
1386 err = snd_pcm_lib_preallocate_pages(substream,
1389 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1397 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1399 struct snd_soc_dai *dai = rtd->cpu_dai;
1400 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1403 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1407 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1411 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1412 SNDRV_PCM_STREAM_PLAYBACK);
1416 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1417 SNDRV_PCM_STREAM_CAPTURE);
1424 static const struct snd_soc_component_driver rsnd_soc_component = {
1425 .ops = &rsnd_pcm_ops,
1426 .pcm_new = rsnd_pcm_new,
1430 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1431 struct rsnd_dai_stream *io)
1435 ret = rsnd_dai_call(probe, io, priv);
1436 if (ret == -EAGAIN) {
1437 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1438 struct rsnd_mod *mod;
1442 * Fallback to PIO mode
1446 * call "remove" for SSI/SRC/DVC
1447 * SSI will be switch to PIO mode if it was DMA mode
1450 * rsnd_ssi_fallback()
1452 rsnd_dai_call(remove, io, priv);
1455 * remove all mod from io
1456 * and, re connect ssi
1458 for_each_rsnd_mod(i, mod, io)
1459 rsnd_dai_disconnect(mod, io, i);
1460 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1465 rsnd_dai_call(fallback, io, priv);
1469 * DAI has SSI which is PIO mode only now.
1471 ret = rsnd_dai_call(probe, io, priv);
1480 static int rsnd_probe(struct platform_device *pdev)
1482 struct rsnd_priv *priv;
1483 struct device *dev = &pdev->dev;
1484 struct rsnd_dai *rdai;
1485 int (*probe_func[])(struct rsnd_priv *priv) = {
1503 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1508 priv->flags = (unsigned long)of_device_get_match_data(dev);
1509 spin_lock_init(&priv->lock);
1514 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1515 ret = probe_func[i](priv);
1520 for_each_rsnd_dai(rdai, priv, i) {
1521 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1523 goto exit_snd_probe;
1525 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1527 goto exit_snd_probe;
1530 dev_set_drvdata(dev, priv);
1535 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1536 priv->daidrv, rsnd_rdai_nr(priv));
1538 dev_err(dev, "cannot snd dai register\n");
1539 goto exit_snd_probe;
1542 pm_runtime_enable(dev);
1544 dev_info(dev, "probed\n");
1548 for_each_rsnd_dai(rdai, priv, i) {
1549 rsnd_dai_call(remove, &rdai->playback, priv);
1550 rsnd_dai_call(remove, &rdai->capture, priv);
1556 static int rsnd_remove(struct platform_device *pdev)
1558 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1559 struct rsnd_dai *rdai;
1560 void (*remove_func[])(struct rsnd_priv *priv) = {
1572 snd_soc_disconnect_sync(&pdev->dev);
1574 pm_runtime_disable(&pdev->dev);
1576 for_each_rsnd_dai(rdai, priv, i) {
1577 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1578 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1581 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1582 remove_func[i](priv);
1587 static int __maybe_unused rsnd_suspend(struct device *dev)
1589 struct rsnd_priv *priv = dev_get_drvdata(dev);
1591 rsnd_adg_clk_disable(priv);
1596 static int __maybe_unused rsnd_resume(struct device *dev)
1598 struct rsnd_priv *priv = dev_get_drvdata(dev);
1600 rsnd_adg_clk_enable(priv);
1605 static const struct dev_pm_ops rsnd_pm_ops = {
1606 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1609 static struct platform_driver rsnd_driver = {
1611 .name = "rcar_sound",
1613 .of_match_table = rsnd_of_match,
1615 .probe = rsnd_probe,
1616 .remove = rsnd_remove,
1618 module_platform_driver(rsnd_driver);
1620 MODULE_LICENSE("GPL v2");
1621 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1622 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1623 MODULE_ALIAS("platform:rcar-pcm-audio");