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 */
21 struct rsnd_kctrl_cfg_m volume;
22 struct rsnd_kctrl_cfg_m mute;
23 struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
24 struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
25 struct rsnd_kctrl_cfg_s rdown; /* Ramp Rate Down */
28 #define rsnd_mod_to_dvc(_mod) \
29 container_of((_mod), struct rsnd_dvc, mod)
31 #define for_each_rsnd_dvc(pos, priv, i) \
33 ((i) < rsnd_dvc_nr(priv)) && \
34 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
37 static const char const *dvc_ramp_rate[] = {
38 "128 dB/1 step", /* 00000 */
39 "64 dB/1 step", /* 00001 */
40 "32 dB/1 step", /* 00010 */
41 "16 dB/1 step", /* 00011 */
42 "8 dB/1 step", /* 00100 */
43 "4 dB/1 step", /* 00101 */
44 "2 dB/1 step", /* 00110 */
45 "1 dB/1 step", /* 00111 */
46 "0.5 dB/1 step", /* 01000 */
47 "0.25 dB/1 step", /* 01001 */
48 "0.125 dB/1 step", /* 01010 */
49 "0.125 dB/2 steps", /* 01011 */
50 "0.125 dB/4 steps", /* 01100 */
51 "0.125 dB/8 steps", /* 01101 */
52 "0.125 dB/16 steps", /* 01110 */
53 "0.125 dB/32 steps", /* 01111 */
54 "0.125 dB/64 steps", /* 10000 */
55 "0.125 dB/128 steps", /* 10001 */
56 "0.125 dB/256 steps", /* 10010 */
57 "0.125 dB/512 steps", /* 10011 */
58 "0.125 dB/1024 steps", /* 10100 */
59 "0.125 dB/2048 steps", /* 10101 */
60 "0.125 dB/4096 steps", /* 10110 */
61 "0.125 dB/8192 steps", /* 10111 */
64 static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
66 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
67 u32 val[RSND_DVC_CHANNELS];
72 for (i = 0; i < dvc->mute.cfg.size; i++)
73 mute |= (!!dvc->mute.cfg.val[i]) << i;
75 /* Disable DVC Register access */
76 rsnd_mod_write(mod, DVC_DVUER, 0);
82 /* Digital Volume Max */
83 for (i = 0; i < RSND_DVC_CHANNELS; i++)
84 val[i] = dvc->volume.cfg.max;
86 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
87 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
91 * use scale-downed Digital Volume
95 rsnd_mod_write(mod, DVC_VRDBR,
96 0x3ff - (dvc->volume.val[0] >> 13));
99 for (i = 0; i < RSND_DVC_CHANNELS; i++)
100 val[i] = dvc->volume.val[i];
103 /* Enable Digital Volume */
105 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
106 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
111 rsnd_mod_write(mod, DVC_ZCMCR, mute);
114 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
116 /* Enable DVC Register access */
117 rsnd_mod_write(mod, DVC_DVUER, 1);
120 static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
121 struct rsnd_dai *rdai)
123 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
124 struct device *dev = rsnd_priv_to_dev(priv);
126 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
127 rsnd_mod_name(mod), rsnd_mod_id(mod));
132 static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
133 struct rsnd_dai *rdai)
135 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
136 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
137 struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
138 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
139 struct device *dev = rsnd_priv_to_dev(priv);
140 int dvc_id = rsnd_mod_id(dvc_mod);
141 int src_id = rsnd_mod_id(src_mod);
151 if (src_id >= ARRAY_SIZE(route)) {
152 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
156 clk_prepare_enable(dvc->clk);
160 * it doesn't support CTU/MIX
162 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
164 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
165 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
167 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
169 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
172 rsnd_dvc_volume_update(dvc_mod);
174 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
176 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
181 static int rsnd_dvc_quit(struct rsnd_mod *mod,
182 struct rsnd_dai *rdai)
184 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
186 clk_disable_unprepare(dvc->clk);
191 static int rsnd_dvc_start(struct rsnd_mod *mod,
192 struct rsnd_dai *rdai)
194 rsnd_mod_write(mod, CMD_CTRL, 0x10);
199 static int rsnd_dvc_stop(struct rsnd_mod *mod,
200 struct rsnd_dai *rdai)
202 rsnd_mod_write(mod, CMD_CTRL, 0);
207 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
208 struct rsnd_dai *rdai,
209 struct snd_soc_pcm_runtime *rtd)
211 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
212 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
216 ret = rsnd_kctrl_new_m(mod, rdai, rtd,
217 rsnd_dai_is_play(rdai, io) ?
218 "DVC Out Playback Volume" : "DVC In Capture Volume",
219 rsnd_dvc_volume_update,
220 &dvc->volume, 0x00800000 - 1);
225 ret = rsnd_kctrl_new_m(mod, rdai, rtd,
226 rsnd_dai_is_play(rdai, io) ?
227 "DVC Out Mute Switch" : "DVC In Mute Switch",
228 rsnd_dvc_volume_update,
234 ret = rsnd_kctrl_new_s(mod, rdai, rtd,
235 rsnd_dai_is_play(rdai, io) ?
236 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
237 rsnd_dvc_volume_update,
242 ret = rsnd_kctrl_new_e(mod, rdai, rtd,
243 rsnd_dai_is_play(rdai, io) ?
244 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
246 rsnd_dvc_volume_update,
247 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
251 ret = rsnd_kctrl_new_e(mod, rdai, rtd,
252 rsnd_dai_is_play(rdai, io) ?
253 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
255 rsnd_dvc_volume_update,
256 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
264 static struct rsnd_mod_ops rsnd_dvc_ops = {
266 .probe = rsnd_dvc_probe_gen2,
267 .init = rsnd_dvc_init,
268 .quit = rsnd_dvc_quit,
269 .start = rsnd_dvc_start,
270 .stop = rsnd_dvc_stop,
271 .pcm_new = rsnd_dvc_pcm_new,
274 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
276 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
279 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
282 static void rsnd_of_parse_dvc(struct platform_device *pdev,
283 const struct rsnd_of_data *of_data,
284 struct rsnd_priv *priv)
286 struct device_node *node;
287 struct rsnd_dvc_platform_info *dvc_info;
288 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
289 struct device *dev = &pdev->dev;
295 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
299 nr = of_get_child_count(node);
301 goto rsnd_of_parse_dvc_end;
303 dvc_info = devm_kzalloc(dev,
304 sizeof(struct rsnd_dvc_platform_info) * nr,
307 dev_err(dev, "dvc info allocation error\n");
308 goto rsnd_of_parse_dvc_end;
311 info->dvc_info = dvc_info;
312 info->dvc_info_nr = nr;
314 rsnd_of_parse_dvc_end:
318 int rsnd_dvc_probe(struct platform_device *pdev,
319 const struct rsnd_of_data *of_data,
320 struct rsnd_priv *priv)
322 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
323 struct device *dev = rsnd_priv_to_dev(priv);
324 struct rsnd_dvc *dvc;
326 char name[RSND_DVC_NAME_SIZE];
329 rsnd_of_parse_dvc(pdev, of_data, priv);
331 nr = info->dvc_info_nr;
335 /* This driver doesn't support Gen1 at this point */
336 if (rsnd_is_gen1(priv)) {
337 dev_warn(dev, "CMD is not supported on Gen1\n");
341 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
343 dev_err(dev, "CMD allocate failed\n");
350 for_each_rsnd_dvc(dvc, priv, i) {
351 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
354 clk = devm_clk_get(dev, name);
358 dvc->info = &info->dvc_info[i];
361 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
363 dev_dbg(dev, "CMD%d probed\n", i);