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_S8 |\
106 SNDRV_PCM_FMTBIT_S16_LE |\
107 SNDRV_PCM_FMTBIT_S24_LE)
109 static const struct of_device_id rsnd_of_match[] = {
110 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
111 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
112 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
115 MODULE_DEVICE_TABLE(of, rsnd_of_match);
120 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
122 if (mod->type != type) {
123 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
124 struct device *dev = rsnd_priv_to_dev(priv);
126 dev_warn(dev, "%s[%d] is not your expected module\n",
127 rsnd_mod_name(mod), rsnd_mod_id(mod));
131 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
132 struct rsnd_mod *mod)
134 if (!mod || !mod->ops || !mod->ops->dma_req)
137 return mod->ops->dma_req(io, mod);
140 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
141 struct rsnd_mod *mod,
142 enum rsnd_mod_type type)
147 int rsnd_mod_init(struct rsnd_priv *priv,
148 struct rsnd_mod *mod,
149 struct rsnd_mod_ops *ops,
151 u32* (*get_status)(struct rsnd_dai_stream *io,
152 struct rsnd_mod *mod,
153 enum rsnd_mod_type type),
154 enum rsnd_mod_type type,
157 int ret = clk_prepare(clk);
167 mod->get_status = get_status;
172 void rsnd_mod_quit(struct rsnd_mod *mod)
174 clk_unprepare(mod->clk);
178 void rsnd_mod_interrupt(struct rsnd_mod *mod,
179 void (*callback)(struct rsnd_mod *mod,
180 struct rsnd_dai_stream *io))
182 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
183 struct rsnd_dai_stream *io;
184 struct rsnd_dai *rdai;
187 for_each_rsnd_dai(rdai, priv, i) {
188 io = &rdai->playback;
189 if (mod == io->mod[mod->type])
193 if (mod == io->mod[mod->type])
198 int rsnd_io_is_working(struct rsnd_dai_stream *io)
200 /* see rsnd_dai_stream_init/quit() */
202 return snd_pcm_running(io->substream);
207 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
208 struct snd_pcm_hw_params *params)
210 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
213 * params will be added when refine
215 * __rsnd_soc_hw_rule_rate()
216 * __rsnd_soc_hw_rule_channels()
219 return params_channels(params);
221 return runtime->channels;
224 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
225 struct snd_pcm_hw_params *params)
227 int chan = rsnd_runtime_channel_original_with_params(io, params);
228 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
231 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
234 return converted_chan;
240 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
241 struct snd_pcm_hw_params *params)
243 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
244 int chan = rsnd_io_is_play(io) ?
245 rsnd_runtime_channel_after_ctu_with_params(io, params) :
246 rsnd_runtime_channel_original_with_params(io, params);
249 if (rsnd_runtime_is_ssi_multi(io))
250 chan /= rsnd_rdai_ssi_lane_get(rdai);
252 /* TDM Extend Mode needs 8ch */
259 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
261 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
262 int lane = rsnd_rdai_ssi_lane_get(rdai);
263 int chan = rsnd_io_is_play(io) ?
264 rsnd_runtime_channel_after_ctu(io) :
265 rsnd_runtime_channel_original(io);
267 return (chan > 2) && (lane > 1);
270 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
272 return rsnd_runtime_channel_for_ssi(io) >= 6;
278 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
280 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
281 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
282 struct device *dev = rsnd_priv_to_dev(priv);
284 switch (snd_pcm_format_width(runtime->format)) {
293 dev_warn(dev, "not supported sample bits\n");
301 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
303 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
304 struct rsnd_mod *target;
305 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
308 * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
312 * We need to care about inversion timing to control
313 * Playback/Capture correctly.
314 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
316 * sL/R : software L/R
317 * hL/R : hardware L/R
318 * (*) : conversion timing
321 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
322 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
325 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
326 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
328 if (rsnd_io_is_play(io)) {
329 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
331 target = src ? src : ssiu;
333 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
335 target = cmd ? cmd : ssiu;
338 /* Non target mod or non 16bit needs normal DALIGN */
339 if ((snd_pcm_format_width(runtime->format) != 16) ||
342 /* Target mod needs inverted DALIGN when 16bit */
347 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
349 enum rsnd_mod_type playback_mods[] = {
354 enum rsnd_mod_type capture_mods[] = {
359 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
360 struct rsnd_mod *tmod = NULL;
361 enum rsnd_mod_type *mods =
362 rsnd_io_is_play(io) ?
363 playback_mods : capture_mods;
367 * This is needed for 24bit data
368 * We need to shift 8bit
370 * Linux 24bit data is located as 0x00******
371 * HW 24bit data is located as 0x******00
374 if (snd_pcm_format_width(runtime->format) != 24)
377 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
378 tmod = rsnd_io_to_mod(io, mods[i]);
386 if (rsnd_io_is_play(io))
387 return (0 << 20) | /* shift to Left */
388 (8 << 16); /* 8bit */
390 return (1 << 20) | /* shift to Right */
391 (8 << 16); /* 8bit */
397 struct rsnd_mod *rsnd_mod_next(int *iterator,
398 struct rsnd_dai_stream *io,
399 enum rsnd_mod_type *array,
402 struct rsnd_mod *mod;
403 enum rsnd_mod_type type;
404 int max = array ? array_size : RSND_MOD_MAX;
406 for (; *iterator < max; (*iterator)++) {
407 type = (array) ? array[*iterator] : *iterator;
408 mod = rsnd_io_to_mod(io, type);
416 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
450 static int rsnd_status_update(u32 *status,
451 int shift, int add, int timing)
453 u32 mask = 0xF << shift;
454 u8 val = (*status >> shift) & 0xF;
455 u8 next_val = (val + add) & 0xF;
456 int func_call = (val == timing);
458 if (next_val == 0xF) /* underflow case */
461 *status = (*status & ~mask) + (next_val << shift);
466 #define rsnd_dai_call(fn, io, param...) \
468 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
469 struct rsnd_mod *mod; \
470 int is_play = rsnd_io_is_play(io); \
472 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
473 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
475 u32 *status = mod->get_status(io, mod, types[i]); \
476 int func_call = rsnd_status_update(status, \
477 __rsnd_mod_shift_##fn, \
478 __rsnd_mod_add_##fn, \
479 __rsnd_mod_call_##fn); \
480 rsnd_dbg_dai_call(dev, "%s[%d]\t0x%08x %s\n", \
481 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
482 (func_call && (mod)->ops->fn) ? #fn : ""); \
483 if (func_call && (mod)->ops->fn) \
484 tmp = (mod)->ops->fn(mod, io, param); \
486 dev_err(dev, "%s[%d] : %s error %d\n", \
487 rsnd_mod_name(mod), rsnd_mod_id(mod), \
494 int rsnd_dai_connect(struct rsnd_mod *mod,
495 struct rsnd_dai_stream *io,
496 enum rsnd_mod_type type)
498 struct rsnd_priv *priv;
504 if (io->mod[type] == mod)
510 priv = rsnd_mod_to_priv(mod);
511 dev = rsnd_priv_to_dev(priv);
515 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
516 rsnd_mod_name(mod), rsnd_mod_id(mod),
517 rsnd_io_is_play(io) ? "Playback" : "Capture");
522 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
523 struct rsnd_dai_stream *io,
524 enum rsnd_mod_type type)
526 io->mod[type] = NULL;
529 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
532 if (max_channels > 0)
533 rdai->max_channels = max_channels;
535 return rdai->max_channels;
538 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
542 rdai->ssi_lane = ssi_lane;
544 return rdai->ssi_lane;
547 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
550 rdai->chan_width = width;
552 return rdai->chan_width;
555 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
557 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
560 return priv->rdai + id;
563 static struct snd_soc_dai_driver
564 *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
566 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
569 return priv->daidrv + id;
572 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
573 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
575 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
577 return rsnd_rdai_get(priv, dai->id);
581 * rsnd_soc_dai functions
583 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
585 struct snd_pcm_substream *substream = io->substream;
588 * this function should be called...
590 * - if rsnd_dai_pointer_update() returns true
591 * - without spin lock
594 snd_pcm_period_elapsed(substream);
597 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
598 struct snd_pcm_substream *substream)
600 io->substream = substream;
603 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
605 io->substream = NULL;
609 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
611 struct snd_soc_pcm_runtime *rtd = substream->private_data;
617 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
618 struct snd_pcm_substream *substream)
620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
621 return &rdai->playback;
623 return &rdai->capture;
626 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
627 struct snd_soc_dai *dai)
629 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
630 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
631 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
635 spin_lock_irqsave(&priv->lock, flags);
638 case SNDRV_PCM_TRIGGER_START:
639 case SNDRV_PCM_TRIGGER_RESUME:
640 ret = rsnd_dai_call(init, io, priv);
642 goto dai_trigger_end;
644 ret = rsnd_dai_call(start, io, priv);
646 goto dai_trigger_end;
648 ret = rsnd_dai_call(irq, io, priv, 1);
650 goto dai_trigger_end;
653 case SNDRV_PCM_TRIGGER_STOP:
654 case SNDRV_PCM_TRIGGER_SUSPEND:
655 ret = rsnd_dai_call(irq, io, priv, 0);
657 ret |= rsnd_dai_call(stop, io, priv);
659 ret |= rsnd_dai_call(quit, io, priv);
667 spin_unlock_irqrestore(&priv->lock, flags);
672 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
674 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
676 /* set master/slave audio interface */
677 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
678 case SND_SOC_DAIFMT_CBM_CFM:
679 rdai->clk_master = 0;
681 case SND_SOC_DAIFMT_CBS_CFS:
682 rdai->clk_master = 1; /* codec is slave, cpu is master */
689 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
690 case SND_SOC_DAIFMT_I2S:
692 rdai->data_alignment = 0;
693 rdai->frm_clk_inv = 0;
695 case SND_SOC_DAIFMT_LEFT_J:
696 case SND_SOC_DAIFMT_DSP_B:
698 rdai->data_alignment = 0;
699 rdai->frm_clk_inv = 1;
701 case SND_SOC_DAIFMT_RIGHT_J:
703 rdai->data_alignment = 1;
704 rdai->frm_clk_inv = 1;
706 case SND_SOC_DAIFMT_DSP_A:
708 rdai->data_alignment = 0;
709 rdai->frm_clk_inv = 1;
713 /* set clock inversion */
714 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
715 case SND_SOC_DAIFMT_NB_IF:
716 rdai->frm_clk_inv = !rdai->frm_clk_inv;
718 case SND_SOC_DAIFMT_IB_NF:
719 rdai->bit_clk_inv = !rdai->bit_clk_inv;
721 case SND_SOC_DAIFMT_IB_IF:
722 rdai->bit_clk_inv = !rdai->bit_clk_inv;
723 rdai->frm_clk_inv = !rdai->frm_clk_inv;
725 case SND_SOC_DAIFMT_NB_NF:
733 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
734 u32 tx_mask, u32 rx_mask,
735 int slots, int slot_width)
737 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
738 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
739 struct device *dev = rsnd_priv_to_dev(priv);
741 switch (slot_width) {
747 dev_err(dev, "unsupported slot width value: %d\n", slot_width);
755 /* TDM Extend Mode */
756 rsnd_rdai_channels_set(rdai, slots);
757 rsnd_rdai_ssi_lane_set(rdai, 1);
758 rsnd_rdai_width_set(rdai, slot_width);
761 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
768 static unsigned int rsnd_soc_hw_channels_list[] = {
772 static unsigned int rsnd_soc_hw_rate_list[] = {
787 static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
788 unsigned int *list, int list_num,
789 struct snd_interval *baseline, struct snd_interval *iv)
791 struct snd_interval p;
795 snd_interval_any(&p);
799 for (i = 0; i < list_num; i++) {
801 if (!snd_interval_test(iv, list[i]))
804 rate = rsnd_ssi_clk_query(rdai,
805 baseline->min, list[i], NULL);
807 p.min = min(p.min, list[i]);
808 p.max = max(p.max, list[i]);
811 rate = rsnd_ssi_clk_query(rdai,
812 baseline->max, list[i], NULL);
814 p.min = min(p.min, list[i]);
815 p.max = max(p.max, list[i]);
819 return snd_interval_refine(iv, &p);
822 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
823 struct snd_pcm_hw_rule *rule)
825 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
826 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
827 struct snd_interval ic;
828 struct rsnd_dai_stream *io = rule->private;
829 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
832 * possible sampling rate limitation is same as
833 * 2ch if it supports multi ssi
834 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
838 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
840 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
841 ARRAY_SIZE(rsnd_soc_hw_rate_list),
845 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
846 struct snd_pcm_hw_rule *rule)
848 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
849 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
850 struct snd_interval ic;
851 struct rsnd_dai_stream *io = rule->private;
852 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
855 * possible sampling rate limitation is same as
856 * 2ch if it supports multi ssi
857 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
861 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
863 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
864 ARRAY_SIZE(rsnd_soc_hw_channels_list),
868 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
869 .info = SNDRV_PCM_INFO_INTERLEAVED |
870 SNDRV_PCM_INFO_MMAP |
871 SNDRV_PCM_INFO_MMAP_VALID,
872 .buffer_bytes_max = 64 * 1024,
873 .period_bytes_min = 32,
874 .period_bytes_max = 8192,
880 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
881 struct snd_soc_dai *dai)
883 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
884 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
885 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
886 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
887 struct snd_pcm_runtime *runtime = substream->runtime;
888 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
892 rsnd_dai_stream_init(io, substream);
896 * It depends on Platform design
898 constraint->list = rsnd_soc_hw_channels_list;
899 constraint->count = 0;
900 constraint->mask = 0;
902 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
903 if (rsnd_soc_hw_channels_list[i] > max_channels)
905 constraint->count = i + 1;
908 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
910 snd_pcm_hw_constraint_list(runtime, 0,
911 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
913 snd_pcm_hw_constraint_integer(runtime,
914 SNDRV_PCM_HW_PARAM_PERIODS);
917 * Sampling Rate / Channel Limitation
918 * It depends on Clock Master Mode
920 if (rsnd_rdai_is_clk_master(rdai)) {
921 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
923 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
924 rsnd_soc_hw_rule_rate,
925 is_play ? &rdai->playback : &rdai->capture,
926 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
927 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
928 rsnd_soc_hw_rule_channels,
929 is_play ? &rdai->playback : &rdai->capture,
930 SNDRV_PCM_HW_PARAM_RATE, -1);
934 * call rsnd_dai_call without spinlock
936 ret = rsnd_dai_call(nolock_start, io, priv);
938 rsnd_dai_call(nolock_stop, io, priv);
943 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
944 struct snd_soc_dai *dai)
946 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
947 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
948 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
951 * call rsnd_dai_call without spinlock
953 rsnd_dai_call(nolock_stop, io, priv);
955 rsnd_dai_stream_quit(io);
958 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
959 struct snd_soc_dai *dai)
961 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
962 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
963 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
965 return rsnd_dai_call(prepare, io, priv);
968 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
969 .startup = rsnd_soc_dai_startup,
970 .shutdown = rsnd_soc_dai_shutdown,
971 .trigger = rsnd_soc_dai_trigger,
972 .set_fmt = rsnd_soc_dai_set_fmt,
973 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
974 .prepare = rsnd_soc_dai_prepare,
977 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
978 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
979 struct device_node *node,
980 struct device_node *playback,
981 struct device_node *capture)
983 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
984 struct device_node *np;
985 struct rsnd_mod *mod;
992 for_each_child_of_node(node, np) {
993 mod = mod_get(priv, i);
995 rsnd_dai_connect(mod, &rdai->playback, mod->type);
997 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1004 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1007 struct device *dev = rsnd_priv_to_dev(priv);
1008 struct device_node *np = dev->of_node;
1009 struct device_node *dai_node;
1010 struct device_node *ret;
1015 * parse both previous dai (= rcar_sound,dai), and
1016 * graph dai (= ports/port)
1018 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1021 goto of_node_compatible;
1026 dai_node = of_graph_get_next_endpoint(np, NULL);
1035 of_node_put(dai_node);
1040 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1041 struct device_node *dai_np,
1044 struct device_node *playback, *capture;
1045 struct rsnd_dai_stream *io_playback;
1046 struct rsnd_dai_stream *io_capture;
1047 struct snd_soc_dai_driver *drv;
1048 struct rsnd_dai *rdai;
1049 struct device *dev = rsnd_priv_to_dev(priv);
1052 rdai = rsnd_rdai_get(priv, dai_i);
1053 drv = rsnd_daidrv_get(priv, dai_i);
1054 io_playback = &rdai->playback;
1055 io_capture = &rdai->capture;
1057 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1060 drv->name = rdai->name;
1061 drv->ops = &rsnd_soc_dai_ops;
1063 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1064 "DAI%d Playback", dai_i);
1065 drv->playback.rates = RSND_RATES;
1066 drv->playback.formats = RSND_FMTS;
1067 drv->playback.channels_min = 2;
1068 drv->playback.channels_max = 8;
1069 drv->playback.stream_name = rdai->playback.name;
1071 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1072 "DAI%d Capture", dai_i);
1073 drv->capture.rates = RSND_RATES;
1074 drv->capture.formats = RSND_FMTS;
1075 drv->capture.channels_min = 2;
1076 drv->capture.channels_max = 8;
1077 drv->capture.stream_name = rdai->capture.name;
1079 rdai->playback.rdai = rdai;
1080 rdai->capture.rdai = rdai;
1081 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1082 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1083 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
1085 for (io_i = 0;; io_i++) {
1086 playback = of_parse_phandle(dai_np, "playback", io_i);
1087 capture = of_parse_phandle(dai_np, "capture", io_i);
1089 if (!playback && !capture)
1092 rsnd_parse_connect_ssi(rdai, playback, capture);
1093 rsnd_parse_connect_src(rdai, playback, capture);
1094 rsnd_parse_connect_ctu(rdai, playback, capture);
1095 rsnd_parse_connect_mix(rdai, playback, capture);
1096 rsnd_parse_connect_dvc(rdai, playback, capture);
1098 of_node_put(playback);
1099 of_node_put(capture);
1102 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1103 rsnd_ssi_is_pin_sharing(io_playback)) {
1104 /* should have symmetric_rates if pin sharing */
1105 drv->symmetric_rates = 1;
1108 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1109 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1110 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1113 static int rsnd_dai_probe(struct rsnd_priv *priv)
1115 struct device_node *dai_node;
1116 struct device_node *dai_np;
1117 struct snd_soc_dai_driver *rdrv;
1118 struct device *dev = rsnd_priv_to_dev(priv);
1119 struct rsnd_dai *rdai;
1124 dai_node = rsnd_dai_of_node(priv, &is_graph);
1126 nr = of_graph_get_endpoint_count(dai_node);
1128 nr = of_get_child_count(dai_node);
1133 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1134 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1139 priv->daidrv = rdrv;
1147 for_each_endpoint_of_node(dai_node, dai_np) {
1148 __rsnd_dai_probe(priv, dai_np, dai_i);
1149 rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
1153 for_each_child_of_node(dai_node, dai_np)
1154 __rsnd_dai_probe(priv, dai_np, dai_i++);
1163 static int rsnd_hw_params(struct snd_pcm_substream *substream,
1164 struct snd_pcm_hw_params *hw_params)
1166 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1167 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1168 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1171 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1175 return snd_pcm_lib_malloc_pages(substream,
1176 params_buffer_bytes(hw_params));
1179 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
1181 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1182 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1183 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1184 snd_pcm_uframes_t pointer = 0;
1186 rsnd_dai_call(pointer, io, &pointer);
1191 static const struct snd_pcm_ops rsnd_pcm_ops = {
1192 .ioctl = snd_pcm_lib_ioctl,
1193 .hw_params = rsnd_hw_params,
1194 .hw_free = snd_pcm_lib_free_pages,
1195 .pointer = rsnd_pointer,
1201 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1202 struct snd_ctl_elem_info *uinfo)
1204 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1207 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1208 uinfo->count = cfg->size;
1209 uinfo->value.enumerated.items = cfg->max;
1210 if (uinfo->value.enumerated.item >= cfg->max)
1211 uinfo->value.enumerated.item = cfg->max - 1;
1212 strlcpy(uinfo->value.enumerated.name,
1213 cfg->texts[uinfo->value.enumerated.item],
1214 sizeof(uinfo->value.enumerated.name));
1216 uinfo->count = cfg->size;
1217 uinfo->value.integer.min = 0;
1218 uinfo->value.integer.max = cfg->max;
1219 uinfo->type = (cfg->max == 1) ?
1220 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1221 SNDRV_CTL_ELEM_TYPE_INTEGER;
1227 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1228 struct snd_ctl_elem_value *uc)
1230 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1233 for (i = 0; i < cfg->size; i++)
1235 uc->value.enumerated.item[i] = cfg->val[i];
1237 uc->value.integer.value[i] = cfg->val[i];
1242 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1243 struct snd_ctl_elem_value *uc)
1245 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1248 if (!cfg->accept(cfg->io))
1251 for (i = 0; i < cfg->size; i++) {
1253 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1254 cfg->val[i] = uc->value.enumerated.item[i];
1256 change |= (uc->value.integer.value[i] != cfg->val[i]);
1257 cfg->val[i] = uc->value.integer.value[i];
1261 if (change && cfg->update)
1262 cfg->update(cfg->io, cfg->mod);
1267 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1272 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1274 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1275 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1276 struct device *dev = rsnd_priv_to_dev(priv);
1279 dev_warn(dev, "Can't update kctrl when idle\n");
1286 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1288 cfg->cfg.val = cfg->val;
1293 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1295 cfg->cfg.val = &cfg->val;
1300 const char * const volume_ramp_rate[] = {
1301 "128 dB/1 step", /* 00000 */
1302 "64 dB/1 step", /* 00001 */
1303 "32 dB/1 step", /* 00010 */
1304 "16 dB/1 step", /* 00011 */
1305 "8 dB/1 step", /* 00100 */
1306 "4 dB/1 step", /* 00101 */
1307 "2 dB/1 step", /* 00110 */
1308 "1 dB/1 step", /* 00111 */
1309 "0.5 dB/1 step", /* 01000 */
1310 "0.25 dB/1 step", /* 01001 */
1311 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1312 "0.125 dB/2 steps", /* 01011 */
1313 "0.125 dB/4 steps", /* 01100 */
1314 "0.125 dB/8 steps", /* 01101 */
1315 "0.125 dB/16 steps", /* 01110 */
1316 "0.125 dB/32 steps", /* 01111 */
1317 "0.125 dB/64 steps", /* 10000 */
1318 "0.125 dB/128 steps", /* 10001 */
1319 "0.125 dB/256 steps", /* 10010 */
1320 "0.125 dB/512 steps", /* 10011 */
1321 "0.125 dB/1024 steps", /* 10100 */
1322 "0.125 dB/2048 steps", /* 10101 */
1323 "0.125 dB/4096 steps", /* 10110 */
1324 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1327 int rsnd_kctrl_new(struct rsnd_mod *mod,
1328 struct rsnd_dai_stream *io,
1329 struct snd_soc_pcm_runtime *rtd,
1330 const unsigned char *name,
1331 int (*accept)(struct rsnd_dai_stream *io),
1332 void (*update)(struct rsnd_dai_stream *io,
1333 struct rsnd_mod *mod),
1334 struct rsnd_kctrl_cfg *cfg,
1335 const char * const *texts,
1339 struct snd_card *card = rtd->card->snd_card;
1340 struct snd_kcontrol *kctrl;
1341 struct snd_kcontrol_new knew = {
1342 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1344 .info = rsnd_kctrl_info,
1346 .get = rsnd_kctrl_get,
1347 .put = rsnd_kctrl_put,
1351 if (size > RSND_MAX_CHANNELS)
1354 kctrl = snd_ctl_new1(&knew, cfg);
1358 ret = snd_ctl_add(card, kctrl);
1365 cfg->accept = accept;
1366 cfg->update = update;
1379 #define PREALLOC_BUFFER (32 * 1024)
1380 #define PREALLOC_BUFFER_MAX (32 * 1024)
1382 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1383 struct rsnd_dai_stream *io,
1386 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1387 struct device *dev = rsnd_priv_to_dev(priv);
1388 struct snd_pcm_substream *substream;
1392 * use Audio-DMAC dev if we can use IPMMU
1394 * rsnd_dmaen_attach()
1399 for (substream = rtd->pcm->streams[stream].substream;
1401 substream = substream->next) {
1402 err = snd_pcm_lib_preallocate_pages(substream,
1405 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1413 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1415 struct snd_soc_dai *dai = rtd->cpu_dai;
1416 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1419 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1423 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1427 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1428 SNDRV_PCM_STREAM_PLAYBACK);
1432 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1433 SNDRV_PCM_STREAM_CAPTURE);
1440 static const struct snd_soc_component_driver rsnd_soc_component = {
1441 .ops = &rsnd_pcm_ops,
1442 .pcm_new = rsnd_pcm_new,
1446 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1447 struct rsnd_dai_stream *io)
1451 ret = rsnd_dai_call(probe, io, priv);
1452 if (ret == -EAGAIN) {
1453 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1454 struct rsnd_mod *mod;
1458 * Fallback to PIO mode
1462 * call "remove" for SSI/SRC/DVC
1463 * SSI will be switch to PIO mode if it was DMA mode
1466 * rsnd_ssi_fallback()
1468 rsnd_dai_call(remove, io, priv);
1471 * remove all mod from io
1472 * and, re connect ssi
1474 for_each_rsnd_mod(i, mod, io)
1475 rsnd_dai_disconnect(mod, io, i);
1476 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1481 rsnd_dai_call(fallback, io, priv);
1485 * DAI has SSI which is PIO mode only now.
1487 ret = rsnd_dai_call(probe, io, priv);
1496 static int rsnd_probe(struct platform_device *pdev)
1498 struct rsnd_priv *priv;
1499 struct device *dev = &pdev->dev;
1500 struct rsnd_dai *rdai;
1501 int (*probe_func[])(struct rsnd_priv *priv) = {
1519 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1524 priv->flags = (unsigned long)of_device_get_match_data(dev);
1525 spin_lock_init(&priv->lock);
1530 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1531 ret = probe_func[i](priv);
1536 for_each_rsnd_dai(rdai, priv, i) {
1537 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1539 goto exit_snd_probe;
1541 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1543 goto exit_snd_probe;
1546 dev_set_drvdata(dev, priv);
1551 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1552 priv->daidrv, rsnd_rdai_nr(priv));
1554 dev_err(dev, "cannot snd dai register\n");
1555 goto exit_snd_probe;
1558 pm_runtime_enable(dev);
1560 dev_info(dev, "probed\n");
1564 for_each_rsnd_dai(rdai, priv, i) {
1565 rsnd_dai_call(remove, &rdai->playback, priv);
1566 rsnd_dai_call(remove, &rdai->capture, priv);
1572 static int rsnd_remove(struct platform_device *pdev)
1574 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1575 struct rsnd_dai *rdai;
1576 void (*remove_func[])(struct rsnd_priv *priv) = {
1588 snd_soc_disconnect_sync(&pdev->dev);
1590 pm_runtime_disable(&pdev->dev);
1592 for_each_rsnd_dai(rdai, priv, i) {
1593 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1594 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1597 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1598 remove_func[i](priv);
1603 static int __maybe_unused rsnd_suspend(struct device *dev)
1605 struct rsnd_priv *priv = dev_get_drvdata(dev);
1607 rsnd_adg_clk_disable(priv);
1612 static int __maybe_unused rsnd_resume(struct device *dev)
1614 struct rsnd_priv *priv = dev_get_drvdata(dev);
1616 rsnd_adg_clk_enable(priv);
1621 static const struct dev_pm_ops rsnd_pm_ops = {
1622 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1625 static struct platform_driver rsnd_driver = {
1627 .name = "rcar_sound",
1629 .of_match_table = rsnd_of_match,
1631 .probe = rsnd_probe,
1632 .remove = rsnd_remove,
1634 module_platform_driver(rsnd_driver);
1636 MODULE_LICENSE("GPL v2");
1637 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1638 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1639 MODULE_ALIAS("platform:rcar-pcm-audio");