2 * Renesas R-Car DVC support
4 * Copyright (C) 2014 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #define RSND_DVC_NAME_SIZE 16
15 #define DVC_NAME "dvc"
19 struct rsnd_kctrl_cfg_m volume;
20 struct rsnd_kctrl_cfg_m mute;
21 struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
22 struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
23 struct rsnd_kctrl_cfg_s rdown; /* Ramp Rate Down */
26 #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
27 #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
28 #define rsnd_dvc_of_node(priv) \
29 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,dvc")
31 #define rsnd_mod_to_dvc(_mod) \
32 container_of((_mod), struct rsnd_dvc, mod)
34 #define for_each_rsnd_dvc(pos, priv, i) \
36 ((i) < rsnd_dvc_nr(priv)) && \
37 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
40 static const char * const dvc_ramp_rate[] = {
41 "128 dB/1 step", /* 00000 */
42 "64 dB/1 step", /* 00001 */
43 "32 dB/1 step", /* 00010 */
44 "16 dB/1 step", /* 00011 */
45 "8 dB/1 step", /* 00100 */
46 "4 dB/1 step", /* 00101 */
47 "2 dB/1 step", /* 00110 */
48 "1 dB/1 step", /* 00111 */
49 "0.5 dB/1 step", /* 01000 */
50 "0.25 dB/1 step", /* 01001 */
51 "0.125 dB/1 step", /* 01010 */
52 "0.125 dB/2 steps", /* 01011 */
53 "0.125 dB/4 steps", /* 01100 */
54 "0.125 dB/8 steps", /* 01101 */
55 "0.125 dB/16 steps", /* 01110 */
56 "0.125 dB/32 steps", /* 01111 */
57 "0.125 dB/64 steps", /* 10000 */
58 "0.125 dB/128 steps", /* 10001 */
59 "0.125 dB/256 steps", /* 10010 */
60 "0.125 dB/512 steps", /* 10011 */
61 "0.125 dB/1024 steps", /* 10100 */
62 "0.125 dB/2048 steps", /* 10101 */
63 "0.125 dB/4096 steps", /* 10110 */
64 "0.125 dB/8192 steps", /* 10111 */
67 static void rsnd_dvc_activation(struct rsnd_mod *mod)
69 rsnd_mod_write(mod, DVC_SWRSR, 0);
70 rsnd_mod_write(mod, DVC_SWRSR, 1);
73 static void rsnd_dvc_halt(struct rsnd_mod *mod)
75 rsnd_mod_write(mod, DVC_DVUIR, 1);
76 rsnd_mod_write(mod, DVC_SWRSR, 0);
79 #define rsnd_dvc_get_vrpdr(dvc) (dvc->rup.val << 8 | dvc->rdown.val)
80 #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (dvc->volume.val[0] >> 13))
82 static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream *io,
85 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
86 u32 val[RSND_DVC_CHANNELS];
91 for (i = 0; i < RSND_DVC_CHANNELS; i++)
92 val[i] = dvc->volume.cfg.max;
94 for (i = 0; i < RSND_DVC_CHANNELS; i++)
95 val[i] = dvc->volume.val[i];
97 /* Enable Digital Volume */
98 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
99 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
100 rsnd_mod_write(mod, DVC_VOL2R, val[2]);
101 rsnd_mod_write(mod, DVC_VOL3R, val[3]);
102 rsnd_mod_write(mod, DVC_VOL4R, val[4]);
103 rsnd_mod_write(mod, DVC_VOL5R, val[5]);
104 rsnd_mod_write(mod, DVC_VOL6R, val[6]);
105 rsnd_mod_write(mod, DVC_VOL7R, val[7]);
108 static void rsnd_dvc_volume_init(struct rsnd_dai_stream *io,
109 struct rsnd_mod *mod)
111 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
118 adinr = rsnd_get_adinr_bit(mod, io) |
119 rsnd_get_adinr_chan(mod, io);
121 /* Enable Digital Volume, Zero Cross Mute Mode */
130 * use scale-downed Digital Volume
135 vrpdr = rsnd_dvc_get_vrpdr(dvc);
136 vrdbr = rsnd_dvc_get_vrdbr(dvc);
139 /* Initialize operation */
140 rsnd_mod_write(mod, DVC_DVUIR, 1);
142 /* General Information */
143 rsnd_mod_write(mod, DVC_ADINR, adinr);
144 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
146 /* Volume Ramp Parameter */
147 rsnd_mod_write(mod, DVC_VRCTR, vrctr);
148 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
149 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
151 /* Digital Volume Function Parameter */
152 rsnd_dvc_volume_parameter(io, mod);
154 /* cancel operation */
155 rsnd_mod_write(mod, DVC_DVUIR, 0);
158 static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
159 struct rsnd_mod *mod)
161 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
167 for (i = 0; i < dvc->mute.cfg.size; i++)
168 zcmcr |= (!!dvc->mute.cfg.val[i]) << i;
171 vrpdr = rsnd_dvc_get_vrpdr(dvc);
172 vrdbr = rsnd_dvc_get_vrdbr(dvc);
175 /* Disable DVC Register access */
176 rsnd_mod_write(mod, DVC_DVUER, 0);
178 /* Zero Cross Mute Function */
179 rsnd_mod_write(mod, DVC_ZCMCR, zcmcr);
181 /* Volume Ramp Function */
182 rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
183 rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
184 /* add DVC_VRWTR here */
186 /* Digital Volume Function Parameter */
187 rsnd_dvc_volume_parameter(io, mod);
189 /* Enable DVC Register access */
190 rsnd_mod_write(mod, DVC_DVUER, 1);
193 static int rsnd_dvc_probe_(struct rsnd_mod *mod,
194 struct rsnd_dai_stream *io,
195 struct rsnd_priv *priv)
197 return rsnd_cmd_attach(io, rsnd_mod_id(mod));
200 static int rsnd_dvc_remove_(struct rsnd_mod *mod,
201 struct rsnd_dai_stream *io,
202 struct rsnd_priv *priv)
204 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
206 rsnd_kctrl_remove(dvc->volume);
207 rsnd_kctrl_remove(dvc->mute);
208 rsnd_kctrl_remove(dvc->ren);
209 rsnd_kctrl_remove(dvc->rup);
210 rsnd_kctrl_remove(dvc->rdown);
215 static int rsnd_dvc_init(struct rsnd_mod *mod,
216 struct rsnd_dai_stream *io,
217 struct rsnd_priv *priv)
219 rsnd_mod_power_on(mod);
221 rsnd_dvc_activation(mod);
223 rsnd_dvc_volume_init(io, mod);
225 rsnd_dvc_volume_update(io, mod);
230 static int rsnd_dvc_quit(struct rsnd_mod *mod,
231 struct rsnd_dai_stream *io,
232 struct rsnd_priv *priv)
236 rsnd_mod_power_off(mod);
241 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
242 struct rsnd_dai_stream *io,
243 struct snd_soc_pcm_runtime *rtd)
245 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
246 int is_play = rsnd_io_is_play(io);
247 int slots = rsnd_get_slot(io);
251 ret = rsnd_kctrl_new_m(mod, io, rtd,
253 "DVC Out Playback Volume" : "DVC In Capture Volume",
254 rsnd_dvc_volume_update,
261 ret = rsnd_kctrl_new_m(mod, io, rtd,
263 "DVC Out Mute Switch" : "DVC In Mute Switch",
264 rsnd_dvc_volume_update,
271 ret = rsnd_kctrl_new_s(mod, io, rtd,
273 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
274 rsnd_dvc_volume_update,
279 ret = rsnd_kctrl_new_e(mod, io, rtd,
281 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
283 rsnd_dvc_volume_update,
284 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
288 ret = rsnd_kctrl_new_e(mod, io, rtd,
290 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
292 rsnd_dvc_volume_update,
293 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
301 static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
302 struct rsnd_mod *mod)
304 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
306 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
310 static struct rsnd_mod_ops rsnd_dvc_ops = {
312 .dma_req = rsnd_dvc_dma_req,
313 .probe = rsnd_dvc_probe_,
314 .remove = rsnd_dvc_remove_,
315 .init = rsnd_dvc_init,
316 .quit = rsnd_dvc_quit,
317 .pcm_new = rsnd_dvc_pcm_new,
320 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
322 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
325 return rsnd_mod_get(rsnd_dvc_get(priv, id));
328 int rsnd_dvc_probe(struct rsnd_priv *priv)
330 struct device_node *node;
331 struct device_node *np;
332 struct device *dev = rsnd_priv_to_dev(priv);
333 struct rsnd_dvc *dvc;
335 char name[RSND_DVC_NAME_SIZE];
338 /* This driver doesn't support Gen1 at this point */
339 if (rsnd_is_gen1(priv))
342 node = rsnd_dvc_of_node(priv);
344 return 0; /* not used is not error */
346 nr = of_get_child_count(node);
349 goto rsnd_dvc_probe_done;
352 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
355 goto rsnd_dvc_probe_done;
363 for_each_child_of_node(node, np) {
364 dvc = rsnd_dvc_get(priv, i);
366 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
369 clk = devm_clk_get(dev, name);
372 goto rsnd_dvc_probe_done;
375 ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
376 clk, RSND_MOD_DVC, i);
378 goto rsnd_dvc_probe_done;
389 void rsnd_dvc_remove(struct rsnd_priv *priv)
391 struct rsnd_dvc *dvc;
394 for_each_rsnd_dvc(dvc, priv, i) {
395 rsnd_mod_quit(rsnd_mod_get(dvc));