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"
18 struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
20 struct rsnd_kctrl_cfg_m volume;
21 struct rsnd_kctrl_cfg_m mute;
22 struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
23 struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
24 struct rsnd_kctrl_cfg_s rdown; /* Ramp Rate Down */
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_soft_reset(struct rsnd_mod *mod)
69 rsnd_mod_write(mod, DVC_SWRSR, 0);
70 rsnd_mod_write(mod, DVC_SWRSR, 1);
73 #define rsnd_dvc_initialize_lock(mod) __rsnd_dvc_initialize_lock(mod, 1)
74 #define rsnd_dvc_initialize_unlock(mod) __rsnd_dvc_initialize_lock(mod, 0)
75 static void __rsnd_dvc_initialize_lock(struct rsnd_mod *mod, u32 enable)
77 rsnd_mod_write(mod, DVC_DVUIR, enable);
80 static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
83 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
84 u32 val[RSND_DVC_CHANNELS];
89 for (i = 0; i < dvc->mute.cfg.size; i++)
90 mute |= (!!dvc->mute.cfg.val[i]) << i;
92 /* Disable DVC Register access */
93 rsnd_mod_write(mod, DVC_DVUER, 0);
99 /* Digital Volume Max */
100 for (i = 0; i < RSND_DVC_CHANNELS; i++)
101 val[i] = dvc->volume.cfg.max;
103 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
104 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
108 * use scale-downed Digital Volume
112 rsnd_mod_write(mod, DVC_VRDBR,
113 0x3ff - (dvc->volume.val[0] >> 13));
116 for (i = 0; i < RSND_DVC_CHANNELS; i++)
117 val[i] = dvc->volume.val[i];
120 /* Enable Digital Volume */
122 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
123 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
128 rsnd_mod_write(mod, DVC_ZCMCR, mute);
131 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
133 /* Enable DVC Register access */
134 rsnd_mod_write(mod, DVC_DVUER, 1);
137 static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
138 struct rsnd_dai_stream *io,
139 struct rsnd_priv *priv)
141 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
143 rsnd_kctrl_remove(dvc->volume);
144 rsnd_kctrl_remove(dvc->mute);
145 rsnd_kctrl_remove(dvc->ren);
146 rsnd_kctrl_remove(dvc->rup);
147 rsnd_kctrl_remove(dvc->rdown);
152 static int rsnd_dvc_init(struct rsnd_mod *mod,
153 struct rsnd_dai_stream *io,
154 struct rsnd_priv *priv)
156 rsnd_mod_power_on(mod);
158 rsnd_dvc_soft_reset(mod);
160 rsnd_dvc_initialize_lock(mod);
162 rsnd_path_parse(priv, io);
164 rsnd_mod_write(mod, DVC_ADINR, rsnd_get_adinr_bit(mod, io));
167 rsnd_dvc_volume_update(io, mod);
169 rsnd_adg_set_cmd_timsel_gen2(mod, io);
174 static int rsnd_dvc_quit(struct rsnd_mod *mod,
175 struct rsnd_dai_stream *io,
176 struct rsnd_priv *priv)
178 rsnd_mod_power_off(mod);
183 static int rsnd_dvc_start(struct rsnd_mod *mod,
184 struct rsnd_dai_stream *io,
185 struct rsnd_priv *priv)
187 rsnd_dvc_initialize_unlock(mod);
189 rsnd_mod_write(mod, CMD_CTRL, 0x10);
194 static int rsnd_dvc_stop(struct rsnd_mod *mod,
195 struct rsnd_dai_stream *io,
196 struct rsnd_priv *priv)
198 rsnd_mod_write(mod, CMD_CTRL, 0);
203 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
204 struct rsnd_dai_stream *io,
205 struct snd_soc_pcm_runtime *rtd)
207 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
208 int is_play = rsnd_io_is_play(io);
212 ret = rsnd_kctrl_new_m(mod, io, rtd,
214 "DVC Out Playback Volume" : "DVC In Capture Volume",
215 rsnd_dvc_volume_update,
216 &dvc->volume, 0x00800000 - 1);
221 ret = rsnd_kctrl_new_m(mod, io, rtd,
223 "DVC Out Mute Switch" : "DVC In Mute Switch",
224 rsnd_dvc_volume_update,
230 ret = rsnd_kctrl_new_s(mod, io, rtd,
232 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
233 rsnd_dvc_volume_update,
238 ret = rsnd_kctrl_new_e(mod, io, rtd,
240 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
242 rsnd_dvc_volume_update,
243 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
247 ret = rsnd_kctrl_new_e(mod, io, rtd,
249 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
251 rsnd_dvc_volume_update,
252 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
260 static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
261 struct rsnd_mod *mod)
263 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
265 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
269 static struct rsnd_mod_ops rsnd_dvc_ops = {
271 .dma_req = rsnd_dvc_dma_req,
272 .remove = rsnd_dvc_remove_gen2,
273 .init = rsnd_dvc_init,
274 .quit = rsnd_dvc_quit,
275 .start = rsnd_dvc_start,
276 .stop = rsnd_dvc_stop,
277 .pcm_new = rsnd_dvc_pcm_new,
280 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
282 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
285 return rsnd_mod_get((struct rsnd_dvc *)(priv->dvc) + id);
288 static void rsnd_of_parse_dvc(struct platform_device *pdev,
289 const struct rsnd_of_data *of_data,
290 struct rsnd_priv *priv)
292 struct device_node *node;
293 struct rsnd_dvc_platform_info *dvc_info;
294 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
295 struct device *dev = &pdev->dev;
301 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
305 nr = of_get_child_count(node);
307 goto rsnd_of_parse_dvc_end;
309 dvc_info = devm_kzalloc(dev,
310 sizeof(struct rsnd_dvc_platform_info) * nr,
313 dev_err(dev, "dvc info allocation error\n");
314 goto rsnd_of_parse_dvc_end;
317 info->dvc_info = dvc_info;
318 info->dvc_info_nr = nr;
320 rsnd_of_parse_dvc_end:
324 int rsnd_dvc_probe(struct platform_device *pdev,
325 const struct rsnd_of_data *of_data,
326 struct rsnd_priv *priv)
328 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
329 struct device *dev = rsnd_priv_to_dev(priv);
330 struct rsnd_dvc *dvc;
332 char name[RSND_DVC_NAME_SIZE];
335 /* This driver doesn't support Gen1 at this point */
336 if (rsnd_is_gen1(priv))
339 rsnd_of_parse_dvc(pdev, of_data, priv);
341 nr = info->dvc_info_nr;
345 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
352 for_each_rsnd_dvc(dvc, priv, i) {
353 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
356 clk = devm_clk_get(dev, name);
360 dvc->info = &info->dvc_info[i];
362 ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
363 clk, RSND_MOD_DVC, i);
371 void rsnd_dvc_remove(struct platform_device *pdev,
372 struct rsnd_priv *priv)
374 struct rsnd_dvc *dvc;
377 for_each_rsnd_dvc(dvc, priv, i) {
378 rsnd_mod_quit(rsnd_mod_get(dvc));