au1x00.c 18.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * BRIEF MODULE DESCRIPTION
 *  Driver for AMD Au1000 MIPS Processor, AC'97 Sound Port
 *
 * Copyright 2004 Cooper Street Innovations Inc.
 * Author: Charles Eidsness	<charles@cooper-street.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.
 *
 *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
 *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
 *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You should have received a copy of the  GNU General Public License along
 *  with this program; if not, write  to the Free Software Foundation, Inc.,
 *  675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * History:
 *
 * 2004-09-09 Charles Eidsness	-- Original verion -- based on
 * 				  sa11xx-uda1341.c ALSA driver and the
 *				  au1000.c OSS driver.
 * 2004-09-09 Matt Porter	-- Added support for ALSA 1.0.6
 *
 */

#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <sound/driver.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/pcm.h>
46
#include <sound/pcm_params.h>
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <sound/ac97_codec.h>
#include <asm/mach-au1x00/au1000.h>
#include <asm/mach-au1x00/au1000_dma.h>

MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>");
MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}");

#define PLAYBACK 0
#define CAPTURE 1
#define AC97_SLOT_3 0x01
#define AC97_SLOT_4 0x02
#define AC97_SLOT_6 0x08
#define AC97_CMD_IRQ 31
#define READ 0
#define WRITE 1
#define READ_WAIT 2
#define RW_DONE 3

struct au1000_period
{
	u32 start;
	u32 relative_end;	/*realtive to start of buffer*/
71
	struct au1000_period * next;
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81 82 83
};

/*Au1000 AC97 Port Control Reisters*/
struct au1000_ac97_reg {
	u32 volatile config;
	u32 volatile status;
	u32 volatile data;
	u32 volatile cmd;
	u32 volatile cntrl;
};

struct audio_stream {
84
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
85 86
	int dma;
	spinlock_t dma_lock;
87
	struct au1000_period * buffer;
T
Takashi Iwai 已提交
88 89
	unsigned int period_size;
	unsigned int periods;
L
Linus Torvalds 已提交
90 91
};

92 93 94
struct snd_au1000 {
	struct snd_card *card;
	struct au1000_ac97_reg volatile *ac97_ioport;
L
Linus Torvalds 已提交
95 96 97

	struct resource *ac97_res_port;
	spinlock_t ac97_lock;
98
	struct snd_ac97 *ac97;
L
Linus Torvalds 已提交
99

100 101 102
	struct snd_pcm *pcm;
	struct audio_stream *stream[2];	/* playback & capture */
};
L
Linus Torvalds 已提交
103 104 105

/*--------------------------- Local Functions --------------------------------*/
static void
106
au1000_set_ac97_xmit_slots(struct snd_au1000 *au1000, long xmit_slots)
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116 117 118
{
	u32 volatile ac97_config;

	spin_lock(&au1000->ac97_lock);
	ac97_config = au1000->ac97_ioport->config;
	ac97_config = ac97_config & ~AC97C_XMIT_SLOTS_MASK;
	ac97_config |= (xmit_slots << AC97C_XMIT_SLOTS_BIT);
	au1000->ac97_ioport->config = ac97_config;
	spin_unlock(&au1000->ac97_lock);
}

static void
119
au1000_set_ac97_recv_slots(struct snd_au1000 *au1000, long recv_slots)
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132
{
	u32 volatile ac97_config;

	spin_lock(&au1000->ac97_lock);
	ac97_config = au1000->ac97_ioport->config;
	ac97_config = ac97_config & ~AC97C_RECV_SLOTS_MASK;
	ac97_config |= (recv_slots << AC97C_RECV_SLOTS_BIT);
	au1000->ac97_ioport->config = ac97_config;
	spin_unlock(&au1000->ac97_lock);
}


static void
133
au1000_release_dma_link(struct audio_stream *stream)
L
Linus Torvalds 已提交
134
{
135 136
	struct au1000_period * pointer;
	struct au1000_period * pointer_next;
L
Linus Torvalds 已提交
137

T
Takashi Iwai 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151
	stream->period_size = 0;
	stream->periods = 0;
	pointer = stream->buffer;
	if (! pointer)
		return;
	do {
		pointer_next = pointer->next;
		kfree(pointer);
		pointer = pointer_next;
	} while (pointer != stream->buffer);
	stream->buffer = NULL;
}

static int
152
au1000_setup_dma_link(struct audio_stream *stream, unsigned int period_bytes,
T
Takashi Iwai 已提交
153 154
		      unsigned int periods)
{
155 156
	struct snd_pcm_substream *substream = stream->substream;
	struct snd_pcm_runtime *runtime = substream->runtime;
157
	struct au1000_period *pointer;
T
Takashi Iwai 已提交
158 159 160 161
	unsigned long dma_start;
	int i;

	dma_start = virt_to_phys(runtime->dma_area);
L
Linus Torvalds 已提交
162

T
Takashi Iwai 已提交
163 164 165
	if (stream->period_size == period_bytes &&
	    stream->periods == periods)
		return 0; /* not changed */
L
Linus Torvalds 已提交
166

T
Takashi Iwai 已提交
167
	au1000_release_dma_link(stream);
L
Linus Torvalds 已提交
168

T
Takashi Iwai 已提交
169 170 171
	stream->period_size = period_bytes;
	stream->periods = periods;

172
	stream->buffer = kmalloc(sizeof(struct au1000_period), GFP_KERNEL);
T
Takashi Iwai 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186
	if (! stream->buffer)
		return -ENOMEM;
	pointer = stream->buffer;
	for (i = 0; i < periods; i++) {
		pointer->start = (u32)(dma_start + (i * period_bytes));
		pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1);
		if (i < periods - 1) {
			pointer->next = kmalloc(sizeof(struct au1000_period), GFP_KERNEL);
			if (! pointer->next) {
				au1000_release_dma_link(stream);
				return -ENOMEM;
			}
			pointer = pointer->next;
		}
L
Linus Torvalds 已提交
187
	}
T
Takashi Iwai 已提交
188 189
	pointer->next = stream->buffer;
	return 0;
L
Linus Torvalds 已提交
190 191 192
}

static void
193
au1000_dma_stop(struct audio_stream *stream)
L
Linus Torvalds 已提交
194
{
T
Takashi Iwai 已提交
195 196 197
	snd_assert(stream->buffer, return);
	disable_dma(stream->dma);
}
L
Linus Torvalds 已提交
198

T
Takashi Iwai 已提交
199
static void
200
au1000_dma_start(struct audio_stream *stream)
T
Takashi Iwai 已提交
201 202
{
	snd_assert(stream->buffer, return);
L
Linus Torvalds 已提交
203

T
Takashi Iwai 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
	init_dma(stream->dma);
	if (get_dma_active_buffer(stream->dma) == 0) {
		clear_dma_done0(stream->dma);
		set_dma_addr0(stream->dma, stream->buffer->start);
		set_dma_count0(stream->dma, stream->period_size >> 1);
		set_dma_addr1(stream->dma, stream->buffer->next->start);
		set_dma_count1(stream->dma, stream->period_size >> 1);
	} else {
		clear_dma_done1(stream->dma);
		set_dma_addr1(stream->dma, stream->buffer->start);
		set_dma_count1(stream->dma, stream->period_size >> 1);
		set_dma_addr0(stream->dma, stream->buffer->next->start);
		set_dma_count0(stream->dma, stream->period_size >> 1);
L
Linus Torvalds 已提交
217
	}
T
Takashi Iwai 已提交
218 219
	enable_dma_buffers(stream->dma);
	start_dma(stream->dma);
L
Linus Torvalds 已提交
220 221 222 223 224
}

static irqreturn_t
au1000_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
225 226
	struct audio_stream *stream = (struct audio_stream *) dev_id;
	struct snd_pcm_substream *substream = stream->substream;
L
Linus Torvalds 已提交
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

	spin_lock(&stream->dma_lock);
	switch (get_dma_buffer_done(stream->dma)) {
	case DMA_D0:
		stream->buffer = stream->buffer->next;
		clear_dma_done0(stream->dma);
		set_dma_addr0(stream->dma, stream->buffer->next->start);
		set_dma_count0(stream->dma, stream->period_size >> 1);
		enable_dma_buffer0(stream->dma);
		break;
	case DMA_D1:
		stream->buffer = stream->buffer->next;
		clear_dma_done1(stream->dma);
		set_dma_addr1(stream->dma, stream->buffer->next->start);
		set_dma_count1(stream->dma, stream->period_size >> 1);
		enable_dma_buffer1(stream->dma);
		break;
	case (DMA_D0 | DMA_D1):
		printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma);
		au1000_dma_stop(stream);
		au1000_dma_start(stream);
		break;
	case (~DMA_D0 & ~DMA_D1):
		printk(KERN_ERR "DMA %d empty irq.\n",stream->dma);
	}
	spin_unlock(&stream->dma_lock);
	snd_pcm_period_elapsed(substream);
	return IRQ_HANDLED;
}

/*-------------------------- PCM Audio Streams -------------------------------*/

static unsigned int rates[] = {8000, 11025, 16000, 22050};
260
static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
261
	.count	= ARRAY_SIZE(rates),
L
Linus Torvalds 已提交
262 263 264 265
	.list	= rates,
	.mask	= 0,
};

266
static struct snd_pcm_hardware snd_au1000_hw =
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
	.info			= (SNDRV_PCM_INFO_INTERLEAVED | \
				SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
	.formats		= SNDRV_PCM_FMTBIT_S16_LE,
	.rates			= (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
				SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050),
	.rate_min		= 8000,
	.rate_max		= 22050,
	.channels_min		= 1,
	.channels_max		= 2,
	.buffer_bytes_max	= 128*1024,
	.period_bytes_min	= 32,
	.period_bytes_max	= 16*1024,
	.periods_min		= 8,
	.periods_max		= 255,
	.fifo_size		= 16,
};

static int
286
snd_au1000_playback_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
287
{
288
	struct snd_au1000 *au1000 = substream->pcm->private_data;
T
Takashi Iwai 已提交
289

L
Linus Torvalds 已提交
290 291 292
	au1000->stream[PLAYBACK]->substream = substream;
	au1000->stream[PLAYBACK]->buffer = NULL;
	substream->private_data = au1000->stream[PLAYBACK];
T
Takashi Iwai 已提交
293
	substream->runtime->hw = snd_au1000_hw;
L
Linus Torvalds 已提交
294 295 296 297 298
	return (snd_pcm_hw_constraint_list(substream->runtime, 0,
		SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
}

static int
299
snd_au1000_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
300
{
301
	struct snd_au1000 *au1000 = substream->pcm->private_data;
T
Takashi Iwai 已提交
302

L
Linus Torvalds 已提交
303 304 305
	au1000->stream[CAPTURE]->substream = substream;
	au1000->stream[CAPTURE]->buffer = NULL;
	substream->private_data = au1000->stream[CAPTURE];
T
Takashi Iwai 已提交
306
	substream->runtime->hw = snd_au1000_hw;
L
Linus Torvalds 已提交
307 308 309 310 311
	return (snd_pcm_hw_constraint_list(substream->runtime, 0,
		SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
}

static int
312
snd_au1000_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
313
{
314
	struct snd_au1000 *au1000 = substream->pcm->private_data;
T
Takashi Iwai 已提交
315

L
Linus Torvalds 已提交
316 317 318 319 320
	au1000->stream[PLAYBACK]->substream = NULL;
	return 0;
}

static int
321
snd_au1000_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
322
{
323
	struct snd_au1000 *au1000 = substream->pcm->private_data;
T
Takashi Iwai 已提交
324

L
Linus Torvalds 已提交
325 326 327 328 329
	au1000->stream[CAPTURE]->substream = NULL;
	return 0;
}

static int
330 331
snd_au1000_hw_params(struct snd_pcm_substream *substream,
					struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
332
{
333
	struct audio_stream *stream = substream->private_data;
T
Takashi Iwai 已提交
334 335 336 337 338 339 340 341 342
	int err;

	err = snd_pcm_lib_malloc_pages(substream,
				       params_buffer_bytes(hw_params));
	if (err < 0)
		return err;
	return au1000_setup_dma_link(stream,
				     params_period_bytes(hw_params),
				     params_periods(hw_params));
L
Linus Torvalds 已提交
343 344 345
}

static int
346
snd_au1000_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
347
{
348
	struct audio_stream *stream = substream->private_data;
T
Takashi Iwai 已提交
349
	au1000_release_dma_link(stream);
L
Linus Torvalds 已提交
350 351 352 353
	return snd_pcm_lib_free_pages(substream);
}

static int
354
snd_au1000_playback_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
355
{
356 357
	struct snd_au1000 *au1000 = substream->pcm->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
358

T
Takashi Iwai 已提交
359 360
	if (runtime->channels == 1)
		au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_4);
L
Linus Torvalds 已提交
361
	else
T
Takashi Iwai 已提交
362
		au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
L
Linus Torvalds 已提交
363 364 365 366 367
	snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
	return 0;
}

static int
368
snd_au1000_capture_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
369
{
370 371
	struct snd_au1000 *au1000 = substream->pcm->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
372

T
Takashi Iwai 已提交
373 374
	if (runtime->channels == 1)
		au1000_set_ac97_recv_slots(au1000, AC97_SLOT_4);
L
Linus Torvalds 已提交
375
	else
T
Takashi Iwai 已提交
376
		au1000_set_ac97_recv_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
L
Linus Torvalds 已提交
377 378 379 380 381
	snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
	return 0;
}

static int
382
snd_au1000_trigger(struct snd_pcm_substream *substream, int cmd)
L
Linus Torvalds 已提交
383
{
384
	struct audio_stream *stream = substream->private_data;
L
Linus Torvalds 已提交
385 386
	int err = 0;

T
Takashi Iwai 已提交
387
	spin_lock(&stream->dma_lock);
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		au1000_dma_start(stream);
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		au1000_dma_stop(stream);
		break;
	default:
		err = -EINVAL;
		break;
	}
T
Takashi Iwai 已提交
399
	spin_unlock(&stream->dma_lock);
L
Linus Torvalds 已提交
400 401 402 403
	return err;
}

static snd_pcm_uframes_t
404
snd_au1000_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
405
{
406 407
	struct audio_stream *stream = substream->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
408 409
	long location;

T
Takashi Iwai 已提交
410
	spin_lock(&stream->dma_lock);
L
Linus Torvalds 已提交
411
	location = get_dma_residue(stream->dma);
T
Takashi Iwai 已提交
412
	spin_unlock(&stream->dma_lock);
L
Linus Torvalds 已提交
413 414 415 416 417 418
	location = stream->buffer->relative_end - location;
	if (location == -1)
		location = 0;
	return bytes_to_frames(runtime,location);
}

419
static struct snd_pcm_ops snd_card_au1000_playback_ops = {
L
Linus Torvalds 已提交
420 421 422 423 424 425 426 427 428 429
	.open			= snd_au1000_playback_open,
	.close			= snd_au1000_playback_close,
	.ioctl			= snd_pcm_lib_ioctl,
	.hw_params	        = snd_au1000_hw_params,
	.hw_free	        = snd_au1000_hw_free,
	.prepare		= snd_au1000_playback_prepare,
	.trigger		= snd_au1000_trigger,
	.pointer		= snd_au1000_pointer,
};

430
static struct snd_pcm_ops snd_card_au1000_capture_ops = {
L
Linus Torvalds 已提交
431 432 433 434 435 436 437 438 439 440 441
	.open			= snd_au1000_capture_open,
	.close			= snd_au1000_capture_close,
	.ioctl			= snd_pcm_lib_ioctl,
	.hw_params	        = snd_au1000_hw_params,
	.hw_free	        = snd_au1000_hw_free,
	.prepare		= snd_au1000_capture_prepare,
	.trigger		= snd_au1000_trigger,
	.pointer		= snd_au1000_pointer,
};

static int __devinit
442
snd_au1000_pcm_new(struct snd_au1000 *au1000)
L
Linus Torvalds 已提交
443
{
444
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	int err;
	unsigned long flags;

	if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0)
		return err;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
		snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024);

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
		&snd_card_au1000_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
		&snd_card_au1000_capture_ops);

	pcm->private_data = au1000;
	pcm->info_flags = 0;
	strcpy(pcm->name, "Au1000 AC97 PCM");

T
Takashi Iwai 已提交
463 464 465
	spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock);
	spin_lock_init(&au1000->stream[CAPTURE]->dma_lock);

L
Linus Torvalds 已提交
466 467
	flags = claim_dma_lock();
	if ((au1000->stream[PLAYBACK]->dma = request_au1000_dma(DMA_ID_AC97C_TX,
468
			"AC97 TX", au1000_dma_interrupt, IRQF_DISABLED,
L
Linus Torvalds 已提交
469 470 471 472 473
			au1000->stream[PLAYBACK])) < 0) {
		release_dma_lock(flags);
		return -EBUSY;
	}
	if ((au1000->stream[CAPTURE]->dma = request_au1000_dma(DMA_ID_AC97C_RX,
474
			"AC97 RX", au1000_dma_interrupt, IRQF_DISABLED,
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
			au1000->stream[CAPTURE])) < 0){
		release_dma_lock(flags);
		return -EBUSY;
	}
	/* enable DMA coherency in read/write DMA channels */
	set_dma_mode(au1000->stream[PLAYBACK]->dma,
		     get_dma_mode(au1000->stream[PLAYBACK]->dma) & ~DMA_NC);
	set_dma_mode(au1000->stream[CAPTURE]->dma,
		     get_dma_mode(au1000->stream[CAPTURE]->dma) & ~DMA_NC);
	release_dma_lock(flags);
	au1000->pcm = pcm;
	return 0;
}


/*-------------------------- AC97 CODEC Control ------------------------------*/

static unsigned short
493
snd_au1000_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
L
Linus Torvalds 已提交
494
{
495
	struct snd_au1000 *au1000 = ac97->private_data;
L
Linus Torvalds 已提交
496 497 498
	u32 volatile cmd;
	u16 volatile data;
	int             i;
T
Takashi Iwai 已提交
499

500
	spin_lock(&au1000->ac97_lock);
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
/* would rather use the interupt than this polling but it works and I can't
get the interupt driven case to work efficiently */
	for (i = 0; i < 0x5000; i++)
		if (!(au1000->ac97_ioport->status & AC97C_CP))
			break;
	if (i == 0x5000)
		printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");

	cmd = (u32) reg & AC97C_INDEX_MASK;
	cmd |= AC97C_READ;
	au1000->ac97_ioport->cmd = cmd;

	/* now wait for the data */
	for (i = 0; i < 0x5000; i++)
		if (!(au1000->ac97_ioport->status & AC97C_CP))
			break;
	if (i == 0x5000) {
		printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
		return 0;
	}

	data = au1000->ac97_ioport->cmd & 0xffff;
523
	spin_unlock(&au1000->ac97_lock);
L
Linus Torvalds 已提交
524 525 526 527 528 529 530

	return data;

}


static void
531
snd_au1000_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
L
Linus Torvalds 已提交
532
{
533
	struct snd_au1000 *au1000 = ac97->private_data;
L
Linus Torvalds 已提交
534 535
	u32 cmd;
	int i;
T
Takashi Iwai 已提交
536

537
	spin_lock(&au1000->ac97_lock);
L
Linus Torvalds 已提交
538 539 540 541 542 543 544 545 546 547 548 549
/* would rather use the interupt than this polling but it works and I can't
get the interupt driven case to work efficiently */
	for (i = 0; i < 0x5000; i++)
		if (!(au1000->ac97_ioport->status & AC97C_CP))
			break;
	if (i == 0x5000)
		printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n");

	cmd = (u32) reg & AC97C_INDEX_MASK;
	cmd &= ~AC97C_READ;
	cmd |= ((u32) val << AC97C_WD_BIT);
	au1000->ac97_ioport->cmd = cmd;
550
	spin_unlock(&au1000->ac97_lock);
L
Linus Torvalds 已提交
551 552 553
}

static int __devinit
554
snd_au1000_ac97_new(struct snd_au1000 *au1000)
L
Linus Torvalds 已提交
555 556
{
	int err;
557 558 559 560 561 562
	struct snd_ac97_bus *pbus;
	struct snd_ac97_template ac97;
 	static struct snd_ac97_bus_ops ops = {
		.write = snd_au1000_ac97_write,
		.read = snd_au1000_ac97_read,
	};
L
Linus Torvalds 已提交
563

564 565
	if ((au1000->ac97_res_port = request_mem_region(CPHYSADDR(AC97C_CONFIG),
	       		0x100000, "Au1x00 AC97")) == NULL) {
L
Linus Torvalds 已提交
566 567 568
		snd_printk(KERN_ERR "ALSA AC97: can't grap AC97 port\n");
		return -EBUSY;
	}
569 570
	au1000->ac97_ioport = (struct au1000_ac97_reg *)
		KSEG1ADDR(au1000->ac97_res_port->start);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

	spin_lock_init(&au1000->ac97_lock);

	/* configure pins for AC'97
	TODO: move to board_setup.c */
	au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);

	/* Initialise Au1000's AC'97 Control Block */
	au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE;
	udelay(10);
	au1000->ac97_ioport->cntrl = AC97C_CE;
	udelay(10);

	/* Initialise External CODEC -- cold reset */
	au1000->ac97_ioport->config = AC97C_RESET;
	udelay(10);
	au1000->ac97_ioport->config = 0x0;
	mdelay(5);

	/* Initialise AC97 middle-layer */
	if ((err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus)) < 0)
 		return err;
T
Takashi Iwai 已提交
593

L
Linus Torvalds 已提交
594 595 596 597 598
	memset(&ac97, 0, sizeof(ac97));
	ac97.private_data = au1000;
	if ((err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97)) < 0)
		return err;

T
Takashi Iwai 已提交
599
	return 0;
L
Linus Torvalds 已提交
600 601 602 603 604
}

/*------------------------------ Setup / Destroy ----------------------------*/

void
605
snd_au1000_free(struct snd_card *card)
L
Linus Torvalds 已提交
606
{
607
	struct snd_au1000 *au1000 = card->private_data;
L
Linus Torvalds 已提交
608 609 610 611 612

	if (au1000->ac97_res_port) {
		/* put internal AC97 block into reset */
		au1000->ac97_ioport->cntrl = AC97C_RS;
		au1000->ac97_ioport = NULL;
613
		release_and_free_resource(au1000->ac97_res_port);
L
Linus Torvalds 已提交
614 615
	}

616 617 618 619 620
	if (au1000->stream[PLAYBACK]) {
	  	if (au1000->stream[PLAYBACK]->dma >= 0)
			free_au1000_dma(au1000->stream[PLAYBACK]->dma);
		kfree(au1000->stream[PLAYBACK]);
	}
L
Linus Torvalds 已提交
621

622 623 624 625 626
	if (au1000->stream[CAPTURE]) {
		if (au1000->stream[CAPTURE]->dma >= 0)
			free_au1000_dma(au1000->stream[CAPTURE]->dma);
		kfree(au1000->stream[CAPTURE]);
	}
L
Linus Torvalds 已提交
627 628
}

T
Takashi Iwai 已提交
629

630
static struct snd_card *au1000_card;
T
Takashi Iwai 已提交
631

L
Linus Torvalds 已提交
632 633 634 635
static int __init
au1000_init(void)
{
	int err;
636 637
	struct snd_card *card;
	struct snd_au1000 *au1000;
L
Linus Torvalds 已提交
638

639
	card = snd_card_new(-1, "AC97", THIS_MODULE, sizeof(struct snd_au1000));
T
Takashi Iwai 已提交
640
	if (card == NULL)
L
Linus Torvalds 已提交
641
		return -ENOMEM;
T
Takashi Iwai 已提交
642 643 644 645

	card->private_free = snd_au1000_free;
	au1000 = card->private_data;
	au1000->card = card;
646

647
	au1000->stream[PLAYBACK] = kmalloc(sizeof(struct audio_stream), GFP_KERNEL);
648 649 650 651 652 653 654 655
	au1000->stream[CAPTURE ] = kmalloc(sizeof(struct audio_stream), GFP_KERNEL);
	/* so that snd_au1000_free will work as intended */
 	au1000->ac97_res_port = NULL;
	if (au1000->stream[PLAYBACK])
		au1000->stream[PLAYBACK]->dma = -1;
	if (au1000->stream[CAPTURE ])
		au1000->stream[CAPTURE ]->dma = -1;

T
Takashi Iwai 已提交
656
	if (au1000->stream[PLAYBACK] == NULL ||
657
	    au1000->stream[CAPTURE ] == NULL) {
T
Takashi Iwai 已提交
658
		snd_card_free(card);
L
Linus Torvalds 已提交
659 660 661
		return -ENOMEM;
	}

T
Takashi Iwai 已提交
662 663
	if ((err = snd_au1000_ac97_new(au1000)) < 0 ) {
		snd_card_free(card);
L
Linus Torvalds 已提交
664 665 666
		return err;
	}

T
Takashi Iwai 已提交
667 668
	if ((err = snd_au1000_pcm_new(au1000)) < 0) {
		snd_card_free(card);
L
Linus Torvalds 已提交
669 670 671
		return err;
	}

T
Takashi Iwai 已提交
672 673 674
	strcpy(card->driver, "Au1000-AC97");
	strcpy(card->shortname, "AMD Au1000-AC97");
	sprintf(card->longname, "AMD Au1000--AC97 ALSA Driver");
L
Linus Torvalds 已提交
675

T
Takashi Iwai 已提交
676 677
	if ((err = snd_card_register(card)) < 0) {
		snd_card_free(card);
L
Linus Torvalds 已提交
678 679 680 681
		return err;
	}

	printk( KERN_INFO "ALSA AC97: Driver Initialized\n" );
T
Takashi Iwai 已提交
682
	au1000_card = card;
L
Linus Torvalds 已提交
683 684 685 686 687
	return 0;
}

static void __exit au1000_exit(void)
{
T
Takashi Iwai 已提交
688
	snd_card_free(au1000_card);
L
Linus Torvalds 已提交
689 690 691 692 693
}

module_init(au1000_init);
module_exit(au1000_exit);