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