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); \
485 if (tmp && (tmp != -EPROBE_DEFER)) \
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) {
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_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
885 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
886 struct snd_pcm_runtime *runtime = substream->runtime;
887 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
890 rsnd_dai_stream_init(io, substream);
894 * It depends on Platform design
896 constraint->list = rsnd_soc_hw_channels_list;
897 constraint->count = 0;
898 constraint->mask = 0;
900 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
901 if (rsnd_soc_hw_channels_list[i] > max_channels)
903 constraint->count = i + 1;
906 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
908 snd_pcm_hw_constraint_list(runtime, 0,
909 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
911 snd_pcm_hw_constraint_integer(runtime,
912 SNDRV_PCM_HW_PARAM_PERIODS);
915 * Sampling Rate / Channel Limitation
916 * It depends on Clock Master Mode
918 if (rsnd_rdai_is_clk_master(rdai)) {
919 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
921 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
922 rsnd_soc_hw_rule_rate,
923 is_play ? &rdai->playback : &rdai->capture,
924 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
925 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
926 rsnd_soc_hw_rule_channels,
927 is_play ? &rdai->playback : &rdai->capture,
928 SNDRV_PCM_HW_PARAM_RATE, -1);
934 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
935 struct snd_soc_dai *dai)
937 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
938 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
939 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
942 * call rsnd_dai_call without spinlock
944 rsnd_dai_call(cleanup, io, priv);
946 rsnd_dai_stream_quit(io);
949 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
950 struct snd_soc_dai *dai)
952 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
953 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
954 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
956 return rsnd_dai_call(prepare, io, priv);
959 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
960 .startup = rsnd_soc_dai_startup,
961 .shutdown = rsnd_soc_dai_shutdown,
962 .trigger = rsnd_soc_dai_trigger,
963 .set_fmt = rsnd_soc_dai_set_fmt,
964 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
965 .prepare = rsnd_soc_dai_prepare,
968 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
969 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
970 struct device_node *node,
971 struct device_node *playback,
972 struct device_node *capture)
974 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
975 struct device_node *np;
976 struct rsnd_mod *mod;
983 for_each_child_of_node(node, np) {
984 mod = mod_get(priv, i);
986 rsnd_dai_connect(mod, &rdai->playback, mod->type);
988 rsnd_dai_connect(mod, &rdai->capture, mod->type);
995 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
998 struct device *dev = rsnd_priv_to_dev(priv);
999 struct device_node *np = dev->of_node;
1000 struct device_node *dai_node;
1001 struct device_node *ret;
1006 * parse both previous dai (= rcar_sound,dai), and
1007 * graph dai (= ports/port)
1009 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1012 goto of_node_compatible;
1017 dai_node = of_graph_get_next_endpoint(np, NULL);
1026 of_node_put(dai_node);
1031 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1032 struct device_node *dai_np,
1035 struct device_node *playback, *capture;
1036 struct rsnd_dai_stream *io_playback;
1037 struct rsnd_dai_stream *io_capture;
1038 struct snd_soc_dai_driver *drv;
1039 struct rsnd_dai *rdai;
1040 struct device *dev = rsnd_priv_to_dev(priv);
1043 rdai = rsnd_rdai_get(priv, dai_i);
1044 drv = rsnd_daidrv_get(priv, dai_i);
1045 io_playback = &rdai->playback;
1046 io_capture = &rdai->capture;
1048 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1051 drv->name = rdai->name;
1052 drv->ops = &rsnd_soc_dai_ops;
1054 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1055 "DAI%d Playback", dai_i);
1056 drv->playback.rates = RSND_RATES;
1057 drv->playback.formats = RSND_FMTS;
1058 drv->playback.channels_min = 2;
1059 drv->playback.channels_max = 8;
1060 drv->playback.stream_name = rdai->playback.name;
1062 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1063 "DAI%d Capture", dai_i);
1064 drv->capture.rates = RSND_RATES;
1065 drv->capture.formats = RSND_FMTS;
1066 drv->capture.channels_min = 2;
1067 drv->capture.channels_max = 8;
1068 drv->capture.stream_name = rdai->capture.name;
1070 rdai->playback.rdai = rdai;
1071 rdai->capture.rdai = rdai;
1072 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1073 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1074 rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
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);
1266 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1267 struct device *dev = rsnd_priv_to_dev(priv);
1270 dev_warn(dev, "Can't update kctrl when idle\n");
1277 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1279 cfg->cfg.val = cfg->val;
1284 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1286 cfg->cfg.val = &cfg->val;
1291 const char * const volume_ramp_rate[] = {
1292 "128 dB/1 step", /* 00000 */
1293 "64 dB/1 step", /* 00001 */
1294 "32 dB/1 step", /* 00010 */
1295 "16 dB/1 step", /* 00011 */
1296 "8 dB/1 step", /* 00100 */
1297 "4 dB/1 step", /* 00101 */
1298 "2 dB/1 step", /* 00110 */
1299 "1 dB/1 step", /* 00111 */
1300 "0.5 dB/1 step", /* 01000 */
1301 "0.25 dB/1 step", /* 01001 */
1302 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1303 "0.125 dB/2 steps", /* 01011 */
1304 "0.125 dB/4 steps", /* 01100 */
1305 "0.125 dB/8 steps", /* 01101 */
1306 "0.125 dB/16 steps", /* 01110 */
1307 "0.125 dB/32 steps", /* 01111 */
1308 "0.125 dB/64 steps", /* 10000 */
1309 "0.125 dB/128 steps", /* 10001 */
1310 "0.125 dB/256 steps", /* 10010 */
1311 "0.125 dB/512 steps", /* 10011 */
1312 "0.125 dB/1024 steps", /* 10100 */
1313 "0.125 dB/2048 steps", /* 10101 */
1314 "0.125 dB/4096 steps", /* 10110 */
1315 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1318 int rsnd_kctrl_new(struct rsnd_mod *mod,
1319 struct rsnd_dai_stream *io,
1320 struct snd_soc_pcm_runtime *rtd,
1321 const unsigned char *name,
1322 int (*accept)(struct rsnd_dai_stream *io),
1323 void (*update)(struct rsnd_dai_stream *io,
1324 struct rsnd_mod *mod),
1325 struct rsnd_kctrl_cfg *cfg,
1326 const char * const *texts,
1330 struct snd_card *card = rtd->card->snd_card;
1331 struct snd_kcontrol *kctrl;
1332 struct snd_kcontrol_new knew = {
1333 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1335 .info = rsnd_kctrl_info,
1337 .get = rsnd_kctrl_get,
1338 .put = rsnd_kctrl_put,
1342 if (size > RSND_MAX_CHANNELS)
1345 kctrl = snd_ctl_new1(&knew, cfg);
1349 ret = snd_ctl_add(card, kctrl);
1356 cfg->accept = accept;
1357 cfg->update = update;
1370 #define PREALLOC_BUFFER (32 * 1024)
1371 #define PREALLOC_BUFFER_MAX (32 * 1024)
1373 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1374 struct rsnd_dai_stream *io,
1377 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1378 struct device *dev = rsnd_priv_to_dev(priv);
1379 struct snd_pcm_substream *substream;
1383 * use Audio-DMAC dev if we can use IPMMU
1385 * rsnd_dmaen_attach()
1390 for (substream = rtd->pcm->streams[stream].substream;
1392 substream = substream->next) {
1393 err = snd_pcm_lib_preallocate_pages(substream,
1396 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1404 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1406 struct snd_soc_dai *dai = rtd->cpu_dai;
1407 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1410 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1414 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1418 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1419 SNDRV_PCM_STREAM_PLAYBACK);
1423 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1424 SNDRV_PCM_STREAM_CAPTURE);
1431 static const struct snd_soc_component_driver rsnd_soc_component = {
1432 .ops = &rsnd_pcm_ops,
1433 .pcm_new = rsnd_pcm_new,
1437 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1438 struct rsnd_dai_stream *io)
1442 ret = rsnd_dai_call(probe, io, priv);
1443 if (ret == -EAGAIN) {
1444 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1445 struct rsnd_mod *mod;
1449 * Fallback to PIO mode
1453 * call "remove" for SSI/SRC/DVC
1454 * SSI will be switch to PIO mode if it was DMA mode
1457 * rsnd_ssi_fallback()
1459 rsnd_dai_call(remove, io, priv);
1462 * remove all mod from io
1463 * and, re connect ssi
1465 for_each_rsnd_mod(i, mod, io)
1466 rsnd_dai_disconnect(mod, io, i);
1467 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1472 rsnd_dai_call(fallback, io, priv);
1476 * DAI has SSI which is PIO mode only now.
1478 ret = rsnd_dai_call(probe, io, priv);
1487 static int rsnd_probe(struct platform_device *pdev)
1489 struct rsnd_priv *priv;
1490 struct device *dev = &pdev->dev;
1491 struct rsnd_dai *rdai;
1492 int (*probe_func[])(struct rsnd_priv *priv) = {
1510 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1515 priv->flags = (unsigned long)of_device_get_match_data(dev);
1516 spin_lock_init(&priv->lock);
1521 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1522 ret = probe_func[i](priv);
1527 for_each_rsnd_dai(rdai, priv, i) {
1528 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1530 goto exit_snd_probe;
1532 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1534 goto exit_snd_probe;
1537 dev_set_drvdata(dev, priv);
1542 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1543 priv->daidrv, rsnd_rdai_nr(priv));
1545 dev_err(dev, "cannot snd dai register\n");
1546 goto exit_snd_probe;
1549 pm_runtime_enable(dev);
1551 dev_info(dev, "probed\n");
1555 for_each_rsnd_dai(rdai, priv, i) {
1556 rsnd_dai_call(remove, &rdai->playback, priv);
1557 rsnd_dai_call(remove, &rdai->capture, priv);
1561 * adg is very special mod which can't use rsnd_dai_call(remove),
1562 * and it registers ADG clock on probe.
1563 * It should be unregister if probe failed.
1564 * Mainly it is assuming -EPROBE_DEFER case
1566 rsnd_adg_remove(priv);
1571 static int rsnd_remove(struct platform_device *pdev)
1573 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1574 struct rsnd_dai *rdai;
1575 void (*remove_func[])(struct rsnd_priv *priv) = {
1587 snd_soc_disconnect_sync(&pdev->dev);
1589 pm_runtime_disable(&pdev->dev);
1591 for_each_rsnd_dai(rdai, priv, i) {
1592 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1593 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1596 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1597 remove_func[i](priv);
1602 static int __maybe_unused rsnd_suspend(struct device *dev)
1604 struct rsnd_priv *priv = dev_get_drvdata(dev);
1606 rsnd_adg_clk_disable(priv);
1611 static int __maybe_unused rsnd_resume(struct device *dev)
1613 struct rsnd_priv *priv = dev_get_drvdata(dev);
1615 rsnd_adg_clk_enable(priv);
1620 static const struct dev_pm_ops rsnd_pm_ops = {
1621 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1624 static struct platform_driver rsnd_driver = {
1626 .name = "rcar_sound",
1628 .of_match_table = rsnd_of_match,
1630 .probe = rsnd_probe,
1631 .remove = rsnd_remove,
1633 module_platform_driver(rsnd_driver);
1635 MODULE_LICENSE("GPL v2");
1636 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1637 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1638 MODULE_ALIAS("platform:rcar-pcm-audio");