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