2 * Renesas R-Car SRU/SCU/SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 * Renesas R-Car sound device structure
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
23 * - CTU : Channel Count Conversion Unit
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
33 * - CTU : Channel Count Conversion Unit
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
45 * | ** this depends on Gen1/Gen2
49 * | ** these depend on data path
50 * | ** gen and platform data control it
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
67 * | ** these control ssi
76 * | ** these control src
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
98 * you can enable below define if you don't need
99 * DAI status debug message when debugging
100 * see rsnd_dbg_dai_call()
102 * #define RSND_DEBUG_NO_DAI_CALL 1
105 #include <linux/pm_runtime.h>
108 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
109 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
111 static const struct of_device_id rsnd_of_match[] = {
112 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
113 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
114 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
117 MODULE_DEVICE_TABLE(of, rsnd_of_match);
122 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
124 if (mod->type != type) {
125 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
126 struct device *dev = rsnd_priv_to_dev(priv);
128 dev_warn(dev, "%s[%d] is not your expected module\n",
129 rsnd_mod_name(mod), rsnd_mod_id(mod));
133 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
134 struct rsnd_mod *mod)
136 if (!mod || !mod->ops || !mod->ops->dma_req)
139 return mod->ops->dma_req(io, mod);
142 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
143 struct rsnd_mod *mod,
144 enum rsnd_mod_type type)
149 int rsnd_mod_init(struct rsnd_priv *priv,
150 struct rsnd_mod *mod,
151 struct rsnd_mod_ops *ops,
153 u32* (*get_status)(struct rsnd_dai_stream *io,
154 struct rsnd_mod *mod,
155 enum rsnd_mod_type type),
156 enum rsnd_mod_type type,
159 int ret = clk_prepare(clk);
169 mod->get_status = get_status;
174 void rsnd_mod_quit(struct rsnd_mod *mod)
176 clk_unprepare(mod->clk);
180 void rsnd_mod_interrupt(struct rsnd_mod *mod,
181 void (*callback)(struct rsnd_mod *mod,
182 struct rsnd_dai_stream *io))
184 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
185 struct rsnd_dai_stream *io;
186 struct rsnd_dai *rdai;
189 for_each_rsnd_dai(rdai, priv, i) {
190 io = &rdai->playback;
191 if (mod == io->mod[mod->type])
195 if (mod == io->mod[mod->type])
200 int rsnd_io_is_working(struct rsnd_dai_stream *io)
202 /* see rsnd_dai_stream_init/quit() */
204 return snd_pcm_running(io->substream);
209 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
210 struct snd_pcm_hw_params *params)
212 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
215 * params will be added when refine
217 * __rsnd_soc_hw_rule_rate()
218 * __rsnd_soc_hw_rule_channels()
221 return params_channels(params);
223 return runtime->channels;
226 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
227 struct snd_pcm_hw_params *params)
229 int chan = rsnd_runtime_channel_original_with_params(io, params);
230 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
233 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
236 return converted_chan;
242 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
243 struct snd_pcm_hw_params *params)
245 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
246 int chan = rsnd_io_is_play(io) ?
247 rsnd_runtime_channel_after_ctu_with_params(io, params) :
248 rsnd_runtime_channel_original_with_params(io, params);
251 if (rsnd_runtime_is_ssi_multi(io))
252 chan /= rsnd_rdai_ssi_lane_get(rdai);
254 /* TDM Extend Mode needs 8ch */
261 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
263 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
264 int lane = rsnd_rdai_ssi_lane_get(rdai);
265 int chan = rsnd_io_is_play(io) ?
266 rsnd_runtime_channel_after_ctu(io) :
267 rsnd_runtime_channel_original(io);
269 return (chan > 2) && (lane > 1);
272 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
274 return rsnd_runtime_channel_for_ssi(io) >= 6;
280 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
282 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
283 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
284 struct device *dev = rsnd_priv_to_dev(priv);
286 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 24bit data 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) == 16)
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 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
549 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
552 return priv->rdai + id;
555 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
556 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
558 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
560 return rsnd_rdai_get(priv, dai->id);
564 * rsnd_soc_dai functions
566 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
568 struct snd_pcm_substream *substream = io->substream;
571 * this function should be called...
573 * - if rsnd_dai_pointer_update() returns true
574 * - without spin lock
577 snd_pcm_period_elapsed(substream);
580 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
581 struct snd_pcm_substream *substream)
583 io->substream = substream;
586 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
588 io->substream = NULL;
592 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
594 struct snd_soc_pcm_runtime *rtd = substream->private_data;
600 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
601 struct snd_pcm_substream *substream)
603 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
604 return &rdai->playback;
606 return &rdai->capture;
609 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
610 struct snd_soc_dai *dai)
612 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
613 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
614 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
618 spin_lock_irqsave(&priv->lock, flags);
621 case SNDRV_PCM_TRIGGER_START:
622 case SNDRV_PCM_TRIGGER_RESUME:
623 ret = rsnd_dai_call(init, io, priv);
625 goto dai_trigger_end;
627 ret = rsnd_dai_call(start, io, priv);
629 goto dai_trigger_end;
631 ret = rsnd_dai_call(irq, io, priv, 1);
633 goto dai_trigger_end;
636 case SNDRV_PCM_TRIGGER_STOP:
637 case SNDRV_PCM_TRIGGER_SUSPEND:
638 ret = rsnd_dai_call(irq, io, priv, 0);
640 ret |= rsnd_dai_call(stop, io, priv);
642 ret |= rsnd_dai_call(quit, io, priv);
650 spin_unlock_irqrestore(&priv->lock, flags);
655 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
657 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
659 /* set master/slave audio interface */
660 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
661 case SND_SOC_DAIFMT_CBM_CFM:
662 rdai->clk_master = 0;
664 case SND_SOC_DAIFMT_CBS_CFS:
665 rdai->clk_master = 1; /* codec is slave, cpu is master */
672 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
673 case SND_SOC_DAIFMT_I2S:
675 rdai->data_alignment = 0;
676 rdai->frm_clk_inv = 0;
678 case SND_SOC_DAIFMT_LEFT_J:
680 rdai->data_alignment = 0;
681 rdai->frm_clk_inv = 1;
683 case SND_SOC_DAIFMT_RIGHT_J:
685 rdai->data_alignment = 1;
686 rdai->frm_clk_inv = 1;
690 /* set clock inversion */
691 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
692 case SND_SOC_DAIFMT_NB_IF:
693 rdai->frm_clk_inv = !rdai->frm_clk_inv;
695 case SND_SOC_DAIFMT_IB_NF:
696 rdai->bit_clk_inv = !rdai->bit_clk_inv;
698 case SND_SOC_DAIFMT_IB_IF:
699 rdai->bit_clk_inv = !rdai->bit_clk_inv;
700 rdai->frm_clk_inv = !rdai->frm_clk_inv;
702 case SND_SOC_DAIFMT_NB_NF:
710 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
711 u32 tx_mask, u32 rx_mask,
712 int slots, int slot_width)
714 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
715 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
716 struct device *dev = rsnd_priv_to_dev(priv);
722 /* TDM Extend Mode */
723 rsnd_rdai_channels_set(rdai, slots);
724 rsnd_rdai_ssi_lane_set(rdai, 1);
727 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
734 static unsigned int rsnd_soc_hw_channels_list[] = {
738 static unsigned int rsnd_soc_hw_rate_list[] = {
753 static int rsnd_soc_hw_rule(struct rsnd_priv *priv,
754 unsigned int *list, int list_num,
755 struct snd_interval *baseline, struct snd_interval *iv)
757 struct snd_interval p;
761 snd_interval_any(&p);
765 for (i = 0; i < list_num; i++) {
767 if (!snd_interval_test(iv, list[i]))
770 rate = rsnd_ssi_clk_query(priv,
771 baseline->min, list[i], NULL);
773 p.min = min(p.min, list[i]);
774 p.max = max(p.max, list[i]);
777 rate = rsnd_ssi_clk_query(priv,
778 baseline->max, list[i], NULL);
780 p.min = min(p.min, list[i]);
781 p.max = max(p.max, list[i]);
785 return snd_interval_refine(iv, &p);
788 static int __rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
789 struct snd_pcm_hw_rule *rule,
792 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
793 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
794 struct snd_interval ic;
795 struct snd_soc_dai *dai = rule->private;
796 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
797 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
798 struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
801 * possible sampling rate limitation is same as
802 * 2ch if it supports multi ssi
803 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
807 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
809 return rsnd_soc_hw_rule(priv, rsnd_soc_hw_rate_list,
810 ARRAY_SIZE(rsnd_soc_hw_rate_list),
814 static int rsnd_soc_hw_rule_rate_playback(struct snd_pcm_hw_params *params,
815 struct snd_pcm_hw_rule *rule)
817 return __rsnd_soc_hw_rule_rate(params, rule, 1);
820 static int rsnd_soc_hw_rule_rate_capture(struct snd_pcm_hw_params *params,
821 struct snd_pcm_hw_rule *rule)
823 return __rsnd_soc_hw_rule_rate(params, rule, 0);
826 static int __rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
827 struct snd_pcm_hw_rule *rule,
830 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
831 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
832 struct snd_interval ic;
833 struct snd_soc_dai *dai = rule->private;
834 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
835 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
836 struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
839 * possible sampling rate limitation is same as
840 * 2ch if it supports multi ssi
841 * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
845 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
847 return rsnd_soc_hw_rule(priv, rsnd_soc_hw_channels_list,
848 ARRAY_SIZE(rsnd_soc_hw_channels_list),
852 static int rsnd_soc_hw_rule_channels_playback(struct snd_pcm_hw_params *params,
853 struct snd_pcm_hw_rule *rule)
855 return __rsnd_soc_hw_rule_channels(params, rule, 1);
858 static int rsnd_soc_hw_rule_channels_capture(struct snd_pcm_hw_params *params,
859 struct snd_pcm_hw_rule *rule)
861 return __rsnd_soc_hw_rule_channels(params, rule, 0);
864 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
865 .info = SNDRV_PCM_INFO_INTERLEAVED |
866 SNDRV_PCM_INFO_MMAP |
867 SNDRV_PCM_INFO_MMAP_VALID,
868 .buffer_bytes_max = 64 * 1024,
869 .period_bytes_min = 32,
870 .period_bytes_max = 8192,
876 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
877 struct snd_soc_dai *dai)
879 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
880 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
881 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
882 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
888 rsnd_dai_stream_init(io, substream);
892 * It depends on Platform design
894 constraint->list = rsnd_soc_hw_channels_list;
895 constraint->count = 0;
896 constraint->mask = 0;
898 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
899 if (rsnd_soc_hw_channels_list[i] > max_channels)
901 constraint->count = i + 1;
904 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
906 snd_pcm_hw_constraint_list(runtime, 0,
907 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
909 snd_pcm_hw_constraint_integer(runtime,
910 SNDRV_PCM_HW_PARAM_PERIODS);
913 * Sampling Rate / Channel Limitation
914 * It depends on Clock Master Mode
916 if (rsnd_rdai_is_clk_master(rdai)) {
917 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
919 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
920 is_play ? rsnd_soc_hw_rule_rate_playback :
921 rsnd_soc_hw_rule_rate_capture,
923 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
924 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
925 is_play ? rsnd_soc_hw_rule_channels_playback :
926 rsnd_soc_hw_rule_channels_capture,
928 SNDRV_PCM_HW_PARAM_RATE, -1);
932 * call rsnd_dai_call without spinlock
934 ret = rsnd_dai_call(nolock_start, io, priv);
936 rsnd_dai_call(nolock_stop, io, priv);
941 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
942 struct snd_soc_dai *dai)
944 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
945 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
946 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
949 * call rsnd_dai_call without spinlock
951 rsnd_dai_call(nolock_stop, io, priv);
953 rsnd_dai_stream_quit(io);
956 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
957 .startup = rsnd_soc_dai_startup,
958 .shutdown = rsnd_soc_dai_shutdown,
959 .trigger = rsnd_soc_dai_trigger,
960 .set_fmt = rsnd_soc_dai_set_fmt,
961 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
964 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
965 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
966 struct device_node *node,
967 struct device_node *playback,
968 struct device_node *capture)
970 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
971 struct device_node *np;
972 struct rsnd_mod *mod;
979 for_each_child_of_node(node, np) {
980 mod = mod_get(priv, i);
982 rsnd_dai_connect(mod, &rdai->playback, mod->type);
984 rsnd_dai_connect(mod, &rdai->capture, mod->type);
991 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
994 struct device *dev = rsnd_priv_to_dev(priv);
995 struct device_node *np = dev->of_node;
996 struct device_node *dai_node;
997 struct device_node *ret;
1002 * parse both previous dai (= rcar_sound,dai), and
1003 * graph dai (= ports/port)
1005 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1008 goto of_node_compatible;
1013 dai_node = of_graph_get_next_endpoint(np, NULL);
1022 of_node_put(dai_node);
1027 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1028 struct device_node *dai_np,
1031 struct device_node *playback, *capture;
1032 struct rsnd_dai_stream *io_playback;
1033 struct rsnd_dai_stream *io_capture;
1034 struct snd_soc_dai_driver *drv;
1035 struct rsnd_dai *rdai;
1036 struct device *dev = rsnd_priv_to_dev(priv);
1039 rdai = rsnd_rdai_get(priv, dai_i);
1040 drv = priv->daidrv + dai_i;
1041 io_playback = &rdai->playback;
1042 io_capture = &rdai->capture;
1044 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1047 drv->name = rdai->name;
1048 drv->ops = &rsnd_soc_dai_ops;
1050 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1051 "DAI%d Playback", dai_i);
1052 drv->playback.rates = RSND_RATES;
1053 drv->playback.formats = RSND_FMTS;
1054 drv->playback.channels_min = 2;
1055 drv->playback.channels_max = 8;
1056 drv->playback.stream_name = rdai->playback.name;
1058 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1059 "DAI%d Capture", dai_i);
1060 drv->capture.rates = RSND_RATES;
1061 drv->capture.formats = RSND_FMTS;
1062 drv->capture.channels_min = 2;
1063 drv->capture.channels_max = 8;
1064 drv->capture.stream_name = rdai->capture.name;
1066 rdai->playback.rdai = rdai;
1067 rdai->capture.rdai = rdai;
1068 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1069 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1071 for (io_i = 0;; io_i++) {
1072 playback = of_parse_phandle(dai_np, "playback", io_i);
1073 capture = of_parse_phandle(dai_np, "capture", io_i);
1075 if (!playback && !capture)
1078 rsnd_parse_connect_ssi(rdai, playback, capture);
1079 rsnd_parse_connect_src(rdai, playback, capture);
1080 rsnd_parse_connect_ctu(rdai, playback, capture);
1081 rsnd_parse_connect_mix(rdai, playback, capture);
1082 rsnd_parse_connect_dvc(rdai, playback, capture);
1084 of_node_put(playback);
1085 of_node_put(capture);
1088 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1089 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1090 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1093 static int rsnd_dai_probe(struct rsnd_priv *priv)
1095 struct device_node *dai_node;
1096 struct device_node *dai_np;
1097 struct snd_soc_dai_driver *rdrv;
1098 struct device *dev = rsnd_priv_to_dev(priv);
1099 struct rsnd_dai *rdai;
1104 dai_node = rsnd_dai_of_node(priv, &is_graph);
1106 nr = of_graph_get_endpoint_count(dai_node);
1108 nr = of_get_child_count(dai_node);
1113 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1114 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1119 priv->daidrv = rdrv;
1127 for_each_endpoint_of_node(dai_node, dai_np) {
1128 __rsnd_dai_probe(priv, dai_np, dai_i);
1129 rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
1133 for_each_child_of_node(dai_node, dai_np)
1134 __rsnd_dai_probe(priv, dai_np, dai_i++);
1143 static int rsnd_hw_params(struct snd_pcm_substream *substream,
1144 struct snd_pcm_hw_params *hw_params)
1146 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1147 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1148 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1151 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1155 return snd_pcm_lib_malloc_pages(substream,
1156 params_buffer_bytes(hw_params));
1159 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
1161 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1162 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1163 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1164 snd_pcm_uframes_t pointer = 0;
1166 rsnd_dai_call(pointer, io, &pointer);
1171 static const struct snd_pcm_ops rsnd_pcm_ops = {
1172 .ioctl = snd_pcm_lib_ioctl,
1173 .hw_params = rsnd_hw_params,
1174 .hw_free = snd_pcm_lib_free_pages,
1175 .pointer = rsnd_pointer,
1181 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1182 struct snd_ctl_elem_info *uinfo)
1184 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1187 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1188 uinfo->count = cfg->size;
1189 uinfo->value.enumerated.items = cfg->max;
1190 if (uinfo->value.enumerated.item >= cfg->max)
1191 uinfo->value.enumerated.item = cfg->max - 1;
1192 strlcpy(uinfo->value.enumerated.name,
1193 cfg->texts[uinfo->value.enumerated.item],
1194 sizeof(uinfo->value.enumerated.name));
1196 uinfo->count = cfg->size;
1197 uinfo->value.integer.min = 0;
1198 uinfo->value.integer.max = cfg->max;
1199 uinfo->type = (cfg->max == 1) ?
1200 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1201 SNDRV_CTL_ELEM_TYPE_INTEGER;
1207 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1208 struct snd_ctl_elem_value *uc)
1210 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1213 for (i = 0; i < cfg->size; i++)
1215 uc->value.enumerated.item[i] = cfg->val[i];
1217 uc->value.integer.value[i] = cfg->val[i];
1222 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1223 struct snd_ctl_elem_value *uc)
1225 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1228 if (!cfg->accept(cfg->io))
1231 for (i = 0; i < cfg->size; i++) {
1233 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1234 cfg->val[i] = uc->value.enumerated.item[i];
1236 change |= (uc->value.integer.value[i] != cfg->val[i]);
1237 cfg->val[i] = uc->value.integer.value[i];
1241 if (change && cfg->update)
1242 cfg->update(cfg->io, cfg->mod);
1247 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1252 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1254 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1259 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1261 cfg->cfg.val = cfg->val;
1266 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1268 cfg->cfg.val = &cfg->val;
1273 const char * const volume_ramp_rate[] = {
1274 "128 dB/1 step", /* 00000 */
1275 "64 dB/1 step", /* 00001 */
1276 "32 dB/1 step", /* 00010 */
1277 "16 dB/1 step", /* 00011 */
1278 "8 dB/1 step", /* 00100 */
1279 "4 dB/1 step", /* 00101 */
1280 "2 dB/1 step", /* 00110 */
1281 "1 dB/1 step", /* 00111 */
1282 "0.5 dB/1 step", /* 01000 */
1283 "0.25 dB/1 step", /* 01001 */
1284 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1285 "0.125 dB/2 steps", /* 01011 */
1286 "0.125 dB/4 steps", /* 01100 */
1287 "0.125 dB/8 steps", /* 01101 */
1288 "0.125 dB/16 steps", /* 01110 */
1289 "0.125 dB/32 steps", /* 01111 */
1290 "0.125 dB/64 steps", /* 10000 */
1291 "0.125 dB/128 steps", /* 10001 */
1292 "0.125 dB/256 steps", /* 10010 */
1293 "0.125 dB/512 steps", /* 10011 */
1294 "0.125 dB/1024 steps", /* 10100 */
1295 "0.125 dB/2048 steps", /* 10101 */
1296 "0.125 dB/4096 steps", /* 10110 */
1297 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1300 int rsnd_kctrl_new(struct rsnd_mod *mod,
1301 struct rsnd_dai_stream *io,
1302 struct snd_soc_pcm_runtime *rtd,
1303 const unsigned char *name,
1304 int (*accept)(struct rsnd_dai_stream *io),
1305 void (*update)(struct rsnd_dai_stream *io,
1306 struct rsnd_mod *mod),
1307 struct rsnd_kctrl_cfg *cfg,
1308 const char * const *texts,
1312 struct snd_card *card = rtd->card->snd_card;
1313 struct snd_kcontrol *kctrl;
1314 struct snd_kcontrol_new knew = {
1315 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1317 .info = rsnd_kctrl_info,
1319 .get = rsnd_kctrl_get,
1320 .put = rsnd_kctrl_put,
1324 if (size > RSND_MAX_CHANNELS)
1327 kctrl = snd_ctl_new1(&knew, cfg);
1331 ret = snd_ctl_add(card, kctrl);
1338 cfg->accept = accept;
1339 cfg->update = update;
1352 #define PREALLOC_BUFFER (32 * 1024)
1353 #define PREALLOC_BUFFER_MAX (32 * 1024)
1355 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1356 struct rsnd_dai_stream *io,
1359 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1360 struct device *dev = rsnd_priv_to_dev(priv);
1361 struct snd_pcm_substream *substream;
1365 * use Audio-DMAC dev if we can use IPMMU
1367 * rsnd_dmaen_attach()
1372 for (substream = rtd->pcm->streams[stream].substream;
1374 substream = substream->next) {
1375 err = snd_pcm_lib_preallocate_pages(substream,
1378 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1386 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1388 struct snd_soc_dai *dai = rtd->cpu_dai;
1389 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1392 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1396 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1400 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1401 SNDRV_PCM_STREAM_PLAYBACK);
1405 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1406 SNDRV_PCM_STREAM_CAPTURE);
1413 static const struct snd_soc_component_driver rsnd_soc_component = {
1414 .ops = &rsnd_pcm_ops,
1415 .pcm_new = rsnd_pcm_new,
1419 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1420 struct rsnd_dai_stream *io)
1424 ret = rsnd_dai_call(probe, io, priv);
1425 if (ret == -EAGAIN) {
1426 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1427 struct rsnd_mod *mod;
1431 * Fallback to PIO mode
1435 * call "remove" for SSI/SRC/DVC
1436 * SSI will be switch to PIO mode if it was DMA mode
1439 * rsnd_ssi_fallback()
1441 rsnd_dai_call(remove, io, priv);
1444 * remove all mod from io
1445 * and, re connect ssi
1447 for_each_rsnd_mod(i, mod, io)
1448 rsnd_dai_disconnect(mod, io, i);
1449 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1454 rsnd_dai_call(fallback, io, priv);
1458 * DAI has SSI which is PIO mode only now.
1460 ret = rsnd_dai_call(probe, io, priv);
1469 static int rsnd_probe(struct platform_device *pdev)
1471 struct rsnd_priv *priv;
1472 struct device *dev = &pdev->dev;
1473 struct rsnd_dai *rdai;
1474 int (*probe_func[])(struct rsnd_priv *priv) = {
1492 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1497 priv->flags = (unsigned long)of_device_get_match_data(dev);
1498 spin_lock_init(&priv->lock);
1503 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1504 ret = probe_func[i](priv);
1509 for_each_rsnd_dai(rdai, priv, i) {
1510 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1512 goto exit_snd_probe;
1514 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1516 goto exit_snd_probe;
1519 dev_set_drvdata(dev, priv);
1524 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1525 priv->daidrv, rsnd_rdai_nr(priv));
1527 dev_err(dev, "cannot snd dai register\n");
1528 goto exit_snd_probe;
1531 pm_runtime_enable(dev);
1533 dev_info(dev, "probed\n");
1537 for_each_rsnd_dai(rdai, priv, i) {
1538 rsnd_dai_call(remove, &rdai->playback, priv);
1539 rsnd_dai_call(remove, &rdai->capture, priv);
1545 static int rsnd_remove(struct platform_device *pdev)
1547 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1548 struct rsnd_dai *rdai;
1549 void (*remove_func[])(struct rsnd_priv *priv) = {
1561 snd_soc_disconnect_sync(&pdev->dev);
1563 pm_runtime_disable(&pdev->dev);
1565 for_each_rsnd_dai(rdai, priv, i) {
1566 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1567 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1570 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1571 remove_func[i](priv);
1576 static int __maybe_unused rsnd_suspend(struct device *dev)
1578 struct rsnd_priv *priv = dev_get_drvdata(dev);
1580 rsnd_adg_clk_disable(priv);
1585 static int __maybe_unused rsnd_resume(struct device *dev)
1587 struct rsnd_priv *priv = dev_get_drvdata(dev);
1589 rsnd_adg_clk_enable(priv);
1594 static const struct dev_pm_ops rsnd_pm_ops = {
1595 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1598 static struct platform_driver rsnd_driver = {
1600 .name = "rcar_sound",
1602 .of_match_table = rsnd_of_match,
1604 .probe = rsnd_probe,
1605 .remove = rsnd_remove,
1607 module_platform_driver(rsnd_driver);
1609 MODULE_LICENSE("GPL");
1610 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1611 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1612 MODULE_ALIAS("platform:rcar-pcm-audio");