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