blob: 58f690900e6d36a704be976b5a440550df76726a [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 Morimoto3bb3d362015-07-15 07:13:29 +000073#define rsnd_dvc_initialize_lock(mod) __rsnd_dvc_initialize_lock(mod, 1)
74#define rsnd_dvc_initialize_unlock(mod) __rsnd_dvc_initialize_lock(mod, 0)
75static void __rsnd_dvc_initialize_lock(struct rsnd_mod *mod, u32 enable)
76{
77 rsnd_mod_write(mod, DVC_DVUIR, enable);
78}
79
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +000080static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
81 struct rsnd_mod *mod)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070082{
83 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080084 u32 val[RSND_DVC_CHANNELS];
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080085 u32 dvucr = 0;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070086 u32 mute = 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070087 int i;
88
Kuninori Morimotoec14af92014-11-04 20:27:46 -080089 for (i = 0; i < dvc->mute.cfg.size; i++)
90 mute |= (!!dvc->mute.cfg.val[i]) << i;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070091
Kuninori Morimoto140bab82014-11-04 20:27:18 -080092 /* Disable DVC Register access */
93 rsnd_mod_write(mod, DVC_DVUER, 0);
94
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080095 /* Enable Ramp */
96 if (dvc->ren.val) {
97 dvucr |= 0x10;
98
99 /* Digital Volume Max */
100 for (i = 0; i < RSND_DVC_CHANNELS; i++)
101 val[i] = dvc->volume.cfg.max;
102
103 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
104 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
105 dvc->rdown.val);
106 /*
107 * FIXME !!
108 * use scale-downed Digital Volume
109 * as Volume Ramp
110 * 7F FFFF -> 3FF
111 */
112 rsnd_mod_write(mod, DVC_VRDBR,
113 0x3ff - (dvc->volume.val[0] >> 13));
114
115 } else {
116 for (i = 0; i < RSND_DVC_CHANNELS; i++)
117 val[i] = dvc->volume.val[i];
118 }
119
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800120 /* Enable Digital Volume */
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800121 dvucr |= 0x100;
122 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
123 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700124
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800125 /* Enable Mute */
126 if (mute) {
127 dvucr |= 0x1;
128 rsnd_mod_write(mod, DVC_ZCMCR, mute);
129 }
130
131 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
Kuninori Morimoto140bab82014-11-04 20:27:18 -0800132
133 /* Enable DVC Register access */
134 rsnd_mod_write(mod, DVC_DVUER, 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700135}
136
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000137static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000138 struct rsnd_dai_stream *io,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000139 struct rsnd_priv *priv)
140{
141 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
142
143 rsnd_kctrl_remove(dvc->volume);
144 rsnd_kctrl_remove(dvc->mute);
145 rsnd_kctrl_remove(dvc->ren);
146 rsnd_kctrl_remove(dvc->rup);
147 rsnd_kctrl_remove(dvc->rdown);
148
149 return 0;
150}
151
Kuninori Morimotoe2c08412015-07-15 07:13:10 +0000152static int rsnd_dvc_init(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000153 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000154 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700155{
Kuninori Morimotoc9929342015-10-22 03:15:04 +0000156 rsnd_mod_power_on(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700157
Kuninori Morimotoe2c08412015-07-15 07:13:10 +0000158 rsnd_dvc_soft_reset(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700159
Kuninori Morimoto3bb3d362015-07-15 07:13:29 +0000160 rsnd_dvc_initialize_lock(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700161
Kuninori Morimoto3bb3d362015-07-15 07:13:29 +0000162 rsnd_path_parse(priv, io);
Kuninori Morimoto636e4ba2015-07-15 07:12:00 +0000163
Kuninori Morimoto3023b382015-07-15 07:14:05 +0000164 rsnd_mod_write(mod, DVC_ADINR, rsnd_get_adinr_bit(mod, io));
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700165
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700166 /* ch0/ch1 Volume */
Kuninori Morimotoe2c08412015-07-15 07:13:10 +0000167 rsnd_dvc_volume_update(io, mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700168
Kuninori Morimotoe2c08412015-07-15 07:13:10 +0000169 rsnd_adg_set_cmd_timsel_gen2(mod, io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700170
171 return 0;
172}
173
174static int rsnd_dvc_quit(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000175 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000176 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700177{
Kuninori Morimotoc9929342015-10-22 03:15:04 +0000178 rsnd_mod_power_off(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700179
180 return 0;
181}
182
183static int rsnd_dvc_start(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000184 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000185 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700186{
Kuninori Morimoto3bb3d362015-07-15 07:13:29 +0000187 rsnd_dvc_initialize_unlock(mod);
188
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700189 rsnd_mod_write(mod, CMD_CTRL, 0x10);
190
191 return 0;
192}
193
194static int rsnd_dvc_stop(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000195 struct rsnd_dai_stream *io,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000196 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700197{
198 rsnd_mod_write(mod, CMD_CTRL, 0);
199
200 return 0;
201}
202
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700203static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
Kuninori Morimoto2c0fac12015-06-15 06:25:20 +0000204 struct rsnd_dai_stream *io,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700205 struct snd_soc_pcm_runtime *rtd)
206{
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700207 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000208 int is_play = rsnd_io_is_play(io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700209 int ret;
210
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700211 /* Volume */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000212 ret = rsnd_kctrl_new_m(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000213 is_play ?
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700214 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000215 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800216 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700217 if (ret < 0)
218 return ret;
219
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700220 /* Mute */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000221 ret = rsnd_kctrl_new_m(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000222 is_play ?
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700223 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000224 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800225 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700226 if (ret < 0)
227 return ret;
228
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800229 /* Ramp */
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000230 ret = rsnd_kctrl_new_s(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000231 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800232 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000233 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800234 &dvc->ren, 1);
235 if (ret < 0)
236 return ret;
237
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000238 ret = rsnd_kctrl_new_e(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000239 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800240 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
241 &dvc->rup,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000242 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800243 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
244 if (ret < 0)
245 return ret;
246
Kuninori Morimotob65a7cc2015-06-15 06:27:28 +0000247 ret = rsnd_kctrl_new_e(mod, io, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000248 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800249 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
250 &dvc->rdown,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000251 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800252 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
253
254 if (ret < 0)
255 return ret;
256
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700257 return 0;
258}
259
Kuninori Morimoto9b99e9a2015-06-15 06:26:25 +0000260static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
261 struct rsnd_mod *mod)
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000262{
263 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
264
265 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
266 mod, "tx");
267}
268
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700269static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700270 .name = DVC_NAME,
Kuninori Morimoto72adc612015-02-20 10:31:23 +0000271 .dma_req = rsnd_dvc_dma_req,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000272 .remove = rsnd_dvc_remove_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700273 .init = rsnd_dvc_init,
274 .quit = rsnd_dvc_quit,
275 .start = rsnd_dvc_start,
276 .stop = rsnd_dvc_stop,
277 .pcm_new = rsnd_dvc_pcm_new,
278};
279
280struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
281{
282 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
283 id = 0;
284
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000285 return rsnd_mod_get((struct rsnd_dvc *)(priv->dvc) + id);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700286}
287
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700288static void rsnd_of_parse_dvc(struct platform_device *pdev,
289 const struct rsnd_of_data *of_data,
290 struct rsnd_priv *priv)
291{
292 struct device_node *node;
293 struct rsnd_dvc_platform_info *dvc_info;
294 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
295 struct device *dev = &pdev->dev;
296 int nr;
297
298 if (!of_data)
299 return;
300
301 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
302 if (!node)
303 return;
304
305 nr = of_get_child_count(node);
306 if (!nr)
307 goto rsnd_of_parse_dvc_end;
308
309 dvc_info = devm_kzalloc(dev,
310 sizeof(struct rsnd_dvc_platform_info) * nr,
311 GFP_KERNEL);
312 if (!dvc_info) {
313 dev_err(dev, "dvc info allocation error\n");
314 goto rsnd_of_parse_dvc_end;
315 }
316
317 info->dvc_info = dvc_info;
318 info->dvc_info_nr = nr;
319
320rsnd_of_parse_dvc_end:
321 of_node_put(node);
322}
323
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700324int rsnd_dvc_probe(struct platform_device *pdev,
325 const struct rsnd_of_data *of_data,
326 struct rsnd_priv *priv)
327{
328 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
329 struct device *dev = rsnd_priv_to_dev(priv);
330 struct rsnd_dvc *dvc;
331 struct clk *clk;
332 char name[RSND_DVC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000333 int i, nr, ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700334
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700335 /* This driver doesn't support Gen1 at this point */
Kuninori Morimoto8a98b422015-10-15 03:25:28 +0000336 if (rsnd_is_gen1(priv))
337 return 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700338
Kuninori Morimoto9469b8b2015-07-15 07:15:47 +0000339 rsnd_of_parse_dvc(pdev, of_data, priv);
340
341 nr = info->dvc_info_nr;
342 if (!nr)
343 return 0;
344
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700345 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
Kuninori Morimoto6abcae32015-07-15 07:09:07 +0000346 if (!dvc)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700347 return -ENOMEM;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700348
349 priv->dvc_nr = nr;
350 priv->dvc = dvc;
351
352 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700353 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
354 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700355
356 clk = devm_clk_get(dev, name);
357 if (IS_ERR(clk))
358 return PTR_ERR(clk);
359
360 dvc->info = &info->dvc_info[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700361
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000362 ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
Kuninori Morimoto85642952015-01-15 08:03:22 +0000363 clk, RSND_MOD_DVC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000364 if (ret)
365 return ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700366 }
367
368 return 0;
369}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000370
371void rsnd_dvc_remove(struct platform_device *pdev,
372 struct rsnd_priv *priv)
373{
374 struct rsnd_dvc *dvc;
375 int i;
376
377 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimotob76e2182015-09-10 07:02:21 +0000378 rsnd_mod_quit(rsnd_mod_get(dvc));
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000379 }
380}