blob: 691bc632f0cb3bfce9b1d0b984b41a3d0e087718 [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
Krzysztof Kozlowski2f4b1e62015-03-24 11:47:43 +010039static const char * const dvc_ramp_rate[] = {
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080040 "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 Morimotod1f83d62015-02-02 04:53:53 +0000122static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000123 struct rsnd_dai_stream *io,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000124 struct rsnd_priv *priv)
125{
126 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
127
128 rsnd_kctrl_remove(dvc->volume);
129 rsnd_kctrl_remove(dvc->mute);
130 rsnd_kctrl_remove(dvc->ren);
131 rsnd_kctrl_remove(dvc->rup);
132 rsnd_kctrl_remove(dvc->rdown);
133
134 return 0;
135}
136
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700137static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000138 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000139 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700140{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700141 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
142 struct device *dev = rsnd_priv_to_dev(priv);
143 int dvc_id = rsnd_mod_id(dvc_mod);
144 int src_id = rsnd_mod_id(src_mod);
145 u32 route[] = {
146 [0] = 0x30000,
147 [1] = 0x30001,
148 [2] = 0x40000,
149 [3] = 0x10000,
150 [4] = 0x20000,
151 [5] = 0x40100
152 };
153
154 if (src_id >= ARRAY_SIZE(route)) {
155 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
156 return -EINVAL;
157 }
158
Kuninori Morimoto85642952015-01-15 08:03:22 +0000159 rsnd_mod_hw_start(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700160
161 /*
162 * fixme
163 * it doesn't support CTU/MIX
164 */
165 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
166
167 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
168 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
169
170 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
171
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000172 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod, io));
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700173
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700174 /* ch0/ch1 Volume */
175 rsnd_dvc_volume_update(dvc_mod);
176
177 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
178
Kuninori Morimotof708d942015-01-15 08:07:19 +0000179 rsnd_adg_set_cmd_timsel_gen2(dvc_mod, io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700180
181 return 0;
182}
183
184static int rsnd_dvc_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000185 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000186 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700187{
Kuninori Morimoto85642952015-01-15 08:03:22 +0000188 rsnd_mod_hw_stop(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700189
190 return 0;
191}
192
193static int rsnd_dvc_start(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000194 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000195 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700196{
197 rsnd_mod_write(mod, CMD_CTRL, 0x10);
198
199 return 0;
200}
201
202static int rsnd_dvc_stop(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000203 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000204 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700205{
206 rsnd_mod_write(mod, CMD_CTRL, 0);
207
208 return 0;
209}
210
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700211static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000212 struct rsnd_dai_stream *io,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700213 struct snd_soc_pcm_runtime *rtd)
214{
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700215 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000216 int is_play = rsnd_io_is_play(io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700217 int ret;
218
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700219 /* Volume */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000220 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000221 is_play ?
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700222 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000223 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800224 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700225 if (ret < 0)
226 return ret;
227
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700228 /* Mute */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000229 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000230 is_play ?
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700231 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000232 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800233 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700234 if (ret < 0)
235 return ret;
236
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800237 /* Ramp */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000238 ret = rsnd_kctrl_new_s(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000239 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800240 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000241 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800242 &dvc->ren, 1);
243 if (ret < 0)
244 return ret;
245
Kuninori Morimotof708d942015-01-15 08:07:19 +0000246 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000247 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800248 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
249 &dvc->rup,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000250 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800251 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
252 if (ret < 0)
253 return ret;
254
Kuninori Morimotof708d942015-01-15 08:07:19 +0000255 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000256 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800257 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
258 &dvc->rdown,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000259 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800260 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
261
262 if (ret < 0)
263 return ret;
264
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700265 return 0;
266}
267
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000268static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_mod *mod)
269{
270 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
271
272 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
273 mod, "tx");
274}
275
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700276static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700277 .name = DVC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000278 .dma_req = rsnd_dvc_dma_req,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000279 .remove = rsnd_dvc_remove_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700280 .init = rsnd_dvc_init,
281 .quit = rsnd_dvc_quit,
282 .start = rsnd_dvc_start,
283 .stop = rsnd_dvc_stop,
284 .pcm_new = rsnd_dvc_pcm_new,
285};
286
287struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
288{
289 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
290 id = 0;
291
292 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
293}
294
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700295static void rsnd_of_parse_dvc(struct platform_device *pdev,
296 const struct rsnd_of_data *of_data,
297 struct rsnd_priv *priv)
298{
299 struct device_node *node;
300 struct rsnd_dvc_platform_info *dvc_info;
301 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
302 struct device *dev = &pdev->dev;
303 int nr;
304
305 if (!of_data)
306 return;
307
308 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
309 if (!node)
310 return;
311
312 nr = of_get_child_count(node);
313 if (!nr)
314 goto rsnd_of_parse_dvc_end;
315
316 dvc_info = devm_kzalloc(dev,
317 sizeof(struct rsnd_dvc_platform_info) * nr,
318 GFP_KERNEL);
319 if (!dvc_info) {
320 dev_err(dev, "dvc info allocation error\n");
321 goto rsnd_of_parse_dvc_end;
322 }
323
324 info->dvc_info = dvc_info;
325 info->dvc_info_nr = nr;
326
327rsnd_of_parse_dvc_end:
328 of_node_put(node);
329}
330
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700331int rsnd_dvc_probe(struct platform_device *pdev,
332 const struct rsnd_of_data *of_data,
333 struct rsnd_priv *priv)
334{
335 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
336 struct device *dev = rsnd_priv_to_dev(priv);
337 struct rsnd_dvc *dvc;
338 struct clk *clk;
339 char name[RSND_DVC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000340 int i, nr, ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700341
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700342 rsnd_of_parse_dvc(pdev, of_data, priv);
343
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700344 nr = info->dvc_info_nr;
345 if (!nr)
346 return 0;
347
348 /* This driver doesn't support Gen1 at this point */
349 if (rsnd_is_gen1(priv)) {
350 dev_warn(dev, "CMD is not supported on Gen1\n");
351 return -EINVAL;
352 }
353
354 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
355 if (!dvc) {
356 dev_err(dev, "CMD allocate failed\n");
357 return -ENOMEM;
358 }
359
360 priv->dvc_nr = nr;
361 priv->dvc = dvc;
362
363 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700364 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
365 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700366
367 clk = devm_clk_get(dev, name);
368 if (IS_ERR(clk))
369 return PTR_ERR(clk);
370
371 dvc->info = &info->dvc_info[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700372
Kuninori Morimoto2099bc82015-06-15 06:24:54 +0000373 ret = rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops,
Kuninori Morimoto85642952015-01-15 08:03:22 +0000374 clk, RSND_MOD_DVC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000375 if (ret)
376 return ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700377 }
378
379 return 0;
380}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000381
382void rsnd_dvc_remove(struct platform_device *pdev,
383 struct rsnd_priv *priv)
384{
385 struct rsnd_dvc *dvc;
386 int i;
387
388 for_each_rsnd_dvc(dvc, priv, i) {
389 rsnd_mod_quit(&dvc->mod);
390 }
391}