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_VOLUME_MAX 100
15 #define RSND_DVC_CHANNELS 2
17 #define DVC_NAME "dvc"
20 struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
23 u8 volume[RSND_DVC_CHANNELS];
24 u8 mute[RSND_DVC_CHANNELS];
27 #define rsnd_mod_to_dvc(_mod) \
28 container_of((_mod), struct rsnd_dvc, mod)
30 #define for_each_rsnd_dvc(pos, priv, i) \
32 ((i) < rsnd_dvc_nr(priv)) && \
33 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
36 static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
38 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
39 u32 max = (0x00800000 - 1);
40 u32 vol[RSND_DVC_CHANNELS];
44 for (i = 0; i < RSND_DVC_CHANNELS; i++) {
45 vol[i] = max / RSND_DVC_VOLUME_MAX * dvc->volume[i];
46 mute |= (!!dvc->mute[i]) << i;
49 rsnd_mod_write(mod, DVC_VOL0R, vol[0]);
50 rsnd_mod_write(mod, DVC_VOL1R, vol[1]);
52 rsnd_mod_write(mod, DVC_ZCMCR, mute);
55 static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
56 struct rsnd_dai *rdai)
58 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
59 struct device *dev = rsnd_priv_to_dev(priv);
61 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
66 static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
67 struct rsnd_dai *rdai)
69 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
70 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
71 struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
72 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
73 struct device *dev = rsnd_priv_to_dev(priv);
74 int dvc_id = rsnd_mod_id(dvc_mod);
75 int src_id = rsnd_mod_id(src_mod);
85 if (src_id >= ARRAY_SIZE(route)) {
86 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
90 clk_prepare_enable(dvc->clk);
94 * it doesn't support CTU/MIX
96 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
98 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
99 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
101 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
103 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
105 /* enable Volume / Mute */
106 rsnd_mod_write(dvc_mod, DVC_DVUCR, 0x101);
109 rsnd_dvc_volume_update(dvc_mod);
111 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
113 rsnd_mod_write(dvc_mod, DVC_DVUER, 1);
115 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
120 static int rsnd_dvc_quit(struct rsnd_mod *mod,
121 struct rsnd_dai *rdai)
123 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
125 clk_disable_unprepare(dvc->clk);
130 static int rsnd_dvc_start(struct rsnd_mod *mod,
131 struct rsnd_dai *rdai)
133 rsnd_mod_write(mod, CMD_CTRL, 0x10);
138 static int rsnd_dvc_stop(struct rsnd_mod *mod,
139 struct rsnd_dai *rdai)
141 rsnd_mod_write(mod, CMD_CTRL, 0);
146 static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
147 struct snd_ctl_elem_info *uinfo)
149 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
150 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
151 u8 *val = (u8 *)kctrl->private_value;
153 uinfo->count = RSND_DVC_CHANNELS;
154 uinfo->value.integer.min = 0;
156 if (val == dvc->volume) {
157 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
158 uinfo->value.integer.max = RSND_DVC_VOLUME_MAX;
160 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
161 uinfo->value.integer.max = 1;
167 static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
168 struct snd_ctl_elem_value *ucontrol)
170 u8 *val = (u8 *)kctrl->private_value;
173 for (i = 0; i < RSND_DVC_CHANNELS; i++)
174 ucontrol->value.integer.value[i] = val[i];
179 static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
180 struct snd_ctl_elem_value *ucontrol)
182 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
183 u8 *val = (u8 *)kctrl->private_value;
186 for (i = 0; i < RSND_DVC_CHANNELS; i++) {
187 change |= (ucontrol->value.integer.value[i] != val[i]);
188 val[i] = ucontrol->value.integer.value[i];
192 rsnd_dvc_volume_update(mod);
197 static int __rsnd_dvc_pcm_new(struct rsnd_mod *mod,
198 struct rsnd_dai *rdai,
199 struct snd_soc_pcm_runtime *rtd,
200 const unsigned char *name,
203 struct snd_card *card = rtd->card->snd_card;
204 struct snd_kcontrol *kctrl;
205 struct snd_kcontrol_new knew = {
206 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
208 .info = rsnd_dvc_volume_info,
209 .get = rsnd_dvc_volume_get,
210 .put = rsnd_dvc_volume_put,
211 .private_value = (unsigned long)private,
215 kctrl = snd_ctl_new1(&knew, mod);
219 ret = snd_ctl_add(card, kctrl);
226 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
227 struct rsnd_dai *rdai,
228 struct snd_soc_pcm_runtime *rtd)
230 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
231 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
235 ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
236 rsnd_dai_is_play(rdai, io) ?
237 "DVC Out Playback Volume" : "DVC In Capture Volume",
243 ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
244 rsnd_dai_is_play(rdai, io) ?
245 "DVC Out Mute Switch" : "DVC In Mute Switch",
253 static struct rsnd_mod_ops rsnd_dvc_ops = {
255 .probe = rsnd_dvc_probe_gen2,
256 .init = rsnd_dvc_init,
257 .quit = rsnd_dvc_quit,
258 .start = rsnd_dvc_start,
259 .stop = rsnd_dvc_stop,
260 .pcm_new = rsnd_dvc_pcm_new,
263 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
265 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
268 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
271 static void rsnd_of_parse_dvc(struct platform_device *pdev,
272 const struct rsnd_of_data *of_data,
273 struct rsnd_priv *priv)
275 struct device_node *node;
276 struct rsnd_dvc_platform_info *dvc_info;
277 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
278 struct device *dev = &pdev->dev;
284 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
288 nr = of_get_child_count(node);
290 goto rsnd_of_parse_dvc_end;
292 dvc_info = devm_kzalloc(dev,
293 sizeof(struct rsnd_dvc_platform_info) * nr,
296 dev_err(dev, "dvc info allocation error\n");
297 goto rsnd_of_parse_dvc_end;
300 info->dvc_info = dvc_info;
301 info->dvc_info_nr = nr;
303 rsnd_of_parse_dvc_end:
307 int rsnd_dvc_probe(struct platform_device *pdev,
308 const struct rsnd_of_data *of_data,
309 struct rsnd_priv *priv)
311 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
312 struct device *dev = rsnd_priv_to_dev(priv);
313 struct rsnd_dvc *dvc;
315 char name[RSND_DVC_NAME_SIZE];
318 rsnd_of_parse_dvc(pdev, of_data, priv);
320 nr = info->dvc_info_nr;
324 /* This driver doesn't support Gen1 at this point */
325 if (rsnd_is_gen1(priv)) {
326 dev_warn(dev, "CMD is not supported on Gen1\n");
330 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
332 dev_err(dev, "CMD allocate failed\n");
339 for_each_rsnd_dvc(dvc, priv, i) {
340 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
343 clk = devm_clk_get(dev, name);
347 dvc->info = &info->dvc_info[i];
350 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
352 dev_dbg(dev, "CMD%d probed\n", i);