src.c 12.1 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0
//
// Renesas R-Car SRC support
//
// Copyright (C) 2013 Renesas Solutions Corp.
// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 8 9 10 11 12 13 14 15

/*
 * you can enable below define if you don't need
 * SSI interrupt status debug message when debugging
 * see rsnd_dbg_irq_status()
 *
 * #define RSND_DEBUG_NO_IRQ_STATUS 1
 */

16 17
#include "rsnd.h"

18 19
#define SRC_NAME "src"

20 21 22
/* SCU_SYSTEM_STATUS0/1 */
#define OUF_SRC(id)	((1 << (id + 16)) | (1 << id))

23
struct rsnd_src {
24
	struct rsnd_mod mod;
25
	struct rsnd_mod *dma;
26 27
	struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
	struct rsnd_kctrl_cfg_s sync; /* sync 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_nr(priv) ((priv)->src_nr)
35
#define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
36

37 38
#define rsnd_mod_to_src(_mod)				\
	container_of((_mod), struct rsnd_src, mod)
39

40
#define for_each_rsnd_src(pos, priv, i)				\
41
	for ((i) = 0;						\
42 43
	     ((i) < rsnd_src_nr(priv)) &&			\
	     ((pos) = (struct rsnd_src *)(priv)->src + i);	\
44 45 46
	     i++)


47 48 49 50 51 52 53 54 55
/*
 *		image of SRC (Sampling Rate Converter)
 *
 * 96kHz   <-> +-----+	48kHz	+-----+	 48kHz	+-------+
 * 48kHz   <-> | SRC | <------>	| SSI |	<----->	| codec |
 * 44.1kHz <-> +-----+		+-----+		+-------+
 * ...
 *
 */
56

57
static void rsnd_src_activation(struct rsnd_mod *mod)
58 59 60 61 62
{
	rsnd_mod_write(mod, SRC_SWRSR, 0);
	rsnd_mod_write(mod, SRC_SWRSR, 1);
}

63 64 65 66 67 68
static void rsnd_src_halt(struct rsnd_mod *mod)
{
	rsnd_mod_write(mod, SRC_SRCIR, 1);
	rsnd_mod_write(mod, SRC_SWRSR, 0);
}

69 70
static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
					 struct rsnd_mod *mod)
71 72 73 74 75 76 77 78 79
{
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
	int is_play = rsnd_io_is_play(io);

	return rsnd_dma_request_channel(rsnd_src_of_node(priv),
					mod,
					is_play ? "rx" : "tx");
}

80
static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
81
				 struct rsnd_mod *mod)
82 83
{
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
84
	struct rsnd_src *src = rsnd_mod_to_src(mod);
85 86 87 88 89
	u32 convert_rate;

	if (!runtime)
		return 0;

90
	if (!rsnd_src_sync_is_enabled(mod))
91
		return rsnd_io_converted_rate(io);
92 93 94 95

	convert_rate = src->sync.val;

	if (!convert_rate)
96
		convert_rate = rsnd_io_converted_rate(io);
97 98 99 100 101 102 103

	if (!convert_rate)
		convert_rate = runtime->rate;

	return convert_rate;
}

104 105 106
unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
			       struct rsnd_dai_stream *io,
			       int is_in)
107
{
108
	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
109
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
110
	unsigned int rate = 0;
111 112 113 114 115 116 117 118 119 120 121 122
	int is_play = rsnd_io_is_play(io);

	/*
	 * Playback
	 * runtime_rate -> [SRC] -> convert_rate
	 *
	 * Capture
	 * convert_rate -> [SRC] -> runtime_rate
	 */

	if (is_play == is_in)
		return runtime->rate;
123

124 125 126 127 128 129
	/*
	 * return convert rate if SRC is used,
	 * otherwise, return runtime->rate as usual
	 */
	if (src_mod)
		rate = rsnd_src_convert_rate(io, src_mod);
130 131 132 133 134 135 136

	if (!rate)
		rate = runtime->rate;

	return rate;
}

137 138
static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
				      struct rsnd_mod *mod)
139
{
140 141 142
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
	struct device *dev = rsnd_priv_to_dev(priv);
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
143
	int is_play = rsnd_io_is_play(io);
144
	int use_src = 0;
145
	u32 fin, fout;
146 147 148
	u32 ifscr, fsrate, adinr;
	u32 cr, route;
	u32 bsdsr, bsisr;
149
	u32 i_busif, o_busif, tmp;
150
	uint ratio;
151

152 153
	if (!runtime)
		return;
154

155 156 157
	fin  = rsnd_src_get_in_rate(priv, io);
	fout = rsnd_src_get_out_rate(priv, io);

158
	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
159
	if (fin == fout)
160
		ratio = 0;
161 162
	else if (fin > fout)
		ratio = 100 * fin / fout;
163
	else
164
		ratio = 100 * fout / fin;
165

166 167 168 169
	if (ratio > 600) {
		dev_err(dev, "FSO/FSI ratio error\n");
		return;
	}
170

171 172
	use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);

173
	/*
174
	 * SRC_ADINR
175
	 */
176
	adinr = rsnd_get_adinr_bit(mod, io) |
177
		rsnd_runtime_channel_original(io);
178

179
	/*
180
	 * SRC_IFSCR / SRC_IFSVR
181 182 183
	 */
	ifscr = 0;
	fsrate = 0;
184
	if (use_src) {
185 186
		u64 n;

187
		ifscr = 1;
188 189 190
		n = (u64)0x0400000 * fin;
		do_div(n, fout);
		fsrate = n;
191
	}
192

193
	/*
194
	 * SRC_SRCCR / SRC_ROUTE_MODE0
195 196 197
	 */
	cr	= 0x00011110;
	route	= 0x0;
198
	if (use_src) {
199
		route	= 0x1;
200

201
		if (rsnd_src_sync_is_enabled(mod)) {
202 203 204 205 206
			cr |= 0x1;
			route |= rsnd_io_is_play(io) ?
				(0x1 << 24) : (0x1 << 25);
		}
	}
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	/*
	 * SRC_BSDSR / SRC_BSISR
	 */
	switch (rsnd_mod_id(mod)) {
	case 5:
	case 6:
	case 7:
	case 8:
		bsdsr = 0x02400000; /* 6 - 1/6 */
		bsisr = 0x00100060; /* 6 - 1/6 */
		break;
	default:
		bsdsr = 0x01800000; /* 6 - 1/6 */
		bsisr = 0x00100060 ;/* 6 - 1/6 */
		break;
	}
224

225 226 227 228 229
	/* BUSIF_MODE */
	tmp = rsnd_get_busif_shift(io, mod);
	i_busif = ( is_play ? tmp : 0) | 1;
	o_busif = (!is_play ? tmp : 0) | 1;

230 231
	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);

232 233 234 235 236 237 238 239
	rsnd_mod_write(mod, SRC_SRCIR, 1);	/* initialize */
	rsnd_mod_write(mod, SRC_ADINR, adinr);
	rsnd_mod_write(mod, SRC_IFSCR, ifscr);
	rsnd_mod_write(mod, SRC_IFSVR, fsrate);
	rsnd_mod_write(mod, SRC_SRCCR, cr);
	rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
	rsnd_mod_write(mod, SRC_BSISR, bsisr);
	rsnd_mod_write(mod, SRC_SRCIR, 0);	/* cancel initialize */
240

241 242 243
	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
	rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);

244
	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
245

246
	rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
247 248
}

249 250 251 252
static int rsnd_src_irq(struct rsnd_mod *mod,
			struct rsnd_dai_stream *io,
			struct rsnd_priv *priv,
			int enable)
253 254 255
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	u32 sys_int_val, int_val, sys_int_mask;
256
	int irq = src->irq;
257 258 259 260 261 262 263 264 265
	int id = rsnd_mod_id(mod);

	sys_int_val =
	sys_int_mask = OUF_SRC(id);
	int_val = 0x3300;

	/*
	 * IRQ is not supported on non-DT
	 * see
266
	 *	rsnd_src_probe_()
267 268 269 270 271 272
	 */
	if ((irq <= 0) || !enable) {
		sys_int_val = 0;
		int_val = 0;
	}

273 274 275
	/*
	 * WORKAROUND
	 *
276
	 * ignore over flow error when rsnd_src_sync_is_enabled()
277
	 */
278
	if (rsnd_src_sync_is_enabled(mod))
279 280
		sys_int_val = sys_int_val & 0xffff;

281 282 283
	rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
	rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
	rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
284 285

	return 0;
286 287
}

288
static void rsnd_src_status_clear(struct rsnd_mod *mod)
289 290 291
{
	u32 val = OUF_SRC(rsnd_mod_id(mod));

292 293
	rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
	rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
294 295
}

296
static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
297
{
298 299
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
	struct device *dev = rsnd_priv_to_dev(priv);
300
	u32 val0, val1;
301
	u32 status0, status1;
302 303
	bool ret = false;

304 305 306 307 308
	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));

	/*
	 * WORKAROUND
	 *
309
	 * ignore over flow error when rsnd_src_sync_is_enabled()
310
	 */
311
	if (rsnd_src_sync_is_enabled(mod))
312 313
		val0 = val0 & 0xffff;

314 315 316
	status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
	status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
	if ((status0 & val0) || (status1 & val1)) {
317 318
		rsnd_dbg_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
			rsnd_mod_name(mod), status0, status1);
319

320
		ret = true;
321
	}
322 323 324 325

	return ret;
}

326 327 328
static int rsnd_src_start(struct rsnd_mod *mod,
			  struct rsnd_dai_stream *io,
			  struct rsnd_priv *priv)
329
{
330 331 332 333 334 335 336
	u32 val;

	/*
	 * WORKAROUND
	 *
	 * Enable SRC output if you want to use sync convert together with DVC
	 */
337
	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
338
		0x01 : 0x11;
339 340 341 342 343 344

	rsnd_mod_write(mod, SRC_CTRL, val);

	return 0;
}

345 346 347
static int rsnd_src_stop(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
348
{
349
	rsnd_mod_write(mod, SRC_CTRL, 0);
350

351 352 353 354 355 356 357 358
	return 0;
}

static int rsnd_src_init(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
359

360 361 362
	/* reset sync convert_rate */
	src->sync.val = 0;

363 364
	rsnd_mod_power_on(mod);

365
	rsnd_src_activation(mod);
366 367 368

	rsnd_src_set_convert_rate(io, mod);

369
	rsnd_src_status_clear(mod);
370 371

	return 0;
372 373
}

374 375 376
static int rsnd_src_quit(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
377
{
378 379
	struct rsnd_src *src = rsnd_mod_to_src(mod);

380 381
	rsnd_src_halt(mod);

382 383 384 385 386 387
	rsnd_mod_power_off(mod);

	/* reset sync convert_rate */
	src->sync.val = 0;

	return 0;
388 389
}

390 391
static void __rsnd_src_interrupt(struct rsnd_mod *mod,
				 struct rsnd_dai_stream *io)
392
{
393
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
394
	bool stop = false;
395 396

	spin_lock(&priv->lock);
397

398
	/* ignore all cases if not working */
399
	if (!rsnd_io_is_working(io))
400
		goto rsnd_src_interrupt_out;
401

402 403
	if (rsnd_src_error_occurred(mod))
		stop = true;
404

405
	rsnd_src_status_clear(mod);
406
rsnd_src_interrupt_out:
407

408
	spin_unlock(&priv->lock);
409 410 411

	if (stop)
		snd_pcm_stop_xrun(io->substream);
412 413
}

414
static irqreturn_t rsnd_src_interrupt(int irq, void *data)
415 416 417
{
	struct rsnd_mod *mod = data;

418
	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
419 420 421 422

	return IRQ_HANDLED;
}

423 424 425
static int rsnd_src_probe_(struct rsnd_mod *mod,
			   struct rsnd_dai_stream *io,
			   struct rsnd_priv *priv)
426
{
427
	struct rsnd_src *src = rsnd_mod_to_src(mod);
428
	struct device *dev = rsnd_priv_to_dev(priv);
429
	int irq = src->irq;
430 431
	int ret;

432 433 434 435
	if (irq > 0) {
		/*
		 * IRQ is not supported on non-DT
		 * see
436
		 *	rsnd_src_irq()
437 438
		 */
		ret = devm_request_irq(dev, irq,
439
				       rsnd_src_interrupt,
440 441 442
				       IRQF_SHARED,
				       dev_name(dev), mod);
		if (ret)
443
			return ret;
444 445
	}

446
	ret = rsnd_dma_attach(io, mod, &src->dma);
447

448 449 450
	return ret;
}

451
static int rsnd_src_pcm_new(struct rsnd_mod *mod,
452
			    struct rsnd_dai_stream *io,
453 454 455 456 457 458 459 460 461
			    struct snd_soc_pcm_runtime *rtd)
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	int ret;

	/*
	 * enable SRC sync convert if possible
	 */

462
	/*
463 464
	 * It can't use SRC Synchronous convert
	 * when Capture if it uses CMD
465
	 */
466
	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
467 468
		return 0;

469 470 471
	/*
	 * enable sync convert
	 */
472
	ret = rsnd_kctrl_new_s(mod, io, rtd,
473 474 475
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate Switch" :
			       "SRC In Rate Switch",
476
			       rsnd_kctrl_accept_anytime,
477
			       rsnd_src_set_convert_rate,
478 479 480 481
			       &src->sen, 1);
	if (ret < 0)
		return ret;

482
	ret = rsnd_kctrl_new_s(mod, io, rtd,
483 484 485
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate" :
			       "SRC In Rate",
486
			       rsnd_kctrl_accept_runtime,
487
			       rsnd_src_set_convert_rate,
488 489 490 491 492
			       &src->sync, 192000);

	return ret;
}

493
static struct rsnd_mod_ops rsnd_src_ops = {
494 495 496 497 498 499 500 501 502 503
	.name		= SRC_NAME,
	.dma_req	= rsnd_src_dma_req,
	.probe		= rsnd_src_probe_,
	.init		= rsnd_src_init,
	.quit		= rsnd_src_quit,
	.start		= rsnd_src_start,
	.stop		= rsnd_src_stop,
	.irq		= rsnd_src_irq,
	.pcm_new	= rsnd_src_pcm_new,
	.get_status	= rsnd_mod_get_status,
504 505
};

506
struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
507
{
508
	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
509
		id = 0;
510

511
	return rsnd_mod_get(rsnd_src_get(priv, id));
512 513
}

514
int rsnd_src_probe(struct rsnd_priv *priv)
515
{
516 517
	struct device_node *node;
	struct device_node *np;
518
	struct device *dev = rsnd_priv_to_dev(priv);
519
	struct rsnd_src *src;
520
	struct clk *clk;
521
	char name[RSND_SRC_NAME_SIZE];
522
	int i, nr, ret;
523

524 525 526
	/* This driver doesn't support Gen1 at this point */
	if (rsnd_is_gen1(priv))
		return 0;
527

528 529 530
	node = rsnd_src_of_node(priv);
	if (!node)
		return 0; /* not used is not error */
531

532 533 534 535 536
	nr = of_get_child_count(node);
	if (!nr) {
		ret = -EINVAL;
		goto rsnd_src_probe_done;
	}
537

538
	src	= devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
539 540 541 542
	if (!src) {
		ret = -ENOMEM;
		goto rsnd_src_probe_done;
	}
543

544 545
	priv->src_nr	= nr;
	priv->src	= src;
546

547 548
	i = 0;
	for_each_child_of_node(node, np) {
549 550 551
		if (!of_device_is_available(np))
			goto skip;

552 553
		src = rsnd_src_get(priv, i);

554 555
		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
			 SRC_NAME, i);
556

557 558 559
		src->irq = irq_of_parse_and_map(np, 0);
		if (!src->irq) {
			ret = -EINVAL;
560
			of_node_put(np);
561 562
			goto rsnd_src_probe_done;
		}
563

564 565 566
		clk = devm_clk_get(dev, name);
		if (IS_ERR(clk)) {
			ret = PTR_ERR(clk);
567
			of_node_put(np);
568 569
			goto rsnd_src_probe_done;
		}
570

571
		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
572
				    &rsnd_src_ops, clk, RSND_MOD_SRC, i);
573 574
		if (ret) {
			of_node_put(np);
575
			goto rsnd_src_probe_done;
576
		}
577

578
skip:
579
		i++;
580
	}
581

582 583 584 585 586 587
	ret = 0;

rsnd_src_probe_done:
	of_node_put(node);

	return ret;
588
}
589

590
void rsnd_src_remove(struct rsnd_priv *priv)
591 592 593 594 595
{
	struct rsnd_src *src;
	int i;

	for_each_rsnd_src(src, priv, i) {
596
		rsnd_mod_quit(rsnd_mod_get(src));
597 598
	}
}