xref: /openbmc/linux/sound/soc/sh/rcar/src.c (revision 4da722ca)
1 /*
2  * Renesas R-Car SRC support
3  *
4  * Copyright (C) 2013 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 SRC_NAME "src"
14 
15 /* SCU_SYSTEM_STATUS0/1 */
16 #define OUF_SRC(id)	((1 << (id + 16)) | (1 << id))
17 
18 struct rsnd_src {
19 	struct rsnd_mod mod;
20 	struct rsnd_mod *dma;
21 	struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
22 	struct rsnd_kctrl_cfg_s sync; /* sync convert */
23 	u32 convert_rate; /* sampling rate convert */
24 	int irq;
25 };
26 
27 #define RSND_SRC_NAME_SIZE 16
28 
29 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
30 #define rsnd_src_to_dma(src) ((src)->dma)
31 #define rsnd_src_nr(priv) ((priv)->src_nr)
32 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
33 
34 #define rsnd_mod_to_src(_mod)				\
35 	container_of((_mod), struct rsnd_src, mod)
36 
37 #define for_each_rsnd_src(pos, priv, i)				\
38 	for ((i) = 0;						\
39 	     ((i) < rsnd_src_nr(priv)) &&			\
40 	     ((pos) = (struct rsnd_src *)(priv)->src + i);	\
41 	     i++)
42 
43 
44 /*
45  *		image of SRC (Sampling Rate Converter)
46  *
47  * 96kHz   <-> +-----+	48kHz	+-----+	 48kHz	+-------+
48  * 48kHz   <-> | SRC | <------>	| SSI |	<----->	| codec |
49  * 44.1kHz <-> +-----+		+-----+		+-------+
50  * ...
51  *
52  */
53 
54 static void rsnd_src_activation(struct rsnd_mod *mod)
55 {
56 	rsnd_mod_write(mod, SRC_SWRSR, 0);
57 	rsnd_mod_write(mod, SRC_SWRSR, 1);
58 }
59 
60 static void rsnd_src_halt(struct rsnd_mod *mod)
61 {
62 	rsnd_mod_write(mod, SRC_SRCIR, 1);
63 	rsnd_mod_write(mod, SRC_SWRSR, 0);
64 }
65 
66 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
67 					 struct rsnd_mod *mod)
68 {
69 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
70 	int is_play = rsnd_io_is_play(io);
71 
72 	return rsnd_dma_request_channel(rsnd_src_of_node(priv),
73 					mod,
74 					is_play ? "rx" : "tx");
75 }
76 
77 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
78 				 struct rsnd_mod *mod)
79 {
80 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
81 	struct rsnd_src *src = rsnd_mod_to_src(mod);
82 	u32 convert_rate;
83 
84 	if (!runtime)
85 		return 0;
86 
87 	if (!rsnd_src_sync_is_enabled(mod))
88 		return src->convert_rate;
89 
90 	convert_rate = src->sync.val;
91 
92 	if (!convert_rate)
93 		convert_rate = src->convert_rate;
94 
95 	if (!convert_rate)
96 		convert_rate = runtime->rate;
97 
98 	return convert_rate;
99 }
100 
101 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
102 			       struct rsnd_dai_stream *io,
103 			       int is_in)
104 {
105 	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
106 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
107 	unsigned int rate = 0;
108 	int is_play = rsnd_io_is_play(io);
109 
110 	/*
111 	 *
112 	 * Playback
113 	 * runtime_rate -> [SRC] -> convert_rate
114 	 *
115 	 * Capture
116 	 * convert_rate -> [SRC] -> runtime_rate
117 	 */
118 
119 	if (is_play == is_in)
120 		return runtime->rate;
121 
122 	/*
123 	 * return convert rate if SRC is used,
124 	 * otherwise, return runtime->rate as usual
125 	 */
126 	if (src_mod)
127 		rate = rsnd_src_convert_rate(io, src_mod);
128 
129 	if (!rate)
130 		rate = runtime->rate;
131 
132 	return rate;
133 }
134 
135 static int rsnd_src_hw_params(struct rsnd_mod *mod,
136 			      struct rsnd_dai_stream *io,
137 			      struct snd_pcm_substream *substream,
138 			      struct snd_pcm_hw_params *fe_params)
139 {
140 	struct rsnd_src *src = rsnd_mod_to_src(mod);
141 	struct snd_soc_pcm_runtime *fe = substream->private_data;
142 
143 	/*
144 	 * SRC assumes that it is used under DPCM if user want to use
145 	 * sampling rate convert. Then, SRC should be FE.
146 	 * And then, this function will be called *after* BE settings.
147 	 * this means, each BE already has fixuped hw_params.
148 	 * see
149 	 *	dpcm_fe_dai_hw_params()
150 	 *	dpcm_be_dai_hw_params()
151 	 */
152 	src->convert_rate = 0;
153 	if (fe->dai_link->dynamic) {
154 		int stream = substream->stream;
155 		struct snd_soc_dpcm *dpcm;
156 		struct snd_pcm_hw_params *be_params;
157 
158 		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
159 			be_params = &dpcm->hw_params;
160 
161 			if (params_rate(fe_params) != params_rate(be_params))
162 				src->convert_rate = params_rate(be_params);
163 		}
164 	}
165 
166 	return 0;
167 }
168 
169 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
170 				      struct rsnd_mod *mod)
171 {
172 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
173 	struct device *dev = rsnd_priv_to_dev(priv);
174 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
175 	int is_play = rsnd_io_is_play(io);
176 	int use_src = 0;
177 	u32 fin, fout;
178 	u32 ifscr, fsrate, adinr;
179 	u32 cr, route;
180 	u32 bsdsr, bsisr;
181 	u32 i_busif, o_busif, tmp;
182 	uint ratio;
183 
184 	if (!runtime)
185 		return;
186 
187 	fin  = rsnd_src_get_in_rate(priv, io);
188 	fout = rsnd_src_get_out_rate(priv, io);
189 
190 	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
191 	if (fin == fout)
192 		ratio = 0;
193 	else if (fin > fout)
194 		ratio = 100 * fin / fout;
195 	else
196 		ratio = 100 * fout / fin;
197 
198 	if (ratio > 600) {
199 		dev_err(dev, "FSO/FSI ratio error\n");
200 		return;
201 	}
202 
203 	use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
204 
205 	/*
206 	 *	SRC_ADINR
207 	 */
208 	adinr = rsnd_get_adinr_bit(mod, io) |
209 		rsnd_runtime_channel_original(io);
210 
211 	/*
212 	 *	SRC_IFSCR / SRC_IFSVR
213 	 */
214 	ifscr = 0;
215 	fsrate = 0;
216 	if (use_src) {
217 		u64 n;
218 
219 		ifscr = 1;
220 		n = (u64)0x0400000 * fin;
221 		do_div(n, fout);
222 		fsrate = n;
223 	}
224 
225 	/*
226 	 *	SRC_SRCCR / SRC_ROUTE_MODE0
227 	 */
228 	cr	= 0x00011110;
229 	route	= 0x0;
230 	if (use_src) {
231 		route	= 0x1;
232 
233 		if (rsnd_src_sync_is_enabled(mod)) {
234 			cr |= 0x1;
235 			route |= rsnd_io_is_play(io) ?
236 				(0x1 << 24) : (0x1 << 25);
237 		}
238 	}
239 
240 	/*
241 	 * SRC_BSDSR / SRC_BSISR
242 	 */
243 	switch (rsnd_mod_id(mod)) {
244 	case 5:
245 	case 6:
246 	case 7:
247 	case 8:
248 		bsdsr = 0x02400000; /* 6 - 1/6 */
249 		bsisr = 0x00100060; /* 6 - 1/6 */
250 		break;
251 	default:
252 		bsdsr = 0x01800000; /* 6 - 1/6 */
253 		bsisr = 0x00100060 ;/* 6 - 1/6 */
254 		break;
255 	}
256 
257 	/* BUSIF_MODE */
258 	tmp = rsnd_get_busif_shift(io, mod);
259 	i_busif = ( is_play ? tmp : 0) | 1;
260 	o_busif = (!is_play ? tmp : 0) | 1;
261 
262 	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
263 
264 	rsnd_mod_write(mod, SRC_SRCIR, 1);	/* initialize */
265 	rsnd_mod_write(mod, SRC_ADINR, adinr);
266 	rsnd_mod_write(mod, SRC_IFSCR, ifscr);
267 	rsnd_mod_write(mod, SRC_IFSVR, fsrate);
268 	rsnd_mod_write(mod, SRC_SRCCR, cr);
269 	rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
270 	rsnd_mod_write(mod, SRC_BSISR, bsisr);
271 	rsnd_mod_write(mod, SRC_SRCIR, 0);	/* cancel initialize */
272 
273 	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
274 	rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
275 
276 	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
277 
278 	rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
279 }
280 
281 static int rsnd_src_irq(struct rsnd_mod *mod,
282 			struct rsnd_dai_stream *io,
283 			struct rsnd_priv *priv,
284 			int enable)
285 {
286 	struct rsnd_src *src = rsnd_mod_to_src(mod);
287 	u32 sys_int_val, int_val, sys_int_mask;
288 	int irq = src->irq;
289 	int id = rsnd_mod_id(mod);
290 
291 	sys_int_val =
292 	sys_int_mask = OUF_SRC(id);
293 	int_val = 0x3300;
294 
295 	/*
296 	 * IRQ is not supported on non-DT
297 	 * see
298 	 *	rsnd_src_probe_()
299 	 */
300 	if ((irq <= 0) || !enable) {
301 		sys_int_val = 0;
302 		int_val = 0;
303 	}
304 
305 	/*
306 	 * WORKAROUND
307 	 *
308 	 * ignore over flow error when rsnd_src_sync_is_enabled()
309 	 */
310 	if (rsnd_src_sync_is_enabled(mod))
311 		sys_int_val = sys_int_val & 0xffff;
312 
313 	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
314 	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
315 	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
316 
317 	return 0;
318 }
319 
320 static void rsnd_src_status_clear(struct rsnd_mod *mod)
321 {
322 	u32 val = OUF_SRC(rsnd_mod_id(mod));
323 
324 	rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
325 	rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
326 }
327 
328 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
329 {
330 	u32 val0, val1;
331 	bool ret = false;
332 
333 	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
334 
335 	/*
336 	 * WORKAROUND
337 	 *
338 	 * ignore over flow error when rsnd_src_sync_is_enabled()
339 	 */
340 	if (rsnd_src_sync_is_enabled(mod))
341 		val0 = val0 & 0xffff;
342 
343 	if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
344 	    (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
345 		ret = true;
346 
347 	return ret;
348 }
349 
350 static int rsnd_src_start(struct rsnd_mod *mod,
351 			  struct rsnd_dai_stream *io,
352 			  struct rsnd_priv *priv)
353 {
354 	u32 val;
355 
356 	/*
357 	 * WORKAROUND
358 	 *
359 	 * Enable SRC output if you want to use sync convert together with DVC
360 	 */
361 	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
362 		0x01 : 0x11;
363 
364 	rsnd_mod_write(mod, SRC_CTRL, val);
365 
366 	return 0;
367 }
368 
369 static int rsnd_src_stop(struct rsnd_mod *mod,
370 			 struct rsnd_dai_stream *io,
371 			 struct rsnd_priv *priv)
372 {
373 	rsnd_mod_write(mod, SRC_CTRL, 0);
374 
375 	return 0;
376 }
377 
378 static int rsnd_src_init(struct rsnd_mod *mod,
379 			 struct rsnd_dai_stream *io,
380 			 struct rsnd_priv *priv)
381 {
382 	struct rsnd_src *src = rsnd_mod_to_src(mod);
383 
384 	/* reset sync convert_rate */
385 	src->sync.val = 0;
386 
387 	rsnd_mod_power_on(mod);
388 
389 	rsnd_src_activation(mod);
390 
391 	rsnd_src_set_convert_rate(io, mod);
392 
393 	rsnd_src_status_clear(mod);
394 
395 	return 0;
396 }
397 
398 static int rsnd_src_quit(struct rsnd_mod *mod,
399 			 struct rsnd_dai_stream *io,
400 			 struct rsnd_priv *priv)
401 {
402 	struct rsnd_src *src = rsnd_mod_to_src(mod);
403 
404 	rsnd_src_halt(mod);
405 
406 	rsnd_mod_power_off(mod);
407 
408 	/* reset sync convert_rate */
409 	src->sync.val = 0;
410 
411 	return 0;
412 }
413 
414 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
415 				 struct rsnd_dai_stream *io)
416 {
417 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
418 	bool stop = false;
419 
420 	spin_lock(&priv->lock);
421 
422 	/* ignore all cases if not working */
423 	if (!rsnd_io_is_working(io))
424 		goto rsnd_src_interrupt_out;
425 
426 	if (rsnd_src_error_occurred(mod))
427 		stop = true;
428 
429 	rsnd_src_status_clear(mod);
430 rsnd_src_interrupt_out:
431 
432 	spin_unlock(&priv->lock);
433 
434 	if (stop)
435 		snd_pcm_stop_xrun(io->substream);
436 }
437 
438 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
439 {
440 	struct rsnd_mod *mod = data;
441 
442 	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
443 
444 	return IRQ_HANDLED;
445 }
446 
447 static int rsnd_src_probe_(struct rsnd_mod *mod,
448 			   struct rsnd_dai_stream *io,
449 			   struct rsnd_priv *priv)
450 {
451 	struct rsnd_src *src = rsnd_mod_to_src(mod);
452 	struct device *dev = rsnd_priv_to_dev(priv);
453 	int irq = src->irq;
454 	int ret;
455 
456 	if (irq > 0) {
457 		/*
458 		 * IRQ is not supported on non-DT
459 		 * see
460 		 *	rsnd_src_irq()
461 		 */
462 		ret = devm_request_irq(dev, irq,
463 				       rsnd_src_interrupt,
464 				       IRQF_SHARED,
465 				       dev_name(dev), mod);
466 		if (ret)
467 			return ret;
468 	}
469 
470 	ret = rsnd_dma_attach(io, mod, &src->dma);
471 
472 	return ret;
473 }
474 
475 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
476 			    struct rsnd_dai_stream *io,
477 			    struct snd_soc_pcm_runtime *rtd)
478 {
479 	struct rsnd_src *src = rsnd_mod_to_src(mod);
480 	int ret;
481 
482 	/*
483 	 * enable SRC sync convert if possible
484 	 */
485 
486 	/*
487 	 * It can't use SRC Synchronous convert
488 	 * when Capture if it uses CMD
489 	 */
490 	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
491 		return 0;
492 
493 	/*
494 	 * enable sync convert
495 	 */
496 	ret = rsnd_kctrl_new_s(mod, io, rtd,
497 			       rsnd_io_is_play(io) ?
498 			       "SRC Out Rate Switch" :
499 			       "SRC In Rate Switch",
500 			       rsnd_kctrl_accept_anytime,
501 			       rsnd_src_set_convert_rate,
502 			       &src->sen, 1);
503 	if (ret < 0)
504 		return ret;
505 
506 	ret = rsnd_kctrl_new_s(mod, io, rtd,
507 			       rsnd_io_is_play(io) ?
508 			       "SRC Out Rate" :
509 			       "SRC In Rate",
510 			       rsnd_kctrl_accept_runtime,
511 			       rsnd_src_set_convert_rate,
512 			       &src->sync, 192000);
513 
514 	return ret;
515 }
516 
517 static struct rsnd_mod_ops rsnd_src_ops = {
518 	.name	= SRC_NAME,
519 	.dma_req = rsnd_src_dma_req,
520 	.probe	= rsnd_src_probe_,
521 	.init	= rsnd_src_init,
522 	.quit	= rsnd_src_quit,
523 	.start	= rsnd_src_start,
524 	.stop	= rsnd_src_stop,
525 	.irq	= rsnd_src_irq,
526 	.hw_params = rsnd_src_hw_params,
527 	.pcm_new = rsnd_src_pcm_new,
528 };
529 
530 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
531 {
532 	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
533 		id = 0;
534 
535 	return rsnd_mod_get(rsnd_src_get(priv, id));
536 }
537 
538 int rsnd_src_probe(struct rsnd_priv *priv)
539 {
540 	struct device_node *node;
541 	struct device_node *np;
542 	struct device *dev = rsnd_priv_to_dev(priv);
543 	struct rsnd_src *src;
544 	struct clk *clk;
545 	char name[RSND_SRC_NAME_SIZE];
546 	int i, nr, ret;
547 
548 	/* This driver doesn't support Gen1 at this point */
549 	if (rsnd_is_gen1(priv))
550 		return 0;
551 
552 	node = rsnd_src_of_node(priv);
553 	if (!node)
554 		return 0; /* not used is not error */
555 
556 	nr = of_get_child_count(node);
557 	if (!nr) {
558 		ret = -EINVAL;
559 		goto rsnd_src_probe_done;
560 	}
561 
562 	src	= devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
563 	if (!src) {
564 		ret = -ENOMEM;
565 		goto rsnd_src_probe_done;
566 	}
567 
568 	priv->src_nr	= nr;
569 	priv->src	= src;
570 
571 	i = 0;
572 	for_each_child_of_node(node, np) {
573 		if (!of_device_is_available(np))
574 			goto skip;
575 
576 		src = rsnd_src_get(priv, i);
577 
578 		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
579 			 SRC_NAME, i);
580 
581 		src->irq = irq_of_parse_and_map(np, 0);
582 		if (!src->irq) {
583 			ret = -EINVAL;
584 			goto rsnd_src_probe_done;
585 		}
586 
587 		clk = devm_clk_get(dev, name);
588 		if (IS_ERR(clk)) {
589 			ret = PTR_ERR(clk);
590 			goto rsnd_src_probe_done;
591 		}
592 
593 		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
594 				    &rsnd_src_ops, clk, rsnd_mod_get_status,
595 				    RSND_MOD_SRC, i);
596 		if (ret)
597 			goto rsnd_src_probe_done;
598 
599 skip:
600 		i++;
601 	}
602 
603 	ret = 0;
604 
605 rsnd_src_probe_done:
606 	of_node_put(node);
607 
608 	return ret;
609 }
610 
611 void rsnd_src_remove(struct rsnd_priv *priv)
612 {
613 	struct rsnd_src *src;
614 	int i;
615 
616 	for_each_rsnd_src(src, priv, i) {
617 		rsnd_mod_quit(rsnd_mod_get(src));
618 	}
619 }
620