ali5451.c 59.7 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 46 47
/*
 *  Matt Wu <Matt_Wu@acersoftech.com.cn>
 *  Apr 26, 2001
 *  Routines for control of ALi pci audio M5451
 *
 *  BUGS:
 *    --
 *
 *  TODO:
 *    --
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public Lcodecnse as published by
 *   the Free Software Foundation; either version 2 of the Lcodecnse, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public Lcodecnse for more details.
 *
 *   You should have received a copy of the GNU General Public Lcodecnse
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <sound/driver.h>
#include <asm/io.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/moduleparam.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/info.h>
#include <sound/ac97_codec.h>
#include <sound/mpu401.h>
#include <sound/initval.h>

MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
MODULE_DESCRIPTION("ALI M5451");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");

48 49 50 51
static int index = SNDRV_DEFAULT_IDX1;	/* Index */
static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
static int pcm_channels = 32;
static int spdif = 0;
L
Linus Torvalds 已提交
52

53
module_param(index, int, 0444);
L
Linus Torvalds 已提交
54
MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
55
module_param(id, charp, 0444);
L
Linus Torvalds 已提交
56
MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
57
module_param(pcm_channels, int, 0444);
L
Linus Torvalds 已提交
58
MODULE_PARM_DESC(pcm_channels, "PCM Channels");
59
module_param(spdif, bool, 0444);
L
Linus Torvalds 已提交
60 61
MODULE_PARM_DESC(spdif, "Support SPDIF I/O");

62 63 64 65 66
/* just for backward compatibility */
static int enable;
module_param(enable, bool, 0444);


L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/*
 *  Debug part definitions
 */

//#define ALI_DEBUG

#ifdef ALI_DEBUG
#define snd_ali_printk(format, args...) printk(format, ##args);
#else
#define snd_ali_printk(format, args...)
#endif

/*
 *  Constants definition
 */

83
#define DEVICE_ID_ALI5451	((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92 93 94


#define ALI_CHANNELS		32

#define ALI_PCM_IN_CHANNEL	31
#define ALI_SPDIF_IN_CHANNEL	19
#define ALI_SPDIF_OUT_CHANNEL	15
#define ALI_CENTER_CHANNEL	24
#define ALI_LEF_CHANNEL		23
#define ALI_SURR_LEFT_CHANNEL	26
#define ALI_SURR_RIGHT_CHANNEL	25
95 96
#define ALI_MODEM_IN_CHANNEL    21
#define ALI_MODEM_OUT_CHANNEL   20
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

#define	SNDRV_ALI_VOICE_TYPE_PCM	01
#define SNDRV_ALI_VOICE_TYPE_OTH	02

#define	ALI_5451_V02		0x02

/*
 *  Direct Registers
 */

#define ALI_LEGACY_DMAR0        0x00  // ADR0
#define ALI_LEGACY_DMAR4        0x04  // CNT0
#define ALI_LEGACY_DMAR11       0x0b  // MOD 
#define ALI_LEGACY_DMAR15       0x0f  // MMR 
#define ALI_MPUR0		0x20
#define ALI_MPUR1		0x21
#define ALI_MPUR2		0x22
#define ALI_MPUR3		0x23

#define	ALI_AC97_WRITE		0x40
#define ALI_AC97_READ		0x44

#define ALI_SCTRL		0x48
#define   ALI_SPDIF_OUT_ENABLE		0x20
121 122 123 124 125 126
#define   ALI_SCTRL_LINE_IN2		(1 << 9)
#define   ALI_SCTRL_GPIO_IN2		(1 << 13)
#define   ALI_SCTRL_LINE_OUT_EN 	(1 << 20)
#define   ALI_SCTRL_GPIO_OUT_EN 	(1 << 23)
#define   ALI_SCTRL_CODEC1_READY	(1 << 24)
#define   ALI_SCTRL_CODEC2_READY	(1 << 25)
L
Linus Torvalds 已提交
127
#define ALI_AC97_GPIO		0x4c
128 129
#define   ALI_AC97_GPIO_ENABLE		0x8000
#define   ALI_AC97_GPIO_DATA_SHIFT	16
L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#define ALI_SPDIF_CS		0x70
#define ALI_SPDIF_CTRL		0x74
#define   ALI_SPDIF_IN_FUNC_ENABLE	0x02
#define   ALI_SPDIF_IN_CH_STATUS	0x40
#define   ALI_SPDIF_OUT_CH_STATUS	0xbf
#define ALI_START		0x80
#define ALI_STOP		0x84
#define ALI_CSPF		0x90
#define ALI_AINT		0x98
#define ALI_GC_CIR		0xa0
	#define ENDLP_IE		0x00001000
	#define MIDLP_IE		0x00002000
#define ALI_AINTEN		0xa4
#define ALI_VOLUME		0xa8
#define ALI_SBDELTA_DELTA_R     0xac
#define ALI_MISCINT		0xb0
	#define ADDRESS_IRQ		0x00000020
	#define TARGET_REACHED		0x00008000
	#define MIXER_OVERFLOW		0x00000800
	#define MIXER_UNDERFLOW		0x00000400
150
	#define GPIO_IRQ		0x01000000
L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#define ALI_SBBL_SBCL           0xc0
#define ALI_SBCTRL_SBE2R_SBDD   0xc4
#define ALI_STIMER		0xc8
#define ALI_GLOBAL_CONTROL	0xd4
#define   ALI_SPDIF_OUT_SEL_PCM		0x00000400 /* bit 10 */
#define   ALI_SPDIF_IN_SUPPORT		0x00000800 /* bit 11 */
#define   ALI_SPDIF_OUT_CH_ENABLE	0x00008000 /* bit 15 */
#define   ALI_SPDIF_IN_CH_ENABLE	0x00080000 /* bit 19 */
#define   ALI_PCM_IN_ENABLE		0x80000000 /* bit 31 */

#define ALI_CSO_ALPHA_FMS	0xe0
#define ALI_LBA			0xe4
#define ALI_ESO_DELTA		0xe8
#define ALI_GVSEL_PAN_VOC_CTRL_EC	0xf0
#define ALI_EBUF1		0xf4
#define ALI_EBUF2		0xf8

#define ALI_REG(codec, x) ((codec)->port + x)

170 171 172
#define MAX_CODECS 2


173 174
struct snd_ali;
struct snd_ali_voice;
L
Linus Torvalds 已提交
175

176
struct snd_ali_channel_control {
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	// register data
	struct REGDATA {
		unsigned int start;
		unsigned int stop;
		unsigned int aint;
		unsigned int ainten;
	} data;
		
	// register addresses
	struct REGS {
		unsigned int start;
		unsigned int stop;
		unsigned int aint;
		unsigned int ainten;
		unsigned int ac97read;
		unsigned int ac97write;
	} regs;

195
};
L
Linus Torvalds 已提交
196

197
struct snd_ali_voice {
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205
	unsigned int number;
	unsigned int use: 1,
	    pcm: 1,
	    midi: 1,
	    mode: 1,
	    synth: 1;

	/* PCM data */
206 207 208
	struct snd_ali *codec;
	struct snd_pcm_substream *substream;
	struct snd_ali_voice *extra;
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221
	
	unsigned int running: 1;

	int eso;                /* final ESO value for channel */
	int count;              /* runtime->period_size */

	/* --- */

	void *private_data;
	void (*private_free)(void *private_data);
};


222
struct snd_alidev {
L
Linus Torvalds 已提交
223

224
	struct snd_ali_voice voices[ALI_CHANNELS];	
L
Linus Torvalds 已提交
225 226 227 228 229

	unsigned int	chcnt;			/* num of opened channels */
	unsigned int	chmap;			/* bitmap for opened channels */
	unsigned int synthcount;

230
};
L
Linus Torvalds 已提交
231 232 233 234 235


#ifdef CONFIG_PM
#define ALI_GLOBAL_REGS		56
#define ALI_CHANNEL_REGS	8
236
struct snd_ali_image {
L
Linus Torvalds 已提交
237 238
	unsigned long regs[ALI_GLOBAL_REGS];
	unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
239
};
L
Linus Torvalds 已提交
240 241 242
#endif


243
struct snd_ali {
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253 254
	unsigned long	irq;
	unsigned long	port;
	unsigned char	revision;

	unsigned int hw_initialized: 1;
	unsigned int spdif_support: 1;

	struct pci_dev	*pci;
	struct pci_dev	*pci_m1533;
	struct pci_dev	*pci_m7101;

255 256 257 258
	struct snd_card	*card;
	struct snd_pcm	*pcm[MAX_CODECS];
	struct snd_alidev	synth;
	struct snd_ali_channel_control chregs;
L
Linus Torvalds 已提交
259 260 261 262 263 264 265

	/* S/PDIF Mask */
	unsigned int	spdif_mask;

	unsigned int spurious_irq_count;
	unsigned int spurious_irq_max_delta;

266 267
	unsigned int num_of_codecs;

268 269
	struct snd_ac97_bus *ac97_bus;
	struct snd_ac97 *ac97[MAX_CODECS];
L
Linus Torvalds 已提交
270 271 272 273 274 275 276
	unsigned short	ac97_ext_id;
	unsigned short	ac97_ext_status;

	spinlock_t	reg_lock;
	spinlock_t	voice_alloc;

#ifdef CONFIG_PM
277
	struct snd_ali_image *image;
L
Linus Torvalds 已提交
278 279 280 281
#endif
};

static struct pci_device_id snd_ali_ids[] = {
282
	{PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
L
Linus Torvalds 已提交
283 284 285 286
	{0, }
};
MODULE_DEVICE_TABLE(pci, snd_ali_ids);

287 288 289
static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short, unsigned short);
L
Linus Torvalds 已提交
290 291 292 293 294 295 296

/*
 *  Debug Part
 */

#ifdef ALI_DEBUG

297
static void ali_read_regs(struct snd_ali *codec, int channel)
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
{
	int i,j;
	unsigned int dwVal;

	printk("channel %d registers map:\n", channel);
	outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));

	printk("    ");
	for(j=0;j<8;j++)
		printk("%2.2x       ", j*4);
	printk("\n");

	for (i=0; i<=0xf8/4;i++) {
		if(i%8 == 0)
			printk("%2.2x  ", (i*4/0x10)*0x10);
		dwVal = inl(ALI_REG(codec,i*4));
		printk("%8.8x ", dwVal);
		if ((i+1)%8 == 0)
			printk("\n");
	}
	printk("\n");
}
static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
{
	unsigned int dwVal;
J
Jiri Slaby 已提交
323
	struct pci_dev *pci_dev;
L
Linus Torvalds 已提交
324 325
	int i,j;

J
Jiri Slaby 已提交
326 327 328
	pci_dev = pci_get_device(vendor, deviceid, NULL);
	if (pci_dev == NULL)
		return ;
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

	printk("\nM%x PCI CFG\n", deviceid);
	printk("    ");
	for(j=0;j<8;j++)
		printk("%d        ",j);
	printk("\n");

	for(i=0;i<8;i++) {
		printk("%d   ",i);
		for(j=0;j<8;j++)
		{
			pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
			printk("%8.8x ", dwVal);
		}
		printk("\n");
	}
J
Jiri Slaby 已提交
345
	pci_dev_put(pci_dev);
L
Linus Torvalds 已提交
346
 }
347
static void ali_read_ac97regs(struct snd_ali *codec, int secondary)
L
Linus Torvalds 已提交
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 374 375
{
	unsigned short i,j;
	unsigned short wVal;

	printk("\ncodec %d registers map:\n", secondary);

	printk("    ");
	for(j=0;j<8;j++)
		printk("%2.2x   ",j*2);
	printk("\n");

	for (i=0; i<64;i++) {
		if(i%8 == 0)
			printk("%2.2x  ", (i/8)*0x10);
		wVal = snd_ali_codec_peek(codec, secondary, i*2);
		printk("%4.4x ", wVal);
		if ((i+1)%8 == 0)
			printk("\n");
	}
	printk("\n");
}

#endif

/*
 *  AC97 ACCESS
 */

376
static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
L
Linus Torvalds 已提交
377 378 379 380 381
						unsigned int port )
{
	return (unsigned int)inl(ALI_REG(codec, port)); 
}

382
static inline void snd_ali_5451_poke(	struct snd_ali *codec,
L
Linus Torvalds 已提交
383 384 385 386 387 388
					unsigned int port,
					unsigned int val )
{
	outl((unsigned int)val, ALI_REG(codec, port));
}

389
static int snd_ali_codec_ready(	struct snd_ali *codec,
390
				unsigned int port )
L
Linus Torvalds 已提交
391 392 393 394
{
	unsigned long end_time;
	unsigned int res;
	
395
	end_time = jiffies + msecs_to_jiffies(250);
L
Linus Torvalds 已提交
396 397 398 399
	do {
		res = snd_ali_5451_peek(codec,port);
		if (! (res & 0x8000))
			return 0;
400
		schedule_timeout_uninterruptible(1);
L
Linus Torvalds 已提交
401 402 403 404 405 406
	} while (time_after_eq(end_time, jiffies));
	snd_ali_5451_poke(codec, port, res & ~0x8000);
	snd_printdd("ali_codec_ready: codec is not ready.\n ");
	return -EIO;
}

407
static int snd_ali_stimer_ready(struct snd_ali *codec)
L
Linus Torvalds 已提交
408 409 410 411 412 413 414
{
	unsigned long end_time;
	unsigned long dwChk1,dwChk2;
	
	dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
	dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);

415
	end_time = jiffies + msecs_to_jiffies(250);
L
Linus Torvalds 已提交
416 417 418 419
	do {
		dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
		if (dwChk2 != dwChk1)
			return 0;
420
		schedule_timeout_uninterruptible(1);
L
Linus Torvalds 已提交
421
	} while (time_after_eq(end_time, jiffies));
422
	snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
L
Linus Torvalds 已提交
423 424 425
	return -EIO;
}

426
static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
L
Linus Torvalds 已提交
427 428 429 430 431 432 433
				     unsigned short reg,
				     unsigned short val)
{
	unsigned int dwVal = 0;
	unsigned int port = 0;

	if (reg >= 0x80) {
434
		snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
L
Linus Torvalds 已提交
435 436 437 438 439
		return;
	}

	port = codec->chregs.regs.ac97write;

440
	if (snd_ali_codec_ready(codec, port) < 0)
L
Linus Torvalds 已提交
441
		return;
442
	if (snd_ali_stimer_ready(codec) < 0)
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453 454
		return;

	dwVal  = (unsigned int) (reg & 0xff);
	dwVal |= 0x8000 | (val << 16);
	if (secondary) dwVal |= 0x0080;
	if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;

	snd_ali_5451_poke(codec,port,dwVal);

	return ;
}

455
static unsigned short snd_ali_codec_peek( struct snd_ali *codec,
L
Linus Torvalds 已提交
456 457 458 459 460 461 462
					  int secondary,
					  unsigned short reg)
{
	unsigned int dwVal = 0;
	unsigned int port = 0;

	if (reg >= 0x80) {
463
		snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
L
Linus Torvalds 已提交
464 465 466 467 468
		return ~0;
	}

	port = codec->chregs.regs.ac97read;

469
	if (snd_ali_codec_ready(codec, port) < 0)
L
Linus Torvalds 已提交
470
		return ~0;
471
	if (snd_ali_stimer_ready(codec) < 0)
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479
		return ~0;

	dwVal  = (unsigned int) (reg & 0xff);
	dwVal |= 0x8000;				/* bit 15*/
	if (secondary) dwVal |= 0x0080;

	snd_ali_5451_poke(codec, port, dwVal);

480
	if (snd_ali_stimer_ready(codec) < 0)
L
Linus Torvalds 已提交
481
		return ~0;
482
	if (snd_ali_codec_ready(codec, port) < 0)
L
Linus Torvalds 已提交
483 484 485 486 487
		return ~0;
	
	return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
}

488
static void snd_ali_codec_write(struct snd_ac97 *ac97,
L
Linus Torvalds 已提交
489 490 491
				unsigned short reg,
				unsigned short val )
{
492
	struct snd_ali *codec = ac97->private_data;
L
Linus Torvalds 已提交
493 494

	snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
495 496 497 498 499 500
	if(reg == AC97_GPIO_STATUS) {
		outl((val << ALI_AC97_GPIO_DATA_SHIFT)|ALI_AC97_GPIO_ENABLE,
			ALI_REG(codec, ALI_AC97_GPIO));
		return;
	}
	snd_ali_codec_poke(codec, ac97->num, reg, val);
L
Linus Torvalds 已提交
501 502 503 504
	return ;
}


505
static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97, unsigned short reg)
L
Linus Torvalds 已提交
506
{
507
	struct snd_ali *codec = ac97->private_data;
L
Linus Torvalds 已提交
508 509

	snd_ali_printk("codec_read reg=%xh.\n", reg);
510
	return (snd_ali_codec_peek(codec, ac97->num, reg));
L
Linus Torvalds 已提交
511 512 513 514 515 516
}

/*
 *	AC97 Reset
 */

517
static int snd_ali_reset_5451(struct snd_ali *codec)
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
{
	struct pci_dev *pci_dev = NULL;
	unsigned short wCount, wReg;
	unsigned int   dwVal;
	
	if ((pci_dev = codec->pci_m1533) != NULL) {
		pci_read_config_dword(pci_dev, 0x7c, &dwVal);
		pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
		udelay(5000);
		pci_read_config_dword(pci_dev, 0x7c, &dwVal);
		pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
		udelay(5000);
	}
	
	pci_dev = codec->pci;
	pci_read_config_dword(pci_dev, 0x44, &dwVal);
	pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
	udelay(500);
	pci_read_config_dword(pci_dev, 0x44, &dwVal);
	pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
	udelay(5000);
	
	wCount = 200;
	while(wCount--) {
		wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
		if((wReg & 0x000f) == 0x000f)
			return 0;
		udelay(5000);
	}

	/* non-fatal if you have a non PM capable codec */
	/* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
	return 0;
}

#ifdef CODEC_RESET

555
static int snd_ali_reset_codec(struct snd_ali *codec)
L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
{
	struct pci_dev *pci_dev = NULL;
	unsigned char bVal = 0;
	unsigned int   dwVal;
	unsigned short wCount, wReg;

	pci_dev = codec->pci_m1533;
	
	pci_read_config_dword(pci_dev, 0x7c, &dwVal);
	pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
	udelay(5000);
	pci_read_config_dword(pci_dev, 0x7c, &dwVal);
	pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
	udelay(5000);

	bVal = inb(ALI_REG(codec,ALI_SCTRL));
	bVal |= 0x02;
	outb(ALI_REG(codec,ALI_SCTRL),bVal);
	udelay(5000);
	bVal = inb(ALI_REG(codec,ALI_SCTRL));
	bVal &= 0xfd;
	outb(ALI_REG(codec,ALI_SCTRL),bVal);
	udelay(15000);

	wCount = 200;
	while(wCount--) {
		wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
		if((wReg & 0x000f) == 0x000f)
			return 0;
		udelay(5000);
	}
	return -1;
}

#endif

/*
 *  ALI 5451 Controller
 */

596
static void snd_ali_enable_special_channel(struct snd_ali *codec, unsigned int channel)
L
Linus Torvalds 已提交
597 598 599 600 601 602 603 604
{
	unsigned long dwVal = 0;

	dwVal  = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
	dwVal |= 1 << (channel & 0x0000001f);
	outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
}

605
static void snd_ali_disable_special_channel(struct snd_ali *codec, unsigned int channel)
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613
{
	unsigned long dwVal = 0;

	dwVal  = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
	dwVal &= ~(1 << (channel & 0x0000001f));
	outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
}

614
static void snd_ali_enable_address_interrupt(struct snd_ali * codec)
L
Linus Torvalds 已提交
615 616 617 618 619 620 621 622 623
{
	unsigned int gc;

	gc  = inl(ALI_REG(codec, ALI_GC_CIR));
	gc |= ENDLP_IE;
	gc |= MIDLP_IE;
	outl( gc, ALI_REG(codec, ALI_GC_CIR));
}

624
static void snd_ali_disable_address_interrupt(struct snd_ali * codec)
L
Linus Torvalds 已提交
625 626 627 628 629 630 631 632 633 634
{
	unsigned int gc;

	gc  = inl(ALI_REG(codec, ALI_GC_CIR));
	gc &= ~ENDLP_IE;
	gc &= ~MIDLP_IE;
	outl(gc, ALI_REG(codec, ALI_GC_CIR));
}

#if 0 // not used
635
static void snd_ali_enable_voice_irq(struct snd_ali *codec, unsigned int channel)
L
Linus Torvalds 已提交
636 637
{
	unsigned int mask;
638
	struct snd_ali_channel_control *pchregs = &(codec->chregs);
L
Linus Torvalds 已提交
639 640 641 642 643 644 645 646 647 648

	snd_ali_printk("enable_voice_irq channel=%d\n",channel);
	
	mask = 1 << (channel & 0x1f);
	pchregs->data.ainten  = inl(ALI_REG(codec,pchregs->regs.ainten));
	pchregs->data.ainten |= mask;
	outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
}
#endif

649
static void snd_ali_disable_voice_irq(struct snd_ali *codec, unsigned int channel)
L
Linus Torvalds 已提交
650 651
{
	unsigned int mask;
652
	struct snd_ali_channel_control *pchregs = &(codec->chregs);
L
Linus Torvalds 已提交
653 654 655 656 657 658 659 660 661

	snd_ali_printk("disable_voice_irq channel=%d\n",channel);

	mask = 1 << (channel & 0x1f);
	pchregs->data.ainten  = inl(ALI_REG(codec,pchregs->regs.ainten));
	pchregs->data.ainten &= ~mask;
	outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
}

662
static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
L
Linus Torvalds 已提交
663 664 665 666
{
	unsigned int idx =  channel & 0x1f;

	if (codec->synth.chcnt >= ALI_CHANNELS){
667
		snd_printk(KERN_ERR "ali_alloc_pcm_channel: no free channels.\n");
L
Linus Torvalds 已提交
668 669 670 671 672 673 674 675 676 677 678 679
		return -1;
	}

	if (!(codec->synth.chmap & (1 << idx))) {
		codec->synth.chmap |= 1 << idx;
		codec->synth.chcnt++;
		snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
		return idx;
	}
	return -1;
}

680
static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
L
Linus Torvalds 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
{
	int idx;
	int result = -1;

	snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");

	// recording
	if (rec) {
		if (codec->spdif_support &&
		    (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
			idx = ALI_SPDIF_IN_CHANNEL;
		else
			idx = ALI_PCM_IN_CHANNEL;

		if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
			return result;
		} else {
698
			snd_printk(KERN_ERR "ali_find_free_channel: record channel is busy now.\n");
L
Linus Torvalds 已提交
699 700 701 702 703 704 705 706 707 708 709
			return -1;
		}
	}

	//playback...
	if (codec->spdif_support &&
	    (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
		idx = ALI_SPDIF_OUT_CHANNEL;
		if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
			return result;
		} else {
710
			snd_printk(KERN_ERR "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
L
Linus Torvalds 已提交
711 712 713 714 715 716 717
		}
	}

	for (idx = 0; idx < ALI_CHANNELS; idx++) {
		if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
			return result;
	}
718
	snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
L
Linus Torvalds 已提交
719 720 721
	return -1;
}

722
static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
L
Linus Torvalds 已提交
723 724 725 726 727 728 729 730 731
{
	unsigned int idx = channel & 0x0000001f;

	snd_ali_printk("free_channel_pcm channel=%d\n",channel);

	if (channel < 0 || channel >= ALI_CHANNELS)
		return;

	if (!(codec->synth.chmap & (1 << idx))) {
732
		snd_printk(KERN_ERR "ali_free_channel_pcm: channel %d is not in use.\n",channel);
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740
		return;
	} else {
		codec->synth.chmap &= ~(1 << idx);
		codec->synth.chcnt--;
	}
}

#if 0 // not used
741
static void snd_ali_start_voice(struct snd_ali * codec, unsigned int channel)
L
Linus Torvalds 已提交
742 743 744 745 746 747 748 749
{
	unsigned int mask = 1 << (channel & 0x1f);
	
	snd_ali_printk("start_voice: channel=%d\n",channel);
	outl(mask, ALI_REG(codec,codec->chregs.regs.start));
}
#endif

750
static void snd_ali_stop_voice(struct snd_ali * codec, unsigned int channel)
L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758 759 760 761
{
	unsigned int mask = 1 << (channel & 0x1f);

	snd_ali_printk("stop_voice: channel=%d\n",channel);
	outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
}

/*
 *    S/PDIF Part
 */

762
static void snd_ali_delay(struct snd_ali *codec,int interval)
L
Linus Torvalds 已提交
763 764 765 766 767 768 769
{
	unsigned long  begintimer,currenttimer;

	begintimer   = inl(ALI_REG(codec, ALI_STIMER));
	currenttimer = inl(ALI_REG(codec, ALI_STIMER));

	while (currenttimer < begintimer + interval) {
770
		if(snd_ali_stimer_ready(codec) < 0)
L
Linus Torvalds 已提交
771 772 773 774 775
			break;
		currenttimer = inl(ALI_REG(codec,  ALI_STIMER));
	}
}

776
static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
{
	u16 wval  = 0;
	u16 count = 0;
	u8  bval = 0, R1 = 0, R2 = 0;

	bval  = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
	bval |= 0x1F;
	outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));

	while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
		count ++;
		snd_ali_delay(codec, 6);
		bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
		R1 = bval & 0x1F;
	}

	if (count > 50000) {
794
		snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802 803 804 805 806
		return;
	}

	count = 0;
	while (count++ <= 50000) {
		snd_ali_delay(codec, 6);
		bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
		R2 = bval & 0x1F;
		if (R2 != R1) R1 = R2; else break;
	}

	if (count > 50000) {
807
		snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
		return;
	}

	if (R2 >= 0x0b && R2 <= 0x0e) {
		wval  = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
		wval &= 0xE0F0;
		wval |= (u16)0x09 << 8 | (u16)0x05;
		outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));

		bval  = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
		outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
	} else if (R2 == 0x12) {
		wval  = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
		wval &= 0xE0F0;
		wval |= (u16)0x0E << 8 | (u16)0x08;
		outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));

		bval  = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
		outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
	}
}

830
static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
L
Linus Torvalds 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
{
	u32	dwRate = 0;
	u8	bval = 0;

	bval  = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
	bval &= 0x7F;
	bval |= 0x40;
	outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));

	snd_ali_detect_spdif_rate(codec);

	bval  = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
	bval &= 0x0F;

	if (bval == 0) dwRate = 44100;
	if (bval == 1) dwRate = 48000;
	if (bval == 2) dwRate = 32000;

	return dwRate;
}

852
static void snd_ali_enable_spdif_in(struct snd_ali *codec)
L
Linus Torvalds 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866
{	
	unsigned int dwVal;

	dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
	dwVal |= ALI_SPDIF_IN_SUPPORT;
	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));

	dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
	dwVal |= 0x02;
	outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));

	snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
}

867
static void snd_ali_disable_spdif_in(struct snd_ali *codec)
L
Linus Torvalds 已提交
868 869 870 871 872 873 874 875 876 877 878
{
	unsigned int dwVal;
	
	dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
	dwVal &= ~ALI_SPDIF_IN_SUPPORT;
	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
	
	snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);	
}


879
static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
L
Linus Torvalds 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
{
	unsigned char  bVal;
	unsigned int  dwRate = 0;
	
	if (rate == 32000) dwRate = 0x300;
	if (rate == 44100) dwRate = 0;
	if (rate == 48000) dwRate = 0x200;
	
	bVal  = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
	bVal &= (unsigned char)(~(1<<6));
	
	bVal |= 0x80;		//select right
	outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
	outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
	
	bVal &= (~0x80);	//select left
	outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
	outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
}

900
static void snd_ali_enable_spdif_out(struct snd_ali *codec)
L
Linus Torvalds 已提交
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 932 933 934 935
{
	unsigned short wVal;
	unsigned char bVal;

        struct pci_dev *pci_dev = NULL;

        pci_dev = codec->pci_m1533;
        if (pci_dev == NULL)
                return;
        pci_read_config_byte(pci_dev, 0x61, &bVal);
        bVal |= 0x40;
        pci_write_config_byte(pci_dev, 0x61, bVal);
        pci_read_config_byte(pci_dev, 0x7d, &bVal);
        bVal |= 0x01;
        pci_write_config_byte(pci_dev, 0x7d, bVal);

        pci_read_config_byte(pci_dev, 0x7e, &bVal);
        bVal &= (~0x20);
        bVal |= 0x10;
        pci_write_config_byte(pci_dev, 0x7e, bVal);

	bVal = inb(ALI_REG(codec, ALI_SCTRL));
	outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));

	bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
	outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
   
	{
   		wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
   		wVal |= ALI_SPDIF_OUT_SEL_PCM;
   		outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
		snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
   	}
}

936
static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
{
	unsigned short wVal = 0;

	wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
   	wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
   	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
/*
	wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
	if (flag & ALI_SPDIF_OUT_NON_PCM)
   		wVal |= 0x0002;
	else	
		wVal &= (~0x0002);
   	outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
*/
	snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
}

954
static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
L
Linus Torvalds 已提交
955 956 957 958 959 960 961 962 963
{
	unsigned short wVal = 0;
  	wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
   	wVal |= ALI_SPDIF_OUT_SEL_PCM;
   	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));

	snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
}

964
static void snd_ali_disable_spdif_out(struct snd_ali *codec)
L
Linus Torvalds 已提交
965 966 967 968 969 970 971 972 973
{
	unsigned char  bVal;

	bVal = inb(ALI_REG(codec, ALI_SCTRL));
	outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));

	snd_ali_disable_spdif_chnout(codec);
}

974
static void snd_ali_update_ptr(struct snd_ali *codec,int channel)
L
Linus Torvalds 已提交
975
{
976 977 978
	struct snd_ali_voice *pvoice = NULL;
	struct snd_pcm_runtime *runtime;
	struct snd_ali_channel_control *pchregs = NULL;
L
Linus Torvalds 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	unsigned int old, mask;
#ifdef ALI_DEBUG
	unsigned int temp, cspf;
#endif

	pchregs = &(codec->chregs);

	// check if interrupt occurred for channel
	old  = pchregs->data.aint;
	mask = ((unsigned int) 1L) << (channel & 0x1f);

	if (!(old & mask))
		return;

	pvoice = &codec->synth.voices[channel];
	runtime = pvoice->substream->runtime;

	udelay(100);
	spin_lock(&codec->reg_lock);

	if (pvoice->pcm && pvoice->substream) {
		/* pcm interrupt */
#ifdef ALI_DEBUG
		outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
		temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
		cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
#endif
		if (pvoice->running) {
			snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
			spin_unlock(&codec->reg_lock);
			snd_pcm_period_elapsed(pvoice->substream);
			spin_lock(&codec->reg_lock);
		} else {
			snd_ali_stop_voice(codec, channel);
			snd_ali_disable_voice_irq(codec, channel);
		}	
	} else if (codec->synth.voices[channel].synth) {
		/* synth interrupt */
	} else if (codec->synth.voices[channel].midi) {
		/* midi interrupt */
	} else {
		/* unknown interrupt */
		snd_ali_stop_voice(codec, channel);
		snd_ali_disable_voice_irq(codec, channel);
	}
	spin_unlock(&codec->reg_lock);
	outl(mask,ALI_REG(codec,pchregs->regs.aint));
	pchregs->data.aint = old & (~mask);
}

1029
static void snd_ali_interrupt(struct snd_ali * codec)
L
Linus Torvalds 已提交
1030 1031 1032
{
	int channel;
	unsigned int audio_int;
1033
	struct snd_ali_channel_control *pchregs = NULL;
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	pchregs = &(codec->chregs);

	audio_int = inl(ALI_REG(codec, ALI_MISCINT));
	if (audio_int & ADDRESS_IRQ) {
		// get interrupt status for all channels
		pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
		for (channel = 0; channel < ALI_CHANNELS; channel++) {
			snd_ali_update_ptr(codec, channel);
		}
	}
	outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
		ALI_REG(codec,ALI_MISCINT));
}


static irqreturn_t snd_ali_card_interrupt(int irq,
				   void *dev_id,
				   struct pt_regs *regs)
{
1053
	struct snd_ali 	*codec = dev_id;
L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059 1060 1061

	if (codec == NULL)
		return IRQ_NONE;
	snd_ali_interrupt(codec);
	return IRQ_HANDLED;
}


1062
static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec, int type, int rec, int channel)
L
Linus Torvalds 已提交
1063
{
1064
	struct snd_ali_voice *pvoice = NULL;
L
Linus Torvalds 已提交
1065 1066 1067 1068
	int idx;

	snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);

1069
	spin_lock_irq(&codec->voice_alloc);
L
Linus Torvalds 已提交
1070
	if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1071 1072
		idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
			snd_ali_find_free_channel(codec,rec);
L
Linus Torvalds 已提交
1073
		if(idx < 0) {
1074
			snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
1075
			spin_unlock_irq(&codec->voice_alloc);
L
Linus Torvalds 已提交
1076 1077 1078
			return NULL;
		}
		pvoice = &(codec->synth.voices[idx]);
1079
		pvoice->codec = codec;
L
Linus Torvalds 已提交
1080 1081 1082
		pvoice->use = 1;
		pvoice->pcm = 1;
		pvoice->mode = rec;
1083
		spin_unlock_irq(&codec->voice_alloc);
L
Linus Torvalds 已提交
1084 1085
		return pvoice;
	}
1086
	spin_unlock_irq(&codec->voice_alloc);
L
Linus Torvalds 已提交
1087 1088 1089 1090
	return NULL;
}


1091
static void snd_ali_free_voice(struct snd_ali * codec, struct snd_ali_voice *pvoice)
L
Linus Torvalds 已提交
1092 1093 1094 1095 1096 1097 1098 1099
{
	void (*private_free)(void *);
	void *private_data;

	snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
	if (pvoice == NULL || !pvoice->use)
		return;
	snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1100
	spin_lock_irq(&codec->voice_alloc);
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109
	private_free = pvoice->private_free;
	private_data = pvoice->private_data;
	pvoice->private_free = NULL;
	pvoice->private_data = NULL;
	if (pvoice->pcm) {
		snd_ali_free_channel_pcm(codec, pvoice->number);
	}
	pvoice->use = pvoice->pcm = pvoice->synth = 0;
	pvoice->substream = NULL;
1110
	spin_unlock_irq(&codec->voice_alloc);
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115
	if (private_free)
		private_free(private_data);
}


1116
static void snd_ali_clear_voices(struct snd_ali * codec,
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
			  unsigned int v_min,
			  unsigned int v_max)
{
	unsigned int i;

	for (i = v_min; i <= v_max; i++) {
		snd_ali_stop_voice(codec, i);
		snd_ali_disable_voice_irq(codec, i);
	}
}

1128
static void snd_ali_write_voice_regs(struct snd_ali * codec,
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 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
			 unsigned int Channel,
			 unsigned int LBA,
			 unsigned int CSO,
			 unsigned int ESO,
			 unsigned int DELTA,
			 unsigned int ALPHA_FMS,
			 unsigned int GVSEL,
			 unsigned int PAN,
			 unsigned int VOL,
			 unsigned int CTRL,
			 unsigned int EC)
{
	unsigned int ctlcmds[4];
	
	outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));

	ctlcmds[0] =  (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
	ctlcmds[1] =  LBA;
	ctlcmds[2] =  (ESO << 16) | (DELTA & 0x0ffff);
	ctlcmds[3] =  (GVSEL << 31) |
		      ((PAN & 0x0000007f) << 24) |
		      ((VOL & 0x000000ff) << 16) |
		      ((CTRL & 0x0000000f) << 12) |
		      (EC & 0x00000fff);

	outb(Channel, ALI_REG(codec, ALI_GC_CIR));

	outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
	outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
	outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
	outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));

	outl(0x30000000, ALI_REG(codec, ALI_EBUF1));	/* Still Mode */
	outl(0x30000000, ALI_REG(codec, ALI_EBUF2));	/* Still Mode */
}

static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
{
	unsigned int delta;

	if (rate < 4000)  rate = 4000;
	if (rate > 48000) rate = 48000;

	if (rec) {
		if (rate == 44100)
			delta = 0x116a;
		else if (rate == 8000)
			delta = 0x6000;
		else if (rate == 48000)
			delta = 0x1000;
		else
			delta = ((48000 << 12) / rate) & 0x0000ffff;
	} else {
		if (rate == 44100)
			delta = 0xeb3;
		else if (rate == 8000)
			delta = 0x2ab;
		else if (rate == 48000)
			delta = 0x1000;
		else 
			delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
	}

	return delta;
}

1195
static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1196 1197
{
	unsigned int CTRL;
1198
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

	/* set ctrl mode
	   CTRL default: 8-bit (unsigned) mono, loop mode enabled
	 */
	CTRL = 0x00000001;
	if (snd_pcm_format_width(runtime->format) == 16)
		CTRL |= 0x00000008;	// 16-bit data
	if (!snd_pcm_format_unsigned(runtime->format))
		CTRL |= 0x00000002;	// signed data
	if (runtime->channels > 1)
		CTRL |= 0x00000004;	// stereo data
	return CTRL;
}

/*
 *  PCM part
 */

1217
static int snd_ali_ioctl(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222
				  unsigned int cmd, void *arg)
{
	return snd_pcm_lib_ioctl(substream, cmd, arg);
}

1223
static int snd_ali_trigger(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
1224 1225 1226
			       int cmd)
				    
{
1227
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1228
	struct list_head *pos;
1229
	struct snd_pcm_substream *s;
L
Linus Torvalds 已提交
1230
	unsigned int what, whati, capture_flag;
1231
	struct snd_ali_voice *pvoice = NULL, *evoice = NULL;
L
Linus Torvalds 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	unsigned int val;
	int do_start;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
		do_start = 1; break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		do_start = 0; break;
	default:
		return -EINVAL;
	}

	what = whati = capture_flag = 0;
	snd_pcm_group_for_each(pos, substream) {
		s = snd_pcm_group_substream_entry(pos);
1249 1250
		if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
			pvoice = s->runtime->private_data;
L
Linus Torvalds 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
			evoice = pvoice->extra;
			what |= 1 << (pvoice->number & 0x1f);
			if (evoice == NULL) {
				whati |= 1 << (pvoice->number & 0x1f);
			} else {
				whati |= 1 << (evoice->number & 0x1f);
				what |= 1 << (evoice->number & 0x1f);
			}
			if (do_start) {
				pvoice->running = 1;
				if (evoice != NULL)
					evoice->running = 1;
			} else {
				pvoice->running = 0;
				if (evoice != NULL)
					evoice->running = 0;
			}
			snd_pcm_trigger_done(s, substream);
			if (pvoice->mode)
				capture_flag = 1;
		}
	}
	spin_lock(&codec->reg_lock);
	if (! do_start) {
		outl(what, ALI_REG(codec, ALI_STOP));
	}
	val = inl(ALI_REG(codec, ALI_AINTEN));
	if (do_start) {
		val |= whati;
	} else {
		val &= ~whati;
	}
	outl(val, ALI_REG(codec, ALI_AINTEN));
	if (do_start) {
		outl(what, ALI_REG(codec, ALI_START));
	}
	snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
	spin_unlock(&codec->reg_lock);

	return 0;
}

1293 1294
static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
1295
{
1296 1297 1298 1299
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ali_voice *pvoice = runtime->private_data;
	struct snd_ali_voice *evoice = pvoice->extra;
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305 1306 1307
	int err;
	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
	if (err < 0) return err;
	
	/* voice management */

	if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
		if (evoice == NULL) {
1308
			evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0, -1);
L
Linus Torvalds 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
			if (evoice == NULL)
				return -ENOMEM;
			pvoice->extra = evoice;
			evoice->substream = substream;
		}
	} else {
		if (evoice != NULL) {
			snd_ali_free_voice(codec, evoice);
			pvoice->extra = evoice = NULL;
		}
	}

	return 0;
}

1324
static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1325
{
1326 1327 1328 1329
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ali_voice *pvoice = runtime->private_data;
	struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
L
Linus Torvalds 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338

	snd_pcm_lib_free_pages(substream);
	if (evoice != NULL) {
		snd_ali_free_voice(codec, evoice);
		pvoice->extra = NULL;
	}
	return 0;
}

1339 1340
static int snd_ali_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
1341 1342 1343 1344
{
	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
}

1345
static int snd_ali_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1346 1347 1348 1349
{
	return snd_pcm_lib_free_pages(substream);
}

1350
static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1351
{
1352 1353 1354 1355
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ali_voice *pvoice = runtime->private_data;
	struct snd_ali_voice *evoice = pvoice->extra;
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

	unsigned int LBA;
	unsigned int Delta;
	unsigned int ESO;
	unsigned int CTRL;
	unsigned int GVSEL;
	unsigned int PAN;
	unsigned int VOL;
	unsigned int EC;
	
	snd_ali_printk("playback_prepare ...\n");

1368
	spin_lock_irq(&codec->reg_lock);	
L
Linus Torvalds 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	
	/* set Delta (rate) value */
	Delta = snd_ali_convert_rate(runtime->rate, 0);

	if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) || 
	    (pvoice->number == ALI_PCM_IN_CHANNEL))
		snd_ali_disable_special_channel(codec, pvoice->number);
	else if (codec->spdif_support &&
		 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
		 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
		snd_ali_set_spdif_out_rate(codec, runtime->rate);
		Delta = 0x1000;
	}
	
	/* set Loop Back Address */
	LBA = runtime->dma_addr;

	/* set interrupt count size */
	pvoice->count = runtime->period_size;

	/* set target ESO for channel */
	pvoice->eso = runtime->buffer_size; 

	snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);

	/* set ESO to capture first MIDLP interrupt */
	ESO = pvoice->eso -1;
	/* set ctrl mode */
	CTRL = snd_ali_control_mode(substream);

	GVSEL = 1;
	PAN = 0;
	VOL = 0;
	EC = 0;
	snd_ali_printk("playback_prepare:\n    ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
	snd_ali_write_voice_regs(    codec,
				     pvoice->number,
				     LBA,
				     0,	/* cso */
				     ESO,
				     Delta,
				     0,	/* alpha */
				     GVSEL,
				     PAN,
				     VOL,
				     CTRL,
				     EC);
	if (evoice != NULL) {
		evoice->count = pvoice->count;
		evoice->eso = pvoice->count << 1;
		ESO = evoice->eso - 1;
		snd_ali_write_voice_regs(codec,
				     evoice->number,
				     LBA,
				     0,	/* cso */
				     ESO,
				     Delta,
				     0,	/* alpha */
				     GVSEL,
				     (unsigned int)0x7f,
				     (unsigned int)0x3ff,
				     CTRL,
				     EC);
	}
1433
	spin_unlock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1434 1435 1436 1437
	return 0;
}


1438
static int snd_ali_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1439
{
1440 1441 1442
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ali_voice *pvoice = runtime->private_data;
L
Linus Torvalds 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
	unsigned int LBA;
	unsigned int Delta;
	unsigned int ESO;
	unsigned int CTRL;
	unsigned int GVSEL;
	unsigned int PAN;
	unsigned int VOL;
	unsigned int EC;
	u8	 bValue;

1453
	spin_lock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1454

1455
	snd_ali_printk("ali_prepare...\n");
L
Linus Torvalds 已提交
1456 1457 1458

	snd_ali_enable_special_channel(codec,pvoice->number);

1459 1460 1461
	Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
		 pvoice->number == ALI_MODEM_OUT_CHANNEL) ? 
		0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
L
Linus Torvalds 已提交
1462 1463 1464 1465 1466 1467

	// Prepare capture intr channel
	if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {

		unsigned int rate;
		
1468 1469
		spin_unlock_irq(&codec->reg_lock);
		if (codec->revision != ALI_5451_V02)
L
Linus Torvalds 已提交
1470
			return -1;
1471

L
Linus Torvalds 已提交
1472 1473
		rate = snd_ali_get_spdif_in_rate(codec);
		if (rate == 0) {
1474
			snd_printk(KERN_WARNING "ali_capture_preapre: spdif rate detect err!\n");
L
Linus Torvalds 已提交
1475 1476
			rate = 48000;
		}
1477
		spin_lock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1478 1479 1480
		bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
		if (bValue & 0x10) {
			outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1481
			printk(KERN_WARNING "clear SPDIF parity error flag.\n");
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
		}

		if (rate != 48000)
			Delta = ((rate << 12)/runtime->rate)&0x00ffff;
	}

	// set target ESO for channel 
	pvoice->eso = runtime->buffer_size; 

	// set interrupt count size 
	pvoice->count = runtime->period_size;

	// set Loop Back Address 
	LBA = runtime->dma_addr;

	// set ESO to capture first MIDLP interrupt 
	ESO = pvoice->eso - 1;
	CTRL = snd_ali_control_mode(substream);
	GVSEL = 0;
	PAN = 0x00;
	VOL = 0x00;
	EC = 0;

	snd_ali_write_voice_regs(    codec,
				     pvoice->number,
				     LBA,
				     0,	/* cso */
				     ESO,
				     Delta,
				     0,	/* alpha */
				     GVSEL,
				     PAN,
				     VOL,
				     CTRL,
				     EC);


1519
	spin_unlock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524

	return 0;
}


1525
static snd_pcm_uframes_t snd_ali_playback_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1526
{
1527 1528 1529
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ali_voice *pvoice = runtime->private_data;
L
Linus Torvalds 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	unsigned int cso;

	spin_lock(&codec->reg_lock);
	if (!pvoice->running) {
		spin_unlock(&codec->reg_lock);
		return 0;
	}
	outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
	cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
	spin_unlock(&codec->reg_lock);
	snd_ali_printk("playback pointer returned cso=%xh.\n", cso);

	return cso;
}


1546
static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1547
{
1548 1549 1550
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ali_voice *pvoice = runtime->private_data;
L
Linus Torvalds 已提交
1551 1552
	unsigned int cso;

1553
	spin_lock(&codec->reg_lock);
L
Linus Torvalds 已提交
1554
	if (!pvoice->running) {
1555
		spin_unlock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1556 1557 1558 1559
		return 0;
	}
	outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
	cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1560
	spin_unlock(&codec->reg_lock);
L
Linus Torvalds 已提交
1561 1562 1563 1564

	return cso;
}

1565
static struct snd_pcm_hardware snd_ali_playback =
L
Linus Torvalds 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_RESUME |
				 SNDRV_PCM_INFO_SYNC_START),
	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
				 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		4000,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(256*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(256*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

/*
 *  Capture support device description
 */

1591
static struct snd_pcm_hardware snd_ali_capture =
L
Linus Torvalds 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_RESUME |
				 SNDRV_PCM_INFO_SYNC_START),
	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
				 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		4000,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

1613
static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
1614
{
1615 1616
	struct snd_ali_voice *pvoice = runtime->private_data;
	struct snd_ali *codec;
L
Linus Torvalds 已提交
1617 1618 1619 1620 1621 1622 1623

	if (pvoice) {
		codec = pvoice->codec;
		snd_ali_free_voice(pvoice->codec, pvoice);
	}
}

1624 1625
static int snd_ali_open(struct snd_pcm_substream *substream, int rec, int channel,
		struct snd_pcm_hardware *phw)
L
Linus Torvalds 已提交
1626
{
1627 1628 1629
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ali_voice *pvoice;
L
Linus Torvalds 已提交
1630

1631
	pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec, channel);
1632
	if (pvoice == NULL)
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638
		return -EAGAIN;

	pvoice->substream = substream;
	runtime->private_data = pvoice;
	runtime->private_free = snd_ali_pcm_free_substream;

1639
	runtime->hw = *phw;
L
Linus Torvalds 已提交
1640 1641 1642 1643 1644
	snd_pcm_set_sync(substream);
	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
	return 0;
}

1645
static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1646 1647 1648
{
	return snd_ali_open(substream, 0, -1, &snd_ali_playback);
}
L
Linus Torvalds 已提交
1649

1650
static int snd_ali_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1651
{
1652
	return snd_ali_open(substream, 1, -1, &snd_ali_capture);
L
Linus Torvalds 已提交
1653 1654
}

1655
static int snd_ali_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1656 1657 1658 1659
{
	return 0;
}

1660
static int snd_ali_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1661
{
1662 1663
	struct snd_ali *codec = snd_pcm_substream_chip(substream);
	struct snd_ali_voice *pvoice = substream->runtime->private_data;
L
Linus Torvalds 已提交
1664 1665 1666 1667 1668 1669

	snd_ali_disable_special_channel(codec,pvoice->number);

	return 0;
}

1670
static struct snd_pcm_ops snd_ali_playback_ops = {
L
Linus Torvalds 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
	.open =		snd_ali_playback_open,
	.close =	snd_ali_playback_close,
	.ioctl =	snd_ali_ioctl,
	.hw_params =	snd_ali_playback_hw_params,
	.hw_free =	snd_ali_playback_hw_free,
	.prepare =	snd_ali_playback_prepare,
	.trigger =	snd_ali_trigger,
	.pointer =	snd_ali_playback_pointer,
};

1681
static struct snd_pcm_ops snd_ali_capture_ops = {
L
Linus Torvalds 已提交
1682
	.open =		snd_ali_capture_open,
1683
	.close =	snd_ali_close,
L
Linus Torvalds 已提交
1684
	.ioctl =	snd_ali_ioctl,
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
	.hw_params =	snd_ali_hw_params,
	.hw_free =	snd_ali_hw_free,
	.prepare =	snd_ali_prepare,
	.trigger =	snd_ali_trigger,
	.pointer =	snd_ali_pointer,
};

/*
 * Modem PCM
 */

1696 1697
static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *hw_params)
1698
{
1699
	struct snd_ali *chip = snd_pcm_substream_chip(substream);
1700 1701 1702 1703 1704 1705
	unsigned int modem_num = chip->num_of_codecs - 1;
	snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE, params_rate(hw_params));
	snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
	return snd_ali_hw_params(substream, hw_params);
}

1706
static struct snd_pcm_hardware snd_ali_modem =
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_RESUME |
				 SNDRV_PCM_INFO_SYNC_START),
	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_KNOT|SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000,
	.rate_min =		8000,
	.rate_max =		16000,
	.channels_min =		1,
	.channels_max =		1,
	.buffer_bytes_max =	(256*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(256*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

1727
static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec, int channel)
1728 1729
{
	static unsigned int rates [] = {8000,9600,12000,16000};
1730
	static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
		.count = ARRAY_SIZE(rates),
		.list = rates,
		.mask = 0,
	};
	int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
	if (err)
		return err;
	return snd_pcm_hw_constraint_list(substream->runtime, 0,
			SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
}

1742
static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1743 1744 1745 1746
{
	return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
}

1747
static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1748 1749 1750 1751
{
	return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
}

1752
static struct snd_pcm_ops snd_ali_modem_playback_ops = {
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
	.open =		snd_ali_modem_playback_open,
	.close =	snd_ali_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_ali_modem_hw_params,
	.hw_free =	snd_ali_hw_free,
	.prepare =	snd_ali_prepare,
	.trigger =	snd_ali_trigger,
	.pointer =	snd_ali_pointer,
};

1763
static struct snd_pcm_ops snd_ali_modem_capture_ops = {
1764 1765 1766 1767 1768 1769
	.open =		snd_ali_modem_capture_open,
	.close =	snd_ali_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_ali_modem_hw_params,
	.hw_free =	snd_ali_hw_free,
	.prepare =	snd_ali_prepare,
L
Linus Torvalds 已提交
1770
	.trigger =	snd_ali_trigger,
1771 1772 1773 1774 1775 1776 1777 1778
	.pointer =	snd_ali_pointer,
};


struct ali_pcm_description {
	char *name;
	unsigned int playback_num;
	unsigned int capture_num;
1779 1780
	struct snd_pcm_ops *playback_ops;
	struct snd_pcm_ops *capture_ops;
1781
	unsigned short class;
L
Linus Torvalds 已提交
1782 1783 1784
};


1785
static void snd_ali_pcm_free(struct snd_pcm *pcm)
L
Linus Torvalds 已提交
1786
{
1787
	struct snd_ali *codec = pcm->private_data;
1788
	codec->pcm[pcm->device] = NULL;
L
Linus Torvalds 已提交
1789 1790
}

1791

1792
static int __devinit snd_ali_pcm(struct snd_ali * codec, int device, struct ali_pcm_description *desc)
L
Linus Torvalds 已提交
1793
{
1794
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1795 1796
	int err;

1797 1798
	err = snd_pcm_new(codec->card, desc->name, device,
			  desc->playback_num, desc->capture_num, &pcm);
L
Linus Torvalds 已提交
1799
	if (err < 0) {
1800
		snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
L
Linus Torvalds 已提交
1801 1802 1803 1804
		return err;
	}
	pcm->private_data = codec;
	pcm->private_free = snd_ali_pcm_free;
1805 1806 1807 1808
	if (desc->playback_ops)
		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, desc->playback_ops);
	if (desc->capture_ops)
		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, desc->capture_ops);
L
Linus Torvalds 已提交
1809 1810 1811 1812 1813

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_pci_data(codec->pci), 64*1024, 128*1024);

	pcm->info_flags = 0;
1814
	pcm->dev_class = desc->class;
L
Linus Torvalds 已提交
1815
	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1816 1817
	strcpy(pcm->name, desc->name);
	codec->pcm[0] = pcm;
L
Linus Torvalds 已提交
1818 1819 1820
	return 0;
}

1821
static struct ali_pcm_description ali_pcms[] = {
1822
	{ "ALI 5451", ALI_CHANNELS, 1, &snd_ali_playback_ops, &snd_ali_capture_ops },
1823
	{ "ALI 5451 modem", 1, 1, &snd_ali_modem_playback_ops, &snd_ali_modem_capture_ops, SNDRV_PCM_CLASS_MODEM }
1824 1825
};

1826
static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
1827 1828 1829 1830 1831 1832 1833 1834 1835
{
	int i, err;
	for(i = 0 ; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms) ; i++)
		if((err = snd_ali_pcm(codec, i, &ali_pcms[i])) < 0)
			return err;
	return 0;
}


L
Linus Torvalds 已提交
1836 1837 1838 1839 1840
#define ALI5451_SPDIF(xname, xindex, value) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
.put = snd_ali5451_spdif_put, .private_value = value}

1841
static int snd_ali5451_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1842 1843 1844 1845 1846 1847 1848 1849
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
        uinfo->count = 1;
        uinfo->value.integer.min = 0;
        uinfo->value.integer.max = 1;
        return 0;
}

1850
static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1851
{
1852
	struct snd_ali *codec = kcontrol->private_data;
L
Linus Torvalds 已提交
1853 1854 1855 1856
	unsigned int enable;

	enable = ucontrol->value.integer.value[0] ? 1 : 0;

1857
	spin_lock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
	switch(kcontrol->private_value) {
	case 0:
		enable = (codec->spdif_mask & 0x02) ? 1 : 0;
		break;
	case 1:
		enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
		break;
	case 2:
		enable = (codec->spdif_mask & 0x01) ? 1 : 0;
		break;
	default:
		break;
	}
	ucontrol->value.integer.value[0] = enable;
1872
	spin_unlock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1873 1874 1875
	return 0;
}

1876
static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1877
{
1878
	struct snd_ali *codec = kcontrol->private_data;
L
Linus Torvalds 已提交
1879 1880 1881 1882
	unsigned int change = 0, enable = 0;

	enable = ucontrol->value.integer.value[0] ? 1 : 0;

1883
	spin_lock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
	switch (kcontrol->private_value) {
	case 0:
		change = (codec->spdif_mask & 0x02) ? 1 : 0;
		change = change ^ enable;
		if (change) {
			if (enable) {
				codec->spdif_mask |= 0x02;
				snd_ali_enable_spdif_out(codec);
			} else {
				codec->spdif_mask &= ~(0x02);
				codec->spdif_mask &= ~(0x04);
				snd_ali_disable_spdif_out(codec);
			}
		}
		break;
	case 1: 
		change = (codec->spdif_mask & 0x04) ? 1 : 0;
		change = change ^ enable;
		if (change && (codec->spdif_mask & 0x02)) {
			if (enable) {
				codec->spdif_mask |= 0x04;
				snd_ali_enable_spdif_chnout(codec);
			} else {
				codec->spdif_mask &= ~(0x04);
				snd_ali_disable_spdif_chnout(codec);
			}
		}
		break;
	case 2:
		change = (codec->spdif_mask & 0x01) ? 1 : 0;
		change = change ^ enable;
		if (change) {
			if (enable) {
				codec->spdif_mask |= 0x01;
				snd_ali_enable_spdif_in(codec);
			} else {
				codec->spdif_mask &= ~(0x01);
				snd_ali_disable_spdif_in(codec);
			}
		}
		break;
	default:
		break;
	}
1928
	spin_unlock_irq(&codec->reg_lock);
L
Linus Torvalds 已提交
1929 1930 1931 1932
	
	return change;
}

1933
static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
L
Linus Torvalds 已提交
1934 1935
	/* spdif aplayback switch */
	/* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1936
	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
L
Linus Torvalds 已提交
1937
	/* spdif out to spdif channel */
1938
	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
L
Linus Torvalds 已提交
1939 1940 1941 1942
	/* spdif in from spdif channel */
	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
};

1943
static void snd_ali_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
L
Linus Torvalds 已提交
1944
{
1945
	struct snd_ali *codec = bus->private_data;
L
Linus Torvalds 已提交
1946 1947 1948
	codec->ac97_bus = NULL;
}

1949
static void snd_ali_mixer_free_ac97(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1950
{
1951
	struct snd_ali *codec = ac97->private_data;
1952
	codec->ac97[ac97->num] = NULL;
L
Linus Torvalds 已提交
1953 1954
}

1955
static int __devinit snd_ali_mixer(struct snd_ali * codec)
L
Linus Torvalds 已提交
1956
{
1957
	struct snd_ac97_template ac97;
L
Linus Torvalds 已提交
1958
	unsigned int idx;
1959
	int i, err;
1960
	static struct snd_ac97_bus_ops ops = {
L
Linus Torvalds 已提交
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
		.write = snd_ali_codec_write,
		.read = snd_ali_codec_read,
	};

	if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
		return err;
	codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;

	memset(&ac97, 0, sizeof(ac97));
	ac97.private_data = codec;
	ac97.private_free = snd_ali_mixer_free_ac97;
1972 1973 1974 1975

	for ( i = 0 ; i < codec->num_of_codecs ; i++) {
		ac97.num = i;
		if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i])) < 0) {
1976
			snd_printk(KERN_ERR "ali mixer %d creating error.\n", i);
1977
			if(i == 0)
1978 1979 1980 1981
				return err;
			codec->num_of_codecs = 1;
			break;
		}
1982 1983
	}

L
Linus Torvalds 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
	if (codec->spdif_support) {
		for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
			err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
			if (err < 0) return err;
		}
	}
	return 0;
}

#ifdef CONFIG_PM
T
Takashi Iwai 已提交
1994
static int ali_suspend(struct pci_dev *pci, pm_message_t state)
L
Linus Torvalds 已提交
1995
{
T
Takashi Iwai 已提交
1996 1997
	struct snd_card *card = pci_get_drvdata(pci);
	struct snd_ali *chip = card->private_data;
1998
	struct snd_ali_image *im;
L
Linus Torvalds 已提交
1999 2000 2001 2002 2003 2004
	int i, j;

	im = chip->image;
	if (! im)
		return 0;

T
Takashi Iwai 已提交
2005
	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2006
	for(i = 0 ; i < chip->num_of_codecs ; i++) {
T
Takashi Iwai 已提交
2007 2008
		snd_pcm_suspend_all(chip->pcm[i]);
		snd_ac97_suspend(chip->ac97[i]);
2009
	}
L
Linus Torvalds 已提交
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035

	spin_lock_irq(&chip->reg_lock);
	
	im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
	// im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
	im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
	
	// disable all IRQ bits
	outl(0, ALI_REG(chip, ALI_MISCINT));
	
	for (i = 0; i < ALI_GLOBAL_REGS; i++) {	
		if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
			continue;
		im->regs[i] = inl(ALI_REG(chip, i*4));
	}
	
	for (i = 0; i < ALI_CHANNELS; i++) {
		outb(i, ALI_REG(chip, ALI_GC_CIR));
		for (j = 0; j < ALI_CHANNEL_REGS; j++) 
			im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
	}

	// stop all HW channel
	outl(0xffffffff, ALI_REG(chip, ALI_STOP));

	spin_unlock_irq(&chip->reg_lock);
T
Takashi Iwai 已提交
2036 2037
	pci_disable_device(pci);
	pci_save_state(pci);
L
Linus Torvalds 已提交
2038 2039 2040
	return 0;
}

T
Takashi Iwai 已提交
2041
static int ali_resume(struct pci_dev *pci)
L
Linus Torvalds 已提交
2042
{
T
Takashi Iwai 已提交
2043 2044
	struct snd_card *card = pci_get_drvdata(pci);
	struct snd_ali *chip = card->private_data;
2045
	struct snd_ali_image *im;
L
Linus Torvalds 已提交
2046 2047 2048 2049 2050 2051
	int i, j;

	im = chip->image;
	if (! im)
		return 0;

T
Takashi Iwai 已提交
2052 2053
	pci_restore_state(pci);
	pci_enable_device(pci);
L
Linus Torvalds 已提交
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075

	spin_lock_irq(&chip->reg_lock);
	
	for (i = 0; i < ALI_CHANNELS; i++) {
		outb(i, ALI_REG(chip, ALI_GC_CIR));
		for (j = 0; j < ALI_CHANNEL_REGS; j++) 
			outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
	}
	
	for (i = 0; i < ALI_GLOBAL_REGS; i++) {	
		if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
			continue;
		outl(im->regs[i], ALI_REG(chip, i*4));
	}
	
	// start HW channel
	outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
	// restore IRQ enable bits
	outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
	
	spin_unlock_irq(&chip->reg_lock);

2076
	for(i = 0 ; i < chip->num_of_codecs ; i++)
T
Takashi Iwai 已提交
2077
		snd_ac97_resume(chip->ac97[i]);
L
Linus Torvalds 已提交
2078
	
T
Takashi Iwai 已提交
2079
	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
L
Linus Torvalds 已提交
2080 2081 2082 2083
	return 0;
}
#endif /* CONFIG_PM */

2084
static int snd_ali_free(struct snd_ali * codec)
L
Linus Torvalds 已提交
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
{
	if (codec->hw_initialized)
		snd_ali_disable_address_interrupt(codec);
	if (codec->irq >= 0) {
		synchronize_irq(codec->irq);
		free_irq(codec->irq, (void *)codec);
	}
	if (codec->port)
		pci_release_regions(codec->pci);
	pci_disable_device(codec->pci);
#ifdef CONFIG_PM
	kfree(codec->image);
#endif
J
Jiri Slaby 已提交
2098 2099
	pci_dev_put(codec->pci_m1533);
	pci_dev_put(codec->pci_m7101);
L
Linus Torvalds 已提交
2100 2101 2102 2103
	kfree(codec);
	return 0;
}

2104
static int snd_ali_chip_init(struct snd_ali *codec)
L
Linus Torvalds 已提交
2105 2106 2107 2108 2109 2110 2111 2112
{
	unsigned int legacy;
	unsigned char temp;
	struct pci_dev *pci_dev = NULL;

	snd_ali_printk("chip initializing ... \n");

	if (snd_ali_reset_5451(codec)) {
2113
		snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
L
Linus Torvalds 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
		return -1;
	}

	if (codec->revision == ALI_5451_V02) {
        	pci_dev = codec->pci_m1533;
		pci_read_config_byte(pci_dev, 0x59, &temp);
		temp |= 0x80;
		pci_write_config_byte(pci_dev, 0x59, temp);
	
		pci_dev = codec->pci_m7101;
		pci_read_config_byte(pci_dev, 0xb8, &temp);
		temp |= 0x20;
		pci_write_config_byte(pci_dev, 0xB8, temp);
	}

	pci_read_config_dword(codec->pci, 0x44, &legacy);
	legacy &= 0xff00ff00;
	legacy |= 0x000800aa;
	pci_write_config_dword(codec->pci, 0x44, legacy);

	outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
	outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
	outl(0xffffffff, ALI_REG(codec, ALI_AINT));
	outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
	outb(0x10, 	 ALI_REG(codec, ALI_MPUR2));

	codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
	codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
	if (codec->spdif_support) {
		snd_ali_enable_spdif_out(codec);
		codec->spdif_mask = 0x00000002;
	}

2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
	codec->num_of_codecs = 1;

	/* secondary codec - modem */
	if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
		codec->num_of_codecs++;
		outl(inl(ALI_REG(codec, ALI_SCTRL)) |
			(ALI_SCTRL_LINE_IN2|ALI_SCTRL_GPIO_IN2|ALI_SCTRL_LINE_OUT_EN),
			ALI_REG(codec, ALI_SCTRL));
	}

L
Linus Torvalds 已提交
2157 2158 2159 2160 2161
	snd_ali_printk("chip initialize succeed.\n");
	return 0;

}

2162
/* proc for register dump */
2163
static void snd_ali_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buf)
2164
{
2165
	struct snd_ali *codec = entry->private_data;
2166 2167 2168 2169 2170
	int i;
	for(i = 0 ; i < 256 ; i+= 4)
		snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
}

2171
static void __devinit snd_ali_proc_init(struct snd_ali *codec)
2172
{
2173
	struct snd_info_entry *entry;
2174 2175 2176 2177
	if(!snd_card_proc_new(codec->card, "ali5451", &entry))
		snd_info_set_text_ops(entry, codec, 1024, snd_ali_proc_read);
}

2178
static int __devinit snd_ali_resources(struct snd_ali *codec)
L
Linus Torvalds 已提交
2179 2180 2181 2182 2183 2184 2185 2186 2187
{
	int err;

	snd_ali_printk("resouces allocation ...\n");
	if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
		return err;
	codec->port = pci_resource_start(codec->pci, 0);

	if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2188
		snd_printk(KERN_ERR "Unable to request irq.\n");
L
Linus Torvalds 已提交
2189 2190 2191 2192 2193 2194
		return -EBUSY;
	}
	codec->irq = codec->pci->irq;
	snd_ali_printk("resouces allocated.\n");
	return 0;
}
2195
static int snd_ali_dev_free(struct snd_device *device) 
L
Linus Torvalds 已提交
2196
{
2197
	struct snd_ali *codec=device->device_data;
L
Linus Torvalds 已提交
2198 2199 2200 2201
	snd_ali_free(codec);
	return 0;
}

2202
static int __devinit snd_ali_create(struct snd_card *card,
L
Linus Torvalds 已提交
2203 2204 2205
				    struct pci_dev *pci,
				    int pcm_streams,
				    int spdif_support,
2206
				    struct snd_ali ** r_ali)
L
Linus Torvalds 已提交
2207
{
2208
	struct snd_ali *codec;
L
Linus Torvalds 已提交
2209
	int i, err;
T
Takashi Iwai 已提交
2210 2211
	unsigned short cmdw;
	static struct snd_device_ops ops = {
2212
		.dev_free = snd_ali_dev_free,
L
Linus Torvalds 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
        };

	*r_ali = NULL;

	snd_ali_printk("creating ...\n");

	/* enable PCI device */
	if ((err = pci_enable_device(pci)) < 0)
		return err;
	/* check, if we can restrict PCI DMA transfers to 31 bits */
	if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
	    pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2225
		snd_printk(KERN_ERR "architecture does not support 31bit PCI busmaster DMA\n");
L
Linus Torvalds 已提交
2226 2227 2228 2229
		pci_disable_device(pci);
		return -ENXIO;
	}

2230
	if ((codec = kzalloc(sizeof(*codec), GFP_KERNEL)) == NULL) {
L
Linus Torvalds 已提交
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
		pci_disable_device(pci);
		return -ENOMEM;
	}

	spin_lock_init(&codec->reg_lock);
	spin_lock_init(&codec->voice_alloc);

	codec->card = card;
	codec->pci = pci;
	codec->irq = -1;
	pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
	codec->spdif_support = spdif_support;

	if (pcm_streams < 1)
		pcm_streams = 1;
	if (pcm_streams > 32)
		pcm_streams = 32;
	
	pci_set_master(pci);
	pci_read_config_word(pci, PCI_COMMAND, &cmdw);
	if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
		cmdw |= PCI_COMMAND_IO;
		pci_write_config_word(pci, PCI_COMMAND, cmdw);
	}
	pci_set_master(pci);
	
	if (snd_ali_resources(codec)) {
		snd_ali_free(codec);
		return -EBUSY;
	}

	synchronize_irq(pci->irq);

	codec->synth.chmap = 0;
	codec->synth.chcnt = 0;
	codec->spdif_mask = 0;
	codec->synth.synthcount = 0;

	if (codec->revision == ALI_5451_V02)
		codec->chregs.regs.ac97read = ALI_AC97_WRITE;
	else
		codec->chregs.regs.ac97read = ALI_AC97_READ;
	codec->chregs.regs.ac97write = ALI_AC97_WRITE;

	codec->chregs.regs.start  = ALI_START;
	codec->chregs.regs.stop   = ALI_STOP;
	codec->chregs.regs.aint   = ALI_AINT;
	codec->chregs.regs.ainten = ALI_AINTEN;

	codec->chregs.data.start  = 0x00;
	codec->chregs.data.stop   = 0x00;
	codec->chregs.data.aint   = 0x00;
	codec->chregs.data.ainten = 0x00;

	/* M1533: southbridge */
T
Takashi Iwai 已提交
2286
	codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
L
Linus Torvalds 已提交
2287 2288 2289 2290 2291 2292
	if (! codec->pci_m1533) {
		snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
		snd_ali_free(codec);
		return -ENODEV;
	}
	/* M7101: power management */
T
Takashi Iwai 已提交
2293
	codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
L
Linus Torvalds 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
	if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
		snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
		snd_ali_free(codec);
		return -ENODEV;
	}

	snd_ali_printk("snd_device_new is called.\n");
	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
		snd_ali_free(codec);
		return err;
	}

	/* initialise synth voices*/
	for (i = 0; i < ALI_CHANNELS; i++ ) {
		codec->synth.voices[i].number = i;
	}

	if ((err = snd_ali_chip_init(codec)) < 0) {
2312
		snd_printk(KERN_ERR "ali create: chip init error.\n");
L
Linus Torvalds 已提交
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
		return err;
	}

#ifdef CONFIG_PM
	codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
	if (! codec->image)
		snd_printk(KERN_WARNING "can't allocate apm buffer\n");
#endif

	snd_ali_enable_address_interrupt(codec);
	codec->hw_initialized = 1;

	*r_ali = codec;
	snd_ali_printk("created.\n");
	return 0;
}

static int __devinit snd_ali_probe(struct pci_dev *pci,
				   const struct pci_device_id *pci_id)
{
2333 2334
	struct snd_card *card;
	struct snd_ali *codec;
L
Linus Torvalds 已提交
2335 2336 2337 2338
	int err;

	snd_ali_printk("probe ...\n");

2339
	card = snd_card_new(index, id, THIS_MODULE, 0);
L
Linus Torvalds 已提交
2340 2341 2342
	if (card == NULL)
		return -ENOMEM;

2343
	if ((err = snd_ali_create(card, pci, pcm_channels, spdif, &codec)) < 0) {
L
Linus Torvalds 已提交
2344 2345 2346
		snd_card_free(card);
		return err;
	}
T
Takashi Iwai 已提交
2347
	card->private_data = codec;
L
Linus Torvalds 已提交
2348 2349 2350 2351 2352 2353 2354 2355

	snd_ali_printk("mixer building ...\n");
	if ((err = snd_ali_mixer(codec)) < 0) {
		snd_card_free(card);
		return err;
	}
	
	snd_ali_printk("pcm building ...\n");
2356
	if ((err = snd_ali_build_pcms(codec)) < 0) {
L
Linus Torvalds 已提交
2357 2358 2359 2360
		snd_card_free(card);
		return err;
	}

2361 2362
	snd_ali_proc_init(codec);

L
Linus Torvalds 已提交
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388
	strcpy(card->driver, "ALI5451");
	strcpy(card->shortname, "ALI 5451");
	
	sprintf(card->longname, "%s at 0x%lx, irq %li",
		card->shortname, codec->port, codec->irq);

	snd_ali_printk("register card.\n");
	if ((err = snd_card_register(card)) < 0) {
		snd_card_free(card);
		return err;
	}
	pci_set_drvdata(pci, card);
	return 0;
}

static void __devexit snd_ali_remove(struct pci_dev *pci)
{
	snd_card_free(pci_get_drvdata(pci));
	pci_set_drvdata(pci, NULL);
}

static struct pci_driver driver = {
	.name = "ALI 5451",
	.id_table = snd_ali_ids,
	.probe = snd_ali_probe,
	.remove = __devexit_p(snd_ali_remove),
T
Takashi Iwai 已提交
2389 2390 2391 2392
#ifdef CONFIG_PM
	.suspend = ali_suspend,
	.resume = ali_resume,
#endif
L
Linus Torvalds 已提交
2393 2394 2395 2396
};                                

static int __init alsa_card_ali_init(void)
{
2397
	return pci_register_driver(&driver);
L
Linus Torvalds 已提交
2398 2399 2400 2401 2402 2403 2404 2405 2406
}

static void __exit alsa_card_ali_exit(void)
{
	pci_unregister_driver(&driver);
}

module_init(alsa_card_ali_init)
module_exit(alsa_card_ali_exit)