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 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
90 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
91 rsnd_mod_write(mod, DVC_VOL2R, val[2]);
92 rsnd_mod_write(mod, DVC_VOL3R, val[3]);
93 rsnd_mod_write(mod, DVC_VOL4R, val[4]);
94 rsnd_mod_write(mod, DVC_VOL5R, val[5]);
95 rsnd_mod_write(mod, DVC_VOL6R, val[6]);
96 rsnd_mod_write(mod, DVC_VOL7R, val[7]);
99 static void rsnd_dvc_volume_init(struct rsnd_dai_stream *io,
100 struct rsnd_mod *mod)
102 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
109 adinr = rsnd_get_adinr_bit(mod, io) |
110 rsnd_runtime_channel_after_ctu(io);
112 /* Enable Digital Volume, Zero Cross Mute Mode */
116 if (rsnd_kctrl_vals(dvc->ren)) {
121 * use scale-downed Digital Volume
126 vrpdr = rsnd_dvc_get_vrpdr(dvc);
127 vrdbr = rsnd_dvc_get_vrdbr(dvc);
130 /* Initialize operation */
131 rsnd_mod_write(mod, DVC_DVUIR, 1);
133 /* General Information */
134 rsnd_mod_write(mod, DVC_ADINR, adinr);
135 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
137 /* Volume Ramp Parameter */
138 rsnd_mod_write(mod, DVC_VRCTR, vrctr);
139 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
140 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
142 /* Digital Volume Function Parameter */
143 rsnd_dvc_volume_parameter(io, mod);
145 /* cancel operation */
146 rsnd_mod_write(mod, DVC_DVUIR, 0);
149 static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
150 struct rsnd_mod *mod)
152 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
158 for (i = 0; i < rsnd_kctrl_size(dvc->mute); i++)
159 zcmcr |= (!!rsnd_kctrl_valm(dvc->mute, i)) << i;
161 if (rsnd_kctrl_vals(dvc->ren)) {
162 vrpdr = rsnd_dvc_get_vrpdr(dvc);
163 vrdbr = rsnd_dvc_get_vrdbr(dvc);
166 /* Disable DVC Register access */
167 rsnd_mod_write(mod, DVC_DVUER, 0);
169 /* Zero Cross Mute Function */
170 rsnd_mod_write(mod, DVC_ZCMCR, zcmcr);
172 /* Volume Ramp Function */
173 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
174 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
175 /* add DVC_VRWTR here */
177 /* Digital Volume Function Parameter */
178 rsnd_dvc_volume_parameter(io, mod);
180 /* Enable DVC Register access */
181 rsnd_mod_write(mod, DVC_DVUER, 1);
184 static int rsnd_dvc_probe_(struct rsnd_mod *mod,
185 struct rsnd_dai_stream *io,
186 struct rsnd_priv *priv)
188 return rsnd_cmd_attach(io, rsnd_mod_id(mod));
191 static int rsnd_dvc_init(struct rsnd_mod *mod,
192 struct rsnd_dai_stream *io,
193 struct rsnd_priv *priv)
195 rsnd_mod_power_on(mod);
197 rsnd_dvc_activation(mod);
199 rsnd_dvc_volume_init(io, mod);
201 rsnd_dvc_volume_update(io, mod);
206 static int rsnd_dvc_quit(struct rsnd_mod *mod,
207 struct rsnd_dai_stream *io,
208 struct rsnd_priv *priv)
212 rsnd_mod_power_off(mod);
217 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
218 struct rsnd_dai_stream *io,
219 struct snd_soc_pcm_runtime *rtd)
221 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
222 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
223 int is_play = rsnd_io_is_play(io);
224 int channels = rsnd_rdai_channels_get(rdai);
228 ret = rsnd_kctrl_new_m(mod, io, rtd,
230 "DVC Out Playback Volume" : "DVC In Capture Volume",
231 rsnd_kctrl_accept_anytime,
232 rsnd_dvc_volume_update,
233 &dvc->volume, channels,
239 ret = rsnd_kctrl_new_m(mod, io, rtd,
241 "DVC Out Mute Switch" : "DVC In Mute Switch",
242 rsnd_kctrl_accept_anytime,
243 rsnd_dvc_volume_update,
244 &dvc->mute, channels,
250 ret = rsnd_kctrl_new_s(mod, io, rtd,
252 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
253 rsnd_kctrl_accept_anytime,
254 rsnd_dvc_volume_update,
259 ret = rsnd_kctrl_new_e(mod, io, rtd,
261 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
262 rsnd_kctrl_accept_anytime,
263 rsnd_dvc_volume_update,
266 VOLUME_RAMP_MAX_DVC);
270 ret = rsnd_kctrl_new_e(mod, io, rtd,
272 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
273 rsnd_kctrl_accept_anytime,
274 rsnd_dvc_volume_update,
277 VOLUME_RAMP_MAX_DVC);
285 static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
286 struct rsnd_mod *mod)
288 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
290 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
294 static struct rsnd_mod_ops rsnd_dvc_ops = {
296 .dma_req = rsnd_dvc_dma_req,
297 .probe = rsnd_dvc_probe_,
298 .init = rsnd_dvc_init,
299 .quit = rsnd_dvc_quit,
300 .pcm_new = rsnd_dvc_pcm_new,
303 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
305 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
308 return rsnd_mod_get(rsnd_dvc_get(priv, id));
311 int rsnd_dvc_probe(struct rsnd_priv *priv)
313 struct device_node *node;
314 struct device_node *np;
315 struct device *dev = rsnd_priv_to_dev(priv);
316 struct rsnd_dvc *dvc;
318 char name[RSND_DVC_NAME_SIZE];
321 /* This driver doesn't support Gen1 at this point */
322 if (rsnd_is_gen1(priv))
325 node = rsnd_dvc_of_node(priv);
327 return 0; /* not used is not error */
329 nr = of_get_child_count(node);
332 goto rsnd_dvc_probe_done;
335 dvc = devm_kcalloc(dev, nr, sizeof(*dvc), GFP_KERNEL);
338 goto rsnd_dvc_probe_done;
346 for_each_child_of_node(node, np) {
347 dvc = rsnd_dvc_get(priv, i);
349 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
352 clk = devm_clk_get(dev, name);
356 goto rsnd_dvc_probe_done;
359 ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
360 clk, rsnd_mod_get_status, RSND_MOD_DVC, i);
363 goto rsnd_dvc_probe_done;
375 void rsnd_dvc_remove(struct rsnd_priv *priv)
377 struct rsnd_dvc *dvc;
380 for_each_rsnd_dvc(dvc, priv, i) {
381 rsnd_mod_quit(rsnd_mod_get(dvc));