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