i2s.c 36.1 KB
Newer Older
1
/* sound/soc/samsung/i2s.c
2 3 4 5
 *
 * ALSA SoC Audio Layer - Samsung I2S Controller driver
 *
 * Copyright (c) 2010 Samsung Electronics Co. Ltd.
6
 *	Jaswinder Singh <jassisinghbrar@gmail.com>
7 8 9 10 11 12
 *
 * 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.
 */

13
#include <dt-bindings/sound/samsung-i2s.h>
14 15 16
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/clk.h>
17
#include <linux/clk-provider.h>
18
#include <linux/io.h>
19
#include <linux/module.h>
20
#include <linux/of.h>
21
#include <linux/of_device.h>
22
#include <linux/of_gpio.h>
23
#include <linux/pm_runtime.h>
24 25

#include <sound/soc.h>
26
#include <sound/pcm_params.h>
27

28
#include <linux/platform_data/asoc-s3c.h>
29 30

#include "dma.h"
31
#include "idma.h"
32
#include "i2s.h"
33
#include "i2s-regs.h"
34 35 36

#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)

37 38 39 40 41 42 43 44 45 46 47 48 49 50
struct samsung_i2s_variant_regs {
	unsigned int	bfs_off;
	unsigned int	rfs_off;
	unsigned int	sdf_off;
	unsigned int	txr_off;
	unsigned int	rclksrc_off;
	unsigned int	mss_off;
	unsigned int	cdclkcon_off;
	unsigned int	lrp_off;
	unsigned int	bfs_mask;
	unsigned int	rfs_mask;
	unsigned int	ftx0cnt_off;
};

51
struct samsung_i2s_dai_data {
52
	u32 quirks;
53
	unsigned int pcm_rates;
54
	const struct samsung_i2s_variant_regs *i2s_variant_regs;
55 56
};

57 58 59
struct i2s_dai {
	/* Platform device for this DAI */
	struct platform_device *pdev;
60
	/* Memory mapped SFR region */
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	void __iomem	*addr;
	/* Rate of RCLK source clock */
	unsigned long rclk_srcrate;
	/* Frame Clock */
	unsigned frmclk;
	/*
	 * Specifically requested RCLK,BCLK by MACHINE Driver.
	 * 0 indicates CPU driver is free to choose any value.
	 */
	unsigned rfs, bfs;
	/* I2S Controller's core clock */
	struct clk *clk;
	/* Clock for generating I2S signals */
	struct clk *op_clk;
	/* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
	struct i2s_dai *pri_dai;
	/* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
	struct i2s_dai *sec_dai;
#define DAI_OPENED	(1 << 0) /* Dai is opened */
#define DAI_MANAGER	(1 << 1) /* Dai is the manager */
	unsigned mode;
	/* Driver for this DAI */
	struct snd_soc_dai_driver i2s_dai_drv;
	/* DMA parameters */
85 86 87
	struct snd_dmaengine_dai_dma_data dma_playback;
	struct snd_dmaengine_dai_dma_data dma_capture;
	struct snd_dmaengine_dai_dma_data idma_playback;
88
	dma_filter_fn filter;
89 90 91 92
	u32	quirks;
	u32	suspend_i2smod;
	u32	suspend_i2scon;
	u32	suspend_i2spsr;
93
	const struct samsung_i2s_variant_regs *variant_regs;
94 95 96 97

	/* Spinlock protecting access to the device's registers */
	spinlock_t spinlock;
	spinlock_t *lock;
98 99 100 101

	/* Below fields are only valid if this is the primary FIFO */
	struct clk *clk_table[3];
	struct clk_onecell_data clk_data;
102 103 104 105 106 107 108 109 110 111 112 113 114 115
};

/* Lock for cross i/f checks */
static DEFINE_SPINLOCK(lock);

/* If this is the 'overlay' stereo DAI */
static inline bool is_secondary(struct i2s_dai *i2s)
{
	return i2s->pri_dai ? true : false;
}

/* If operating in SoC-Slave mode */
static inline bool is_slave(struct i2s_dai *i2s)
{
116 117
	u32 mod = readl(i2s->addr + I2SMOD);
	return (mod & (1 << i2s->variant_regs->mss_off)) ? true : false;
118 119 120 121 122 123 124 125 126 127
}

/* If this interface of the controller is transmitting data */
static inline bool tx_active(struct i2s_dai *i2s)
{
	u32 active;

	if (!i2s)
		return false;

128
	active = readl(i2s->addr + I2SCON);
129 130 131 132 133 134 135 136 137

	if (is_secondary(i2s))
		active &= CON_TXSDMA_ACTIVE;
	else
		active &= CON_TXDMA_ACTIVE;

	return active ? true : false;
}

138 139 140 141 142 143
/* Return pointer to the other DAI */
static inline struct i2s_dai *get_other_dai(struct i2s_dai *i2s)
{
	return i2s->pri_dai ? : i2s->sec_dai;
}

144 145 146
/* If the other interface of the controller is transmitting data */
static inline bool other_tx_active(struct i2s_dai *i2s)
{
147
	struct i2s_dai *other = get_other_dai(i2s);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	return tx_active(other);
}

/* If any interface of the controller is transmitting data */
static inline bool any_tx_active(struct i2s_dai *i2s)
{
	return tx_active(i2s) || other_tx_active(i2s);
}

/* If this interface of the controller is receiving data */
static inline bool rx_active(struct i2s_dai *i2s)
{
	u32 active;

	if (!i2s)
		return false;

166
	active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
167 168 169 170 171 172 173

	return active ? true : false;
}

/* If the other interface of the controller is receiving data */
static inline bool other_rx_active(struct i2s_dai *i2s)
{
174
	struct i2s_dai *other = get_other_dai(i2s);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	return rx_active(other);
}

/* If any interface of the controller is receiving data */
static inline bool any_rx_active(struct i2s_dai *i2s)
{
	return rx_active(i2s) || other_rx_active(i2s);
}

/* If the other DAI is transmitting or receiving data */
static inline bool other_active(struct i2s_dai *i2s)
{
	return other_rx_active(i2s) || other_tx_active(i2s);
}

/* If this DAI is transmitting or receiving data */
static inline bool this_active(struct i2s_dai *i2s)
{
	return tx_active(i2s) || rx_active(i2s);
}

/* If the controller is active anyway */
static inline bool any_active(struct i2s_dai *i2s)
{
	return this_active(i2s) || other_active(i2s);
}

static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
{
	return snd_soc_dai_get_drvdata(dai);
}

static inline bool is_opened(struct i2s_dai *i2s)
{
	if (i2s && (i2s->mode & DAI_OPENED))
		return true;
	else
		return false;
}

static inline bool is_manager(struct i2s_dai *i2s)
{
	if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
		return true;
	else
		return false;
}

/* Read RCLK of I2S (in multiples of LRCLK) */
static inline unsigned get_rfs(struct i2s_dai *i2s)
{
227
	u32 rfs;
228 229
	rfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->rfs_off;
	rfs &= i2s->variant_regs->rfs_mask;
230 231

	switch (rfs) {
232 233 234 235
	case 7: return 192;
	case 6: return 96;
	case 5: return 128;
	case 4: return 64;
236 237 238 239 240 241 242 243 244 245 246
	case 3:	return 768;
	case 2: return 384;
	case 1:	return 512;
	default: return 256;
	}
}

/* Write RCLK of I2S (in multiples of LRCLK) */
static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
{
	u32 mod = readl(i2s->addr + I2SMOD);
247
	int rfs_shift = i2s->variant_regs->rfs_off;
248

249
	mod &= ~(i2s->variant_regs->rfs_mask << rfs_shift);
250 251

	switch (rfs) {
252 253 254 255 256 257 258 259 260 261 262 263
	case 192:
		mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift);
		break;
	case 96:
		mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift);
		break;
	case 128:
		mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift);
		break;
	case 64:
		mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift);
		break;
264
	case 768:
265
		mod |= (MOD_RCLK_768FS << rfs_shift);
266 267
		break;
	case 512:
268
		mod |= (MOD_RCLK_512FS << rfs_shift);
269 270
		break;
	case 384:
271
		mod |= (MOD_RCLK_384FS << rfs_shift);
272 273
		break;
	default:
274
		mod |= (MOD_RCLK_256FS << rfs_shift);
275 276 277 278 279 280 281 282 283
		break;
	}

	writel(mod, i2s->addr + I2SMOD);
}

/* Read Bit-Clock of I2S (in multiples of LRCLK) */
static inline unsigned get_bfs(struct i2s_dai *i2s)
{
284
	u32 bfs;
285 286
	bfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->bfs_off;
	bfs &= i2s->variant_regs->bfs_mask;
287 288

	switch (bfs) {
289 290 291 292 293
	case 8: return 256;
	case 7: return 192;
	case 6: return 128;
	case 5: return 96;
	case 4: return 64;
294 295 296 297 298 299 300 301 302 303 304
	case 3: return 24;
	case 2: return 16;
	case 1:	return 48;
	default: return 32;
	}
}

/* Write Bit-Clock of I2S (in multiples of LRCLK) */
static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
{
	u32 mod = readl(i2s->addr + I2SMOD);
305
	int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
306
	int bfs_shift = i2s->variant_regs->bfs_off;
307 308 309 310 311 312

	/* Non-TDM I2S controllers do not support BCLK > 48 * FS */
	if (!tdm && bfs > 48) {
		dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
		return;
	}
313

314 315
	mod &= ~(i2s->variant_regs->bfs_mask << bfs_shift);

316 317
	switch (bfs) {
	case 48:
318
		mod |= (MOD_BCLK_48FS << bfs_shift);
319 320
		break;
	case 32:
321
		mod |= (MOD_BCLK_32FS << bfs_shift);
322 323
		break;
	case 24:
324
		mod |= (MOD_BCLK_24FS << bfs_shift);
325 326
		break;
	case 16:
327
		mod |= (MOD_BCLK_16FS << bfs_shift);
328
		break;
329 330 331 332 333 334 335 336 337 338 339 340 341 342
	case 64:
		mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
		break;
	case 96:
		mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
		break;
	case 128:
		mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
		break;
	case 192:
		mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
		break;
	case 256:
		mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		break;
	default:
		dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
		return;
	}

	writel(mod, i2s->addr + I2SMOD);
}

/* Sample-Size */
static inline int get_blc(struct i2s_dai *i2s)
{
	int blc = readl(i2s->addr + I2SMOD);

	blc = (blc >> 13) & 0x3;

	switch (blc) {
	case 2: return 24;
	case 1:	return 8;
	default: return 16;
	}
}

/* TX Channel Control */
static void i2s_txctrl(struct i2s_dai *i2s, int on)
{
	void __iomem *addr = i2s->addr;
370
	int txr_off = i2s->variant_regs->txr_off;
371
	u32 con = readl(addr + I2SCON);
372
	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
373 374 375 376 377 378 379 380 381 382 383 384 385 386

	if (on) {
		con |= CON_ACTIVE;
		con &= ~CON_TXCH_PAUSE;

		if (is_secondary(i2s)) {
			con |= CON_TXSDMA_ACTIVE;
			con &= ~CON_TXSDMA_PAUSE;
		} else {
			con |= CON_TXDMA_ACTIVE;
			con &= ~CON_TXDMA_PAUSE;
		}

		if (any_rx_active(i2s))
387
			mod |= 2 << txr_off;
388
		else
389
			mod |= 0 << txr_off;
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	} else {
		if (is_secondary(i2s)) {
			con |=  CON_TXSDMA_PAUSE;
			con &= ~CON_TXSDMA_ACTIVE;
		} else {
			con |=  CON_TXDMA_PAUSE;
			con &= ~CON_TXDMA_ACTIVE;
		}

		if (other_tx_active(i2s)) {
			writel(con, addr + I2SCON);
			return;
		}

		con |=  CON_TXCH_PAUSE;

		if (any_rx_active(i2s))
407
			mod |= 1 << txr_off;
408 409 410 411 412 413 414 415 416 417 418 419
		else
			con &= ~CON_ACTIVE;
	}

	writel(mod, addr + I2SMOD);
	writel(con, addr + I2SCON);
}

/* RX Channel Control */
static void i2s_rxctrl(struct i2s_dai *i2s, int on)
{
	void __iomem *addr = i2s->addr;
420
	int txr_off = i2s->variant_regs->txr_off;
421
	u32 con = readl(addr + I2SCON);
422
	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
423 424 425 426 427 428

	if (on) {
		con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
		con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);

		if (any_tx_active(i2s))
429
			mod |= 2 << txr_off;
430
		else
431
			mod |= 1 << txr_off;
432 433 434 435 436
	} else {
		con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
		con &= ~CON_RXDMA_ACTIVE;

		if (any_tx_active(i2s))
437
			mod |= 0 << txr_off;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
		else
			con &= ~CON_ACTIVE;
	}

	writel(mod, addr + I2SMOD);
	writel(con, addr + I2SCON);
}

/* Flush FIFO of an interface */
static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
{
	void __iomem *fic;
	u32 val;

	if (!i2s)
		return;

	if (is_secondary(i2s))
		fic = i2s->addr + I2SFICS;
	else
		fic = i2s->addr + I2SFIC;

	/* Flush the FIFO */
	writel(readl(fic) | flush, fic);

	/* Be patient */
	val = msecs_to_loops(1) / 1000; /* 1 usec */
	while (--val)
		cpu_relax();

	writel(readl(fic) & ~flush, fic);
}

static int i2s_set_sysclk(struct snd_soc_dai *dai,
	  int clk_id, unsigned int rfs, int dir)
{
	struct i2s_dai *i2s = to_info(dai);
475
	struct i2s_dai *other = get_other_dai(i2s);
476 477 478
	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
	unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
	unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
479
	u32 mod, mask, val = 0;
480
	unsigned long flags;
481 482 483
	int ret = 0;

	pm_runtime_get_sync(dai->dev);
484

485
	spin_lock_irqsave(i2s->lock, flags);
486
	mod = readl(i2s->addr + I2SMOD);
487
	spin_unlock_irqrestore(i2s->lock, flags);
488 489

	switch (clk_id) {
490
	case SAMSUNG_I2S_OPCLK:
491 492
		mask = MOD_OPCLK_MASK;
		val = dir;
493
		break;
494
	case SAMSUNG_I2S_CDCLK:
495
		mask = 1 << i2s_regs->cdclkcon_off;
496 497 498 499
		/* Shouldn't matter in GATING(CLOCK_IN) mode */
		if (dir == SND_SOC_CLOCK_IN)
			rfs = 0;

500
		if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
501 502
				(any_active(i2s) &&
				(((dir == SND_SOC_CLOCK_IN)
503
					&& !(mod & cdcon_mask)) ||
504
				((dir == SND_SOC_CLOCK_OUT)
505
					&& (mod & cdcon_mask))))) {
506 507
			dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
508 509
			ret = -EAGAIN;
			goto err;
510 511 512
		}

		if (dir == SND_SOC_CLOCK_IN)
513
			val = 1 << i2s_regs->cdclkcon_off;
514 515 516 517 518 519

		i2s->rfs = rfs;
		break;

	case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
	case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
520 521
		mask = 1 << i2s_regs->rclksrc_off;

522 523 524 525 526 527 528
		if ((i2s->quirks & QUIRK_NO_MUXPSR)
				|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
			clk_id = 0;
		else
			clk_id = 1;

		if (!any_active(i2s)) {
529
			if (i2s->op_clk && !IS_ERR(i2s->op_clk)) {
530 531
				if ((clk_id && !(mod & rsrc_mask)) ||
					(!clk_id && (mod & rsrc_mask))) {
532
					clk_disable_unprepare(i2s->op_clk);
533 534
					clk_put(i2s->op_clk);
				} else {
535 536
					i2s->rclk_srcrate =
						clk_get_rate(i2s->op_clk);
537
					goto done;
538 539 540
				}
			}

541 542 543 544 545 546
			if (clk_id)
				i2s->op_clk = clk_get(&i2s->pdev->dev,
						"i2s_opclk1");
			else
				i2s->op_clk = clk_get(&i2s->pdev->dev,
						"i2s_opclk0");
547

548 549
			if (WARN_ON(IS_ERR(i2s->op_clk))) {
				ret = PTR_ERR(i2s->op_clk);
550
				i2s->op_clk = NULL;
551 552
				goto err;
			}
553

554
			ret = clk_prepare_enable(i2s->op_clk);
555 556 557
			if (ret) {
				clk_put(i2s->op_clk);
				i2s->op_clk = NULL;
558
				goto err;
559
			}
560 561 562 563 564 565 566
			i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);

			/* Over-ride the other's */
			if (other) {
				other->op_clk = i2s->op_clk;
				other->rclk_srcrate = i2s->rclk_srcrate;
			}
567 568
		} else if ((!clk_id && (mod & rsrc_mask))
				|| (clk_id && !(mod & rsrc_mask))) {
569 570
			dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
571 572
			ret = -EAGAIN;
			goto err;
573 574 575 576
		} else {
			/* Call can't be on the active DAI */
			i2s->op_clk = other->op_clk;
			i2s->rclk_srcrate = other->rclk_srcrate;
577
			goto done;
578 579
		}

580 581
		if (clk_id == 1)
			val = 1 << i2s_regs->rclksrc_off;
582
		break;
583 584
	default:
		dev_err(&i2s->pdev->dev, "We don't serve that!\n");
585 586
		ret = -EINVAL;
		goto err;
587 588
	}

589
	spin_lock_irqsave(i2s->lock, flags);
590 591
	mod = readl(i2s->addr + I2SMOD);
	mod = (mod & ~mask) | val;
592
	writel(mod, i2s->addr + I2SMOD);
593
	spin_unlock_irqrestore(i2s->lock, flags);
594 595
done:
	pm_runtime_put(dai->dev);
596 597

	return 0;
598 599 600
err:
	pm_runtime_put(dai->dev);
	return ret;
601 602 603 604 605 606
}

static int i2s_set_fmt(struct snd_soc_dai *dai,
	unsigned int fmt)
{
	struct i2s_dai *i2s = to_info(dai);
607
	int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
608
	u32 mod, tmp = 0;
609
	unsigned long flags;
610

611 612 613
	lrp_shift = i2s->variant_regs->lrp_off;
	sdf_shift = i2s->variant_regs->sdf_off;
	mod_slave = 1 << i2s->variant_regs->mss_off;
614

615 616 617
	sdf_mask = MOD_SDF_MASK << sdf_shift;
	lrp_rlow = MOD_LR_RLOW << lrp_shift;

618 619 620
	/* Format is priority */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_RIGHT_J:
621 622
		tmp |= lrp_rlow;
		tmp |= (MOD_SDF_MSB << sdf_shift);
623 624
		break;
	case SND_SOC_DAIFMT_LEFT_J:
625 626
		tmp |= lrp_rlow;
		tmp |= (MOD_SDF_LSB << sdf_shift);
627 628
		break;
	case SND_SOC_DAIFMT_I2S:
629
		tmp |= (MOD_SDF_IIS << sdf_shift);
630 631 632 633 634 635 636 637 638 639 640 641 642 643
		break;
	default:
		dev_err(&i2s->pdev->dev, "Format not supported\n");
		return -EINVAL;
	}

	/*
	 * INV flag is relative to the FORMAT flag - if set it simply
	 * flips the polarity specified by the Standard
	 */
	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF:
		break;
	case SND_SOC_DAIFMT_NB_IF:
644 645
		if (tmp & lrp_rlow)
			tmp &= ~lrp_rlow;
646
		else
647
			tmp |= lrp_rlow;
648 649 650 651 652 653 654 655
		break;
	default:
		dev_err(&i2s->pdev->dev, "Polarity not supported\n");
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
656
		tmp |= mod_slave;
657 658 659 660 661 662 663 664 665 666 667 668
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		/* Set default source clock in Master mode */
		if (i2s->rclk_srcrate == 0)
			i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
							0, SND_SOC_CLOCK_IN);
		break;
	default:
		dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
		return -EINVAL;
	}

669
	pm_runtime_get_sync(dai->dev);
670
	spin_lock_irqsave(i2s->lock, flags);
671
	mod = readl(i2s->addr + I2SMOD);
672 673 674 675
	/*
	 * Don't change the I2S mode if any controller is active on this
	 * channel.
	 */
676
	if (any_active(i2s) &&
677
		((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
678
		spin_unlock_irqrestore(i2s->lock, flags);
679
		pm_runtime_put(dai->dev);
680 681 682 683 684
		dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
		return -EAGAIN;
	}

685
	mod &= ~(sdf_mask | lrp_rlow | mod_slave);
686 687
	mod |= tmp;
	writel(mod, i2s->addr + I2SMOD);
688
	spin_unlock_irqrestore(i2s->lock, flags);
689
	pm_runtime_put(dai->dev);
690 691 692 693 694 695 696 697

	return 0;
}

static int i2s_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);
698
	u32 mod, mask = 0, val = 0;
699
	unsigned long flags;
700

701 702
	WARN_ON(!pm_runtime_active(dai->dev));

703
	if (!is_secondary(i2s))
704
		mask |= (MOD_DC2_EN | MOD_DC1_EN);
705 706 707

	switch (params_channels(params)) {
	case 6:
708
		val |= MOD_DC2_EN;
709
	case 4:
710
		val |= MOD_DC1_EN;
711 712
		break;
	case 2:
713
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
714
			i2s->dma_playback.addr_width = 4;
715
		else
716
			i2s->dma_capture.addr_width = 4;
717 718 719
		break;
	case 1:
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
720
			i2s->dma_playback.addr_width = 2;
721
		else
722
			i2s->dma_capture.addr_width = 2;
723

724 725 726 727 728 729 730 731
		break;
	default:
		dev_err(&i2s->pdev->dev, "%d channels not supported\n",
				params_channels(params));
		return -EINVAL;
	}

	if (is_secondary(i2s))
732
		mask |= MOD_BLCS_MASK;
733
	else
734
		mask |= MOD_BLCP_MASK;
735 736

	if (is_manager(i2s))
737
		mask |= MOD_BLC_MASK;
738

739 740
	switch (params_width(params)) {
	case 8:
741
		if (is_secondary(i2s))
742
			val |= MOD_BLCS_8BIT;
743
		else
744
			val |= MOD_BLCP_8BIT;
745
		if (is_manager(i2s))
746
			val |= MOD_BLC_8BIT;
747
		break;
748
	case 16:
749
		if (is_secondary(i2s))
750
			val |= MOD_BLCS_16BIT;
751
		else
752
			val |= MOD_BLCP_16BIT;
753
		if (is_manager(i2s))
754
			val |= MOD_BLC_16BIT;
755
		break;
756
	case 24:
757
		if (is_secondary(i2s))
758
			val |= MOD_BLCS_24BIT;
759
		else
760
			val |= MOD_BLCP_24BIT;
761
		if (is_manager(i2s))
762
			val |= MOD_BLC_24BIT;
763 764 765 766 767 768
		break;
	default:
		dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
				params_format(params));
		return -EINVAL;
	}
769

770
	spin_lock_irqsave(i2s->lock, flags);
771 772
	mod = readl(i2s->addr + I2SMOD);
	mod = (mod & ~mask) | val;
773
	writel(mod, i2s->addr + I2SMOD);
774
	spin_unlock_irqrestore(i2s->lock, flags);
775

776
	snd_soc_dai_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
777

778 779 780 781 782 783 784 785 786 787
	i2s->frmclk = params_rate(params);

	return 0;
}

/* We set constraints on the substream acc to the version of I2S */
static int i2s_startup(struct snd_pcm_substream *substream,
	  struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);
788
	struct i2s_dai *other = get_other_dai(i2s);
789 790
	unsigned long flags;

791 792
	pm_runtime_get_sync(dai->dev);

793 794 795 796 797 798 799 800 801
	spin_lock_irqsave(&lock, flags);

	i2s->mode |= DAI_OPENED;

	if (is_manager(other))
		i2s->mode &= ~DAI_MANAGER;
	else
		i2s->mode |= DAI_MANAGER;

802 803 804
	if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
		writel(CON_RSTCLR, i2s->addr + I2SCON);

805 806 807 808 809 810 811 812 813
	spin_unlock_irqrestore(&lock, flags);

	return 0;
}

static void i2s_shutdown(struct snd_pcm_substream *substream,
	struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);
814
	struct i2s_dai *other = get_other_dai(i2s);
815 816 817 818 819 820 821
	unsigned long flags;

	spin_lock_irqsave(&lock, flags);

	i2s->mode &= ~DAI_OPENED;
	i2s->mode &= ~DAI_MANAGER;

822
	if (is_opened(other))
823
		other->mode |= DAI_MANAGER;
824

825 826 827 828 829
	/* Reset any constraint on RFS and BFS */
	i2s->rfs = 0;
	i2s->bfs = 0;

	spin_unlock_irqrestore(&lock, flags);
830 831

	pm_runtime_put(dai->dev);
832 833 834 835
}

static int config_setup(struct i2s_dai *i2s)
{
836
	struct i2s_dai *other = get_other_dai(i2s);
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	unsigned rfs, bfs, blc;
	u32 psr;

	blc = get_blc(i2s);

	bfs = i2s->bfs;

	if (!bfs && other)
		bfs = other->bfs;

	/* Select least possible multiple(2) if no constraint set */
	if (!bfs)
		bfs = blc * 2;

	rfs = i2s->rfs;

	if (!rfs && other)
		rfs = other->rfs;

	if ((rfs == 256 || rfs == 512) && (blc == 24)) {
		dev_err(&i2s->pdev->dev,
			"%d-RFS not supported for 24-blc\n", rfs);
		return -EINVAL;
	}

	if (!rfs) {
		if (bfs == 16 || bfs == 32)
			rfs = 256;
		else
			rfs = 384;
	}

	/* If already setup and running */
	if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
		dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
		return -EAGAIN;
	}

	set_bfs(i2s, bfs);
	set_rfs(i2s, rfs);

879 880 881 882
	/* Don't bother with PSR in Slave mode */
	if (is_slave(i2s))
		return 0;

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
	if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
		psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
		writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
		dev_dbg(&i2s->pdev->dev,
			"RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
				i2s->rclk_srcrate, psr, rfs, bfs);
	}

	return 0;
}

static int i2s_trigger(struct snd_pcm_substream *substream,
	int cmd, struct snd_soc_dai *dai)
{
	int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct i2s_dai *i2s = to_info(rtd->cpu_dai);
	unsigned long flags;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
906
		pm_runtime_get_sync(dai->dev);
907
		spin_lock_irqsave(i2s->lock, flags);
908 909

		if (config_setup(i2s)) {
910
			spin_unlock_irqrestore(i2s->lock, flags);
911 912 913 914 915 916 917 918
			return -EINVAL;
		}

		if (capture)
			i2s_rxctrl(i2s, 1);
		else
			i2s_txctrl(i2s, 1);

919
		spin_unlock_irqrestore(i2s->lock, flags);
920 921 922 923
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
924
		spin_lock_irqsave(i2s->lock, flags);
925

926
		if (capture) {
927
			i2s_rxctrl(i2s, 0);
928
			i2s_fifo(i2s, FIC_RXFLUSH);
929 930
		} else {
			i2s_txctrl(i2s, 0);
931
			i2s_fifo(i2s, FIC_TXFLUSH);
932
		}
933

934
		spin_unlock_irqrestore(i2s->lock, flags);
935
		pm_runtime_put(dai->dev);
936 937 938 939 940 941 942 943 944 945
		break;
	}

	return 0;
}

static int i2s_set_clkdiv(struct snd_soc_dai *dai,
	int div_id, int div)
{
	struct i2s_dai *i2s = to_info(dai);
946
	struct i2s_dai *other = get_other_dai(i2s);
947 948 949

	switch (div_id) {
	case SAMSUNG_I2S_DIV_BCLK:
950
		pm_runtime_get_sync(dai->dev);
951 952
		if ((any_active(i2s) && div && (get_bfs(i2s) != div))
			|| (other && other->bfs && (other->bfs != div))) {
953
			pm_runtime_put(dai->dev);
954 955 956 957 958
			dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
			return -EAGAIN;
		}
		i2s->bfs = div;
959
		pm_runtime_put(dai->dev);
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
		break;
	default:
		dev_err(&i2s->pdev->dev,
			"Invalid clock divider(%d)\n", div_id);
		return -EINVAL;
	}

	return 0;
}

static snd_pcm_sframes_t
i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);
	u32 reg = readl(i2s->addr + I2SFIC);
	snd_pcm_sframes_t delay;
976
	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
977

978 979
	WARN_ON(!pm_runtime_active(dai->dev));

980 981 982 983 984
	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
		delay = FIC_RXCOUNT(reg);
	else if (is_secondary(i2s))
		delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
	else
985
		delay = (reg >> i2s_regs->ftx0cnt_off) & 0x7f;
986 987 988 989 990 991 992

	return delay;
}

#ifdef CONFIG_PM
static int i2s_suspend(struct snd_soc_dai *dai)
{
993
	return pm_runtime_force_suspend(dai->dev);
994 995 996 997
}

static int i2s_resume(struct snd_soc_dai *dai)
{
998
	return pm_runtime_force_resume(dai->dev);
999 1000 1001 1002 1003 1004 1005 1006 1007
}
#else
#define i2s_suspend NULL
#define i2s_resume  NULL
#endif

static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);
1008
	struct i2s_dai *other = get_other_dai(i2s);
1009
	unsigned long flags;
1010

1011 1012
	pm_runtime_get_sync(dai->dev);

1013
	if (is_secondary(i2s)) { /* If this is probe on the secondary DAI */
1014
		snd_soc_dai_init_dma_data(dai, &other->sec_dai->dma_playback,
1015
					   NULL);
1016
	} else {
1017
		snd_soc_dai_init_dma_data(dai, &i2s->dma_playback,
1018
					   &i2s->dma_capture);
1019

1020 1021
		if (i2s->quirks & QUIRK_NEED_RSTCLR)
			writel(CON_RSTCLR, i2s->addr + I2SCON);
1022

1023 1024
		if (i2s->quirks & QUIRK_SUPPORTS_IDMA)
			idma_reg_addr_init(i2s->addr,
1025
					i2s->sec_dai->idma_playback.addr);
1026
	}
1027

1028 1029 1030
	/* Reset any constraint on RFS and BFS */
	i2s->rfs = 0;
	i2s->bfs = 0;
1031
	i2s->rclk_srcrate = 0;
1032 1033

	spin_lock_irqsave(i2s->lock, flags);
1034 1035 1036 1037 1038
	i2s_txctrl(i2s, 0);
	i2s_rxctrl(i2s, 0);
	i2s_fifo(i2s, FIC_TXFLUSH);
	i2s_fifo(other, FIC_TXFLUSH);
	i2s_fifo(i2s, FIC_RXFLUSH);
1039
	spin_unlock_irqrestore(i2s->lock, flags);
1040 1041 1042 1043 1044

	/* Gate CDCLK by default */
	if (!is_opened(other))
		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
				0, SND_SOC_CLOCK_IN);
1045
	pm_runtime_put(dai->dev);
1046 1047 1048 1049 1050 1051 1052

	return 0;
}

static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
1053
	unsigned long flags;
1054

1055 1056
	pm_runtime_get_sync(dai->dev);

1057
	if (!is_secondary(i2s)) {
1058
		if (i2s->quirks & QUIRK_NEED_RSTCLR) {
1059
			spin_lock_irqsave(i2s->lock, flags);
1060
			writel(0, i2s->addr + I2SCON);
1061
			spin_unlock_irqrestore(i2s->lock, flags);
1062
		}
1063 1064
	}

1065 1066
	pm_runtime_put(dai->dev);

1067 1068 1069
	return 0;
}

1070
static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	.trigger = i2s_trigger,
	.hw_params = i2s_hw_params,
	.set_fmt = i2s_set_fmt,
	.set_clkdiv = i2s_set_clkdiv,
	.set_sysclk = i2s_set_sysclk,
	.startup = i2s_startup,
	.shutdown = i2s_shutdown,
	.delay = i2s_delay,
};

1081 1082 1083 1084
static const struct snd_soc_component_driver samsung_i2s_component = {
	.name		= "samsung-i2s",
};

1085 1086 1087 1088
#define SAMSUNG_I2S_FMTS	(SNDRV_PCM_FMTBIT_S8 | \
					SNDRV_PCM_FMTBIT_S16_LE | \
					SNDRV_PCM_FMTBIT_S24_LE)

1089 1090 1091
static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev,
				const struct samsung_i2s_dai_data *i2s_dai_data,
				bool sec)
1092 1093 1094
{
	struct i2s_dai *i2s;

1095
	i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
1096 1097 1098 1099 1100 1101
	if (i2s == NULL)
		return NULL;

	i2s->pdev = pdev;
	i2s->pri_dai = NULL;
	i2s->sec_dai = NULL;
1102
	i2s->i2s_dai_drv.id = 1;
1103 1104 1105 1106 1107 1108
	i2s->i2s_dai_drv.symmetric_rates = 1;
	i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
	i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
	i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
	i2s->i2s_dai_drv.suspend = i2s_suspend;
	i2s->i2s_dai_drv.resume = i2s_resume;
1109
	i2s->i2s_dai_drv.playback.channels_min = 1;
1110
	i2s->i2s_dai_drv.playback.channels_max = 2;
1111
	i2s->i2s_dai_drv.playback.rates = i2s_dai_data->pcm_rates;
1112 1113 1114
	i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;

	if (!sec) {
1115
		i2s->i2s_dai_drv.name = SAMSUNG_I2S_DAI;
1116
		i2s->i2s_dai_drv.capture.channels_min = 1;
1117
		i2s->i2s_dai_drv.capture.channels_max = 2;
1118
		i2s->i2s_dai_drv.capture.rates = i2s_dai_data->pcm_rates;
1119
		i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1120 1121
	} else {
		i2s->i2s_dai_drv.name = SAMSUNG_I2S_DAI_SEC;
1122
	}
1123 1124 1125
	return i2s;
}

1126
#ifdef CONFIG_PM
1127 1128 1129 1130
static int i2s_runtime_suspend(struct device *dev)
{
	struct i2s_dai *i2s = dev_get_drvdata(dev);

1131 1132 1133 1134
	i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
	i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
	i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);

1135 1136
	if (i2s->op_clk)
		clk_disable_unprepare(i2s->op_clk);
1137 1138 1139 1140 1141 1142 1143 1144
	clk_disable_unprepare(i2s->clk);

	return 0;
}

static int i2s_runtime_resume(struct device *dev)
{
	struct i2s_dai *i2s = dev_get_drvdata(dev);
1145
	int ret;
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	ret = clk_prepare_enable(i2s->clk);
	if (ret)
		return ret;

	if (i2s->op_clk) {
		ret = clk_prepare_enable(i2s->op_clk);
		if (ret) {
			clk_disable_unprepare(i2s->clk);
			return ret;
		}
	}
1158

1159 1160 1161
	writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
	writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
	writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
1162 1163 1164

	return 0;
}
1165
#endif /* CONFIG_PM */
1166

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
static void i2s_unregister_clocks(struct i2s_dai *i2s)
{
	int i;

	for (i = 0; i < i2s->clk_data.clk_num; i++) {
		if (!IS_ERR(i2s->clk_table[i]))
			clk_unregister(i2s->clk_table[i]);
	}
}

static void i2s_unregister_clock_provider(struct platform_device *pdev)
{
	struct i2s_dai *i2s = dev_get_drvdata(&pdev->dev);

	of_clk_del_provider(pdev->dev.of_node);
	i2s_unregister_clocks(i2s);
}

static int i2s_register_clock_provider(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct i2s_dai *i2s = dev_get_drvdata(dev);
	const char *clk_name[2] = { "i2s_opclk0", "i2s_opclk1" };
	const char *p_names[2] = { NULL };
	const struct samsung_i2s_variant_regs *reg_info = i2s->variant_regs;
	struct clk *rclksrc;
	int ret, i;

	/* Register the clock provider only if it's expected in the DTB */
	if (!of_find_property(dev->of_node, "#clock-cells", NULL))
		return 0;

	/* Get the RCLKSRC mux clock parent clock names */
	for (i = 0; i < ARRAY_SIZE(p_names); i++) {
		rclksrc = clk_get(dev, clk_name[i]);
		if (IS_ERR(rclksrc))
			continue;
		p_names[i] = __clk_get_name(rclksrc);
		clk_put(rclksrc);
	}

	if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
		/* Activate the prescaler */
		u32 val = readl(i2s->addr + I2SPSR);
		writel(val | PSR_PSREN, i2s->addr + I2SPSR);

1213
		i2s->clk_table[CLK_I2S_RCLK_SRC] = clk_register_mux(dev,
1214 1215 1216 1217 1218
				"i2s_rclksrc", p_names, ARRAY_SIZE(p_names),
				CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
				i2s->addr + I2SMOD, reg_info->rclksrc_off,
				1, 0, i2s->lock);

1219
		i2s->clk_table[CLK_I2S_RCLK_PSR] = clk_register_divider(dev,
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
				"i2s_presc", "i2s_rclksrc",
				CLK_SET_RATE_PARENT,
				i2s->addr + I2SPSR, 8, 6, 0, i2s->lock);

		p_names[0] = "i2s_presc";
		i2s->clk_data.clk_num = 2;
	}
	of_property_read_string_index(dev->of_node,
				"clock-output-names", 0, &clk_name[0]);

1230
	i2s->clk_table[CLK_I2S_CDCLK] = clk_register_gate(dev, clk_name[0],
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
				p_names[0], CLK_SET_RATE_PARENT,
				i2s->addr + I2SMOD, reg_info->cdclkcon_off,
				CLK_GATE_SET_TO_DISABLE, i2s->lock);

	i2s->clk_data.clk_num += 1;
	i2s->clk_data.clks = i2s->clk_table;

	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
				  &i2s->clk_data);
	if (ret < 0) {
		dev_err(dev, "failed to add clock provider: %d\n", ret);
		i2s_unregister_clocks(i2s);
	}

	return ret;
}

1248
static int samsung_i2s_probe(struct platform_device *pdev)
1249 1250
{
	struct i2s_dai *pri_dai, *sec_dai = NULL;
1251
	struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
1252
	struct resource *res;
1253 1254
	u32 regs_base, quirks = 0, idma_addr = 0;
	struct device_node *np = pdev->dev.of_node;
1255
	const struct samsung_i2s_dai_data *i2s_dai_data;
1256
	int ret;
1257

1258 1259 1260 1261 1262
	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
		i2s_dai_data = of_device_get_match_data(&pdev->dev);
	else
		i2s_dai_data = (struct samsung_i2s_dai_data *)
				platform_get_device_id(pdev)->driver_data;
1263

1264
	pri_dai = i2s_alloc_dai(pdev, i2s_dai_data, false);
1265 1266 1267
	if (!pri_dai) {
		dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
		return -ENOMEM;
1268 1269
	}

1270 1271 1272
	spin_lock_init(&pri_dai->spinlock);
	pri_dai->lock = &pri_dai->spinlock;

1273 1274 1275 1276 1277 1278
	if (!np) {
		if (i2s_pdata == NULL) {
			dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
			return -EINVAL;
		}

1279 1280
		pri_dai->dma_playback.filter_data = i2s_pdata->dma_playback;
		pri_dai->dma_capture.filter_data = i2s_pdata->dma_capture;
1281
		pri_dai->filter = i2s_pdata->dma_filter;
1282

1283 1284
		quirks = i2s_pdata->type.quirks;
		idma_addr = i2s_pdata->type.idma_addr;
1285
	} else {
1286
		quirks = i2s_dai_data->quirks;
1287 1288
		if (of_property_read_u32(np, "samsung,idma-addr",
					 &idma_addr)) {
1289 1290
			if (quirks & QUIRK_SUPPORTS_IDMA) {
				dev_info(&pdev->dev, "idma address is not"\
1291 1292 1293 1294
						"specified");
			}
		}
	}
1295 1296

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1297 1298 1299
	pri_dai->addr = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(pri_dai->addr))
		return PTR_ERR(pri_dai->addr);
1300 1301 1302

	regs_base = res->start;

1303 1304 1305 1306 1307
	pri_dai->clk = devm_clk_get(&pdev->dev, "iis");
	if (IS_ERR(pri_dai->clk)) {
		dev_err(&pdev->dev, "Failed to get iis clock\n");
		return PTR_ERR(pri_dai->clk);
	}
1308 1309 1310 1311 1312 1313

	ret = clk_prepare_enable(pri_dai->clk);
	if (ret != 0) {
		dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
		return ret;
	}
1314 1315
	pri_dai->dma_playback.addr = regs_base + I2STXD;
	pri_dai->dma_capture.addr = regs_base + I2SRXD;
1316 1317
	pri_dai->dma_playback.chan_name = "tx";
	pri_dai->dma_capture.chan_name = "rx";
1318 1319
	pri_dai->dma_playback.addr_width = 4;
	pri_dai->dma_capture.addr_width = 4;
1320
	pri_dai->quirks = quirks;
1321
	pri_dai->variant_regs = i2s_dai_data->i2s_variant_regs;
1322 1323 1324 1325

	if (quirks & QUIRK_PRI_6CHAN)
		pri_dai->i2s_dai_drv.playback.channels_max = 6;

1326 1327 1328 1329 1330
	ret = samsung_asoc_dma_platform_register(&pdev->dev, pri_dai->filter,
						 NULL, NULL);
	if (ret < 0)
		goto err_disable_clk;

1331 1332 1333 1334 1335 1336
	ret = devm_snd_soc_register_component(&pdev->dev,
					&samsung_i2s_component,
					&pri_dai->i2s_dai_drv, 1);
	if (ret < 0)
		goto err_disable_clk;

1337
	if (quirks & QUIRK_SEC_DAI) {
1338
		sec_dai = i2s_alloc_dai(pdev, i2s_dai_data, true);
1339 1340
		if (!sec_dai) {
			dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1341 1342
			ret = -ENOMEM;
			goto err_disable_clk;
1343
		}
1344

1345
		sec_dai->lock = &pri_dai->spinlock;
1346
		sec_dai->variant_regs = pri_dai->variant_regs;
1347
		sec_dai->dma_playback.addr = regs_base + I2STXDS;
1348
		sec_dai->dma_playback.chan_name = "tx-sec";
1349

1350
		if (!np) {
1351
			sec_dai->dma_playback.filter_data = i2s_pdata->dma_play_sec;
1352 1353
			sec_dai->filter = i2s_pdata->dma_filter;
		}
1354

1355
		sec_dai->dma_playback.addr_width = 4;
1356
		sec_dai->addr = pri_dai->addr;
1357
		sec_dai->clk = pri_dai->clk;
1358
		sec_dai->quirks = quirks;
1359
		sec_dai->idma_playback.addr = idma_addr;
1360 1361
		sec_dai->pri_dai = pri_dai;
		pri_dai->sec_dai = sec_dai;
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

		ret = samsung_asoc_dma_platform_register(&pdev->dev,
					sec_dai->filter, "tx-sec", NULL);
		if (ret < 0)
			goto err_disable_clk;

		ret = devm_snd_soc_register_component(&pdev->dev,
						&samsung_i2s_component,
						&sec_dai->i2s_dai_drv, 1);
		if (ret < 0)
			goto err_disable_clk;
1373 1374
	}

1375 1376
	if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
		dev_err(&pdev->dev, "Unable to configure gpio\n");
1377 1378
		ret = -EINVAL;
		goto err_disable_clk;
1379 1380
	}

1381
	dev_set_drvdata(&pdev->dev, pri_dai);
1382

1383
	pm_runtime_set_active(&pdev->dev);
1384 1385
	pm_runtime_enable(&pdev->dev);

1386 1387 1388
	ret = i2s_register_clock_provider(pdev);
	if (!ret)
		return 0;
1389

1390
	pm_runtime_disable(&pdev->dev);
1391 1392
err_disable_clk:
	clk_disable_unprepare(pri_dai->clk);
1393
	return ret;
1394 1395
}

1396
static int samsung_i2s_remove(struct platform_device *pdev)
1397
{
1398
	struct i2s_dai *pri_dai;
1399

1400
	pri_dai = dev_get_drvdata(&pdev->dev);
1401

1402
	pm_runtime_get_sync(&pdev->dev);
1403
	pm_runtime_disable(&pdev->dev);
1404

1405 1406
	i2s_unregister_clock_provider(pdev);
	clk_disable_unprepare(pri_dai->clk);
1407
	pm_runtime_put_noidle(&pdev->dev);
1408 1409 1410 1411

	return 0;
}

1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
static const struct samsung_i2s_variant_regs i2sv3_regs = {
	.bfs_off = 1,
	.rfs_off = 3,
	.sdf_off = 5,
	.txr_off = 8,
	.rclksrc_off = 10,
	.mss_off = 11,
	.cdclkcon_off = 12,
	.lrp_off = 7,
	.bfs_mask = 0x3,
	.rfs_mask = 0x3,
	.ftx0cnt_off = 8,
};

static const struct samsung_i2s_variant_regs i2sv6_regs = {
	.bfs_off = 0,
	.rfs_off = 4,
	.sdf_off = 6,
	.txr_off = 8,
	.rclksrc_off = 10,
	.mss_off = 11,
	.cdclkcon_off = 12,
	.lrp_off = 15,
	.bfs_mask = 0xf,
	.rfs_mask = 0x3,
	.ftx0cnt_off = 8,
};

static const struct samsung_i2s_variant_regs i2sv7_regs = {
	.bfs_off = 0,
	.rfs_off = 4,
	.sdf_off = 7,
	.txr_off = 9,
	.rclksrc_off = 11,
	.mss_off = 12,
	.cdclkcon_off = 22,
	.lrp_off = 15,
	.bfs_mask = 0xf,
	.rfs_mask = 0x7,
	.ftx0cnt_off = 0,
};

static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = {
	.bfs_off = 0,
	.rfs_off = 3,
	.sdf_off = 6,
	.txr_off = 8,
	.rclksrc_off = 10,
	.mss_off = 11,
	.cdclkcon_off = 12,
	.lrp_off = 15,
	.bfs_mask = 0x7,
	.rfs_mask = 0x7,
	.ftx0cnt_off = 8,
};

1468 1469
static const struct samsung_i2s_dai_data i2sv3_dai_type = {
	.quirks = QUIRK_NO_MUXPSR,
1470
	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1471
	.i2s_variant_regs = &i2sv3_regs,
1472 1473 1474
};

static const struct samsung_i2s_dai_data i2sv5_dai_type = {
1475 1476
	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
			QUIRK_SUPPORTS_IDMA,
1477
	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1478
	.i2s_variant_regs = &i2sv3_regs,
1479 1480
};

1481 1482
static const struct samsung_i2s_dai_data i2sv6_dai_type = {
	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1483
			QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
1484
	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1485 1486 1487 1488 1489 1490
	.i2s_variant_regs = &i2sv6_regs,
};

static const struct samsung_i2s_dai_data i2sv7_dai_type = {
	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
			QUIRK_SUPPORTS_TDM,
1491
	.pcm_rates = SNDRV_PCM_RATE_8000_192000,
1492 1493 1494 1495 1496
	.i2s_variant_regs = &i2sv7_regs,
};

static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 = {
	.quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
1497
	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1498
	.i2s_variant_regs = &i2sv5_i2s1_regs,
1499 1500
};

1501
static const struct platform_device_id samsung_i2s_driver_ids[] = {
1502 1503
	{
		.name           = "samsung-i2s",
1504
		.driver_data	= (kernel_ulong_t)&i2sv3_dai_type,
1505 1506 1507
	},
	{},
};
1508
MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
1509

1510 1511
#ifdef CONFIG_OF
static const struct of_device_id exynos_i2s_match[] = {
1512 1513 1514 1515 1516 1517
	{
		.compatible = "samsung,s3c6410-i2s",
		.data = &i2sv3_dai_type,
	}, {
		.compatible = "samsung,s5pv210-i2s",
		.data = &i2sv5_dai_type,
1518 1519 1520
	}, {
		.compatible = "samsung,exynos5420-i2s",
		.data = &i2sv6_dai_type,
1521 1522 1523 1524 1525 1526
	}, {
		.compatible = "samsung,exynos7-i2s",
		.data = &i2sv7_dai_type,
	}, {
		.compatible = "samsung,exynos7-i2s1",
		.data = &i2sv5_dai_type_i2s1,
1527 1528 1529 1530 1531 1532
	},
	{},
};
MODULE_DEVICE_TABLE(of, exynos_i2s_match);
#endif

1533 1534 1535
static const struct dev_pm_ops samsung_i2s_pm = {
	SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
				i2s_runtime_resume, NULL)
1536 1537
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				     pm_runtime_force_resume)
1538 1539
};

1540 1541
static struct platform_driver samsung_i2s_driver = {
	.probe  = samsung_i2s_probe,
1542
	.remove = samsung_i2s_remove,
1543
	.id_table = samsung_i2s_driver_ids,
1544 1545
	.driver = {
		.name = "samsung-i2s",
1546
		.of_match_table = of_match_ptr(exynos_i2s_match),
1547
		.pm = &samsung_i2s_pm,
1548 1549 1550
	},
};

1551
module_platform_driver(samsung_i2s_driver);
1552 1553

/* Module information */
1554
MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1555 1556 1557
MODULE_DESCRIPTION("Samsung I2S Interface");
MODULE_ALIAS("platform:samsung-i2s");
MODULE_LICENSE("GPL");