blob: 8a61aa3963061dc6457f4dbfb838c6edc44ed059 [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 Morimoto4f35fab2015-07-15 07:11:02 +000027#define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
Kuninori Morimoto93b986e2015-02-20 10:30:41 +000028#define rsnd_dvc_of_node(priv) \
29 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,dvc")
30
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070031#define rsnd_mod_to_dvc(_mod) \
32 container_of((_mod), struct rsnd_dvc, mod)
33
34#define for_each_rsnd_dvc(pos, priv, i) \
35 for ((i) = 0; \
36 ((i) < rsnd_dvc_nr(priv)) && \
37 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
38 i++)
39
Krzysztof Kozlowski2f4b1e62015-03-24 11:47:43 +010040static const char * const dvc_ramp_rate[] = {
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080041 "128 dB/1 step", /* 00000 */
42 "64 dB/1 step", /* 00001 */
43 "32 dB/1 step", /* 00010 */
44 "16 dB/1 step", /* 00011 */
45 "8 dB/1 step", /* 00100 */
46 "4 dB/1 step", /* 00101 */
47 "2 dB/1 step", /* 00110 */
48 "1 dB/1 step", /* 00111 */
49 "0.5 dB/1 step", /* 01000 */
50 "0.25 dB/1 step", /* 01001 */
51 "0.125 dB/1 step", /* 01010 */
52 "0.125 dB/2 steps", /* 01011 */
53 "0.125 dB/4 steps", /* 01100 */
54 "0.125 dB/8 steps", /* 01101 */
55 "0.125 dB/16 steps", /* 01110 */
56 "0.125 dB/32 steps", /* 01111 */
57 "0.125 dB/64 steps", /* 10000 */
58 "0.125 dB/128 steps", /* 10001 */
59 "0.125 dB/256 steps", /* 10010 */
60 "0.125 dB/512 steps", /* 10011 */
61 "0.125 dB/1024 steps", /* 10100 */
62 "0.125 dB/2048 steps", /* 10101 */
63 "0.125 dB/4096 steps", /* 10110 */
64 "0.125 dB/8192 steps", /* 10111 */
65};
66
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +000067static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
68 struct rsnd_mod *mod)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070069{
70 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080071 u32 val[RSND_DVC_CHANNELS];
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080072 u32 dvucr = 0;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070073 u32 mute = 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070074 int i;
75
Kuninori Morimotoec14af92014-11-04 20:27:46 -080076 for (i = 0; i < dvc->mute.cfg.size; i++)
77 mute |= (!!dvc->mute.cfg.val[i]) << i;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070078
Kuninori Morimoto140bab82014-11-04 20:27:18 -080079 /* Disable DVC Register access */
80 rsnd_mod_write(mod, DVC_DVUER, 0);
81
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080082 /* Enable Ramp */
83 if (dvc->ren.val) {
84 dvucr |= 0x10;
85
86 /* Digital Volume Max */
87 for (i = 0; i < RSND_DVC_CHANNELS; i++)
88 val[i] = dvc->volume.cfg.max;
89
90 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
91 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
92 dvc->rdown.val);
93 /*
94 * FIXME !!
95 * use scale-downed Digital Volume
96 * as Volume Ramp
97 * 7F FFFF -> 3FF
98 */
99 rsnd_mod_write(mod, DVC_VRDBR,
100 0x3ff - (dvc->volume.val[0] >> 13));
101
102 } else {
103 for (i = 0; i < RSND_DVC_CHANNELS; i++)
104 val[i] = dvc->volume.val[i];
105 }
106
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800107 /* Enable Digital Volume */
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800108 dvucr |= 0x100;
109 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
110 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700111
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800112 /* Enable Mute */
113 if (mute) {
114 dvucr |= 0x1;
115 rsnd_mod_write(mod, DVC_ZCMCR, mute);
116 }
117
118 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
Kuninori Morimoto140bab82014-11-04 20:27:18 -0800119
120 /* Enable DVC Register access */
121 rsnd_mod_write(mod, DVC_DVUER, 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700122}
123
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000124static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000125 struct rsnd_dai_stream *io,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000126 struct rsnd_priv *priv)
127{
128 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
129
130 rsnd_kctrl_remove(dvc->volume);
131 rsnd_kctrl_remove(dvc->mute);
132 rsnd_kctrl_remove(dvc->ren);
133 rsnd_kctrl_remove(dvc->rup);
134 rsnd_kctrl_remove(dvc->rdown);
135
136 return 0;
137}
138
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700139static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000140 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000141 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700142{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700143 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
144 struct device *dev = rsnd_priv_to_dev(priv);
145 int dvc_id = rsnd_mod_id(dvc_mod);
146 int src_id = rsnd_mod_id(src_mod);
147 u32 route[] = {
148 [0] = 0x30000,
149 [1] = 0x30001,
150 [2] = 0x40000,
151 [3] = 0x10000,
152 [4] = 0x20000,
153 [5] = 0x40100
154 };
155
156 if (src_id >= ARRAY_SIZE(route)) {
157 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
158 return -EINVAL;
159 }
160
Kuninori Morimoto85642952015-01-15 08:03:22 +0000161 rsnd_mod_hw_start(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700162
163 /*
164 * fixme
165 * it doesn't support CTU/MIX
166 */
167 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
168
169 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
170 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
171
172 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
173
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000174 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod, io));
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700175
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700176 /* ch0/ch1 Volume */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000177 rsnd_dvc_volume_update(io, dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700178
179 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
180
Kuninori Morimotof708d942015-01-15 08:07:19 +0000181 rsnd_adg_set_cmd_timsel_gen2(dvc_mod, io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700182
183 return 0;
184}
185
186static int rsnd_dvc_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000187 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000188 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700189{
Kuninori Morimoto85642952015-01-15 08:03:22 +0000190 rsnd_mod_hw_stop(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700191
192 return 0;
193}
194
195static int rsnd_dvc_start(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000196 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000197 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700198{
199 rsnd_mod_write(mod, CMD_CTRL, 0x10);
200
201 return 0;
202}
203
204static int rsnd_dvc_stop(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000205 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000206 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700207{
208 rsnd_mod_write(mod, CMD_CTRL, 0);
209
210 return 0;
211}
212
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700213static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000214 struct rsnd_dai_stream *io,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700215 struct snd_soc_pcm_runtime *rtd)
216{
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700217 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000218 int is_play = rsnd_io_is_play(io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700219 int ret;
220
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700221 /* Volume */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000222 ret = rsnd_kctrl_new_m(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000223 is_play ?
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700224 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000225 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800226 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700227 if (ret < 0)
228 return ret;
229
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700230 /* Mute */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000231 ret = rsnd_kctrl_new_m(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000232 is_play ?
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700233 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000234 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800235 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700236 if (ret < 0)
237 return ret;
238
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800239 /* Ramp */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000240 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000241 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800242 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000243 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800244 &dvc->ren, 1);
245 if (ret < 0)
246 return ret;
247
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000248 ret = rsnd_kctrl_new_e(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000249 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800250 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
251 &dvc->rup,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000252 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800253 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
254 if (ret < 0)
255 return ret;
256
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000257 ret = rsnd_kctrl_new_e(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000258 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800259 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
260 &dvc->rdown,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000261 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800262 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
263
264 if (ret < 0)
265 return ret;
266
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700267 return 0;
268}
269
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000270static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
271 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000272{
273 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
274
275 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
276 mod, "tx");
277}
278
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700279static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700280 .name = DVC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000281 .dma_req = rsnd_dvc_dma_req,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000282 .remove = rsnd_dvc_remove_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700283 .init = rsnd_dvc_init,
284 .quit = rsnd_dvc_quit,
285 .start = rsnd_dvc_start,
286 .stop = rsnd_dvc_stop,
287 .pcm_new = rsnd_dvc_pcm_new,
288};
289
290struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
291{
292 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
293 id = 0;
294
295 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
296}
297
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700298static void rsnd_of_parse_dvc(struct platform_device *pdev,
299 const struct rsnd_of_data *of_data,
300 struct rsnd_priv *priv)
301{
302 struct device_node *node;
303 struct rsnd_dvc_platform_info *dvc_info;
304 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
305 struct device *dev = &pdev->dev;
306 int nr;
307
308 if (!of_data)
309 return;
310
311 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
312 if (!node)
313 return;
314
315 nr = of_get_child_count(node);
316 if (!nr)
317 goto rsnd_of_parse_dvc_end;
318
319 dvc_info = devm_kzalloc(dev,
320 sizeof(struct rsnd_dvc_platform_info) * nr,
321 GFP_KERNEL);
322 if (!dvc_info) {
323 dev_err(dev, "dvc info allocation error\n");
324 goto rsnd_of_parse_dvc_end;
325 }
326
327 info->dvc_info = dvc_info;
328 info->dvc_info_nr = nr;
329
330rsnd_of_parse_dvc_end:
331 of_node_put(node);
332}
333
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700334int rsnd_dvc_probe(struct platform_device *pdev,
335 const struct rsnd_of_data *of_data,
336 struct rsnd_priv *priv)
337{
338 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
339 struct device *dev = rsnd_priv_to_dev(priv);
340 struct rsnd_dvc *dvc;
341 struct clk *clk;
342 char name[RSND_DVC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000343 int i, nr, ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700344
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700345 rsnd_of_parse_dvc(pdev, of_data, priv);
346
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700347 nr = info->dvc_info_nr;
348 if (!nr)
349 return 0;
350
351 /* This driver doesn't support Gen1 at this point */
352 if (rsnd_is_gen1(priv)) {
353 dev_warn(dev, "CMD is not supported on Gen1\n");
354 return -EINVAL;
355 }
356
357 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
Kuninori Morimoto6abcae32015-07-15 07:09:07 +0000358 if (!dvc)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700359 return -ENOMEM;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700360
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];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700373
Kuninori Morimoto2099bc82015-06-15 06:24:54 +0000374 ret = rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops,
Kuninori Morimoto85642952015-01-15 08:03:22 +0000375 clk, RSND_MOD_DVC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000376 if (ret)
377 return ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700378 }
379
380 return 0;
381}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000382
383void rsnd_dvc_remove(struct platform_device *pdev,
384 struct rsnd_priv *priv)
385{
386 struct rsnd_dvc *dvc;
387 int i;
388
389 for_each_rsnd_dvc(dvc, priv, i) {
390 rsnd_mod_quit(&dvc->mod);
391 }
392}