pxa-ssp.c 19.7 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
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/io.h>
23
#include <linux/pxa2xx_ssp.h>
24
#include <linux/of.h>
25
#include <linux/dmaengine.h>
M
Mark Brown 已提交
26

27 28
#include <asm/irq.h>

M
Mark Brown 已提交
29 30 31 32 33 34
#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>
35
#include <sound/dmaengine_pcm.h>
M
Mark Brown 已提交
36

H
Haojian Zhuang 已提交
37
#include "../../arm/pxa2xx-pcm.h"
M
Mark Brown 已提交
38 39 40 41 42 43
#include "pxa-ssp.h"

/*
 * SSP audio private data
 */
struct ssp_priv {
44
	struct ssp_device *ssp;
M
Mark Brown 已提交
45 46 47
	unsigned int sysclk;
	int dai_fmt;
#ifdef CONFIG_PM
48 49 50 51
	uint32_t	cr0;
	uint32_t	cr1;
	uint32_t	to;
	uint32_t	psp;
M
Mark Brown 已提交
52 53 54 55 56 57
#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 已提交
58 59
		 pxa_ssp_read_reg(ssp, SSCR0), pxa_ssp_read_reg(ssp, SSCR1),
		 pxa_ssp_read_reg(ssp, SSTO));
M
Mark Brown 已提交
60 61

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

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

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

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

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

82
static void pxa_ssp_set_dma_params(struct ssp_device *ssp, int width4,
83
			int out, struct snd_dmaengine_dai_dma_data *dma)
84
{
85 86 87 88
	dma->addr_width = width4 ? DMA_SLAVE_BUSWIDTH_4_BYTES :
				   DMA_SLAVE_BUSWIDTH_2_BYTES;
	dma->maxburst = 16;
	dma->addr = ssp->phys_base + SSDR;
89 90
}

91
static int pxa_ssp_startup(struct snd_pcm_substream *substream,
92
			   struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
93
{
94
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
95
	struct ssp_device *ssp = priv->ssp;
96
	struct snd_dmaengine_dai_dma_data *dma;
M
Mark Brown 已提交
97 98 99
	int ret = 0;

	if (!cpu_dai->active) {
100
		clk_enable(ssp->clk);
H
Haojian Zhuang 已提交
101
		pxa_ssp_disable(ssp);
M
Mark Brown 已提交
102
	}
103

104
	dma = kzalloc(sizeof(struct snd_dmaengine_dai_dma_data), GFP_KERNEL);
105 106
	if (!dma)
		return -ENOMEM;
107 108 109 110

	dma->filter_data = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
				&ssp->drcmr_tx : &ssp->drcmr_rx;

111
	snd_soc_dai_set_dma_data(cpu_dai, substream, dma);
112

M
Mark Brown 已提交
113 114 115
	return ret;
}

116
static void pxa_ssp_shutdown(struct snd_pcm_substream *substream,
117
			     struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
118
{
119
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
120
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
121 122

	if (!cpu_dai->active) {
H
Haojian Zhuang 已提交
123
		pxa_ssp_disable(ssp);
124
		clk_disable(ssp->clk);
M
Mark Brown 已提交
125
	}
126

127 128
	kfree(snd_soc_dai_get_dma_data(cpu_dai, substream));
	snd_soc_dai_set_dma_data(cpu_dai, substream, NULL);
M
Mark Brown 已提交
129 130 131 132
}

#ifdef CONFIG_PM

133
static int pxa_ssp_suspend(struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
134
{
135
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
136
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
137 138

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

141 142 143 144 145
	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 已提交
146
	pxa_ssp_disable(ssp);
147
	clk_disable(ssp->clk);
M
Mark Brown 已提交
148 149 150
	return 0;
}

151
static int pxa_ssp_resume(struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
152
{
153
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
154 155
	struct ssp_device *ssp = priv->ssp;
	uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE;
M
Mark Brown 已提交
156

157 158 159 160 161 162 163
	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 已提交
164 165

	if (cpu_dai->active)
H
Haojian Zhuang 已提交
166
		pxa_ssp_enable(ssp);
D
Daniel Mack 已提交
167
	else
168
		clk_disable(ssp->clk);
M
Mark Brown 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181

	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 已提交
182
static void pxa_ssp_set_scr(struct ssp_device *ssp, u32 div)
M
Mark Brown 已提交
183
{
H
Haojian Zhuang 已提交
184
	u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
185

186
	if (ssp->type == PXA25x_SSP) {
187 188 189 190 191 192
		sscr0 &= ~0x0000ff00;
		sscr0 |= ((div - 2)/2) << 8; /* 2..512 */
	} else {
		sscr0 &= ~0x000fff00;
		sscr0 |= (div - 1) << 8;     /* 1..4096 */
	}
H
Haojian Zhuang 已提交
193
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
194 195 196
}

/**
H
Haojian Zhuang 已提交
197
 * pxa_ssp_get_clkdiv - get SSP clock divider
198
 */
H
Haojian Zhuang 已提交
199
static u32 pxa_ssp_get_scr(struct ssp_device *ssp)
200
{
H
Haojian Zhuang 已提交
201
	u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
202
	u32 div;
M
Mark Brown 已提交
203

204
	if (ssp->type == PXA25x_SSP)
205 206 207 208
		div = ((sscr0 >> 8) & 0xff) * 2 + 2;
	else
		div = ((sscr0 >> 8) & 0xfff) + 1;
	return div;
M
Mark Brown 已提交
209 210 211 212 213 214 215 216
}

/*
 * 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)
{
217
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
218
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
219 220
	int val;

H
Haojian Zhuang 已提交
221
	u32 sscr0 = pxa_ssp_read_reg(ssp, SSCR0) &
222
		~(SSCR0_ECS |  SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
M
Mark Brown 已提交
223 224

	dev_dbg(&ssp->pdev->dev,
225
		"pxa_ssp_set_dai_sysclk id: %d, clk_id %d, freq %u\n",
M
Mark Brown 已提交
226 227 228 229 230 231 232 233
		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 */
234
		if (ssp->type == PXA25x_SSP)
M
Mark Brown 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248
			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 已提交
249
		pxa_ssp_set_scr(ssp, 1);
250
		sscr0 |= SSCR0_ACS;
M
Mark Brown 已提交
251 252 253 254 255 256 257
		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. */
258
	if (ssp->type != PXA3xx_SSP)
259
		clk_disable(ssp->clk);
H
Haojian Zhuang 已提交
260 261
	val = pxa_ssp_read_reg(ssp, SSCR0) | sscr0;
	pxa_ssp_write_reg(ssp, SSCR0, val);
262
	if (ssp->type != PXA3xx_SSP)
263
		clk_enable(ssp->clk);
M
Mark Brown 已提交
264 265 266 267 268 269 270 271 272 273

	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)
{
274
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
275
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
276 277 278 279
	int val;

	switch (div_id) {
	case PXA_SSP_AUDIO_DIV_ACDS:
H
Haojian Zhuang 已提交
280 281
		val = (pxa_ssp_read_reg(ssp, SSACD) & ~0x7) | SSACD_ACDS(div);
		pxa_ssp_write_reg(ssp, SSACD, val);
M
Mark Brown 已提交
282 283
		break;
	case PXA_SSP_AUDIO_DIV_SCDB:
H
Haojian Zhuang 已提交
284
		val = pxa_ssp_read_reg(ssp, SSACD);
M
Mark Brown 已提交
285
		val &= ~SSACD_SCDB;
286
		if (ssp->type == PXA3xx_SSP)
M
Mark Brown 已提交
287 288 289 290 291 292 293 294
			val &= ~SSACD_SCDX8;
		switch (div) {
		case PXA_SSP_CLK_SCDB_1:
			val |= SSACD_SCDB;
			break;
		case PXA_SSP_CLK_SCDB_4:
			break;
		case PXA_SSP_CLK_SCDB_8:
295
			if (ssp->type == PXA3xx_SSP)
M
Mark Brown 已提交
296 297 298 299 300 301 302
				val |= SSACD_SCDX8;
			else
				return -EINVAL;
			break;
		default:
			return -EINVAL;
		}
H
Haojian Zhuang 已提交
303
		pxa_ssp_write_reg(ssp, SSACD, val);
M
Mark Brown 已提交
304 305
		break;
	case PXA_SSP_DIV_SCR:
H
Haojian Zhuang 已提交
306
		pxa_ssp_set_scr(ssp, div);
M
Mark Brown 已提交
307 308 309 310 311 312 313 314 315 316 317
		break;
	default:
		return -ENODEV;
	}

	return 0;
}

/*
 * Configure the PLL frequency pxa27x and (afaik - pxa320 only)
 */
318 319
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 已提交
320
{
321
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
322
	struct ssp_device *ssp = priv->ssp;
H
Haojian Zhuang 已提交
323
	u32 ssacd = pxa_ssp_read_reg(ssp, SSACD) & ~0x70;
M
Mark Brown 已提交
324

325
	if (ssp->type == PXA3xx_SSP)
H
Haojian Zhuang 已提交
326
		pxa_ssp_write_reg(ssp, SSACDD, 0);
M
Mark Brown 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

	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:
		/* PXA3xx has a clock ditherer which can be used to generate
		 * a wider range of frequencies - calculate a value for it.
		 */
354
		if (ssp->type == PXA3xx_SSP) {
M
Mark Brown 已提交
355 356 357 358 359 360
			u32 val;
			u64 tmp = 19968;
			tmp *= 1000000;
			do_div(tmp, freq_out);
			val = tmp;

361
			val = (val << 16) | 64;
H
Haojian Zhuang 已提交
362
			pxa_ssp_write_reg(ssp, SSACDD, val);
M
Mark Brown 已提交
363 364 365 366

			ssacd |= (0x6 << 4);

			dev_dbg(&ssp->pdev->dev,
367
				"Using SSACDD %x to supply %uHz\n",
M
Mark Brown 已提交
368 369 370 371 372 373 374
				val, freq_out);
			break;
		}

		return -EINVAL;
	}

H
Haojian Zhuang 已提交
375
	pxa_ssp_write_reg(ssp, SSACD, ssacd);
M
Mark Brown 已提交
376 377 378 379 380 381 382 383

	return 0;
}

/*
 * Set the active slots in TDM/Network mode
 */
static int pxa_ssp_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
384
	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
M
Mark Brown 已提交
385
{
386
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
387
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
388 389
	u32 sscr0;

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

393 394 395 396 397 398 399 400 401 402 403 404 405 406
	/* 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 已提交
407 408
		pxa_ssp_write_reg(ssp, SSTSA, tx_mask);
		pxa_ssp_write_reg(ssp, SSRSA, rx_mask);
409
	}
H
Haojian Zhuang 已提交
410
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
M
Mark Brown 已提交
411 412 413 414 415 416 417 418 419 420

	return 0;
}

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

H
Haojian Zhuang 已提交
425
	sscr1 = pxa_ssp_read_reg(ssp, SSCR1);
M
Mark Brown 已提交
426 427 428 429
	if (tristate)
		sscr1 &= ~SSCR1_TTE;
	else
		sscr1 |= SSCR1_TTE;
H
Haojian Zhuang 已提交
430
	pxa_ssp_write_reg(ssp, SSCR1, sscr1);
M
Mark Brown 已提交
431 432 433 434 435 436 437 438 439 440 441 442

	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)
{
443
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
444
	struct ssp_device *ssp = priv->ssp;
445
	u32 sscr0, sscr1, sspsp, scfr;
M
Mark Brown 已提交
446

447 448 449 450 451
	/* 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 已提交
452
	if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE) {
453 454 455 456 457
		dev_err(&ssp->pdev->dev,
			"can't change hardware dai format: stream is in use");
		return -EINVAL;
	}

M
Mark Brown 已提交
458
	/* reset port settings */
H
Haojian Zhuang 已提交
459
	sscr0 = pxa_ssp_read_reg(ssp, SSCR0) &
460
		~(SSCR0_ECS |  SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
M
Mark Brown 已提交
461 462 463 464 465
	sscr1 = SSCR1_RxTresh(8) | SSCR1_TxTresh(7);
	sspsp = 0;

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
466
		sscr1 |= SSCR1_SCLKDIR | SSCR1_SFRMDIR | SSCR1_SCFR;
M
Mark Brown 已提交
467 468
		break;
	case SND_SOC_DAIFMT_CBM_CFS:
469
		sscr1 |= SSCR1_SCLKDIR | SSCR1_SCFR;
M
Mark Brown 已提交
470 471 472 473 474 475 476
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		break;
	default:
		return -EINVAL;
	}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	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 已提交
492 493 494

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
495
		sscr0 |= SSCR0_PSP;
M
Mark Brown 已提交
496
		sscr1 |= SSCR1_RWOT | SSCR1_TRAIL;
497
		/* See hw_params() */
M
Mark Brown 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510
		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 已提交
511 512 513
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
	pxa_ssp_write_reg(ssp, SSCR1, sscr1);
	pxa_ssp_write_reg(ssp, SSPSP, sspsp);
M
Mark Brown 已提交
514

515 516 517 518 519 520 521 522 523 524 525
	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
	case SND_SOC_DAIFMT_CBM_CFS:
		scfr = pxa_ssp_read_reg(ssp, SSCR1) | SSCR1_SCFR;
		pxa_ssp_write_reg(ssp, SSCR1, scfr);

		while (pxa_ssp_read_reg(ssp, SSSR) & SSSR_BSY)
			cpu_relax();
		break;
	}

M
Mark Brown 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	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,
542
				struct snd_pcm_hw_params *params,
543
				struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
544
{
545
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
546
	struct ssp_device *ssp = priv->ssp;
547
	int chn = params_channels(params);
M
Mark Brown 已提交
548 549 550
	u32 sscr0;
	u32 sspsp;
	int width = snd_pcm_format_physical_width(params_format(params));
H
Haojian Zhuang 已提交
551
	int ttsa = pxa_ssp_read_reg(ssp, SSTSA) & 0xf;
552
	struct snd_dmaengine_dai_dma_data *dma_data;
553

554
	dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
M
Mark Brown 已提交
555

556 557 558 559
	/* 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.
	 */
560 561 562
	pxa_ssp_set_dma_params(ssp,
		((chn == 2) && (ttsa != 1)) || (width == 32),
		substream->stream == SNDRV_PCM_STREAM_PLAYBACK, dma_data);
563

M
Mark Brown 已提交
564
	/* we can only change the settings if the port is not in use */
H
Haojian Zhuang 已提交
565
	if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE)
M
Mark Brown 已提交
566 567 568
		return 0;

	/* clear selected SSP bits */
H
Haojian Zhuang 已提交
569
	sscr0 = pxa_ssp_read_reg(ssp, SSCR0) & ~(SSCR0_DSS | SSCR0_EDSS);
M
Mark Brown 已提交
570 571 572 573

	/* bit size */
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
574
		if (ssp->type == PXA3xx_SSP)
M
Mark Brown 已提交
575 576 577 578 579 580 581 582 583 584
			sscr0 |= SSCR0_FPCKE;
		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 已提交
585
	pxa_ssp_write_reg(ssp, SSCR0, sscr0);
M
Mark Brown 已提交
586 587 588

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

H
Haojian Zhuang 已提交
591
		if ((pxa_ssp_get_scr(ssp) == 4) && (width == 16)) {
592 593 594 595 596 597 598 599
			/* 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.
			*/
600
			if (ssp->type != PXA3xx_SSP)
601 602 603 604 605 606 607
				return -EINVAL;

			sspsp |= SSPSP_SFRMWDTH(width * 2);
			sspsp |= SSPSP_SFRMDLY(width * 4);
			sspsp |= SSPSP_EDMYSTOP(3);
			sspsp |= SSPSP_DMYSTOP(3);
			sspsp |= SSPSP_DMYSTRT(1);
608 609 610 611 612 613 614 615 616 617 618
		} 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);
		}
619

H
Haojian Zhuang 已提交
620
		pxa_ssp_write_reg(ssp, SSPSP, sspsp);
M
Mark Brown 已提交
621 622 623 624 625
		break;
	default:
		break;
	}

626
	/* When we use a network mode, we always require TDM slots
M
Mark Brown 已提交
627 628
	 * - complain loudly and fail if they've not been set up yet.
	 */
629
	if ((sscr0 & SSCR0_MOD) && !ttsa) {
M
Mark Brown 已提交
630 631 632 633 634 635 636 637 638
		dev_err(&ssp->pdev->dev, "No TDM timeslot configured\n");
		return -EINVAL;
	}

	dump_registers(ssp);

	return 0;
}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
static void pxa_ssp_set_running_bit(struct snd_pcm_substream *substream,
				    struct ssp_device *ssp, int value)
{
	uint32_t sscr0 = pxa_ssp_read_reg(ssp, SSCR0);
	uint32_t sscr1 = pxa_ssp_read_reg(ssp, SSCR1);
	uint32_t sspsp = pxa_ssp_read_reg(ssp, SSPSP);
	uint32_t sssr = pxa_ssp_read_reg(ssp, SSSR);

	if (value && (sscr0 & SSCR0_SSE))
		pxa_ssp_write_reg(ssp, SSCR0, sscr0 & ~SSCR0_SSE);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		if (value)
			sscr1 |= SSCR1_TSRE;
		else
			sscr1 &= ~SSCR1_TSRE;
	} else {
		if (value)
			sscr1 |= SSCR1_RSRE;
		else
			sscr1 &= ~SSCR1_RSRE;
	}

	pxa_ssp_write_reg(ssp, SSCR1, sscr1);

	if (value) {
		pxa_ssp_write_reg(ssp, SSSR, sssr);
		pxa_ssp_write_reg(ssp, SSPSP, sspsp);
		pxa_ssp_write_reg(ssp, SSCR0, sscr0 | SSCR0_SSE);
	}
}

671
static int pxa_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
672
			   struct snd_soc_dai *cpu_dai)
M
Mark Brown 已提交
673 674
{
	int ret = 0;
675
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(cpu_dai);
676
	struct ssp_device *ssp = priv->ssp;
M
Mark Brown 已提交
677 678 679 680
	int val;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_RESUME:
H
Haojian Zhuang 已提交
681
		pxa_ssp_enable(ssp);
M
Mark Brown 已提交
682 683
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
684
		pxa_ssp_set_running_bit(substream, ssp, 1);
H
Haojian Zhuang 已提交
685 686
		val = pxa_ssp_read_reg(ssp, SSSR);
		pxa_ssp_write_reg(ssp, SSSR, val);
M
Mark Brown 已提交
687 688
		break;
	case SNDRV_PCM_TRIGGER_START:
689
		pxa_ssp_set_running_bit(substream, ssp, 1);
M
Mark Brown 已提交
690 691
		break;
	case SNDRV_PCM_TRIGGER_STOP:
692
		pxa_ssp_set_running_bit(substream, ssp, 0);
M
Mark Brown 已提交
693 694
		break;
	case SNDRV_PCM_TRIGGER_SUSPEND:
H
Haojian Zhuang 已提交
695
		pxa_ssp_disable(ssp);
M
Mark Brown 已提交
696 697
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
698
		pxa_ssp_set_running_bit(substream, ssp, 0);
M
Mark Brown 已提交
699 700 701 702 703 704 705 706 707 708 709
		break;

	default:
		ret = -EINVAL;
	}

	dump_registers(ssp);

	return ret;
}

710
static int pxa_ssp_probe(struct snd_soc_dai *dai)
M
Mark Brown 已提交
711
{
712
	struct device *dev = dai->dev;
M
Mark Brown 已提交
713 714 715 716 717 718 719
	struct ssp_priv *priv;
	int ret;

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

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	if (dev->of_node) {
		struct device_node *ssp_handle;

		ssp_handle = of_parse_phandle(dev->of_node, "port", 0);
		if (!ssp_handle) {
			dev_err(dev, "unable to get 'port' phandle\n");
			return -ENODEV;
		}

		priv->ssp = pxa_ssp_request_of(ssp_handle, "SoC audio");
		if (priv->ssp == NULL) {
			ret = -ENODEV;
			goto err_priv;
		}
	} else {
		priv->ssp = pxa_ssp_request(dai->id + 1, "SoC audio");
		if (priv->ssp == NULL) {
			ret = -ENODEV;
			goto err_priv;
		}
M
Mark Brown 已提交
740 741
	}

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

	return 0;

err_priv:
	kfree(priv);
	return ret;
}

752
static int pxa_ssp_remove(struct snd_soc_dai *dai)
M
Mark Brown 已提交
753
{
754 755
	struct ssp_priv *priv = snd_soc_dai_get_drvdata(dai);

H
Haojian Zhuang 已提交
756
	pxa_ssp_free(priv->ssp);
757
	kfree(priv);
758
	return 0;
M
Mark Brown 已提交
759 760 761 762
}

#define PXA_SSP_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
			  SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |	\
763 764
			  SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |	\
			  SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 |	\
M
Mark Brown 已提交
765 766 767 768 769 770
			  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)

771
static const struct snd_soc_dai_ops pxa_ssp_dai_ops = {
772 773 774 775 776 777 778 779 780 781 782 783
	.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,
};

784
static struct snd_soc_dai_driver pxa_ssp_dai = {
M
Mark Brown 已提交
785 786 787 788 789 790
		.probe = pxa_ssp_probe,
		.remove = pxa_ssp_remove,
		.suspend = pxa_ssp_suspend,
		.resume = pxa_ssp_resume,
		.playback = {
			.channels_min = 1,
791
			.channels_max = 8,
M
Mark Brown 已提交
792 793 794 795 796
			.rates = PXA_SSP_RATES,
			.formats = PXA_SSP_FORMATS,
		},
		.capture = {
			 .channels_min = 1,
797
			 .channels_max = 8,
M
Mark Brown 已提交
798 799 800
			.rates = PXA_SSP_RATES,
			.formats = PXA_SSP_FORMATS,
		 },
801
		.ops = &pxa_ssp_dai_ops,
802 803
};

804 805 806 807
static const struct snd_soc_component_driver pxa_ssp_component = {
	.name		= "pxa-ssp",
};

808 809 810
#ifdef CONFIG_OF
static const struct of_device_id pxa_ssp_of_ids[] = {
	{ .compatible = "mrvl,pxa-ssp-dai" },
811
	{}
812 813 814
};
#endif

815
static int asoc_ssp_probe(struct platform_device *pdev)
816
{
817 818
	return snd_soc_register_component(&pdev->dev, &pxa_ssp_component,
					  &pxa_ssp_dai, 1);
819 820
}

821
static int asoc_ssp_remove(struct platform_device *pdev)
822
{
823
	snd_soc_unregister_component(&pdev->dev);
824 825 826 827 828
	return 0;
}

static struct platform_driver asoc_ssp_driver = {
	.driver = {
829 830 831
		.name = "pxa-ssp-dai",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(pxa_ssp_of_ids),
M
Mark Brown 已提交
832
	},
833 834

	.probe = asoc_ssp_probe,
835
	.remove = asoc_ssp_remove,
M
Mark Brown 已提交
836 837
};

838
module_platform_driver(asoc_ssp_driver);
M
Mark Brown 已提交
839

M
Mark Brown 已提交
840 841 842 843
/* Module information */
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
MODULE_DESCRIPTION("PXA SSP/PCM SoC Interface");
MODULE_LICENSE("GPL");