blob: 4ddca4f380c7ba5dfccbc1ec3b25fc829cbbc770 [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,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000120 struct rsnd_priv *priv)
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700121{
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700122 struct device *dev = rsnd_priv_to_dev(priv);
123
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800124 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
125 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700126
127 return 0;
128}
129
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700130static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000131 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700132{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700133 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700134 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
135 struct device *dev = rsnd_priv_to_dev(priv);
136 int dvc_id = rsnd_mod_id(dvc_mod);
137 int src_id = rsnd_mod_id(src_mod);
138 u32 route[] = {
139 [0] = 0x30000,
140 [1] = 0x30001,
141 [2] = 0x40000,
142 [3] = 0x10000,
143 [4] = 0x20000,
144 [5] = 0x40100
145 };
146
147 if (src_id >= ARRAY_SIZE(route)) {
148 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
149 return -EINVAL;
150 }
151
Kuninori Morimoto85642952015-01-15 08:03:22 +0000152 rsnd_mod_hw_start(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700153
154 /*
155 * fixme
156 * it doesn't support CTU/MIX
157 */
158 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
159
160 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
161 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
162
163 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
164
165 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
166
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700167 /* ch0/ch1 Volume */
168 rsnd_dvc_volume_update(dvc_mod);
169
170 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
171
Kuninori Morimotof708d942015-01-15 08:07:19 +0000172 rsnd_adg_set_cmd_timsel_gen2(dvc_mod, io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700173
174 return 0;
175}
176
177static int rsnd_dvc_quit(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000178 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700179{
Kuninori Morimoto85642952015-01-15 08:03:22 +0000180 rsnd_mod_hw_stop(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700181
182 return 0;
183}
184
185static int rsnd_dvc_start(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000186 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700187{
188 rsnd_mod_write(mod, CMD_CTRL, 0x10);
189
190 return 0;
191}
192
193static int rsnd_dvc_stop(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000194 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700195{
196 rsnd_mod_write(mod, CMD_CTRL, 0);
197
198 return 0;
199}
200
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700201static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700202 struct snd_soc_pcm_runtime *rtd)
203{
204 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700205 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000206 int is_play = rsnd_io_is_play(io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700207 int ret;
208
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700209 /* Volume */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000210 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000211 is_play ?
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700212 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000213 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800214 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700215 if (ret < 0)
216 return ret;
217
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700218 /* Mute */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000219 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000220 is_play ?
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700221 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000222 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800223 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700224 if (ret < 0)
225 return ret;
226
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800227 /* Ramp */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000228 ret = rsnd_kctrl_new_s(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000229 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800230 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000231 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800232 &dvc->ren, 1);
233 if (ret < 0)
234 return ret;
235
Kuninori Morimotof708d942015-01-15 08:07:19 +0000236 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000237 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800238 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
239 &dvc->rup,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000240 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800241 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
242 if (ret < 0)
243 return ret;
244
Kuninori Morimotof708d942015-01-15 08:07:19 +0000245 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000246 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800247 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
248 &dvc->rdown,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000249 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800250 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
251
252 if (ret < 0)
253 return ret;
254
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700255 return 0;
256}
257
258static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700259 .name = DVC_NAME,
260 .probe = rsnd_dvc_probe_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700261 .init = rsnd_dvc_init,
262 .quit = rsnd_dvc_quit,
263 .start = rsnd_dvc_start,
264 .stop = rsnd_dvc_stop,
265 .pcm_new = rsnd_dvc_pcm_new,
266};
267
268struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
269{
270 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
271 id = 0;
272
273 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
274}
275
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700276static void rsnd_of_parse_dvc(struct platform_device *pdev,
277 const struct rsnd_of_data *of_data,
278 struct rsnd_priv *priv)
279{
280 struct device_node *node;
281 struct rsnd_dvc_platform_info *dvc_info;
282 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
283 struct device *dev = &pdev->dev;
284 int nr;
285
286 if (!of_data)
287 return;
288
289 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
290 if (!node)
291 return;
292
293 nr = of_get_child_count(node);
294 if (!nr)
295 goto rsnd_of_parse_dvc_end;
296
297 dvc_info = devm_kzalloc(dev,
298 sizeof(struct rsnd_dvc_platform_info) * nr,
299 GFP_KERNEL);
300 if (!dvc_info) {
301 dev_err(dev, "dvc info allocation error\n");
302 goto rsnd_of_parse_dvc_end;
303 }
304
305 info->dvc_info = dvc_info;
306 info->dvc_info_nr = nr;
307
308rsnd_of_parse_dvc_end:
309 of_node_put(node);
310}
311
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700312int rsnd_dvc_probe(struct platform_device *pdev,
313 const struct rsnd_of_data *of_data,
314 struct rsnd_priv *priv)
315{
316 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
317 struct device *dev = rsnd_priv_to_dev(priv);
318 struct rsnd_dvc *dvc;
319 struct clk *clk;
320 char name[RSND_DVC_NAME_SIZE];
321 int i, nr;
322
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700323 rsnd_of_parse_dvc(pdev, of_data, priv);
324
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700325 nr = info->dvc_info_nr;
326 if (!nr)
327 return 0;
328
329 /* This driver doesn't support Gen1 at this point */
330 if (rsnd_is_gen1(priv)) {
331 dev_warn(dev, "CMD is not supported on Gen1\n");
332 return -EINVAL;
333 }
334
335 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
336 if (!dvc) {
337 dev_err(dev, "CMD allocate failed\n");
338 return -ENOMEM;
339 }
340
341 priv->dvc_nr = nr;
342 priv->dvc = dvc;
343
344 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700345 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
346 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700347
348 clk = devm_clk_get(dev, name);
349 if (IS_ERR(clk))
350 return PTR_ERR(clk);
351
352 dvc->info = &info->dvc_info[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700353
Kuninori Morimoto85642952015-01-15 08:03:22 +0000354 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops,
355 clk, RSND_MOD_DVC, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700356
357 dev_dbg(dev, "CMD%d probed\n", i);
358 }
359
360 return 0;
361}