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);
47 for (i = 0; i < RSND_DVC_CHANNELS; i++) {
48 mute |= (!!dvc->mute.val[i]) << i;
51 /* Disable DVC Register access */
52 rsnd_mod_write(mod, DVC_DVUER, 0);
54 /* Enable Digital Volume */
56 rsnd_mod_write(mod, DVC_VOL0R, dvc->volume.val[0]);
57 rsnd_mod_write(mod, DVC_VOL1R, dvc->volume.val[1]);
62 rsnd_mod_write(mod, DVC_ZCMCR, mute);
65 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
67 /* Enable DVC Register access */
68 rsnd_mod_write(mod, DVC_DVUER, 1);
71 static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
72 struct rsnd_dai *rdai)
74 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
75 struct device *dev = rsnd_priv_to_dev(priv);
77 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
82 static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
83 struct rsnd_dai *rdai)
85 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
86 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
87 struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
88 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
89 struct device *dev = rsnd_priv_to_dev(priv);
90 int dvc_id = rsnd_mod_id(dvc_mod);
91 int src_id = rsnd_mod_id(src_mod);
101 if (src_id >= ARRAY_SIZE(route)) {
102 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
106 clk_prepare_enable(dvc->clk);
110 * it doesn't support CTU/MIX
112 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
114 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
115 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
117 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
119 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
122 rsnd_dvc_volume_update(dvc_mod);
124 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
126 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
131 static int rsnd_dvc_quit(struct rsnd_mod *mod,
132 struct rsnd_dai *rdai)
134 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
136 clk_disable_unprepare(dvc->clk);
141 static int rsnd_dvc_start(struct rsnd_mod *mod,
142 struct rsnd_dai *rdai)
144 rsnd_mod_write(mod, CMD_CTRL, 0x10);
149 static int rsnd_dvc_stop(struct rsnd_mod *mod,
150 struct rsnd_dai *rdai)
152 rsnd_mod_write(mod, CMD_CTRL, 0);
157 static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
158 struct snd_ctl_elem_info *uinfo)
160 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
162 uinfo->count = RSND_DVC_CHANNELS;
163 uinfo->value.integer.min = 0;
164 uinfo->value.integer.max = cfg->max;
167 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
169 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
174 static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
175 struct snd_ctl_elem_value *ucontrol)
177 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
180 for (i = 0; i < RSND_DVC_CHANNELS; i++)
181 ucontrol->value.integer.value[i] = cfg->val[i];
186 static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
187 struct snd_ctl_elem_value *ucontrol)
189 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
190 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
193 for (i = 0; i < RSND_DVC_CHANNELS; i++) {
194 change |= (ucontrol->value.integer.value[i] != cfg->val[i]);
195 cfg->val[i] = ucontrol->value.integer.value[i];
199 rsnd_dvc_volume_update(mod);
204 static int __rsnd_dvc_pcm_new(struct rsnd_mod *mod,
205 struct rsnd_dai *rdai,
206 struct snd_soc_pcm_runtime *rtd,
207 const unsigned char *name,
208 struct rsnd_dvc_cfg *private)
210 struct snd_card *card = rtd->card->snd_card;
211 struct snd_kcontrol *kctrl;
212 struct snd_kcontrol_new knew = {
213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
215 .info = rsnd_dvc_volume_info,
216 .get = rsnd_dvc_volume_get,
217 .put = rsnd_dvc_volume_put,
218 .private_value = (unsigned long)private,
222 kctrl = snd_ctl_new1(&knew, mod);
226 ret = snd_ctl_add(card, kctrl);
233 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
234 struct rsnd_dai *rdai,
235 struct snd_soc_pcm_runtime *rtd)
237 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
238 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
242 dvc->volume.max = 0x00800000 - 1;
243 ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
244 rsnd_dai_is_play(rdai, io) ?
245 "DVC Out Playback Volume" : "DVC In Capture Volume",
252 ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
253 rsnd_dai_is_play(rdai, io) ?
254 "DVC Out Mute Switch" : "DVC In Mute Switch",
262 static struct rsnd_mod_ops rsnd_dvc_ops = {
264 .probe = rsnd_dvc_probe_gen2,
265 .init = rsnd_dvc_init,
266 .quit = rsnd_dvc_quit,
267 .start = rsnd_dvc_start,
268 .stop = rsnd_dvc_stop,
269 .pcm_new = rsnd_dvc_pcm_new,
272 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
274 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
277 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
280 static void rsnd_of_parse_dvc(struct platform_device *pdev,
281 const struct rsnd_of_data *of_data,
282 struct rsnd_priv *priv)
284 struct device_node *node;
285 struct rsnd_dvc_platform_info *dvc_info;
286 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
287 struct device *dev = &pdev->dev;
293 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
297 nr = of_get_child_count(node);
299 goto rsnd_of_parse_dvc_end;
301 dvc_info = devm_kzalloc(dev,
302 sizeof(struct rsnd_dvc_platform_info) * nr,
305 dev_err(dev, "dvc info allocation error\n");
306 goto rsnd_of_parse_dvc_end;
309 info->dvc_info = dvc_info;
310 info->dvc_info_nr = nr;
312 rsnd_of_parse_dvc_end:
316 int rsnd_dvc_probe(struct platform_device *pdev,
317 const struct rsnd_of_data *of_data,
318 struct rsnd_priv *priv)
320 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
321 struct device *dev = rsnd_priv_to_dev(priv);
322 struct rsnd_dvc *dvc;
324 char name[RSND_DVC_NAME_SIZE];
327 rsnd_of_parse_dvc(pdev, of_data, priv);
329 nr = info->dvc_info_nr;
333 /* This driver doesn't support Gen1 at this point */
334 if (rsnd_is_gen1(priv)) {
335 dev_warn(dev, "CMD is not supported on Gen1\n");
339 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
341 dev_err(dev, "CMD allocate failed\n");
348 for_each_rsnd_dvc(dvc, priv, i) {
349 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
352 clk = devm_clk_get(dev, name);
356 dvc->info = &info->dvc_info[i];
359 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
361 dev_dbg(dev, "CMD%d probed\n", i);