blob: c729e268f5d44477dd1876f3a86456051b6b71f0 [file] [log] [blame]
Kuninori Morimotobff58ea2014-05-08 17:44:49 -07001/*
2 * Renesas R-Car DVC support
3 *
4 * Copyright (C) 2014 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
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.
10 */
11#include "rsnd.h"
12
13#define RSND_DVC_NAME_SIZE 16
Kuninori Morimoto9960ce92014-10-21 18:13:56 -070014#define RSND_DVC_CHANNELS 2
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070015
16#define DVC_NAME "dvc"
17
Kuninori Morimoto92b9a692014-10-21 18:14:14 -070018struct rsnd_dvc_cfg {
19 unsigned int max;
Kuninori Morimotoec14af92014-11-04 20:27:46 -080020 unsigned int size;
21 u32 *val;
22};
23
24struct rsnd_dvc_cfg_m {
25 struct rsnd_dvc_cfg cfg;
Kuninori Morimoto92b9a692014-10-21 18:14:14 -070026 u32 val[RSND_DVC_CHANNELS];
27};
28
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070029struct rsnd_dvc {
30 struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
31 struct rsnd_mod mod;
32 struct clk *clk;
Kuninori Morimotoec14af92014-11-04 20:27:46 -080033 struct rsnd_dvc_cfg_m volume;
34 struct rsnd_dvc_cfg_m mute;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070035};
36
37#define rsnd_mod_to_dvc(_mod) \
38 container_of((_mod), struct rsnd_dvc, mod)
39
40#define for_each_rsnd_dvc(pos, priv, i) \
41 for ((i) = 0; \
42 ((i) < rsnd_dvc_nr(priv)) && \
43 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
44 i++)
45
46static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
47{
48 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080049 u32 dvucr = 0;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070050 u32 mute = 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070051 int i;
52
Kuninori Morimotoec14af92014-11-04 20:27:46 -080053 for (i = 0; i < dvc->mute.cfg.size; i++)
54 mute |= (!!dvc->mute.cfg.val[i]) << i;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070055
Kuninori Morimoto140bab82014-11-04 20:27:18 -080056 /* Disable DVC Register access */
57 rsnd_mod_write(mod, DVC_DVUER, 0);
58
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080059 /* Enable Digital Volume */
60 dvucr = 0x100;
Kuninori Morimoto92b9a692014-10-21 18:14:14 -070061 rsnd_mod_write(mod, DVC_VOL0R, dvc->volume.val[0]);
62 rsnd_mod_write(mod, DVC_VOL1R, dvc->volume.val[1]);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070063
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080064 /* Enable Mute */
65 if (mute) {
66 dvucr |= 0x1;
67 rsnd_mod_write(mod, DVC_ZCMCR, mute);
68 }
69
70 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
Kuninori Morimoto140bab82014-11-04 20:27:18 -080071
72 /* Enable DVC Register access */
73 rsnd_mod_write(mod, DVC_DVUER, 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070074}
75
Kuninori Morimoto8aefda52014-05-22 23:25:43 -070076static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
77 struct rsnd_dai *rdai)
78{
79 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
80 struct device *dev = rsnd_priv_to_dev(priv);
81
82 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
83
84 return 0;
85}
86
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070087static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
88 struct rsnd_dai *rdai)
89{
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);
97 u32 route[] = {
98 [0] = 0x30000,
99 [1] = 0x30001,
100 [2] = 0x40000,
101 [3] = 0x10000,
102 [4] = 0x20000,
103 [5] = 0x40100
104 };
105
106 if (src_id >= ARRAY_SIZE(route)) {
107 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
108 return -EINVAL;
109 }
110
111 clk_prepare_enable(dvc->clk);
112
113 /*
114 * fixme
115 * it doesn't support CTU/MIX
116 */
117 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
118
119 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
120 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
121
122 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
123
124 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
125
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700126 /* ch0/ch1 Volume */
127 rsnd_dvc_volume_update(dvc_mod);
128
129 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
130
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700131 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
132
133 return 0;
134}
135
136static int rsnd_dvc_quit(struct rsnd_mod *mod,
137 struct rsnd_dai *rdai)
138{
139 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
140
141 clk_disable_unprepare(dvc->clk);
142
143 return 0;
144}
145
146static int rsnd_dvc_start(struct rsnd_mod *mod,
147 struct rsnd_dai *rdai)
148{
149 rsnd_mod_write(mod, CMD_CTRL, 0x10);
150
151 return 0;
152}
153
154static int rsnd_dvc_stop(struct rsnd_mod *mod,
155 struct rsnd_dai *rdai)
156{
157 rsnd_mod_write(mod, CMD_CTRL, 0);
158
159 return 0;
160}
161
162static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
163 struct snd_ctl_elem_info *uinfo)
164{
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700165 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700166
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800167 uinfo->count = cfg->size;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700168 uinfo->value.integer.min = 0;
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700169 uinfo->value.integer.max = cfg->max;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700170
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700171 if (cfg->max == 1)
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700172 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700173 else
174 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700175
176 return 0;
177}
178
179static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
180 struct snd_ctl_elem_value *ucontrol)
181{
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700182 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700183 int i;
184
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800185 for (i = 0; i < cfg->size; i++)
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700186 ucontrol->value.integer.value[i] = cfg->val[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700187
188 return 0;
189}
190
191static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
192 struct snd_ctl_elem_value *ucontrol)
193{
194 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700195 struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700196 int i, change = 0;
197
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800198 for (i = 0; i < cfg->size; i++) {
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700199 change |= (ucontrol->value.integer.value[i] != cfg->val[i]);
200 cfg->val[i] = ucontrol->value.integer.value[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700201 }
202
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700203 if (change)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700204 rsnd_dvc_volume_update(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700205
206 return change;
207}
208
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700209static 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,
Kuninori Morimoto92b9a692014-10-21 18:14:14 -0700213 struct rsnd_dvc_cfg *private)
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700214{
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,
219 .name = name,
220 .info = rsnd_dvc_volume_info,
221 .get = rsnd_dvc_volume_get,
222 .put = rsnd_dvc_volume_put,
223 .private_value = (unsigned long)private,
224 };
225 int ret;
226
227 kctrl = snd_ctl_new1(&knew, mod);
228 if (!kctrl)
229 return -ENOMEM;
230
231 ret = snd_ctl_add(card, kctrl);
232 if (ret < 0)
233 return ret;
234
235 return 0;
236}
237
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800238static 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,
243 u32 max)
244{
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);
249}
250
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700251static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
252 struct rsnd_dai *rdai,
253 struct snd_soc_pcm_runtime *rtd)
254{
255 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700256 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700257 int ret;
258
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700259 /* Volume */
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800260 ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700261 rsnd_dai_is_play(rdai, io) ?
262 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800263 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700264 if (ret < 0)
265 return ret;
266
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700267 /* Mute */
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800268 ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700269 rsnd_dai_is_play(rdai, io) ?
270 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800271 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700272 if (ret < 0)
273 return ret;
274
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700275 return 0;
276}
277
278static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700279 .name = DVC_NAME,
280 .probe = rsnd_dvc_probe_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700281 .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,
286};
287
288struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
289{
290 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
291 id = 0;
292
293 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
294}
295
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700296static void rsnd_of_parse_dvc(struct platform_device *pdev,
297 const struct rsnd_of_data *of_data,
298 struct rsnd_priv *priv)
299{
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;
304 int nr;
305
306 if (!of_data)
307 return;
308
309 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
310 if (!node)
311 return;
312
313 nr = of_get_child_count(node);
314 if (!nr)
315 goto rsnd_of_parse_dvc_end;
316
317 dvc_info = devm_kzalloc(dev,
318 sizeof(struct rsnd_dvc_platform_info) * nr,
319 GFP_KERNEL);
320 if (!dvc_info) {
321 dev_err(dev, "dvc info allocation error\n");
322 goto rsnd_of_parse_dvc_end;
323 }
324
325 info->dvc_info = dvc_info;
326 info->dvc_info_nr = nr;
327
328rsnd_of_parse_dvc_end:
329 of_node_put(node);
330}
331
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700332int rsnd_dvc_probe(struct platform_device *pdev,
333 const struct rsnd_of_data *of_data,
334 struct rsnd_priv *priv)
335{
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;
339 struct clk *clk;
340 char name[RSND_DVC_NAME_SIZE];
341 int i, nr;
342
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700343 rsnd_of_parse_dvc(pdev, of_data, priv);
344
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700345 nr = info->dvc_info_nr;
346 if (!nr)
347 return 0;
348
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");
352 return -EINVAL;
353 }
354
355 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
356 if (!dvc) {
357 dev_err(dev, "CMD allocate failed\n");
358 return -ENOMEM;
359 }
360
361 priv->dvc_nr = nr;
362 priv->dvc = dvc;
363
364 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700365 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
366 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700367
368 clk = devm_clk_get(dev, name);
369 if (IS_ERR(clk))
370 return PTR_ERR(clk);
371
372 dvc->info = &info->dvc_info[i];
373 dvc->clk = clk;
374
375 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
376
377 dev_dbg(dev, "CMD%d probed\n", i);
378 }
379
380 return 0;
381}