blob: 24d07634c7499fcd9c31cd2bedc790ae3bef547c [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 Morimoto636e4ba2015-07-15 07:12:00 +000067static void rsnd_dvc_soft_reset(struct rsnd_mod *mod)
68{
69 rsnd_mod_write(mod, DVC_SWRSR, 0);
70 rsnd_mod_write(mod, DVC_SWRSR, 1);
71}
72
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +000073static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
74 struct rsnd_mod *mod)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070075{
76 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080077 u32 val[RSND_DVC_CHANNELS];
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080078 u32 dvucr = 0;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070079 u32 mute = 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070080 int i;
81
Kuninori Morimotoec14af92014-11-04 20:27:46 -080082 for (i = 0; i < dvc->mute.cfg.size; i++)
83 mute |= (!!dvc->mute.cfg.val[i]) << i;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070084
Kuninori Morimoto140bab82014-11-04 20:27:18 -080085 /* Disable DVC Register access */
86 rsnd_mod_write(mod, DVC_DVUER, 0);
87
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080088 /* Enable Ramp */
89 if (dvc->ren.val) {
90 dvucr |= 0x10;
91
92 /* Digital Volume Max */
93 for (i = 0; i < RSND_DVC_CHANNELS; i++)
94 val[i] = dvc->volume.cfg.max;
95
96 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
97 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
98 dvc->rdown.val);
99 /*
100 * FIXME !!
101 * use scale-downed Digital Volume
102 * as Volume Ramp
103 * 7F FFFF -> 3FF
104 */
105 rsnd_mod_write(mod, DVC_VRDBR,
106 0x3ff - (dvc->volume.val[0] >> 13));
107
108 } else {
109 for (i = 0; i < RSND_DVC_CHANNELS; i++)
110 val[i] = dvc->volume.val[i];
111 }
112
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800113 /* Enable Digital Volume */
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800114 dvucr |= 0x100;
115 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
116 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700117
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800118 /* Enable Mute */
119 if (mute) {
120 dvucr |= 0x1;
121 rsnd_mod_write(mod, DVC_ZCMCR, mute);
122 }
123
124 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
Kuninori Morimoto140bab82014-11-04 20:27:18 -0800125
126 /* Enable DVC Register access */
127 rsnd_mod_write(mod, DVC_DVUER, 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700128}
129
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000130static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000131 struct rsnd_dai_stream *io,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000132 struct rsnd_priv *priv)
133{
134 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
135
136 rsnd_kctrl_remove(dvc->volume);
137 rsnd_kctrl_remove(dvc->mute);
138 rsnd_kctrl_remove(dvc->ren);
139 rsnd_kctrl_remove(dvc->rup);
140 rsnd_kctrl_remove(dvc->rdown);
141
142 return 0;
143}
144
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700145static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000146 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000147 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700148{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700149 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
150 struct device *dev = rsnd_priv_to_dev(priv);
151 int dvc_id = rsnd_mod_id(dvc_mod);
152 int src_id = rsnd_mod_id(src_mod);
153 u32 route[] = {
154 [0] = 0x30000,
155 [1] = 0x30001,
156 [2] = 0x40000,
157 [3] = 0x10000,
158 [4] = 0x20000,
159 [5] = 0x40100
160 };
161
162 if (src_id >= ARRAY_SIZE(route)) {
163 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
164 return -EINVAL;
165 }
166
Kuninori Morimoto85642952015-01-15 08:03:22 +0000167 rsnd_mod_hw_start(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700168
Kuninori Morimoto636e4ba2015-07-15 07:12:00 +0000169 rsnd_dvc_soft_reset(dvc_mod);
170
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700171 /*
172 * fixme
173 * it doesn't support CTU/MIX
174 */
175 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
176
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700177 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
178
Kuninori Morimoto4e2639f2015-06-15 06:26:08 +0000179 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod, io));
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700180
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700181 /* ch0/ch1 Volume */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000182 rsnd_dvc_volume_update(io, dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700183
184 rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
185
Kuninori Morimotof708d942015-01-15 08:07:19 +0000186 rsnd_adg_set_cmd_timsel_gen2(dvc_mod, io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700187
188 return 0;
189}
190
191static int rsnd_dvc_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000192 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000193 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700194{
Kuninori Morimoto85642952015-01-15 08:03:22 +0000195 rsnd_mod_hw_stop(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700196
197 return 0;
198}
199
200static int rsnd_dvc_start(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000201 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000202 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700203{
204 rsnd_mod_write(mod, CMD_CTRL, 0x10);
205
206 return 0;
207}
208
209static int rsnd_dvc_stop(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000210 struct rsnd_dai_stream *io,
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 Morimoto2c0fac12015-06-15 06:25:20 +0000219 struct rsnd_dai_stream *io,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700220 struct snd_soc_pcm_runtime *rtd)
221{
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 Morimotob65a7cc2015-06-15 06:27:28 +0000227 ret = rsnd_kctrl_new_m(mod, io, 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 Morimotob65a7cc2015-06-15 06:27:28 +0000236 ret = rsnd_kctrl_new_m(mod, io, 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 Morimotob65a7cc2015-06-15 06:27:28 +0000245 ret = rsnd_kctrl_new_s(mod, io, 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 Morimotob65a7cc2015-06-15 06:27:28 +0000253 ret = rsnd_kctrl_new_e(mod, io, 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 Morimotob65a7cc2015-06-15 06:27:28 +0000262 ret = rsnd_kctrl_new_e(mod, io, 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
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000275static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
276 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000277{
278 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
279
280 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
281 mod, "tx");
282}
283
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700284static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700285 .name = DVC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000286 .dma_req = rsnd_dvc_dma_req,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000287 .remove = rsnd_dvc_remove_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700288 .init = rsnd_dvc_init,
289 .quit = rsnd_dvc_quit,
290 .start = rsnd_dvc_start,
291 .stop = rsnd_dvc_stop,
292 .pcm_new = rsnd_dvc_pcm_new,
293};
294
295struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
296{
297 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
298 id = 0;
299
300 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
301}
302
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700303static void rsnd_of_parse_dvc(struct platform_device *pdev,
304 const struct rsnd_of_data *of_data,
305 struct rsnd_priv *priv)
306{
307 struct device_node *node;
308 struct rsnd_dvc_platform_info *dvc_info;
309 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
310 struct device *dev = &pdev->dev;
311 int nr;
312
313 if (!of_data)
314 return;
315
316 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
317 if (!node)
318 return;
319
320 nr = of_get_child_count(node);
321 if (!nr)
322 goto rsnd_of_parse_dvc_end;
323
324 dvc_info = devm_kzalloc(dev,
325 sizeof(struct rsnd_dvc_platform_info) * nr,
326 GFP_KERNEL);
327 if (!dvc_info) {
328 dev_err(dev, "dvc info allocation error\n");
329 goto rsnd_of_parse_dvc_end;
330 }
331
332 info->dvc_info = dvc_info;
333 info->dvc_info_nr = nr;
334
335rsnd_of_parse_dvc_end:
336 of_node_put(node);
337}
338
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700339int rsnd_dvc_probe(struct platform_device *pdev,
340 const struct rsnd_of_data *of_data,
341 struct rsnd_priv *priv)
342{
343 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
344 struct device *dev = rsnd_priv_to_dev(priv);
345 struct rsnd_dvc *dvc;
346 struct clk *clk;
347 char name[RSND_DVC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000348 int i, nr, ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700349
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700350 rsnd_of_parse_dvc(pdev, of_data, priv);
351
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700352 nr = info->dvc_info_nr;
353 if (!nr)
354 return 0;
355
356 /* This driver doesn't support Gen1 at this point */
357 if (rsnd_is_gen1(priv)) {
358 dev_warn(dev, "CMD is not supported on Gen1\n");
359 return -EINVAL;
360 }
361
362 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
Kuninori Morimoto6abcae32015-07-15 07:09:07 +0000363 if (!dvc)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700364 return -ENOMEM;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700365
366 priv->dvc_nr = nr;
367 priv->dvc = dvc;
368
369 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700370 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
371 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700372
373 clk = devm_clk_get(dev, name);
374 if (IS_ERR(clk))
375 return PTR_ERR(clk);
376
377 dvc->info = &info->dvc_info[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700378
Kuninori Morimoto2099bc82015-06-15 06:24:54 +0000379 ret = rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops,
Kuninori Morimoto85642952015-01-15 08:03:22 +0000380 clk, RSND_MOD_DVC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000381 if (ret)
382 return ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700383 }
384
385 return 0;
386}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000387
388void rsnd_dvc_remove(struct platform_device *pdev,
389 struct rsnd_priv *priv)
390{
391 struct rsnd_dvc *dvc;
392 int i;
393
394 for_each_rsnd_dvc(dvc, priv, i) {
395 rsnd_mod_quit(&dvc->mod);
396 }
397}