Linux Audio

Check our new training course

Loading...
v3.15
 
   1/*
   2 *  Matt Wu <Matt_Wu@acersoftech.com.cn>
   3 *  Apr 26, 2001
   4 *  Routines for control of ALi pci audio M5451
   5 *
   6 *  BUGS:
   7 *    --
   8 *
   9 *  TODO:
  10 *    --
  11 *
  12 *   This program is free software; you can redistribute it and/or modify
  13 *   it under the terms of the GNU General Public Lcodecnse as published by
  14 *   the Free Software Foundation; either version 2 of the Lcodecnse, or
  15 *   (at your option) any later version.
  16 *
  17 *   This program is distributed in the hope that it will be useful,
  18 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 *   GNU General Public Lcodecnse for more details.
  21 *
  22 *   You should have received a copy of the GNU General Public Lcodecnse
  23 *   along with this program; if not, write to the Free Software
  24 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  25 *
  26 */
  27
  28#include <asm/io.h>
  29#include <linux/delay.h>
  30#include <linux/interrupt.h>
  31#include <linux/init.h>
  32#include <linux/pci.h>
  33#include <linux/slab.h>
  34#include <linux/module.h>
  35#include <linux/dma-mapping.h>
  36#include <sound/core.h>
  37#include <sound/pcm.h>
  38#include <sound/info.h>
  39#include <sound/ac97_codec.h>
  40#include <sound/mpu401.h>
  41#include <sound/initval.h>
  42
  43MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
  44MODULE_DESCRIPTION("ALI M5451");
  45MODULE_LICENSE("GPL");
  46MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
  47
  48static int index = SNDRV_DEFAULT_IDX1;	/* Index */
  49static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
  50static int pcm_channels = 32;
  51static bool spdif;
  52
  53module_param(index, int, 0444);
  54MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
  55module_param(id, charp, 0444);
  56MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
  57module_param(pcm_channels, int, 0444);
  58MODULE_PARM_DESC(pcm_channels, "PCM Channels");
  59module_param(spdif, bool, 0444);
  60MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
  61
  62/* just for backward compatibility */
  63static bool enable;
  64module_param(enable, bool, 0444);
  65
  66
  67/*
  68 *  Constants definition
  69 */
  70
  71#define DEVICE_ID_ALI5451	((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
  72
  73
  74#define ALI_CHANNELS		32
  75
  76#define ALI_PCM_IN_CHANNEL	31
  77#define ALI_SPDIF_IN_CHANNEL	19
  78#define ALI_SPDIF_OUT_CHANNEL	15
  79#define ALI_CENTER_CHANNEL	24
  80#define ALI_LEF_CHANNEL		23
  81#define ALI_SURR_LEFT_CHANNEL	26
  82#define ALI_SURR_RIGHT_CHANNEL	25
  83#define ALI_MODEM_IN_CHANNEL    21
  84#define ALI_MODEM_OUT_CHANNEL   20
  85
  86#define	SNDRV_ALI_VOICE_TYPE_PCM	01
  87#define SNDRV_ALI_VOICE_TYPE_OTH	02
  88
  89#define	ALI_5451_V02		0x02
  90
  91/*
  92 *  Direct Registers
  93 */
  94
  95#define ALI_LEGACY_DMAR0        0x00  /* ADR0 */
  96#define ALI_LEGACY_DMAR4        0x04  /* CNT0 */
  97#define ALI_LEGACY_DMAR11       0x0b  /* MOD  */
  98#define ALI_LEGACY_DMAR15       0x0f  /* MMR  */
  99#define ALI_MPUR0		0x20
 100#define ALI_MPUR1		0x21
 101#define ALI_MPUR2		0x22
 102#define ALI_MPUR3		0x23
 103
 104#define	ALI_AC97_WRITE		0x40
 105#define ALI_AC97_READ		0x44
 106
 107#define ALI_SCTRL		0x48
 108#define   ALI_SPDIF_OUT_ENABLE		0x20
 109#define   ALI_SCTRL_LINE_IN2		(1 << 9)
 110#define   ALI_SCTRL_GPIO_IN2		(1 << 13)
 111#define   ALI_SCTRL_LINE_OUT_EN 	(1 << 20)
 112#define   ALI_SCTRL_GPIO_OUT_EN 	(1 << 23)
 113#define   ALI_SCTRL_CODEC1_READY	(1 << 24)
 114#define   ALI_SCTRL_CODEC2_READY	(1 << 25)
 115#define ALI_AC97_GPIO		0x4c
 116#define   ALI_AC97_GPIO_ENABLE		0x8000
 117#define   ALI_AC97_GPIO_DATA_SHIFT	16
 118#define ALI_SPDIF_CS		0x70
 119#define ALI_SPDIF_CTRL		0x74
 120#define   ALI_SPDIF_IN_FUNC_ENABLE	0x02
 121#define   ALI_SPDIF_IN_CH_STATUS	0x40
 122#define   ALI_SPDIF_OUT_CH_STATUS	0xbf
 123#define ALI_START		0x80
 124#define ALI_STOP		0x84
 125#define ALI_CSPF		0x90
 126#define ALI_AINT		0x98
 127#define ALI_GC_CIR		0xa0
 128	#define ENDLP_IE		0x00001000
 129	#define MIDLP_IE		0x00002000
 130#define ALI_AINTEN		0xa4
 131#define ALI_VOLUME		0xa8
 132#define ALI_SBDELTA_DELTA_R     0xac
 133#define ALI_MISCINT		0xb0
 134	#define ADDRESS_IRQ		0x00000020
 135	#define TARGET_REACHED		0x00008000
 136	#define MIXER_OVERFLOW		0x00000800
 137	#define MIXER_UNDERFLOW		0x00000400
 138	#define GPIO_IRQ		0x01000000
 139#define ALI_SBBL_SBCL           0xc0
 140#define ALI_SBCTRL_SBE2R_SBDD   0xc4
 141#define ALI_STIMER		0xc8
 142#define ALI_GLOBAL_CONTROL	0xd4
 143#define   ALI_SPDIF_OUT_SEL_PCM		0x00000400 /* bit 10 */
 144#define   ALI_SPDIF_IN_SUPPORT		0x00000800 /* bit 11 */
 145#define   ALI_SPDIF_OUT_CH_ENABLE	0x00008000 /* bit 15 */
 146#define   ALI_SPDIF_IN_CH_ENABLE	0x00080000 /* bit 19 */
 147#define   ALI_PCM_IN_ENABLE		0x80000000 /* bit 31 */
 148
 149#define ALI_CSO_ALPHA_FMS	0xe0
 150#define ALI_LBA			0xe4
 151#define ALI_ESO_DELTA		0xe8
 152#define ALI_GVSEL_PAN_VOC_CTRL_EC	0xf0
 153#define ALI_EBUF1		0xf4
 154#define ALI_EBUF2		0xf8
 155
 156#define ALI_REG(codec, x) ((codec)->port + x)
 157
 158#define MAX_CODECS 2
 159
 160
 161struct snd_ali;
 162struct snd_ali_voice;
 163
 164struct snd_ali_channel_control {
 165	/* register data */
 166	struct REGDATA {
 167		unsigned int start;
 168		unsigned int stop;
 169		unsigned int aint;
 170		unsigned int ainten;
 171	} data;
 172		
 173	/* register addresses */
 174	struct REGS {
 175		unsigned int start;
 176		unsigned int stop;
 177		unsigned int aint;
 178		unsigned int ainten;
 179		unsigned int ac97read;
 180		unsigned int ac97write;
 181	} regs;
 182
 183};
 184
 185struct snd_ali_voice {
 186	unsigned int number;
 187	unsigned int use :1,
 188		pcm :1,
 189		midi :1,
 190		mode :1,
 191		synth :1,
 192		running :1;
 193
 194	/* PCM data */
 195	struct snd_ali *codec;
 196	struct snd_pcm_substream *substream;
 197	struct snd_ali_voice *extra;
 198	
 199	int eso;                /* final ESO value for channel */
 200	int count;              /* runtime->period_size */
 201
 202	/* --- */
 203
 204	void *private_data;
 205	void (*private_free)(void *private_data);
 206};
 207
 208
 209struct snd_alidev {
 210
 211	struct snd_ali_voice voices[ALI_CHANNELS];	
 212
 213	unsigned int	chcnt;			/* num of opened channels */
 214	unsigned int	chmap;			/* bitmap for opened channels */
 215	unsigned int synthcount;
 216
 217};
 218
 219
 220#define ALI_GLOBAL_REGS		56
 221#define ALI_CHANNEL_REGS	8
 222struct snd_ali_image {
 223	u32 regs[ALI_GLOBAL_REGS];
 224	u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
 225};
 226
 227
 228struct snd_ali {
 229	int		irq;
 230	unsigned long	port;
 231	unsigned char	revision;
 232
 233	unsigned int hw_initialized :1;
 234	unsigned int spdif_support :1;
 235
 236	struct pci_dev	*pci;
 237	struct pci_dev	*pci_m1533;
 238	struct pci_dev	*pci_m7101;
 239
 240	struct snd_card	*card;
 241	struct snd_pcm	*pcm[MAX_CODECS];
 242	struct snd_alidev	synth;
 243	struct snd_ali_channel_control chregs;
 244
 245	/* S/PDIF Mask */
 246	unsigned int	spdif_mask;
 247
 248	unsigned int spurious_irq_count;
 249	unsigned int spurious_irq_max_delta;
 250
 251	unsigned int num_of_codecs;
 252
 253	struct snd_ac97_bus *ac97_bus;
 254	struct snd_ac97 *ac97[MAX_CODECS];
 255	unsigned short	ac97_ext_id;
 256	unsigned short	ac97_ext_status;
 257
 258	spinlock_t	reg_lock;
 259	spinlock_t	voice_alloc;
 260
 261#ifdef CONFIG_PM_SLEEP
 262	struct snd_ali_image *image;
 263#endif
 264};
 265
 266static DEFINE_PCI_DEVICE_TABLE(snd_ali_ids) = {
 267	{PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
 268	{0, }
 269};
 270MODULE_DEVICE_TABLE(pci, snd_ali_ids);
 271
 272static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
 273static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
 274static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
 275			       unsigned short);
 276
 277/*
 278 *  AC97 ACCESS
 279 */
 280
 281static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
 282					     unsigned int port)
 283{
 284	return (unsigned int)inl(ALI_REG(codec, port)); 
 285}
 286
 287static inline void snd_ali_5451_poke(struct snd_ali *codec,
 288				     unsigned int port,
 289				     unsigned int val)
 290{
 291	outl((unsigned int)val, ALI_REG(codec, port));
 292}
 293
 294static int snd_ali_codec_ready(struct snd_ali *codec,
 295			       unsigned int port)
 296{
 297	unsigned long end_time;
 298	unsigned int res;
 299	
 300	end_time = jiffies + msecs_to_jiffies(250);
 301
 302	for (;;) {
 303		res = snd_ali_5451_peek(codec,port);
 304		if (!(res & 0x8000))
 305			return 0;
 306		if (!time_after_eq(end_time, jiffies))
 307			break;
 308		schedule_timeout_uninterruptible(1);
 309	}
 310
 311	snd_ali_5451_poke(codec, port, res & ~0x8000);
 312	dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n ");
 313	return -EIO;
 314}
 315
 316static int snd_ali_stimer_ready(struct snd_ali *codec)
 317{
 318	unsigned long end_time;
 319	unsigned long dwChk1,dwChk2;
 320	
 321	dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
 322	end_time = jiffies + msecs_to_jiffies(250);
 323
 324	for (;;) {
 325		dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
 326		if (dwChk2 != dwChk1)
 327			return 0;
 328		if (!time_after_eq(end_time, jiffies))
 329			break;
 330		schedule_timeout_uninterruptible(1);
 331	}
 332
 333	dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n");
 334	return -EIO;
 335}
 336
 337static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
 338			       unsigned short reg,
 339			       unsigned short val)
 340{
 341	unsigned int dwVal;
 342	unsigned int port;
 343
 344	if (reg >= 0x80) {
 345		dev_err(codec->card->dev,
 346			"ali_codec_poke: reg(%xh) invalid.\n", reg);
 347		return;
 348	}
 349
 350	port = codec->chregs.regs.ac97write;
 351
 352	if (snd_ali_codec_ready(codec, port) < 0)
 353		return;
 354	if (snd_ali_stimer_ready(codec) < 0)
 355		return;
 356
 357	dwVal  = (unsigned int) (reg & 0xff);
 358	dwVal |= 0x8000 | (val << 16);
 359	if (secondary)
 360		dwVal |= 0x0080;
 361	if (codec->revision == ALI_5451_V02)
 362		dwVal |= 0x0100;
 363
 364	snd_ali_5451_poke(codec, port, dwVal);
 365
 366	return ;
 367}
 368
 369static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
 370					 int secondary,
 371					 unsigned short reg)
 372{
 373	unsigned int dwVal;
 374	unsigned int port;
 375
 376	if (reg >= 0x80) {
 377		dev_err(codec->card->dev,
 378			"ali_codec_peek: reg(%xh) invalid.\n", reg);
 379		return ~0;
 380	}
 381
 382	port = codec->chregs.regs.ac97read;
 383
 384	if (snd_ali_codec_ready(codec, port) < 0)
 385		return ~0;
 386	if (snd_ali_stimer_ready(codec) < 0)
 387		return ~0;
 388
 389	dwVal  = (unsigned int) (reg & 0xff);
 390	dwVal |= 0x8000;				/* bit 15*/
 391	if (secondary)
 392		dwVal |= 0x0080;
 393
 394	snd_ali_5451_poke(codec, port, dwVal);
 395
 396	if (snd_ali_stimer_ready(codec) < 0)
 397		return ~0;
 398	if (snd_ali_codec_ready(codec, port) < 0)
 399		return ~0;
 400	
 401	return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
 402}
 403
 404static void snd_ali_codec_write(struct snd_ac97 *ac97,
 405				unsigned short reg,
 406				unsigned short val )
 407{
 408	struct snd_ali *codec = ac97->private_data;
 409
 410	dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val);
 411	if (reg == AC97_GPIO_STATUS) {
 412		outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
 413		     ALI_REG(codec, ALI_AC97_GPIO));
 414		return;
 415	}
 416	snd_ali_codec_poke(codec, ac97->num, reg, val);
 417	return ;
 418}
 419
 420
 421static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
 422					 unsigned short reg)
 423{
 424	struct snd_ali *codec = ac97->private_data;
 425
 426	dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg);
 427	return snd_ali_codec_peek(codec, ac97->num, reg);
 428}
 429
 430/*
 431 *	AC97 Reset
 432 */
 433
 434static int snd_ali_reset_5451(struct snd_ali *codec)
 435{
 436	struct pci_dev *pci_dev;
 437	unsigned short wCount, wReg;
 438	unsigned int   dwVal;
 439	
 440	pci_dev = codec->pci_m1533;
 441	if (pci_dev) {
 442		pci_read_config_dword(pci_dev, 0x7c, &dwVal);
 443		pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
 444		mdelay(5);
 445		pci_read_config_dword(pci_dev, 0x7c, &dwVal);
 446		pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
 447		mdelay(5);
 448	}
 449	
 450	pci_dev = codec->pci;
 451	pci_read_config_dword(pci_dev, 0x44, &dwVal);
 452	pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
 453	udelay(500);
 454	pci_read_config_dword(pci_dev, 0x44, &dwVal);
 455	pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
 456	mdelay(5);
 457	
 458	wCount = 200;
 459	while(wCount--) {
 460		wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
 461		if ((wReg & 0x000f) == 0x000f)
 462			return 0;
 463		mdelay(5);
 464	}
 465
 466	/* non-fatal if you have a non PM capable codec */
 467	/* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */
 468	return 0;
 469}
 470
 471/*
 472 *  ALI 5451 Controller
 473 */
 474
 475static void snd_ali_enable_special_channel(struct snd_ali *codec,
 476					   unsigned int channel)
 477{
 478	unsigned long dwVal;
 479
 480	dwVal  = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 481	dwVal |= 1 << (channel & 0x0000001f);
 482	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 483}
 484
 485static void snd_ali_disable_special_channel(struct snd_ali *codec,
 486					    unsigned int channel)
 487{
 488	unsigned long dwVal;
 489
 490	dwVal  = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 491	dwVal &= ~(1 << (channel & 0x0000001f));
 492	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 493}
 494
 495static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
 496{
 497	unsigned int gc;
 498
 499	gc  = inl(ALI_REG(codec, ALI_GC_CIR));
 500	gc |= ENDLP_IE;
 501	gc |= MIDLP_IE;
 502	outl( gc, ALI_REG(codec, ALI_GC_CIR));
 503}
 504
 505static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
 506{
 507	unsigned int gc;
 508
 509	gc  = inl(ALI_REG(codec, ALI_GC_CIR));
 510	gc &= ~ENDLP_IE;
 511	gc &= ~MIDLP_IE;
 512	outl(gc, ALI_REG(codec, ALI_GC_CIR));
 513}
 514
 515static void snd_ali_disable_voice_irq(struct snd_ali *codec,
 516				      unsigned int channel)
 517{
 518	unsigned int mask;
 519	struct snd_ali_channel_control *pchregs = &(codec->chregs);
 520
 521	dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel);
 522
 523	mask = 1 << (channel & 0x1f);
 524	pchregs->data.ainten  = inl(ALI_REG(codec, pchregs->regs.ainten));
 525	pchregs->data.ainten &= ~mask;
 526	outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
 527}
 528
 529static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
 530{
 531	unsigned int idx =  channel & 0x1f;
 532
 533	if (codec->synth.chcnt >= ALI_CHANNELS){
 534		dev_err(codec->card->dev,
 535			   "ali_alloc_pcm_channel: no free channels.\n");
 536		return -1;
 537	}
 538
 539	if (!(codec->synth.chmap & (1 << idx))) {
 540		codec->synth.chmap |= 1 << idx;
 541		codec->synth.chcnt++;
 542		dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx);
 543		return idx;
 544	}
 545	return -1;
 546}
 547
 548static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
 549{
 550	int idx;
 551	int result = -1;
 552
 553	dev_dbg(codec->card->dev,
 554		"find_free_channel: for %s\n", rec ? "rec" : "pcm");
 555
 556	/* recording */
 557	if (rec) {
 558		if (codec->spdif_support &&
 559		    (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
 560		     ALI_SPDIF_IN_SUPPORT))
 561			idx = ALI_SPDIF_IN_CHANNEL;
 562		else
 563			idx = ALI_PCM_IN_CHANNEL;
 564
 565		result = snd_ali_alloc_pcm_channel(codec, idx);
 566		if (result >= 0)
 567			return result;
 568		else {
 569			dev_err(codec->card->dev,
 570				"ali_find_free_channel: record channel is busy now.\n");
 571			return -1;
 572		}
 573	}
 574
 575	/* playback... */
 576	if (codec->spdif_support &&
 577	    (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
 578	     ALI_SPDIF_OUT_CH_ENABLE)) {
 579		idx = ALI_SPDIF_OUT_CHANNEL;
 580		result = snd_ali_alloc_pcm_channel(codec, idx);
 581		if (result >= 0)
 582			return result;
 583		else
 584			dev_err(codec->card->dev,
 585				"ali_find_free_channel: S/PDIF out channel is in busy now.\n");
 586	}
 587
 588	for (idx = 0; idx < ALI_CHANNELS; idx++) {
 589		result = snd_ali_alloc_pcm_channel(codec, idx);
 590		if (result >= 0)
 591			return result;
 592	}
 593	dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n");
 594	return -1;
 595}
 596
 597static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
 598{
 599	unsigned int idx = channel & 0x0000001f;
 600
 601	dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel);
 602
 603	if (channel < 0 || channel >= ALI_CHANNELS)
 604		return;
 605
 606	if (!(codec->synth.chmap & (1 << idx))) {
 607		dev_err(codec->card->dev,
 608			"ali_free_channel_pcm: channel %d is not in use.\n",
 609			channel);
 610		return;
 611	} else {
 612		codec->synth.chmap &= ~(1 << idx);
 613		codec->synth.chcnt--;
 614	}
 615}
 616
 617static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
 618{
 619	unsigned int mask = 1 << (channel & 0x1f);
 620
 621	dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel);
 622	outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
 623}
 624
 625/*
 626 *    S/PDIF Part
 627 */
 628
 629static void snd_ali_delay(struct snd_ali *codec,int interval)
 630{
 631	unsigned long  begintimer,currenttimer;
 632
 633	begintimer   = inl(ALI_REG(codec, ALI_STIMER));
 634	currenttimer = inl(ALI_REG(codec, ALI_STIMER));
 635
 636	while (currenttimer < begintimer + interval) {
 637		if (snd_ali_stimer_ready(codec) < 0)
 638			break;
 639		currenttimer = inl(ALI_REG(codec,  ALI_STIMER));
 640		cpu_relax();
 641	}
 642}
 643
 644static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
 645{
 646	u16 wval;
 647	u16 count = 0;
 648	u8  bval, R1 = 0, R2;
 649
 650	bval  = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
 651	bval |= 0x1F;
 652	outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
 653
 654	while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
 655		count ++;
 656		snd_ali_delay(codec, 6);
 657		bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
 658		R1 = bval & 0x1F;
 659	}
 660
 661	if (count > 50000) {
 662		dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
 663		return;
 664	}
 665
 666	for (count = 0; count <= 50000; count++) {
 667		snd_ali_delay(codec, 6);
 668		bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
 669		R2 = bval & 0x1F;
 670		if (R2 != R1)
 671			R1 = R2;
 672		else
 673			break;
 674	}
 675
 676	if (count > 50000) {
 677		dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
 678		return;
 679	}
 680
 681	if (R2 >= 0x0b && R2 <= 0x0e) {
 682		wval  = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 683		wval &= 0xe0f0;
 684		wval |= (0x09 << 8) | 0x05;
 685		outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 686
 687		bval  = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
 688		outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
 689	} else if (R2 == 0x12) {
 690		wval  = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 691		wval &= 0xe0f0;
 692		wval |= (0x0e << 8) | 0x08;
 693		outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 694
 695		bval  = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
 696		outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
 697	}
 698}
 699
 700static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
 701{
 702	u32	dwRate;
 703	u8	bval;
 704
 705	bval  = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 706	bval &= 0x7f;
 707	bval |= 0x40;
 708	outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
 709
 710	snd_ali_detect_spdif_rate(codec);
 711
 712	bval  = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
 713	bval &= 0x0f;
 714
 715	switch (bval) {
 716	case 0: dwRate = 44100; break;
 717	case 1: dwRate = 48000; break;
 718	case 2: dwRate = 32000; break;
 719	default: dwRate = 0; break;
 720	}
 721
 722	return dwRate;
 723}
 724
 725static void snd_ali_enable_spdif_in(struct snd_ali *codec)
 726{	
 727	unsigned int dwVal;
 728
 729	dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 730	dwVal |= ALI_SPDIF_IN_SUPPORT;
 731	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 732
 733	dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 734	dwVal |= 0x02;
 735	outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
 736
 737	snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
 738}
 739
 740static void snd_ali_disable_spdif_in(struct snd_ali *codec)
 741{
 742	unsigned int dwVal;
 743	
 744	dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 745	dwVal &= ~ALI_SPDIF_IN_SUPPORT;
 746	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 747	
 748	snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);	
 749}
 750
 751
 752static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
 753{
 754	unsigned char  bVal;
 755	unsigned int  dwRate;
 756	
 757	switch (rate) {
 758	case 32000: dwRate = 0x300; break;
 759	case 48000: dwRate = 0x200; break;
 760	default: dwRate = 0; break;
 761	}
 762	
 763	bVal  = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 764	bVal &= (unsigned char)(~(1<<6));
 765	
 766	bVal |= 0x80;		/* select right */
 767	outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
 768	outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
 769	
 770	bVal &= ~0x80;	/* select left */
 771	outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
 772	outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
 773}
 774
 775static void snd_ali_enable_spdif_out(struct snd_ali *codec)
 776{
 777	unsigned short wVal;
 778	unsigned char bVal;
 779        struct pci_dev *pci_dev;
 780
 781        pci_dev = codec->pci_m1533;
 782        if (pci_dev == NULL)
 783                return;
 784        pci_read_config_byte(pci_dev, 0x61, &bVal);
 785        bVal |= 0x40;
 786        pci_write_config_byte(pci_dev, 0x61, bVal);
 787        pci_read_config_byte(pci_dev, 0x7d, &bVal);
 788        bVal |= 0x01;
 789        pci_write_config_byte(pci_dev, 0x7d, bVal);
 790
 791        pci_read_config_byte(pci_dev, 0x7e, &bVal);
 792        bVal &= (~0x20);
 793        bVal |= 0x10;
 794        pci_write_config_byte(pci_dev, 0x7e, bVal);
 795
 796	bVal = inb(ALI_REG(codec, ALI_SCTRL));
 797	outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
 798
 799	bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 800	outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
 801   
 802	wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 803	wVal |= ALI_SPDIF_OUT_SEL_PCM;
 804	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 805	snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
 806}
 807
 808static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
 809{
 810	unsigned short wVal;
 811
 812	wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 813   	wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
 814   	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 815/*
 816	wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
 817	if (flag & ALI_SPDIF_OUT_NON_PCM)
 818   		wVal |= 0x0002;
 819	else	
 820		wVal &= (~0x0002);
 821   	outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
 822*/
 823	snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
 824}
 825
 826static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
 827{
 828	unsigned short wVal;
 829
 830  	wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 831   	wVal |= ALI_SPDIF_OUT_SEL_PCM;
 832   	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 833
 834	snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
 835}
 836
 837static void snd_ali_disable_spdif_out(struct snd_ali *codec)
 838{
 839	unsigned char  bVal;
 840
 841	bVal = inb(ALI_REG(codec, ALI_SCTRL));
 842	outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
 843
 844	snd_ali_disable_spdif_chnout(codec);
 845}
 846
 847static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
 848{
 849	struct snd_ali_voice *pvoice;
 850	struct snd_ali_channel_control *pchregs;
 851	unsigned int old, mask;
 852
 853	pchregs = &(codec->chregs);
 854
 855	/* check if interrupt occurred for channel */
 856	old  = pchregs->data.aint;
 857	mask = 1U << (channel & 0x1f);
 858
 859	if (!(old & mask))
 860		return;
 861
 862	pvoice = &codec->synth.voices[channel];
 863
 864	udelay(100);
 865	spin_lock(&codec->reg_lock);
 866
 867	if (pvoice->pcm && pvoice->substream) {
 868		/* pcm interrupt */
 869		if (pvoice->running) {
 870			dev_dbg(codec->card->dev,
 871				"update_ptr: cso=%4.4x cspf=%d.\n",
 872				inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)),
 873				(inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask);
 874			spin_unlock(&codec->reg_lock);
 875			snd_pcm_period_elapsed(pvoice->substream);
 876			spin_lock(&codec->reg_lock);
 877		} else {
 878			snd_ali_stop_voice(codec, channel);
 879			snd_ali_disable_voice_irq(codec, channel);
 880		}	
 881	} else if (codec->synth.voices[channel].synth) {
 882		/* synth interrupt */
 883	} else if (codec->synth.voices[channel].midi) {
 884		/* midi interrupt */
 885	} else {
 886		/* unknown interrupt */
 887		snd_ali_stop_voice(codec, channel);
 888		snd_ali_disable_voice_irq(codec, channel);
 889	}
 890	spin_unlock(&codec->reg_lock);
 891	outl(mask,ALI_REG(codec,pchregs->regs.aint));
 892	pchregs->data.aint = old & (~mask);
 893}
 894
 895static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
 896{
 897	struct snd_ali 	*codec = dev_id;
 898	int channel;
 899	unsigned int audio_int;
 900	struct snd_ali_channel_control *pchregs;
 901
 902	if (codec == NULL || !codec->hw_initialized)
 903		return IRQ_NONE;
 904
 905	audio_int = inl(ALI_REG(codec, ALI_MISCINT));
 906	if (!audio_int)
 907		return IRQ_NONE;
 908
 909	pchregs = &(codec->chregs);
 910	if (audio_int & ADDRESS_IRQ) {
 911		/* get interrupt status for all channels */
 912		pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
 913		for (channel = 0; channel < ALI_CHANNELS; channel++)
 914			snd_ali_update_ptr(codec, channel);
 915	}
 916	outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
 917		ALI_REG(codec, ALI_MISCINT));
 918
 919	return IRQ_HANDLED;
 920}
 921
 922
 923static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
 924						 int type, int rec, int channel)
 925{
 926	struct snd_ali_voice *pvoice;
 927	int idx;
 928
 929	dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec);
 930
 931	spin_lock_irq(&codec->voice_alloc);
 932	if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
 933		idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
 934			snd_ali_find_free_channel(codec,rec);
 935		if (idx < 0) {
 936			dev_err(codec->card->dev, "ali_alloc_voice: err.\n");
 937			spin_unlock_irq(&codec->voice_alloc);
 938			return NULL;
 939		}
 940		pvoice = &(codec->synth.voices[idx]);
 941		pvoice->codec = codec;
 942		pvoice->use = 1;
 943		pvoice->pcm = 1;
 944		pvoice->mode = rec;
 945		spin_unlock_irq(&codec->voice_alloc);
 946		return pvoice;
 947	}
 948	spin_unlock_irq(&codec->voice_alloc);
 949	return NULL;
 950}
 951
 952
 953static void snd_ali_free_voice(struct snd_ali * codec,
 954			       struct snd_ali_voice *pvoice)
 955{
 956	void (*private_free)(void *);
 957	void *private_data;
 958
 959	dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number);
 960	if (!pvoice->use)
 961		return;
 962	snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
 963	spin_lock_irq(&codec->voice_alloc);
 964	private_free = pvoice->private_free;
 965	private_data = pvoice->private_data;
 966	pvoice->private_free = NULL;
 967	pvoice->private_data = NULL;
 968	if (pvoice->pcm)
 969		snd_ali_free_channel_pcm(codec, pvoice->number);
 970	pvoice->use = pvoice->pcm = pvoice->synth = 0;
 971	pvoice->substream = NULL;
 972	spin_unlock_irq(&codec->voice_alloc);
 973	if (private_free)
 974		private_free(private_data);
 975}
 976
 977
 978static void snd_ali_clear_voices(struct snd_ali *codec,
 979				 unsigned int v_min,
 980				 unsigned int v_max)
 981{
 982	unsigned int i;
 983
 984	for (i = v_min; i <= v_max; i++) {
 985		snd_ali_stop_voice(codec, i);
 986		snd_ali_disable_voice_irq(codec, i);
 987	}
 988}
 989
 990static void snd_ali_write_voice_regs(struct snd_ali *codec,
 991			 unsigned int Channel,
 992			 unsigned int LBA,
 993			 unsigned int CSO,
 994			 unsigned int ESO,
 995			 unsigned int DELTA,
 996			 unsigned int ALPHA_FMS,
 997			 unsigned int GVSEL,
 998			 unsigned int PAN,
 999			 unsigned int VOL,
1000			 unsigned int CTRL,
1001			 unsigned int EC)
1002{
1003	unsigned int ctlcmds[4];
1004	
1005	outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
1006
1007	ctlcmds[0] =  (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1008	ctlcmds[1] =  LBA;
1009	ctlcmds[2] =  (ESO << 16) | (DELTA & 0x0ffff);
1010	ctlcmds[3] =  (GVSEL << 31) |
1011		      ((PAN & 0x0000007f) << 24) |
1012		      ((VOL & 0x000000ff) << 16) |
1013		      ((CTRL & 0x0000000f) << 12) |
1014		      (EC & 0x00000fff);
1015
1016	outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1017
1018	outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1019	outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1020	outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1021	outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1022
1023	outl(0x30000000, ALI_REG(codec, ALI_EBUF1));	/* Still Mode */
1024	outl(0x30000000, ALI_REG(codec, ALI_EBUF2));	/* Still Mode */
1025}
1026
1027static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1028{
1029	unsigned int delta;
1030
1031	if (rate < 4000)
1032		rate = 4000;
1033	if (rate > 48000)
1034		rate = 48000;
1035
1036	if (rec) {
1037		if (rate == 44100)
1038			delta = 0x116a;
1039		else if (rate == 8000)
1040			delta = 0x6000;
1041		else if (rate == 48000)
1042			delta = 0x1000;
1043		else
1044			delta = ((48000 << 12) / rate) & 0x0000ffff;
1045	} else {
1046		if (rate == 44100)
1047			delta = 0xeb3;
1048		else if (rate == 8000)
1049			delta = 0x2ab;
1050		else if (rate == 48000)
1051			delta = 0x1000;
1052		else 
1053			delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1054	}
1055
1056	return delta;
1057}
1058
1059static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1060{
1061	unsigned int CTRL;
1062	struct snd_pcm_runtime *runtime = substream->runtime;
1063
1064	/* set ctrl mode
1065	   CTRL default: 8-bit (unsigned) mono, loop mode enabled
1066	 */
1067	CTRL = 0x00000001;
1068	if (snd_pcm_format_width(runtime->format) == 16)
1069		CTRL |= 0x00000008;	/* 16-bit data */
1070	if (!snd_pcm_format_unsigned(runtime->format))
1071		CTRL |= 0x00000002;	/* signed data */
1072	if (runtime->channels > 1)
1073		CTRL |= 0x00000004;	/* stereo data */
1074	return CTRL;
1075}
1076
1077/*
1078 *  PCM part
1079 */
1080
1081static int snd_ali_trigger(struct snd_pcm_substream *substream,
1082			       int cmd)
1083				    
1084{
1085	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1086	struct snd_pcm_substream *s;
1087	unsigned int what, whati, capture_flag;
1088	struct snd_ali_voice *pvoice, *evoice;
1089	unsigned int val;
1090	int do_start;
1091
1092	switch (cmd) {
1093	case SNDRV_PCM_TRIGGER_START:
1094	case SNDRV_PCM_TRIGGER_RESUME:
1095		do_start = 1;
1096		break;
1097	case SNDRV_PCM_TRIGGER_STOP:
1098	case SNDRV_PCM_TRIGGER_SUSPEND:
1099		do_start = 0;
1100		break;
1101	default:
1102		return -EINVAL;
1103	}
1104
1105	what = whati = capture_flag = 0;
1106	snd_pcm_group_for_each_entry(s, substream) {
1107		if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1108			pvoice = s->runtime->private_data;
1109			evoice = pvoice->extra;
1110			what |= 1 << (pvoice->number & 0x1f);
1111			if (evoice == NULL)
1112				whati |= 1 << (pvoice->number & 0x1f);
1113			else {
1114				whati |= 1 << (evoice->number & 0x1f);
1115				what |= 1 << (evoice->number & 0x1f);
1116			}
1117			if (do_start) {
1118				pvoice->running = 1;
1119				if (evoice != NULL)
1120					evoice->running = 1;
1121			} else {
1122				pvoice->running = 0;
1123				if (evoice != NULL)
1124					evoice->running = 0;
1125			}
1126			snd_pcm_trigger_done(s, substream);
1127			if (pvoice->mode)
1128				capture_flag = 1;
1129		}
1130	}
1131	spin_lock(&codec->reg_lock);
1132	if (!do_start)
1133		outl(what, ALI_REG(codec, ALI_STOP));
1134	val = inl(ALI_REG(codec, ALI_AINTEN));
1135	if (do_start)
1136		val |= whati;
1137	else
1138		val &= ~whati;
1139	outl(val, ALI_REG(codec, ALI_AINTEN));
1140	if (do_start)
1141		outl(what, ALI_REG(codec, ALI_START));
1142	dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati);
1143	spin_unlock(&codec->reg_lock);
1144
1145	return 0;
1146}
1147
1148static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1149				      struct snd_pcm_hw_params *hw_params)
1150{
1151	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1152	struct snd_pcm_runtime *runtime = substream->runtime;
1153	struct snd_ali_voice *pvoice = runtime->private_data;
1154	struct snd_ali_voice *evoice = pvoice->extra;
1155	int err;
1156
1157	err = snd_pcm_lib_malloc_pages(substream,
1158				       params_buffer_bytes(hw_params));
1159	if (err < 0)
1160		return err;
1161	
1162	/* voice management */
1163
1164	if (params_buffer_size(hw_params) / 2 !=
1165	    params_period_size(hw_params)) {
1166		if (!evoice) {
1167			evoice = snd_ali_alloc_voice(codec,
1168						     SNDRV_ALI_VOICE_TYPE_PCM,
1169						     0, -1);
1170			if (!evoice)
1171				return -ENOMEM;
1172			pvoice->extra = evoice;
1173			evoice->substream = substream;
1174		}
1175	} else {
1176		if (evoice) {
1177			snd_ali_free_voice(codec, evoice);
1178			pvoice->extra = evoice = NULL;
1179		}
1180	}
1181
1182	return 0;
1183}
1184
1185static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1186{
1187	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1188	struct snd_pcm_runtime *runtime = substream->runtime;
1189	struct snd_ali_voice *pvoice = runtime->private_data;
1190	struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1191
1192	snd_pcm_lib_free_pages(substream);
1193	if (evoice) {
1194		snd_ali_free_voice(codec, evoice);
1195		pvoice->extra = NULL;
1196	}
1197	return 0;
1198}
1199
1200static int snd_ali_hw_params(struct snd_pcm_substream *substream,
1201			     struct snd_pcm_hw_params *hw_params)
1202{
1203	return snd_pcm_lib_malloc_pages(substream,
1204					params_buffer_bytes(hw_params));
1205}
1206
1207static int snd_ali_hw_free(struct snd_pcm_substream *substream)
1208{
1209	return snd_pcm_lib_free_pages(substream);
1210}
1211
1212static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1213{
1214	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1215	struct snd_pcm_runtime *runtime = substream->runtime;
1216	struct snd_ali_voice *pvoice = runtime->private_data;
1217	struct snd_ali_voice *evoice = pvoice->extra;
1218
1219	unsigned int LBA;
1220	unsigned int Delta;
1221	unsigned int ESO;
1222	unsigned int CTRL;
1223	unsigned int GVSEL;
1224	unsigned int PAN;
1225	unsigned int VOL;
1226	unsigned int EC;
1227	
1228	dev_dbg(codec->card->dev, "playback_prepare ...\n");
1229
1230	spin_lock_irq(&codec->reg_lock);	
1231	
1232	/* set Delta (rate) value */
1233	Delta = snd_ali_convert_rate(runtime->rate, 0);
1234
1235	if (pvoice->number == ALI_SPDIF_IN_CHANNEL || 
1236	    pvoice->number == ALI_PCM_IN_CHANNEL)
1237		snd_ali_disable_special_channel(codec, pvoice->number);
1238	else if (codec->spdif_support &&
1239		 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1240		  ALI_SPDIF_OUT_CH_ENABLE)
1241		 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1242		snd_ali_set_spdif_out_rate(codec, runtime->rate);
1243		Delta = 0x1000;
1244	}
1245	
1246	/* set Loop Back Address */
1247	LBA = runtime->dma_addr;
1248
1249	/* set interrupt count size */
1250	pvoice->count = runtime->period_size;
1251
1252	/* set target ESO for channel */
1253	pvoice->eso = runtime->buffer_size; 
1254
1255	dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n",
1256		       pvoice->eso, pvoice->count);
1257
1258	/* set ESO to capture first MIDLP interrupt */
1259	ESO = pvoice->eso -1;
1260	/* set ctrl mode */
1261	CTRL = snd_ali_control_mode(substream);
1262
1263	GVSEL = 1;
1264	PAN = 0;
1265	VOL = 0;
1266	EC = 0;
1267	dev_dbg(codec->card->dev, "playback_prepare:\n");
1268	dev_dbg(codec->card->dev,
1269		"ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1270		       pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1271	snd_ali_write_voice_regs(codec,
1272				 pvoice->number,
1273				 LBA,
1274				 0,	/* cso */
1275				 ESO,
1276				 Delta,
1277				 0,	/* alpha */
1278				 GVSEL,
1279				 PAN,
1280				 VOL,
1281				 CTRL,
1282				 EC);
1283	if (evoice) {
1284		evoice->count = pvoice->count;
1285		evoice->eso = pvoice->count << 1;
1286		ESO = evoice->eso - 1;
1287		snd_ali_write_voice_regs(codec,
1288					 evoice->number,
1289					 LBA,
1290					 0,	/* cso */
1291					 ESO,
1292					 Delta,
1293					 0,	/* alpha */
1294					 GVSEL,
1295					 0x7f,
1296					 0x3ff,
1297					 CTRL,
1298					 EC);
1299	}
1300	spin_unlock_irq(&codec->reg_lock);
1301	return 0;
1302}
1303
1304
1305static int snd_ali_prepare(struct snd_pcm_substream *substream)
1306{
1307	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1308	struct snd_pcm_runtime *runtime = substream->runtime;
1309	struct snd_ali_voice *pvoice = runtime->private_data;
1310	unsigned int LBA;
1311	unsigned int Delta;
1312	unsigned int ESO;
1313	unsigned int CTRL;
1314	unsigned int GVSEL;
1315	unsigned int PAN;
1316	unsigned int VOL;
1317	unsigned int EC;
1318	u8	 bValue;
1319
1320	spin_lock_irq(&codec->reg_lock);
1321
1322	dev_dbg(codec->card->dev, "ali_prepare...\n");
1323
1324	snd_ali_enable_special_channel(codec,pvoice->number);
1325
1326	Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1327		 pvoice->number == ALI_MODEM_OUT_CHANNEL) ? 
1328		0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1329
1330	/* Prepare capture intr channel */
1331	if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1332
1333		unsigned int rate;
1334		
1335		spin_unlock_irq(&codec->reg_lock);
1336		if (codec->revision != ALI_5451_V02)
1337			return -1;
1338
1339		rate = snd_ali_get_spdif_in_rate(codec);
1340		if (rate == 0) {
1341			dev_warn(codec->card->dev,
1342				 "ali_capture_preapre: spdif rate detect err!\n");
1343			rate = 48000;
1344		}
1345		spin_lock_irq(&codec->reg_lock);
1346		bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1347		if (bValue & 0x10) {
1348			outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1349			dev_warn(codec->card->dev,
1350				 "clear SPDIF parity error flag.\n");
1351		}
1352
1353		if (rate != 48000)
1354			Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1355	}
1356
1357	/* set target ESO for channel  */
1358	pvoice->eso = runtime->buffer_size; 
1359
1360	/* set interrupt count size  */
1361	pvoice->count = runtime->period_size;
1362
1363	/* set Loop Back Address  */
1364	LBA = runtime->dma_addr;
1365
1366	/* set ESO to capture first MIDLP interrupt  */
1367	ESO = pvoice->eso - 1;
1368	CTRL = snd_ali_control_mode(substream);
1369	GVSEL = 0;
1370	PAN = 0x00;
1371	VOL = 0x00;
1372	EC = 0;
1373
1374	snd_ali_write_voice_regs(    codec,
1375				     pvoice->number,
1376				     LBA,
1377				     0,	/* cso */
1378				     ESO,
1379				     Delta,
1380				     0,	/* alpha */
1381				     GVSEL,
1382				     PAN,
1383				     VOL,
1384				     CTRL,
1385				     EC);
1386
1387	spin_unlock_irq(&codec->reg_lock);
1388
1389	return 0;
1390}
1391
1392
1393static snd_pcm_uframes_t
1394snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1395{
1396	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1397	struct snd_pcm_runtime *runtime = substream->runtime;
1398	struct snd_ali_voice *pvoice = runtime->private_data;
1399	unsigned int cso;
1400
1401	spin_lock(&codec->reg_lock);
1402	if (!pvoice->running) {
1403		spin_unlock(&codec->reg_lock);
1404		return 0;
1405	}
1406	outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1407	cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1408	spin_unlock(&codec->reg_lock);
1409	dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso);
1410
 
1411	return cso;
1412}
1413
1414
1415static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1416{
1417	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1418	struct snd_pcm_runtime *runtime = substream->runtime;
1419	struct snd_ali_voice *pvoice = runtime->private_data;
1420	unsigned int cso;
1421
1422	spin_lock(&codec->reg_lock);
1423	if (!pvoice->running) {
1424		spin_unlock(&codec->reg_lock);
1425		return 0;
1426	}
1427	outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1428	cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1429	spin_unlock(&codec->reg_lock);
1430
 
1431	return cso;
1432}
1433
1434static struct snd_pcm_hardware snd_ali_playback =
1435{
1436	.info =		(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1437			 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1438			 SNDRV_PCM_INFO_MMAP_VALID |
1439			 SNDRV_PCM_INFO_RESUME |
1440			 SNDRV_PCM_INFO_SYNC_START),
1441	.formats =	(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1442			 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1443	.rates =	SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1444	.rate_min =		4000,
1445	.rate_max =		48000,
1446	.channels_min =		1,
1447	.channels_max =		2,
1448	.buffer_bytes_max =	(256*1024),
1449	.period_bytes_min =	64,
1450	.period_bytes_max =	(256*1024),
1451	.periods_min =		1,
1452	.periods_max =		1024,
1453	.fifo_size =		0,
1454};
1455
1456/*
1457 *  Capture support device description
1458 */
1459
1460static struct snd_pcm_hardware snd_ali_capture =
1461{
1462	.info =		(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1463			 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1464			 SNDRV_PCM_INFO_MMAP_VALID |
1465			 SNDRV_PCM_INFO_RESUME |
1466			 SNDRV_PCM_INFO_SYNC_START),
1467	.formats =	(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1468			 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1469	.rates =	SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1470	.rate_min =		4000,
1471	.rate_max =		48000,
1472	.channels_min =		1,
1473	.channels_max =		2,
1474	.buffer_bytes_max =	(128*1024),
1475	.period_bytes_min =	64,
1476	.period_bytes_max =	(128*1024),
1477	.periods_min =		1,
1478	.periods_max =		1024,
1479	.fifo_size =		0,
1480};
1481
1482static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1483{
1484	struct snd_ali_voice *pvoice = runtime->private_data;
1485	struct snd_ali *codec;
1486
1487	if (pvoice) {
1488		codec = pvoice->codec;
1489		snd_ali_free_voice(pvoice->codec, pvoice);
1490	}
1491}
1492
1493static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1494			int channel, struct snd_pcm_hardware *phw)
1495{
1496	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1497	struct snd_pcm_runtime *runtime = substream->runtime;
1498	struct snd_ali_voice *pvoice;
1499
1500	pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1501				     channel);
1502	if (!pvoice)
1503		return -EAGAIN;
1504
1505	pvoice->substream = substream;
1506	runtime->private_data = pvoice;
1507	runtime->private_free = snd_ali_pcm_free_substream;
1508
1509	runtime->hw = *phw;
1510	snd_pcm_set_sync(substream);
1511	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1512				     0, 64*1024);
1513	return 0;
1514}
1515
1516static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1517{
1518	return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1519}
1520
1521static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1522{
1523	return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1524}
1525
1526static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1527{
1528	return 0;
1529}
1530
1531static int snd_ali_close(struct snd_pcm_substream *substream)
1532{
1533	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1534	struct snd_ali_voice *pvoice = substream->runtime->private_data;
1535
1536	snd_ali_disable_special_channel(codec,pvoice->number);
1537
1538	return 0;
1539}
1540
1541static struct snd_pcm_ops snd_ali_playback_ops = {
1542	.open =		snd_ali_playback_open,
1543	.close =	snd_ali_playback_close,
1544	.ioctl =	snd_pcm_lib_ioctl,
1545	.hw_params =	snd_ali_playback_hw_params,
1546	.hw_free =	snd_ali_playback_hw_free,
1547	.prepare =	snd_ali_playback_prepare,
1548	.trigger =	snd_ali_trigger,
1549	.pointer =	snd_ali_playback_pointer,
1550};
1551
1552static struct snd_pcm_ops snd_ali_capture_ops = {
1553	.open =		snd_ali_capture_open,
1554	.close =	snd_ali_close,
1555	.ioctl =	snd_pcm_lib_ioctl,
1556	.hw_params =	snd_ali_hw_params,
1557	.hw_free =	snd_ali_hw_free,
1558	.prepare =	snd_ali_prepare,
1559	.trigger =	snd_ali_trigger,
1560	.pointer =	snd_ali_pointer,
1561};
1562
1563/*
1564 * Modem PCM
1565 */
1566
1567static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1568				 struct snd_pcm_hw_params *hw_params)
1569{
1570	struct snd_ali *chip = snd_pcm_substream_chip(substream);
1571	unsigned int modem_num = chip->num_of_codecs - 1;
1572	snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1573		       params_rate(hw_params));
1574	snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1575	return snd_ali_hw_params(substream, hw_params);
1576}
1577
1578static struct snd_pcm_hardware snd_ali_modem =
1579{
1580	.info =		(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1581			 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1582			 SNDRV_PCM_INFO_MMAP_VALID |
1583			 SNDRV_PCM_INFO_RESUME |
1584			 SNDRV_PCM_INFO_SYNC_START),
1585	.formats =	SNDRV_PCM_FMTBIT_S16_LE,
1586	.rates =	(SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1587			 SNDRV_PCM_RATE_16000),
1588	.rate_min =		8000,
1589	.rate_max =		16000,
1590	.channels_min =		1,
1591	.channels_max =		1,
1592	.buffer_bytes_max =	(256*1024),
1593	.period_bytes_min =	64,
1594	.period_bytes_max =	(256*1024),
1595	.periods_min =		1,
1596	.periods_max =		1024,
1597	.fifo_size =		0,
1598};
1599
1600static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1601			      int channel)
1602{
1603	static unsigned int rates[] = {8000, 9600, 12000, 16000};
1604	static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1605		.count = ARRAY_SIZE(rates),
1606		.list = rates,
1607		.mask = 0,
1608	};
1609	int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1610
1611	if (err)
1612		return err;
1613	return snd_pcm_hw_constraint_list(substream->runtime, 0,
1614			SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1615}
1616
1617static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1618{
1619	return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1620}
1621
1622static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1623{
1624	return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1625}
1626
1627static struct snd_pcm_ops snd_ali_modem_playback_ops = {
1628	.open =		snd_ali_modem_playback_open,
1629	.close =	snd_ali_close,
1630	.ioctl =	snd_pcm_lib_ioctl,
1631	.hw_params =	snd_ali_modem_hw_params,
1632	.hw_free =	snd_ali_hw_free,
1633	.prepare =	snd_ali_prepare,
1634	.trigger =	snd_ali_trigger,
1635	.pointer =	snd_ali_pointer,
1636};
1637
1638static struct snd_pcm_ops snd_ali_modem_capture_ops = {
1639	.open =		snd_ali_modem_capture_open,
1640	.close =	snd_ali_close,
1641	.ioctl =	snd_pcm_lib_ioctl,
1642	.hw_params =	snd_ali_modem_hw_params,
1643	.hw_free =	snd_ali_hw_free,
1644	.prepare =	snd_ali_prepare,
1645	.trigger =	snd_ali_trigger,
1646	.pointer =	snd_ali_pointer,
1647};
1648
1649
1650struct ali_pcm_description {
1651	char *name;
1652	unsigned int playback_num;
1653	unsigned int capture_num;
1654	struct snd_pcm_ops *playback_ops;
1655	struct snd_pcm_ops *capture_ops;
1656	unsigned short class;
1657};
1658
1659
1660static void snd_ali_pcm_free(struct snd_pcm *pcm)
1661{
1662	struct snd_ali *codec = pcm->private_data;
1663	codec->pcm[pcm->device] = NULL;
1664}
1665
1666
1667static int snd_ali_pcm(struct snd_ali *codec, int device,
1668		       struct ali_pcm_description *desc)
1669{
1670	struct snd_pcm *pcm;
1671	int err;
1672
1673	err = snd_pcm_new(codec->card, desc->name, device,
1674			  desc->playback_num, desc->capture_num, &pcm);
1675	if (err < 0) {
1676		dev_err(codec->card->dev,
1677			"snd_ali_pcm: err called snd_pcm_new.\n");
1678		return err;
1679	}
1680	pcm->private_data = codec;
1681	pcm->private_free = snd_ali_pcm_free;
1682	if (desc->playback_ops)
1683		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1684				desc->playback_ops);
1685	if (desc->capture_ops)
1686		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1687				desc->capture_ops);
1688
1689	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1690					      snd_dma_pci_data(codec->pci),
1691					      64*1024, 128*1024);
1692
1693	pcm->info_flags = 0;
1694	pcm->dev_class = desc->class;
1695	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1696	strcpy(pcm->name, desc->name);
1697	codec->pcm[0] = pcm;
1698	return 0;
1699}
1700
1701static struct ali_pcm_description ali_pcms[] = {
1702	{ .name = "ALI 5451",
1703	  .playback_num = ALI_CHANNELS,
1704	  .capture_num = 1,
1705	  .playback_ops = &snd_ali_playback_ops,
1706	  .capture_ops = &snd_ali_capture_ops
1707	},
1708	{ .name = "ALI 5451 modem",
1709	  .playback_num = 1,
1710	  .capture_num = 1,
1711	  .playback_ops = &snd_ali_modem_playback_ops,
1712	  .capture_ops = &snd_ali_modem_capture_ops,
1713	  .class = SNDRV_PCM_CLASS_MODEM
1714	}
1715};
1716
1717static int snd_ali_build_pcms(struct snd_ali *codec)
1718{
1719	int i, err;
1720	for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1721		err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1722		if (err < 0)
1723			return err;
1724	}
1725	return 0;
1726}
1727
1728
1729#define ALI5451_SPDIF(xname, xindex, value) \
1730{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1731.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1732.put = snd_ali5451_spdif_put, .private_value = value}
1733
1734#define snd_ali5451_spdif_info		snd_ctl_boolean_mono_info
1735
1736static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1737				 struct snd_ctl_elem_value *ucontrol)
1738{
1739	struct snd_ali *codec = kcontrol->private_data;
1740	unsigned int spdif_enable;
1741
1742	spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1743
1744	spin_lock_irq(&codec->reg_lock);
1745	switch (kcontrol->private_value) {
1746	case 0:
1747		spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1748		break;
1749	case 1:
1750		spdif_enable = ((codec->spdif_mask & 0x02) &&
1751			  (codec->spdif_mask & 0x04)) ? 1 : 0;
1752		break;
1753	case 2:
1754		spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1755		break;
1756	default:
1757		break;
1758	}
1759	ucontrol->value.integer.value[0] = spdif_enable;
1760	spin_unlock_irq(&codec->reg_lock);
1761	return 0;
1762}
1763
1764static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1765				 struct snd_ctl_elem_value *ucontrol)
1766{
1767	struct snd_ali *codec = kcontrol->private_data;
1768	unsigned int change = 0, spdif_enable = 0;
1769
1770	spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1771
1772	spin_lock_irq(&codec->reg_lock);
1773	switch (kcontrol->private_value) {
1774	case 0:
1775		change = (codec->spdif_mask & 0x02) ? 1 : 0;
1776		change = change ^ spdif_enable;
1777		if (change) {
1778			if (spdif_enable) {
1779				codec->spdif_mask |= 0x02;
1780				snd_ali_enable_spdif_out(codec);
1781			} else {
1782				codec->spdif_mask &= ~(0x02);
1783				codec->spdif_mask &= ~(0x04);
1784				snd_ali_disable_spdif_out(codec);
1785			}
1786		}
1787		break;
1788	case 1: 
1789		change = (codec->spdif_mask & 0x04) ? 1 : 0;
1790		change = change ^ spdif_enable;
1791		if (change && (codec->spdif_mask & 0x02)) {
1792			if (spdif_enable) {
1793				codec->spdif_mask |= 0x04;
1794				snd_ali_enable_spdif_chnout(codec);
1795			} else {
1796				codec->spdif_mask &= ~(0x04);
1797				snd_ali_disable_spdif_chnout(codec);
1798			}
1799		}
1800		break;
1801	case 2:
1802		change = (codec->spdif_mask & 0x01) ? 1 : 0;
1803		change = change ^ spdif_enable;
1804		if (change) {
1805			if (spdif_enable) {
1806				codec->spdif_mask |= 0x01;
1807				snd_ali_enable_spdif_in(codec);
1808			} else {
1809				codec->spdif_mask &= ~(0x01);
1810				snd_ali_disable_spdif_in(codec);
1811			}
1812		}
1813		break;
1814	default:
1815		break;
1816	}
1817	spin_unlock_irq(&codec->reg_lock);
1818	
1819	return change;
1820}
1821
1822static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
1823	/* spdif aplayback switch */
1824	/* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1825	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1826	/* spdif out to spdif channel */
1827	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1828	/* spdif in from spdif channel */
1829	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1830};
1831
1832static int snd_ali_mixer(struct snd_ali *codec)
1833{
1834	struct snd_ac97_template ac97;
1835	unsigned int idx;
1836	int i, err;
1837	static struct snd_ac97_bus_ops ops = {
1838		.write = snd_ali_codec_write,
1839		.read = snd_ali_codec_read,
1840	};
1841
1842	err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1843	if (err < 0)
1844		return err;
1845
1846	memset(&ac97, 0, sizeof(ac97));
1847	ac97.private_data = codec;
1848
1849	for (i = 0; i < codec->num_of_codecs; i++) {
1850		ac97.num = i;
1851		err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1852		if (err < 0) {
1853			dev_err(codec->card->dev,
1854				   "ali mixer %d creating error.\n", i);
1855			if (i == 0)
1856				return err;
1857			codec->num_of_codecs = 1;
1858			break;
1859		}
1860	}
1861
1862	if (codec->spdif_support) {
1863		for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1864			err = snd_ctl_add(codec->card,
1865					  snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1866			if (err < 0)
1867				return err;
1868		}
1869	}
1870	return 0;
1871}
1872
1873#ifdef CONFIG_PM_SLEEP
1874static int ali_suspend(struct device *dev)
1875{
1876	struct pci_dev *pci = to_pci_dev(dev);
1877	struct snd_card *card = dev_get_drvdata(dev);
1878	struct snd_ali *chip = card->private_data;
1879	struct snd_ali_image *im;
1880	int i, j;
1881
1882	im = chip->image;
1883	if (!im)
1884		return 0;
1885
1886	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1887	for (i = 0; i < chip->num_of_codecs; i++) {
1888		snd_pcm_suspend_all(chip->pcm[i]);
1889		snd_ac97_suspend(chip->ac97[i]);
1890	}
1891
1892	spin_lock_irq(&chip->reg_lock);
1893	
1894	im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1895	/* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1896	im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1897	
1898	/* disable all IRQ bits */
1899	outl(0, ALI_REG(chip, ALI_MISCINT));
1900	
1901	for (i = 0; i < ALI_GLOBAL_REGS; i++) {	
1902		if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1903			continue;
1904		im->regs[i] = inl(ALI_REG(chip, i*4));
1905	}
1906	
1907	for (i = 0; i < ALI_CHANNELS; i++) {
1908		outb(i, ALI_REG(chip, ALI_GC_CIR));
1909		for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1910			im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1911	}
1912
1913	/* stop all HW channel */
1914	outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1915
1916	spin_unlock_irq(&chip->reg_lock);
1917
1918	pci_disable_device(pci);
1919	pci_save_state(pci);
1920	pci_set_power_state(pci, PCI_D3hot);
1921	return 0;
1922}
1923
1924static int ali_resume(struct device *dev)
1925{
1926	struct pci_dev *pci = to_pci_dev(dev);
1927	struct snd_card *card = dev_get_drvdata(dev);
1928	struct snd_ali *chip = card->private_data;
1929	struct snd_ali_image *im;
1930	int i, j;
1931
1932	im = chip->image;
1933	if (!im)
1934		return 0;
1935
1936	pci_set_power_state(pci, PCI_D0);
1937	pci_restore_state(pci);
1938	if (pci_enable_device(pci) < 0) {
1939		dev_err(dev, "pci_enable_device failed, disabling device\n");
1940		snd_card_disconnect(card);
1941		return -EIO;
1942	}
1943	pci_set_master(pci);
1944
1945	spin_lock_irq(&chip->reg_lock);
1946	
1947	for (i = 0; i < ALI_CHANNELS; i++) {
1948		outb(i, ALI_REG(chip, ALI_GC_CIR));
1949		for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1950			outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1951	}
1952	
1953	for (i = 0; i < ALI_GLOBAL_REGS; i++) {	
1954		if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1955		    (i*4 == ALI_START))
1956			continue;
1957		outl(im->regs[i], ALI_REG(chip, i*4));
1958	}
1959	
1960	/* start HW channel */
1961	outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1962	/* restore IRQ enable bits */
1963	outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1964	
1965	spin_unlock_irq(&chip->reg_lock);
1966
1967	for (i = 0 ; i < chip->num_of_codecs; i++)
1968		snd_ac97_resume(chip->ac97[i]);
1969	
1970	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1971	return 0;
1972}
1973
1974static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
1975#define ALI_PM_OPS	&ali_pm
1976#else
1977#define ALI_PM_OPS	NULL
1978#endif /* CONFIG_PM_SLEEP */
1979
1980static int snd_ali_free(struct snd_ali * codec)
1981{
 
 
1982	if (codec->hw_initialized)
1983		snd_ali_disable_address_interrupt(codec);
1984	if (codec->irq >= 0)
1985		free_irq(codec->irq, codec);
1986	if (codec->port)
1987		pci_release_regions(codec->pci);
1988	pci_disable_device(codec->pci);
1989#ifdef CONFIG_PM_SLEEP
1990	kfree(codec->image);
1991#endif
1992	pci_dev_put(codec->pci_m1533);
1993	pci_dev_put(codec->pci_m7101);
1994	kfree(codec);
1995	return 0;
1996}
1997
1998static int snd_ali_chip_init(struct snd_ali *codec)
1999{
2000	unsigned int legacy;
2001	unsigned char temp;
2002	struct pci_dev *pci_dev;
2003
2004	dev_dbg(codec->card->dev, "chip initializing ...\n");
2005
2006	if (snd_ali_reset_5451(codec)) {
2007		dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
2008		return -1;
2009	}
2010
2011	if (codec->revision == ALI_5451_V02) {
2012        	pci_dev = codec->pci_m1533;
2013		pci_read_config_byte(pci_dev, 0x59, &temp);
2014		temp |= 0x80;
2015		pci_write_config_byte(pci_dev, 0x59, temp);
2016	
2017		pci_dev = codec->pci_m7101;
2018		pci_read_config_byte(pci_dev, 0xb8, &temp);
2019		temp |= 0x20;
2020		pci_write_config_byte(pci_dev, 0xB8, temp);
2021	}
2022
2023	pci_read_config_dword(codec->pci, 0x44, &legacy);
2024	legacy &= 0xff00ff00;
2025	legacy |= 0x000800aa;
2026	pci_write_config_dword(codec->pci, 0x44, legacy);
2027
2028	outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2029	outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2030	outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2031	outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2032	outb(0x10, 	 ALI_REG(codec, ALI_MPUR2));
2033
2034	codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2035	codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2036						    AC97_EXTENDED_STATUS);
2037	if (codec->spdif_support) {
2038		snd_ali_enable_spdif_out(codec);
2039		codec->spdif_mask = 0x00000002;
2040	}
2041
2042	codec->num_of_codecs = 1;
2043
2044	/* secondary codec - modem */
2045	if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2046		codec->num_of_codecs++;
2047		outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2048		     (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2049		      ALI_SCTRL_LINE_OUT_EN),
2050		     ALI_REG(codec, ALI_SCTRL));
2051	}
2052
2053	dev_dbg(codec->card->dev, "chip initialize succeed.\n");
2054	return 0;
2055
2056}
2057
2058/* proc for register dump */
2059static void snd_ali_proc_read(struct snd_info_entry *entry,
2060			      struct snd_info_buffer *buf)
2061{
2062	struct snd_ali *codec = entry->private_data;
2063	int i;
2064	for (i = 0; i < 256 ; i+= 4)
2065		snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2066}
2067
2068static void snd_ali_proc_init(struct snd_ali *codec)
2069{
2070	struct snd_info_entry *entry;
2071	if (!snd_card_proc_new(codec->card, "ali5451", &entry))
2072		snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
2073}
2074
2075static int snd_ali_resources(struct snd_ali *codec)
2076{
2077	int err;
2078
2079	dev_dbg(codec->card->dev, "resources allocation ...\n");
2080	err = pci_request_regions(codec->pci, "ALI 5451");
2081	if (err < 0)
2082		return err;
2083	codec->port = pci_resource_start(codec->pci, 0);
2084
2085	if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2086			IRQF_SHARED, KBUILD_MODNAME, codec)) {
 
2087		dev_err(codec->card->dev, "Unable to request irq.\n");
2088		return -EBUSY;
2089	}
2090	codec->irq = codec->pci->irq;
 
2091	dev_dbg(codec->card->dev, "resources allocated.\n");
2092	return 0;
2093}
2094static int snd_ali_dev_free(struct snd_device *device)
2095{
2096	struct snd_ali *codec = device->device_data;
2097	snd_ali_free(codec);
2098	return 0;
2099}
2100
2101static int snd_ali_create(struct snd_card *card,
2102			  struct pci_dev *pci,
2103			  int pcm_streams,
2104			  int spdif_support,
2105			  struct snd_ali **r_ali)
2106{
2107	struct snd_ali *codec;
2108	int i, err;
2109	unsigned short cmdw;
2110	static struct snd_device_ops ops = {
2111		.dev_free = snd_ali_dev_free,
2112        };
2113
2114	*r_ali = NULL;
2115
2116	dev_dbg(card->dev, "creating ...\n");
2117
2118	/* enable PCI device */
2119	err = pci_enable_device(pci);
2120	if (err < 0)
2121		return err;
2122	/* check, if we can restrict PCI DMA transfers to 31 bits */
2123	if (pci_set_dma_mask(pci, DMA_BIT_MASK(31)) < 0 ||
2124	    pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(31)) < 0) {
2125		dev_err(card->dev,
2126			"architecture does not support 31bit PCI busmaster DMA\n");
2127		pci_disable_device(pci);
2128		return -ENXIO;
2129	}
2130
2131	codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2132	if (!codec) {
2133		pci_disable_device(pci);
2134		return -ENOMEM;
2135	}
2136
2137	spin_lock_init(&codec->reg_lock);
2138	spin_lock_init(&codec->voice_alloc);
2139
2140	codec->card = card;
2141	codec->pci = pci;
2142	codec->irq = -1;
2143	codec->revision = pci->revision;
2144	codec->spdif_support = spdif_support;
2145
2146	if (pcm_streams < 1)
2147		pcm_streams = 1;
2148	if (pcm_streams > 32)
2149		pcm_streams = 32;
2150	
2151	pci_set_master(pci);
2152	pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2153	if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2154		cmdw |= PCI_COMMAND_IO;
2155		pci_write_config_word(pci, PCI_COMMAND, cmdw);
2156	}
2157	pci_set_master(pci);
2158	
2159	if (snd_ali_resources(codec)) {
2160		snd_ali_free(codec);
2161		return -EBUSY;
2162	}
2163
2164	synchronize_irq(pci->irq);
2165
2166	codec->synth.chmap = 0;
2167	codec->synth.chcnt = 0;
2168	codec->spdif_mask = 0;
2169	codec->synth.synthcount = 0;
2170
2171	if (codec->revision == ALI_5451_V02)
2172		codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2173	else
2174		codec->chregs.regs.ac97read = ALI_AC97_READ;
2175	codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2176
2177	codec->chregs.regs.start  = ALI_START;
2178	codec->chregs.regs.stop   = ALI_STOP;
2179	codec->chregs.regs.aint   = ALI_AINT;
2180	codec->chregs.regs.ainten = ALI_AINTEN;
2181
2182	codec->chregs.data.start  = 0x00;
2183	codec->chregs.data.stop   = 0x00;
2184	codec->chregs.data.aint   = 0x00;
2185	codec->chregs.data.ainten = 0x00;
2186
2187	/* M1533: southbridge */
2188	codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2189	if (!codec->pci_m1533) {
2190		dev_err(card->dev, "cannot find ALi 1533 chip.\n");
2191		snd_ali_free(codec);
2192		return -ENODEV;
2193	}
2194	/* M7101: power management */
2195	codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2196	if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2197		dev_err(card->dev, "cannot find ALi 7101 chip.\n");
2198		snd_ali_free(codec);
2199		return -ENODEV;
2200	}
2201
2202	dev_dbg(card->dev, "snd_device_new is called.\n");
2203	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2204	if (err < 0) {
2205		snd_ali_free(codec);
2206		return err;
2207	}
2208
2209	/* initialise synth voices*/
2210	for (i = 0; i < ALI_CHANNELS; i++)
2211		codec->synth.voices[i].number = i;
2212
2213	err = snd_ali_chip_init(codec);
2214	if (err < 0) {
2215		dev_err(card->dev, "ali create: chip init error.\n");
2216		return err;
2217	}
2218
2219#ifdef CONFIG_PM_SLEEP
2220	codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2221	if (!codec->image)
2222		dev_warn(card->dev, "can't allocate apm buffer\n");
2223#endif
2224
2225	snd_ali_enable_address_interrupt(codec);
2226	codec->hw_initialized = 1;
2227
2228	*r_ali = codec;
2229	dev_dbg(card->dev, "created.\n");
2230	return 0;
2231}
2232
2233static int snd_ali_probe(struct pci_dev *pci,
2234			 const struct pci_device_id *pci_id)
2235{
2236	struct snd_card *card;
2237	struct snd_ali *codec;
2238	int err;
2239
2240	dev_dbg(&pci->dev, "probe ...\n");
2241
2242	err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
 
2243	if (err < 0)
2244		return err;
 
2245
2246	err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2247	if (err < 0)
2248		goto error;
2249	card->private_data = codec;
2250
2251	dev_dbg(&pci->dev, "mixer building ...\n");
2252	err = snd_ali_mixer(codec);
2253	if (err < 0)
2254		goto error;
2255	
2256	dev_dbg(&pci->dev, "pcm building ...\n");
2257	err = snd_ali_build_pcms(codec);
2258	if (err < 0)
2259		goto error;
2260
2261	snd_ali_proc_init(codec);
2262
2263	strcpy(card->driver, "ALI5451");
2264	strcpy(card->shortname, "ALI 5451");
2265	
2266	sprintf(card->longname, "%s at 0x%lx, irq %i",
2267		card->shortname, codec->port, codec->irq);
2268
2269	dev_dbg(&pci->dev, "register card.\n");
2270	err = snd_card_register(card);
2271	if (err < 0)
2272		goto error;
2273
2274	pci_set_drvdata(pci, card);
2275	return 0;
2276
2277 error:
2278	snd_card_free(card);
2279	return err;
2280}
2281
2282static void snd_ali_remove(struct pci_dev *pci)
 
2283{
2284	snd_card_free(pci_get_drvdata(pci));
2285}
2286
2287static struct pci_driver ali5451_driver = {
2288	.name = KBUILD_MODNAME,
2289	.id_table = snd_ali_ids,
2290	.probe = snd_ali_probe,
2291	.remove = snd_ali_remove,
2292	.driver = {
2293		.pm = ALI_PM_OPS,
2294	},
2295};                                
2296
2297module_pci_driver(ali5451_driver);
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Matt Wu <Matt_Wu@acersoftech.com.cn>
   4 *  Apr 26, 2001
   5 *  Routines for control of ALi pci audio M5451
   6 *
   7 *  BUGS:
   8 *    --
   9 *
  10 *  TODO:
  11 *    --
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  12 */
  13
  14#include <linux/io.h>
  15#include <linux/delay.h>
  16#include <linux/interrupt.h>
  17#include <linux/init.h>
  18#include <linux/pci.h>
  19#include <linux/slab.h>
  20#include <linux/module.h>
  21#include <linux/dma-mapping.h>
  22#include <sound/core.h>
  23#include <sound/pcm.h>
  24#include <sound/info.h>
  25#include <sound/ac97_codec.h>
  26#include <sound/mpu401.h>
  27#include <sound/initval.h>
  28
  29MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
  30MODULE_DESCRIPTION("ALI M5451");
  31MODULE_LICENSE("GPL");
 
  32
  33static int index = SNDRV_DEFAULT_IDX1;	/* Index */
  34static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
  35static int pcm_channels = 32;
  36static bool spdif;
  37
  38module_param(index, int, 0444);
  39MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
  40module_param(id, charp, 0444);
  41MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
  42module_param(pcm_channels, int, 0444);
  43MODULE_PARM_DESC(pcm_channels, "PCM Channels");
  44module_param(spdif, bool, 0444);
  45MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
  46
  47/* just for backward compatibility */
  48static bool enable;
  49module_param(enable, bool, 0444);
  50
  51
  52/*
  53 *  Constants definition
  54 */
  55
  56#define DEVICE_ID_ALI5451	((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
  57
  58
  59#define ALI_CHANNELS		32
  60
  61#define ALI_PCM_IN_CHANNEL	31
  62#define ALI_SPDIF_IN_CHANNEL	19
  63#define ALI_SPDIF_OUT_CHANNEL	15
  64#define ALI_CENTER_CHANNEL	24
  65#define ALI_LEF_CHANNEL		23
  66#define ALI_SURR_LEFT_CHANNEL	26
  67#define ALI_SURR_RIGHT_CHANNEL	25
  68#define ALI_MODEM_IN_CHANNEL    21
  69#define ALI_MODEM_OUT_CHANNEL   20
  70
  71#define	SNDRV_ALI_VOICE_TYPE_PCM	01
  72#define SNDRV_ALI_VOICE_TYPE_OTH	02
  73
  74#define	ALI_5451_V02		0x02
  75
  76/*
  77 *  Direct Registers
  78 */
  79
  80#define ALI_LEGACY_DMAR0        0x00  /* ADR0 */
  81#define ALI_LEGACY_DMAR4        0x04  /* CNT0 */
  82#define ALI_LEGACY_DMAR11       0x0b  /* MOD  */
  83#define ALI_LEGACY_DMAR15       0x0f  /* MMR  */
  84#define ALI_MPUR0		0x20
  85#define ALI_MPUR1		0x21
  86#define ALI_MPUR2		0x22
  87#define ALI_MPUR3		0x23
  88
  89#define	ALI_AC97_WRITE		0x40
  90#define ALI_AC97_READ		0x44
  91
  92#define ALI_SCTRL		0x48
  93#define   ALI_SPDIF_OUT_ENABLE		0x20
  94#define   ALI_SCTRL_LINE_IN2		(1 << 9)
  95#define   ALI_SCTRL_GPIO_IN2		(1 << 13)
  96#define   ALI_SCTRL_LINE_OUT_EN 	(1 << 20)
  97#define   ALI_SCTRL_GPIO_OUT_EN 	(1 << 23)
  98#define   ALI_SCTRL_CODEC1_READY	(1 << 24)
  99#define   ALI_SCTRL_CODEC2_READY	(1 << 25)
 100#define ALI_AC97_GPIO		0x4c
 101#define   ALI_AC97_GPIO_ENABLE		0x8000
 102#define   ALI_AC97_GPIO_DATA_SHIFT	16
 103#define ALI_SPDIF_CS		0x70
 104#define ALI_SPDIF_CTRL		0x74
 105#define   ALI_SPDIF_IN_FUNC_ENABLE	0x02
 106#define   ALI_SPDIF_IN_CH_STATUS	0x40
 107#define   ALI_SPDIF_OUT_CH_STATUS	0xbf
 108#define ALI_START		0x80
 109#define ALI_STOP		0x84
 110#define ALI_CSPF		0x90
 111#define ALI_AINT		0x98
 112#define ALI_GC_CIR		0xa0
 113	#define ENDLP_IE		0x00001000
 114	#define MIDLP_IE		0x00002000
 115#define ALI_AINTEN		0xa4
 116#define ALI_VOLUME		0xa8
 117#define ALI_SBDELTA_DELTA_R     0xac
 118#define ALI_MISCINT		0xb0
 119	#define ADDRESS_IRQ		0x00000020
 120	#define TARGET_REACHED		0x00008000
 121	#define MIXER_OVERFLOW		0x00000800
 122	#define MIXER_UNDERFLOW		0x00000400
 123	#define GPIO_IRQ		0x01000000
 124#define ALI_SBBL_SBCL           0xc0
 125#define ALI_SBCTRL_SBE2R_SBDD   0xc4
 126#define ALI_STIMER		0xc8
 127#define ALI_GLOBAL_CONTROL	0xd4
 128#define   ALI_SPDIF_OUT_SEL_PCM		0x00000400 /* bit 10 */
 129#define   ALI_SPDIF_IN_SUPPORT		0x00000800 /* bit 11 */
 130#define   ALI_SPDIF_OUT_CH_ENABLE	0x00008000 /* bit 15 */
 131#define   ALI_SPDIF_IN_CH_ENABLE	0x00080000 /* bit 19 */
 132#define   ALI_PCM_IN_ENABLE		0x80000000 /* bit 31 */
 133
 134#define ALI_CSO_ALPHA_FMS	0xe0
 135#define ALI_LBA			0xe4
 136#define ALI_ESO_DELTA		0xe8
 137#define ALI_GVSEL_PAN_VOC_CTRL_EC	0xf0
 138#define ALI_EBUF1		0xf4
 139#define ALI_EBUF2		0xf8
 140
 141#define ALI_REG(codec, x) ((codec)->port + x)
 142
 143#define MAX_CODECS 2
 144
 145
 146struct snd_ali;
 147struct snd_ali_voice;
 148
 149struct snd_ali_channel_control {
 150	/* register data */
 151	struct REGDATA {
 152		unsigned int start;
 153		unsigned int stop;
 154		unsigned int aint;
 155		unsigned int ainten;
 156	} data;
 157		
 158	/* register addresses */
 159	struct REGS {
 160		unsigned int start;
 161		unsigned int stop;
 162		unsigned int aint;
 163		unsigned int ainten;
 164		unsigned int ac97read;
 165		unsigned int ac97write;
 166	} regs;
 167
 168};
 169
 170struct snd_ali_voice {
 171	unsigned int number;
 172	unsigned int use :1,
 173		pcm :1,
 174		midi :1,
 175		mode :1,
 176		synth :1,
 177		running :1;
 178
 179	/* PCM data */
 180	struct snd_ali *codec;
 181	struct snd_pcm_substream *substream;
 182	struct snd_ali_voice *extra;
 183	
 184	int eso;                /* final ESO value for channel */
 185	int count;              /* runtime->period_size */
 186
 187	/* --- */
 188
 189	void *private_data;
 190	void (*private_free)(void *private_data);
 191};
 192
 193
 194struct snd_alidev {
 195
 196	struct snd_ali_voice voices[ALI_CHANNELS];	
 197
 198	unsigned int	chcnt;			/* num of opened channels */
 199	unsigned int	chmap;			/* bitmap for opened channels */
 200	unsigned int synthcount;
 201
 202};
 203
 204
 205#define ALI_GLOBAL_REGS		56
 206#define ALI_CHANNEL_REGS	8
 207struct snd_ali_image {
 208	u32 regs[ALI_GLOBAL_REGS];
 209	u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
 210};
 211
 212
 213struct snd_ali {
 214	int		irq;
 215	unsigned long	port;
 216	unsigned char	revision;
 217
 218	unsigned int hw_initialized :1;
 219	unsigned int spdif_support :1;
 220
 221	struct pci_dev	*pci;
 222	struct pci_dev	*pci_m1533;
 223	struct pci_dev	*pci_m7101;
 224
 225	struct snd_card	*card;
 226	struct snd_pcm	*pcm[MAX_CODECS];
 227	struct snd_alidev	synth;
 228	struct snd_ali_channel_control chregs;
 229
 230	/* S/PDIF Mask */
 231	unsigned int	spdif_mask;
 232
 233	unsigned int spurious_irq_count;
 234	unsigned int spurious_irq_max_delta;
 235
 236	unsigned int num_of_codecs;
 237
 238	struct snd_ac97_bus *ac97_bus;
 239	struct snd_ac97 *ac97[MAX_CODECS];
 240	unsigned short	ac97_ext_id;
 241	unsigned short	ac97_ext_status;
 242
 243	spinlock_t	reg_lock;
 244	spinlock_t	voice_alloc;
 245
 246	struct snd_ali_image image;
 
 
 247};
 248
 249static const struct pci_device_id snd_ali_ids[] = {
 250	{PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
 251	{0, }
 252};
 253MODULE_DEVICE_TABLE(pci, snd_ali_ids);
 254
 255static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
 256static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
 257static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
 258			       unsigned short);
 259
 260/*
 261 *  AC97 ACCESS
 262 */
 263
 264static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
 265					     unsigned int port)
 266{
 267	return (unsigned int)inl(ALI_REG(codec, port)); 
 268}
 269
 270static inline void snd_ali_5451_poke(struct snd_ali *codec,
 271				     unsigned int port,
 272				     unsigned int val)
 273{
 274	outl((unsigned int)val, ALI_REG(codec, port));
 275}
 276
 277static int snd_ali_codec_ready(struct snd_ali *codec,
 278			       unsigned int port)
 279{
 280	unsigned long end_time;
 281	unsigned int res;
 282	
 283	end_time = jiffies + msecs_to_jiffies(250);
 284
 285	for (;;) {
 286		res = snd_ali_5451_peek(codec,port);
 287		if (!(res & 0x8000))
 288			return 0;
 289		if (!time_after_eq(end_time, jiffies))
 290			break;
 291		schedule_timeout_uninterruptible(1);
 292	}
 293
 294	snd_ali_5451_poke(codec, port, res & ~0x8000);
 295	dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n");
 296	return -EIO;
 297}
 298
 299static int snd_ali_stimer_ready(struct snd_ali *codec)
 300{
 301	unsigned long end_time;
 302	unsigned long dwChk1,dwChk2;
 303	
 304	dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
 305	end_time = jiffies + msecs_to_jiffies(250);
 306
 307	for (;;) {
 308		dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
 309		if (dwChk2 != dwChk1)
 310			return 0;
 311		if (!time_after_eq(end_time, jiffies))
 312			break;
 313		schedule_timeout_uninterruptible(1);
 314	}
 315
 316	dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n");
 317	return -EIO;
 318}
 319
 320static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
 321			       unsigned short reg,
 322			       unsigned short val)
 323{
 324	unsigned int dwVal;
 325	unsigned int port;
 326
 327	if (reg >= 0x80) {
 328		dev_err(codec->card->dev,
 329			"ali_codec_poke: reg(%xh) invalid.\n", reg);
 330		return;
 331	}
 332
 333	port = codec->chregs.regs.ac97write;
 334
 335	if (snd_ali_codec_ready(codec, port) < 0)
 336		return;
 337	if (snd_ali_stimer_ready(codec) < 0)
 338		return;
 339
 340	dwVal  = (unsigned int) (reg & 0xff);
 341	dwVal |= 0x8000 | (val << 16);
 342	if (secondary)
 343		dwVal |= 0x0080;
 344	if (codec->revision == ALI_5451_V02)
 345		dwVal |= 0x0100;
 346
 347	snd_ali_5451_poke(codec, port, dwVal);
 348
 349	return ;
 350}
 351
 352static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
 353					 int secondary,
 354					 unsigned short reg)
 355{
 356	unsigned int dwVal;
 357	unsigned int port;
 358
 359	if (reg >= 0x80) {
 360		dev_err(codec->card->dev,
 361			"ali_codec_peek: reg(%xh) invalid.\n", reg);
 362		return ~0;
 363	}
 364
 365	port = codec->chregs.regs.ac97read;
 366
 367	if (snd_ali_codec_ready(codec, port) < 0)
 368		return ~0;
 369	if (snd_ali_stimer_ready(codec) < 0)
 370		return ~0;
 371
 372	dwVal  = (unsigned int) (reg & 0xff);
 373	dwVal |= 0x8000;				/* bit 15*/
 374	if (secondary)
 375		dwVal |= 0x0080;
 376
 377	snd_ali_5451_poke(codec, port, dwVal);
 378
 379	if (snd_ali_stimer_ready(codec) < 0)
 380		return ~0;
 381	if (snd_ali_codec_ready(codec, port) < 0)
 382		return ~0;
 383	
 384	return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
 385}
 386
 387static void snd_ali_codec_write(struct snd_ac97 *ac97,
 388				unsigned short reg,
 389				unsigned short val )
 390{
 391	struct snd_ali *codec = ac97->private_data;
 392
 393	dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val);
 394	if (reg == AC97_GPIO_STATUS) {
 395		outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
 396		     ALI_REG(codec, ALI_AC97_GPIO));
 397		return;
 398	}
 399	snd_ali_codec_poke(codec, ac97->num, reg, val);
 400	return ;
 401}
 402
 403
 404static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
 405					 unsigned short reg)
 406{
 407	struct snd_ali *codec = ac97->private_data;
 408
 409	dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg);
 410	return snd_ali_codec_peek(codec, ac97->num, reg);
 411}
 412
 413/*
 414 *	AC97 Reset
 415 */
 416
 417static int snd_ali_reset_5451(struct snd_ali *codec)
 418{
 419	struct pci_dev *pci_dev;
 420	unsigned short wCount, wReg;
 421	unsigned int   dwVal;
 422	
 423	pci_dev = codec->pci_m1533;
 424	if (pci_dev) {
 425		pci_read_config_dword(pci_dev, 0x7c, &dwVal);
 426		pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
 427		mdelay(5);
 428		pci_read_config_dword(pci_dev, 0x7c, &dwVal);
 429		pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
 430		mdelay(5);
 431	}
 432	
 433	pci_dev = codec->pci;
 434	pci_read_config_dword(pci_dev, 0x44, &dwVal);
 435	pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
 436	udelay(500);
 437	pci_read_config_dword(pci_dev, 0x44, &dwVal);
 438	pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
 439	mdelay(5);
 440	
 441	wCount = 200;
 442	while(wCount--) {
 443		wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
 444		if ((wReg & 0x000f) == 0x000f)
 445			return 0;
 446		mdelay(5);
 447	}
 448
 449	/* non-fatal if you have a non PM capable codec */
 450	/* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */
 451	return 0;
 452}
 453
 454/*
 455 *  ALI 5451 Controller
 456 */
 457
 458static void snd_ali_enable_special_channel(struct snd_ali *codec,
 459					   unsigned int channel)
 460{
 461	unsigned long dwVal;
 462
 463	dwVal  = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 464	dwVal |= 1 << (channel & 0x0000001f);
 465	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 466}
 467
 468static void snd_ali_disable_special_channel(struct snd_ali *codec,
 469					    unsigned int channel)
 470{
 471	unsigned long dwVal;
 472
 473	dwVal  = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 474	dwVal &= ~(1 << (channel & 0x0000001f));
 475	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 476}
 477
 478static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
 479{
 480	unsigned int gc;
 481
 482	gc  = inl(ALI_REG(codec, ALI_GC_CIR));
 483	gc |= ENDLP_IE;
 484	gc |= MIDLP_IE;
 485	outl( gc, ALI_REG(codec, ALI_GC_CIR));
 486}
 487
 488static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
 489{
 490	unsigned int gc;
 491
 492	gc  = inl(ALI_REG(codec, ALI_GC_CIR));
 493	gc &= ~ENDLP_IE;
 494	gc &= ~MIDLP_IE;
 495	outl(gc, ALI_REG(codec, ALI_GC_CIR));
 496}
 497
 498static void snd_ali_disable_voice_irq(struct snd_ali *codec,
 499				      unsigned int channel)
 500{
 501	unsigned int mask;
 502	struct snd_ali_channel_control *pchregs = &(codec->chregs);
 503
 504	dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel);
 505
 506	mask = 1 << (channel & 0x1f);
 507	pchregs->data.ainten  = inl(ALI_REG(codec, pchregs->regs.ainten));
 508	pchregs->data.ainten &= ~mask;
 509	outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
 510}
 511
 512static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
 513{
 514	unsigned int idx =  channel & 0x1f;
 515
 516	if (codec->synth.chcnt >= ALI_CHANNELS){
 517		dev_err(codec->card->dev,
 518			   "ali_alloc_pcm_channel: no free channels.\n");
 519		return -1;
 520	}
 521
 522	if (!(codec->synth.chmap & (1 << idx))) {
 523		codec->synth.chmap |= 1 << idx;
 524		codec->synth.chcnt++;
 525		dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx);
 526		return idx;
 527	}
 528	return -1;
 529}
 530
 531static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
 532{
 533	int idx;
 534	int result = -1;
 535
 536	dev_dbg(codec->card->dev,
 537		"find_free_channel: for %s\n", rec ? "rec" : "pcm");
 538
 539	/* recording */
 540	if (rec) {
 541		if (codec->spdif_support &&
 542		    (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
 543		     ALI_SPDIF_IN_SUPPORT))
 544			idx = ALI_SPDIF_IN_CHANNEL;
 545		else
 546			idx = ALI_PCM_IN_CHANNEL;
 547
 548		result = snd_ali_alloc_pcm_channel(codec, idx);
 549		if (result >= 0)
 550			return result;
 551		else {
 552			dev_err(codec->card->dev,
 553				"ali_find_free_channel: record channel is busy now.\n");
 554			return -1;
 555		}
 556	}
 557
 558	/* playback... */
 559	if (codec->spdif_support &&
 560	    (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
 561	     ALI_SPDIF_OUT_CH_ENABLE)) {
 562		idx = ALI_SPDIF_OUT_CHANNEL;
 563		result = snd_ali_alloc_pcm_channel(codec, idx);
 564		if (result >= 0)
 565			return result;
 566		else
 567			dev_err(codec->card->dev,
 568				"ali_find_free_channel: S/PDIF out channel is in busy now.\n");
 569	}
 570
 571	for (idx = 0; idx < ALI_CHANNELS; idx++) {
 572		result = snd_ali_alloc_pcm_channel(codec, idx);
 573		if (result >= 0)
 574			return result;
 575	}
 576	dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n");
 577	return -1;
 578}
 579
 580static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
 581{
 582	unsigned int idx = channel & 0x0000001f;
 583
 584	dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel);
 585
 586	if (channel < 0 || channel >= ALI_CHANNELS)
 587		return;
 588
 589	if (!(codec->synth.chmap & (1 << idx))) {
 590		dev_err(codec->card->dev,
 591			"ali_free_channel_pcm: channel %d is not in use.\n",
 592			channel);
 593		return;
 594	} else {
 595		codec->synth.chmap &= ~(1 << idx);
 596		codec->synth.chcnt--;
 597	}
 598}
 599
 600static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
 601{
 602	unsigned int mask = 1 << (channel & 0x1f);
 603
 604	dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel);
 605	outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
 606}
 607
 608/*
 609 *    S/PDIF Part
 610 */
 611
 612static void snd_ali_delay(struct snd_ali *codec,int interval)
 613{
 614	unsigned long  begintimer,currenttimer;
 615
 616	begintimer   = inl(ALI_REG(codec, ALI_STIMER));
 617	currenttimer = inl(ALI_REG(codec, ALI_STIMER));
 618
 619	while (currenttimer < begintimer + interval) {
 620		if (snd_ali_stimer_ready(codec) < 0)
 621			break;
 622		currenttimer = inl(ALI_REG(codec,  ALI_STIMER));
 623		cpu_relax();
 624	}
 625}
 626
 627static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
 628{
 629	u16 wval;
 630	u16 count = 0;
 631	u8  bval, R1 = 0, R2;
 632
 633	bval  = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
 634	bval |= 0x1F;
 635	outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
 636
 637	while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
 638		count ++;
 639		snd_ali_delay(codec, 6);
 640		bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
 641		R1 = bval & 0x1F;
 642	}
 643
 644	if (count > 50000) {
 645		dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
 646		return;
 647	}
 648
 649	for (count = 0; count <= 50000; count++) {
 650		snd_ali_delay(codec, 6);
 651		bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
 652		R2 = bval & 0x1F;
 653		if (R2 != R1)
 654			R1 = R2;
 655		else
 656			break;
 657	}
 658
 659	if (count > 50000) {
 660		dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
 661		return;
 662	}
 663
 664	if (R2 >= 0x0b && R2 <= 0x0e) {
 665		wval  = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 666		wval &= 0xe0f0;
 667		wval |= (0x09 << 8) | 0x05;
 668		outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 669
 670		bval  = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
 671		outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
 672	} else if (R2 == 0x12) {
 673		wval  = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 674		wval &= 0xe0f0;
 675		wval |= (0x0e << 8) | 0x08;
 676		outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
 677
 678		bval  = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
 679		outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
 680	}
 681}
 682
 683static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
 684{
 685	u32	dwRate;
 686	u8	bval;
 687
 688	bval  = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 689	bval &= 0x7f;
 690	bval |= 0x40;
 691	outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
 692
 693	snd_ali_detect_spdif_rate(codec);
 694
 695	bval  = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
 696	bval &= 0x0f;
 697
 698	switch (bval) {
 699	case 0: dwRate = 44100; break;
 700	case 1: dwRate = 48000; break;
 701	case 2: dwRate = 32000; break;
 702	default: dwRate = 0; break;
 703	}
 704
 705	return dwRate;
 706}
 707
 708static void snd_ali_enable_spdif_in(struct snd_ali *codec)
 709{	
 710	unsigned int dwVal;
 711
 712	dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 713	dwVal |= ALI_SPDIF_IN_SUPPORT;
 714	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 715
 716	dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 717	dwVal |= 0x02;
 718	outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
 719
 720	snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
 721}
 722
 723static void snd_ali_disable_spdif_in(struct snd_ali *codec)
 724{
 725	unsigned int dwVal;
 726	
 727	dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 728	dwVal &= ~ALI_SPDIF_IN_SUPPORT;
 729	outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 730	
 731	snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);	
 732}
 733
 734
 735static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
 736{
 737	unsigned char  bVal;
 738	unsigned int  dwRate;
 739	
 740	switch (rate) {
 741	case 32000: dwRate = 0x300; break;
 742	case 48000: dwRate = 0x200; break;
 743	default: dwRate = 0; break;
 744	}
 745	
 746	bVal  = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 747	bVal &= (unsigned char)(~(1<<6));
 748	
 749	bVal |= 0x80;		/* select right */
 750	outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
 751	outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
 752	
 753	bVal &= ~0x80;	/* select left */
 754	outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
 755	outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
 756}
 757
 758static void snd_ali_enable_spdif_out(struct snd_ali *codec)
 759{
 760	unsigned short wVal;
 761	unsigned char bVal;
 762        struct pci_dev *pci_dev;
 763
 764        pci_dev = codec->pci_m1533;
 765        if (pci_dev == NULL)
 766                return;
 767        pci_read_config_byte(pci_dev, 0x61, &bVal);
 768        bVal |= 0x40;
 769        pci_write_config_byte(pci_dev, 0x61, bVal);
 770        pci_read_config_byte(pci_dev, 0x7d, &bVal);
 771        bVal |= 0x01;
 772        pci_write_config_byte(pci_dev, 0x7d, bVal);
 773
 774        pci_read_config_byte(pci_dev, 0x7e, &bVal);
 775        bVal &= (~0x20);
 776        bVal |= 0x10;
 777        pci_write_config_byte(pci_dev, 0x7e, bVal);
 778
 779	bVal = inb(ALI_REG(codec, ALI_SCTRL));
 780	outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
 781
 782	bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
 783	outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
 784   
 785	wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 786	wVal |= ALI_SPDIF_OUT_SEL_PCM;
 787	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 788	snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
 789}
 790
 791static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
 792{
 793	unsigned short wVal;
 794
 795	wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 796   	wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
 797   	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 798/*
 799	wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
 800	if (flag & ALI_SPDIF_OUT_NON_PCM)
 801   		wVal |= 0x0002;
 802	else	
 803		wVal &= (~0x0002);
 804   	outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
 805*/
 806	snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
 807}
 808
 809static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
 810{
 811	unsigned short wVal;
 812
 813  	wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
 814   	wVal |= ALI_SPDIF_OUT_SEL_PCM;
 815   	outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
 816
 817	snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
 818}
 819
 820static void snd_ali_disable_spdif_out(struct snd_ali *codec)
 821{
 822	unsigned char  bVal;
 823
 824	bVal = inb(ALI_REG(codec, ALI_SCTRL));
 825	outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
 826
 827	snd_ali_disable_spdif_chnout(codec);
 828}
 829
 830static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
 831{
 832	struct snd_ali_voice *pvoice;
 833	struct snd_ali_channel_control *pchregs;
 834	unsigned int old, mask;
 835
 836	pchregs = &(codec->chregs);
 837
 838	/* check if interrupt occurred for channel */
 839	old  = pchregs->data.aint;
 840	mask = 1U << (channel & 0x1f);
 841
 842	if (!(old & mask))
 843		return;
 844
 845	pvoice = &codec->synth.voices[channel];
 846
 847	udelay(100);
 848	spin_lock(&codec->reg_lock);
 849
 850	if (pvoice->pcm && pvoice->substream) {
 851		/* pcm interrupt */
 852		if (pvoice->running) {
 853			dev_dbg(codec->card->dev,
 854				"update_ptr: cso=%4.4x cspf=%d.\n",
 855				inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)),
 856				(inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask);
 857			spin_unlock(&codec->reg_lock);
 858			snd_pcm_period_elapsed(pvoice->substream);
 859			spin_lock(&codec->reg_lock);
 860		} else {
 861			snd_ali_stop_voice(codec, channel);
 862			snd_ali_disable_voice_irq(codec, channel);
 863		}	
 864	} else if (codec->synth.voices[channel].synth) {
 865		/* synth interrupt */
 866	} else if (codec->synth.voices[channel].midi) {
 867		/* midi interrupt */
 868	} else {
 869		/* unknown interrupt */
 870		snd_ali_stop_voice(codec, channel);
 871		snd_ali_disable_voice_irq(codec, channel);
 872	}
 873	spin_unlock(&codec->reg_lock);
 874	outl(mask,ALI_REG(codec,pchregs->regs.aint));
 875	pchregs->data.aint = old & (~mask);
 876}
 877
 878static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
 879{
 880	struct snd_ali 	*codec = dev_id;
 881	int channel;
 882	unsigned int audio_int;
 883	struct snd_ali_channel_control *pchregs;
 884
 885	if (codec == NULL || !codec->hw_initialized)
 886		return IRQ_NONE;
 887
 888	audio_int = inl(ALI_REG(codec, ALI_MISCINT));
 889	if (!audio_int)
 890		return IRQ_NONE;
 891
 892	pchregs = &(codec->chregs);
 893	if (audio_int & ADDRESS_IRQ) {
 894		/* get interrupt status for all channels */
 895		pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
 896		for (channel = 0; channel < ALI_CHANNELS; channel++)
 897			snd_ali_update_ptr(codec, channel);
 898	}
 899	outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
 900		ALI_REG(codec, ALI_MISCINT));
 901
 902	return IRQ_HANDLED;
 903}
 904
 905
 906static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
 907						 int type, int rec, int channel)
 908{
 909	struct snd_ali_voice *pvoice;
 910	int idx;
 911
 912	dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec);
 913
 914	spin_lock_irq(&codec->voice_alloc);
 915	if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
 916		idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
 917			snd_ali_find_free_channel(codec,rec);
 918		if (idx < 0) {
 919			dev_err(codec->card->dev, "ali_alloc_voice: err.\n");
 920			spin_unlock_irq(&codec->voice_alloc);
 921			return NULL;
 922		}
 923		pvoice = &(codec->synth.voices[idx]);
 924		pvoice->codec = codec;
 925		pvoice->use = 1;
 926		pvoice->pcm = 1;
 927		pvoice->mode = rec;
 928		spin_unlock_irq(&codec->voice_alloc);
 929		return pvoice;
 930	}
 931	spin_unlock_irq(&codec->voice_alloc);
 932	return NULL;
 933}
 934
 935
 936static void snd_ali_free_voice(struct snd_ali * codec,
 937			       struct snd_ali_voice *pvoice)
 938{
 939	void (*private_free)(void *);
 940	void *private_data;
 941
 942	dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number);
 943	if (!pvoice->use)
 944		return;
 945	snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
 946	spin_lock_irq(&codec->voice_alloc);
 947	private_free = pvoice->private_free;
 948	private_data = pvoice->private_data;
 949	pvoice->private_free = NULL;
 950	pvoice->private_data = NULL;
 951	if (pvoice->pcm)
 952		snd_ali_free_channel_pcm(codec, pvoice->number);
 953	pvoice->use = pvoice->pcm = pvoice->synth = 0;
 954	pvoice->substream = NULL;
 955	spin_unlock_irq(&codec->voice_alloc);
 956	if (private_free)
 957		private_free(private_data);
 958}
 959
 960
 961static void snd_ali_clear_voices(struct snd_ali *codec,
 962				 unsigned int v_min,
 963				 unsigned int v_max)
 964{
 965	unsigned int i;
 966
 967	for (i = v_min; i <= v_max; i++) {
 968		snd_ali_stop_voice(codec, i);
 969		snd_ali_disable_voice_irq(codec, i);
 970	}
 971}
 972
 973static void snd_ali_write_voice_regs(struct snd_ali *codec,
 974			 unsigned int Channel,
 975			 unsigned int LBA,
 976			 unsigned int CSO,
 977			 unsigned int ESO,
 978			 unsigned int DELTA,
 979			 unsigned int ALPHA_FMS,
 980			 unsigned int GVSEL,
 981			 unsigned int PAN,
 982			 unsigned int VOL,
 983			 unsigned int CTRL,
 984			 unsigned int EC)
 985{
 986	unsigned int ctlcmds[4];
 987	
 988	outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
 989
 990	ctlcmds[0] =  (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
 991	ctlcmds[1] =  LBA;
 992	ctlcmds[2] =  (ESO << 16) | (DELTA & 0x0ffff);
 993	ctlcmds[3] =  (GVSEL << 31) |
 994		      ((PAN & 0x0000007f) << 24) |
 995		      ((VOL & 0x000000ff) << 16) |
 996		      ((CTRL & 0x0000000f) << 12) |
 997		      (EC & 0x00000fff);
 998
 999	outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1000
1001	outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1002	outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1003	outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1004	outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1005
1006	outl(0x30000000, ALI_REG(codec, ALI_EBUF1));	/* Still Mode */
1007	outl(0x30000000, ALI_REG(codec, ALI_EBUF2));	/* Still Mode */
1008}
1009
1010static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1011{
1012	unsigned int delta;
1013
1014	if (rate < 4000)
1015		rate = 4000;
1016	if (rate > 48000)
1017		rate = 48000;
1018
1019	if (rec) {
1020		if (rate == 44100)
1021			delta = 0x116a;
1022		else if (rate == 8000)
1023			delta = 0x6000;
1024		else if (rate == 48000)
1025			delta = 0x1000;
1026		else
1027			delta = ((48000 << 12) / rate) & 0x0000ffff;
1028	} else {
1029		if (rate == 44100)
1030			delta = 0xeb3;
1031		else if (rate == 8000)
1032			delta = 0x2ab;
1033		else if (rate == 48000)
1034			delta = 0x1000;
1035		else 
1036			delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1037	}
1038
1039	return delta;
1040}
1041
1042static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1043{
1044	unsigned int CTRL;
1045	struct snd_pcm_runtime *runtime = substream->runtime;
1046
1047	/* set ctrl mode
1048	   CTRL default: 8-bit (unsigned) mono, loop mode enabled
1049	 */
1050	CTRL = 0x00000001;
1051	if (snd_pcm_format_width(runtime->format) == 16)
1052		CTRL |= 0x00000008;	/* 16-bit data */
1053	if (!snd_pcm_format_unsigned(runtime->format))
1054		CTRL |= 0x00000002;	/* signed data */
1055	if (runtime->channels > 1)
1056		CTRL |= 0x00000004;	/* stereo data */
1057	return CTRL;
1058}
1059
1060/*
1061 *  PCM part
1062 */
1063
1064static int snd_ali_trigger(struct snd_pcm_substream *substream,
1065			       int cmd)
1066				    
1067{
1068	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1069	struct snd_pcm_substream *s;
1070	unsigned int what, whati;
1071	struct snd_ali_voice *pvoice, *evoice;
1072	unsigned int val;
1073	int do_start;
1074
1075	switch (cmd) {
1076	case SNDRV_PCM_TRIGGER_START:
1077	case SNDRV_PCM_TRIGGER_RESUME:
1078		do_start = 1;
1079		break;
1080	case SNDRV_PCM_TRIGGER_STOP:
1081	case SNDRV_PCM_TRIGGER_SUSPEND:
1082		do_start = 0;
1083		break;
1084	default:
1085		return -EINVAL;
1086	}
1087
1088	what = whati = 0;
1089	snd_pcm_group_for_each_entry(s, substream) {
1090		if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1091			pvoice = s->runtime->private_data;
1092			evoice = pvoice->extra;
1093			what |= 1 << (pvoice->number & 0x1f);
1094			if (evoice == NULL)
1095				whati |= 1 << (pvoice->number & 0x1f);
1096			else {
1097				whati |= 1 << (evoice->number & 0x1f);
1098				what |= 1 << (evoice->number & 0x1f);
1099			}
1100			if (do_start) {
1101				pvoice->running = 1;
1102				if (evoice != NULL)
1103					evoice->running = 1;
1104			} else {
1105				pvoice->running = 0;
1106				if (evoice != NULL)
1107					evoice->running = 0;
1108			}
1109			snd_pcm_trigger_done(s, substream);
 
 
1110		}
1111	}
1112	spin_lock(&codec->reg_lock);
1113	if (!do_start)
1114		outl(what, ALI_REG(codec, ALI_STOP));
1115	val = inl(ALI_REG(codec, ALI_AINTEN));
1116	if (do_start)
1117		val |= whati;
1118	else
1119		val &= ~whati;
1120	outl(val, ALI_REG(codec, ALI_AINTEN));
1121	if (do_start)
1122		outl(what, ALI_REG(codec, ALI_START));
1123	dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati);
1124	spin_unlock(&codec->reg_lock);
1125
1126	return 0;
1127}
1128
1129static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1130				      struct snd_pcm_hw_params *hw_params)
1131{
1132	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1133	struct snd_pcm_runtime *runtime = substream->runtime;
1134	struct snd_ali_voice *pvoice = runtime->private_data;
1135	struct snd_ali_voice *evoice = pvoice->extra;
 
1136
 
 
 
 
 
1137	/* voice management */
1138
1139	if (params_buffer_size(hw_params) / 2 !=
1140	    params_period_size(hw_params)) {
1141		if (!evoice) {
1142			evoice = snd_ali_alloc_voice(codec,
1143						     SNDRV_ALI_VOICE_TYPE_PCM,
1144						     0, -1);
1145			if (!evoice)
1146				return -ENOMEM;
1147			pvoice->extra = evoice;
1148			evoice->substream = substream;
1149		}
1150	} else {
1151		if (evoice) {
1152			snd_ali_free_voice(codec, evoice);
1153			pvoice->extra = evoice = NULL;
1154		}
1155	}
1156
1157	return 0;
1158}
1159
1160static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1161{
1162	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1163	struct snd_pcm_runtime *runtime = substream->runtime;
1164	struct snd_ali_voice *pvoice = runtime->private_data;
1165	struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1166
 
1167	if (evoice) {
1168		snd_ali_free_voice(codec, evoice);
1169		pvoice->extra = NULL;
1170	}
1171	return 0;
1172}
1173
 
 
 
 
 
 
 
 
 
 
 
 
1174static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1175{
1176	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1177	struct snd_pcm_runtime *runtime = substream->runtime;
1178	struct snd_ali_voice *pvoice = runtime->private_data;
1179	struct snd_ali_voice *evoice = pvoice->extra;
1180
1181	unsigned int LBA;
1182	unsigned int Delta;
1183	unsigned int ESO;
1184	unsigned int CTRL;
1185	unsigned int GVSEL;
1186	unsigned int PAN;
1187	unsigned int VOL;
1188	unsigned int EC;
1189	
1190	dev_dbg(codec->card->dev, "playback_prepare ...\n");
1191
1192	spin_lock_irq(&codec->reg_lock);	
1193	
1194	/* set Delta (rate) value */
1195	Delta = snd_ali_convert_rate(runtime->rate, 0);
1196
1197	if (pvoice->number == ALI_SPDIF_IN_CHANNEL || 
1198	    pvoice->number == ALI_PCM_IN_CHANNEL)
1199		snd_ali_disable_special_channel(codec, pvoice->number);
1200	else if (codec->spdif_support &&
1201		 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1202		  ALI_SPDIF_OUT_CH_ENABLE)
1203		 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1204		snd_ali_set_spdif_out_rate(codec, runtime->rate);
1205		Delta = 0x1000;
1206	}
1207	
1208	/* set Loop Back Address */
1209	LBA = runtime->dma_addr;
1210
1211	/* set interrupt count size */
1212	pvoice->count = runtime->period_size;
1213
1214	/* set target ESO for channel */
1215	pvoice->eso = runtime->buffer_size; 
1216
1217	dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n",
1218		       pvoice->eso, pvoice->count);
1219
1220	/* set ESO to capture first MIDLP interrupt */
1221	ESO = pvoice->eso -1;
1222	/* set ctrl mode */
1223	CTRL = snd_ali_control_mode(substream);
1224
1225	GVSEL = 1;
1226	PAN = 0;
1227	VOL = 0;
1228	EC = 0;
1229	dev_dbg(codec->card->dev, "playback_prepare:\n");
1230	dev_dbg(codec->card->dev,
1231		"ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1232		       pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1233	snd_ali_write_voice_regs(codec,
1234				 pvoice->number,
1235				 LBA,
1236				 0,	/* cso */
1237				 ESO,
1238				 Delta,
1239				 0,	/* alpha */
1240				 GVSEL,
1241				 PAN,
1242				 VOL,
1243				 CTRL,
1244				 EC);
1245	if (evoice) {
1246		evoice->count = pvoice->count;
1247		evoice->eso = pvoice->count << 1;
1248		ESO = evoice->eso - 1;
1249		snd_ali_write_voice_regs(codec,
1250					 evoice->number,
1251					 LBA,
1252					 0,	/* cso */
1253					 ESO,
1254					 Delta,
1255					 0,	/* alpha */
1256					 GVSEL,
1257					 0x7f,
1258					 0x3ff,
1259					 CTRL,
1260					 EC);
1261	}
1262	spin_unlock_irq(&codec->reg_lock);
1263	return 0;
1264}
1265
1266
1267static int snd_ali_prepare(struct snd_pcm_substream *substream)
1268{
1269	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1270	struct snd_pcm_runtime *runtime = substream->runtime;
1271	struct snd_ali_voice *pvoice = runtime->private_data;
1272	unsigned int LBA;
1273	unsigned int Delta;
1274	unsigned int ESO;
1275	unsigned int CTRL;
1276	unsigned int GVSEL;
1277	unsigned int PAN;
1278	unsigned int VOL;
1279	unsigned int EC;
1280	u8	 bValue;
1281
1282	spin_lock_irq(&codec->reg_lock);
1283
1284	dev_dbg(codec->card->dev, "ali_prepare...\n");
1285
1286	snd_ali_enable_special_channel(codec,pvoice->number);
1287
1288	Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1289		 pvoice->number == ALI_MODEM_OUT_CHANNEL) ? 
1290		0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1291
1292	/* Prepare capture intr channel */
1293	if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1294
1295		unsigned int rate;
1296		
1297		spin_unlock_irq(&codec->reg_lock);
1298		if (codec->revision != ALI_5451_V02)
1299			return -1;
1300
1301		rate = snd_ali_get_spdif_in_rate(codec);
1302		if (rate == 0) {
1303			dev_warn(codec->card->dev,
1304				 "ali_capture_prepare: spdif rate detect err!\n");
1305			rate = 48000;
1306		}
1307		spin_lock_irq(&codec->reg_lock);
1308		bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1309		if (bValue & 0x10) {
1310			outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1311			dev_warn(codec->card->dev,
1312				 "clear SPDIF parity error flag.\n");
1313		}
1314
1315		if (rate != 48000)
1316			Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1317	}
1318
1319	/* set target ESO for channel  */
1320	pvoice->eso = runtime->buffer_size; 
1321
1322	/* set interrupt count size  */
1323	pvoice->count = runtime->period_size;
1324
1325	/* set Loop Back Address  */
1326	LBA = runtime->dma_addr;
1327
1328	/* set ESO to capture first MIDLP interrupt  */
1329	ESO = pvoice->eso - 1;
1330	CTRL = snd_ali_control_mode(substream);
1331	GVSEL = 0;
1332	PAN = 0x00;
1333	VOL = 0x00;
1334	EC = 0;
1335
1336	snd_ali_write_voice_regs(    codec,
1337				     pvoice->number,
1338				     LBA,
1339				     0,	/* cso */
1340				     ESO,
1341				     Delta,
1342				     0,	/* alpha */
1343				     GVSEL,
1344				     PAN,
1345				     VOL,
1346				     CTRL,
1347				     EC);
1348
1349	spin_unlock_irq(&codec->reg_lock);
1350
1351	return 0;
1352}
1353
1354
1355static snd_pcm_uframes_t
1356snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1357{
1358	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1359	struct snd_pcm_runtime *runtime = substream->runtime;
1360	struct snd_ali_voice *pvoice = runtime->private_data;
1361	unsigned int cso;
1362
1363	spin_lock(&codec->reg_lock);
1364	if (!pvoice->running) {
1365		spin_unlock(&codec->reg_lock);
1366		return 0;
1367	}
1368	outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1369	cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1370	spin_unlock(&codec->reg_lock);
1371	dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso);
1372
1373	cso %= runtime->buffer_size;
1374	return cso;
1375}
1376
1377
1378static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1379{
1380	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1381	struct snd_pcm_runtime *runtime = substream->runtime;
1382	struct snd_ali_voice *pvoice = runtime->private_data;
1383	unsigned int cso;
1384
1385	spin_lock(&codec->reg_lock);
1386	if (!pvoice->running) {
1387		spin_unlock(&codec->reg_lock);
1388		return 0;
1389	}
1390	outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1391	cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1392	spin_unlock(&codec->reg_lock);
1393
1394	cso %= runtime->buffer_size;
1395	return cso;
1396}
1397
1398static const struct snd_pcm_hardware snd_ali_playback =
1399{
1400	.info =		(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1401			 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1402			 SNDRV_PCM_INFO_MMAP_VALID |
1403			 SNDRV_PCM_INFO_RESUME |
1404			 SNDRV_PCM_INFO_SYNC_START),
1405	.formats =	(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1406			 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1407	.rates =	SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1408	.rate_min =		4000,
1409	.rate_max =		48000,
1410	.channels_min =		1,
1411	.channels_max =		2,
1412	.buffer_bytes_max =	(256*1024),
1413	.period_bytes_min =	64,
1414	.period_bytes_max =	(256*1024),
1415	.periods_min =		1,
1416	.periods_max =		1024,
1417	.fifo_size =		0,
1418};
1419
1420/*
1421 *  Capture support device description
1422 */
1423
1424static const struct snd_pcm_hardware snd_ali_capture =
1425{
1426	.info =		(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1427			 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1428			 SNDRV_PCM_INFO_MMAP_VALID |
1429			 SNDRV_PCM_INFO_RESUME |
1430			 SNDRV_PCM_INFO_SYNC_START),
1431	.formats =	(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1432			 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1433	.rates =	SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1434	.rate_min =		4000,
1435	.rate_max =		48000,
1436	.channels_min =		1,
1437	.channels_max =		2,
1438	.buffer_bytes_max =	(128*1024),
1439	.period_bytes_min =	64,
1440	.period_bytes_max =	(128*1024),
1441	.periods_min =		1,
1442	.periods_max =		1024,
1443	.fifo_size =		0,
1444};
1445
1446static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1447{
1448	struct snd_ali_voice *pvoice = runtime->private_data;
 
1449
1450	if (pvoice)
 
1451		snd_ali_free_voice(pvoice->codec, pvoice);
 
1452}
1453
1454static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1455			int channel, const struct snd_pcm_hardware *phw)
1456{
1457	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1458	struct snd_pcm_runtime *runtime = substream->runtime;
1459	struct snd_ali_voice *pvoice;
1460
1461	pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1462				     channel);
1463	if (!pvoice)
1464		return -EAGAIN;
1465
1466	pvoice->substream = substream;
1467	runtime->private_data = pvoice;
1468	runtime->private_free = snd_ali_pcm_free_substream;
1469
1470	runtime->hw = *phw;
1471	snd_pcm_set_sync(substream);
1472	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1473				     0, 64*1024);
1474	return 0;
1475}
1476
1477static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1478{
1479	return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1480}
1481
1482static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1483{
1484	return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1485}
1486
1487static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1488{
1489	return 0;
1490}
1491
1492static int snd_ali_close(struct snd_pcm_substream *substream)
1493{
1494	struct snd_ali *codec = snd_pcm_substream_chip(substream);
1495	struct snd_ali_voice *pvoice = substream->runtime->private_data;
1496
1497	snd_ali_disable_special_channel(codec,pvoice->number);
1498
1499	return 0;
1500}
1501
1502static const struct snd_pcm_ops snd_ali_playback_ops = {
1503	.open =		snd_ali_playback_open,
1504	.close =	snd_ali_playback_close,
 
1505	.hw_params =	snd_ali_playback_hw_params,
1506	.hw_free =	snd_ali_playback_hw_free,
1507	.prepare =	snd_ali_playback_prepare,
1508	.trigger =	snd_ali_trigger,
1509	.pointer =	snd_ali_playback_pointer,
1510};
1511
1512static const struct snd_pcm_ops snd_ali_capture_ops = {
1513	.open =		snd_ali_capture_open,
1514	.close =	snd_ali_close,
 
 
 
1515	.prepare =	snd_ali_prepare,
1516	.trigger =	snd_ali_trigger,
1517	.pointer =	snd_ali_pointer,
1518};
1519
1520/*
1521 * Modem PCM
1522 */
1523
1524static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1525				 struct snd_pcm_hw_params *hw_params)
1526{
1527	struct snd_ali *chip = snd_pcm_substream_chip(substream);
1528	unsigned int modem_num = chip->num_of_codecs - 1;
1529	snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1530		       params_rate(hw_params));
1531	snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1532	return 0;
1533}
1534
1535static const struct snd_pcm_hardware snd_ali_modem =
1536{
1537	.info =		(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1538			 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1539			 SNDRV_PCM_INFO_MMAP_VALID |
1540			 SNDRV_PCM_INFO_RESUME |
1541			 SNDRV_PCM_INFO_SYNC_START),
1542	.formats =	SNDRV_PCM_FMTBIT_S16_LE,
1543	.rates =	(SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1544			 SNDRV_PCM_RATE_16000),
1545	.rate_min =		8000,
1546	.rate_max =		16000,
1547	.channels_min =		1,
1548	.channels_max =		1,
1549	.buffer_bytes_max =	(256*1024),
1550	.period_bytes_min =	64,
1551	.period_bytes_max =	(256*1024),
1552	.periods_min =		1,
1553	.periods_max =		1024,
1554	.fifo_size =		0,
1555};
1556
1557static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1558			      int channel)
1559{
1560	static const unsigned int rates[] = {8000, 9600, 12000, 16000};
1561	static const struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1562		.count = ARRAY_SIZE(rates),
1563		.list = rates,
1564		.mask = 0,
1565	};
1566	int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1567
1568	if (err)
1569		return err;
1570	return snd_pcm_hw_constraint_list(substream->runtime, 0,
1571			SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1572}
1573
1574static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1575{
1576	return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1577}
1578
1579static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1580{
1581	return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1582}
1583
1584static const struct snd_pcm_ops snd_ali_modem_playback_ops = {
1585	.open =		snd_ali_modem_playback_open,
1586	.close =	snd_ali_close,
 
1587	.hw_params =	snd_ali_modem_hw_params,
 
1588	.prepare =	snd_ali_prepare,
1589	.trigger =	snd_ali_trigger,
1590	.pointer =	snd_ali_pointer,
1591};
1592
1593static const struct snd_pcm_ops snd_ali_modem_capture_ops = {
1594	.open =		snd_ali_modem_capture_open,
1595	.close =	snd_ali_close,
 
1596	.hw_params =	snd_ali_modem_hw_params,
 
1597	.prepare =	snd_ali_prepare,
1598	.trigger =	snd_ali_trigger,
1599	.pointer =	snd_ali_pointer,
1600};
1601
1602
1603struct ali_pcm_description {
1604	char *name;
1605	unsigned int playback_num;
1606	unsigned int capture_num;
1607	const struct snd_pcm_ops *playback_ops;
1608	const struct snd_pcm_ops *capture_ops;
1609	unsigned short class;
1610};
1611
1612
1613static void snd_ali_pcm_free(struct snd_pcm *pcm)
1614{
1615	struct snd_ali *codec = pcm->private_data;
1616	codec->pcm[pcm->device] = NULL;
1617}
1618
1619
1620static int snd_ali_pcm(struct snd_ali *codec, int device,
1621		       struct ali_pcm_description *desc)
1622{
1623	struct snd_pcm *pcm;
1624	int err;
1625
1626	err = snd_pcm_new(codec->card, desc->name, device,
1627			  desc->playback_num, desc->capture_num, &pcm);
1628	if (err < 0) {
1629		dev_err(codec->card->dev,
1630			"snd_ali_pcm: err called snd_pcm_new.\n");
1631		return err;
1632	}
1633	pcm->private_data = codec;
1634	pcm->private_free = snd_ali_pcm_free;
1635	if (desc->playback_ops)
1636		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1637				desc->playback_ops);
1638	if (desc->capture_ops)
1639		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1640				desc->capture_ops);
1641
1642	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1643				       &codec->pci->dev, 64*1024, 128*1024);
 
1644
1645	pcm->info_flags = 0;
1646	pcm->dev_class = desc->class;
1647	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1648	strcpy(pcm->name, desc->name);
1649	codec->pcm[0] = pcm;
1650	return 0;
1651}
1652
1653static struct ali_pcm_description ali_pcms[] = {
1654	{ .name = "ALI 5451",
1655	  .playback_num = ALI_CHANNELS,
1656	  .capture_num = 1,
1657	  .playback_ops = &snd_ali_playback_ops,
1658	  .capture_ops = &snd_ali_capture_ops
1659	},
1660	{ .name = "ALI 5451 modem",
1661	  .playback_num = 1,
1662	  .capture_num = 1,
1663	  .playback_ops = &snd_ali_modem_playback_ops,
1664	  .capture_ops = &snd_ali_modem_capture_ops,
1665	  .class = SNDRV_PCM_CLASS_MODEM
1666	}
1667};
1668
1669static int snd_ali_build_pcms(struct snd_ali *codec)
1670{
1671	int i, err;
1672	for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1673		err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1674		if (err < 0)
1675			return err;
1676	}
1677	return 0;
1678}
1679
1680
1681#define ALI5451_SPDIF(xname, xindex, value) \
1682{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1683.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1684.put = snd_ali5451_spdif_put, .private_value = value}
1685
1686#define snd_ali5451_spdif_info		snd_ctl_boolean_mono_info
1687
1688static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1689				 struct snd_ctl_elem_value *ucontrol)
1690{
1691	struct snd_ali *codec = kcontrol->private_data;
1692	unsigned int spdif_enable;
1693
1694	spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1695
1696	spin_lock_irq(&codec->reg_lock);
1697	switch (kcontrol->private_value) {
1698	case 0:
1699		spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1700		break;
1701	case 1:
1702		spdif_enable = ((codec->spdif_mask & 0x02) &&
1703			  (codec->spdif_mask & 0x04)) ? 1 : 0;
1704		break;
1705	case 2:
1706		spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1707		break;
1708	default:
1709		break;
1710	}
1711	ucontrol->value.integer.value[0] = spdif_enable;
1712	spin_unlock_irq(&codec->reg_lock);
1713	return 0;
1714}
1715
1716static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1717				 struct snd_ctl_elem_value *ucontrol)
1718{
1719	struct snd_ali *codec = kcontrol->private_data;
1720	unsigned int change = 0, spdif_enable = 0;
1721
1722	spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1723
1724	spin_lock_irq(&codec->reg_lock);
1725	switch (kcontrol->private_value) {
1726	case 0:
1727		change = (codec->spdif_mask & 0x02) ? 1 : 0;
1728		change = change ^ spdif_enable;
1729		if (change) {
1730			if (spdif_enable) {
1731				codec->spdif_mask |= 0x02;
1732				snd_ali_enable_spdif_out(codec);
1733			} else {
1734				codec->spdif_mask &= ~(0x02);
1735				codec->spdif_mask &= ~(0x04);
1736				snd_ali_disable_spdif_out(codec);
1737			}
1738		}
1739		break;
1740	case 1: 
1741		change = (codec->spdif_mask & 0x04) ? 1 : 0;
1742		change = change ^ spdif_enable;
1743		if (change && (codec->spdif_mask & 0x02)) {
1744			if (spdif_enable) {
1745				codec->spdif_mask |= 0x04;
1746				snd_ali_enable_spdif_chnout(codec);
1747			} else {
1748				codec->spdif_mask &= ~(0x04);
1749				snd_ali_disable_spdif_chnout(codec);
1750			}
1751		}
1752		break;
1753	case 2:
1754		change = (codec->spdif_mask & 0x01) ? 1 : 0;
1755		change = change ^ spdif_enable;
1756		if (change) {
1757			if (spdif_enable) {
1758				codec->spdif_mask |= 0x01;
1759				snd_ali_enable_spdif_in(codec);
1760			} else {
1761				codec->spdif_mask &= ~(0x01);
1762				snd_ali_disable_spdif_in(codec);
1763			}
1764		}
1765		break;
1766	default:
1767		break;
1768	}
1769	spin_unlock_irq(&codec->reg_lock);
1770	
1771	return change;
1772}
1773
1774static const struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
1775	/* spdif aplayback switch */
1776	/* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1777	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1778	/* spdif out to spdif channel */
1779	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1780	/* spdif in from spdif channel */
1781	ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1782};
1783
1784static int snd_ali_mixer(struct snd_ali *codec)
1785{
1786	struct snd_ac97_template ac97;
1787	unsigned int idx;
1788	int i, err;
1789	static const struct snd_ac97_bus_ops ops = {
1790		.write = snd_ali_codec_write,
1791		.read = snd_ali_codec_read,
1792	};
1793
1794	err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1795	if (err < 0)
1796		return err;
1797
1798	memset(&ac97, 0, sizeof(ac97));
1799	ac97.private_data = codec;
1800
1801	for (i = 0; i < codec->num_of_codecs; i++) {
1802		ac97.num = i;
1803		err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1804		if (err < 0) {
1805			dev_err(codec->card->dev,
1806				   "ali mixer %d creating error.\n", i);
1807			if (i == 0)
1808				return err;
1809			codec->num_of_codecs = 1;
1810			break;
1811		}
1812	}
1813
1814	if (codec->spdif_support) {
1815		for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1816			err = snd_ctl_add(codec->card,
1817					  snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1818			if (err < 0)
1819				return err;
1820		}
1821	}
1822	return 0;
1823}
1824
 
1825static int ali_suspend(struct device *dev)
1826{
 
1827	struct snd_card *card = dev_get_drvdata(dev);
1828	struct snd_ali *chip = card->private_data;
1829	struct snd_ali_image *im = &chip->image;
1830	int i, j;
1831
 
 
 
 
1832	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1833	for (i = 0; i < chip->num_of_codecs; i++)
 
1834		snd_ac97_suspend(chip->ac97[i]);
 
1835
1836	spin_lock_irq(&chip->reg_lock);
1837	
1838	im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1839	/* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1840	im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1841	
1842	/* disable all IRQ bits */
1843	outl(0, ALI_REG(chip, ALI_MISCINT));
1844	
1845	for (i = 0; i < ALI_GLOBAL_REGS; i++) {	
1846		if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1847			continue;
1848		im->regs[i] = inl(ALI_REG(chip, i*4));
1849	}
1850	
1851	for (i = 0; i < ALI_CHANNELS; i++) {
1852		outb(i, ALI_REG(chip, ALI_GC_CIR));
1853		for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1854			im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1855	}
1856
1857	/* stop all HW channel */
1858	outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1859
1860	spin_unlock_irq(&chip->reg_lock);
 
 
 
 
1861	return 0;
1862}
1863
1864static int ali_resume(struct device *dev)
1865{
 
1866	struct snd_card *card = dev_get_drvdata(dev);
1867	struct snd_ali *chip = card->private_data;
1868	struct snd_ali_image *im = &chip->image;
1869	int i, j;
1870
 
 
 
 
 
 
 
 
 
 
 
 
 
1871	spin_lock_irq(&chip->reg_lock);
1872	
1873	for (i = 0; i < ALI_CHANNELS; i++) {
1874		outb(i, ALI_REG(chip, ALI_GC_CIR));
1875		for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1876			outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1877	}
1878	
1879	for (i = 0; i < ALI_GLOBAL_REGS; i++) {	
1880		if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1881		    (i*4 == ALI_START))
1882			continue;
1883		outl(im->regs[i], ALI_REG(chip, i*4));
1884	}
1885	
1886	/* start HW channel */
1887	outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1888	/* restore IRQ enable bits */
1889	outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1890	
1891	spin_unlock_irq(&chip->reg_lock);
1892
1893	for (i = 0 ; i < chip->num_of_codecs; i++)
1894		snd_ac97_resume(chip->ac97[i]);
1895	
1896	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1897	return 0;
1898}
1899
1900static DEFINE_SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
 
 
 
 
1901
1902static void snd_ali_free(struct snd_card *card)
1903{
1904	struct snd_ali *codec = card->private_data;
1905
1906	if (codec->hw_initialized)
1907		snd_ali_disable_address_interrupt(codec);
 
 
 
 
 
 
 
 
1908	pci_dev_put(codec->pci_m1533);
1909	pci_dev_put(codec->pci_m7101);
 
 
1910}
1911
1912static int snd_ali_chip_init(struct snd_ali *codec)
1913{
1914	unsigned int legacy;
1915	unsigned char temp;
1916	struct pci_dev *pci_dev;
1917
1918	dev_dbg(codec->card->dev, "chip initializing ...\n");
1919
1920	if (snd_ali_reset_5451(codec)) {
1921		dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
1922		return -1;
1923	}
1924
1925	if (codec->revision == ALI_5451_V02) {
1926        	pci_dev = codec->pci_m1533;
1927		pci_read_config_byte(pci_dev, 0x59, &temp);
1928		temp |= 0x80;
1929		pci_write_config_byte(pci_dev, 0x59, temp);
1930	
1931		pci_dev = codec->pci_m7101;
1932		pci_read_config_byte(pci_dev, 0xb8, &temp);
1933		temp |= 0x20;
1934		pci_write_config_byte(pci_dev, 0xB8, temp);
1935	}
1936
1937	pci_read_config_dword(codec->pci, 0x44, &legacy);
1938	legacy &= 0xff00ff00;
1939	legacy |= 0x000800aa;
1940	pci_write_config_dword(codec->pci, 0x44, legacy);
1941
1942	outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
1943	outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
1944	outl(0xffffffff, ALI_REG(codec, ALI_AINT));
1945	outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
1946	outb(0x10, 	 ALI_REG(codec, ALI_MPUR2));
1947
1948	codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
1949	codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
1950						    AC97_EXTENDED_STATUS);
1951	if (codec->spdif_support) {
1952		snd_ali_enable_spdif_out(codec);
1953		codec->spdif_mask = 0x00000002;
1954	}
1955
1956	codec->num_of_codecs = 1;
1957
1958	/* secondary codec - modem */
1959	if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
1960		codec->num_of_codecs++;
1961		outl(inl(ALI_REG(codec, ALI_SCTRL)) |
1962		     (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
1963		      ALI_SCTRL_LINE_OUT_EN),
1964		     ALI_REG(codec, ALI_SCTRL));
1965	}
1966
1967	dev_dbg(codec->card->dev, "chip initialize succeed.\n");
1968	return 0;
1969
1970}
1971
1972/* proc for register dump */
1973static void snd_ali_proc_read(struct snd_info_entry *entry,
1974			      struct snd_info_buffer *buf)
1975{
1976	struct snd_ali *codec = entry->private_data;
1977	int i;
1978	for (i = 0; i < 256 ; i+= 4)
1979		snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
1980}
1981
1982static void snd_ali_proc_init(struct snd_ali *codec)
1983{
1984	snd_card_ro_proc_new(codec->card, "ali5451", codec, snd_ali_proc_read);
 
 
1985}
1986
1987static int snd_ali_resources(struct snd_ali *codec)
1988{
1989	int err;
1990
1991	dev_dbg(codec->card->dev, "resources allocation ...\n");
1992	err = pci_request_regions(codec->pci, "ALI 5451");
1993	if (err < 0)
1994		return err;
1995	codec->port = pci_resource_start(codec->pci, 0);
1996
1997	if (devm_request_irq(&codec->pci->dev, codec->pci->irq,
1998			     snd_ali_card_interrupt,
1999			     IRQF_SHARED, KBUILD_MODNAME, codec)) {
2000		dev_err(codec->card->dev, "Unable to request irq.\n");
2001		return -EBUSY;
2002	}
2003	codec->irq = codec->pci->irq;
2004	codec->card->sync_irq = codec->irq;
2005	dev_dbg(codec->card->dev, "resources allocated.\n");
2006	return 0;
2007}
 
 
 
 
 
 
2008
2009static int snd_ali_create(struct snd_card *card,
2010			  struct pci_dev *pci,
2011			  int pcm_streams,
2012			  int spdif_support)
 
2013{
2014	struct snd_ali *codec = card->private_data;
2015	int i, err;
2016	unsigned short cmdw;
 
 
 
 
 
2017
2018	dev_dbg(card->dev, "creating ...\n");
2019
2020	/* enable PCI device */
2021	err = pcim_enable_device(pci);
2022	if (err < 0)
2023		return err;
2024	/* check, if we can restrict PCI DMA transfers to 31 bits */
2025	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(31))) {
 
2026		dev_err(card->dev,
2027			"architecture does not support 31bit PCI busmaster DMA\n");
 
2028		return -ENXIO;
2029	}
2030
 
 
 
 
 
 
2031	spin_lock_init(&codec->reg_lock);
2032	spin_lock_init(&codec->voice_alloc);
2033
2034	codec->card = card;
2035	codec->pci = pci;
2036	codec->irq = -1;
2037	codec->revision = pci->revision;
2038	codec->spdif_support = spdif_support;
2039
2040	if (pcm_streams < 1)
2041		pcm_streams = 1;
2042	if (pcm_streams > 32)
2043		pcm_streams = 32;
2044	
2045	pci_set_master(pci);
2046	pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2047	if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2048		cmdw |= PCI_COMMAND_IO;
2049		pci_write_config_word(pci, PCI_COMMAND, cmdw);
2050	}
 
2051	
2052	if (snd_ali_resources(codec))
 
2053		return -EBUSY;
2054	card->private_free = snd_ali_free;
 
 
2055
2056	codec->synth.chmap = 0;
2057	codec->synth.chcnt = 0;
2058	codec->spdif_mask = 0;
2059	codec->synth.synthcount = 0;
2060
2061	if (codec->revision == ALI_5451_V02)
2062		codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2063	else
2064		codec->chregs.regs.ac97read = ALI_AC97_READ;
2065	codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2066
2067	codec->chregs.regs.start  = ALI_START;
2068	codec->chregs.regs.stop   = ALI_STOP;
2069	codec->chregs.regs.aint   = ALI_AINT;
2070	codec->chregs.regs.ainten = ALI_AINTEN;
2071
2072	codec->chregs.data.start  = 0x00;
2073	codec->chregs.data.stop   = 0x00;
2074	codec->chregs.data.aint   = 0x00;
2075	codec->chregs.data.ainten = 0x00;
2076
2077	/* M1533: southbridge */
2078	codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2079	if (!codec->pci_m1533) {
2080		dev_err(card->dev, "cannot find ALi 1533 chip.\n");
 
2081		return -ENODEV;
2082	}
2083	/* M7101: power management */
2084	codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2085	if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2086		dev_err(card->dev, "cannot find ALi 7101 chip.\n");
 
2087		return -ENODEV;
2088	}
2089
 
 
 
 
 
 
 
2090	/* initialise synth voices*/
2091	for (i = 0; i < ALI_CHANNELS; i++)
2092		codec->synth.voices[i].number = i;
2093
2094	err = snd_ali_chip_init(codec);
2095	if (err < 0) {
2096		dev_err(card->dev, "ali create: chip init error.\n");
2097		return err;
2098	}
2099
 
 
 
 
 
 
2100	snd_ali_enable_address_interrupt(codec);
2101	codec->hw_initialized = 1;
 
 
 
2102	return 0;
2103}
2104
2105static int __snd_ali_probe(struct pci_dev *pci,
2106			   const struct pci_device_id *pci_id)
2107{
2108	struct snd_card *card;
2109	struct snd_ali *codec;
2110	int err;
2111
2112	dev_dbg(&pci->dev, "probe ...\n");
2113
2114	err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
2115				sizeof(*codec), &card);
2116	if (err < 0)
2117		return err;
2118	codec = card->private_data;
2119
2120	err = snd_ali_create(card, pci, pcm_channels, spdif);
2121	if (err < 0)
2122		return err;
 
2123
2124	dev_dbg(&pci->dev, "mixer building ...\n");
2125	err = snd_ali_mixer(codec);
2126	if (err < 0)
2127		return err;
2128	
2129	dev_dbg(&pci->dev, "pcm building ...\n");
2130	err = snd_ali_build_pcms(codec);
2131	if (err < 0)
2132		return err;
2133
2134	snd_ali_proc_init(codec);
2135
2136	strcpy(card->driver, "ALI5451");
2137	strcpy(card->shortname, "ALI 5451");
2138	
2139	sprintf(card->longname, "%s at 0x%lx, irq %i",
2140		card->shortname, codec->port, codec->irq);
2141
2142	dev_dbg(&pci->dev, "register card.\n");
2143	err = snd_card_register(card);
2144	if (err < 0)
2145		return err;
2146
2147	pci_set_drvdata(pci, card);
2148	return 0;
 
 
 
 
2149}
2150
2151static int snd_ali_probe(struct pci_dev *pci,
2152			 const struct pci_device_id *pci_id)
2153{
2154	return snd_card_free_on_error(&pci->dev, __snd_ali_probe(pci, pci_id));
2155}
2156
2157static struct pci_driver ali5451_driver = {
2158	.name = KBUILD_MODNAME,
2159	.id_table = snd_ali_ids,
2160	.probe = snd_ali_probe,
 
2161	.driver = {
2162		.pm = &ali_pm,
2163	},
2164};                                
2165
2166module_pci_driver(ali5451_driver);