1 // SPDX-License-Identifier: GPL-2.0
3 // Renesas R-Car DVC support
5 // Copyright (C) 2014 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
10 * amixer set "DVC Out" 100%
13 * amixer set "DVC In" 100%
16 * amixer set "DVC Out Mute" on
19 * amixer set "DVC In Mute" on
22 * amixer set "DVC Out Ramp Up Rate" "0.125 dB/64 steps"
23 * amixer set "DVC Out Ramp Down Rate" "0.125 dB/512 steps"
24 * amixer set "DVC Out Ramp" on
26 * amixer set "DVC Out" 80% // Volume Down
27 * amixer set "DVC Out" 100% // Volume Up
32 #define RSND_DVC_NAME_SIZE 16
34 #define DVC_NAME "dvc"
38 struct rsnd_kctrl_cfg_m volume;
39 struct rsnd_kctrl_cfg_m mute;
40 struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
41 struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
42 struct rsnd_kctrl_cfg_s rdown; /* Ramp Rate Down */
46 #define KCTRL_INITIALIZED (1 << 0)
48 #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
49 #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
51 #define rsnd_mod_to_dvc(_mod) \
52 container_of((_mod), struct rsnd_dvc, mod)
54 #define for_each_rsnd_dvc(pos, priv, i) \
56 ((i) < rsnd_dvc_nr(priv)) && \
57 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
60 static void rsnd_dvc_activation(struct rsnd_mod *mod)
62 rsnd_mod_write(mod, DVC_SWRSR, 0);
63 rsnd_mod_write(mod, DVC_SWRSR, 1);
66 static void rsnd_dvc_halt(struct rsnd_mod *mod)
68 rsnd_mod_write(mod, DVC_DVUIR, 1);
69 rsnd_mod_write(mod, DVC_SWRSR, 0);
72 #define rsnd_dvc_get_vrpdr(dvc) (rsnd_kctrl_vals(dvc->rup) << 8 | \
73 rsnd_kctrl_vals(dvc->rdown))
74 #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (rsnd_kctrl_valm(dvc->volume, 0) >> 13))
76 static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream *io,
79 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
80 u32 val[RSND_MAX_CHANNELS];
84 if (rsnd_kctrl_vals(dvc->ren))
85 for (i = 0; i < RSND_MAX_CHANNELS; i++)
86 val[i] = rsnd_kctrl_max(dvc->volume);
88 for (i = 0; i < RSND_MAX_CHANNELS; i++)
89 val[i] = rsnd_kctrl_valm(dvc->volume, i);
91 /* Enable Digital Volume */
92 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
93 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
94 rsnd_mod_write(mod, DVC_VOL2R, val[2]);
95 rsnd_mod_write(mod, DVC_VOL3R, val[3]);
96 rsnd_mod_write(mod, DVC_VOL4R, val[4]);
97 rsnd_mod_write(mod, DVC_VOL5R, val[5]);
98 rsnd_mod_write(mod, DVC_VOL6R, val[6]);
99 rsnd_mod_write(mod, DVC_VOL7R, val[7]);
102 static void rsnd_dvc_volume_init(struct rsnd_dai_stream *io,
103 struct rsnd_mod *mod)
105 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
112 adinr = rsnd_get_adinr_bit(mod, io) |
113 rsnd_runtime_channel_after_ctu(io);
115 /* Enable Digital Volume, Zero Cross Mute Mode */
119 if (rsnd_kctrl_vals(dvc->ren)) {
124 * use scale-downed Digital Volume
129 vrpdr = rsnd_dvc_get_vrpdr(dvc);
130 vrdbr = rsnd_dvc_get_vrdbr(dvc);
133 /* Initialize operation */
134 rsnd_mod_write(mod, DVC_DVUIR, 1);
136 /* General Information */
137 rsnd_mod_write(mod, DVC_ADINR, adinr);
138 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
140 /* Volume Ramp Parameter */
141 rsnd_mod_write(mod, DVC_VRCTR, vrctr);
142 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
143 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
145 /* Digital Volume Function Parameter */
146 rsnd_dvc_volume_parameter(io, mod);
148 /* cancel operation */
149 rsnd_mod_write(mod, DVC_DVUIR, 0);
152 static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
153 struct rsnd_mod *mod)
155 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
161 for (i = 0; i < rsnd_kctrl_size(dvc->mute); i++)
162 zcmcr |= (!!rsnd_kctrl_valm(dvc->mute, i)) << i;
164 if (rsnd_kctrl_vals(dvc->ren)) {
165 vrpdr = rsnd_dvc_get_vrpdr(dvc);
166 vrdbr = rsnd_dvc_get_vrdbr(dvc);
169 /* Disable DVC Register access */
170 rsnd_mod_write(mod, DVC_DVUER, 0);
172 /* Zero Cross Mute Function */
173 rsnd_mod_write(mod, DVC_ZCMCR, zcmcr);
175 /* Volume Ramp Function */
176 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
177 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
178 /* add DVC_VRWTR here */
180 /* Digital Volume Function Parameter */
181 rsnd_dvc_volume_parameter(io, mod);
183 /* Enable DVC Register access */
184 rsnd_mod_write(mod, DVC_DVUER, 1);
187 static int rsnd_dvc_probe_(struct rsnd_mod *mod,
188 struct rsnd_dai_stream *io,
189 struct rsnd_priv *priv)
191 return rsnd_cmd_attach(io, rsnd_mod_id(mod));
194 static int rsnd_dvc_init(struct rsnd_mod *mod,
195 struct rsnd_dai_stream *io,
196 struct rsnd_priv *priv)
198 rsnd_mod_power_on(mod);
200 rsnd_dvc_activation(mod);
202 rsnd_dvc_volume_init(io, mod);
204 rsnd_dvc_volume_update(io, mod);
209 static int rsnd_dvc_quit(struct rsnd_mod *mod,
210 struct rsnd_dai_stream *io,
211 struct rsnd_priv *priv)
215 rsnd_mod_power_off(mod);
220 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
221 struct rsnd_dai_stream *io,
222 struct snd_soc_pcm_runtime *rtd)
224 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
225 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
226 int is_play = rsnd_io_is_play(io);
227 int channels = rsnd_rdai_channels_get(rdai);
230 if (rsnd_flags_has(dvc, KCTRL_INITIALIZED))
234 ret = rsnd_kctrl_new_m(mod, io, rtd,
236 "DVC Out Playback Volume" : "DVC In Capture Volume",
237 rsnd_kctrl_accept_anytime,
238 rsnd_dvc_volume_update,
239 &dvc->volume, channels,
245 ret = rsnd_kctrl_new_m(mod, io, rtd,
247 "DVC Out Mute Switch" : "DVC In Mute Switch",
248 rsnd_kctrl_accept_anytime,
249 rsnd_dvc_volume_update,
250 &dvc->mute, channels,
256 ret = rsnd_kctrl_new_s(mod, io, rtd,
258 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
259 rsnd_kctrl_accept_anytime,
260 rsnd_dvc_volume_update,
265 ret = rsnd_kctrl_new_e(mod, io, rtd,
267 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
268 rsnd_kctrl_accept_anytime,
269 rsnd_dvc_volume_update,
272 VOLUME_RAMP_MAX_DVC);
276 ret = rsnd_kctrl_new_e(mod, io, rtd,
278 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
279 rsnd_kctrl_accept_anytime,
280 rsnd_dvc_volume_update,
283 VOLUME_RAMP_MAX_DVC);
288 rsnd_flags_set(dvc, KCTRL_INITIALIZED);
293 static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
294 struct rsnd_mod *mod)
296 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
298 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
302 static struct rsnd_mod_ops rsnd_dvc_ops = {
304 .dma_req = rsnd_dvc_dma_req,
305 .probe = rsnd_dvc_probe_,
306 .init = rsnd_dvc_init,
307 .quit = rsnd_dvc_quit,
308 .pcm_new = rsnd_dvc_pcm_new,
311 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
313 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
316 return rsnd_mod_get(rsnd_dvc_get(priv, id));
319 int rsnd_dvc_probe(struct rsnd_priv *priv)
321 struct device_node *node;
322 struct device_node *np;
323 struct device *dev = rsnd_priv_to_dev(priv);
324 struct rsnd_dvc *dvc;
326 char name[RSND_DVC_NAME_SIZE];
329 /* This driver doesn't support Gen1 at this point */
330 if (rsnd_is_gen1(priv))
333 node = rsnd_dvc_of_node(priv);
335 return 0; /* not used is not error */
337 nr = of_get_child_count(node);
340 goto rsnd_dvc_probe_done;
343 dvc = devm_kcalloc(dev, nr, sizeof(*dvc), GFP_KERNEL);
346 goto rsnd_dvc_probe_done;
354 for_each_child_of_node(node, np) {
355 dvc = rsnd_dvc_get(priv, i);
357 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
360 clk = devm_clk_get(dev, name);
364 goto rsnd_dvc_probe_done;
367 ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
368 clk, rsnd_mod_get_status, RSND_MOD_DVC, i);
371 goto rsnd_dvc_probe_done;
383 void rsnd_dvc_remove(struct rsnd_priv *priv)
385 struct rsnd_dvc *dvc;
388 for_each_rsnd_dvc(dvc, priv, i) {
389 rsnd_mod_quit(rsnd_mod_get(dvc));