i2s.c 33.6 KB
Newer Older
1
/* sound/soc/samsung/i2s.c
J
Jassi Brar 已提交
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>
J
Jassi Brar 已提交
7 8 9 10 11 12 13 14 15 16
 *
 * 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 <linux/delay.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/io.h>
17
#include <linux/module.h>
18 19
#include <linux/of.h>
#include <linux/of_gpio.h>
20
#include <linux/pm_runtime.h>
J
Jassi Brar 已提交
21 22

#include <sound/soc.h>
23
#include <sound/pcm_params.h>
J
Jassi Brar 已提交
24

25
#include <linux/platform_data/asoc-s3c.h>
J
Jassi Brar 已提交
26 27

#include "dma.h"
28
#include "idma.h"
J
Jassi Brar 已提交
29
#include "i2s.h"
30
#include "i2s-regs.h"
J
Jassi Brar 已提交
31 32 33

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

34 35 36 37 38
enum samsung_dai_type {
	TYPE_PRI,
	TYPE_SEC,
};

39 40 41 42 43 44 45 46 47 48 49 50 51 52
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;
};

53 54
struct samsung_i2s_dai_data {
	int dai_type;
55
	u32 quirks;
56
	const struct samsung_i2s_variant_regs *i2s_variant_regs;
57 58
};

J
Jassi Brar 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
struct i2s_dai {
	/* Platform device for this DAI */
	struct platform_device *pdev;
	/* IOREMAP'd SFRs */
	void __iomem	*addr;
	/* Physical base address of SFRs */
	u32	base;
	/* 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;
86 87
	/* CDCLK pin direction: 0  - input, 1 - output */
	unsigned int cdclk_out:1;
J
Jassi Brar 已提交
88 89 90 91 92
	/* Driver for this DAI */
	struct snd_soc_dai_driver i2s_dai_drv;
	/* DMA parameters */
	struct s3c_dma_params dma_playback;
	struct s3c_dma_params dma_capture;
93
	struct s3c_dma_params idma_playback;
J
Jassi Brar 已提交
94 95 96 97
	u32	quirks;
	u32	suspend_i2smod;
	u32	suspend_i2scon;
	u32	suspend_i2spsr;
98
	const struct samsung_i2s_variant_regs *variant_regs;
J
Jassi Brar 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
};

/* 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)
{
113 114
	u32 mod = readl(i2s->addr + I2SMOD);
	return (mod & (1 << i2s->variant_regs->mss_off)) ? true : false;
J
Jassi Brar 已提交
115 116 117 118 119 120 121 122 123 124
}

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

	if (!i2s)
		return false;

125
	active = readl(i2s->addr + I2SCON);
J
Jassi Brar 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

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

	return active ? true : false;
}

/* If the other interface of the controller is transmitting data */
static inline bool other_tx_active(struct i2s_dai *i2s)
{
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;

	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;

157
	active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
J
Jassi Brar 已提交
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 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

	return active ? true : false;
}

/* If the other interface of the controller is receiving data */
static inline bool other_rx_active(struct i2s_dai *i2s)
{
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;

	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)
{
218
	u32 rfs;
219 220
	rfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->rfs_off;
	rfs &= i2s->variant_regs->rfs_mask;
J
Jassi Brar 已提交
221 222

	switch (rfs) {
223 224 225 226
	case 7: return 192;
	case 6: return 96;
	case 5: return 128;
	case 4: return 64;
J
Jassi Brar 已提交
227 228 229 230 231 232 233 234 235 236 237
	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);
238
	int rfs_shift = i2s->variant_regs->rfs_off;
J
Jassi Brar 已提交
239

240
	mod &= ~(i2s->variant_regs->rfs_mask << rfs_shift);
J
Jassi Brar 已提交
241 242

	switch (rfs) {
243 244 245 246 247 248 249 250 251 252 253 254
	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;
J
Jassi Brar 已提交
255
	case 768:
256
		mod |= (MOD_RCLK_768FS << rfs_shift);
J
Jassi Brar 已提交
257 258
		break;
	case 512:
259
		mod |= (MOD_RCLK_512FS << rfs_shift);
J
Jassi Brar 已提交
260 261
		break;
	case 384:
262
		mod |= (MOD_RCLK_384FS << rfs_shift);
J
Jassi Brar 已提交
263 264
		break;
	default:
265
		mod |= (MOD_RCLK_256FS << rfs_shift);
J
Jassi Brar 已提交
266 267 268 269 270 271 272 273 274
		break;
	}

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

/* Read Bit-Clock of I2S (in multiples of LRCLK) */
static inline unsigned get_bfs(struct i2s_dai *i2s)
{
275
	u32 bfs;
276 277
	bfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->bfs_off;
	bfs &= i2s->variant_regs->bfs_mask;
J
Jassi Brar 已提交
278 279

	switch (bfs) {
280 281 282 283 284
	case 8: return 256;
	case 7: return 192;
	case 6: return 128;
	case 5: return 96;
	case 4: return 64;
J
Jassi Brar 已提交
285 286 287 288 289 290 291 292 293 294 295
	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);
296
	int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
297
	int bfs_shift = i2s->variant_regs->bfs_off;
298 299 300 301 302 303

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

305 306
	mod &= ~(i2s->variant_regs->bfs_mask << bfs_shift);

J
Jassi Brar 已提交
307 308
	switch (bfs) {
	case 48:
309
		mod |= (MOD_BCLK_48FS << bfs_shift);
J
Jassi Brar 已提交
310 311
		break;
	case 32:
312
		mod |= (MOD_BCLK_32FS << bfs_shift);
J
Jassi Brar 已提交
313 314
		break;
	case 24:
315
		mod |= (MOD_BCLK_24FS << bfs_shift);
J
Jassi Brar 已提交
316 317
		break;
	case 16:
318
		mod |= (MOD_BCLK_16FS << bfs_shift);
J
Jassi Brar 已提交
319
		break;
320 321 322 323 324 325 326 327 328 329 330 331 332 333
	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);
J
Jassi Brar 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
		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;
361
	int txr_off = i2s->variant_regs->txr_off;
J
Jassi Brar 已提交
362
	u32 con = readl(addr + I2SCON);
363
	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
J
Jassi Brar 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377

	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))
378
			mod |= 2 << txr_off;
J
Jassi Brar 已提交
379
		else
380
			mod |= 0 << txr_off;
J
Jassi Brar 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	} 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))
398
			mod |= 1 << txr_off;
J
Jassi Brar 已提交
399 400 401 402 403 404 405 406 407 408 409 410
		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;
411
	int txr_off = i2s->variant_regs->txr_off;
J
Jassi Brar 已提交
412
	u32 con = readl(addr + I2SCON);
413
	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
J
Jassi Brar 已提交
414 415 416 417 418 419

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

		if (any_tx_active(i2s))
420
			mod |= 2 << txr_off;
J
Jassi Brar 已提交
421
		else
422
			mod |= 1 << txr_off;
J
Jassi Brar 已提交
423 424 425 426 427
	} else {
		con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
		con &= ~CON_RXDMA_ACTIVE;

		if (any_tx_active(i2s))
428
			mod |= 0 << txr_off;
J
Jassi Brar 已提交
429 430 431 432 433 434 435 436 437 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
		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);
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
	u32 mod = readl(i2s->addr + I2SMOD);
468 469 470
	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;
J
Jassi Brar 已提交
471 472

	switch (clk_id) {
473 474 475 476
	case SAMSUNG_I2S_OPCLK:
		mod &= ~MOD_OPCLK_MASK;
		mod |= dir;
		break;
J
Jassi Brar 已提交
477 478 479 480 481
	case SAMSUNG_I2S_CDCLK:
		/* Shouldn't matter in GATING(CLOCK_IN) mode */
		if (dir == SND_SOC_CLOCK_IN)
			rfs = 0;

482
		if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
J
Jassi Brar 已提交
483 484
				(any_active(i2s) &&
				(((dir == SND_SOC_CLOCK_IN)
485
					&& !(mod & cdcon_mask)) ||
J
Jassi Brar 已提交
486
				((dir == SND_SOC_CLOCK_OUT)
487
					&& (mod & cdcon_mask))))) {
J
Jassi Brar 已提交
488 489 490 491 492 493
			dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
			return -EAGAIN;
		}

		if (dir == SND_SOC_CLOCK_IN)
494
			mod |= 1 << i2s_regs->cdclkcon_off;
J
Jassi Brar 已提交
495
		else
496
			mod &= ~(1 << i2s_regs->cdclkcon_off);
J
Jassi Brar 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509

		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 */
		if ((i2s->quirks & QUIRK_NO_MUXPSR)
				|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
			clk_id = 0;
		else
			clk_id = 1;

		if (!any_active(i2s)) {
510
			if (i2s->op_clk && !IS_ERR(i2s->op_clk)) {
511 512
				if ((clk_id && !(mod & rsrc_mask)) ||
					(!clk_id && (mod & rsrc_mask))) {
513
					clk_disable_unprepare(i2s->op_clk);
J
Jassi Brar 已提交
514 515
					clk_put(i2s->op_clk);
				} else {
516 517
					i2s->rclk_srcrate =
						clk_get_rate(i2s->op_clk);
J
Jassi Brar 已提交
518 519 520 521
					return 0;
				}
			}

522 523 524 525 526 527
			if (clk_id)
				i2s->op_clk = clk_get(&i2s->pdev->dev,
						"i2s_opclk1");
			else
				i2s->op_clk = clk_get(&i2s->pdev->dev,
						"i2s_opclk0");
528 529 530 531

			if (WARN_ON(IS_ERR(i2s->op_clk)))
				return PTR_ERR(i2s->op_clk);

532
			clk_prepare_enable(i2s->op_clk);
J
Jassi Brar 已提交
533 534 535 536 537 538 539
			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;
			}
540 541
		} else if ((!clk_id && (mod & rsrc_mask))
				|| (clk_id && !(mod & rsrc_mask))) {
J
Jassi Brar 已提交
542 543 544 545 546 547 548 549 550 551 552
			dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
			return -EAGAIN;
		} else {
			/* Call can't be on the active DAI */
			i2s->op_clk = other->op_clk;
			i2s->rclk_srcrate = other->rclk_srcrate;
			return 0;
		}

		if (clk_id == 0)
553
			mod &= ~(1 << i2s_regs->rclksrc_off);
J
Jassi Brar 已提交
554
		else
555
			mod |= 1 << i2s_regs->rclksrc_off;
J
Jassi Brar 已提交
556

557
		break;
J
Jassi Brar 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
	default:
		dev_err(&i2s->pdev->dev, "We don't serve that!\n");
		return -EINVAL;
	}

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

	return 0;
}

static int i2s_set_fmt(struct snd_soc_dai *dai,
	unsigned int fmt)
{
	struct i2s_dai *i2s = to_info(dai);
	u32 mod = readl(i2s->addr + I2SMOD);
573
	int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
J
Jassi Brar 已提交
574 575
	u32 tmp = 0;

576 577 578
	lrp_shift = i2s->variant_regs->lrp_off;
	sdf_shift = i2s->variant_regs->sdf_off;
	mod_slave = 1 << i2s->variant_regs->mss_off;
579

580 581 582
	sdf_mask = MOD_SDF_MASK << sdf_shift;
	lrp_rlow = MOD_LR_RLOW << lrp_shift;

J
Jassi Brar 已提交
583 584 585
	/* Format is priority */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_RIGHT_J:
586 587
		tmp |= lrp_rlow;
		tmp |= (MOD_SDF_MSB << sdf_shift);
J
Jassi Brar 已提交
588 589
		break;
	case SND_SOC_DAIFMT_LEFT_J:
590 591
		tmp |= lrp_rlow;
		tmp |= (MOD_SDF_LSB << sdf_shift);
J
Jassi Brar 已提交
592 593
		break;
	case SND_SOC_DAIFMT_I2S:
594
		tmp |= (MOD_SDF_IIS << sdf_shift);
J
Jassi Brar 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608
		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:
609 610
		if (tmp & lrp_rlow)
			tmp &= ~lrp_rlow;
J
Jassi Brar 已提交
611
		else
612
			tmp |= lrp_rlow;
J
Jassi Brar 已提交
613 614 615 616 617 618 619 620
		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:
621
		tmp |= mod_slave;
J
Jassi Brar 已提交
622 623 624 625 626 627 628 629 630 631 632 633
		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;
	}

634 635 636 637
	/*
	 * Don't change the I2S mode if any controller is active on this
	 * channel.
	 */
J
Jassi Brar 已提交
638
	if (any_active(i2s) &&
639
		((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
J
Jassi Brar 已提交
640 641 642 643 644
		dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
		return -EAGAIN;
	}

645
	mod &= ~(sdf_mask | lrp_rlow | mod_slave);
J
Jassi Brar 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	mod |= tmp;
	writel(mod, i2s->addr + I2SMOD);

	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);
	u32 mod = readl(i2s->addr + I2SMOD);

	if (!is_secondary(i2s))
		mod &= ~(MOD_DC2_EN | MOD_DC1_EN);

	switch (params_channels(params)) {
	case 6:
		mod |= MOD_DC2_EN;
	case 4:
		mod |= MOD_DC1_EN;
		break;
	case 2:
668 669 670 671 672 673 674 675 676 677 678
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			i2s->dma_playback.dma_size = 4;
		else
			i2s->dma_capture.dma_size = 4;
		break;
	case 1:
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			i2s->dma_playback.dma_size = 2;
		else
			i2s->dma_capture.dma_size = 2;

J
Jassi Brar 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		break;
	default:
		dev_err(&i2s->pdev->dev, "%d channels not supported\n",
				params_channels(params));
		return -EINVAL;
	}

	if (is_secondary(i2s))
		mod &= ~MOD_BLCS_MASK;
	else
		mod &= ~MOD_BLCP_MASK;

	if (is_manager(i2s))
		mod &= ~MOD_BLC_MASK;

694 695
	switch (params_width(params)) {
	case 8:
J
Jassi Brar 已提交
696 697 698 699 700 701 702
		if (is_secondary(i2s))
			mod |= MOD_BLCS_8BIT;
		else
			mod |= MOD_BLCP_8BIT;
		if (is_manager(i2s))
			mod |= MOD_BLC_8BIT;
		break;
703
	case 16:
J
Jassi Brar 已提交
704 705 706 707 708 709 710
		if (is_secondary(i2s))
			mod |= MOD_BLCS_16BIT;
		else
			mod |= MOD_BLCP_16BIT;
		if (is_manager(i2s))
			mod |= MOD_BLC_16BIT;
		break;
711
	case 24:
J
Jassi Brar 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725
		if (is_secondary(i2s))
			mod |= MOD_BLCS_24BIT;
		else
			mod |= MOD_BLCP_24BIT;
		if (is_manager(i2s))
			mod |= MOD_BLC_24BIT;
		break;
	default:
		dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
				params_format(params));
		return -EINVAL;
	}
	writel(mod, i2s->addr + I2SMOD);

726 727
	samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);

J
Jassi Brar 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
	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);
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
	unsigned long flags;

	spin_lock_irqsave(&lock, flags);

	i2s->mode |= DAI_OPENED;

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

750 751 752
	if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
		writel(CON_RSTCLR, i2s->addr + I2SCON);

J
Jassi Brar 已提交
753 754
	spin_unlock_irqrestore(&lock, flags);

755 756 757
	if (!is_opened(other) && i2s->cdclk_out)
		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
				0, SND_SOC_CLOCK_OUT);
J
Jassi Brar 已提交
758 759 760 761 762 763 764 765 766
	return 0;
}

static void i2s_shutdown(struct snd_pcm_substream *substream,
	struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
	unsigned long flags;
767
	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
J
Jassi Brar 已提交
768 769 770 771 772 773

	spin_lock_irqsave(&lock, flags);

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

774
	if (is_opened(other)) {
J
Jassi Brar 已提交
775
		other->mode |= DAI_MANAGER;
776 777
	} else {
		u32 mod = readl(i2s->addr + I2SMOD);
778
		i2s->cdclk_out = !(mod & (1 << i2s_regs->cdclkcon_off));
779 780
		if (other)
			other->cdclk_out = i2s->cdclk_out;
781
	}
J
Jassi Brar 已提交
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
	/* Reset any constraint on RFS and BFS */
	i2s->rfs = 0;
	i2s->bfs = 0;

	spin_unlock_irqrestore(&lock, flags);

	/* Gate CDCLK by default */
	if (!is_opened(other))
		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
				0, SND_SOC_CLOCK_IN);
}

static int config_setup(struct i2s_dai *i2s)
{
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
	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);

839 840 841 842
	/* Don't bother with PSR in Slave mode */
	if (is_slave(i2s))
		return 0;

J
Jassi Brar 已提交
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 879 880 881 882 883 884
	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:
		local_irq_save(flags);

		if (config_setup(i2s)) {
			local_irq_restore(flags);
			return -EINVAL;
		}

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

		local_irq_restore(flags);
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		local_irq_save(flags);

885
		if (capture) {
J
Jassi Brar 已提交
886
			i2s_rxctrl(i2s, 0);
887
			i2s_fifo(i2s, FIC_RXFLUSH);
888 889
		} else {
			i2s_txctrl(i2s, 0);
890
			i2s_fifo(i2s, FIC_TXFLUSH);
891
		}
892

J
Jassi Brar 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
		local_irq_restore(flags);
		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);
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;

	switch (div_id) {
	case SAMSUNG_I2S_DIV_BCLK:
		if ((any_active(i2s) && div && (get_bfs(i2s) != div))
			|| (other && other->bfs && (other->bfs != div))) {
			dev_err(&i2s->pdev->dev,
				"%s:%d Other DAI busy\n", __func__, __LINE__);
			return -EAGAIN;
		}
		i2s->bfs = div;
		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;
931
	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
J
Jassi Brar 已提交
932 933 934 935 936 937

	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
		delay = FIC_RXCOUNT(reg);
	else if (is_secondary(i2s))
		delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
	else
938
		delay = (reg >> i2s_regs->ftx0cnt_off) & 0x7f;
J
Jassi Brar 已提交
939 940 941 942 943 944 945 946 947

	return delay;
}

#ifdef CONFIG_PM
static int i2s_suspend(struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);

948 949 950
	i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
	i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
	i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
J
Jassi Brar 已提交
951 952 953 954 955 956 957 958

	return 0;
}

static int i2s_resume(struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = to_info(dai);

959 960 961
	writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
	writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
	writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
J
Jassi Brar 已提交
962 963 964 965 966 967 968 969 970 971 972 973

	return 0;
}
#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);
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
974
	int ret;
J
Jassi Brar 已提交
975

976 977 978
	if (other && other->clk) { /* If this is probe on secondary */
		samsung_asoc_init_dma_data(dai, &other->sec_dai->dma_playback,
					   NULL);
J
Jassi Brar 已提交
979
		goto probe_exit;
980
	}
J
Jassi Brar 已提交
981 982 983 984 985 986 987 988 989 990 991

	i2s->addr = ioremap(i2s->base, 0x100);
	if (i2s->addr == NULL) {
		dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
		return -ENXIO;
	}

	i2s->clk = clk_get(&i2s->pdev->dev, "iis");
	if (IS_ERR(i2s->clk)) {
		dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
		iounmap(i2s->addr);
992 993 994 995 996 997 998
		return PTR_ERR(i2s->clk);
	}

	ret = clk_prepare_enable(i2s->clk);
	if (ret != 0) {
		dev_err(&i2s->pdev->dev, "failed to enable clock: %d\n", ret);
		return ret;
J
Jassi Brar 已提交
999 1000
	}

1001
	samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
1002

J
Jassi Brar 已提交
1003 1004 1005 1006 1007 1008 1009 1010
	if (other) {
		other->addr = i2s->addr;
		other->clk = i2s->clk;
	}

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

1011
	if (i2s->quirks & QUIRK_SUPPORTS_IDMA)
1012
		idma_reg_addr_init(i2s->addr,
1013 1014
					i2s->sec_dai->idma_playback.dma_addr);

J
Jassi Brar 已提交
1015 1016 1017 1018
probe_exit:
	/* Reset any constraint on RFS and BFS */
	i2s->rfs = 0;
	i2s->bfs = 0;
1019
	i2s->rclk_srcrate = 0;
J
Jassi Brar 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	i2s_txctrl(i2s, 0);
	i2s_rxctrl(i2s, 0);
	i2s_fifo(i2s, FIC_TXFLUSH);
	i2s_fifo(other, FIC_TXFLUSH);
	i2s_fifo(i2s, FIC_RXFLUSH);

	/* Gate CDCLK by default */
	if (!is_opened(other))
		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
				0, SND_SOC_CLOCK_IN);

	return 0;
}

static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
{
	struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;

	if (!other || !other->clk) {

		if (i2s->quirks & QUIRK_NEED_RSTCLR)
			writel(0, i2s->addr + I2SCON);

1044
		clk_disable_unprepare(i2s->clk);
J
Jassi Brar 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
		clk_put(i2s->clk);

		iounmap(i2s->addr);
	}

	i2s->clk = NULL;

	return 0;
}

1055
static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
J
Jassi Brar 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	.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,
};

1066 1067 1068 1069
static const struct snd_soc_component_driver samsung_i2s_component = {
	.name		= "samsung-i2s",
};

J
Jassi Brar 已提交
1070 1071 1072 1073 1074 1075
#define SAMSUNG_I2S_RATES	SNDRV_PCM_RATE_8000_96000

#define SAMSUNG_I2S_FMTS	(SNDRV_PCM_FMTBIT_S8 | \
					SNDRV_PCM_FMTBIT_S16_LE | \
					SNDRV_PCM_FMTBIT_S24_LE)

1076
static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
J
Jassi Brar 已提交
1077 1078
{
	struct i2s_dai *i2s;
1079
	int ret;
J
Jassi Brar 已提交
1080

1081
	i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
J
Jassi Brar 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	if (i2s == NULL)
		return NULL;

	i2s->pdev = pdev;
	i2s->pri_dai = NULL;
	i2s->sec_dai = NULL;
	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;
1094
	i2s->i2s_dai_drv.playback.channels_min = 1;
J
Jassi Brar 已提交
1095 1096 1097 1098 1099
	i2s->i2s_dai_drv.playback.channels_max = 2;
	i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
	i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;

	if (!sec) {
1100
		i2s->i2s_dai_drv.capture.channels_min = 1;
J
Jassi Brar 已提交
1101 1102 1103
		i2s->i2s_dai_drv.capture.channels_max = 2;
		i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
		i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1104
		dev_set_drvdata(&i2s->pdev->dev, i2s);
J
Jassi Brar 已提交
1105
	} else {	/* Create a new platform_device for Secondary */
1106
		i2s->pdev = platform_device_alloc("samsung-i2s-sec", -1);
1107
		if (!i2s->pdev)
J
Jassi Brar 已提交
1108 1109
			return NULL;

1110 1111
		i2s->pdev->dev.parent = &pdev->dev;

1112 1113 1114 1115 1116
		platform_set_drvdata(i2s->pdev, i2s);
		ret = platform_device_add(i2s->pdev);
		if (ret < 0)
			return NULL;
	}
J
Jassi Brar 已提交
1117 1118 1119 1120

	return i2s;
}

1121 1122
static const struct of_device_id exynos_i2s_match[];

1123 1124
static inline const struct samsung_i2s_dai_data *samsung_i2s_get_driver_data(
						struct platform_device *pdev)
1125
{
1126
	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
1127 1128
		const struct of_device_id *match;
		match = of_match_node(exynos_i2s_match, pdev->dev.of_node);
1129 1130
		return match ? match->data : NULL;
	} else {
1131 1132
		return (struct samsung_i2s_dai_data *)
				platform_get_device_id(pdev)->driver_data;
1133
	}
1134 1135
}

1136
#ifdef CONFIG_PM
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
static int i2s_runtime_suspend(struct device *dev)
{
	struct i2s_dai *i2s = dev_get_drvdata(dev);

	clk_disable_unprepare(i2s->clk);

	return 0;
}

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

	clk_prepare_enable(i2s->clk);

	return 0;
}
1154
#endif /* CONFIG_PM */
1155

1156
static int samsung_i2s_probe(struct platform_device *pdev)
J
Jassi Brar 已提交
1157 1158
{
	struct i2s_dai *pri_dai, *sec_dai = NULL;
1159 1160
	struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
	struct samsung_i2s *i2s_cfg = NULL;
J
Jassi Brar 已提交
1161
	struct resource *res;
1162 1163
	u32 regs_base, quirks = 0, idma_addr = 0;
	struct device_node *np = pdev->dev.of_node;
1164
	const struct samsung_i2s_dai_data *i2s_dai_data;
J
Jassi Brar 已提交
1165 1166 1167
	int ret = 0;

	/* Call during Seconday interface registration */
1168
	i2s_dai_data = samsung_i2s_get_driver_data(pdev);
1169

1170
	if (i2s_dai_data->dai_type == TYPE_SEC) {
J
Jassi Brar 已提交
1171
		sec_dai = dev_get_drvdata(&pdev->dev);
1172 1173 1174 1175
		if (!sec_dai) {
			dev_err(&pdev->dev, "Unable to get drvdata\n");
			return -EFAULT;
		}
1176 1177 1178
		devm_snd_soc_register_component(&sec_dai->pdev->dev,
						&samsung_i2s_component,
						&sec_dai->i2s_dai_drv, 1);
1179
		samsung_asoc_dma_platform_register(&pdev->dev);
J
Jassi Brar 已提交
1180 1181 1182
		return 0;
	}

1183 1184 1185 1186
	pri_dai = i2s_alloc_dai(pdev, false);
	if (!pri_dai) {
		dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
		return -ENOMEM;
J
Jassi Brar 已提交
1187 1188
	}

1189 1190 1191 1192 1193 1194 1195 1196
	if (!np) {
		res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
		if (!res) {
			dev_err(&pdev->dev,
				"Unable to get I2S-TX dma resource\n");
			return -ENXIO;
		}
		pri_dai->dma_playback.channel = res->start;
J
Jassi Brar 已提交
1197

1198 1199 1200 1201 1202 1203 1204
		res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
		if (!res) {
			dev_err(&pdev->dev,
				"Unable to get I2S-RX dma resource\n");
			return -ENXIO;
		}
		pri_dai->dma_capture.channel = res->start;
J
Jassi Brar 已提交
1205

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
		if (i2s_pdata == NULL) {
			dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
			return -EINVAL;
		}

		if (&i2s_pdata->type)
			i2s_cfg = &i2s_pdata->type.i2s;

		if (i2s_cfg) {
			quirks = i2s_cfg->quirks;
			idma_addr = i2s_cfg->idma_addr;
		}
	} else {
1219
		quirks = i2s_dai_data->quirks;
1220 1221
		if (of_property_read_u32(np, "samsung,idma-addr",
					 &idma_addr)) {
1222 1223
			if (quirks & QUIRK_SUPPORTS_IDMA) {
				dev_info(&pdev->dev, "idma address is not"\
1224 1225 1226 1227
						"specified");
			}
		}
	}
J
Jassi Brar 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
		return -ENXIO;
	}

	if (!request_mem_region(res->start, resource_size(res),
							"samsung-i2s")) {
		dev_err(&pdev->dev, "Unable to request SFR region\n");
		return -EBUSY;
	}
	regs_base = res->start;

	pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
	pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
1244 1245
	pri_dai->dma_playback.ch_name = "tx";
	pri_dai->dma_capture.ch_name = "rx";
J
Jassi Brar 已提交
1246 1247 1248 1249
	pri_dai->dma_playback.dma_size = 4;
	pri_dai->dma_capture.dma_size = 4;
	pri_dai->base = regs_base;
	pri_dai->quirks = quirks;
1250
	pri_dai->variant_regs = i2s_dai_data->i2s_variant_regs;
J
Jassi Brar 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259

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

	if (quirks & QUIRK_SEC_DAI) {
		sec_dai = i2s_alloc_dai(pdev, true);
		if (!sec_dai) {
			dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
			ret = -ENOMEM;
1260
			goto err;
J
Jassi Brar 已提交
1261
		}
1262 1263

		sec_dai->variant_regs = pri_dai->variant_regs;
J
Jassi Brar 已提交
1264
		sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
1265 1266 1267 1268 1269 1270 1271 1272
		sec_dai->dma_playback.ch_name = "tx-sec";

		if (!np) {
			res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
			if (res)
				sec_dai->dma_playback.channel = res->start;
		}

J
Jassi Brar 已提交
1273 1274 1275
		sec_dai->dma_playback.dma_size = 4;
		sec_dai->base = regs_base;
		sec_dai->quirks = quirks;
1276
		sec_dai->idma_playback.dma_addr = idma_addr;
J
Jassi Brar 已提交
1277 1278 1279 1280
		sec_dai->pri_dai = pri_dai;
		pri_dai->sec_dai = sec_dai;
	}

1281 1282 1283 1284
	if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
		dev_err(&pdev->dev, "Unable to configure gpio\n");
		ret = -EINVAL;
		goto err;
J
Jassi Brar 已提交
1285 1286
	}

1287 1288 1289
	devm_snd_soc_register_component(&pri_dai->pdev->dev,
					&samsung_i2s_component,
					&pri_dai->i2s_dai_drv, 1);
J
Jassi Brar 已提交
1290

1291 1292
	pm_runtime_enable(&pdev->dev);

1293
	samsung_asoc_dma_platform_register(&pdev->dev);
1294

J
Jassi Brar 已提交
1295
	return 0;
1296
err:
1297 1298
	if (res)
		release_mem_region(regs_base, resource_size(res));
J
Jassi Brar 已提交
1299 1300 1301 1302

	return ret;
}

1303
static int samsung_i2s_remove(struct platform_device *pdev)
J
Jassi Brar 已提交
1304 1305
{
	struct i2s_dai *i2s, *other;
1306
	struct resource *res;
J
Jassi Brar 已提交
1307 1308 1309 1310 1311 1312 1313 1314

	i2s = dev_get_drvdata(&pdev->dev);
	other = i2s->pri_dai ? : i2s->sec_dai;

	if (other) {
		other->pri_dai = NULL;
		other->sec_dai = NULL;
	} else {
1315
		pm_runtime_disable(&pdev->dev);
J
Jassi Brar 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
		if (res)
			release_mem_region(res->start, resource_size(res));
	}

	i2s->pri_dai = NULL;
	i2s->sec_dai = NULL;

	return 0;
}

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
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,
};

1383 1384 1385
static const struct samsung_i2s_dai_data i2sv3_dai_type = {
	.dai_type = TYPE_PRI,
	.quirks = QUIRK_NO_MUXPSR,
1386
	.i2s_variant_regs = &i2sv3_regs,
1387 1388 1389 1390
};

static const struct samsung_i2s_dai_data i2sv5_dai_type = {
	.dai_type = TYPE_PRI,
1391 1392
	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
			QUIRK_SUPPORTS_IDMA,
1393
	.i2s_variant_regs = &i2sv3_regs,
1394 1395
};

1396 1397 1398
static const struct samsung_i2s_dai_data i2sv6_dai_type = {
	.dai_type = TYPE_PRI,
	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1399
			QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	.i2s_variant_regs = &i2sv6_regs,
};

static const struct samsung_i2s_dai_data i2sv7_dai_type = {
	.dai_type = TYPE_PRI,
	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
			QUIRK_SUPPORTS_TDM,
	.i2s_variant_regs = &i2sv7_regs,
};

static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 = {
	.dai_type = TYPE_PRI,
	.quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
	.i2s_variant_regs = &i2sv5_i2s1_regs,
1414 1415
};

1416 1417 1418 1419 1420 1421 1422 1423
static const struct samsung_i2s_dai_data samsung_dai_type_pri = {
	.dai_type = TYPE_PRI,
};

static const struct samsung_i2s_dai_data samsung_dai_type_sec = {
	.dai_type = TYPE_SEC,
};

1424 1425 1426
static struct platform_device_id samsung_i2s_driver_ids[] = {
	{
		.name           = "samsung-i2s",
1427
		.driver_data	= (kernel_ulong_t)&i2sv3_dai_type,
1428 1429
	}, {
		.name           = "samsung-i2s-sec",
1430
		.driver_data    = (kernel_ulong_t)&samsung_dai_type_sec,
1431 1432 1433
	}, {
		.name		= "samsung-i2sv4",
		.driver_data	= (kernel_ulong_t)&i2sv5_dai_type,
1434 1435 1436
	},
	{},
};
1437
MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
1438

1439 1440
#ifdef CONFIG_OF
static const struct of_device_id exynos_i2s_match[] = {
1441 1442 1443 1444 1445 1446
	{
		.compatible = "samsung,s3c6410-i2s",
		.data = &i2sv3_dai_type,
	}, {
		.compatible = "samsung,s5pv210-i2s",
		.data = &i2sv5_dai_type,
1447 1448 1449
	}, {
		.compatible = "samsung,exynos5420-i2s",
		.data = &i2sv6_dai_type,
1450 1451 1452 1453 1454 1455
	}, {
		.compatible = "samsung,exynos7-i2s",
		.data = &i2sv7_dai_type,
	}, {
		.compatible = "samsung,exynos7-i2s1",
		.data = &i2sv5_dai_type_i2s1,
1456 1457 1458 1459 1460 1461
	},
	{},
};
MODULE_DEVICE_TABLE(of, exynos_i2s_match);
#endif

1462 1463 1464 1465 1466
static const struct dev_pm_ops samsung_i2s_pm = {
	SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
				i2s_runtime_resume, NULL)
};

J
Jassi Brar 已提交
1467 1468
static struct platform_driver samsung_i2s_driver = {
	.probe  = samsung_i2s_probe,
1469
	.remove = samsung_i2s_remove,
1470
	.id_table = samsung_i2s_driver_ids,
J
Jassi Brar 已提交
1471 1472
	.driver = {
		.name = "samsung-i2s",
1473
		.of_match_table = of_match_ptr(exynos_i2s_match),
1474
		.pm = &samsung_i2s_pm,
J
Jassi Brar 已提交
1475 1476 1477
	},
};

1478
module_platform_driver(samsung_i2s_driver);
J
Jassi Brar 已提交
1479 1480

/* Module information */
1481
MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
J
Jassi Brar 已提交
1482 1483 1484
MODULE_DESCRIPTION("Samsung I2S Interface");
MODULE_ALIAS("platform:samsung-i2s");
MODULE_LICENSE("GPL");