blob: 261997a3f5899d0b3ed357211181eeb00bf7310c [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
27#define rsnd_mod_to_dvc(_mod) \
28 container_of((_mod), struct rsnd_dvc, mod)
29
30#define for_each_rsnd_dvc(pos, priv, i) \
31 for ((i) = 0; \
32 ((i) < rsnd_dvc_nr(priv)) && \
33 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
34 i++)
35
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080036static const char const *dvc_ramp_rate[] = {
37 "128 dB/1 step", /* 00000 */
38 "64 dB/1 step", /* 00001 */
39 "32 dB/1 step", /* 00010 */
40 "16 dB/1 step", /* 00011 */
41 "8 dB/1 step", /* 00100 */
42 "4 dB/1 step", /* 00101 */
43 "2 dB/1 step", /* 00110 */
44 "1 dB/1 step", /* 00111 */
45 "0.5 dB/1 step", /* 01000 */
46 "0.25 dB/1 step", /* 01001 */
47 "0.125 dB/1 step", /* 01010 */
48 "0.125 dB/2 steps", /* 01011 */
49 "0.125 dB/4 steps", /* 01100 */
50 "0.125 dB/8 steps", /* 01101 */
51 "0.125 dB/16 steps", /* 01110 */
52 "0.125 dB/32 steps", /* 01111 */
53 "0.125 dB/64 steps", /* 10000 */
54 "0.125 dB/128 steps", /* 10001 */
55 "0.125 dB/256 steps", /* 10010 */
56 "0.125 dB/512 steps", /* 10011 */
57 "0.125 dB/1024 steps", /* 10100 */
58 "0.125 dB/2048 steps", /* 10101 */
59 "0.125 dB/4096 steps", /* 10110 */
60 "0.125 dB/8192 steps", /* 10111 */
61};
62
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070063static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
64{
65 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080066 u32 val[RSND_DVC_CHANNELS];
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -080067 u32 dvucr = 0;
Kuninori Morimotocd2b6572014-08-01 03:10:55 -070068 u32 mute = 0;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070069 int i;
70
Kuninori Morimotoec14af92014-11-04 20:27:46 -080071 for (i = 0; i < dvc->mute.cfg.size; i++)
72 mute |= (!!dvc->mute.cfg.val[i]) << i;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070073
Kuninori Morimoto140bab82014-11-04 20:27:18 -080074 /* Disable DVC Register access */
75 rsnd_mod_write(mod, DVC_DVUER, 0);
76
Kuninori Morimoto3539cac2014-11-09 19:52:06 -080077 /* Enable Ramp */
78 if (dvc->ren.val) {
79 dvucr |= 0x10;
80
81 /* Digital Volume Max */
82 for (i = 0; i < RSND_DVC_CHANNELS; i++)
83 val[i] = dvc->volume.cfg.max;
84
85 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
86 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
87 dvc->rdown.val);
88 /*
89 * FIXME !!
90 * use scale-downed Digital Volume
91 * as Volume Ramp
92 * 7F FFFF -> 3FF
93 */
94 rsnd_mod_write(mod, DVC_VRDBR,
95 0x3ff - (dvc->volume.val[0] >> 13));
96
97 } else {
98 for (i = 0; i < RSND_DVC_CHANNELS; i++)
99 val[i] = dvc->volume.val[i];
100 }
101
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800102 /* Enable Digital Volume */
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800103 dvucr |= 0x100;
104 rsnd_mod_write(mod, DVC_VOL0R, val[0]);
105 rsnd_mod_write(mod, DVC_VOL1R, val[1]);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700106
Kuninori Morimoto1c5d1c92014-11-04 20:26:53 -0800107 /* Enable Mute */
108 if (mute) {
109 dvucr |= 0x1;
110 rsnd_mod_write(mod, DVC_ZCMCR, mute);
111 }
112
113 rsnd_mod_write(mod, DVC_DVUCR, dvucr);
Kuninori Morimoto140bab82014-11-04 20:27:18 -0800114
115 /* Enable DVC Register access */
116 rsnd_mod_write(mod, DVC_DVUER, 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700117}
118
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700119static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000120 struct rsnd_priv *priv)
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700121{
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700122 struct device *dev = rsnd_priv_to_dev(priv);
123
Kuninori Morimoto30cc4fa2014-11-09 20:00:30 -0800124 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
125 rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700126
127 return 0;
128}
129
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000130static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
131 struct rsnd_priv *priv)
132{
133 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
134
135 rsnd_kctrl_remove(dvc->volume);
136 rsnd_kctrl_remove(dvc->mute);
137 rsnd_kctrl_remove(dvc->ren);
138 rsnd_kctrl_remove(dvc->rup);
139 rsnd_kctrl_remove(dvc->rdown);
140
141 return 0;
142}
143
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700144static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000145 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700146{
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700147 struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700148 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
149 struct device *dev = rsnd_priv_to_dev(priv);
150 int dvc_id = rsnd_mod_id(dvc_mod);
151 int src_id = rsnd_mod_id(src_mod);
152 u32 route[] = {
153 [0] = 0x30000,
154 [1] = 0x30001,
155 [2] = 0x40000,
156 [3] = 0x10000,
157 [4] = 0x20000,
158 [5] = 0x40100
159 };
160
161 if (src_id >= ARRAY_SIZE(route)) {
162 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
163 return -EINVAL;
164 }
165
Kuninori Morimoto85642952015-01-15 08:03:22 +0000166 rsnd_mod_hw_start(dvc_mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700167
168 /*
169 * fixme
170 * it doesn't support CTU/MIX
171 */
172 rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
173
174 rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
175 rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
176
177 rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
178
179 rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
180
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700181 /* ch0/ch1 Volume */
182 rsnd_dvc_volume_update(dvc_mod);
183
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 Morimoto690602f2015-01-15 08:07:47 +0000192 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700193{
Kuninori Morimoto85642952015-01-15 08:03:22 +0000194 rsnd_mod_hw_stop(mod);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700195
196 return 0;
197}
198
199static int rsnd_dvc_start(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000200 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700201{
202 rsnd_mod_write(mod, CMD_CTRL, 0x10);
203
204 return 0;
205}
206
207static int rsnd_dvc_stop(struct rsnd_mod *mod,
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000208 struct rsnd_priv *priv)
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700209{
210 rsnd_mod_write(mod, CMD_CTRL, 0);
211
212 return 0;
213}
214
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700215static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700216 struct snd_soc_pcm_runtime *rtd)
217{
218 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700219 struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000220 int is_play = rsnd_io_is_play(io);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700221 int ret;
222
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700223 /* Volume */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000224 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000225 is_play ?
Kuninori Morimoto486b09c2014-08-01 03:10:47 -0700226 "DVC Out Playback Volume" : "DVC In Capture Volume",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000227 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800228 &dvc->volume, 0x00800000 - 1);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700229 if (ret < 0)
230 return ret;
231
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700232 /* Mute */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000233 ret = rsnd_kctrl_new_m(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000234 is_play ?
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700235 "DVC Out Mute Switch" : "DVC In Mute Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000236 rsnd_dvc_volume_update,
Kuninori Morimotoec14af92014-11-04 20:27:46 -0800237 &dvc->mute, 1);
Kuninori Morimotocd2b6572014-08-01 03:10:55 -0700238 if (ret < 0)
239 return ret;
240
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800241 /* Ramp */
Kuninori Morimotof708d942015-01-15 08:07:19 +0000242 ret = rsnd_kctrl_new_s(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000243 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800244 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000245 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800246 &dvc->ren, 1);
247 if (ret < 0)
248 return ret;
249
Kuninori Morimotof708d942015-01-15 08:07:19 +0000250 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000251 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800252 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
253 &dvc->rup,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000254 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800255 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
256 if (ret < 0)
257 return ret;
258
Kuninori Morimotof708d942015-01-15 08:07:19 +0000259 ret = rsnd_kctrl_new_e(mod, rtd,
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000260 is_play ?
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800261 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
262 &dvc->rdown,
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000263 rsnd_dvc_volume_update,
Kuninori Morimoto3539cac2014-11-09 19:52:06 -0800264 dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
265
266 if (ret < 0)
267 return ret;
268
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700269 return 0;
270}
271
272static struct rsnd_mod_ops rsnd_dvc_ops = {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700273 .name = DVC_NAME,
274 .probe = rsnd_dvc_probe_gen2,
Kuninori Morimotod1f83d62015-02-02 04:53:53 +0000275 .remove = rsnd_dvc_remove_gen2,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700276 .init = rsnd_dvc_init,
277 .quit = rsnd_dvc_quit,
278 .start = rsnd_dvc_start,
279 .stop = rsnd_dvc_stop,
280 .pcm_new = rsnd_dvc_pcm_new,
281};
282
283struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
284{
285 if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
286 id = 0;
287
288 return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
289}
290
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700291static void rsnd_of_parse_dvc(struct platform_device *pdev,
292 const struct rsnd_of_data *of_data,
293 struct rsnd_priv *priv)
294{
295 struct device_node *node;
296 struct rsnd_dvc_platform_info *dvc_info;
297 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
298 struct device *dev = &pdev->dev;
299 int nr;
300
301 if (!of_data)
302 return;
303
304 node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
305 if (!node)
306 return;
307
308 nr = of_get_child_count(node);
309 if (!nr)
310 goto rsnd_of_parse_dvc_end;
311
312 dvc_info = devm_kzalloc(dev,
313 sizeof(struct rsnd_dvc_platform_info) * nr,
314 GFP_KERNEL);
315 if (!dvc_info) {
316 dev_err(dev, "dvc info allocation error\n");
317 goto rsnd_of_parse_dvc_end;
318 }
319
320 info->dvc_info = dvc_info;
321 info->dvc_info_nr = nr;
322
323rsnd_of_parse_dvc_end:
324 of_node_put(node);
325}
326
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700327int rsnd_dvc_probe(struct platform_device *pdev,
328 const struct rsnd_of_data *of_data,
329 struct rsnd_priv *priv)
330{
331 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
332 struct device *dev = rsnd_priv_to_dev(priv);
333 struct rsnd_dvc *dvc;
334 struct clk *clk;
335 char name[RSND_DVC_NAME_SIZE];
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000336 int i, nr, ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700337
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700338 rsnd_of_parse_dvc(pdev, of_data, priv);
339
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700340 nr = info->dvc_info_nr;
341 if (!nr)
342 return 0;
343
344 /* This driver doesn't support Gen1 at this point */
345 if (rsnd_is_gen1(priv)) {
346 dev_warn(dev, "CMD is not supported on Gen1\n");
347 return -EINVAL;
348 }
349
350 dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
351 if (!dvc) {
352 dev_err(dev, "CMD allocate failed\n");
353 return -ENOMEM;
354 }
355
356 priv->dvc_nr = nr;
357 priv->dvc = dvc;
358
359 for_each_rsnd_dvc(dvc, priv, i) {
Kuninori Morimoto8aefda52014-05-22 23:25:43 -0700360 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
361 DVC_NAME, i);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700362
363 clk = devm_clk_get(dev, name);
364 if (IS_ERR(clk))
365 return PTR_ERR(clk);
366
367 dvc->info = &info->dvc_info[i];
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700368
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000369 ret = rsnd_mod_init(&dvc->mod, &rsnd_dvc_ops,
Kuninori Morimoto85642952015-01-15 08:03:22 +0000370 clk, RSND_MOD_DVC, i);
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000371 if (ret)
372 return ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700373
374 dev_dbg(dev, "CMD%d probed\n", i);
375 }
376
377 return 0;
378}
Kuninori Morimoto2f78dd72015-03-26 04:02:09 +0000379
380void rsnd_dvc_remove(struct platform_device *pdev,
381 struct rsnd_priv *priv)
382{
383 struct rsnd_dvc *dvc;
384 int i;
385
386 for_each_rsnd_dvc(dvc, priv, i) {
387 rsnd_mod_quit(&dvc->mod);
388 }
389}