blob: e0990180e1ea2bc4ad9d2e0d14060e9681b3237d [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
Kuninori Morimoto93b986e2015-02-20 10:30:41 +000027#define rsnd_dvc_of_node(priv) \
28 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,dvc")
29
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070030#define rsnd_mod_to_dvc(_mod) \
31 container_of((_mod), struct rsnd_dvc, mod)
32
33#define for_each_rsnd_dvc(pos, priv, i) \
34 for ((i) = 0; \
35 ((i) < rsnd_dvc_nr(priv)) && \
36 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
37 i++)
38
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080039static const char const *dvc_ramp_rate[] = {
40 "128 dB/1 step", /* 00000 */
41 "64 dB/1 step", /* 00001 */
42 "32 dB/1 step", /* 00010 */
43 "16 dB/1 step", /* 00011 */
44 "8 dB/1 step", /* 00100 */
45 "4 dB/1 step", /* 00101 */
46 "2 dB/1 step", /* 00110 */
47 "1 dB/1 step", /* 00111 */
48 "0.5 dB/1 step", /* 01000 */
49 "0.25 dB/1 step", /* 01001 */
50 "0.125 dB/1 step", /* 01010 */
51 "0.125 dB/2 steps", /* 01011 */
52 "0.125 dB/4 steps", /* 01100 */
53 "0.125 dB/8 steps", /* 01101 */
54 "0.125 dB/16 steps", /* 01110 */
55 "0.125 dB/32 steps", /* 01111 */
56 "0.125 dB/64 steps", /* 10000 */
57 "0.125 dB/128 steps", /* 10001 */
58 "0.125 dB/256 steps", /* 10010 */
59 "0.125 dB/512 steps", /* 10011 */
60 "0.125 dB/1024 steps", /* 10100 */
61 "0.125 dB/2048 steps", /* 10101 */
62 "0.125 dB/4096 steps", /* 10110 */
63 "0.125 dB/8192 steps", /* 10111 */
64};
65
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070066static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
67{
68 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080069 u32 val[RSND_DVC_CHANNELS];
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080070 u32 dvucr = 0;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070071 u32 mute = 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070072 int i;
73
Kuninori Morimotoec14af92014-11-04 20:27:46 -080074 for (i = 0; i < dvc->mute.cfg.size; i++)
75 mute |= (!!dvc->mute.cfg.val[i]) << i;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070076
Kuninori Morimoto140bab82014-11-04 20:27:18 -080077 /* Disable DVC Register access */
78 rsnd_mod_write(mod, DVC_DVUER, 0);
79
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080080 /* Enable Ramp */
81 if (dvc->ren.val) {
82 dvucr |= 0x10;
83
84 /* Digital Volume Max */
85 for (i = 0; i < RSND_DVC_CHANNELS; i++)
86 val[i] = dvc->volume.cfg.max;
87
88 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
89 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
90 dvc->rdown.val);
91 /*
92 * FIXME !!
93 * use scale-downed Digital Volume
94 * as Volume Ramp
95 * 7F FFFF -> 3FF
96 */
97 rsnd_mod_write(mod, DVC_VRDBR,
98 0x3ff - (dvc->volume.val[0] >> 13));
99
100 } else {
101 for (i = 0; i < RSND_DVC_CHANNELS; i++)
102 val[i] = dvc->volume.val[i];
103 }
104
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800105 /* Enable Digital Volume */
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800106 dvucr |= 0x100;
107 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
108 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700109
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800110 /* Enable Mute */
111 if (mute) {
112 dvucr |= 0x1;
113 rsnd_mod_write(mod, DVC_ZCMCR, mute);
114 }
115
116 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
Kuninori Morimoto140bab82014-11-04 20:27:18 -0800117
118 /* Enable DVC Register access */
119 rsnd_mod_write(mod, DVC_DVUER, 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700120}
121
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700122static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000123 struct rsnd_priv *priv)
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700124{
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700125 struct device *dev = rsnd_priv_to_dev(priv);
126
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800127 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
128 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700129
130 return 0;
131}
132
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000133static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
134 struct rsnd_priv *priv)
135{
136 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
137
138 rsnd_kctrl_remove(dvc->volume);
139 rsnd_kctrl_remove(dvc->mute);
140 rsnd_kctrl_remove(dvc->ren);
141 rsnd_kctrl_remove(dvc->rup);
142 rsnd_kctrl_remove(dvc->rdown);
143
144 return 0;
145}
146
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700147static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000148 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700149{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700150 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700151 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
152 struct device *dev = rsnd_priv_to_dev(priv);
153 int dvc_id = rsnd_mod_id(dvc_mod);
154 int src_id = rsnd_mod_id(src_mod);
155 u32 route[] = {
156 [0] = 0x30000,
157 [1] = 0x30001,
158 [2] = 0x40000,
159 [3] = 0x10000,
160 [4] = 0x20000,
161 [5] = 0x40100
162 };
163
164 if (src_id >= ARRAY_SIZE(route)) {
165 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
166 return -EINVAL;
167 }
168
Kuninori Morimoto85642952015-01-15 08:03:22 +0000169 rsnd_mod_hw_start(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700170
171 /*
172 * fixme
173 * it doesn't support CTU/MIX
174 */
175 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
176
177 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
178 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
179
180 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
181
182 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
183
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700184 /* ch0/ch1 Volume */
185 rsnd_dvc_volume_update(dvc_mod);
186
187 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
188
Kuninori Morimotof708d942015-01-15 08:07:19 +0000189 rsnd_adg_set_cmd_timsel_gen2(dvc_mod, io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700190
191 return 0;
192}
193
194static int rsnd_dvc_quit(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000195 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700196{
Kuninori Morimoto85642952015-01-15 08:03:22 +0000197 rsnd_mod_hw_stop(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700198
199 return 0;
200}
201
202static int rsnd_dvc_start(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000203 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700204{
205 rsnd_mod_write(mod, CMD_CTRL, 0x10);
206
207 return 0;
208}
209
210static int rsnd_dvc_stop(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000211 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700212{
213 rsnd_mod_write(mod, CMD_CTRL, 0);
214
215 return 0;
216}
217
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700218static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700219 struct snd_soc_pcm_runtime *rtd)
220{
221 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700222 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000223 int is_play = rsnd_io_is_play(io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700224 int ret;
225
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700226 /* Volume */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000227 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000228 is_play ?
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700229 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000230 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800231 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700232 if (ret < 0)
233 return ret;
234
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700235 /* Mute */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000236 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000237 is_play ?
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700238 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000239 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800240 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700241 if (ret < 0)
242 return ret;
243
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800244 /* Ramp */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000245 ret = rsnd_kctrl_new_s(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000246 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800247 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000248 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800249 &dvc->ren, 1);
250 if (ret < 0)
251 return ret;
252
Kuninori Morimotof708d942015-01-15 08:07:19 +0000253 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000254 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800255 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
256 &dvc->rup,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000257 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800258 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
259 if (ret < 0)
260 return ret;
261
Kuninori Morimotof708d942015-01-15 08:07:19 +0000262 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000263 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800264 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
265 &dvc->rdown,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000266 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800267 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
268
269 if (ret < 0)
270 return ret;
271
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700272 return 0;
273}
274
275static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700276 .name = DVC_NAME,
277 .probe = rsnd_dvc_probe_gen2,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000278 .remove = rsnd_dvc_remove_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700279 .init = rsnd_dvc_init,
280 .quit = rsnd_dvc_quit,
281 .start = rsnd_dvc_start,
282 .stop = rsnd_dvc_stop,
283 .pcm_new = rsnd_dvc_pcm_new,
284};
285
286struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
287{
288 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
289 id = 0;
290
291 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
292}
293
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700294static void rsnd_of_parse_dvc(struct platform_device *pdev,
295 const struct rsnd_of_data *of_data,
296 struct rsnd_priv *priv)
297{
298 struct device_node *node;
299 struct rsnd_dvc_platform_info *dvc_info;
300 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
301 struct device *dev = &pdev->dev;
302 int nr;
303
304 if (!of_data)
305 return;
306
307 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
308 if (!node)
309 return;
310
311 nr = of_get_child_count(node);
312 if (!nr)
313 goto rsnd_of_parse_dvc_end;
314
315 dvc_info = devm_kzalloc(dev,
316 sizeof(struct rsnd_dvc_platform_info) * nr,
317 GFP_KERNEL);
318 if (!dvc_info) {
319 dev_err(dev, "dvc info allocation error\n");
320 goto rsnd_of_parse_dvc_end;
321 }
322
323 info->dvc_info = dvc_info;
324 info->dvc_info_nr = nr;
325
326rsnd_of_parse_dvc_end:
327 of_node_put(node);
328}
329
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700330int rsnd_dvc_probe(struct platform_device *pdev,
331 const struct rsnd_of_data *of_data,
332 struct rsnd_priv *priv)
333{
334 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
335 struct device *dev = rsnd_priv_to_dev(priv);
336 struct rsnd_dvc *dvc;
337 struct clk *clk;
338 char name[RSND_DVC_NAME_SIZE];
339 int i, nr;
340
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700341 rsnd_of_parse_dvc(pdev, of_data, priv);
342
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700343 nr = info->dvc_info_nr;
344 if (!nr)
345 return 0;
346
347 /* This driver doesn't support Gen1 at this point */
348 if (rsnd_is_gen1(priv)) {
349 dev_warn(dev, "CMD is not supported on Gen1\n");
350 return -EINVAL;
351 }
352
353 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
354 if (!dvc) {
355 dev_err(dev, "CMD allocate failed\n");
356 return -ENOMEM;
357 }
358
359 priv->dvc_nr = nr;
360 priv->dvc = dvc;
361
362 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700363 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
364 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700365
366 clk = devm_clk_get(dev, name);
367 if (IS_ERR(clk))
368 return PTR_ERR(clk);
369
370 dvc->info = &info->dvc_info[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700371
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000372 rsnd_mod_init(&dvc->mod, &rsnd_dvc_ops,
Kuninori Morimoto85642952015-01-15 08:03:22 +0000373 clk, RSND_MOD_DVC, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700374
375 dev_dbg(dev, "CMD%d probed\n", i);
376 }
377
378 return 0;
379}