src.c 12.7 KB
Newer Older
1
/*
2
 * Renesas R-Car SRC support
3 4 5 6 7 8 9 10 11 12
 *
 * Copyright (C) 2013 Renesas Solutions Corp.
 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include "rsnd.h"

13 14
#define SRC_NAME "src"

15 16 17 18 19 20 21
/* SRCx_STATUS */
#define OUF_SRCO	((1 << 12) | (1 << 13))
#define OUF_SRCI	((1 <<  9) | (1 <<  8))

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

22
struct rsnd_src {
23
	struct rsnd_mod mod;
24
	struct rsnd_mod *dma;
25 26
	struct rsnd_kctrl_cfg_s sen;  /* sync convert enable */
	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 39
#define rsnd_mod_to_src(_mod)				\
	container_of((_mod), struct rsnd_src, mod)
40

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


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

58
/*
59
 * src.c is caring...
60 61 62 63 64 65 66 67
 *
 * Gen1
 *
 * [mem] -> [SRU] -> [SSI]
 *        |--------|
 *
 * Gen2
 *
68
 * [mem] -> [SRC] -> [SSIU] -> [SSI]
69 70 71
 *        |-----------------|
 */

72
static void rsnd_src_activation(struct rsnd_mod *mod)
73 74 75 76 77
{
	rsnd_mod_write(mod, SRC_SWRSR, 0);
	rsnd_mod_write(mod, SRC_SWRSR, 1);
}

78 79 80 81 82 83
static void rsnd_src_halt(struct rsnd_mod *mod)
{
	rsnd_mod_write(mod, SRC_SRCIR, 1);
	rsnd_mod_write(mod, SRC_SWRSR, 0);
}

84 85
static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
					 struct rsnd_mod *mod)
86 87 88 89 90 91 92 93 94
{
	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");
}

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 101 102 103 104
	u32 convert_rate;

	if (!runtime)
		return 0;

105
	if (!rsnd_src_sync_is_enabled(mod))
106 107 108 109 110 111 112 113 114 115 116 117 118
		return src->convert_rate;

	convert_rate = src->sync.val;

	if (!convert_rate)
		convert_rate = src->convert_rate;

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

	return convert_rate;
}

119 120 121
unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
			       struct rsnd_dai_stream *io,
			       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 127 128 129 130 131 132 133 134 135 136 137 138
	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;
139

140 141 142 143 144 145
	/*
	 * return convert rate if SRC is used,
	 * otherwise, return runtime->rate as usual
	 */
	if (src_mod)
		rate = rsnd_src_convert_rate(io, src_mod);
146 147 148 149 150 151 152

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

	return rate;
}

153
static int rsnd_src_hw_params(struct rsnd_mod *mod,
154
			      struct rsnd_dai_stream *io,
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
			      struct snd_pcm_substream *substream,
			      struct snd_pcm_hw_params *fe_params)
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	struct snd_soc_pcm_runtime *fe = substream->private_data;

	/*
	 * SRC assumes that it is used under DPCM if user want to use
	 * sampling rate convert. Then, SRC should be FE.
	 * And then, this function will be called *after* BE settings.
	 * this means, each BE already has fixuped hw_params.
	 * see
	 *	dpcm_fe_dai_hw_params()
	 *	dpcm_be_dai_hw_params()
	 */
	if (fe->dai_link->dynamic) {
		int stream = substream->stream;
		struct snd_soc_dpcm *dpcm;
		struct snd_pcm_hw_params *be_params;

		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
			be_params = &dpcm->hw_params;

			if (params_rate(fe_params) != params_rate(be_params))
				src->convert_rate = params_rate(be_params);
		}
	}

	return 0;
}

186 187
static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
				      struct rsnd_mod *mod)
188
{
189 190 191
	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);
192
	u32 fin, fout;
193 194 195 196
	u32 ifscr, fsrate, adinr;
	u32 cr, route;
	u32 bsdsr, bsisr;
	uint ratio;
197

198 199
	if (!runtime)
		return;
200

201 202 203
	fin  = rsnd_src_get_in_rate(priv, io);
	fout = rsnd_src_get_out_rate(priv, io);

204
	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
205
	if (fin == fout)
206
		ratio = 0;
207 208
	else if (fin > fout)
		ratio = 100 * fin / fout;
209
	else
210
		ratio = 100 * fout / fin;
211

212 213 214 215
	if (ratio > 600) {
		dev_err(dev, "FSO/FSI ratio error\n");
		return;
	}
216

217 218 219
	/*
	 *	SRC_ADINR
	 */
220
	adinr = rsnd_get_adinr_bit(mod, io) |
221
		rsnd_runtime_channel_original(io);
222

223 224 225 226 227
	/*
	 *	SRC_IFSCR / SRC_IFSVR
	 */
	ifscr = 0;
	fsrate = 0;
228
	if (fin != fout) {
229
		ifscr = 1;
230
		fsrate = 0x0400000 / fout * fin;
231
	}
232

233 234 235 236 237
	/*
	 *	SRC_SRCCR / SRC_ROUTE_MODE0
	 */
	cr	= 0x00011110;
	route	= 0x0;
238
	if (fin != fout) {
239
		route	= 0x1;
240

241
		if (rsnd_src_sync_is_enabled(mod)) {
242 243 244 245 246
			cr |= 0x1;
			route |= rsnd_io_is_play(io) ?
				(0x1 << 24) : (0x1 << 25);
		}
	}
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	/*
	 * 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;
	}
264

265 266
	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);

267 268 269 270 271 272 273 274
	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 */
275

276 277
	rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1);
	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 284 285 286
static int rsnd_src_irq(struct rsnd_mod *mod,
			struct rsnd_dai_stream *io,
			struct rsnd_priv *priv,
			int enable)
287 288 289
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	u32 sys_int_val, int_val, sys_int_mask;
290
	int irq = src->irq;
291 292 293 294 295 296 297 298 299
	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
300
	 *	rsnd_src_probe_()
301 302 303 304 305 306
	 */
	if ((irq <= 0) || !enable) {
		sys_int_val = 0;
		int_val = 0;
	}

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

315 316 317
	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);
318 319

	return 0;
320 321
}

322
static void rsnd_src_status_clear(struct rsnd_mod *mod)
323 324 325 326 327 328 329
{
	u32 val = OUF_SRC(rsnd_mod_id(mod));

	rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
	rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
}

330
static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
331
{
332
	u32 val0, val1;
333 334
	bool ret = false;

335 336 337 338 339
	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));

	/*
	 * WORKAROUND
	 *
340
	 * ignore over flow error when rsnd_src_sync_is_enabled()
341
	 */
342
	if (rsnd_src_sync_is_enabled(mod))
343 344 345
		val0 = val0 & 0xffff;

	if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
346
	    (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
347 348 349 350 351
		ret = true;

	return ret;
}

352 353 354
static int rsnd_src_start(struct rsnd_mod *mod,
			  struct rsnd_dai_stream *io,
			  struct rsnd_priv *priv)
355
{
356 357 358 359 360 361 362
	u32 val;

	/*
	 * WORKAROUND
	 *
	 * Enable SRC output if you want to use sync convert together with DVC
	 */
363
	val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
364
		0x01 : 0x11;
365 366 367 368 369 370

	rsnd_mod_write(mod, SRC_CTRL, val);

	return 0;
}

371 372 373
static int rsnd_src_stop(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
374
{
375
	rsnd_mod_write(mod, SRC_CTRL, 0);
376

377 378 379 380 381 382 383 384
	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);
385

386 387
	rsnd_mod_power_on(mod);

388
	rsnd_src_activation(mod);
389 390 391

	rsnd_src_set_convert_rate(io, mod);

392
	rsnd_src_status_clear(mod);
393 394 395 396 397

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

	return 0;
398 399
}

400 401 402
static int rsnd_src_quit(struct rsnd_mod *mod,
			 struct rsnd_dai_stream *io,
			 struct rsnd_priv *priv)
403
{
404 405
	struct rsnd_src *src = rsnd_mod_to_src(mod);

406 407
	rsnd_src_halt(mod);

408 409 410 411 412 413 414 415
	rsnd_mod_power_off(mod);

	src->convert_rate = 0;

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

	return 0;
416 417
}

418 419
static void __rsnd_src_interrupt(struct rsnd_mod *mod,
				 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 431
	if (rsnd_src_error_occurred(mod))
		stop = true;
432

433
	rsnd_src_status_clear(mod);
434
rsnd_src_interrupt_out:
435

436
	spin_unlock(&priv->lock);
437 438 439

	if (stop)
		snd_pcm_stop_xrun(io->substream);
440 441
}

442
static irqreturn_t rsnd_src_interrupt(int irq, void *data)
443 444 445
{
	struct rsnd_mod *mod = data;

446
	rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
447 448 449 450

	return IRQ_HANDLED;
}

451 452 453
static int rsnd_src_probe_(struct rsnd_mod *mod,
			   struct rsnd_dai_stream *io,
			   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 459
	int ret;

460 461 462 463
	if (irq > 0) {
		/*
		 * IRQ is not supported on non-DT
		 * see
464
		 *	rsnd_src_irq()
465 466
		 */
		ret = devm_request_irq(dev, irq,
467
				       rsnd_src_interrupt,
468 469 470
				       IRQF_SHARED,
				       dev_name(dev), mod);
		if (ret)
471
			return ret;
472 473
	}

474
	ret = rsnd_dma_attach(io, mod, &src->dma, 0);
475

476 477 478
	return ret;
}

479
static int rsnd_src_pcm_new(struct rsnd_mod *mod,
480
			    struct rsnd_dai_stream *io,
481 482 483 484 485 486 487 488 489
			    struct snd_soc_pcm_runtime *rtd)
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	int ret;

	/*
	 * enable SRC sync convert if possible
	 */

490
	/*
491 492
	 * It can't use SRC Synchronous convert
	 * when Capture if it uses CMD
493
	 */
494
	if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
495 496
		return 0;

497 498 499
	/*
	 * enable sync convert
	 */
500
	ret = rsnd_kctrl_new_s(mod, io, rtd,
501 502 503
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate Switch" :
			       "SRC In Rate Switch",
504
			       rsnd_src_set_convert_rate,
505 506 507 508
			       &src->sen, 1);
	if (ret < 0)
		return ret;

509
	ret = rsnd_kctrl_new_s(mod, io, rtd,
510 511 512
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate" :
			       "SRC In Rate",
513
			       rsnd_src_set_convert_rate,
514 515 516 517 518
			       &src->sync, 192000);

	return ret;
}

519
static struct rsnd_mod_ops rsnd_src_ops = {
520
	.name	= SRC_NAME,
521
	.dma_req = rsnd_src_dma_req,
522 523 524 525 526
	.probe	= rsnd_src_probe_,
	.init	= rsnd_src_init,
	.quit	= rsnd_src_quit,
	.start	= rsnd_src_start,
	.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 543
	struct device_node *node;
	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 551 552
	/* This driver doesn't support Gen1 at this point */
	if (rsnd_is_gen1(priv))
		return 0;
553

554 555 556
	node = rsnd_src_of_node(priv);
	if (!node)
		return 0; /* not used is not error */
557

558 559 560 561 562
	nr = of_get_child_count(node);
	if (!nr) {
		ret = -EINVAL;
		goto rsnd_src_probe_done;
	}
563

564
	src	= devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
565 566 567 568
	if (!src) {
		ret = -ENOMEM;
		goto rsnd_src_probe_done;
	}
569

570 571
	priv->src_nr	= nr;
	priv->src	= src;
572

573 574
	i = 0;
	for_each_child_of_node(node, np) {
575 576 577
		if (!of_device_is_available(np))
			goto skip;

578 579
		src = rsnd_src_get(priv, i);

580 581
		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
			 SRC_NAME, i);
582

583 584 585 586 587
		src->irq = irq_of_parse_and_map(np, 0);
		if (!src->irq) {
			ret = -EINVAL;
			goto rsnd_src_probe_done;
		}
588

589 590 591 592 593
		clk = devm_clk_get(dev, name);
		if (IS_ERR(clk)) {
			ret = PTR_ERR(clk);
			goto rsnd_src_probe_done;
		}
594

595
		ret = rsnd_mod_init(priv, rsnd_mod_get(src),
596 597
				    &rsnd_src_ops, clk, rsnd_mod_get_status,
				    RSND_MOD_SRC, i);
598
		if (ret)
599 600
			goto rsnd_src_probe_done;

601
skip:
602
		i++;
603
	}
604

605 606 607 608 609 610
	ret = 0;

rsnd_src_probe_done:
	of_node_put(node);

	return ret;
611
}
612

613
void rsnd_src_remove(struct rsnd_priv *priv)
614 615 616 617 618
{
	struct rsnd_src *src;
	int i;

	for_each_rsnd_src(src, priv, i) {
619
		rsnd_mod_quit(rsnd_mod_get(src));
620 621
	}
}