1 // SPDX-License-Identifier: GPL-2.0
5 // Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
10 * [SRC3 / SRC6] -> |CTU n0| -> [MIX n0| ->
11 * [SRC4 / SRC9] -> |CTU n1| -> [MIX n1| ->
12 * [SRC0 / SRC1] -> |CTU n2| -> [MIX n2| ->
13 * [SRC2 / SRC5] -> |CTU n3| -> [MIX n3| ->
17 * DAI0 : playback = <&src0 &ctu02 &mix0 &dvc0 &ssi0>;
18 * DAI1 : playback = <&src2 &ctu03 &mix0 &dvc0 &ssi0>;
21 * amixer set "MIX",0 100% // DAI0 Volume
22 * amixer set "MIX",1 100% // DAI1 Volume
25 * amixer set "MIX Ramp Up Rate" "0.125 dB/1 step"
26 * amixer set "MIX Ramp Down Rate" "4 dB/1 step"
27 * amixer set "MIX Ramp" on
29 * amixer set "MIX",0 80% // DAI0 Volume Down
30 * amixer set "MIX",1 100% // DAI1 Volume Up
35 #define MIX_NAME_SIZE 16
36 #define MIX_NAME "mix"
40 struct rsnd_kctrl_cfg_s volumeA; /* MDBAR */
41 struct rsnd_kctrl_cfg_s volumeB; /* MDBBR */
42 struct rsnd_kctrl_cfg_s volumeC; /* MDBCR */
43 struct rsnd_kctrl_cfg_s volumeD; /* MDBDR */
44 struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
45 struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
46 struct rsnd_kctrl_cfg_s rdw; /* Ramp Rate Down */
50 #define ONCE_KCTRL_INITIALIZED (1 << 0)
51 #define HAS_VOLA (1 << 1)
52 #define HAS_VOLB (1 << 2)
53 #define HAS_VOLC (1 << 3)
54 #define HAS_VOLD (1 << 4)
58 #define rsnd_mod_to_mix(_mod) \
59 container_of((_mod), struct rsnd_mix, mod)
61 #define rsnd_mix_get(priv, id) ((struct rsnd_mix *)(priv->mix) + id)
62 #define rsnd_mix_nr(priv) ((priv)->mix_nr)
63 #define for_each_rsnd_mix(pos, priv, i) \
65 ((i) < rsnd_mix_nr(priv)) && \
66 ((pos) = (struct rsnd_mix *)(priv)->mix + i); \
69 static void rsnd_mix_activation(struct rsnd_mod *mod)
71 rsnd_mod_write(mod, MIX_SWRSR, 0);
72 rsnd_mod_write(mod, MIX_SWRSR, 1);
75 static void rsnd_mix_halt(struct rsnd_mod *mod)
77 rsnd_mod_write(mod, MIX_MIXIR, 1);
78 rsnd_mod_write(mod, MIX_SWRSR, 0);
81 #define rsnd_mix_get_vol(mix, X) \
82 rsnd_flags_has(mix, HAS_VOL##X) ? \
83 (VOL_MAX - rsnd_kctrl_vals(mix->volume##X)) : 0
84 static void rsnd_mix_volume_parameter(struct rsnd_dai_stream *io,
87 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
88 struct device *dev = rsnd_priv_to_dev(priv);
89 struct rsnd_mix *mix = rsnd_mod_to_mix(mod);
90 u32 volA = rsnd_mix_get_vol(mix, A);
91 u32 volB = rsnd_mix_get_vol(mix, B);
92 u32 volC = rsnd_mix_get_vol(mix, C);
93 u32 volD = rsnd_mix_get_vol(mix, D);
95 dev_dbg(dev, "MIX A/B/C/D = %02x/%02x/%02x/%02x\n",
96 volA, volB, volC, volD);
98 rsnd_mod_write(mod, MIX_MDBAR, volA);
99 rsnd_mod_write(mod, MIX_MDBBR, volB);
100 rsnd_mod_write(mod, MIX_MDBCR, volC);
101 rsnd_mod_write(mod, MIX_MDBDR, volD);
104 static void rsnd_mix_volume_init(struct rsnd_dai_stream *io,
105 struct rsnd_mod *mod)
107 struct rsnd_mix *mix = rsnd_mod_to_mix(mod);
109 rsnd_mod_write(mod, MIX_MIXIR, 1);
111 /* General Information */
112 rsnd_mod_write(mod, MIX_ADINR, rsnd_runtime_channel_after_ctu(io));
115 rsnd_mod_write(mod, MIX_MIXMR, rsnd_kctrl_vals(mix->ren));
116 rsnd_mod_write(mod, MIX_MVPDR, rsnd_kctrl_vals(mix->rup) << 8 |
117 rsnd_kctrl_vals(mix->rdw));
119 /* common volume parameter */
120 rsnd_mix_volume_parameter(io, mod);
122 rsnd_mod_write(mod, MIX_MIXIR, 0);
125 static void rsnd_mix_volume_update(struct rsnd_dai_stream *io,
126 struct rsnd_mod *mod)
128 /* Disable MIX dB setting */
129 rsnd_mod_write(mod, MIX_MDBER, 0);
131 /* common volume parameter */
132 rsnd_mix_volume_parameter(io, mod);
134 /* Enable MIX dB setting */
135 rsnd_mod_write(mod, MIX_MDBER, 1);
138 static int rsnd_mix_probe_(struct rsnd_mod *mod,
139 struct rsnd_dai_stream *io,
140 struct rsnd_priv *priv)
142 return rsnd_cmd_attach(io, rsnd_mod_id(mod));
145 static int rsnd_mix_init(struct rsnd_mod *mod,
146 struct rsnd_dai_stream *io,
147 struct rsnd_priv *priv)
151 ret = rsnd_mod_power_on(mod);
155 rsnd_mix_activation(mod);
157 rsnd_mix_volume_init(io, mod);
159 rsnd_mix_volume_update(io, mod);
164 static int rsnd_mix_quit(struct rsnd_mod *mod,
165 struct rsnd_dai_stream *io,
166 struct rsnd_priv *priv)
170 rsnd_mod_power_off(mod);
175 static int rsnd_mix_pcm_new(struct rsnd_mod *mod,
176 struct rsnd_dai_stream *io,
177 struct snd_soc_pcm_runtime *rtd)
179 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
180 struct device *dev = rsnd_priv_to_dev(priv);
181 struct rsnd_mix *mix = rsnd_mod_to_mix(mod);
182 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
183 struct rsnd_kctrl_cfg_s *volume;
186 switch (rsnd_mod_id(src_mod)) {
189 volume = &mix->volumeA;
190 rsnd_flags_set(mix, HAS_VOLA);
194 volume = &mix->volumeB;
195 rsnd_flags_set(mix, HAS_VOLB);
199 volume = &mix->volumeC;
200 rsnd_flags_set(mix, HAS_VOLC);
204 volume = &mix->volumeD;
205 rsnd_flags_set(mix, HAS_VOLD);
208 dev_err(dev, "unknown SRC is connected\n");
213 ret = rsnd_kctrl_new_s(mod, io, rtd,
214 "MIX Playback Volume",
215 rsnd_kctrl_accept_anytime,
216 rsnd_mix_volume_update,
220 rsnd_kctrl_vals(*volume) = VOL_MAX;
222 if (rsnd_flags_has(mix, ONCE_KCTRL_INITIALIZED))
226 ret = rsnd_kctrl_new_s(mod, io, rtd,
228 rsnd_kctrl_accept_anytime,
229 rsnd_mix_volume_update,
234 ret = rsnd_kctrl_new_e(mod, io, rtd,
236 rsnd_kctrl_accept_anytime,
237 rsnd_mix_volume_update,
240 VOLUME_RAMP_MAX_MIX);
244 ret = rsnd_kctrl_new_e(mod, io, rtd,
245 "MIX Ramp Down Rate",
246 rsnd_kctrl_accept_anytime,
247 rsnd_mix_volume_update,
250 VOLUME_RAMP_MAX_MIX);
252 rsnd_flags_set(mix, ONCE_KCTRL_INITIALIZED);
257 #ifdef CONFIG_DEBUG_FS
258 static void rsnd_mix_debug_info(struct seq_file *m,
259 struct rsnd_dai_stream *io,
260 struct rsnd_mod *mod)
262 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
263 0xd00 + rsnd_mod_id(mod) * 0x40, 0x30);
265 #define DEBUG_INFO .debug_info = rsnd_mix_debug_info
270 static struct rsnd_mod_ops rsnd_mix_ops = {
272 .probe = rsnd_mix_probe_,
273 .init = rsnd_mix_init,
274 .quit = rsnd_mix_quit,
275 .pcm_new = rsnd_mix_pcm_new,
276 .get_status = rsnd_mod_get_status,
280 struct rsnd_mod *rsnd_mix_mod_get(struct rsnd_priv *priv, int id)
282 if (WARN_ON(id < 0 || id >= rsnd_mix_nr(priv)))
285 return rsnd_mod_get(rsnd_mix_get(priv, id));
288 int rsnd_mix_probe(struct rsnd_priv *priv)
290 struct device_node *node;
291 struct device_node *np;
292 struct device *dev = rsnd_priv_to_dev(priv);
293 struct rsnd_mix *mix;
295 char name[MIX_NAME_SIZE];
298 /* This driver doesn't support Gen1 at this point */
299 if (rsnd_is_gen1(priv))
302 node = rsnd_mix_of_node(priv);
304 return 0; /* not used is not error */
306 nr = of_get_child_count(node);
309 goto rsnd_mix_probe_done;
312 mix = devm_kcalloc(dev, nr, sizeof(*mix), GFP_KERNEL);
315 goto rsnd_mix_probe_done;
323 for_each_child_of_node(node, np) {
324 mix = rsnd_mix_get(priv, i);
326 snprintf(name, MIX_NAME_SIZE, "%s.%d",
329 clk = devm_clk_get(dev, name);
333 goto rsnd_mix_probe_done;
336 ret = rsnd_mod_init(priv, rsnd_mod_get(mix), &rsnd_mix_ops,
337 clk, RSND_MOD_MIX, i);
340 goto rsnd_mix_probe_done;
352 void rsnd_mix_remove(struct rsnd_priv *priv)
354 struct rsnd_mix *mix;
357 for_each_rsnd_mix(mix, priv, i) {
358 rsnd_mod_quit(rsnd_mod_get(mix));