src.c 21.2 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 23
struct rsnd_src {
	struct rsnd_src_platform_info *info; /* rcar_snd.h */
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
	u32 convert_rate; /* sampling rate convert */
29
	int err;
30 31
};

32
#define RSND_SRC_NAME_SIZE 16
33

34
#define rsnd_src_to_dma(src) ((src)->dma)
35
#define rsnd_src_nr(priv) ((priv)->src_nr)
36
#define rsnd_enable_sync_convert(src) ((src)->sen.val)
37 38 39
#define rsnd_src_of_node(priv) \
	of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")

40 41
#define rsnd_mod_to_src(_mod)				\
	container_of((_mod), struct rsnd_src, mod)
42

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


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

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

74 75 76 77 78 79 80 81 82 83 84 85
/*
 *	How to use SRC bypass mode for debugging
 *
 * SRC has bypass mode, and it is useful for debugging.
 * In Gen2 case,
 * SRCm_MODE controls whether SRC is used or not
 * SSI_MODE0 controls whether SSIU which receives SRC data
 * is used or not.
 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
 * but SRC bypass mode needs SSI_MODE0 only.
 *
 * This driver request
86
 * struct rsnd_src_platform_info {
87
 *	u32 convert_rate;
88
 *	int dma_id;
89 90
 * }
 *
91
 * rsnd_src_convert_rate() indicates
92 93 94 95
 * above convert_rate, and it controls
 * whether SRC is used or not.
 *
 * ex) doesn't use SRC
96 97
 * static struct rsnd_dai_platform_info rsnd_dai = {
 *	.playback = { .ssi = &rsnd_ssi[0], },
98 99 100
 * };
 *
 * ex) uses SRC
101 102 103 104 105 106
 * static struct rsnd_src_platform_info rsnd_src[] = {
 *	RSND_SCU(48000, 0),
 *	...
 * };
 * static struct rsnd_dai_platform_info rsnd_dai = {
 *	.playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
107 108 109
 * };
 *
 * ex) uses SRC bypass mode
110 111 112 113 114 115
 * static struct rsnd_src_platform_info rsnd_src[] = {
 *	RSND_SCU(0, 0),
 *	...
 * };
 * static struct rsnd_dai_platform_info rsnd_dai = {
 *	.playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
116 117 118 119
 * };
 *
 */

120 121 122
/*
 *		Gen1/Gen2 common functions
 */
123 124 125 126 127 128
static void rsnd_src_soft_reset(struct rsnd_mod *mod)
{
	rsnd_mod_write(mod, SRC_SWRSR, 0);
	rsnd_mod_write(mod, SRC_SWRSR, 1);
}

129 130 131 132 133 134 135 136

#define rsnd_src_initialize_lock(mod)	__rsnd_src_initialize_lock(mod, 1)
#define rsnd_src_initialize_unlock(mod)	__rsnd_src_initialize_lock(mod, 0)
static void __rsnd_src_initialize_lock(struct rsnd_mod *mod, u32 enable)
{
	rsnd_mod_write(mod, SRC_SRCIR, enable);
}

137 138
static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
					 struct rsnd_mod *mod)
139 140 141 142 143 144 145 146 147
{
	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");
}

148 149
static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
				 struct rsnd_src *src)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
{
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
	u32 convert_rate;

	if (!runtime)
		return 0;

	if (!rsnd_enable_sync_convert(src))
		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;
}

171
unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
172
				   struct rsnd_dai_stream *io,
173 174
				   struct snd_pcm_runtime *runtime)
{
175
	struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
176
	struct rsnd_src *src;
177
	unsigned int rate = 0;
178

179 180 181 182 183 184 185
	if (src_mod) {
		src = rsnd_mod_to_src(src_mod);

		/*
		 * return convert rate if SRC is used,
		 * otherwise, return runtime->rate as usual
		 */
186
		rate = rsnd_src_convert_rate(io, src);
187
	}
188 189 190 191 192 193 194

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

	return rate;
}

195 196
static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
				     struct rsnd_dai_stream *io)
197 198
{
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
199
	struct rsnd_src *src = rsnd_mod_to_src(mod);
200
	u32 convert_rate = rsnd_src_convert_rate(io, src);
201 202 203 204 205 206
	u32 fsrate = 0;

	if (convert_rate)
		fsrate = 0x0400000 / convert_rate * runtime->rate;

	/* Set channel number and output bit length */
207
	rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr_bit(mod, io));
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

	/* Enable the initial value of IFS */
	if (fsrate) {
		rsnd_mod_write(mod, SRC_IFSCR, 1);

		/* Set initial value of IFS */
		rsnd_mod_write(mod, SRC_IFSVR, fsrate);
	}

	/* use DMA transfer */
	rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);

	return 0;
}

223
static int rsnd_src_hw_params(struct rsnd_mod *mod,
224
			      struct rsnd_dai_stream *io,
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
			      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;

	/* default value (mainly for non-DT) */
	src->convert_rate = src->info->convert_rate;

	/*
	 * 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;
}

static int rsnd_src_init(struct rsnd_mod *mod,
			 struct rsnd_priv *priv)
261
{
262
	struct rsnd_src *src = rsnd_mod_to_src(mod);
263

264
	rsnd_mod_power_on(mod);
265

266 267
	rsnd_src_soft_reset(mod);

268 269
	rsnd_src_initialize_lock(mod);

270 271
	src->err = 0;

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

275 276 277
	return 0;
}

278
static int rsnd_src_quit(struct rsnd_mod *mod,
279
			 struct rsnd_dai_stream *io,
280
			 struct rsnd_priv *priv)
281
{
282
	struct rsnd_src *src = rsnd_mod_to_src(mod);
283
	struct device *dev = rsnd_priv_to_dev(priv);
284

285
	rsnd_mod_power_off(mod);
286

287
	if (src->err)
288 289
		dev_warn(dev, "%s[%d] under/over flow err = %d\n",
			 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
290

291 292
	src->convert_rate = 0;

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

296 297 298
	return 0;
}

299
static int rsnd_src_start(struct rsnd_mod *mod)
300
{
301
	rsnd_src_initialize_unlock(mod);
302 303 304 305

	return 0;
}

306
static int rsnd_src_stop(struct rsnd_mod *mod)
307
{
308
	/* nothing to do */
309 310 311 312 313 314
	return 0;
}

/*
 *		Gen1 functions
 */
315 316
static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
				   struct rsnd_mod *mod)
317
{
318
	struct src_route_config {
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		u32 mask;
		int shift;
	} routes[] = {
		{ 0xF,  0, }, /* 0 */
		{ 0xF,  4, }, /* 1 */
		{ 0xF,  8, }, /* 2 */
		{ 0x7, 12, }, /* 3 */
		{ 0x7, 16, }, /* 4 */
		{ 0x7, 20, }, /* 5 */
		{ 0x7, 24, }, /* 6 */
		{ 0x3, 28, }, /* 7 */
		{ 0x3, 30, }, /* 8 */
	};
	u32 mask;
	u32 val;
	int id;

	id = rsnd_mod_id(mod);
337
	if (id < 0 || id >= ARRAY_SIZE(routes))
338 339 340 341 342
		return -EIO;

	/*
	 * SRC_ROUTE_SELECT
	 */
343
	val = rsnd_io_is_play(io) ? 0x1 : 0x2;
344 345 346 347 348
	val = val		<< routes[id].shift;
	mask = routes[id].mask	<< routes[id].shift;

	rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);

349 350 351
	return 0;
}

352 353
static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
					    struct rsnd_mod *mod)
354 355
{
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
356
	struct rsnd_src *src = rsnd_mod_to_src(mod);
357
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
358
	u32 convert_rate = rsnd_src_convert_rate(io, src);
359 360 361 362 363 364
	u32 mask;
	u32 val;
	int shift;
	int id = rsnd_mod_id(mod);
	int ret;

365 366 367 368 369
	/*
	 * SRC_TIMING_SELECT
	 */
	shift	= (id % 4) * 8;
	mask	= 0x1F << shift;
370 371 372 373 374 375 376

	/*
	 * ADG is used as source clock if SRC was used,
	 * then, SSI WS is used as destination clock.
	 * SSI WS is used as source clock if SRC is not used
	 * (when playback, source/destination become reverse when capture)
	 */
377 378 379
	ret = 0;
	if (convert_rate) {
		/* use ADG */
380
		val = 0;
381 382 383 384 385
		ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
						    runtime->rate,
						    convert_rate);
	} else if (8 == id) {
		/* use SSI WS, but SRU8 is special */
386
		val = id << shift;
387 388
	} else {
		/* use SSI WS */
389
		val = (id + 1) << shift;
390 391 392 393
	}

	if (ret < 0)
		return ret;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

	switch (id / 4) {
	case 0:
		rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
		break;
	case 1:
		rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
		break;
	case 2:
		rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
		break;
	}

	return 0;
}

410 411
static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
					  struct rsnd_dai_stream *io)
412
{
413
	struct rsnd_src *src = rsnd_mod_to_src(mod);
414
	int ret;
415

416
	ret = rsnd_src_set_convert_rate(mod, io);
417 418
	if (ret < 0)
		return ret;
419

420 421
	/* Select SRC mode (fixed value) */
	rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
422

423 424 425
	/* Set the restriction value of the FS ratio (98%) */
	rsnd_mod_write(mod, SRC_MNFSR,
		       rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
426

427
	/* Gen1/Gen2 are not compatible */
428
	if (rsnd_src_convert_rate(io, src))
429 430
		rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);

431
	/* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
432

433 434 435
	return 0;
}

436
static int rsnd_src_init_gen1(struct rsnd_mod *mod,
437
			      struct rsnd_dai_stream *io,
438
			      struct rsnd_priv *priv)
439
{
440 441
	int ret;

442
	ret = rsnd_src_init(mod, priv);
443 444 445
	if (ret < 0)
		return ret;

446
	ret = rsnd_src_set_route_gen1(io, mod);
447 448 449
	if (ret < 0)
		return ret;

450
	ret = rsnd_src_set_convert_rate_gen1(mod, io);
451 452
	if (ret < 0)
		return ret;
453

454
	ret = rsnd_src_set_convert_timing_gen1(io, mod);
455 456 457
	if (ret < 0)
		return ret;

458 459 460
	return 0;
}

461
static int rsnd_src_start_gen1(struct rsnd_mod *mod,
462
			       struct rsnd_dai_stream *io,
463
			       struct rsnd_priv *priv)
464
{
465
	int id = rsnd_mod_id(mod);
466

467
	rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
468

469
	return rsnd_src_start(mod);
470 471
}

472
static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
473
			      struct rsnd_dai_stream *io,
474
			      struct rsnd_priv *priv)
475
{
476
	int id = rsnd_mod_id(mod);
477

478
	rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
479

480
	return rsnd_src_stop(mod);
481 482
}

483
static struct rsnd_mod_ops rsnd_src_gen1_ops = {
484
	.name	= SRC_NAME,
485
	.dma_req = rsnd_src_dma_req,
486 487 488 489
	.init	= rsnd_src_init_gen1,
	.quit	= rsnd_src_quit,
	.start	= rsnd_src_start_gen1,
	.stop	= rsnd_src_stop_gen1,
490
	.hw_params = rsnd_src_hw_params,
491
};
492

493 494 495
/*
 *		Gen2 functions
 */
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
#define rsnd_src_irq_enable_gen2(mod)  rsnd_src_irq_ctrol_gen2(mod, 1)
#define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
{
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	u32 sys_int_val, int_val, sys_int_mask;
	int irq = src->info->irq;
	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
	 *	rsnd_src_probe_gen2()
	 */
	if ((irq <= 0) || !enable) {
		sys_int_val = 0;
		int_val = 0;
	}

519 520 521 522 523 524 525 526
	/*
	 * WORKAROUND
	 *
	 * ignore over flow error when rsnd_enable_sync_convert()
	 */
	if (rsnd_enable_sync_convert(src))
		sys_int_val = sys_int_val & 0xffff;

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	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);
}

static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
{
	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);
}

static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
{
542 543
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	u32 val0, val1;
544 545
	bool ret = false;

546 547 548 549 550 551 552 553 554 555 556 557
	val0 = val1 = OUF_SRC(rsnd_mod_id(mod));

	/*
	 * WORKAROUND
	 *
	 * ignore over flow error when rsnd_enable_sync_convert()
	 */
	if (rsnd_enable_sync_convert(src))
		val0 = val0 & 0xffff;

	if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
	    (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
558 559 560 561 562 563 564 565 566 567 568 569
		struct rsnd_src *src = rsnd_mod_to_src(mod);

		src->err++;
		ret = true;
	}

	/* clear error static */
	rsnd_src_error_clear_gen2(mod);

	return ret;
}

570 571 572
static int rsnd_src_start_gen2(struct rsnd_mod *mod,
			       struct rsnd_dai_stream *io,
			       struct rsnd_priv *priv)
573
{
574 575 576
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	u32 val;

577 578 579 580
	val = rsnd_get_dalign(mod, io);

	rsnd_mod_write(mod, SRC_BUSIF_DALIGN, val);

581 582 583 584 585 586 587
	/*
	 * WORKAROUND
	 *
	 * Enable SRC output if you want to use sync convert together with DVC
	 */
	val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
		0x01 : 0x11;
588 589 590 591 592 593 594 595 596 597 598 599

	rsnd_mod_write(mod, SRC_CTRL, val);

	rsnd_src_error_clear_gen2(mod);

	rsnd_src_start(mod);

	rsnd_src_irq_enable_gen2(mod);

	return 0;
}

600 601 602
static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
			      struct rsnd_dai_stream *io,
			      struct rsnd_priv *priv)
603 604 605
{
	rsnd_src_irq_disable_gen2(mod);

606 607 608 609 610
	/*
	 * stop SRC output only
	 * see rsnd_src_quit_gen2
	 */
	rsnd_mod_write(mod, SRC_CTRL, 0x01);
611 612 613 614 615 616

	rsnd_src_error_record_gen2(mod);

	return rsnd_src_stop(mod);
}

617 618 619 620 621 622 623
static int rsnd_src_quit_gen2(struct rsnd_mod *mod,
			      struct rsnd_dai_stream *io,
			      struct rsnd_priv *priv)
{
	/* stop both out/in */
	rsnd_mod_write(mod, SRC_CTRL, 0);

624
	return rsnd_src_quit(mod, io, priv);
625 626
}

627 628
static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
				      struct rsnd_dai_stream *io)
629
{
630
	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
631 632
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	struct device *dev = rsnd_priv_to_dev(priv);
633 634

	spin_lock(&priv->lock);
635

636
	/* ignore all cases if not working */
637
	if (!rsnd_io_is_working(io))
638
		goto rsnd_src_interrupt_gen2_out;
639 640 641 642 643

	if (rsnd_src_error_record_gen2(mod)) {

		dev_dbg(dev, "%s[%d] restart\n",
			rsnd_mod_name(mod), rsnd_mod_id(mod));
644

645 646
		rsnd_src_stop_gen2(mod, io, priv);
		rsnd_src_start_gen2(mod, io, priv);
647 648 649 650 651 652 653
	}

	if (src->err > 1024) {
		rsnd_src_irq_disable_gen2(mod);

		dev_warn(dev, "no more %s[%d] restart\n",
			 rsnd_mod_name(mod), rsnd_mod_id(mod));
654
	}
655

656 657
rsnd_src_interrupt_gen2_out:
	spin_unlock(&priv->lock);
658 659 660 661 662 663 664
}

static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
{
	struct rsnd_mod *mod = data;

	rsnd_mod_interrupt(mod, __rsnd_src_interrupt_gen2);
665 666 667 668

	return IRQ_HANDLED;
}

669 670
static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
					  struct rsnd_dai_stream *io)
671
{
672 673 674 675
	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);
	struct rsnd_src *src = rsnd_mod_to_src(mod);
676
	u32 convert_rate = rsnd_src_convert_rate(io, src);
677
	u32 cr, route;
678
	uint ratio;
679 680
	int ret;

681
	/* 6 - 1/6 are very enough ratio for SRC_BSDSR */
682
	if (!convert_rate)
683
		ratio = 0;
684 685
	else if (convert_rate > runtime->rate)
		ratio = 100 * convert_rate / runtime->rate;
686
	else
687
		ratio = 100 * runtime->rate / convert_rate;
688 689 690 691 692 693

	if (ratio > 600) {
		dev_err(dev, "FSO/FSI ratio error\n");
		return -EINVAL;
	}

694
	ret = rsnd_src_set_convert_rate(mod, io);
695 696 697
	if (ret < 0)
		return ret;

698 699
	cr	= 0x00011110;
	route	= 0x0;
700
	if (convert_rate) {
701 702 703 704 705 706 707
		route	= 0x1;

		if (rsnd_enable_sync_convert(src)) {
			cr |= 0x1;
			route |= rsnd_io_is_play(io) ?
				(0x1 << 24) : (0x1 << 25);
		}
708 709
	}

710 711 712
	rsnd_mod_write(mod, SRC_SRCCR, cr);
	rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);

713 714 715 716 717 718 719 720 721 722 723 724
	switch (rsnd_mod_id(mod)) {
	case 5:
	case 6:
	case 7:
	case 8:
		rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
		break;
	default:
		rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
		break;
	}

725 726 727 728 729
	rsnd_mod_write(mod, SRC_BSISR, 0x00100060);

	return 0;
}

730 731
static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
					    struct rsnd_mod *mod)
732 733
{
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
734
	struct rsnd_src *src = rsnd_mod_to_src(mod);
735
	u32 convert_rate = rsnd_src_convert_rate(io, src);
736 737 738
	int ret;

	if (convert_rate)
739
		ret = rsnd_adg_set_convert_clk_gen2(mod, io,
740 741 742
						    runtime->rate,
						    convert_rate);
	else
743
		ret = rsnd_adg_set_convert_timing_gen2(mod, io);
744 745 746 747

	return ret;
}

748
static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
749
			       struct rsnd_dai_stream *io,
750
			       struct rsnd_priv *priv)
751
{
752
	struct rsnd_src *src = rsnd_mod_to_src(mod);
753
	struct device *dev = rsnd_priv_to_dev(priv);
754
	int irq = src->info->irq;
755 756
	int ret;

757 758 759 760 761 762 763 764 765 766 767
	if (irq > 0) {
		/*
		 * IRQ is not supported on non-DT
		 * see
		 *	rsnd_src_irq_enable_gen2()
		 */
		ret = devm_request_irq(dev, irq,
				       rsnd_src_interrupt_gen2,
				       IRQF_SHARED,
				       dev_name(dev), mod);
		if (ret)
768
			return ret;
769 770
	}

771
	src->dma = rsnd_dma_attach(io, mod, src->info->dma_id);
772 773
	if (IS_ERR(src->dma))
		return PTR_ERR(src->dma);
774

775 776 777
	return ret;
}

778
static int rsnd_src_init_gen2(struct rsnd_mod *mod,
779
			      struct rsnd_dai_stream *io,
780
			      struct rsnd_priv *priv)
781 782 783
{
	int ret;

784
	ret = rsnd_src_init(mod, priv);
785 786 787
	if (ret < 0)
		return ret;

788
	ret = rsnd_src_set_convert_rate_gen2(mod, io);
789 790 791
	if (ret < 0)
		return ret;

792
	ret = rsnd_src_set_convert_timing_gen2(io, mod);
793 794 795 796 797 798
	if (ret < 0)
		return ret;

	return 0;
}

799 800
static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
				      struct rsnd_mod *mod)
801 802 803
{
	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
	struct rsnd_src *src = rsnd_mod_to_src(mod);
804
	u32 convert_rate = rsnd_src_convert_rate(io, src);
805 806 807 808 809 810 811 812 813 814 815 816 817 818
	u32 fsrate;

	if (!runtime)
		return;

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

	fsrate = 0x0400000 / convert_rate * runtime->rate;

	/* update IFS */
	rsnd_mod_write(mod, SRC_IFSVR, fsrate);
}

819
static int rsnd_src_pcm_new_gen2(struct rsnd_mod *mod,
820
			    struct rsnd_dai_stream *io,
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
			    struct snd_soc_pcm_runtime *rtd)
{
	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
	struct rsnd_src *src = rsnd_mod_to_src(mod);
	int ret;

	/*
	 * enable SRC sync convert if possible
	 */

	/*
	 * SRC sync convert needs clock master
	 */
	if (!rsnd_rdai_is_clk_master(rdai))
		return 0;

	/*
	 * enable sync convert
	 */
840
	ret = rsnd_kctrl_new_s(mod, io, rtd,
841 842 843 844 845 846 847 848
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate Switch" :
			       "SRC In Rate Switch",
			       rsnd_src_reconvert_update,
			       &src->sen, 1);
	if (ret < 0)
		return ret;

849
	ret = rsnd_kctrl_new_s(mod, io, rtd,
850 851 852 853 854 855 856 857 858
			       rsnd_io_is_play(io) ?
			       "SRC Out Rate" :
			       "SRC In Rate",
			       rsnd_src_reconvert_update,
			       &src->sync, 192000);

	return ret;
}

859
static struct rsnd_mod_ops rsnd_src_gen2_ops = {
860
	.name	= SRC_NAME,
861
	.dma_req = rsnd_src_dma_req,
862 863
	.probe	= rsnd_src_probe_gen2,
	.init	= rsnd_src_init_gen2,
864
	.quit	= rsnd_src_quit_gen2,
865 866
	.start	= rsnd_src_start_gen2,
	.stop	= rsnd_src_stop_gen2,
867
	.hw_params = rsnd_src_hw_params,
868
	.pcm_new = rsnd_src_pcm_new_gen2,
869 870
};

871
struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
872
{
873
	if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
874
		id = 0;
875

876
	return rsnd_mod_get((struct rsnd_src *)(priv->src) + id);
877 878
}

879 880 881 882 883
static void rsnd_of_parse_src(struct platform_device *pdev,
			      const struct rsnd_of_data *of_data,
			      struct rsnd_priv *priv)
{
	struct device_node *src_node;
884
	struct device_node *np;
885 886 887
	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
	struct rsnd_src_platform_info *src_info;
	struct device *dev = &pdev->dev;
888
	int nr, i;
889 890 891 892

	if (!of_data)
		return;

893
	src_node = rsnd_src_of_node(priv);
894 895 896 897 898
	if (!src_node)
		return;

	nr = of_get_child_count(src_node);
	if (!nr)
899
		goto rsnd_of_parse_src_end;
900 901 902 903 904 905

	src_info = devm_kzalloc(dev,
				sizeof(struct rsnd_src_platform_info) * nr,
				GFP_KERNEL);
	if (!src_info) {
		dev_err(dev, "src info allocation error\n");
906
		goto rsnd_of_parse_src_end;
907 908 909 910
	}

	info->src_info		= src_info;
	info->src_info_nr	= nr;
911

912 913 914 915 916 917 918
	i = 0;
	for_each_child_of_node(src_node, np) {
		src_info[i].irq = irq_of_parse_and_map(np, 0);

		i++;
	}

919 920
rsnd_of_parse_src_end:
	of_node_put(src_node);
921 922
}

923
int rsnd_src_probe(struct platform_device *pdev,
924
		   const struct rsnd_of_data *of_data,
925 926
		   struct rsnd_priv *priv)
{
927
	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
928
	struct device *dev = rsnd_priv_to_dev(priv);
929
	struct rsnd_src *src;
930
	struct rsnd_mod_ops *ops;
931
	struct clk *clk;
932
	char name[RSND_SRC_NAME_SIZE];
933
	int i, nr, ret;
934

935
	ops = NULL;
936
	if (rsnd_is_gen1(priv)) {
937
		ops = &rsnd_src_gen1_ops;
938 939
		dev_warn(dev, "Gen1 support will be removed soon\n");
	}
940 941 942 943 944 945 946
	if (rsnd_is_gen2(priv))
		ops = &rsnd_src_gen2_ops;
	if (!ops) {
		dev_err(dev, "unknown Generation\n");
		return -EIO;
	}

947 948
	rsnd_of_parse_src(pdev, of_data, priv);

949
	/*
950
	 * init SRC
951
	 */
952
	nr	= info->src_info_nr;
953 954 955
	if (!nr)
		return 0;

956
	src	= devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
957
	if (!src)
958 959
		return -ENOMEM;

960 961
	priv->src_nr	= nr;
	priv->src	= src;
962

963
	for_each_rsnd_src(src, priv, i) {
964 965
		snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
			 SRC_NAME, i);
966 967 968 969 970

		clk = devm_clk_get(dev, name);
		if (IS_ERR(clk))
			return PTR_ERR(clk);

971
		src->info = &info->src_info[i];
972

973
		ret = rsnd_mod_init(priv, rsnd_mod_get(src), ops, clk, RSND_MOD_SRC, i);
974 975
		if (ret)
			return ret;
976
	}
977 978 979

	return 0;
}
980 981 982 983 984 985 986 987

void rsnd_src_remove(struct platform_device *pdev,
		     struct rsnd_priv *priv)
{
	struct rsnd_src *src;
	int i;

	for_each_rsnd_src(src, priv, i) {
988
		rsnd_mod_quit(rsnd_mod_get(src));
989 990
	}
}