pxa2xx-ac97.c 10.8 KB
Newer Older
T
Takashi Iwai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/kernel.h>
16
#include <linux/platform_device.h>
T
Takashi Iwai 已提交
17 18
#include <linux/interrupt.h>
#include <linux/wait.h>
19
#include <linux/clk.h>
T
Takashi Iwai 已提交
20 21 22 23 24 25 26 27
#include <linux/delay.h>

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

#include <asm/irq.h>
28
#include <linux/mutex.h>
T
Takashi Iwai 已提交
29 30
#include <asm/hardware.h>
#include <asm/arch/pxa-regs.h>
31
#include <asm/arch/pxa2xx-gpio.h>
T
Takashi Iwai 已提交
32 33 34 35 36
#include <asm/arch/audio.h>

#include "pxa2xx-pcm.h"


37
static DEFINE_MUTEX(car_mutex);
T
Takashi Iwai 已提交
38 39
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
T
Takashi Iwai 已提交
44

45 46 47 48 49 50 51 52 53 54
/*
 * 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).
 */ 

55
static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
T
Takashi Iwai 已提交
56 57 58 59
{
	unsigned short val = -1;
	volatile u32 *reg_addr;

60
	mutex_lock(&car_mutex);
T
Takashi Iwai 已提交
61 62 63 64 65 66

	/* set up primary or secondary codec space */
	reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
	reg_addr += (reg >> 1);

	/* start read access across the ac97 link */
67
	GSR = GSR_CDONE | GSR_SDONE;
T
Takashi Iwai 已提交
68 69 70 71
	gsr_bits = 0;
	val = *reg_addr;
	if (reg == AC97_GPIO_STATUS)
		goto out;
72 73
	if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1) <= 0 &&
	    !((GSR | gsr_bits) & GSR_SDONE)) {
T
Takashi Iwai 已提交
74
		printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
75
				__FUNCTION__, reg, GSR | gsr_bits);
T
Takashi Iwai 已提交
76 77 78 79 80
		val = -1;
		goto out;
	}

	/* valid data now */
81
	GSR = GSR_CDONE | GSR_SDONE;
T
Takashi Iwai 已提交
82 83 84
	gsr_bits = 0;
	val = *reg_addr;			
	/* but we've just started another cycle... */
85
	wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
T
Takashi Iwai 已提交
86

87
out:	mutex_unlock(&car_mutex);
T
Takashi Iwai 已提交
88 89 90
	return val;
}

91
static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
T
Takashi Iwai 已提交
92 93 94
{
	volatile u32 *reg_addr;

95
	mutex_lock(&car_mutex);
T
Takashi Iwai 已提交
96 97 98 99

	/* set up primary or secondary codec space */
	reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
	reg_addr += (reg >> 1);
100 101

	GSR = GSR_CDONE | GSR_SDONE;
T
Takashi Iwai 已提交
102 103
	gsr_bits = 0;
	*reg_addr = val;
104 105
	if (wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1) <= 0 &&
	    !((GSR | gsr_bits) & GSR_CDONE))
T
Takashi Iwai 已提交
106
		printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
107
				__FUNCTION__, reg, GSR | gsr_bits);
T
Takashi Iwai 已提交
108

109
	mutex_unlock(&car_mutex);
T
Takashi Iwai 已提交
110 111
}

112
static void pxa2xx_ac97_reset(struct snd_ac97 *ac97)
T
Takashi Iwai 已提交
113 114 115 116 117 118 119 120
{
	/* First, try cold reset */
	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 */
121
	clk_enable(ac97conf_clk);
T
Takashi Iwai 已提交
122
	udelay(5);
123
	clk_disable(ac97conf_clk);
T
Takashi Iwai 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	GCR = GCR_COLD_RST;
	udelay(50);
#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",
				 __FUNCTION__, gsr_bits);

		/* let's try warm reset */
		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);
L
Liam Girdwood 已提交
145
		udelay(500);
T
Takashi Iwai 已提交
146
#else
L
Liam Girdwood 已提交
147
		GCR |= GCR_WARM_RST|GCR_PRIRDY_IEN|GCR_SECRDY_IEN;
T
Takashi Iwai 已提交
148 149 150 151 152 153 154 155 156 157 158 159
		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",
					 __FUNCTION__, gsr_bits);
	}

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

160
static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
T
Takashi Iwai 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
{
	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;
}

185
static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
T
Takashi Iwai 已提交
186 187 188 189 190
	.read	= pxa2xx_ac97_read,
	.write	= pxa2xx_ac97_write,
	.reset	= pxa2xx_ac97_reset,
};

191
static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_out = {
T
Takashi Iwai 已提交
192 193 194 195 196 197 198
	.name			= "AC97 PCM out",
	.dev_addr		= __PREG(PCDR),
	.drcmr			= &DRCMRTXPCDR,
	.dcmd			= DCMD_INCSRCADDR | DCMD_FLOWTRG |
				  DCMD_BURST32 | DCMD_WIDTH4,
};

199
static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_in = {
T
Takashi Iwai 已提交
200 201 202 203 204 205 206
	.name			= "AC97 PCM in",
	.dev_addr		= __PREG(PCDR),
	.drcmr			= &DRCMRRXPCDR,
	.dcmd			= DCMD_INCTRGADDR | DCMD_FLOWSRC |
				  DCMD_BURST32 | DCMD_WIDTH4,
};

207 208
static struct snd_pcm *pxa2xx_ac97_pcm;
static struct snd_ac97 *pxa2xx_ac97_ac97;
T
Takashi Iwai 已提交
209

210
static int pxa2xx_ac97_pcm_startup(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
211
{
212
	struct snd_pcm_runtime *runtime = substream->runtime;
T
Takashi Iwai 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	pxa2xx_audio_ops_t *platform_ops;
	int r;

	runtime->hw.channels_min = 2;
	runtime->hw.channels_max = 2;

	r = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
	    AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
	runtime->hw.rates = pxa2xx_ac97_ac97->rates[r];
	snd_pcm_limit_hw_rates(runtime);

       	platform_ops = substream->pcm->card->dev->platform_data;
	if (platform_ops && platform_ops->startup)
		return platform_ops->startup(substream, platform_ops->priv);
	else
		return 0;
}

231
static void pxa2xx_ac97_pcm_shutdown(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
232 233 234 235 236 237 238 239
{
	pxa2xx_audio_ops_t *platform_ops;

       	platform_ops = substream->pcm->card->dev->platform_data;
	if (platform_ops && platform_ops->shutdown)
		platform_ops->shutdown(substream, platform_ops->priv);
}

240
static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
241
{
242
	struct snd_pcm_runtime *runtime = substream->runtime;
T
Takashi Iwai 已提交
243 244 245 246 247
	int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
		  AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
	return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
}

248
static struct pxa2xx_pcm_client pxa2xx_ac97_pcm_client = {
T
Takashi Iwai 已提交
249 250 251 252 253 254 255 256 257
	.playback_params	= &pxa2xx_ac97_pcm_out,
	.capture_params		= &pxa2xx_ac97_pcm_in,
	.startup		= pxa2xx_ac97_pcm_startup,
	.shutdown		= pxa2xx_ac97_pcm_shutdown,
	.prepare		= pxa2xx_ac97_pcm_prepare,
};

#ifdef CONFIG_PM

258
static int pxa2xx_ac97_do_suspend(struct snd_card *card, pm_message_t state)
T
Takashi Iwai 已提交
259
{
T
Takashi Iwai 已提交
260 261 262 263 264 265 266 267
	pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;

	snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
	snd_pcm_suspend_all(pxa2xx_ac97_pcm);
	snd_ac97_suspend(pxa2xx_ac97_ac97);
	if (platform_ops && platform_ops->suspend)
		platform_ops->suspend(platform_ops->priv);
	GCR |= GCR_ACLINK_OFF;
268
	clk_disable(ac97_clk);
T
Takashi Iwai 已提交
269 270 271 272

	return 0;
}

273
static int pxa2xx_ac97_do_resume(struct snd_card *card)
T
Takashi Iwai 已提交
274
{
T
Takashi Iwai 已提交
275 276
	pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;

277
	clk_enable(ac97_clk);
T
Takashi Iwai 已提交
278 279 280 281
	if (platform_ops && platform_ops->resume)
		platform_ops->resume(platform_ops->priv);
	snd_ac97_resume(pxa2xx_ac97_ac97);
	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
T
Takashi Iwai 已提交
282 283 284 285

	return 0;
}

286
static int pxa2xx_ac97_suspend(struct platform_device *dev, pm_message_t state)
T
Takashi Iwai 已提交
287
{
288
	struct snd_card *card = platform_get_drvdata(dev);
T
Takashi Iwai 已提交
289 290
	int ret = 0;

291
	if (card)
292
		ret = pxa2xx_ac97_do_suspend(card, PMSG_SUSPEND);
T
Takashi Iwai 已提交
293 294 295 296

	return ret;
}

297
static int pxa2xx_ac97_resume(struct platform_device *dev)
T
Takashi Iwai 已提交
298
{
299
	struct snd_card *card = platform_get_drvdata(dev);
T
Takashi Iwai 已提交
300 301
	int ret = 0;

302
	if (card)
303
		ret = pxa2xx_ac97_do_resume(card);
T
Takashi Iwai 已提交
304 305 306 307 308 309 310 311 312

	return ret;
}

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

313
static int __devinit pxa2xx_ac97_probe(struct platform_device *dev)
T
Takashi Iwai 已提交
314
{
315 316 317
	struct snd_card *card;
	struct snd_ac97_bus *ac97_bus;
	struct snd_ac97_template ac97_template;
T
Takashi Iwai 已提交
318 319 320 321 322 323 324 325
	int ret;

	ret = -ENOMEM;
	card = snd_card_new(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
			    THIS_MODULE, 0);
	if (!card)
		goto err;

326 327
	card->dev = &dev->dev;
	strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
T
Takashi Iwai 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);
	if (ret)
		goto err;

	ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "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);
344 345 346 347 348 349
	ac97conf_clk = clk_get(&dev->dev, "AC97CONFCLK");
	if (IS_ERR(ac97conf_clk)) {
		ret = PTR_ERR(ac97conf_clk);
		ac97conf_clk = NULL;
		goto err;
	}
T
Takashi Iwai 已提交
350
#endif
351 352 353 354 355 356 357 358

	ac97_clk = clk_get(&dev->dev, "AC97CLK");
	if (IS_ERR(ac97_clk)) {
		ret = PTR_ERR(ac97_clk);
		ac97_clk = NULL;
		goto err;
	}
	clk_enable(ac97_clk);
T
Takashi Iwai 已提交
359 360 361 362 363 364 365 366 367 368 369 370

	ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
	if (ret)
		goto err;
	memset(&ac97_template, 0, sizeof(ac97_template));
	ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
	if (ret)
		goto err;

	snprintf(card->shortname, sizeof(card->shortname),
		 "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
	snprintf(card->longname, sizeof(card->longname),
371
		 "%s (%s)", dev->dev.driver->name, card->mixername);
T
Takashi Iwai 已提交
372

T
Takashi Iwai 已提交
373
	snd_card_set_dev(card, &dev->dev);
T
Takashi Iwai 已提交
374 375
	ret = snd_card_register(card);
	if (ret == 0) {
376
		platform_set_drvdata(dev, card);
T
Takashi Iwai 已提交
377 378 379 380 381 382
		return 0;
	}

 err:
	if (card)
		snd_card_free(card);
383
	if (ac97_clk) {
T
Takashi Iwai 已提交
384 385
		GCR |= GCR_ACLINK_OFF;
		free_irq(IRQ_AC97, NULL);
386 387 388 389 390 391 392 393
		clk_disable(ac97_clk);
		clk_put(ac97_clk);
		ac97_clk = NULL;
	}
#ifdef CONFIG_PXA27x
	if (ac97conf_clk) {
		clk_put(ac97conf_clk);
		ac97conf_clk = NULL;
T
Takashi Iwai 已提交
394
	}
395
#endif
T
Takashi Iwai 已提交
396 397 398
	return ret;
}

399
static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)
T
Takashi Iwai 已提交
400
{
401
	struct snd_card *card = platform_get_drvdata(dev);
T
Takashi Iwai 已提交
402 403 404

	if (card) {
		snd_card_free(card);
405
		platform_set_drvdata(dev, NULL);
T
Takashi Iwai 已提交
406 407
		GCR |= GCR_ACLINK_OFF;
		free_irq(IRQ_AC97, NULL);
408 409 410 411 412 413 414
		clk_disable(ac97_clk);
		clk_put(ac97_clk);
		ac97_clk = NULL;
#ifdef CONFIG_PXA27x
		clk_put(ac97conf_clk);
		ac97conf_clk = NULL;
#endif
T
Takashi Iwai 已提交
415 416 417 418 419
	}

	return 0;
}

420
static struct platform_driver pxa2xx_ac97_driver = {
T
Takashi Iwai 已提交
421
	.probe		= pxa2xx_ac97_probe,
422
	.remove		= __devexit_p(pxa2xx_ac97_remove),
T
Takashi Iwai 已提交
423 424
	.suspend	= pxa2xx_ac97_suspend,
	.resume		= pxa2xx_ac97_resume,
425 426 427
	.driver		= {
		.name	= "pxa2xx-ac97",
	},
T
Takashi Iwai 已提交
428 429 430 431
};

static int __init pxa2xx_ac97_init(void)
{
432
	return platform_driver_register(&pxa2xx_ac97_driver);
T
Takashi Iwai 已提交
433 434 435 436
}

static void __exit pxa2xx_ac97_exit(void)
{
437
	platform_driver_unregister(&pxa2xx_ac97_driver);
T
Takashi Iwai 已提交
438 439 440 441 442 443 444 445
}

module_init(pxa2xx_ac97_init);
module_exit(pxa2xx_ac97_exit);

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