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
14 #define RSND_DVC_CHANNELS 2
16 #define DVC_NAME "dvc"
22 const char * const *texts;
25 struct rsnd_dvc_cfg_m {
26 struct rsnd_dvc_cfg cfg;
27 u32 val[RSND_DVC_CHANNELS];
30 struct rsnd_dvc_cfg_s {
31 struct rsnd_dvc_cfg cfg;
36 struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
39 struct rsnd_dvc_cfg_m volume;
40 struct rsnd_dvc_cfg_m mute;
41 struct rsnd_dvc_cfg_s ren; /* Ramp Enable */
42 struct rsnd_dvc_cfg_s rup; /* Ramp Rate Up */
43 struct rsnd_dvc_cfg_s rdown; /* Ramp Rate Down */
46 #define rsnd_mod_to_dvc(_mod) \
47 container_of((_mod), struct rsnd_dvc, mod)
49 #define for_each_rsnd_dvc(pos, priv, i) \
51 ((i) < rsnd_dvc_nr(priv)) && \
52 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
55 static const char const *dvc_ramp_rate[] = {
56 "128 dB/1 step", /* 00000 */
57 "64 dB/1 step", /* 00001 */
58 "32 dB/1 step", /* 00010 */
59 "16 dB/1 step", /* 00011 */
60 "8 dB/1 step", /* 00100 */
61 "4 dB/1 step", /* 00101 */
62 "2 dB/1 step", /* 00110 */
63 "1 dB/1 step", /* 00111 */
64 "0.5 dB/1 step", /* 01000 */
65 "0.25 dB/1 step", /* 01001 */
66 "0.125 dB/1 step", /* 01010 */
67 "0.125 dB/2 steps", /* 01011 */
68 "0.125 dB/4 steps", /* 01100 */
69 "0.125 dB/8 steps", /* 01101 */
70 "0.125 dB/16 steps", /* 01110 */
71 "0.125 dB/32 steps", /* 01111 */
72 "0.125 dB/64 steps", /* 10000 */
73 "0.125 dB/128 steps", /* 10001 */
74 "0.125 dB/256 steps", /* 10010 */
75 "0.125 dB/512 steps", /* 10011 */
76 "0.125 dB/1024 steps", /* 10100 */
77 "0.125 dB/2048 steps", /* 10101 */
78 "0.125 dB/4096 steps", /* 10110 */
79 "0.125 dB/8192 steps", /* 10111 */
82 static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
84 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
85 u32 val[RSND_DVC_CHANNELS];
90 for (i = 0; i < dvc->mute.cfg.size; i++)
91 mute |= (!!dvc->mute.cfg.val[i]) << i;
93 /* Disable DVC Register access */
94 rsnd_mod_write(mod, DVC_DVUER, 0);
100 /* Digital Volume Max */
101 for (i = 0; i < RSND_DVC_CHANNELS; i++)
102 val[i] = dvc->volume.cfg.max;
104 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
105 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
109 * use scale-downed Digital Volume
113 rsnd_mod_write(mod, DVC_VRDBR,
114 0x3ff - (dvc->volume.val[0] >> 13));
117 for (i = 0; i < RSND_DVC_CHANNELS; i++)
118 val[i] = dvc->volume.val[i];
121 /* Enable Digital Volume */
123 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
124 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
129 rsnd_mod_write(mod, DVC_ZCMCR, mute);
132 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
134 /* Enable DVC Register access */
135 rsnd_mod_write(mod, DVC_DVUER, 1);
138 static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
139 struct rsnd_dai *rdai)
141 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
142 struct device *dev = rsnd_priv_to_dev(priv);
144 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
145 rsnd_mod_name(mod), rsnd_mod_id(mod));
150 static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
151 struct rsnd_dai *rdai)
153 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
154 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
155 struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
156 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
157 struct device *dev = rsnd_priv_to_dev(priv);
158 int dvc_id = rsnd_mod_id(dvc_mod);
159 int src_id = rsnd_mod_id(src_mod);
169 if (src_id >= ARRAY_SIZE(route)) {
170 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
174 clk_prepare_enable(dvc->clk);
178 * it doesn't support CTU/MIX
180 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
182 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
183 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
185 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
187 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
190 rsnd_dvc_volume_update(dvc_mod);
192 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
194 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
199 static int rsnd_dvc_quit(struct rsnd_mod *mod,
200 struct rsnd_dai *rdai)
202 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
204 clk_disable_unprepare(dvc->clk);
209 static int rsnd_dvc_start(struct rsnd_mod *mod,
210 struct rsnd_dai *rdai)
212 rsnd_mod_write(mod, CMD_CTRL, 0x10);
217 static int rsnd_dvc_stop(struct rsnd_mod *mod,
218 struct rsnd_dai *rdai)
220 rsnd_mod_write(mod, CMD_CTRL, 0);
225 static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
226 struct snd_ctl_elem_info *uinfo)
228 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
231 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
232 uinfo->count = cfg->size;
233 uinfo->value.enumerated.items = cfg->max;
234 if (uinfo->value.enumerated.item >= cfg->max)
235 uinfo->value.enumerated.item = cfg->max - 1;
236 strlcpy(uinfo->value.enumerated.name,
237 cfg->texts[uinfo->value.enumerated.item],
238 sizeof(uinfo->value.enumerated.name));
240 uinfo->count = cfg->size;
241 uinfo->value.integer.min = 0;
242 uinfo->value.integer.max = cfg->max;
243 uinfo->type = (cfg->max == 1) ?
244 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
245 SNDRV_CTL_ELEM_TYPE_INTEGER;
251 static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
252 struct snd_ctl_elem_value *ucontrol)
254 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
257 for (i = 0; i < cfg->size; i++)
259 ucontrol->value.enumerated.item[i] = cfg->val[i];
261 ucontrol->value.integer.value[i] = cfg->val[i];
266 static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
267 struct snd_ctl_elem_value *ucontrol)
269 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
270 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
273 for (i = 0; i < cfg->size; i++) {
275 change |= (ucontrol->value.enumerated.item[i] != cfg->val[i]);
276 cfg->val[i] = ucontrol->value.enumerated.item[i];
278 change |= (ucontrol->value.integer.value[i] != cfg->val[i]);
279 cfg->val[i] = ucontrol->value.integer.value[i];
284 rsnd_dvc_volume_update(mod);
289 static int __rsnd_dvc_pcm_new(struct rsnd_mod *mod,
290 struct rsnd_dai *rdai,
291 struct snd_soc_pcm_runtime *rtd,
292 const unsigned char *name,
293 struct rsnd_dvc_cfg *private)
295 struct snd_card *card = rtd->card->snd_card;
296 struct snd_kcontrol *kctrl;
297 struct snd_kcontrol_new knew = {
298 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
300 .info = rsnd_dvc_volume_info,
301 .get = rsnd_dvc_volume_get,
302 .put = rsnd_dvc_volume_put,
303 .private_value = (unsigned long)private,
307 kctrl = snd_ctl_new1(&knew, mod);
311 ret = snd_ctl_add(card, kctrl);
318 static int _rsnd_dvc_pcm_new_m(struct rsnd_mod *mod,
319 struct rsnd_dai *rdai,
320 struct snd_soc_pcm_runtime *rtd,
321 const unsigned char *name,
322 struct rsnd_dvc_cfg_m *private,
325 private->cfg.max = max;
326 private->cfg.size = RSND_DVC_CHANNELS;
327 private->cfg.val = private->val;
328 return __rsnd_dvc_pcm_new(mod, rdai, rtd, name, &private->cfg);
331 static int _rsnd_dvc_pcm_new_s(struct rsnd_mod *mod,
332 struct rsnd_dai *rdai,
333 struct snd_soc_pcm_runtime *rtd,
334 const unsigned char *name,
335 struct rsnd_dvc_cfg_s *private,
338 private->cfg.max = max;
339 private->cfg.size = 1;
340 private->cfg.val = &private->val;
341 return __rsnd_dvc_pcm_new(mod, rdai, rtd, name, &private->cfg);
344 static int _rsnd_dvc_pcm_new_e(struct rsnd_mod *mod,
345 struct rsnd_dai *rdai,
346 struct snd_soc_pcm_runtime *rtd,
347 const unsigned char *name,
348 struct rsnd_dvc_cfg_s *private,
349 const char * const *texts,
352 private->cfg.max = max;
353 private->cfg.size = 1;
354 private->cfg.val = &private->val;
355 private->cfg.texts = texts;
356 return __rsnd_dvc_pcm_new(mod, rdai, rtd, name, &private->cfg);
359 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
360 struct rsnd_dai *rdai,
361 struct snd_soc_pcm_runtime *rtd)
363 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
364 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
368 ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
369 rsnd_dai_is_play(rdai, io) ?
370 "DVC Out Playback Volume" : "DVC In Capture Volume",
371 &dvc->volume, 0x00800000 - 1);
376 ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
377 rsnd_dai_is_play(rdai, io) ?
378 "DVC Out Mute Switch" : "DVC In Mute Switch",
384 ret = _rsnd_dvc_pcm_new_s(mod, rdai, rtd,
385 rsnd_dai_is_play(rdai, io) ?
386 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
391 ret = _rsnd_dvc_pcm_new_e(mod, rdai, rtd,
392 rsnd_dai_is_play(rdai, io) ?
393 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
395 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
399 ret = _rsnd_dvc_pcm_new_e(mod, rdai, rtd,
400 rsnd_dai_is_play(rdai, io) ?
401 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
403 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
411 static struct rsnd_mod_ops rsnd_dvc_ops = {
413 .probe = rsnd_dvc_probe_gen2,
414 .init = rsnd_dvc_init,
415 .quit = rsnd_dvc_quit,
416 .start = rsnd_dvc_start,
417 .stop = rsnd_dvc_stop,
418 .pcm_new = rsnd_dvc_pcm_new,
421 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
423 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
426 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
429 static void rsnd_of_parse_dvc(struct platform_device *pdev,
430 const struct rsnd_of_data *of_data,
431 struct rsnd_priv *priv)
433 struct device_node *node;
434 struct rsnd_dvc_platform_info *dvc_info;
435 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
436 struct device *dev = &pdev->dev;
442 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
446 nr = of_get_child_count(node);
448 goto rsnd_of_parse_dvc_end;
450 dvc_info = devm_kzalloc(dev,
451 sizeof(struct rsnd_dvc_platform_info) * nr,
454 dev_err(dev, "dvc info allocation error\n");
455 goto rsnd_of_parse_dvc_end;
458 info->dvc_info = dvc_info;
459 info->dvc_info_nr = nr;
461 rsnd_of_parse_dvc_end:
465 int rsnd_dvc_probe(struct platform_device *pdev,
466 const struct rsnd_of_data *of_data,
467 struct rsnd_priv *priv)
469 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
470 struct device *dev = rsnd_priv_to_dev(priv);
471 struct rsnd_dvc *dvc;
473 char name[RSND_DVC_NAME_SIZE];
476 rsnd_of_parse_dvc(pdev, of_data, priv);
478 nr = info->dvc_info_nr;
482 /* This driver doesn't support Gen1 at this point */
483 if (rsnd_is_gen1(priv)) {
484 dev_warn(dev, "CMD is not supported on Gen1\n");
488 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
490 dev_err(dev, "CMD allocate failed\n");
497 for_each_rsnd_dvc(dvc, priv, i) {
498 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
501 clk = devm_clk_get(dev, name);
505 dvc->info = &info->dvc_info[i];
508 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
510 dev_dbg(dev, "CMD%d probed\n", i);