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