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 */
45 #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
46 #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
48 #define rsnd_mod_to_dvc(_mod) \
49 container_of((_mod), struct rsnd_dvc, mod)
51 #define for_each_rsnd_dvc(pos, priv, i) \
53 ((i) < rsnd_dvc_nr(priv)) && \
54 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
57 static void rsnd_dvc_activation(struct rsnd_mod *mod)
59 rsnd_mod_write(mod, DVC_SWRSR, 0);
60 rsnd_mod_write(mod, DVC_SWRSR, 1);
63 static void rsnd_dvc_halt(struct rsnd_mod *mod)
65 rsnd_mod_write(mod, DVC_DVUIR, 1);
66 rsnd_mod_write(mod, DVC_SWRSR, 0);
69 #define rsnd_dvc_get_vrpdr(dvc) (rsnd_kctrl_vals(dvc->rup) << 8 | \
70 rsnd_kctrl_vals(dvc->rdown))
71 #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (rsnd_kctrl_valm(dvc->volume, 0) >> 13))
73 static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream *io,
76 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
77 u32 val[RSND_MAX_CHANNELS];
81 if (rsnd_kctrl_vals(dvc->ren))
82 for (i = 0; i < RSND_MAX_CHANNELS; i++)
83 val[i] = rsnd_kctrl_max(dvc->volume);
85 for (i = 0; i < RSND_MAX_CHANNELS; i++)
86 val[i] = rsnd_kctrl_valm(dvc->volume, i);
88 /* Enable Digital Volume */
89 for (i = 0; i < RSND_MAX_CHANNELS; i++)
90 rsnd_mod_write(mod, DVC_VOLxR(i), val[i]);
93 static void rsnd_dvc_volume_init(struct rsnd_dai_stream *io,
96 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
103 adinr = rsnd_get_adinr_bit(mod, io) |
104 rsnd_runtime_channel_after_ctu(io);
106 /* Enable Digital Volume, Zero Cross Mute Mode */
110 if (rsnd_kctrl_vals(dvc->ren)) {
115 * use scale-downed Digital Volume
120 vrpdr = rsnd_dvc_get_vrpdr(dvc);
121 vrdbr = rsnd_dvc_get_vrdbr(dvc);
124 /* Initialize operation */
125 rsnd_mod_write(mod, DVC_DVUIR, 1);
127 /* General Information */
128 rsnd_mod_write(mod, DVC_ADINR, adinr);
129 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
131 /* Volume Ramp Parameter */
132 rsnd_mod_write(mod, DVC_VRCTR, vrctr);
133 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
134 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
136 /* Digital Volume Function Parameter */
137 rsnd_dvc_volume_parameter(io, mod);
139 /* cancel operation */
140 rsnd_mod_write(mod, DVC_DVUIR, 0);
143 static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
144 struct rsnd_mod *mod)
146 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
152 for (i = 0; i < rsnd_kctrl_size(dvc->mute); i++)
153 zcmcr |= (!!rsnd_kctrl_valm(dvc->mute, i)) << i;
155 if (rsnd_kctrl_vals(dvc->ren)) {
156 vrpdr = rsnd_dvc_get_vrpdr(dvc);
157 vrdbr = rsnd_dvc_get_vrdbr(dvc);
160 /* Disable DVC Register access */
161 rsnd_mod_write(mod, DVC_DVUER, 0);
163 /* Zero Cross Mute Function */
164 rsnd_mod_write(mod, DVC_ZCMCR, zcmcr);
166 /* Volume Ramp Function */
167 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
168 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
169 /* add DVC_VRWTR here */
171 /* Digital Volume Function Parameter */
172 rsnd_dvc_volume_parameter(io, mod);
174 /* Enable DVC Register access */
175 rsnd_mod_write(mod, DVC_DVUER, 1);
178 static int rsnd_dvc_probe_(struct rsnd_mod *mod,
179 struct rsnd_dai_stream *io,
180 struct rsnd_priv *priv)
182 return rsnd_cmd_attach(io, rsnd_mod_id(mod));
185 static int rsnd_dvc_init(struct rsnd_mod *mod,
186 struct rsnd_dai_stream *io,
187 struct rsnd_priv *priv)
191 ret = rsnd_mod_power_on(mod);
195 rsnd_dvc_activation(mod);
197 rsnd_dvc_volume_init(io, mod);
199 rsnd_dvc_volume_update(io, mod);
204 static int rsnd_dvc_quit(struct rsnd_mod *mod,
205 struct rsnd_dai_stream *io,
206 struct rsnd_priv *priv)
210 rsnd_mod_power_off(mod);
215 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
216 struct rsnd_dai_stream *io,
217 struct snd_soc_pcm_runtime *rtd)
219 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
220 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
221 int is_play = rsnd_io_is_play(io);
222 int channels = rsnd_rdai_channels_get(rdai);
226 ret = rsnd_kctrl_new_m(mod, io, rtd,
228 "DVC Out Playback Volume" : "DVC In Capture Volume",
229 rsnd_kctrl_accept_anytime,
230 rsnd_dvc_volume_update,
231 &dvc->volume, channels,
237 ret = rsnd_kctrl_new_m(mod, io, rtd,
239 "DVC Out Mute Switch" : "DVC In Mute Switch",
240 rsnd_kctrl_accept_anytime,
241 rsnd_dvc_volume_update,
242 &dvc->mute, channels,
248 ret = rsnd_kctrl_new_s(mod, io, rtd,
250 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
251 rsnd_kctrl_accept_anytime,
252 rsnd_dvc_volume_update,
257 ret = rsnd_kctrl_new_e(mod, io, rtd,
259 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
260 rsnd_kctrl_accept_anytime,
261 rsnd_dvc_volume_update,
264 VOLUME_RAMP_MAX_DVC);
268 ret = rsnd_kctrl_new_e(mod, io, rtd,
270 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
271 rsnd_kctrl_accept_anytime,
272 rsnd_dvc_volume_update,
275 VOLUME_RAMP_MAX_DVC);
283 static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
284 struct rsnd_mod *mod)
286 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
288 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
289 DVC_NAME, mod, "tx");
292 #ifdef CONFIG_DEBUG_FS
293 static void rsnd_dvc_debug_info(struct seq_file *m,
294 struct rsnd_dai_stream *io,
295 struct rsnd_mod *mod)
297 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
298 0xe00 + rsnd_mod_id(mod) * 0x100, 0x60);
300 #define DEBUG_INFO .debug_info = rsnd_dvc_debug_info
305 static struct rsnd_mod_ops rsnd_dvc_ops = {
307 .dma_req = rsnd_dvc_dma_req,
308 .probe = rsnd_dvc_probe_,
309 .init = rsnd_dvc_init,
310 .quit = rsnd_dvc_quit,
311 .pcm_new = rsnd_dvc_pcm_new,
312 .get_status = rsnd_mod_get_status,
316 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
318 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
321 return rsnd_mod_get(rsnd_dvc_get(priv, id));
324 int rsnd_dvc_probe(struct rsnd_priv *priv)
326 struct device_node *node;
327 struct device_node *np;
328 struct device *dev = rsnd_priv_to_dev(priv);
329 struct rsnd_dvc *dvc;
331 char name[RSND_DVC_NAME_SIZE];
334 /* This driver doesn't support Gen1 at this point */
335 if (rsnd_is_gen1(priv))
338 node = rsnd_dvc_of_node(priv);
340 return 0; /* not used is not error */
342 nr = of_get_child_count(node);
345 goto rsnd_dvc_probe_done;
348 dvc = devm_kcalloc(dev, nr, sizeof(*dvc), GFP_KERNEL);
351 goto rsnd_dvc_probe_done;
359 for_each_child_of_node(node, np) {
360 dvc = rsnd_dvc_get(priv, i);
362 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
365 clk = devm_clk_get(dev, name);
369 goto rsnd_dvc_probe_done;
372 ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
373 clk, RSND_MOD_DVC, i);
376 goto rsnd_dvc_probe_done;
388 void rsnd_dvc_remove(struct rsnd_priv *priv)
390 struct rsnd_dvc *dvc;
393 for_each_rsnd_dvc(dvc, priv, i) {
394 rsnd_mod_quit(rsnd_mod_get(dvc));