xref: /linux/sound/soc/renesas/rcar/dvc.c (revision 9611c0ce215a66770ccbe5c126bf57ba8c31bcad)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car DVC support
4 //
5 // Copyright (C) 2014 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 
8 /*
9  * Playback Volume
10  *	amixer set "DVC Out" 100%
11  *
12  * Capture Volume
13  *	amixer set "DVC In" 100%
14  *
15  * Playback Mute
16  *	amixer set "DVC Out Mute" on
17  *
18  * Capture Mute
19  *	amixer set "DVC In Mute" on
20  *
21  * Volume Ramp
22  *	amixer set "DVC Out Ramp Up Rate"   "0.125 dB/64 steps"
23  *	amixer set "DVC Out Ramp Down Rate" "0.125 dB/512 steps"
24  *	amixer set "DVC Out Ramp" on
25  *	aplay xxx.wav &
26  *	amixer set "DVC Out"  80%  // Volume Down
27  *	amixer set "DVC Out" 100%  // Volume Up
28  */
29 
30 #include "rsnd.h"
31 
32 
33 #define DVC_NAME "dvc"
34 
35 struct rsnd_dvc {
36 	struct rsnd_mod mod;
37 	struct rsnd_kctrl_cfg_m volume;
38 	struct rsnd_kctrl_cfg_m mute;
39 	struct rsnd_kctrl_cfg_s ren;	/* Ramp Enable */
40 	struct rsnd_kctrl_cfg_s rup;	/* Ramp Rate Up */
41 	struct rsnd_kctrl_cfg_s rdown;	/* Ramp Rate Down */
42 };
43 
44 #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
45 #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
46 
47 #define rsnd_mod_to_dvc(_mod)	\
48 	container_of((_mod), struct rsnd_dvc, mod)
49 
50 #define for_each_rsnd_dvc(pos, priv, i)				\
51 	for ((i) = 0;						\
52 	     ((i) < rsnd_dvc_nr(priv)) &&			\
53 	     ((pos) = (struct rsnd_dvc *)(priv)->dvc + i);	\
54 	     i++)
55 
56 static void rsnd_dvc_activation(struct rsnd_mod *mod)
57 {
58 	rsnd_mod_write(mod, DVC_SWRSR, 0);
59 	rsnd_mod_write(mod, DVC_SWRSR, 1);
60 }
61 
62 static void rsnd_dvc_halt(struct rsnd_mod *mod)
63 {
64 	rsnd_mod_write(mod, DVC_DVUIR, 1);
65 	rsnd_mod_write(mod, DVC_SWRSR, 0);
66 }
67 
68 #define rsnd_dvc_get_vrpdr(dvc) (rsnd_kctrl_vals(dvc->rup) << 8 | \
69 				 rsnd_kctrl_vals(dvc->rdown))
70 #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (rsnd_kctrl_valm(dvc->volume, 0) >> 13))
71 
72 static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream *io,
73 					      struct rsnd_mod *mod)
74 {
75 	struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
76 	u32 val[RSND_MAX_CHANNELS];
77 	int i;
78 
79 	/* Enable Ramp */
80 	if (rsnd_kctrl_vals(dvc->ren))
81 		for (i = 0; i < RSND_MAX_CHANNELS; i++)
82 			val[i] = rsnd_kctrl_max(dvc->volume);
83 	else
84 		for (i = 0; i < RSND_MAX_CHANNELS; i++)
85 			val[i] = rsnd_kctrl_valm(dvc->volume, i);
86 
87 	/* Enable Digital Volume */
88 	for (i = 0; i < RSND_MAX_CHANNELS; i++)
89 		rsnd_mod_write(mod, DVC_VOLxR(i), val[i]);
90 }
91 
92 static void rsnd_dvc_volume_init(struct rsnd_dai_stream *io,
93 				 struct rsnd_mod *mod)
94 {
95 	struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
96 	u32 adinr = 0;
97 	u32 dvucr = 0;
98 	u32 vrctr = 0;
99 	u32 vrpdr = 0;
100 	u32 vrdbr = 0;
101 
102 	adinr = rsnd_get_adinr_bit(mod, io) |
103 		rsnd_runtime_channel_after_ctu(io);
104 
105 	/* Enable Digital Volume, Zero Cross Mute Mode */
106 	dvucr |= 0x101;
107 
108 	/* Enable Ramp */
109 	if (rsnd_kctrl_vals(dvc->ren)) {
110 		dvucr |= 0x10;
111 
112 		/*
113 		 * FIXME !!
114 		 * use scale-downed Digital Volume
115 		 * as Volume Ramp
116 		 * 7F FFFF -> 3FF
117 		 */
118 		vrctr = 0xff;
119 		vrpdr = rsnd_dvc_get_vrpdr(dvc);
120 		vrdbr = rsnd_dvc_get_vrdbr(dvc);
121 	}
122 
123 	/* Initialize operation */
124 	rsnd_mod_write(mod, DVC_DVUIR, 1);
125 
126 	/* General Information */
127 	rsnd_mod_write(mod, DVC_ADINR, adinr);
128 	rsnd_mod_write(mod, DVC_DVUCR, dvucr);
129 
130 	/* Volume Ramp Parameter */
131 	rsnd_mod_write(mod, DVC_VRCTR, vrctr);
132 	rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
133 	rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
134 
135 	/* Digital Volume Function Parameter */
136 	rsnd_dvc_volume_parameter(io, mod);
137 
138 	/* cancel operation */
139 	rsnd_mod_write(mod, DVC_DVUIR, 0);
140 }
141 
142 static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
143 				   struct rsnd_mod *mod)
144 {
145 	struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
146 	u32 zcmcr = 0;
147 	u32 vrpdr = 0;
148 	u32 vrdbr = 0;
149 	int i;
150 
151 	for (i = 0; i < rsnd_kctrl_size(dvc->mute); i++)
152 		zcmcr |= (!!rsnd_kctrl_valm(dvc->mute, i)) << i;
153 
154 	if (rsnd_kctrl_vals(dvc->ren)) {
155 		vrpdr = rsnd_dvc_get_vrpdr(dvc);
156 		vrdbr = rsnd_dvc_get_vrdbr(dvc);
157 	}
158 
159 	/* Disable DVC Register access */
160 	rsnd_mod_write(mod, DVC_DVUER, 0);
161 
162 	/* Zero Cross Mute Function */
163 	rsnd_mod_write(mod, DVC_ZCMCR, zcmcr);
164 
165 	/* Volume Ramp Function */
166 	rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
167 	rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
168 	/* add DVC_VRWTR here */
169 
170 	/* Digital Volume Function Parameter */
171 	rsnd_dvc_volume_parameter(io, mod);
172 
173 	/* Enable DVC Register access */
174 	rsnd_mod_write(mod, DVC_DVUER, 1);
175 }
176 
177 static int rsnd_dvc_probe_(struct rsnd_mod *mod,
178 			   struct rsnd_dai_stream *io,
179 			   struct rsnd_priv *priv)
180 {
181 	return rsnd_cmd_attach(io, rsnd_mod_id(mod));
182 }
183 
184 static int rsnd_dvc_init(struct rsnd_mod *mod,
185 			 struct rsnd_dai_stream *io,
186 			 struct rsnd_priv *priv)
187 {
188 	int ret;
189 
190 	ret = rsnd_mod_power_on(mod);
191 	if (ret < 0)
192 		return ret;
193 
194 	rsnd_dvc_activation(mod);
195 
196 	rsnd_dvc_volume_init(io, mod);
197 
198 	rsnd_dvc_volume_update(io, mod);
199 
200 	return 0;
201 }
202 
203 static int rsnd_dvc_quit(struct rsnd_mod *mod,
204 			 struct rsnd_dai_stream *io,
205 			 struct rsnd_priv *priv)
206 {
207 	rsnd_dvc_halt(mod);
208 
209 	rsnd_mod_power_off(mod);
210 
211 	return 0;
212 }
213 
214 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
215 			    struct rsnd_dai_stream *io,
216 			    struct snd_soc_pcm_runtime *rtd)
217 {
218 	struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
219 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
220 	int is_play = rsnd_io_is_play(io);
221 	int channels = rsnd_rdai_channels_get(rdai);
222 	int ret;
223 
224 	/* Volume */
225 	ret = rsnd_kctrl_new_m(mod, io, rtd,
226 			is_play ?
227 			"DVC Out Playback Volume" : "DVC In Capture Volume",
228 			rsnd_kctrl_accept_anytime,
229 			rsnd_dvc_volume_update,
230 			&dvc->volume, channels,
231 			0x00800000 - 1);
232 	if (ret < 0)
233 		return ret;
234 
235 	/* Mute */
236 	ret = rsnd_kctrl_new_m(mod, io, rtd,
237 			is_play ?
238 			"DVC Out Mute Switch" : "DVC In Mute Switch",
239 			rsnd_kctrl_accept_anytime,
240 			rsnd_dvc_volume_update,
241 			&dvc->mute, channels,
242 			1);
243 	if (ret < 0)
244 		return ret;
245 
246 	/* Ramp */
247 	ret = rsnd_kctrl_new_s(mod, io, rtd,
248 			is_play ?
249 			"DVC Out Ramp Switch" : "DVC In Ramp Switch",
250 			rsnd_kctrl_accept_anytime,
251 			rsnd_dvc_volume_update,
252 			&dvc->ren, 1);
253 	if (ret < 0)
254 		return ret;
255 
256 	ret = rsnd_kctrl_new_e(mod, io, rtd,
257 			is_play ?
258 			"DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
259 			rsnd_kctrl_accept_anytime,
260 			rsnd_dvc_volume_update,
261 			&dvc->rup,
262 			volume_ramp_rate,
263 			VOLUME_RAMP_MAX_DVC);
264 	if (ret < 0)
265 		return ret;
266 
267 	ret = rsnd_kctrl_new_e(mod, io, rtd,
268 			is_play ?
269 			"DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
270 			rsnd_kctrl_accept_anytime,
271 			rsnd_dvc_volume_update,
272 			&dvc->rdown,
273 			volume_ramp_rate,
274 			VOLUME_RAMP_MAX_DVC);
275 
276 	if (ret < 0)
277 		return ret;
278 
279 	return 0;
280 }
281 
282 static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
283 					 struct rsnd_mod *mod)
284 {
285 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
286 
287 	return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
288 					DVC_NAME, mod, "tx");
289 }
290 
291 #ifdef CONFIG_DEBUG_FS
292 static void rsnd_dvc_debug_info(struct seq_file *m,
293 				struct rsnd_dai_stream *io,
294 				struct rsnd_mod *mod)
295 {
296 	rsnd_debugfs_mod_reg_show(m, mod, RSND_BASE_SCU,
297 				  0xe00 + rsnd_mod_id(mod) * 0x100, 0x60);
298 }
299 #define DEBUG_INFO .debug_info = rsnd_dvc_debug_info
300 #else
301 #define DEBUG_INFO
302 #endif
303 
304 static struct rsnd_mod_ops rsnd_dvc_ops = {
305 	.name		= DVC_NAME,
306 	.dma_req	= rsnd_dvc_dma_req,
307 	.probe		= rsnd_dvc_probe_,
308 	.init		= rsnd_dvc_init,
309 	.quit		= rsnd_dvc_quit,
310 	.pcm_new	= rsnd_dvc_pcm_new,
311 	.get_status	= rsnd_mod_get_status,
312 	DEBUG_INFO
313 };
314 
315 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
316 {
317 	if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
318 		id = 0;
319 
320 	return rsnd_mod_get(rsnd_dvc_get(priv, id));
321 }
322 
323 int rsnd_dvc_probe(struct rsnd_priv *priv)
324 {
325 	struct device_node *node;
326 	struct device *dev = rsnd_priv_to_dev(priv);
327 	struct rsnd_dvc *dvc;
328 	struct clk *clk;
329 	int i, nr, ret;
330 
331 	node = rsnd_dvc_of_node(priv);
332 	if (!node)
333 		return 0; /* not used is not error */
334 
335 	nr = of_get_child_count(node);
336 	if (!nr) {
337 		ret = -EINVAL;
338 		goto rsnd_dvc_probe_done;
339 	}
340 
341 	dvc	= devm_kcalloc(dev, nr, sizeof(*dvc), GFP_KERNEL);
342 	if (!dvc) {
343 		ret = -ENOMEM;
344 		goto rsnd_dvc_probe_done;
345 	}
346 
347 	priv->dvc_nr	= nr;
348 	priv->dvc	= dvc;
349 
350 	i = 0;
351 	ret = 0;
352 	for_each_child_of_node_scoped(node, np) {
353 		dvc = rsnd_dvc_get(priv, i);
354 
355 		clk = rsnd_devm_clk_get_indexed(dev, DVC_NAME, i);
356 		if (IS_ERR(clk)) {
357 			ret = PTR_ERR(clk);
358 			goto rsnd_dvc_probe_done;
359 		}
360 
361 		ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
362 				    clk, NULL, RSND_MOD_DVC, i);
363 		if (ret)
364 			goto rsnd_dvc_probe_done;
365 
366 		i++;
367 	}
368 
369 rsnd_dvc_probe_done:
370 	of_node_put(node);
371 
372 	return ret;
373 }
374 
375 void rsnd_dvc_remove(struct rsnd_priv *priv)
376 {
377 	struct rsnd_dvc *dvc;
378 	int i;
379 
380 	for_each_rsnd_dvc(dvc, priv, i) {
381 		rsnd_mod_quit(rsnd_mod_get(dvc));
382 	}
383 }
384 
385 void rsnd_dvc_suspend(struct rsnd_priv *priv)
386 {
387 	struct rsnd_dvc *dvc;
388 	int i;
389 
390 	for_each_rsnd_dvc(dvc, priv, i)
391 		rsnd_suspend_clk_reset(rsnd_mod_get(dvc)->clk,
392 				       rsnd_mod_get(dvc)->rstc);
393 }
394 
395 void rsnd_dvc_resume(struct rsnd_priv *priv)
396 {
397 	struct rsnd_dvc *dvc;
398 	int i;
399 
400 	for_each_rsnd_dvc(dvc, priv, i)
401 		rsnd_resume_clk_reset(rsnd_mod_get(dvc)->clk,
402 				      rsnd_mod_get(dvc)->rstc);
403 }
404