pxa-ssp.c 19.2 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * pxa-ssp.c  --  ALSA Soc Audio Layer
 *
 * Copyright 2005,2008 Wolfson Microelectronics PLC.
 * Author: Liam Girdwood
 *         Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 * TODO:
 *  o Test network mode for > 16bit sample size
 */

#include <linux/init.h>
#include <linux/module.h>
19
#include <linux/slab.h>
M
Mark Brown 已提交
20 21 22 23
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/io.h>

24 25
#include <asm/irq.h>

M
Mark Brown 已提交
26 27 28 29 30 31 32 33
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/initval.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/pxa2xx-lib.h>

#include <mach/hardware.h>
34
#include <mach/dma.h>
M
Mark Brown 已提交
35
#include <mach/audio.h>
36
#include <plat/ssp.h>
M
Mark Brown 已提交
37 38 39 40 41 42 43 44

#include "pxa2xx-pcm.h"
#include "pxa-ssp.h"

/*
 * SSP audio private data
 */
struct ssp_priv {
45
	struct ssp_device *ssp;
M
Mark Brown 已提交
46 47 48
	unsigned int sysclk;
	int dai_fmt;
#ifdef CONFIG_PM
49 50 51 52
	uint32_t	cr0;
	uint32_t	cr1;
	uint32_t	to;
	uint32_t	psp;
M
Mark Brown 已提交
53 54 55 56 57 58
#endif
};

static void dump_registers(struct ssp_device *ssp)
{
	dev_dbg(&ssp->pdev->dev, "SSCR0 0x%08x SSCR1 0x%08x SSTO 0x%08x\n",
H
Haojian Zhuang 已提交
59 60
		 pxa_ssp_read_reg(ssp, SSCR0), pxa_ssp_read_reg(ssp, SSCR1),
		 pxa_ssp_read_reg(ssp, SSTO));
M
Mark Brown 已提交
61 62

	dev_dbg(&ssp->pdev->dev, "SSPSP 0x%08x SSSR 0x%08x SSACD 0x%08x\n",
H
Haojian Zhuang 已提交
63 64
		 pxa_ssp_read_reg(ssp, SSPSP), pxa_ssp_read_reg(ssp, SSSR),
		 pxa_ssp_read_reg(ssp, SSACD));
M
Mark Brown 已提交
65 66
}

H
Haojian Zhuang 已提交
67
static void pxa_ssp_enable(struct ssp_device *ssp)
68 69 70 71 72 73 74
{
	uint32_t sscr0;

	sscr0 = __raw_readl(ssp->mmio_base + SSCR0) | SSCR0_SSE;
	__raw_writel(sscr0, ssp->mmio_base + SSCR0);
}

H
Haojian Zhuang 已提交
75
static void pxa_ssp_disable(struct ssp_device *ssp)
76 77 78 79 80 81 82
{
	uint32_t sscr0;

	sscr0 = __raw_readl(ssp->mmio_base + SSCR0) & ~SSCR0_SSE;
	__raw_writel(sscr0, ssp->mmio_base + SSCR0);
}

83 84 85
struct pxa2xx_pcm_dma_data {
	struct pxa2xx_pcm_dma_params params;
	char name[20];
M
Mark Brown 已提交
86 87
};

88
static struct pxa2xx_pcm_dma_params *
H
Haojian Zhuang 已提交
89
pxa_ssp_get_dma_params(struct ssp_device *ssp, int width4, int out)
90 91 92 93 94 95 96 97
{
	struct pxa2xx_pcm_dma_data *dma;

	dma = kzalloc(sizeof(struct pxa2xx_pcm_dma_data), GFP_KERNEL);
	if (dma == NULL)
		return NULL;

	snprintf(dma->name, 20, "SSP%d PCM %s %s", ssp->port_id,
98
			width4 ? "32-bit" : "16-bit", out ? "out" : "in");
99 100 101 102 103

	dma->params.name = dma->name;
	dma->params.drcmr = &DRCMR(out ? ssp->drcmr_tx : ssp->drcmr_rx);
	dma->params.dcmd = (out ? (DCMD_INCSRCADDR | DCMD_FLOWTRG) :
				  (DCMD_INCTRGADDR | DCMD_FLOWSRC)) |
104
			(width4 ? DCMD_WIDTH4 : DCMD_WIDTH2) | DCMD_BURST16;
105 106 107 108 109
	dma->params.dev_addr = ssp->phys_base + SSDR;

	return &dma->params;
}

110
static int pxa_ssp_startup(struct snd_pcm_substream *substream,
111
			   struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
112
{
113
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
114
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
115 116 117
	int ret = 0;

	if (!cpu_dai->active) {
118
		clk_enable(ssp->clk);
H
Haojian Zhuang 已提交
119
		pxa_ssp_disable(ssp);
M
Mark Brown 已提交
120
	}
121

122 123 124
	kfree(snd_soc_dai_get_dma_data(cpu_dai, substream));
	snd_soc_dai_set_dma_data(cpu_dai, substream, NULL);

M
Mark Brown 已提交
125 126 127
	return ret;
}

128
static void pxa_ssp_shutdown(struct snd_pcm_substream *substream,
129
			     struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
130
{
131
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
132
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
133 134

	if (!cpu_dai->active) {
H
Haojian Zhuang 已提交
135
		pxa_ssp_disable(ssp);
136
		clk_disable(ssp->clk);
M
Mark Brown 已提交
137
	}
138

139 140
	kfree(snd_soc_dai_get_dma_data(cpu_dai, substream));
	snd_soc_dai_set_dma_data(cpu_dai, substream, NULL);
M
Mark Brown 已提交
141 142 143 144
}

#ifdef CONFIG_PM

145
static int pxa_ssp_suspend(struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
146
{
147
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
148
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
149 150

	if (!cpu_dai->active)
151
		clk_enable(ssp->clk);
M
Mark Brown 已提交
152

153 154 155 156 157
	priv->cr0 = __raw_readl(ssp->mmio_base + SSCR0);
	priv->cr1 = __raw_readl(ssp->mmio_base + SSCR1);
	priv->to  = __raw_readl(ssp->mmio_base + SSTO);
	priv->psp = __raw_readl(ssp->mmio_base + SSPSP);

H
Haojian Zhuang 已提交
158
	pxa_ssp_disable(ssp);
159
	clk_disable(ssp->clk);
M
Mark Brown 已提交
160 161 162
	return 0;
}

163
static int pxa_ssp_resume(struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
164
{
165
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
166 167
	struct ssp_device *ssp = priv->ssp;
	uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE;
M
Mark Brown 已提交
168

169 170 171 172 173 174 175
	clk_enable(ssp->clk);

	__raw_writel(sssr, ssp->mmio_base + SSSR);
	__raw_writel(priv->cr0 & ~SSCR0_SSE, ssp->mmio_base + SSCR0);
	__raw_writel(priv->cr1, ssp->mmio_base + SSCR1);
	__raw_writel(priv->to,  ssp->mmio_base + SSTO);
	__raw_writel(priv->psp, ssp->mmio_base + SSPSP);
D
Daniel Mack 已提交
176 177

	if (cpu_dai->active)
H
Haojian Zhuang 已提交
178
		pxa_ssp_enable(ssp);
D
Daniel Mack 已提交
179
	else
180
		clk_disable(ssp->clk);
M
Mark Brown 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193

	return 0;
}

#else
#define pxa_ssp_suspend	NULL
#define pxa_ssp_resume	NULL
#endif

/**
 * ssp_set_clkdiv - set SSP clock divider
 * @div: serial clock rate divider
 */
H
Haojian Zhuang 已提交
194
static void pxa_ssp_set_scr(struct ssp_device *ssp, u32 div)
M
Mark Brown 已提交
195
{
H
Haojian Zhuang 已提交
196
	u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
197 198 199 200 201 202 203 204

	if (cpu_is_pxa25x() && ssp->type == PXA25x_SSP) {
		sscr0 &= ~0x0000ff00;
		sscr0 |= ((div - 2)/2) << 8; /* 2..512 */
	} else {
		sscr0 &= ~0x000fff00;
		sscr0 |= (div - 1) << 8;     /* 1..4096 */
	}
H
Haojian Zhuang 已提交
205
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
206 207 208
}

/**
H
Haojian Zhuang 已提交
209
 * pxa_ssp_get_clkdiv - get SSP clock divider
210
 */
H
Haojian Zhuang 已提交
211
static u32 pxa_ssp_get_scr(struct ssp_device *ssp)
212
{
H
Haojian Zhuang 已提交
213
	u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
214
	u32 div;
M
Mark Brown 已提交
215

216 217 218 219 220
	if (cpu_is_pxa25x() && ssp->type == PXA25x_SSP)
		div = ((sscr0 >> 8) & 0xff) * 2 + 2;
	else
		div = ((sscr0 >> 8) & 0xfff) + 1;
	return div;
M
Mark Brown 已提交
221 222 223 224 225 226 227 228
}

/*
 * Set the SSP ports SYSCLK.
 */
static int pxa_ssp_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
	int clk_id, unsigned int freq, int dir)
{
229
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
230
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
231 232
	int val;

H
Haojian Zhuang 已提交
233
	u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0) &
234
		~(SSCR0_ECS |  SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
M
Mark Brown 已提交
235 236

	dev_dbg(&ssp->pdev->dev,
237
		"pxa_ssp_set_dai_sysclk id: %d, clk_id %d, freq %u\n",
M
Mark Brown 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
		cpu_dai->id, clk_id, freq);

	switch (clk_id) {
	case PXA_SSP_CLK_NET_PLL:
		sscr0 |= SSCR0_MOD;
		break;
	case PXA_SSP_CLK_PLL:
		/* Internal PLL is fixed */
		if (cpu_is_pxa25x())
			priv->sysclk = 1843200;
		else
			priv->sysclk = 13000000;
		break;
	case PXA_SSP_CLK_EXT:
		priv->sysclk = freq;
		sscr0 |= SSCR0_ECS;
		break;
	case PXA_SSP_CLK_NET:
		priv->sysclk = freq;
		sscr0 |= SSCR0_NCS | SSCR0_MOD;
		break;
	case PXA_SSP_CLK_AUDIO:
		priv->sysclk = 0;
H
Haojian Zhuang 已提交
261
		pxa_ssp_set_scr(ssp, 1);
262
		sscr0 |= SSCR0_ACS;
M
Mark Brown 已提交
263 264 265 266 267 268 269 270
		break;
	default:
		return -ENODEV;
	}

	/* The SSP clock must be disabled when changing SSP clock mode
	 * on PXA2xx.  On PXA3xx it must be enabled when doing so. */
	if (!cpu_is_pxa3xx())
271
		clk_disable(ssp->clk);
H
Haojian Zhuang 已提交
272 273
	val = pxa_ssp_read_reg(ssp, SSCR0) | sscr0;
	pxa_ssp_write_reg(ssp, SSCR0, val);
M
Mark Brown 已提交
274
	if (!cpu_is_pxa3xx())
275
		clk_enable(ssp->clk);
M
Mark Brown 已提交
276 277 278 279 280 281 282 283 284 285

	return 0;
}

/*
 * Set the SSP clock dividers.
 */
static int pxa_ssp_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
	int div_id, int div)
{
286
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
287
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
288 289 290 291
	int val;

	switch (div_id) {
	case PXA_SSP_AUDIO_DIV_ACDS:
H
Haojian Zhuang 已提交
292 293
		val = (pxa_ssp_read_reg(ssp, SSACD) & ~0x7) | SSACD_ACDS(div);
		pxa_ssp_write_reg(ssp, SSACD, val);
M
Mark Brown 已提交
294 295
		break;
	case PXA_SSP_AUDIO_DIV_SCDB:
H
Haojian Zhuang 已提交
296
		val = pxa_ssp_read_reg(ssp, SSACD);
M
Mark Brown 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
		val &= ~SSACD_SCDB;
#if defined(CONFIG_PXA3xx)
		if (cpu_is_pxa3xx())
			val &= ~SSACD_SCDX8;
#endif
		switch (div) {
		case PXA_SSP_CLK_SCDB_1:
			val |= SSACD_SCDB;
			break;
		case PXA_SSP_CLK_SCDB_4:
			break;
#if defined(CONFIG_PXA3xx)
		case PXA_SSP_CLK_SCDB_8:
			if (cpu_is_pxa3xx())
				val |= SSACD_SCDX8;
			else
				return -EINVAL;
			break;
#endif
		default:
			return -EINVAL;
		}
H
Haojian Zhuang 已提交
319
		pxa_ssp_write_reg(ssp, SSACD, val);
M
Mark Brown 已提交
320 321
		break;
	case PXA_SSP_DIV_SCR:
H
Haojian Zhuang 已提交
322
		pxa_ssp_set_scr(ssp, div);
M
Mark Brown 已提交
323 324 325 326 327 328 329 330 331 332 333
		break;
	default:
		return -ENODEV;
	}

	return 0;
}

/*
 * Configure the PLL frequency pxa27x and (afaik - pxa320 only)
 */
334 335
static int pxa_ssp_set_dai_pll(struct snd_soc_dai *cpu_dai, int pll_id,
	int source, unsigned int freq_in, unsigned int freq_out)
M
Mark Brown 已提交
336
{
337
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
338
	struct ssp_device *ssp = priv->ssp;
H
Haojian Zhuang 已提交
339
	u32 ssacd = pxa_ssp_read_reg(ssp, SSACD) & ~0x70;
M
Mark Brown 已提交
340 341 342

#if defined(CONFIG_PXA3xx)
	if (cpu_is_pxa3xx())
H
Haojian Zhuang 已提交
343
		pxa_ssp_write_reg(ssp, SSACDD, 0);
M
Mark Brown 已提交
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 370 371 372 373 374 375 376 377 378 379
#endif

	switch (freq_out) {
	case 5622000:
		break;
	case 11345000:
		ssacd |= (0x1 << 4);
		break;
	case 12235000:
		ssacd |= (0x2 << 4);
		break;
	case 14857000:
		ssacd |= (0x3 << 4);
		break;
	case 32842000:
		ssacd |= (0x4 << 4);
		break;
	case 48000000:
		ssacd |= (0x5 << 4);
		break;
	case 0:
		/* Disable */
		break;

	default:
#ifdef CONFIG_PXA3xx
		/* PXA3xx has a clock ditherer which can be used to generate
		 * a wider range of frequencies - calculate a value for it.
		 */
		if (cpu_is_pxa3xx()) {
			u32 val;
			u64 tmp = 19968;
			tmp *= 1000000;
			do_div(tmp, freq_out);
			val = tmp;

380
			val = (val << 16) | 64;
H
Haojian Zhuang 已提交
381
			pxa_ssp_write_reg(ssp, SSACDD, val);
M
Mark Brown 已提交
382 383 384 385

			ssacd |= (0x6 << 4);

			dev_dbg(&ssp->pdev->dev,
386
				"Using SSACDD %x to supply %uHz\n",
M
Mark Brown 已提交
387 388 389 390 391 392 393 394
				val, freq_out);
			break;
		}
#endif

		return -EINVAL;
	}

H
Haojian Zhuang 已提交
395
	pxa_ssp_write_reg(ssp, SSACD, ssacd);
M
Mark Brown 已提交
396 397 398 399 400 401 402 403

	return 0;
}

/*
 * Set the active slots in TDM/Network mode
 */
static int pxa_ssp_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
404
	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
M
Mark Brown 已提交
405
{
406
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
407
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
408 409
	u32 sscr0;

H
Haojian Zhuang 已提交
410
	sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
411
	sscr0 &= ~(SSCR0_MOD | SSCR0_SlotsPerFrm(8) | SSCR0_EDSS | SSCR0_DSS);
M
Mark Brown 已提交
412

413 414 415 416 417 418 419 420 421 422 423 424 425 426
	/* set slot width */
	if (slot_width > 16)
		sscr0 |= SSCR0_EDSS | SSCR0_DataSize(slot_width - 16);
	else
		sscr0 |= SSCR0_DataSize(slot_width);

	if (slots > 1) {
		/* enable network mode */
		sscr0 |= SSCR0_MOD;

		/* set number of active slots */
		sscr0 |= SSCR0_SlotsPerFrm(slots);

		/* set active slot mask */
H
Haojian Zhuang 已提交
427 428
		pxa_ssp_write_reg(ssp, SSTSA, tx_mask);
		pxa_ssp_write_reg(ssp, SSRSA, rx_mask);
429
	}
H
Haojian Zhuang 已提交
430
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
M
Mark Brown 已提交
431 432 433 434 435 436 437 438 439 440

	return 0;
}

/*
 * Tristate the SSP DAI lines
 */
static int pxa_ssp_set_dai_tristate(struct snd_soc_dai *cpu_dai,
	int tristate)
{
441
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
442
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
443 444
	u32 sscr1;

H
Haojian Zhuang 已提交
445
	sscr1 = pxa_ssp_read_reg(ssp, SSCR1);
M
Mark Brown 已提交
446 447 448 449
	if (tristate)
		sscr1 &= ~SSCR1_TTE;
	else
		sscr1 |= SSCR1_TTE;
H
Haojian Zhuang 已提交
450
	pxa_ssp_write_reg(ssp, SSCR1, sscr1);
M
Mark Brown 已提交
451 452 453 454 455 456 457 458 459 460 461 462

	return 0;
}

/*
 * Set up the SSP DAI format.
 * The SSP Port must be inactive before calling this function as the
 * physical interface format is changed.
 */
static int pxa_ssp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
		unsigned int fmt)
{
463
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
464
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
465 466 467 468
	u32 sscr0;
	u32 sscr1;
	u32 sspsp;

469 470 471 472 473
	/* check if we need to change anything at all */
	if (priv->dai_fmt == fmt)
		return 0;

	/* we can only change the settings if the port is not in use */
H
Haojian Zhuang 已提交
474
	if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE) {
475 476 477 478 479
		dev_err(&ssp->pdev->dev,
			"can't change hardware dai format: stream is in use");
		return -EINVAL;
	}

M
Mark Brown 已提交
480
	/* reset port settings */
H
Haojian Zhuang 已提交
481
	sscr0 = pxa_ssp_read_reg(ssp, SSCR0) &
482
		(SSCR0_ECS |  SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
M
Mark Brown 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	sscr1 = SSCR1_RxTresh(8) | SSCR1_TxTresh(7);
	sspsp = 0;

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
		sscr1 |= SSCR1_SCLKDIR | SSCR1_SFRMDIR;
		break;
	case SND_SOC_DAIFMT_CBM_CFS:
		sscr1 |= SSCR1_SCLKDIR;
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		break;
	default:
		return -EINVAL;
	}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF:
		sspsp |= SSPSP_SFRMP;
		break;
	case SND_SOC_DAIFMT_NB_IF:
		break;
	case SND_SOC_DAIFMT_IB_IF:
		sspsp |= SSPSP_SCMODE(2);
		break;
	case SND_SOC_DAIFMT_IB_NF:
		sspsp |= SSPSP_SCMODE(2) | SSPSP_SFRMP;
		break;
	default:
		return -EINVAL;
	}
M
Mark Brown 已提交
514 515 516

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
517
		sscr0 |= SSCR0_PSP;
M
Mark Brown 已提交
518
		sscr1 |= SSCR1_RWOT | SSCR1_TRAIL;
519
		/* See hw_params() */
M
Mark Brown 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532
		break;

	case SND_SOC_DAIFMT_DSP_A:
		sspsp |= SSPSP_FSRT;
	case SND_SOC_DAIFMT_DSP_B:
		sscr0 |= SSCR0_MOD | SSCR0_PSP;
		sscr1 |= SSCR1_TRAIL | SSCR1_RWOT;
		break;

	default:
		return -EINVAL;
	}

H
Haojian Zhuang 已提交
533 534 535
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
	pxa_ssp_write_reg(ssp, SSCR1, sscr1);
	pxa_ssp_write_reg(ssp, SSPSP, sspsp);
M
Mark Brown 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

	dump_registers(ssp);

	/* Since we are configuring the timings for the format by hand
	 * we have to defer some things until hw_params() where we
	 * know parameters like the sample size.
	 */
	priv->dai_fmt = fmt;

	return 0;
}

/*
 * Set the SSP audio DMA parameters and sample size.
 * Can be called multiple times by oss emulation.
 */
static int pxa_ssp_hw_params(struct snd_pcm_substream *substream,
553
				struct snd_pcm_hw_params *params,
554
				struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
555
{
556
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
557
	struct ssp_device *ssp = priv->ssp;
558
	int chn = params_channels(params);
M
Mark Brown 已提交
559 560 561
	u32 sscr0;
	u32 sspsp;
	int width = snd_pcm_format_physical_width(params_format(params));
H
Haojian Zhuang 已提交
562
	int ttsa = pxa_ssp_read_reg(ssp, SSTSA) & 0xf;
563 564
	struct pxa2xx_pcm_dma_params *dma_data;

565
	dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
M
Mark Brown 已提交
566

567
	/* generate correct DMA params */
568
	kfree(dma_data);
569

570 571 572 573
	/* Network mode with one active slot (ttsa == 1) can be used
	 * to force 16-bit frame width on the wire (for S16_LE), even
	 * with two channels. Use 16-bit DMA transfers for this case.
	 */
H
Haojian Zhuang 已提交
574
	dma_data = pxa_ssp_get_dma_params(ssp,
575 576
			((chn == 2) && (ttsa != 1)) || (width == 32),
			substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
M
Mark Brown 已提交
577

578
	snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
579

M
Mark Brown 已提交
580
	/* we can only change the settings if the port is not in use */
H
Haojian Zhuang 已提交
581
	if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE)
M
Mark Brown 已提交
582 583 584
		return 0;

	/* clear selected SSP bits */
H
Haojian Zhuang 已提交
585 586
	sscr0 = pxa_ssp_read_reg(ssp, SSCR0) & ~(SSCR0_DSS | SSCR0_EDSS);
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
M
Mark Brown 已提交
587 588

	/* bit size */
H
Haojian Zhuang 已提交
589
	sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
M
Mark Brown 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
#ifdef CONFIG_PXA3xx
		if (cpu_is_pxa3xx())
			sscr0 |= SSCR0_FPCKE;
#endif
		sscr0 |= SSCR0_DataSize(16);
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(8));
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(16));
		break;
	}
H
Haojian Zhuang 已提交
605
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
M
Mark Brown 已提交
606 607 608

	switch (priv->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
H
Haojian Zhuang 已提交
609
	       sspsp = pxa_ssp_read_reg(ssp, SSPSP);
610

H
Haojian Zhuang 已提交
611
		if ((pxa_ssp_get_scr(ssp) == 4) && (width == 16)) {
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
			/* This is a special case where the bitclk is 64fs
			* and we're not dealing with 2*32 bits of audio
			* samples.
			*
			* The SSP values used for that are all found out by
			* trying and failing a lot; some of the registers
			* needed for that mode are only available on PXA3xx.
			*/

#ifdef CONFIG_PXA3xx
			if (!cpu_is_pxa3xx())
				return -EINVAL;

			sspsp |= SSPSP_SFRMWDTH(width * 2);
			sspsp |= SSPSP_SFRMDLY(width * 4);
			sspsp |= SSPSP_EDMYSTOP(3);
			sspsp |= SSPSP_DMYSTOP(3);
			sspsp |= SSPSP_DMYSTRT(1);
#else
			return -EINVAL;
#endif
633 634 635 636 637 638 639 640 641 642 643
		} else {
			/* The frame width is the width the LRCLK is
			 * asserted for; the delay is expressed in
			 * half cycle units.  We need the extra cycle
			 * because the data starts clocking out one BCLK
			 * after LRCLK changes polarity.
			 */
			sspsp |= SSPSP_SFRMWDTH(width + 1);
			sspsp |= SSPSP_SFRMDLY((width + 1) * 2);
			sspsp |= SSPSP_DMYSTRT(1);
		}
644

H
Haojian Zhuang 已提交
645
		pxa_ssp_write_reg(ssp, SSPSP, sspsp);
M
Mark Brown 已提交
646 647 648 649 650
		break;
	default:
		break;
	}

651
	/* When we use a network mode, we always require TDM slots
M
Mark Brown 已提交
652 653
	 * - complain loudly and fail if they've not been set up yet.
	 */
654
	if ((sscr0 & SSCR0_MOD) && !ttsa) {
M
Mark Brown 已提交
655 656 657 658 659 660 661 662 663
		dev_err(&ssp->pdev->dev, "No TDM timeslot configured\n");
		return -EINVAL;
	}

	dump_registers(ssp);

	return 0;
}

664
static int pxa_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
665
			   struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
666 667
{
	int ret = 0;
668
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
669
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
670 671 672 673
	int val;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_RESUME:
H
Haojian Zhuang 已提交
674
		pxa_ssp_enable(ssp);
M
Mark Brown 已提交
675 676
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
H
Haojian Zhuang 已提交
677
		val = pxa_ssp_read_reg(ssp, SSCR1);
M
Mark Brown 已提交
678 679 680 681
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			val |= SSCR1_TSRE;
		else
			val |= SSCR1_RSRE;
H
Haojian Zhuang 已提交
682 683 684
		pxa_ssp_write_reg(ssp, SSCR1, val);
		val = pxa_ssp_read_reg(ssp, SSSR);
		pxa_ssp_write_reg(ssp, SSSR, val);
M
Mark Brown 已提交
685 686
		break;
	case SNDRV_PCM_TRIGGER_START:
H
Haojian Zhuang 已提交
687
		val = pxa_ssp_read_reg(ssp, SSCR1);
M
Mark Brown 已提交
688 689 690 691
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			val |= SSCR1_TSRE;
		else
			val |= SSCR1_RSRE;
H
Haojian Zhuang 已提交
692 693
		pxa_ssp_write_reg(ssp, SSCR1, val);
		pxa_ssp_enable(ssp);
M
Mark Brown 已提交
694 695
		break;
	case SNDRV_PCM_TRIGGER_STOP:
H
Haojian Zhuang 已提交
696
		val = pxa_ssp_read_reg(ssp, SSCR1);
M
Mark Brown 已提交
697 698 699 700
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			val &= ~SSCR1_TSRE;
		else
			val &= ~SSCR1_RSRE;
H
Haojian Zhuang 已提交
701
		pxa_ssp_write_reg(ssp, SSCR1, val);
M
Mark Brown 已提交
702 703
		break;
	case SNDRV_PCM_TRIGGER_SUSPEND:
H
Haojian Zhuang 已提交
704
		pxa_ssp_disable(ssp);
M
Mark Brown 已提交
705 706
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
H
Haojian Zhuang 已提交
707
		val = pxa_ssp_read_reg(ssp, SSCR1);
M
Mark Brown 已提交
708 709 710 711
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			val &= ~SSCR1_TSRE;
		else
			val &= ~SSCR1_RSRE;
H
Haojian Zhuang 已提交
712
		pxa_ssp_write_reg(ssp, SSCR1, val);
M
Mark Brown 已提交
713 714 715 716 717 718 719 720 721 722 723
		break;

	default:
		ret = -EINVAL;
	}

	dump_registers(ssp);

	return ret;
}

724
static int pxa_ssp_probe(struct snd_soc_dai *dai)
M
Mark Brown 已提交
725 726 727 728 729 730 731 732
{
	struct ssp_priv *priv;
	int ret;

	priv = kzalloc(sizeof(struct ssp_priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

H
Haojian Zhuang 已提交
733
	priv->ssp = pxa_ssp_request(dai->id + 1, "SoC audio");
734
	if (priv->ssp == NULL) {
M
Mark Brown 已提交
735 736 737 738
		ret = -ENODEV;
		goto err_priv;
	}

739
	priv->dai_fmt = (unsigned int) -1;
740
	snd_soc_dai_set_drvdata(dai, priv);
M
Mark Brown 已提交
741 742 743 744 745 746 747 748

	return 0;

err_priv:
	kfree(priv);
	return ret;
}

749
static int pxa_ssp_remove(struct snd_soc_dai *dai)
M
Mark Brown 已提交
750
{
751 752
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(dai);

H
Haojian Zhuang 已提交
753
	pxa_ssp_free(priv->ssp);
754
	return 0;
M
Mark Brown 已提交
755 756 757 758 759 760 761 762 763 764 765
}

#define PXA_SSP_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
			  SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |	\
			  SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |	\
			  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)

#define PXA_SSP_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
			    SNDRV_PCM_FMTBIT_S24_LE |	\
			    SNDRV_PCM_FMTBIT_S32_LE)

766 767 768 769 770 771 772 773 774 775 776 777 778
static struct snd_soc_dai_ops pxa_ssp_dai_ops = {
	.startup	= pxa_ssp_startup,
	.shutdown	= pxa_ssp_shutdown,
	.trigger	= pxa_ssp_trigger,
	.hw_params	= pxa_ssp_hw_params,
	.set_sysclk	= pxa_ssp_set_dai_sysclk,
	.set_clkdiv	= pxa_ssp_set_dai_clkdiv,
	.set_pll	= pxa_ssp_set_dai_pll,
	.set_fmt	= pxa_ssp_set_dai_fmt,
	.set_tdm_slot	= pxa_ssp_set_dai_tdm_slot,
	.set_tristate	= pxa_ssp_set_dai_tristate,
};

779
static struct snd_soc_dai_driver pxa_ssp_dai = {
M
Mark Brown 已提交
780 781 782 783 784 785
		.probe = pxa_ssp_probe,
		.remove = pxa_ssp_remove,
		.suspend = pxa_ssp_suspend,
		.resume = pxa_ssp_resume,
		.playback = {
			.channels_min = 1,
786
			.channels_max = 8,
M
Mark Brown 已提交
787 788 789 790 791
			.rates = PXA_SSP_RATES,
			.formats = PXA_SSP_FORMATS,
		},
		.capture = {
			 .channels_min = 1,
792
			 .channels_max = 8,
M
Mark Brown 已提交
793 794 795
			.rates = PXA_SSP_RATES,
			.formats = PXA_SSP_FORMATS,
		 },
796
		.ops = &pxa_ssp_dai_ops,
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
};

static __devinit int asoc_ssp_probe(struct platform_device *pdev)
{
	return snd_soc_register_dai(&pdev->dev, &pxa_ssp_dai);
}

static int __devexit asoc_ssp_remove(struct platform_device *pdev)
{
	snd_soc_unregister_dai(&pdev->dev);
	return 0;
}

static struct platform_driver asoc_ssp_driver = {
	.driver = {
			.name = "pxa-ssp-dai",
			.owner = THIS_MODULE,
M
Mark Brown 已提交
814
	},
815 816 817

	.probe = asoc_ssp_probe,
	.remove = __devexit_p(asoc_ssp_remove),
M
Mark Brown 已提交
818 819
};

820
static int __init pxa_ssp_init(void)
M
Mark Brown 已提交
821
{
822
	return platform_driver_register(&asoc_ssp_driver);
M
Mark Brown 已提交
823 824 825 826 827
}
module_init(pxa_ssp_init);

static void __exit pxa_ssp_exit(void)
{
828
	platform_driver_unregister(&asoc_ssp_driver);
M
Mark Brown 已提交
829 830 831
}
module_exit(pxa_ssp_exit);

M
Mark Brown 已提交
832 833 834 835
/* Module information */
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
MODULE_DESCRIPTION("PXA SSP/PCM SoC Interface");
MODULE_LICENSE("GPL");