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"
24 struct rsnd_dvc_cfg_m {
25 struct rsnd_dvc_cfg cfg;
26 u32 val[RSND_DVC_CHANNELS];
30 struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
33 struct rsnd_dvc_cfg_m volume;
34 struct rsnd_dvc_cfg_m mute;
37 #define rsnd_mod_to_dvc(_mod) \
38 container_of((_mod), struct rsnd_dvc, mod)
40 #define for_each_rsnd_dvc(pos, priv, i) \
42 ((i) < rsnd_dvc_nr(priv)) && \
43 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
46 static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
48 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
53 for (i = 0; i < dvc->mute.cfg.size; i++)
54 mute |= (!!dvc->mute.cfg.val[i]) << i;
56 /* Disable DVC Register access */
57 rsnd_mod_write(mod, DVC_DVUER, 0);
59 /* Enable Digital Volume */
61 rsnd_mod_write(mod, DVC_VOL0R, dvc->volume.val[0]);
62 rsnd_mod_write(mod, DVC_VOL1R, dvc->volume.val[1]);
67 rsnd_mod_write(mod, DVC_ZCMCR, mute);
70 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
72 /* Enable DVC Register access */
73 rsnd_mod_write(mod, DVC_DVUER, 1);
76 static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
77 struct rsnd_dai *rdai)
79 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
80 struct device *dev = rsnd_priv_to_dev(priv);
82 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
87 static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
88 struct rsnd_dai *rdai)
90 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
91 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
92 struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
93 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
94 struct device *dev = rsnd_priv_to_dev(priv);
95 int dvc_id = rsnd_mod_id(dvc_mod);
96 int src_id = rsnd_mod_id(src_mod);
106 if (src_id >= ARRAY_SIZE(route)) {
107 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
111 clk_prepare_enable(dvc->clk);
115 * it doesn't support CTU/MIX
117 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
119 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
120 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
122 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
124 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
127 rsnd_dvc_volume_update(dvc_mod);
129 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
131 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
136 static int rsnd_dvc_quit(struct rsnd_mod *mod,
137 struct rsnd_dai *rdai)
139 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
141 clk_disable_unprepare(dvc->clk);
146 static int rsnd_dvc_start(struct rsnd_mod *mod,
147 struct rsnd_dai *rdai)
149 rsnd_mod_write(mod, CMD_CTRL, 0x10);
154 static int rsnd_dvc_stop(struct rsnd_mod *mod,
155 struct rsnd_dai *rdai)
157 rsnd_mod_write(mod, CMD_CTRL, 0);
162 static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
163 struct snd_ctl_elem_info *uinfo)
165 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
167 uinfo->count = cfg->size;
168 uinfo->value.integer.min = 0;
169 uinfo->value.integer.max = cfg->max;
172 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
174 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
179 static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
180 struct snd_ctl_elem_value *ucontrol)
182 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
185 for (i = 0; i < cfg->size; i++)
186 ucontrol->value.integer.value[i] = cfg->val[i];
191 static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
192 struct snd_ctl_elem_value *ucontrol)
194 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
195 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
198 for (i = 0; i < cfg->size; i++) {
199 change |= (ucontrol->value.integer.value[i] != cfg->val[i]);
200 cfg->val[i] = ucontrol->value.integer.value[i];
204 rsnd_dvc_volume_update(mod);
209 static int __rsnd_dvc_pcm_new(struct rsnd_mod *mod,
210 struct rsnd_dai *rdai,
211 struct snd_soc_pcm_runtime *rtd,
212 const unsigned char *name,
213 struct rsnd_dvc_cfg *private)
215 struct snd_card *card = rtd->card->snd_card;
216 struct snd_kcontrol *kctrl;
217 struct snd_kcontrol_new knew = {
218 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
220 .info = rsnd_dvc_volume_info,
221 .get = rsnd_dvc_volume_get,
222 .put = rsnd_dvc_volume_put,
223 .private_value = (unsigned long)private,
227 kctrl = snd_ctl_new1(&knew, mod);
231 ret = snd_ctl_add(card, kctrl);
238 static int _rsnd_dvc_pcm_new_m(struct rsnd_mod *mod,
239 struct rsnd_dai *rdai,
240 struct snd_soc_pcm_runtime *rtd,
241 const unsigned char *name,
242 struct rsnd_dvc_cfg_m *private,
245 private->cfg.max = max;
246 private->cfg.size = RSND_DVC_CHANNELS;
247 private->cfg.val = private->val;
248 return __rsnd_dvc_pcm_new(mod, rdai, rtd, name, &private->cfg);
251 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
252 struct rsnd_dai *rdai,
253 struct snd_soc_pcm_runtime *rtd)
255 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
256 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
260 ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
261 rsnd_dai_is_play(rdai, io) ?
262 "DVC Out Playback Volume" : "DVC In Capture Volume",
263 &dvc->volume, 0x00800000 - 1);
268 ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
269 rsnd_dai_is_play(rdai, io) ?
270 "DVC Out Mute Switch" : "DVC In Mute Switch",
278 static struct rsnd_mod_ops rsnd_dvc_ops = {
280 .probe = rsnd_dvc_probe_gen2,
281 .init = rsnd_dvc_init,
282 .quit = rsnd_dvc_quit,
283 .start = rsnd_dvc_start,
284 .stop = rsnd_dvc_stop,
285 .pcm_new = rsnd_dvc_pcm_new,
288 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
290 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
293 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
296 static void rsnd_of_parse_dvc(struct platform_device *pdev,
297 const struct rsnd_of_data *of_data,
298 struct rsnd_priv *priv)
300 struct device_node *node;
301 struct rsnd_dvc_platform_info *dvc_info;
302 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
303 struct device *dev = &pdev->dev;
309 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
313 nr = of_get_child_count(node);
315 goto rsnd_of_parse_dvc_end;
317 dvc_info = devm_kzalloc(dev,
318 sizeof(struct rsnd_dvc_platform_info) * nr,
321 dev_err(dev, "dvc info allocation error\n");
322 goto rsnd_of_parse_dvc_end;
325 info->dvc_info = dvc_info;
326 info->dvc_info_nr = nr;
328 rsnd_of_parse_dvc_end:
332 int rsnd_dvc_probe(struct platform_device *pdev,
333 const struct rsnd_of_data *of_data,
334 struct rsnd_priv *priv)
336 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
337 struct device *dev = rsnd_priv_to_dev(priv);
338 struct rsnd_dvc *dvc;
340 char name[RSND_DVC_NAME_SIZE];
343 rsnd_of_parse_dvc(pdev, of_data, priv);
345 nr = info->dvc_info_nr;
349 /* This driver doesn't support Gen1 at this point */
350 if (rsnd_is_gen1(priv)) {
351 dev_warn(dev, "CMD is not supported on Gen1\n");
355 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
357 dev_err(dev, "CMD allocate failed\n");
364 for_each_rsnd_dvc(dvc, priv, i) {
365 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
368 clk = devm_clk_get(dev, name);
372 dvc->info = &info->dvc_info[i];
375 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
377 dev_dbg(dev, "CMD%d probed\n", i);