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