pxa2xx-ac97.c 11.7 KB
Newer Older
L
Liam Girdwood 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
 *
 * Author:	Nicolas Pitre
 * Created:	Dec 02, 2004
 * Copyright:	MontaVista Software Inc.
 *
 * 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/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
18
#include <linux/clk.h>
L
Liam Girdwood 已提交
19 20 21 22 23 24 25 26 27 28 29 30
#include <linux/delay.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/ac97_codec.h>
#include <sound/initval.h>
#include <sound/soc.h>

#include <asm/irq.h>
#include <linux/mutex.h>
#include <asm/hardware.h>
#include <asm/arch/pxa-regs.h>
31
#include <asm/arch/pxa2xx-gpio.h>
L
Liam Girdwood 已提交
32 33 34
#include <asm/arch/audio.h>

#include "pxa2xx-pcm.h"
35
#include "pxa2xx-ac97.h"
L
Liam Girdwood 已提交
36 37 38 39

static DEFINE_MUTEX(car_mutex);
static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
static volatile long gsr_bits;
40 41 42 43
static struct clk *ac97_clk;
#ifdef CONFIG_PXA27x
static struct clk *ac97conf_clk;
#endif
L
Liam Girdwood 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/*
 * Beware PXA27x bugs:
 *
 *   o Slot 12 read from modem space will hang controller.
 *   o CDONE, SDONE interrupt fails after any slot 12 IO.
 *
 * We therefore have an hybrid approach for waiting on SDONE (interrupt or
 * 1 jiffy timeout if interrupt never comes).
 */

static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97,
	unsigned short reg)
{
	unsigned short val = -1;
	volatile u32 *reg_addr;

	mutex_lock(&car_mutex);

	/* set up primary or secondary codec/modem space */
M
Mark Brown 已提交
64
#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
L
Liam Girdwood 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
#else
	if (reg == AC97_GPIO_STATUS)
		reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
	else
		reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
#endif
	reg_addr += (reg >> 1);

#ifndef CONFIG_PXA27x
	if (reg == AC97_GPIO_STATUS) {
		/* read from controller cache */
		val = *reg_addr;
		goto out;
	}
#endif

	/* start read access across the ac97 link */
	GSR = GSR_CDONE | GSR_SDONE;
	gsr_bits = 0;
	val = *reg_addr;

	wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
	if (!((GSR | gsr_bits) & GSR_SDONE)) {
		printk(KERN_ERR "%s: read error (ac97_reg=%x GSR=%#lx)\n",
90
				__func__, reg, GSR | gsr_bits);
L
Liam Girdwood 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
		val = -1;
		goto out;
	}

	/* valid data now */
	GSR = GSR_CDONE | GSR_SDONE;
	gsr_bits = 0;
	val = *reg_addr;
	/* but we've just started another cycle... */
	wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);

out:	mutex_unlock(&car_mutex);
	return val;
}

static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
	unsigned short val)
{
	volatile u32 *reg_addr;

	mutex_lock(&car_mutex);

	/* set up primary or secondary codec/modem space */
M
Mark Brown 已提交
114
#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
L
Liam Girdwood 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
#else
	if (reg == AC97_GPIO_STATUS)
		reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
	else
		reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
#endif
	reg_addr += (reg >> 1);

	GSR = GSR_CDONE | GSR_SDONE;
	gsr_bits = 0;
	*reg_addr = val;
	wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1);
	if (!((GSR | gsr_bits) & GSR_CDONE))
		printk(KERN_ERR "%s: write error (ac97_reg=%x GSR=%#lx)\n",
130
				__func__, reg, GSR | gsr_bits);
L
Liam Girdwood 已提交
131 132 133 134 135 136

	mutex_unlock(&car_mutex);
}

static void pxa2xx_ac97_warm_reset(struct snd_ac97 *ac97)
{
M
Mark Brown 已提交
137 138 139
#ifdef CONFIG_PXA3xx
	int timeout = 100;
#endif
L
Liam Girdwood 已提交
140 141 142 143 144 145 146 147 148 149
	gsr_bits = 0;

#ifdef CONFIG_PXA27x
	/* warm reset broken on Bulverde,
	   so manually keep AC97 reset high */
	pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
	udelay(10);
	GCR |= GCR_WARM_RST;
	pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
	udelay(500);
M
Mark Brown 已提交
150 151 152 153 154
#elif defined(CONFIG_PXA3xx)
	/* Can't use interrupts */
	GCR |= GCR_WARM_RST;
	while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
		mdelay(1);
L
Liam Girdwood 已提交
155 156 157 158 159 160 161
#else
	GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
	wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
#endif

	if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
		printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
162
				 __func__, gsr_bits);
L
Liam Girdwood 已提交
163 164 165 166 167 168 169

	GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
	GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
}

static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
{
M
Mark Brown 已提交
170 171 172 173 174 175 176 177 178 179
#ifdef CONFIG_PXA3xx
	int timeout = 1000;

	/* Hold CLKBPB for 100us */
	GCR = 0;
	GCR = GCR_CLKBPB;
	udelay(100);
	GCR = 0;
#endif

L
Liam Girdwood 已提交
180 181 182 183 184 185
	GCR &=  GCR_COLD_RST;  /* clear everything but nCRST */
	GCR &= ~GCR_COLD_RST;  /* then assert nCRST */

	gsr_bits = 0;
#ifdef CONFIG_PXA27x
	/* PXA27x Developers Manual section 13.5.2.2.1 */
186
	clk_enable(ac97conf_clk);
L
Liam Girdwood 已提交
187
	udelay(5);
188
	clk_disable(ac97conf_clk);
L
Liam Girdwood 已提交
189 190
	GCR = GCR_COLD_RST;
	udelay(50);
M
Mark Brown 已提交
191 192 193 194 195 196 197
#elif defined(CONFIG_PXA3xx)
	/* Can't use interrupts on PXA3xx */
	GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);

	GCR = GCR_WARM_RST | GCR_COLD_RST;
	while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
		mdelay(10);
L
Liam Girdwood 已提交
198 199 200 201 202 203 204 205
#else
	GCR = GCR_COLD_RST;
	GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
	wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
#endif

	if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
		printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
206
				 __func__, gsr_bits);
L
Liam Girdwood 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

	GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
	GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
}

static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
{
	long status;

	status = GSR;
	if (status) {
		GSR = status;
		gsr_bits |= status;
		wake_up(&gsr_wq);

#ifdef CONFIG_PXA27x
		/* Although we don't use those we still need to clear them
		   since they tend to spuriously trigger when MMC is used
		   (hardware bug? go figure)... */
		MISR = MISR_EOC;
		PISR = PISR_EOC;
		MCSR = MCSR_EOC;
#endif

		return IRQ_HANDLED;
	}

	return IRQ_NONE;
}

struct snd_ac97_bus_ops soc_ac97_ops = {
	.read	= pxa2xx_ac97_read,
	.write	= pxa2xx_ac97_write,
	.warm_reset	= pxa2xx_ac97_warm_reset,
	.reset	= pxa2xx_ac97_cold_reset,
};

static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_out = {
	.name			= "AC97 PCM Stereo out",
	.dev_addr		= __PREG(PCDR),
	.drcmr			= &DRCMRTXPCDR,
	.dcmd			= DCMD_INCSRCADDR | DCMD_FLOWTRG |
				  DCMD_BURST32 | DCMD_WIDTH4,
};

static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_in = {
	.name			= "AC97 PCM Stereo in",
	.dev_addr		= __PREG(PCDR),
	.drcmr			= &DRCMRRXPCDR,
	.dcmd			= DCMD_INCTRGADDR | DCMD_FLOWSRC |
				  DCMD_BURST32 | DCMD_WIDTH4,
};

static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_out = {
	.name			= "AC97 Aux PCM (Slot 5) Mono out",
	.dev_addr		= __PREG(MODR),
	.drcmr			= &DRCMRTXMODR,
	.dcmd			= DCMD_INCSRCADDR | DCMD_FLOWTRG |
				  DCMD_BURST16 | DCMD_WIDTH2,
};

static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_in = {
	.name			= "AC97 Aux PCM (Slot 5) Mono in",
	.dev_addr		= __PREG(MODR),
	.drcmr			= &DRCMRRXMODR,
	.dcmd			= DCMD_INCTRGADDR | DCMD_FLOWSRC |
				  DCMD_BURST16 | DCMD_WIDTH2,
};

static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_mic_mono_in = {
	.name			= "AC97 Mic PCM (Slot 6) Mono in",
	.dev_addr		= __PREG(MCDR),
	.drcmr			= &DRCMRRXMCDR,
	.dcmd			= DCMD_INCTRGADDR | DCMD_FLOWSRC |
				  DCMD_BURST16 | DCMD_WIDTH2,
};

#ifdef CONFIG_PM
static int pxa2xx_ac97_suspend(struct platform_device *pdev,
286
	struct snd_soc_dai *dai)
L
Liam Girdwood 已提交
287 288
{
	GCR |= GCR_ACLINK_OFF;
289
	clk_disable(ac97_clk);
L
Liam Girdwood 已提交
290 291 292 293
	return 0;
}

static int pxa2xx_ac97_resume(struct platform_device *pdev,
294
	struct snd_soc_dai *dai)
L
Liam Girdwood 已提交
295 296 297 298 299 300 301 302 303
{
	pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
	pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
	pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
	pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
#ifdef CONFIG_PXA27x
	/* Use GPIO 113 as AC97 Reset on Bulverde */
	pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
#endif
304
	clk_enable(ac97_clk);
L
Liam Girdwood 已提交
305 306 307 308 309 310 311 312
	return 0;
}

#else
#define pxa2xx_ac97_suspend	NULL
#define pxa2xx_ac97_resume	NULL
#endif

313
static int pxa2xx_ac97_probe(struct platform_device *pdev,
314
			     struct snd_soc_dai *dai)
L
Liam Girdwood 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328
{
	int ret;

	ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
	if (ret < 0)
		goto err;

	pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
	pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
	pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
	pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
#ifdef CONFIG_PXA27x
	/* Use GPIO 113 as AC97 Reset on Bulverde */
	pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
329 330 331 332 333 334 335

	ac97conf_clk = clk_get(&pdev->dev, "AC97CONFCLK");
	if (IS_ERR(ac97conf_clk)) {
		ret = PTR_ERR(ac97conf_clk);
		ac97conf_clk = NULL;
		goto err_irq;
	}
L
Liam Girdwood 已提交
336
#endif
337 338 339 340 341 342
	ac97_clk = clk_get(&pdev->dev, "AC97CLK");
	if (IS_ERR(ac97_clk)) {
		ret = PTR_ERR(ac97_clk);
		ac97_clk = NULL;
		goto err_irq;
	}
343
	clk_enable(ac97_clk);
L
Liam Girdwood 已提交
344 345
	return 0;

346 347 348 349 350 351
 err_irq:
	GCR |= GCR_ACLINK_OFF;
#ifdef CONFIG_PXA27x
	if (ac97conf_clk) {
		clk_put(ac97conf_clk);
		ac97conf_clk = NULL;
L
Liam Girdwood 已提交
352
	}
353 354 355
#endif
	free_irq(IRQ_AC97, NULL);
 err:
L
Liam Girdwood 已提交
356 357 358 359 360 361 362
	return ret;
}

static void pxa2xx_ac97_remove(struct platform_device *pdev)
{
	GCR |= GCR_ACLINK_OFF;
	free_irq(IRQ_AC97, NULL);
363 364 365 366 367 368 369
#ifdef CONFIG_PXA27x
	clk_put(ac97conf_clk);
	ac97conf_clk = NULL;
#endif
	clk_disable(ac97_clk);
	clk_put(ac97_clk);
	ac97_clk = NULL;
L
Liam Girdwood 已提交
370 371 372 373 374 375
}

static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
376
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
L
Liam Girdwood 已提交
377 378

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
379
		cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_out;
L
Liam Girdwood 已提交
380
	else
381
		cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_in;
L
Liam Girdwood 已提交
382 383 384 385 386 387 388 389

	return 0;
}

static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
390
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
L
Liam Girdwood 已提交
391 392

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
393
		cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
L
Liam Girdwood 已提交
394
	else
395
		cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
L
Liam Girdwood 已提交
396 397 398 399 400 401 402 403

	return 0;
}

static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
404
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
L
Liam Girdwood 已提交
405 406 407 408

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		return -ENODEV;
	else
409
		cpu_dai->dma_data = &pxa2xx_ac97_pcm_mic_mono_in;
L
Liam Girdwood 已提交
410 411 412 413

	return 0;
}

414 415 416 417
#define PXA2XX_AC97_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)

L
Liam Girdwood 已提交
418 419 420 421
/*
 * There is only 1 physical AC97 interface for pxa2xx, but it
 * has extra fifo's that can be used for aux DACs and ADCs.
 */
422
struct snd_soc_dai pxa_ac97_dai[] = {
L
Liam Girdwood 已提交
423 424 425 426 427 428 429 430 431 432 433
{
	.name = "pxa2xx-ac97",
	.id = 0,
	.type = SND_SOC_DAI_AC97,
	.probe = pxa2xx_ac97_probe,
	.remove = pxa2xx_ac97_remove,
	.suspend = pxa2xx_ac97_suspend,
	.resume = pxa2xx_ac97_resume,
	.playback = {
		.stream_name = "AC97 Playback",
		.channels_min = 2,
434 435 436
		.channels_max = 2,
		.rates = PXA2XX_AC97_RATES,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
L
Liam Girdwood 已提交
437 438 439
	.capture = {
		.stream_name = "AC97 Capture",
		.channels_min = 2,
440 441 442
		.channels_max = 2,
		.rates = PXA2XX_AC97_RATES,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
L
Liam Girdwood 已提交
443 444 445 446 447 448 449 450 451 452
	.ops = {
		.hw_params = pxa2xx_ac97_hw_params,},
},
{
	.name = "pxa2xx-ac97-aux",
	.id = 1,
	.type = SND_SOC_DAI_AC97,
	.playback = {
		.stream_name = "AC97 Aux Playback",
		.channels_min = 1,
453 454 455
		.channels_max = 1,
		.rates = PXA2XX_AC97_RATES,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
L
Liam Girdwood 已提交
456 457 458
	.capture = {
		.stream_name = "AC97 Aux Capture",
		.channels_min = 1,
459 460 461
		.channels_max = 1,
		.rates = PXA2XX_AC97_RATES,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
L
Liam Girdwood 已提交
462 463 464 465 466 467 468 469 470 471
	.ops = {
		.hw_params = pxa2xx_ac97_hw_aux_params,},
},
{
	.name = "pxa2xx-ac97-mic",
	.id = 2,
	.type = SND_SOC_DAI_AC97,
	.capture = {
		.stream_name = "AC97 Mic Capture",
		.channels_min = 1,
472 473 474
		.channels_max = 1,
		.rates = PXA2XX_AC97_RATES,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
L
Liam Girdwood 已提交
475 476
	.ops = {
		.hw_params = pxa2xx_ac97_hw_mic_params,},
477
},
L
Liam Girdwood 已提交
478 479 480 481 482 483 484 485
};

EXPORT_SYMBOL_GPL(pxa_ac97_dai);
EXPORT_SYMBOL_GPL(soc_ac97_ops);

MODULE_AUTHOR("Nicolas Pitre");
MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
MODULE_LICENSE("GPL");