blob: 77cb0089047188ec2269db121bb8d4a434c157e8 [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 Morimoto8aefda52014-05-22 23:25:43 -070014
15#define DVC_NAME "dvc"
16
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070017struct rsnd_dvc {
18 struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
19 struct rsnd_mod mod;
Kuninori Morimoto170a2492014-11-27 08:06:14 +000020 struct rsnd_kctrl_cfg_m volume;
21 struct rsnd_kctrl_cfg_m mute;
22 struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
23 struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
24 struct rsnd_kctrl_cfg_s rdown; /* Ramp Rate Down */
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070025};
26
27#define rsnd_mod_to_dvc(_mod) \
28 container_of((_mod), struct rsnd_dvc, mod)
29
30#define for_each_rsnd_dvc(pos, priv, i) \
31 for ((i) = 0; \
32 ((i) < rsnd_dvc_nr(priv)) && \
33 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
34 i++)
35
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080036static const char const *dvc_ramp_rate[] = {
37 "128 dB/1 step", /* 00000 */
38 "64 dB/1 step", /* 00001 */
39 "32 dB/1 step", /* 00010 */
40 "16 dB/1 step", /* 00011 */
41 "8 dB/1 step", /* 00100 */
42 "4 dB/1 step", /* 00101 */
43 "2 dB/1 step", /* 00110 */
44 "1 dB/1 step", /* 00111 */
45 "0.5 dB/1 step", /* 01000 */
46 "0.25 dB/1 step", /* 01001 */
47 "0.125 dB/1 step", /* 01010 */
48 "0.125 dB/2 steps", /* 01011 */
49 "0.125 dB/4 steps", /* 01100 */
50 "0.125 dB/8 steps", /* 01101 */
51 "0.125 dB/16 steps", /* 01110 */
52 "0.125 dB/32 steps", /* 01111 */
53 "0.125 dB/64 steps", /* 10000 */
54 "0.125 dB/128 steps", /* 10001 */
55 "0.125 dB/256 steps", /* 10010 */
56 "0.125 dB/512 steps", /* 10011 */
57 "0.125 dB/1024 steps", /* 10100 */
58 "0.125 dB/2048 steps", /* 10101 */
59 "0.125 dB/4096 steps", /* 10110 */
60 "0.125 dB/8192 steps", /* 10111 */
61};
62
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070063static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
64{
65 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080066 u32 val[RSND_DVC_CHANNELS];
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080067 u32 dvucr = 0;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070068 u32 mute = 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070069 int i;
70
Kuninori Morimotoec14af92014-11-04 20:27:46 -080071 for (i = 0; i < dvc->mute.cfg.size; i++)
72 mute |= (!!dvc->mute.cfg.val[i]) << i;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070073
Kuninori Morimoto140bab82014-11-04 20:27:18 -080074 /* Disable DVC Register access */
75 rsnd_mod_write(mod, DVC_DVUER, 0);
76
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080077 /* Enable Ramp */
78 if (dvc->ren.val) {
79 dvucr |= 0x10;
80
81 /* Digital Volume Max */
82 for (i = 0; i < RSND_DVC_CHANNELS; i++)
83 val[i] = dvc->volume.cfg.max;
84
85 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
86 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
87 dvc->rdown.val);
88 /*
89 * FIXME !!
90 * use scale-downed Digital Volume
91 * as Volume Ramp
92 * 7F FFFF -> 3FF
93 */
94 rsnd_mod_write(mod, DVC_VRDBR,
95 0x3ff - (dvc->volume.val[0] >> 13));
96
97 } else {
98 for (i = 0; i < RSND_DVC_CHANNELS; i++)
99 val[i] = dvc->volume.val[i];
100 }
101
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800102 /* Enable Digital Volume */
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800103 dvucr |= 0x100;
104 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
105 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700106
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800107 /* Enable Mute */
108 if (mute) {
109 dvucr |= 0x1;
110 rsnd_mod_write(mod, DVC_ZCMCR, mute);
111 }
112
113 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
Kuninori Morimoto140bab82014-11-04 20:27:18 -0800114
115 /* Enable DVC Register access */
116 rsnd_mod_write(mod, DVC_DVUER, 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700117}
118
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700119static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
120 struct rsnd_dai *rdai)
121{
122 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
123 struct device *dev = rsnd_priv_to_dev(priv);
124
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800125 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
126 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700127
128 return 0;
129}
130
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700131static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
132 struct rsnd_dai *rdai)
133{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700134 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
135 struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
136 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
137 struct device *dev = rsnd_priv_to_dev(priv);
138 int dvc_id = rsnd_mod_id(dvc_mod);
139 int src_id = rsnd_mod_id(src_mod);
140 u32 route[] = {
141 [0] = 0x30000,
142 [1] = 0x30001,
143 [2] = 0x40000,
144 [3] = 0x10000,
145 [4] = 0x20000,
146 [5] = 0x40100
147 };
148
149 if (src_id >= ARRAY_SIZE(route)) {
150 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
151 return -EINVAL;
152 }
153
Kuninori Morimoto85642952015-01-15 08:03:22 +0000154 rsnd_mod_hw_start(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700155
156 /*
157 * fixme
158 * it doesn't support CTU/MIX
159 */
160 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
161
162 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
163 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
164
165 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
166
167 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
168
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700169 /* ch0/ch1 Volume */
170 rsnd_dvc_volume_update(dvc_mod);
171
172 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
173
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700174 rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
175
176 return 0;
177}
178
179static int rsnd_dvc_quit(struct rsnd_mod *mod,
180 struct rsnd_dai *rdai)
181{
Kuninori Morimoto85642952015-01-15 08:03:22 +0000182 rsnd_mod_hw_stop(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700183
184 return 0;
185}
186
187static int rsnd_dvc_start(struct rsnd_mod *mod,
188 struct rsnd_dai *rdai)
189{
190 rsnd_mod_write(mod, CMD_CTRL, 0x10);
191
192 return 0;
193}
194
195static int rsnd_dvc_stop(struct rsnd_mod *mod,
196 struct rsnd_dai *rdai)
197{
198 rsnd_mod_write(mod, CMD_CTRL, 0);
199
200 return 0;
201}
202
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700203static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
204 struct rsnd_dai *rdai,
205 struct snd_soc_pcm_runtime *rtd)
206{
207 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700208 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700209 int ret;
210
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700211 /* Volume */
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000212 ret = rsnd_kctrl_new_m(mod, rdai, rtd,
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700213 rsnd_dai_is_play(rdai, io) ?
214 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000215 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800216 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700217 if (ret < 0)
218 return ret;
219
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700220 /* Mute */
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000221 ret = rsnd_kctrl_new_m(mod, rdai, rtd,
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700222 rsnd_dai_is_play(rdai, io) ?
223 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000224 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800225 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700226 if (ret < 0)
227 return ret;
228
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800229 /* Ramp */
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000230 ret = rsnd_kctrl_new_s(mod, rdai, rtd,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800231 rsnd_dai_is_play(rdai, io) ?
232 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000233 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800234 &dvc->ren, 1);
235 if (ret < 0)
236 return ret;
237
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000238 ret = rsnd_kctrl_new_e(mod, rdai, rtd,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800239 rsnd_dai_is_play(rdai, io) ?
240 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
241 &dvc->rup,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000242 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800243 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
244 if (ret < 0)
245 return ret;
246
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000247 ret = rsnd_kctrl_new_e(mod, rdai, rtd,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800248 rsnd_dai_is_play(rdai, io) ?
249 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
250 &dvc->rdown,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000251 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800252 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
253
254 if (ret < 0)
255 return ret;
256
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700257 return 0;
258}
259
260static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700261 .name = DVC_NAME,
262 .probe = rsnd_dvc_probe_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700263 .init = rsnd_dvc_init,
264 .quit = rsnd_dvc_quit,
265 .start = rsnd_dvc_start,
266 .stop = rsnd_dvc_stop,
267 .pcm_new = rsnd_dvc_pcm_new,
268};
269
270struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
271{
272 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
273 id = 0;
274
275 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
276}
277
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700278static void rsnd_of_parse_dvc(struct platform_device *pdev,
279 const struct rsnd_of_data *of_data,
280 struct rsnd_priv *priv)
281{
282 struct device_node *node;
283 struct rsnd_dvc_platform_info *dvc_info;
284 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
285 struct device *dev = &pdev->dev;
286 int nr;
287
288 if (!of_data)
289 return;
290
291 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
292 if (!node)
293 return;
294
295 nr = of_get_child_count(node);
296 if (!nr)
297 goto rsnd_of_parse_dvc_end;
298
299 dvc_info = devm_kzalloc(dev,
300 sizeof(struct rsnd_dvc_platform_info) * nr,
301 GFP_KERNEL);
302 if (!dvc_info) {
303 dev_err(dev, "dvc info allocation error\n");
304 goto rsnd_of_parse_dvc_end;
305 }
306
307 info->dvc_info = dvc_info;
308 info->dvc_info_nr = nr;
309
310rsnd_of_parse_dvc_end:
311 of_node_put(node);
312}
313
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700314int rsnd_dvc_probe(struct platform_device *pdev,
315 const struct rsnd_of_data *of_data,
316 struct rsnd_priv *priv)
317{
318 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
319 struct device *dev = rsnd_priv_to_dev(priv);
320 struct rsnd_dvc *dvc;
321 struct clk *clk;
322 char name[RSND_DVC_NAME_SIZE];
323 int i, nr;
324
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700325 rsnd_of_parse_dvc(pdev, of_data, priv);
326
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700327 nr = info->dvc_info_nr;
328 if (!nr)
329 return 0;
330
331 /* This driver doesn't support Gen1 at this point */
332 if (rsnd_is_gen1(priv)) {
333 dev_warn(dev, "CMD is not supported on Gen1\n");
334 return -EINVAL;
335 }
336
337 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
338 if (!dvc) {
339 dev_err(dev, "CMD allocate failed\n");
340 return -ENOMEM;
341 }
342
343 priv->dvc_nr = nr;
344 priv->dvc = dvc;
345
346 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700347 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
348 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700349
350 clk = devm_clk_get(dev, name);
351 if (IS_ERR(clk))
352 return PTR_ERR(clk);
353
354 dvc->info = &info->dvc_info[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700355
Kuninori Morimoto85642952015-01-15 08:03:22 +0000356 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops,
357 clk, RSND_MOD_DVC, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700358
359 dev_dbg(dev, "CMD%d probed\n", i);
360 }
361
362 return 0;
363}