src.c 14 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
static const u32 bsdsr_table_pattern1[] = {
138 139 140 141 142 143 144 145
	0x01800000, /* 6 - 1/6 */
	0x01000000, /* 6 - 1/4 */
	0x00c00000, /* 6 - 1/3 */
	0x00800000, /* 6 - 1/2 */
	0x00600000, /* 6 - 2/3 */
	0x00400000, /* 6 - 1   */
};

146
static const u32 bsdsr_table_pattern2[] = {
147 148 149 150 151 152 153 154
	0x02400000, /* 6 - 1/6 */
	0x01800000, /* 6 - 1/4 */
	0x01200000, /* 6 - 1/3 */
	0x00c00000, /* 6 - 1/2 */
	0x00900000, /* 6 - 2/3 */
	0x00600000, /* 6 - 1   */
};

155
static const u32 bsisr_table[] = {
156 157 158 159 160 161 162 163
	0x00100060, /* 6 - 1/6 */
	0x00100040, /* 6 - 1/4 */
	0x00100030, /* 6 - 1/3 */
	0x00100020, /* 6 - 1/2 */
	0x00100020, /* 6 - 2/3 */
	0x00100020, /* 6 - 1   */
};

164
static const u32 chan288888[] = {
165 166 167 168 169 170 171 172
	0x00000006, /* 1 to 2 */
	0x000001fe, /* 1 to 8 */
	0x000001fe, /* 1 to 8 */
	0x000001fe, /* 1 to 8 */
	0x000001fe, /* 1 to 8 */
	0x000001fe, /* 1 to 8 */
};

173
static const u32 chan244888[] = {
174 175 176 177 178 179 180 181
	0x00000006, /* 1 to 2 */
	0x0000001e, /* 1 to 4 */
	0x0000001e, /* 1 to 4 */
	0x000001fe, /* 1 to 8 */
	0x000001fe, /* 1 to 8 */
	0x000001fe, /* 1 to 8 */
};

182
static const u32 chan222222[] = {
183 184 185 186 187 188 189 190
	0x00000006, /* 1 to 2 */
	0x00000006, /* 1 to 2 */
	0x00000006, /* 1 to 2 */
	0x00000006, /* 1 to 2 */
	0x00000006, /* 1 to 2 */
	0x00000006, /* 1 to 2 */
};

191 192
static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
				      struct rsnd_mod *mod)
193
{
194 195 196
	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);
197
	int is_play = rsnd_io_is_play(io);
198
	int use_src = 0;
199
	u32 fin, fout;
200 201
	u32 ifscr, fsrate, adinr;
	u32 cr, route;
202
	u32 i_busif, o_busif, tmp;
203 204
	const u32 *bsdsr_table;
	const u32 *chptn;
205
	uint ratio;
206 207
	int chan;
	int idx;
208

209 210
	if (!runtime)
		return;
211

212 213 214
	fin  = rsnd_src_get_in_rate(priv, io);
	fout = rsnd_src_get_out_rate(priv, io);

215 216
	chan = rsnd_runtime_channel_original(io);

217
	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
218
	if (fin == fout)
219
		ratio = 0;
220 221
	else if (fin > fout)
		ratio = 100 * fin / fout;
222
	else
223
		ratio = 100 * fout / fin;
224

225 226 227 228
	if (ratio > 600) {
		dev_err(dev, "FSO/FSI ratio error\n");
		return;
	}
229

230 231
	use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);

232
	/*
233
	 * SRC_ADINR
234
	 */
235
	adinr = rsnd_get_adinr_bit(mod, io) | chan;
236

237
	/*
238
	 * SRC_IFSCR / SRC_IFSVR
239 240 241
	 */
	ifscr = 0;
	fsrate = 0;
242
	if (use_src) {
243 244
		u64 n;

245
		ifscr = 1;
246 247 248
		n = (u64)0x0400000 * fin;
		do_div(n, fout);
		fsrate = n;
249
	}
250

251
	/*
252
	 * SRC_SRCCR / SRC_ROUTE_MODE0
253 254 255
	 */
	cr	= 0x00011110;
	route	= 0x0;
256
	if (use_src) {
257
		route	= 0x1;
258

259
		if (rsnd_src_sync_is_enabled(mod)) {
260 261 262 263 264
			cr |= 0x1;
			route |= rsnd_io_is_play(io) ?
				(0x1 << 24) : (0x1 << 25);
		}
	}
265

266 267
	/*
	 * SRC_BSDSR / SRC_BSISR
268 269 270 271
	 *
	 * see
	 *	Combination of Register Setting Related to
	 *	FSO/FSI Ratio and Channel, Latency
272 273
	 */
	switch (rsnd_mod_id(mod)) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	case 0:
		chptn		= chan288888;
		bsdsr_table	= bsdsr_table_pattern1;
		break;
	case 1:
	case 3:
	case 4:
		chptn		= chan244888;
		bsdsr_table	= bsdsr_table_pattern1;
		break;
	case 2:
	case 9:
		chptn		= chan222222;
		bsdsr_table	= bsdsr_table_pattern1;
		break;
289 290 291 292
	case 5:
	case 6:
	case 7:
	case 8:
293 294
		chptn		= chan222222;
		bsdsr_table	= bsdsr_table_pattern2;
295 296
		break;
	default:
297
		goto convert_rate_err;
298
	}
299

300 301 302
	/*
	 * E3 need to overwrite
	 */
303
	if (rsnd_is_e3(priv))
304 305 306 307 308 309 310 311 312 313 314 315 316 317
		switch (rsnd_mod_id(mod)) {
		case 0:
		case 4:
			chptn	= chan222222;
		}

	for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
		if (chptn[idx] & (1 << chan))
			break;

	if (chan > 8 ||
	    idx >= ARRAY_SIZE(chan222222))
		goto convert_rate_err;

318 319 320 321 322
	/* BUSIF_MODE */
	tmp = rsnd_get_busif_shift(io, mod);
	i_busif = ( is_play ? tmp : 0) | 1;
	o_busif = (!is_play ? tmp : 0) | 1;

323 324
	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);

325 326 327 328 329
	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);
330 331
	rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
	rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
332
	rsnd_mod_write(mod, SRC_SRCIR, 0);	/* cancel initialize */
333

334 335 336
	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
	rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);

337
	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
338

339
	rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
340 341 342 343 344

	return;

convert_rate_err:
	dev_err(dev, "unknown BSDSR/BSDIR settings\n");
345 346
}

347 348 349 350
static int rsnd_src_irq(struct rsnd_mod *mod,
			struct rsnd_dai_stream *io,
			struct rsnd_priv *priv,
			int enable)
351 352 353
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	u32 sys_int_val, int_val, sys_int_mask;
354
	int irq = src->irq;
355 356 357 358 359 360 361 362 363
	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
364
	 *	rsnd_src_probe_()
365 366 367 368 369 370
	 */
	if ((irq <= 0) || !enable) {
		sys_int_val = 0;
		int_val = 0;
	}

371 372 373
	/*
	 * WORKAROUND
	 *
374
	 * ignore over flow error when rsnd_src_sync_is_enabled()
375
	 */
376
	if (rsnd_src_sync_is_enabled(mod))
377 378
		sys_int_val = sys_int_val & 0xffff;

379 380 381
	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);
382 383

	return 0;
384 385
}

386
static void rsnd_src_status_clear(struct rsnd_mod *mod)
387 388 389
{
	u32 val = OUF_SRC(rsnd_mod_id(mod));

390 391
	rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
	rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
392 393
}

394
static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
395
{
396 397
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
	struct device *dev = rsnd_priv_to_dev(priv);
398
	u32 val0, val1;
399
	u32 status0, status1;
400 401
	bool ret = false;

402 403 404 405 406
	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));

	/*
	 * WORKAROUND
	 *
407
	 * ignore over flow error when rsnd_src_sync_is_enabled()
408
	 */
409
	if (rsnd_src_sync_is_enabled(mod))
410 411
		val0 = val0 & 0xffff;

412 413 414
	status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
	status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
	if ((status0 & val0) || (status1 & val1)) {
415 416
		rsnd_dbg_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
			rsnd_mod_name(mod), status0, status1);
417

418
		ret = true;
419
	}
420 421 422 423

	return ret;
}

424 425 426
static int rsnd_src_start(struct rsnd_mod *mod,
			  struct rsnd_dai_stream *io,
			  struct rsnd_priv *priv)
427
{
428 429 430 431 432 433 434
	u32 val;

	/*
	 * WORKAROUND
	 *
	 * Enable SRC output if you want to use sync convert together with DVC
	 */
435
	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
436
		0x01 : 0x11;
437 438 439 440 441 442

	rsnd_mod_write(mod, SRC_CTRL, val);

	return 0;
}

443 444 445
static int rsnd_src_stop(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
446
{
447
	rsnd_mod_write(mod, SRC_CTRL, 0);
448

449 450 451 452 453 454 455 456
	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);
457

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

461 462
	rsnd_mod_power_on(mod);

463
	rsnd_src_activation(mod);
464 465 466

	rsnd_src_set_convert_rate(io, mod);

467
	rsnd_src_status_clear(mod);
468 469

	return 0;
470 471
}

472 473 474
static int rsnd_src_quit(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
475
{
476 477
	struct rsnd_src *src = rsnd_mod_to_src(mod);

478 479
	rsnd_src_halt(mod);

480 481 482 483 484 485
	rsnd_mod_power_off(mod);

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

	return 0;
486 487
}

488 489
static void __rsnd_src_interrupt(struct rsnd_mod *mod,
				 struct rsnd_dai_stream *io)
490
{
491
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
492
	bool stop = false;
493 494

	spin_lock(&priv->lock);
495

496
	/* ignore all cases if not working */
497
	if (!rsnd_io_is_working(io))
498
		goto rsnd_src_interrupt_out;
499

500 501
	if (rsnd_src_error_occurred(mod))
		stop = true;
502

503
	rsnd_src_status_clear(mod);
504
rsnd_src_interrupt_out:
505

506
	spin_unlock(&priv->lock);
507 508 509

	if (stop)
		snd_pcm_stop_xrun(io->substream);
510 511
}

512
static irqreturn_t rsnd_src_interrupt(int irq, void *data)
513 514 515
{
	struct rsnd_mod *mod = data;

516
	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
517 518 519 520

	return IRQ_HANDLED;
}

521 522 523
static int rsnd_src_probe_(struct rsnd_mod *mod,
			   struct rsnd_dai_stream *io,
			   struct rsnd_priv *priv)
524
{
525
	struct rsnd_src *src = rsnd_mod_to_src(mod);
526
	struct device *dev = rsnd_priv_to_dev(priv);
527
	int irq = src->irq;
528 529
	int ret;

530 531 532 533
	if (irq > 0) {
		/*
		 * IRQ is not supported on non-DT
		 * see
534
		 *	rsnd_src_irq()
535 536
		 */
		ret = devm_request_irq(dev, irq,
537
				       rsnd_src_interrupt,
538 539 540
				       IRQF_SHARED,
				       dev_name(dev), mod);
		if (ret)
541
			return ret;
542 543
	}

544
	ret = rsnd_dma_attach(io, mod, &src->dma);
545

546 547 548
	return ret;
}

549
static int rsnd_src_pcm_new(struct rsnd_mod *mod,
550
			    struct rsnd_dai_stream *io,
551 552 553 554 555 556 557 558 559
			    struct snd_soc_pcm_runtime *rtd)
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	int ret;

	/*
	 * enable SRC sync convert if possible
	 */

560
	/*
561 562
	 * It can't use SRC Synchronous convert
	 * when Capture if it uses CMD
563
	 */
564
	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
565 566
		return 0;

567 568 569
	/*
	 * enable sync convert
	 */
570
	ret = rsnd_kctrl_new_s(mod, io, rtd,
571 572 573
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate Switch" :
			       "SRC In Rate Switch",
574
			       rsnd_kctrl_accept_anytime,
575
			       rsnd_src_set_convert_rate,
576 577 578 579
			       &src->sen, 1);
	if (ret < 0)
		return ret;

580
	ret = rsnd_kctrl_new_s(mod, io, rtd,
581 582 583
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate" :
			       "SRC In Rate",
584
			       rsnd_kctrl_accept_runtime,
585
			       rsnd_src_set_convert_rate,
586 587 588 589 590
			       &src->sync, 192000);

	return ret;
}

591
static struct rsnd_mod_ops rsnd_src_ops = {
592 593 594 595 596 597 598 599 600 601
	.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,
602 603
};

604
struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
605
{
606
	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
607
		id = 0;
608

609
	return rsnd_mod_get(rsnd_src_get(priv, id));
610 611
}

612
int rsnd_src_probe(struct rsnd_priv *priv)
613
{
614 615
	struct device_node *node;
	struct device_node *np;
616
	struct device *dev = rsnd_priv_to_dev(priv);
617
	struct rsnd_src *src;
618
	struct clk *clk;
619
	char name[RSND_SRC_NAME_SIZE];
620
	int i, nr, ret;
621

622 623 624
	/* This driver doesn't support Gen1 at this point */
	if (rsnd_is_gen1(priv))
		return 0;
625

626 627 628
	node = rsnd_src_of_node(priv);
	if (!node)
		return 0; /* not used is not error */
629

630 631 632 633 634
	nr = of_get_child_count(node);
	if (!nr) {
		ret = -EINVAL;
		goto rsnd_src_probe_done;
	}
635

636
	src	= devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
637 638 639 640
	if (!src) {
		ret = -ENOMEM;
		goto rsnd_src_probe_done;
	}
641

642 643
	priv->src_nr	= nr;
	priv->src	= src;
644

645 646
	i = 0;
	for_each_child_of_node(node, np) {
647 648 649
		if (!of_device_is_available(np))
			goto skip;

650 651
		src = rsnd_src_get(priv, i);

652 653
		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
			 SRC_NAME, i);
654

655 656 657
		src->irq = irq_of_parse_and_map(np, 0);
		if (!src->irq) {
			ret = -EINVAL;
658
			of_node_put(np);
659 660
			goto rsnd_src_probe_done;
		}
661

662 663 664
		clk = devm_clk_get(dev, name);
		if (IS_ERR(clk)) {
			ret = PTR_ERR(clk);
665
			of_node_put(np);
666 667
			goto rsnd_src_probe_done;
		}
668

669
		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
670
				    &rsnd_src_ops, clk, RSND_MOD_SRC, i);
671 672
		if (ret) {
			of_node_put(np);
673
			goto rsnd_src_probe_done;
674
		}
675

676
skip:
677
		i++;
678
	}
679

680 681 682 683 684 685
	ret = 0;

rsnd_src_probe_done:
	of_node_put(node);

	return ret;
686
}
687

688
void rsnd_src_remove(struct rsnd_priv *priv)
689 690 691 692 693
{
	struct rsnd_src *src;
	int i;

	for_each_rsnd_src(src, priv, i) {
694
		rsnd_mod_quit(rsnd_mod_get(src));
695 696
	}
}