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"
20 u32 val[RSND_DVC_CHANNELS];
24 struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
27 struct rsnd_dvc_cfg volume;
28 struct rsnd_dvc_cfg mute;
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 void rsnd_dvc_volume_update(struct rsnd_mod *mod)
42 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
46 for (i = 0; i < RSND_DVC_CHANNELS; i++) {
47 mute |= (!!dvc->mute.val[i]) << i;
50 rsnd_mod_write(mod, DVC_VOL0R, dvc->volume.val[0]);
51 rsnd_mod_write(mod, DVC_VOL1R, dvc->volume.val[1]);
53 rsnd_mod_write(mod, DVC_ZCMCR, mute);
56 static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
57 struct rsnd_dai *rdai)
59 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
60 struct device *dev = rsnd_priv_to_dev(priv);
62 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
67 static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
68 struct rsnd_dai *rdai)
70 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
71 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
72 struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
73 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
74 struct device *dev = rsnd_priv_to_dev(priv);
75 int dvc_id = rsnd_mod_id(dvc_mod);
76 int src_id = rsnd_mod_id(src_mod);
86 if (src_id >= ARRAY_SIZE(route)) {
87 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
91 clk_prepare_enable(dvc->clk);
95 * it doesn't support CTU/MIX
97 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
99 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
100 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
102 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
104 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
106 /* enable Volume / Mute */
107 rsnd_mod_write(dvc_mod, DVC_DVUCR, 0x101);
110 rsnd_dvc_volume_update(dvc_mod);
112 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
114 rsnd_mod_write(dvc_mod, DVC_DVUER, 1);
116 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
121 static int rsnd_dvc_quit(struct rsnd_mod *mod,
122 struct rsnd_dai *rdai)
124 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
126 clk_disable_unprepare(dvc->clk);
131 static int rsnd_dvc_start(struct rsnd_mod *mod,
132 struct rsnd_dai *rdai)
134 rsnd_mod_write(mod, CMD_CTRL, 0x10);
139 static int rsnd_dvc_stop(struct rsnd_mod *mod,
140 struct rsnd_dai *rdai)
142 rsnd_mod_write(mod, CMD_CTRL, 0);
147 static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
148 struct snd_ctl_elem_info *uinfo)
150 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
152 uinfo->count = RSND_DVC_CHANNELS;
153 uinfo->value.integer.min = 0;
154 uinfo->value.integer.max = cfg->max;
157 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
159 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
164 static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
165 struct snd_ctl_elem_value *ucontrol)
167 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
170 for (i = 0; i < RSND_DVC_CHANNELS; i++)
171 ucontrol->value.integer.value[i] = cfg->val[i];
176 static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
177 struct snd_ctl_elem_value *ucontrol)
179 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
180 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
183 for (i = 0; i < RSND_DVC_CHANNELS; i++) {
184 change |= (ucontrol->value.integer.value[i] != cfg->val[i]);
185 cfg->val[i] = ucontrol->value.integer.value[i];
189 rsnd_dvc_volume_update(mod);
194 static int __rsnd_dvc_pcm_new(struct rsnd_mod *mod,
195 struct rsnd_dai *rdai,
196 struct snd_soc_pcm_runtime *rtd,
197 const unsigned char *name,
198 struct rsnd_dvc_cfg *private)
200 struct snd_card *card = rtd->card->snd_card;
201 struct snd_kcontrol *kctrl;
202 struct snd_kcontrol_new knew = {
203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
205 .info = rsnd_dvc_volume_info,
206 .get = rsnd_dvc_volume_get,
207 .put = rsnd_dvc_volume_put,
208 .private_value = (unsigned long)private,
212 kctrl = snd_ctl_new1(&knew, mod);
216 ret = snd_ctl_add(card, kctrl);
223 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
224 struct rsnd_dai *rdai,
225 struct snd_soc_pcm_runtime *rtd)
227 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
228 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
232 dvc->volume.max = 0x00800000 - 1;
233 ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
234 rsnd_dai_is_play(rdai, io) ?
235 "DVC Out Playback Volume" : "DVC In Capture Volume",
242 ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
243 rsnd_dai_is_play(rdai, io) ?
244 "DVC Out Mute Switch" : "DVC In Mute Switch",
252 static struct rsnd_mod_ops rsnd_dvc_ops = {
254 .probe = rsnd_dvc_probe_gen2,
255 .init = rsnd_dvc_init,
256 .quit = rsnd_dvc_quit,
257 .start = rsnd_dvc_start,
258 .stop = rsnd_dvc_stop,
259 .pcm_new = rsnd_dvc_pcm_new,
262 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
264 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
267 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
270 static void rsnd_of_parse_dvc(struct platform_device *pdev,
271 const struct rsnd_of_data *of_data,
272 struct rsnd_priv *priv)
274 struct device_node *node;
275 struct rsnd_dvc_platform_info *dvc_info;
276 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
277 struct device *dev = &pdev->dev;
283 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
287 nr = of_get_child_count(node);
289 goto rsnd_of_parse_dvc_end;
291 dvc_info = devm_kzalloc(dev,
292 sizeof(struct rsnd_dvc_platform_info) * nr,
295 dev_err(dev, "dvc info allocation error\n");
296 goto rsnd_of_parse_dvc_end;
299 info->dvc_info = dvc_info;
300 info->dvc_info_nr = nr;
302 rsnd_of_parse_dvc_end:
306 int rsnd_dvc_probe(struct platform_device *pdev,
307 const struct rsnd_of_data *of_data,
308 struct rsnd_priv *priv)
310 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
311 struct device *dev = rsnd_priv_to_dev(priv);
312 struct rsnd_dvc *dvc;
314 char name[RSND_DVC_NAME_SIZE];
317 rsnd_of_parse_dvc(pdev, of_data, priv);
319 nr = info->dvc_info_nr;
323 /* This driver doesn't support Gen1 at this point */
324 if (rsnd_is_gen1(priv)) {
325 dev_warn(dev, "CMD is not supported on Gen1\n");
329 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
331 dev_err(dev, "CMD allocate failed\n");
338 for_each_rsnd_dvc(dvc, priv, i) {
339 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
342 clk = devm_clk_get(dev, name);
346 dvc->info = &info->dvc_info[i];
349 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
351 dev_dbg(dev, "CMD%d probed\n", i);