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