aaci.c 26.8 KB
Newer Older
R
Russell King 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  linux/sound/arm/aaci.c - ARM PrimeCell AACI PL041 driver
 *
 *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
 *
 * 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.
 *
 *  Documentation: ARM DDI 0173B
 */
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/device.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/err.h>
20
#include <linux/amba/bus.h>
R
Russell King 已提交
21 22 23

#include <asm/io.h>
#include <asm/irq.h>
24
#include <asm/sizes.h>
R
Russell King 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

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

#include "aaci.h"
#include "devdma.h"

#define DRIVER_NAME	"aaci-pl041"

/*
 * PM support is not complete.  Turn it off.
 */
#undef CONFIG_PM

42
static void aaci_ac97_select_codec(struct aaci *aaci, struct snd_ac97 *ac97)
R
Russell King 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
	u32 v, maincr = aaci->maincr | MAINCR_SCRA(ac97->num);

	/*
	 * Ensure that the slot 1/2 RX registers are empty.
	 */
	v = readl(aaci->base + AACI_SLFR);
	if (v & SLFR_2RXV)
		readl(aaci->base + AACI_SL2RX);
	if (v & SLFR_1RXV)
		readl(aaci->base + AACI_SL1RX);

	writel(maincr, aaci->base + AACI_MAINCR);
}

/*
 * P29:
 *  The recommended use of programming the external codec through slot 1
 *  and slot 2 data is to use the channels during setup routines and the
 *  slot register at any other time.  The data written into slot 1, slot 2
 *  and slot 12 registers is transmitted only when their corresponding
 *  SI1TxEn, SI2TxEn and SI12TxEn bits are set in the AACI_MAINCR
 *  register.
 */
67 68
static void aaci_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
			    unsigned short val)
R
Russell King 已提交
69 70 71
{
	struct aaci *aaci = ac97->private_data;
	u32 v;
72
	int timeout = 5000;
R
Russell King 已提交
73 74 75 76

	if (ac97->num >= 4)
		return;

77
	mutex_lock(&aaci->ac97_sem);
R
Russell King 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

	aaci_ac97_select_codec(aaci, ac97);

	/*
	 * P54: You must ensure that AACI_SL2TX is always written
	 * to, if required, before data is written to AACI_SL1TX.
	 */
	writel(val << 4, aaci->base + AACI_SL2TX);
	writel(reg << 12, aaci->base + AACI_SL1TX);

	/*
	 * Wait for the transmission of both slots to complete.
	 */
	do {
		v = readl(aaci->base + AACI_SLFR);
R
Roel Kluin 已提交
93
	} while ((v & (SLFR_1TXB|SLFR_2TXB)) && --timeout);
94 95 96 97

	if (!timeout)
		dev_err(&aaci->dev->dev,
			"timeout waiting for write to complete\n");
R
Russell King 已提交
98

99
	mutex_unlock(&aaci->ac97_sem);
R
Russell King 已提交
100 101 102 103 104
}

/*
 * Read an AC'97 register.
 */
105
static unsigned short aaci_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
R
Russell King 已提交
106 107 108
{
	struct aaci *aaci = ac97->private_data;
	u32 v;
109 110
	int timeout = 5000;
	int retries = 10;
R
Russell King 已提交
111 112 113 114

	if (ac97->num >= 4)
		return ~0;

115
	mutex_lock(&aaci->ac97_sem);
R
Russell King 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128

	aaci_ac97_select_codec(aaci, ac97);

	/*
	 * Write the register address to slot 1.
	 */
	writel((reg << 12) | (1 << 19), aaci->base + AACI_SL1TX);

	/*
	 * Wait for the transmission to complete.
	 */
	do {
		v = readl(aaci->base + AACI_SLFR);
R
Roel Kluin 已提交
129
	} while ((v & SLFR_1TXB) && --timeout);
130 131 132 133 134 135

	if (!timeout) {
		dev_err(&aaci->dev->dev, "timeout on slot 1 TX busy\n");
		v = ~0;
		goto out;
	}
R
Russell King 已提交
136 137 138 139 140 141 142 143 144 145

	/*
	 * Give the AC'97 codec more than enough time
	 * to respond. (42us = ~2 frames at 48kHz.)
	 */
	udelay(42);

	/*
	 * Wait for slot 2 to indicate data.
	 */
146
	timeout = 5000;
R
Russell King 已提交
147 148 149
	do {
		cond_resched();
		v = readl(aaci->base + AACI_SLFR) & (SLFR_1RXV|SLFR_2RXV);
R
Roel Kluin 已提交
150
	} while ((v != (SLFR_1RXV|SLFR_2RXV)) && --timeout);
R
Russell King 已提交
151

152 153
	if (!timeout) {
		dev_err(&aaci->dev->dev, "timeout on RX valid\n");
R
Russell King 已提交
154
		v = ~0;
155
		goto out;
R
Russell King 已提交
156 157
	}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	do {
		v = readl(aaci->base + AACI_SL1RX) >> 12;
		if (v == reg) {
			v = readl(aaci->base + AACI_SL2RX) >> 4;
			break;
		} else if (--retries) {
			dev_warn(&aaci->dev->dev,
				 "ac97 read back fail.  retry\n");
			continue;
		} else {
			dev_warn(&aaci->dev->dev,
				"wrong ac97 register read back (%x != %x)\n",
				v, reg);
			v = ~0;
		}
	} while (retries);
 out:
175
	mutex_unlock(&aaci->ac97_sem);
R
Russell King 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	return v;
}

static inline void aaci_chan_wait_ready(struct aaci_runtime *aacirun)
{
	u32 val;
	int timeout = 5000;

	do {
		val = readl(aacirun->base + AACI_SR);
	} while (val & (SR_TXB|SR_RXB) && timeout--);
}



/*
 * Interrupt support.
 */
194
static void aaci_fifo_irq(struct aaci *aaci, int channel, u32 mask)
R
Russell King 已提交
195
{
K
Kevin Hilman 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	if (mask & ISR_ORINTR) {
		dev_warn(&aaci->dev->dev, "RX overrun on chan %d\n", channel);
		writel(ICLR_RXOEC1 << channel, aaci->base + AACI_INTCLR);
	}

	if (mask & ISR_RXTOINTR) {
		dev_warn(&aaci->dev->dev, "RX timeout on chan %d\n", channel);
		writel(ICLR_RXTOFEC1 << channel, aaci->base + AACI_INTCLR);
	}

	if (mask & ISR_RXINTR) {
		struct aaci_runtime *aacirun = &aaci->capture;
		void *ptr;

		if (!aacirun->substream || !aacirun->start) {
211
			dev_warn(&aaci->dev->dev, "RX interrupt???\n");
K
Kevin Hilman 已提交
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
			writel(0, aacirun->base + AACI_IE);
			return;
		}
		ptr = aacirun->ptr;

		do {
			unsigned int len = aacirun->fifosz;
			u32 val;

			if (aacirun->bytes <= 0) {
				aacirun->bytes += aacirun->period;
				aacirun->ptr = ptr;
				spin_unlock(&aaci->lock);
				snd_pcm_period_elapsed(aacirun->substream);
				spin_lock(&aaci->lock);
			}
			if (!(aacirun->cr & CR_EN))
				break;

			val = readl(aacirun->base + AACI_SR);
			if (!(val & SR_RXHF))
				break;
			if (!(val & SR_RXFF))
				len >>= 1;

			aacirun->bytes -= len;

			/* reading 16 bytes at a time */
			for( ; len > 0; len -= 16) {
				asm(
					"ldmia	%1, {r0, r1, r2, r3}\n\t"
					"stmia	%0!, {r0, r1, r2, r3}"
					: "+r" (ptr)
					: "r" (aacirun->fifo)
					: "r0", "r1", "r2", "r3", "cc");

				if (ptr >= aacirun->end)
					ptr = aacirun->start;
			}
		} while(1);
		aacirun->ptr = ptr;
	}

R
Russell King 已提交
255
	if (mask & ISR_URINTR) {
256 257
		dev_dbg(&aaci->dev->dev, "TX underrun on chan %d\n", channel);
		writel(ICLR_TXUEC1 << channel, aaci->base + AACI_INTCLR);
R
Russell King 已提交
258 259 260 261 262 263 264
	}

	if (mask & ISR_TXINTR) {
		struct aaci_runtime *aacirun = &aaci->playback;
		void *ptr;

		if (!aacirun->substream || !aacirun->start) {
265
			dev_warn(&aaci->dev->dev, "TX interrupt???\n");
R
Russell King 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			writel(0, aacirun->base + AACI_IE);
			return;
		}

		ptr = aacirun->ptr;
		do {
			unsigned int len = aacirun->fifosz;
			u32 val;

			if (aacirun->bytes <= 0) {
				aacirun->bytes += aacirun->period;
				aacirun->ptr = ptr;
				spin_unlock(&aaci->lock);
				snd_pcm_period_elapsed(aacirun->substream);
				spin_lock(&aaci->lock);
			}
K
Kevin Hilman 已提交
282
			if (!(aacirun->cr & CR_EN))
R
Russell King 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
				break;

			val = readl(aacirun->base + AACI_SR);
			if (!(val & SR_TXHE))
				break;
			if (!(val & SR_TXFE))
				len >>= 1;

			aacirun->bytes -= len;

			/* writing 16 bytes at a time */
			for ( ; len > 0; len -= 16) {
				asm(
					"ldmia	%0!, {r0, r1, r2, r3}\n\t"
					"stmia	%1, {r0, r1, r2, r3}"
					: "+r" (ptr)
					: "r" (aacirun->fifo)
					: "r0", "r1", "r2", "r3", "cc");

				if (ptr >= aacirun->end)
					ptr = aacirun->start;
			}
		} while (1);

		aacirun->ptr = ptr;
	}
}

311
static irqreturn_t aaci_irq(int irq, void *devid)
R
Russell King 已提交
312 313 314 315 316 317 318 319 320 321 322
{
	struct aaci *aaci = devid;
	u32 mask;
	int i;

	spin_lock(&aaci->lock);
	mask = readl(aaci->base + AACI_ALLINTS);
	if (mask) {
		u32 m = mask;
		for (i = 0; i < 4; i++, m >>= 7) {
			if (m & 0x7f) {
323
				aaci_fifo_irq(aaci, i, m);
R
Russell King 已提交
324 325 326 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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
			}
		}
	}
	spin_unlock(&aaci->lock);

	return mask ? IRQ_HANDLED : IRQ_NONE;
}



/*
 * ALSA support.
 */

struct aaci_stream {
	unsigned char codec_idx;
	unsigned char rate_idx;
};

static struct aaci_stream aaci_streams[] = {
	[ACSTREAM_FRONT] = {
		.codec_idx	= 0,
		.rate_idx	= AC97_RATES_FRONT_DAC,
	},
	[ACSTREAM_SURROUND] = {
		.codec_idx	= 0,
		.rate_idx	= AC97_RATES_SURR_DAC,
	},
	[ACSTREAM_LFE] = {
		.codec_idx	= 0,
		.rate_idx	= AC97_RATES_LFE_DAC,
	},
};

static inline unsigned int aaci_rate_mask(struct aaci *aaci, int streamid)
{
	struct aaci_stream *s = aaci_streams + streamid;
	return aaci->ac97_bus->codec[s->codec_idx]->rates[s->rate_idx];
}

static unsigned int rate_list[] = {
	5512, 8000, 11025, 16000, 22050, 32000, 44100,
	48000, 64000, 88200, 96000, 176400, 192000
};

/*
 * Double-rate rule: we can support double rate iff channels == 2
 *  (unimplemented)
 */
static int
374
aaci_rule_rate_by_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
R
Russell King 已提交
375 376 377
{
	struct aaci *aaci = rule->private;
	unsigned int rate_mask = SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_5512;
378
	struct snd_interval *c = hw_param_interval(p, SNDRV_PCM_HW_PARAM_CHANNELS);
R
Russell King 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

	switch (c->max) {
	case 6:
		rate_mask &= aaci_rate_mask(aaci, ACSTREAM_LFE);
	case 4:
		rate_mask &= aaci_rate_mask(aaci, ACSTREAM_SURROUND);
	case 2:
		rate_mask &= aaci_rate_mask(aaci, ACSTREAM_FRONT);
	}

	return snd_interval_list(hw_param_interval(p, rule->var),
				 ARRAY_SIZE(rate_list), rate_list,
				 rate_mask);
}

394
static struct snd_pcm_hardware aaci_hw_info = {
R
Russell King 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	.info			= SNDRV_PCM_INFO_MMAP |
				  SNDRV_PCM_INFO_MMAP_VALID |
				  SNDRV_PCM_INFO_INTERLEAVED |
				  SNDRV_PCM_INFO_BLOCK_TRANSFER |
				  SNDRV_PCM_INFO_RESUME,

	/*
	 * ALSA doesn't support 18-bit or 20-bit packed into 32-bit
	 * words.  It also doesn't support 12-bit at all.
	 */
	.formats		= SNDRV_PCM_FMTBIT_S16_LE,

	/* should this be continuous or knot? */
	.rates			= SNDRV_PCM_RATE_CONTINUOUS,
	.rate_max		= 48000,
	.rate_min		= 4000,
	.channels_min		= 2,
	.channels_max		= 6,
	.buffer_bytes_max	= 64 * 1024,
	.period_bytes_min	= 256,
	.period_bytes_max	= PAGE_SIZE,
	.periods_min		= 4,
	.periods_max		= PAGE_SIZE / 16,
};

K
Kevin Hilman 已提交
420 421 422
static int __aaci_pcm_open(struct aaci *aaci,
			   struct snd_pcm_substream *substream,
			   struct aaci_runtime *aacirun)
R
Russell King 已提交
423
{
424
	struct snd_pcm_runtime *runtime = substream->runtime;
R
Russell King 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	int ret;

	aacirun->substream = substream;
	runtime->private_data = aacirun;
	runtime->hw = aaci_hw_info;

	/*
	 * FIXME: ALSA specifies fifo_size in bytes.  If we're in normal
	 * mode, each 32-bit word contains one sample.  If we're in
	 * compact mode, each 32-bit word contains two samples, effectively
	 * halving the FIFO size.  However, we don't know for sure which
	 * we'll be using at this point.  We set this to the lower limit.
	 */
	runtime->hw.fifo_size = aaci->fifosize * 2;

	/*
	 * Add rule describing hardware rate dependency
	 * on the number of channels.
	 */
	ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				  aaci_rule_rate_by_channels, aaci,
				  SNDRV_PCM_HW_PARAM_CHANNELS,
				  SNDRV_PCM_HW_PARAM_RATE, -1);
	if (ret)
		goto out;

451
	ret = request_irq(aaci->dev->irq[0], aaci_irq, IRQF_SHARED|IRQF_DISABLED,
R
Russell King 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465
			  DRIVER_NAME, aaci);
	if (ret)
		goto out;

	return 0;

 out:
	return ret;
}


/*
 * Common ALSA stuff
 */
466
static int aaci_pcm_close(struct snd_pcm_substream *substream)
R
Russell King 已提交
467 468 469 470
{
	struct aaci *aaci = substream->private_data;
	struct aaci_runtime *aacirun = substream->runtime->private_data;

K
Kevin Hilman 已提交
471
	WARN_ON(aacirun->cr & CR_EN);
R
Russell King 已提交
472 473 474 475 476 477 478

	aacirun->substream = NULL;
	free_irq(aaci->dev->irq[0], aaci);

	return 0;
}

479
static int aaci_pcm_hw_free(struct snd_pcm_substream *substream)
R
Russell King 已提交
480 481 482 483 484 485
{
	struct aaci_runtime *aacirun = substream->runtime->private_data;

	/*
	 * This must not be called with the device enabled.
	 */
K
Kevin Hilman 已提交
486
	WARN_ON(aacirun->cr & CR_EN);
R
Russell King 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499

	if (aacirun->pcm_open)
		snd_ac97_pcm_close(aacirun->pcm);
	aacirun->pcm_open = 0;

	/*
	 * Clear out the DMA and any allocated buffers.
	 */
	devdma_hw_free(NULL, substream);

	return 0;
}

500
static int aaci_pcm_hw_params(struct snd_pcm_substream *substream,
R
Russell King 已提交
501
			      struct aaci_runtime *aacirun,
502
			      struct snd_pcm_hw_params *params)
R
Russell King 已提交
503 504 505 506
{
	int err;

	aaci_pcm_hw_free(substream);
507 508 509 510
	if (aacirun->pcm_open) {
		snd_ac97_pcm_close(aacirun->pcm);
		aacirun->pcm_open = 0;
	}
R
Russell King 已提交
511 512 513 514 515 516

	err = devdma_hw_alloc(NULL, substream,
			      params_buffer_bytes(params));
	if (err < 0)
		goto out;

K
Kevin Hilman 已提交
517 518 519 520 521 522 523
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
					params_channels(params),
					aacirun->pcm->r[0].slots);
	else
		err = snd_ac97_pcm_open(aacirun->pcm, params_rate(params),
					params_channels(params),
R
Russell King 已提交
524
					aacirun->pcm->r[0].slots);
K
Kevin Hilman 已提交
525

R
Russell King 已提交
526 527 528 529 530 531 532 533 534
	if (err)
		goto out;

	aacirun->pcm_open = 1;

 out:
	return err;
}

535
static int aaci_pcm_prepare(struct snd_pcm_substream *substream)
R
Russell King 已提交
536
{
537
	struct snd_pcm_runtime *runtime = substream->runtime;
R
Russell King 已提交
538 539 540 541 542 543 544 545 546 547 548
	struct aaci_runtime *aacirun = runtime->private_data;

	aacirun->start	= (void *)runtime->dma_area;
	aacirun->end	= aacirun->start + runtime->dma_bytes;
	aacirun->ptr	= aacirun->start;
	aacirun->period	=
	aacirun->bytes	= frames_to_bytes(runtime, runtime->period_size);

	return 0;
}

549
static snd_pcm_uframes_t aaci_pcm_pointer(struct snd_pcm_substream *substream)
R
Russell King 已提交
550
{
551
	struct snd_pcm_runtime *runtime = substream->runtime;
R
Russell King 已提交
552 553 554 555 556 557
	struct aaci_runtime *aacirun = runtime->private_data;
	ssize_t bytes = aacirun->ptr - aacirun->start;

	return bytes_to_frames(runtime, bytes);
}

558
static int aaci_pcm_mmap(struct snd_pcm_substream *substream, struct vm_area_struct *vma)
R
Russell King 已提交
559 560 561 562 563 564 565 566 567
{
	return devdma_mmap(NULL, substream, vma);
}


/*
 * Playback specific ALSA stuff
 */
static const u32 channels_to_txmask[] = {
K
Kevin Hilman 已提交
568 569 570
	[2] = CR_SL3 | CR_SL4,
	[4] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8,
	[6] = CR_SL3 | CR_SL4 | CR_SL7 | CR_SL8 | CR_SL6 | CR_SL9,
R
Russell King 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584
};

/*
 * We can support two and four channel audio.  Unfortunately
 * six channel audio requires a non-standard channel ordering:
 *   2 -> FL(3), FR(4)
 *   4 -> FL(3), FR(4), SL(7), SR(8)
 *   6 -> FL(3), FR(4), SL(7), SR(8), C(6), LFE(9) (required)
 *        FL(3), FR(4), C(6), SL(7), SR(8), LFE(9) (actual)
 * This requires an ALSA configuration file to correct.
 */
static unsigned int channel_list[] = { 2, 4, 6 };

static int
585
aaci_rule_channels(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *rule)
R
Russell King 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
{
	struct aaci *aaci = rule->private;
	unsigned int chan_mask = 1 << 0, slots;

	/*
	 * pcms[0] is the our 5.1 PCM instance.
	 */
	slots = aaci->ac97_bus->pcms[0].r[0].slots;
	if (slots & (1 << AC97_SLOT_PCM_SLEFT)) {
		chan_mask |= 1 << 1;
		if (slots & (1 << AC97_SLOT_LFE))
			chan_mask |= 1 << 2;
	}

	return snd_interval_list(hw_param_interval(p, rule->var),
				 ARRAY_SIZE(channel_list), channel_list,
				 chan_mask);
}

K
Kevin Hilman 已提交
605
static int aaci_pcm_open(struct snd_pcm_substream *substream)
R
Russell King 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619
{
	struct aaci *aaci = substream->private_data;
	int ret;

	/*
	 * Add rule describing channel dependency.
	 */
	ret = snd_pcm_hw_rule_add(substream->runtime, 0,
				  SNDRV_PCM_HW_PARAM_CHANNELS,
				  aaci_rule_channels, aaci,
				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	if (ret)
		return ret;

K
Kevin Hilman 已提交
620 621 622 623 624 625
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		ret = __aaci_pcm_open(aaci, substream, &aaci->playback);
	} else {
		ret = __aaci_pcm_open(aaci, substream, &aaci->capture);
	}
	return ret;
R
Russell King 已提交
626 627
}

628 629
static int aaci_pcm_playback_hw_params(struct snd_pcm_substream *substream,
				       struct snd_pcm_hw_params *params)
R
Russell King 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
{
	struct aaci *aaci = substream->private_data;
	struct aaci_runtime *aacirun = substream->runtime->private_data;
	unsigned int channels = params_channels(params);
	int ret;

	WARN_ON(channels >= ARRAY_SIZE(channels_to_txmask) ||
		!channels_to_txmask[channels]);

	ret = aaci_pcm_hw_params(substream, aacirun, params);

	/*
	 * Enable FIFO, compact mode, 16 bits per sample.
	 * FIXME: double rate slots?
	 */
	if (ret >= 0) {
K
Kevin Hilman 已提交
646
		aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;
R
Russell King 已提交
647 648 649
		aacirun->cr |= channels_to_txmask[channels];

		aacirun->fifosz	= aaci->fifosize * 4;
K
Kevin Hilman 已提交
650
		if (aacirun->cr & CR_COMPACT)
R
Russell King 已提交
651 652 653 654 655 656 657 658 659 660 661 662
			aacirun->fifosz >>= 1;
	}
	return ret;
}

static void aaci_pcm_playback_stop(struct aaci_runtime *aacirun)
{
	u32 ie;

	ie = readl(aacirun->base + AACI_IE);
	ie &= ~(IE_URIE|IE_TXIE);
	writel(ie, aacirun->base + AACI_IE);
K
Kevin Hilman 已提交
663
	aacirun->cr &= ~CR_EN;
R
Russell King 已提交
664 665 666 667 668 669 670 671 672
	aaci_chan_wait_ready(aacirun);
	writel(aacirun->cr, aacirun->base + AACI_TXCR);
}

static void aaci_pcm_playback_start(struct aaci_runtime *aacirun)
{
	u32 ie;

	aaci_chan_wait_ready(aacirun);
K
Kevin Hilman 已提交
673
	aacirun->cr |= CR_EN;
R
Russell King 已提交
674 675 676 677 678 679 680

	ie = readl(aacirun->base + AACI_IE);
	ie |= IE_URIE | IE_TXIE;
	writel(ie, aacirun->base + AACI_IE);
	writel(aacirun->cr, aacirun->base + AACI_TXCR);
}

681
static int aaci_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
R
Russell King 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
	struct aaci *aaci = substream->private_data;
	struct aaci_runtime *aacirun = substream->runtime->private_data;
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&aaci->lock, flags);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		aaci_pcm_playback_start(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_RESUME:
		aaci_pcm_playback_start(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_STOP:
		aaci_pcm_playback_stop(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_SUSPEND:
		aaci_pcm_playback_stop(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		break;

	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		break;

	default:
		ret = -EINVAL;
	}
	spin_unlock_irqrestore(&aaci->lock, flags);

	return ret;
}

720
static struct snd_pcm_ops aaci_playback_ops = {
K
Kevin Hilman 已提交
721
	.open		= aaci_pcm_open,
R
Russell King 已提交
722 723 724 725 726 727 728 729 730 731
	.close		= aaci_pcm_close,
	.ioctl		= snd_pcm_lib_ioctl,
	.hw_params	= aaci_pcm_playback_hw_params,
	.hw_free	= aaci_pcm_hw_free,
	.prepare	= aaci_pcm_prepare,
	.trigger	= aaci_pcm_playback_trigger,
	.pointer	= aaci_pcm_pointer,
	.mmap		= aaci_pcm_mmap,
};

R
Russell King 已提交
732 733
static int aaci_pcm_capture_hw_params(struct snd_pcm_substream *substream,
				      struct snd_pcm_hw_params *params)
K
Kevin Hilman 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
{
	struct aaci *aaci = substream->private_data;
	struct aaci_runtime *aacirun = substream->runtime->private_data;
	int ret;

	ret = aaci_pcm_hw_params(substream, aacirun, params);

	if (ret >= 0) {
		aacirun->cr = CR_FEN | CR_COMPACT | CR_SZ16;

		/* Line in record: slot 3 and 4 */
		aacirun->cr |= CR_SL3 | CR_SL4;

		aacirun->fifosz = aaci->fifosize * 4;

		if (aacirun->cr & CR_COMPACT)
			aacirun->fifosz >>= 1;
	}
	return ret;
}

static void aaci_pcm_capture_stop(struct aaci_runtime *aacirun)
{
	u32 ie;

	aaci_chan_wait_ready(aacirun);

	ie = readl(aacirun->base + AACI_IE);
	ie &= ~(IE_ORIE | IE_RXIE);
	writel(ie, aacirun->base+AACI_IE);

	aacirun->cr &= ~CR_EN;
R
Russell King 已提交
766

K
Kevin Hilman 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
	writel(aacirun->cr, aacirun->base + AACI_RXCR);
}

static void aaci_pcm_capture_start(struct aaci_runtime *aacirun)
{
	u32 ie;

	aaci_chan_wait_ready(aacirun);

#ifdef DEBUG
	/* RX Timeout value: bits 28:17 in RXCR */
	aacirun->cr |= 0xf << 17;
#endif

	aacirun->cr |= CR_EN;
	writel(aacirun->cr, aacirun->base + AACI_RXCR);

	ie = readl(aacirun->base + AACI_IE);
	ie |= IE_ORIE |IE_RXIE; // overrun and rx interrupt -- half full
	writel(ie, aacirun->base + AACI_IE);
}

R
Russell King 已提交
789 790
static int aaci_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
{
K
Kevin Hilman 已提交
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
	struct aaci *aaci = substream->private_data;
	struct aaci_runtime *aacirun = substream->runtime->private_data;
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&aaci->lock, flags);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		aaci_pcm_capture_start(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_RESUME:
		aaci_pcm_capture_start(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_STOP:
		aaci_pcm_capture_stop(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_SUSPEND:
		aaci_pcm_capture_stop(aacirun);
		break;

	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		break;

	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		break;

	default:
		ret = -EINVAL;
	}

	spin_unlock_irqrestore(&aaci->lock, flags);

	return ret;
}

R
Russell King 已提交
830
static int aaci_pcm_capture_prepare(struct snd_pcm_substream *substream)
K
Kevin Hilman 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aaci *aaci = substream->private_data;

	aaci_pcm_prepare(substream);

	/* allow changing of sample rate */
	aaci_ac97_write(aaci->ac97, AC97_EXTENDED_STATUS, 0x0001); /* VRA */
	aaci_ac97_write(aaci->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
	aaci_ac97_write(aaci->ac97, AC97_PCM_MIC_ADC_RATE, runtime->rate);

	/* Record select: Mic: 0, Aux: 3, Line: 4 */
	aaci_ac97_write(aaci->ac97, AC97_REC_SEL, 0x0404);

	return 0;
}

R
Russell King 已提交
848
static struct snd_pcm_ops aaci_capture_ops = {
K
Kevin Hilman 已提交
849 850 851 852 853 854 855 856 857 858
	.open		= aaci_pcm_open,
	.close		= aaci_pcm_close,
	.ioctl		= snd_pcm_lib_ioctl,
	.hw_params	= aaci_pcm_capture_hw_params,
	.hw_free	= aaci_pcm_hw_free,
	.prepare	= aaci_pcm_capture_prepare,
	.trigger	= aaci_pcm_capture_trigger,
	.pointer	= aaci_pcm_pointer,
	.mmap		= aaci_pcm_mmap,
};
R
Russell King 已提交
859 860 861 862 863

/*
 * Power Management.
 */
#ifdef CONFIG_PM
864
static int aaci_do_suspend(struct snd_card *card, unsigned int state)
R
Russell King 已提交
865 866
{
	struct aaci *aaci = card->private_data;
T
Takashi Iwai 已提交
867 868
	snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
	snd_pcm_suspend_all(aaci->pcm);
R
Russell King 已提交
869 870 871
	return 0;
}

872
static int aaci_do_resume(struct snd_card *card, unsigned int state)
R
Russell King 已提交
873
{
T
Takashi Iwai 已提交
874
	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
R
Russell King 已提交
875 876 877
	return 0;
}

878
static int aaci_suspend(struct amba_device *dev, pm_message_t state)
R
Russell King 已提交
879
{
880
	struct snd_card *card = amba_get_drvdata(dev);
R
Russell King 已提交
881 882 883 884 885
	return card ? aaci_do_suspend(card) : 0;
}

static int aaci_resume(struct amba_device *dev)
{
886
	struct snd_card *card = amba_get_drvdata(dev);
R
Russell King 已提交
887 888 889 890 891 892 893 894 895 896 897
	return card ? aaci_do_resume(card) : 0;
}
#else
#define aaci_do_suspend		NULL
#define aaci_do_resume		NULL
#define aaci_suspend		NULL
#define aaci_resume		NULL
#endif


static struct ac97_pcm ac97_defs[] __devinitdata = {
K
Kevin Hilman 已提交
898
	[0] = {	/* Front PCM */
R
Russell King 已提交
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 931
		.exclusive = 1,
		.r = {
			[0] = {
				.slots	= (1 << AC97_SLOT_PCM_LEFT) |
					  (1 << AC97_SLOT_PCM_RIGHT) |
					  (1 << AC97_SLOT_PCM_CENTER) |
					  (1 << AC97_SLOT_PCM_SLEFT) |
					  (1 << AC97_SLOT_PCM_SRIGHT) |
					  (1 << AC97_SLOT_LFE),
			},
		},
	},
	[1] = {	/* PCM in */
		.stream = 1,
		.exclusive = 1,
		.r = {
			[0] = {
				.slots	= (1 << AC97_SLOT_PCM_LEFT) |
					  (1 << AC97_SLOT_PCM_RIGHT),
			},
		},
	},
	[2] = {	/* Mic in */
		.stream = 1,
		.exclusive = 1,
		.r = {
			[0] = {
				.slots	= (1 << AC97_SLOT_MIC),
			},
		},
	}
};

932
static struct snd_ac97_bus_ops aaci_bus_ops = {
R
Russell King 已提交
933 934 935 936 937 938
	.write	= aaci_ac97_write,
	.read	= aaci_ac97_read,
};

static int __devinit aaci_probe_ac97(struct aaci *aaci)
{
939 940 941
	struct snd_ac97_template ac97_template;
	struct snd_ac97_bus *ac97_bus;
	struct snd_ac97 *ac97;
R
Russell King 已提交
942 943
	int ret;

944
	writel(0, aaci->base + AC97_POWERDOWN);
R
Russell King 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	/*
	 * Assert AACIRESET for 2us
	 */
	writel(0, aaci->base + AACI_RESET);
	udelay(2);
	writel(RESET_NRST, aaci->base + AACI_RESET);

	/*
	 * Give the AC'97 codec more than enough time
	 * to wake up. (42us = ~2 frames at 48kHz.)
	 */
	udelay(42);

	ret = snd_ac97_bus(aaci->card, 0, &aaci_bus_ops, aaci, &ac97_bus);
	if (ret)
		goto out;

	ac97_bus->clock = 48000;
	aaci->ac97_bus = ac97_bus;

965
	memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
R
Russell King 已提交
966 967 968 969 970 971 972
	ac97_template.private_data = aaci;
	ac97_template.num = 0;
	ac97_template.scaps = AC97_SCAP_SKIP_MODEM;

	ret = snd_ac97_mixer(ac97_bus, &ac97_template, &ac97);
	if (ret)
		goto out;
K
Kevin Hilman 已提交
973
	aaci->ac97 = ac97;
R
Russell King 已提交
974 975 976 977 978 979 980 981 982 983 984 985

	/*
	 * Disable AC97 PC Beep input on audio codecs.
	 */
	if (ac97_is_audio(ac97))
		snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);

	ret = snd_ac97_pcm_assign(ac97_bus, ARRAY_SIZE(ac97_defs), ac97_defs);
	if (ret)
		goto out;

	aaci->playback.pcm = &ac97_bus->pcms[0];
K
Kevin Hilman 已提交
986
	aaci->capture.pcm  = &ac97_bus->pcms[1];
R
Russell King 已提交
987 988 989 990 991

 out:
	return ret;
}

992
static void aaci_free_card(struct snd_card *card)
R
Russell King 已提交
993 994 995 996 997 998 999 1000 1001
{
	struct aaci *aaci = card->private_data;
	if (aaci->base)
		iounmap(aaci->base);
}

static struct aaci * __devinit aaci_init_card(struct amba_device *dev)
{
	struct aaci *aaci;
1002
	struct snd_card *card;
1003
	int err;
R
Russell King 已提交
1004

1005 1006 1007
	err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
			      THIS_MODULE, sizeof(struct aaci), &card);
	if (err < 0)
1008
		return NULL;
R
Russell King 已提交
1009 1010 1011 1012 1013 1014

	card->private_free = aaci_free_card;

	strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
	strlcpy(card->shortname, "ARM AC'97 Interface", sizeof(card->shortname));
	snprintf(card->longname, sizeof(card->longname),
1015 1016 1017
		 "%s at 0x%016llx, irq %d",
		 card->shortname, (unsigned long long)dev->res.start,
		 dev->irq[0]);
R
Russell King 已提交
1018 1019

	aaci = card->private_data;
1020
	mutex_init(&aaci->ac97_sem);
R
Russell King 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	spin_lock_init(&aaci->lock);
	aaci->card = card;
	aaci->dev = dev;

	/* Set MAINCR to allow slot 1 and 2 data IO */
	aaci->maincr = MAINCR_IE | MAINCR_SL1RXEN | MAINCR_SL1TXEN |
		       MAINCR_SL2RXEN | MAINCR_SL2TXEN;

	return aaci;
}

static int __devinit aaci_init_pcm(struct aaci *aaci)
{
1034
	struct snd_pcm *pcm;
R
Russell King 已提交
1035 1036
	int ret;

K
Kevin Hilman 已提交
1037
	ret = snd_pcm_new(aaci->card, "AACI AC'97", 0, 1, 1, &pcm);
R
Russell King 已提交
1038 1039 1040 1041 1042 1043 1044 1045
	if (ret == 0) {
		aaci->pcm = pcm;
		pcm->private_data = aaci;
		pcm->info_flags = 0;

		strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));

		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaci_playback_ops);
K
Kevin Hilman 已提交
1046
		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaci_capture_ops);
R
Russell King 已提交
1047 1048 1049 1050 1051 1052 1053
	}

	return ret;
}

static unsigned int __devinit aaci_size_fifo(struct aaci *aaci)
{
K
Kevin Hilman 已提交
1054
	struct aaci_runtime *aacirun = &aaci->playback;
R
Russell King 已提交
1055 1056
	int i;

K
Kevin Hilman 已提交
1057
	writel(CR_FEN | CR_SZ16 | CR_EN, aacirun->base + AACI_TXCR);
R
Russell King 已提交
1058

K
Kevin Hilman 已提交
1059 1060
	for (i = 0; !(readl(aacirun->base + AACI_SR) & SR_TXFF) && i < 4096; i++)
		writel(0, aacirun->fifo);
R
Russell King 已提交
1061

K
Kevin Hilman 已提交
1062
	writel(0, aacirun->base + AACI_TXCR);
R
Russell King 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

	/*
	 * Re-initialise the AACI after the FIFO depth test, to
	 * ensure that the FIFOs are empty.  Unfortunately, merely
	 * disabling the channel doesn't clear the FIFO.
	 */
	writel(aaci->maincr & ~MAINCR_IE, aaci->base + AACI_MAINCR);
	writel(aaci->maincr, aaci->base + AACI_MAINCR);

	/*
	 * If we hit 4096, we failed.  Go back to the specified
	 * fifo depth.
	 */
	if (i == 4096)
		i = 8;

	return i;
}

1082
static int __devinit aaci_probe(struct amba_device *dev, struct amba_id *id)
R
Russell King 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091
{
	struct aaci *aaci;
	int ret, i;

	ret = amba_request_regions(dev, NULL);
	if (ret)
		return ret;

	aaci = aaci_init_card(dev);
1092 1093
	if (!aaci) {
		ret = -ENOMEM;
R
Russell King 已提交
1094 1095 1096
		goto out;
	}

1097
	aaci->base = ioremap(dev->res.start, resource_size(&dev->res));
R
Russell King 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	if (!aaci->base) {
		ret = -ENOMEM;
		goto out;
	}

	/*
	 * Playback uses AACI channel 0
	 */
	aaci->playback.base = aaci->base + AACI_CSCH1;
	aaci->playback.fifo = aaci->base + AACI_DR1;

K
Kevin Hilman 已提交
1109 1110 1111 1112 1113 1114
	/*
	 * Capture uses AACI channel 0
	 */
	aaci->capture.base = aaci->base + AACI_CSCH1;
	aaci->capture.fifo = aaci->base + AACI_DR1;

R
Russell King 已提交
1115
	for (i = 0; i < 4; i++) {
1116
		void __iomem *base = aaci->base + i * 0x14;
R
Russell King 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125

		writel(0, base + AACI_IE);
		writel(0, base + AACI_TXCR);
		writel(0, base + AACI_RXCR);
	}

	writel(0x1fff, aaci->base + AACI_INTCLR);
	writel(aaci->maincr, aaci->base + AACI_MAINCR);

1126 1127 1128 1129
	ret = aaci_probe_ac97(aaci);
	if (ret)
		goto out;

R
Russell King 已提交
1130
	/*
1131
	 * Size the FIFOs (must be multiple of 16).
R
Russell King 已提交
1132 1133
	 */
	aaci->fifosize = aaci_size_fifo(aaci);
1134 1135 1136 1137
	if (aaci->fifosize & 15) {
		printk(KERN_WARNING "AACI: fifosize = %d not supported\n",
		       aaci->fifosize);
		ret = -ENODEV;
R
Russell King 已提交
1138
		goto out;
1139
	}
R
Russell King 已提交
1140 1141 1142 1143 1144

	ret = aaci_init_pcm(aaci);
	if (ret)
		goto out;

T
Takashi Iwai 已提交
1145 1146
	snd_card_set_dev(aaci->card, &dev->dev);

R
Russell King 已提交
1147 1148 1149
	ret = snd_card_register(aaci->card);
	if (ret == 0) {
		dev_info(&dev->dev, "%s, fifo %d\n", aaci->card->longname,
K
Kevin Hilman 已提交
1150
			 aaci->fifosize);
R
Russell King 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		amba_set_drvdata(dev, aaci->card);
		return ret;
	}

 out:
	if (aaci)
		snd_card_free(aaci->card);
	amba_release_regions(dev);
	return ret;
}

static int __devexit aaci_remove(struct amba_device *dev)
{
1164
	struct snd_card *card = amba_get_drvdata(dev);
R
Russell King 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

	amba_set_drvdata(dev, NULL);

	if (card) {
		struct aaci *aaci = card->private_data;
		writel(0, aaci->base + AACI_MAINCR);

		snd_card_free(card);
		amba_release_regions(dev);
	}

	return 0;
}

static struct amba_id aaci_ids[] = {
	{
		.id	= 0x00041041,
		.mask	= 0x000fffff,
	},
	{ 0, 0 },
};

static struct amba_driver aaci_driver = {
	.drv		= {
		.name	= DRIVER_NAME,
	},
	.probe		= aaci_probe,
	.remove		= __devexit_p(aaci_remove),
	.suspend	= aaci_suspend,
	.resume		= aaci_resume,
	.id_table	= aaci_ids,
};

static int __init aaci_init(void)
{
	return amba_driver_register(&aaci_driver);
}

static void __exit aaci_exit(void)
{
	amba_driver_unregister(&aaci_driver);
}

module_init(aaci_init);
module_exit(aaci_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ARM PrimeCell PL041 Advanced Audio CODEC Interface driver");