xref: /openbmc/linux/sound/soc/sh/rcar/src.c (revision 293d5b43)
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 	u32 fin, fout;
193 	u32 ifscr, fsrate, adinr;
194 	u32 cr, route;
195 	u32 bsdsr, bsisr;
196 	uint ratio;
197 
198 	if (!runtime)
199 		return;
200 
201 	fin  = rsnd_src_get_in_rate(priv, io);
202 	fout = rsnd_src_get_out_rate(priv, io);
203 
204 	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
205 	if (fin == fout)
206 		ratio = 0;
207 	else if (fin > fout)
208 		ratio = 100 * fin / fout;
209 	else
210 		ratio = 100 * fout / fin;
211 
212 	if (ratio > 600) {
213 		dev_err(dev, "FSO/FSI ratio error\n");
214 		return;
215 	}
216 
217 	/*
218 	 *	SRC_ADINR
219 	 */
220 	adinr = rsnd_get_adinr_bit(mod, io) |
221 		rsnd_runtime_channel_original(io);
222 
223 	/*
224 	 *	SRC_IFSCR / SRC_IFSVR
225 	 */
226 	ifscr = 0;
227 	fsrate = 0;
228 	if (fin != fout) {
229 		ifscr = 1;
230 		fsrate = 0x0400000 / fout * fin;
231 	}
232 
233 	/*
234 	 *	SRC_SRCCR / SRC_ROUTE_MODE0
235 	 */
236 	cr	= 0x00011110;
237 	route	= 0x0;
238 	if (fin != fout) {
239 		route	= 0x1;
240 
241 		if (rsnd_src_sync_is_enabled(mod)) {
242 			cr |= 0x1;
243 			route |= rsnd_io_is_play(io) ?
244 				(0x1 << 24) : (0x1 << 25);
245 		}
246 	}
247 
248 	/*
249 	 * SRC_BSDSR / SRC_BSISR
250 	 */
251 	switch (rsnd_mod_id(mod)) {
252 	case 5:
253 	case 6:
254 	case 7:
255 	case 8:
256 		bsdsr = 0x02400000; /* 6 - 1/6 */
257 		bsisr = 0x00100060; /* 6 - 1/6 */
258 		break;
259 	default:
260 		bsdsr = 0x01800000; /* 6 - 1/6 */
261 		bsisr = 0x00100060 ;/* 6 - 1/6 */
262 		break;
263 	}
264 
265 	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
266 
267 	rsnd_mod_write(mod, SRC_SRCIR, 1);	/* initialize */
268 	rsnd_mod_write(mod, SRC_ADINR, adinr);
269 	rsnd_mod_write(mod, SRC_IFSCR, ifscr);
270 	rsnd_mod_write(mod, SRC_IFSVR, fsrate);
271 	rsnd_mod_write(mod, SRC_SRCCR, cr);
272 	rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
273 	rsnd_mod_write(mod, SRC_BSISR, bsisr);
274 	rsnd_mod_write(mod, SRC_SRCIR, 0);	/* cancel initialize */
275 
276 	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1);
277 	rsnd_mod_write(mod, SRC_O_BUSIF_MODE, 1);
278 	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
279 
280 	rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
281 }
282 
283 static int rsnd_src_irq(struct rsnd_mod *mod,
284 			struct rsnd_dai_stream *io,
285 			struct rsnd_priv *priv,
286 			int enable)
287 {
288 	struct rsnd_src *src = rsnd_mod_to_src(mod);
289 	u32 sys_int_val, int_val, sys_int_mask;
290 	int irq = src->irq;
291 	int id = rsnd_mod_id(mod);
292 
293 	sys_int_val =
294 	sys_int_mask = OUF_SRC(id);
295 	int_val = 0x3300;
296 
297 	/*
298 	 * IRQ is not supported on non-DT
299 	 * see
300 	 *	rsnd_src_probe_()
301 	 */
302 	if ((irq <= 0) || !enable) {
303 		sys_int_val = 0;
304 		int_val = 0;
305 	}
306 
307 	/*
308 	 * WORKAROUND
309 	 *
310 	 * ignore over flow error when rsnd_src_sync_is_enabled()
311 	 */
312 	if (rsnd_src_sync_is_enabled(mod))
313 		sys_int_val = sys_int_val & 0xffff;
314 
315 	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
316 	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
317 	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
318 
319 	return 0;
320 }
321 
322 static void rsnd_src_status_clear(struct rsnd_mod *mod)
323 {
324 	u32 val = OUF_SRC(rsnd_mod_id(mod));
325 
326 	rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
327 	rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
328 }
329 
330 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
331 {
332 	u32 val0, val1;
333 	bool ret = false;
334 
335 	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
336 
337 	/*
338 	 * WORKAROUND
339 	 *
340 	 * ignore over flow error when rsnd_src_sync_is_enabled()
341 	 */
342 	if (rsnd_src_sync_is_enabled(mod))
343 		val0 = val0 & 0xffff;
344 
345 	if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
346 	    (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
347 		ret = true;
348 
349 	return ret;
350 }
351 
352 static int rsnd_src_start(struct rsnd_mod *mod,
353 			  struct rsnd_dai_stream *io,
354 			  struct rsnd_priv *priv)
355 {
356 	u32 val;
357 
358 	/*
359 	 * WORKAROUND
360 	 *
361 	 * Enable SRC output if you want to use sync convert together with DVC
362 	 */
363 	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
364 		0x01 : 0x11;
365 
366 	rsnd_mod_write(mod, SRC_CTRL, val);
367 
368 	return 0;
369 }
370 
371 static int rsnd_src_stop(struct rsnd_mod *mod,
372 			 struct rsnd_dai_stream *io,
373 			 struct rsnd_priv *priv)
374 {
375 	rsnd_mod_write(mod, SRC_CTRL, 0);
376 
377 	return 0;
378 }
379 
380 static int rsnd_src_init(struct rsnd_mod *mod,
381 			 struct rsnd_dai_stream *io,
382 			 struct rsnd_priv *priv)
383 {
384 	struct rsnd_src *src = rsnd_mod_to_src(mod);
385 
386 	rsnd_mod_power_on(mod);
387 
388 	rsnd_src_activation(mod);
389 
390 	rsnd_src_set_convert_rate(io, mod);
391 
392 	rsnd_src_status_clear(mod);
393 
394 	/* reset sync convert_rate */
395 	src->sync.val = 0;
396 
397 	return 0;
398 }
399 
400 static int rsnd_src_quit(struct rsnd_mod *mod,
401 			 struct rsnd_dai_stream *io,
402 			 struct rsnd_priv *priv)
403 {
404 	struct rsnd_src *src = rsnd_mod_to_src(mod);
405 
406 	rsnd_src_halt(mod);
407 
408 	rsnd_mod_power_off(mod);
409 
410 	src->convert_rate = 0;
411 
412 	/* reset sync convert_rate */
413 	src->sync.val = 0;
414 
415 	return 0;
416 }
417 
418 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
419 				 struct rsnd_dai_stream *io)
420 {
421 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
422 	bool stop = false;
423 
424 	spin_lock(&priv->lock);
425 
426 	/* ignore all cases if not working */
427 	if (!rsnd_io_is_working(io))
428 		goto rsnd_src_interrupt_out;
429 
430 	if (rsnd_src_error_occurred(mod))
431 		stop = true;
432 
433 	rsnd_src_status_clear(mod);
434 rsnd_src_interrupt_out:
435 
436 	spin_unlock(&priv->lock);
437 
438 	if (stop)
439 		snd_pcm_stop_xrun(io->substream);
440 }
441 
442 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
443 {
444 	struct rsnd_mod *mod = data;
445 
446 	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
447 
448 	return IRQ_HANDLED;
449 }
450 
451 static int rsnd_src_probe_(struct rsnd_mod *mod,
452 			   struct rsnd_dai_stream *io,
453 			   struct rsnd_priv *priv)
454 {
455 	struct rsnd_src *src = rsnd_mod_to_src(mod);
456 	struct device *dev = rsnd_priv_to_dev(priv);
457 	int irq = src->irq;
458 	int ret;
459 
460 	if (irq > 0) {
461 		/*
462 		 * IRQ is not supported on non-DT
463 		 * see
464 		 *	rsnd_src_irq()
465 		 */
466 		ret = devm_request_irq(dev, irq,
467 				       rsnd_src_interrupt,
468 				       IRQF_SHARED,
469 				       dev_name(dev), mod);
470 		if (ret)
471 			return ret;
472 	}
473 
474 	ret = rsnd_dma_attach(io, mod, &src->dma, 0);
475 
476 	return ret;
477 }
478 
479 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
480 			    struct rsnd_dai_stream *io,
481 			    struct snd_soc_pcm_runtime *rtd)
482 {
483 	struct rsnd_src *src = rsnd_mod_to_src(mod);
484 	int ret;
485 
486 	/*
487 	 * enable SRC sync convert if possible
488 	 */
489 
490 	/*
491 	 * It can't use SRC Synchronous convert
492 	 * when Capture if it uses CMD
493 	 */
494 	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
495 		return 0;
496 
497 	/*
498 	 * enable sync convert
499 	 */
500 	ret = rsnd_kctrl_new_s(mod, io, rtd,
501 			       rsnd_io_is_play(io) ?
502 			       "SRC Out Rate Switch" :
503 			       "SRC In Rate Switch",
504 			       rsnd_src_set_convert_rate,
505 			       &src->sen, 1);
506 	if (ret < 0)
507 		return ret;
508 
509 	ret = rsnd_kctrl_new_s(mod, io, rtd,
510 			       rsnd_io_is_play(io) ?
511 			       "SRC Out Rate" :
512 			       "SRC In Rate",
513 			       rsnd_src_set_convert_rate,
514 			       &src->sync, 192000);
515 
516 	return ret;
517 }
518 
519 static struct rsnd_mod_ops rsnd_src_ops = {
520 	.name	= SRC_NAME,
521 	.dma_req = rsnd_src_dma_req,
522 	.probe	= rsnd_src_probe_,
523 	.init	= rsnd_src_init,
524 	.quit	= rsnd_src_quit,
525 	.start	= rsnd_src_start,
526 	.stop	= rsnd_src_stop,
527 	.irq	= rsnd_src_irq,
528 	.hw_params = rsnd_src_hw_params,
529 	.pcm_new = rsnd_src_pcm_new,
530 };
531 
532 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
533 {
534 	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
535 		id = 0;
536 
537 	return rsnd_mod_get(rsnd_src_get(priv, id));
538 }
539 
540 int rsnd_src_probe(struct rsnd_priv *priv)
541 {
542 	struct device_node *node;
543 	struct device_node *np;
544 	struct device *dev = rsnd_priv_to_dev(priv);
545 	struct rsnd_src *src;
546 	struct clk *clk;
547 	char name[RSND_SRC_NAME_SIZE];
548 	int i, nr, ret;
549 
550 	/* This driver doesn't support Gen1 at this point */
551 	if (rsnd_is_gen1(priv))
552 		return 0;
553 
554 	node = rsnd_src_of_node(priv);
555 	if (!node)
556 		return 0; /* not used is not error */
557 
558 	nr = of_get_child_count(node);
559 	if (!nr) {
560 		ret = -EINVAL;
561 		goto rsnd_src_probe_done;
562 	}
563 
564 	src	= devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
565 	if (!src) {
566 		ret = -ENOMEM;
567 		goto rsnd_src_probe_done;
568 	}
569 
570 	priv->src_nr	= nr;
571 	priv->src	= src;
572 
573 	i = 0;
574 	for_each_child_of_node(node, np) {
575 		if (!of_device_is_available(np))
576 			goto skip;
577 
578 		src = rsnd_src_get(priv, i);
579 
580 		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
581 			 SRC_NAME, i);
582 
583 		src->irq = irq_of_parse_and_map(np, 0);
584 		if (!src->irq) {
585 			ret = -EINVAL;
586 			goto rsnd_src_probe_done;
587 		}
588 
589 		clk = devm_clk_get(dev, name);
590 		if (IS_ERR(clk)) {
591 			ret = PTR_ERR(clk);
592 			goto rsnd_src_probe_done;
593 		}
594 
595 		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
596 				    &rsnd_src_ops, clk, rsnd_mod_get_status,
597 				    RSND_MOD_SRC, i);
598 		if (ret)
599 			goto rsnd_src_probe_done;
600 
601 skip:
602 		i++;
603 	}
604 
605 	ret = 0;
606 
607 rsnd_src_probe_done:
608 	of_node_put(node);
609 
610 	return ret;
611 }
612 
613 void rsnd_src_remove(struct rsnd_priv *priv)
614 {
615 	struct rsnd_src *src;
616 	int i;
617 
618 	for_each_rsnd_src(src, priv, i) {
619 		rsnd_mod_quit(rsnd_mod_get(src));
620 	}
621 }
622