2 * Matt Wu <Matt_Wu@acersoftech.com.cn>
4 * Routines for control of ALi pci audio M5451
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.
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.
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
28 #include <sound/driver.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>
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}}");
48 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
49 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
50 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
51 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 32};
52 static int spdif[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
54 module_param_array(index, int, NULL, 0444);
55 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
56 module_param_array(id, charp, NULL, 0444);
57 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
58 module_param_array(enable, bool, NULL, 0444);
59 MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
60 module_param_array(pcm_channels, int, NULL, 0444);
61 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
62 module_param_array(spdif, bool, NULL, 0444);
63 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
66 * Debug part definitions
72 #define snd_ali_printk(format, args...) printk(format, ##args);
74 #define snd_ali_printk(format, args...)
78 * Constants definition
81 #ifndef PCI_VENDOR_ID_ALI
82 #define PCI_VENDOR_ID_ALI 0x10b9
85 #ifndef PCI_DEVICE_ID_ALI_5451
86 #define PCI_DEVICE_ID_ALI_5451 0x5451
89 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_ALI<<16)|PCI_DEVICE_ID_ALI_5451)
92 #define ALI_CHANNELS 32
94 #define ALI_PCM_IN_CHANNEL 31
95 #define ALI_SPDIF_IN_CHANNEL 19
96 #define ALI_SPDIF_OUT_CHANNEL 15
97 #define ALI_CENTER_CHANNEL 24
98 #define ALI_LEF_CHANNEL 23
99 #define ALI_SURR_LEFT_CHANNEL 26
100 #define ALI_SURR_RIGHT_CHANNEL 25
101 #define ALI_MODEM_IN_CHANNEL 21
102 #define ALI_MODEM_OUT_CHANNEL 20
104 #define SNDRV_ALI_VOICE_TYPE_PCM 01
105 #define SNDRV_ALI_VOICE_TYPE_OTH 02
107 #define ALI_5451_V02 0x02
113 #define ALI_LEGACY_DMAR0 0x00 // ADR0
114 #define ALI_LEGACY_DMAR4 0x04 // CNT0
115 #define ALI_LEGACY_DMAR11 0x0b // MOD
116 #define ALI_LEGACY_DMAR15 0x0f // MMR
117 #define ALI_MPUR0 0x20
118 #define ALI_MPUR1 0x21
119 #define ALI_MPUR2 0x22
120 #define ALI_MPUR3 0x23
122 #define ALI_AC97_WRITE 0x40
123 #define ALI_AC97_READ 0x44
125 #define ALI_SCTRL 0x48
126 #define ALI_SPDIF_OUT_ENABLE 0x20
127 #define ALI_SCTRL_LINE_IN2 (1 << 9)
128 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
129 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
130 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
131 #define ALI_SCTRL_CODEC1_READY (1 << 24)
132 #define ALI_SCTRL_CODEC2_READY (1 << 25)
133 #define ALI_AC97_GPIO 0x4c
134 #define ALI_AC97_GPIO_ENABLE 0x8000
135 #define ALI_AC97_GPIO_DATA_SHIFT 16
136 #define ALI_SPDIF_CS 0x70
137 #define ALI_SPDIF_CTRL 0x74
138 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
139 #define ALI_SPDIF_IN_CH_STATUS 0x40
140 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
141 #define ALI_START 0x80
142 #define ALI_STOP 0x84
143 #define ALI_CSPF 0x90
144 #define ALI_AINT 0x98
145 #define ALI_GC_CIR 0xa0
146 #define ENDLP_IE 0x00001000
147 #define MIDLP_IE 0x00002000
148 #define ALI_AINTEN 0xa4
149 #define ALI_VOLUME 0xa8
150 #define ALI_SBDELTA_DELTA_R 0xac
151 #define ALI_MISCINT 0xb0
152 #define ADDRESS_IRQ 0x00000020
153 #define TARGET_REACHED 0x00008000
154 #define MIXER_OVERFLOW 0x00000800
155 #define MIXER_UNDERFLOW 0x00000400
156 #define GPIO_IRQ 0x01000000
157 #define ALI_SBBL_SBCL 0xc0
158 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
159 #define ALI_STIMER 0xc8
160 #define ALI_GLOBAL_CONTROL 0xd4
161 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
162 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
163 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
164 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
165 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
167 #define ALI_CSO_ALPHA_FMS 0xe0
169 #define ALI_ESO_DELTA 0xe8
170 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
171 #define ALI_EBUF1 0xf4
172 #define ALI_EBUF2 0xf8
174 #define ALI_REG(codec, x) ((codec)->port + x)
179 typedef struct snd_stru_ali ali_t;
180 typedef struct snd_ali_stru_voice snd_ali_voice_t;
182 typedef struct snd_ali_channel_control {
191 // register addresses
197 unsigned int ac97read;
198 unsigned int ac97write;
201 } snd_ali_channel_control_t;
203 struct snd_ali_stru_voice {
213 snd_pcm_substream_t *substream;
214 snd_ali_voice_t *extra;
216 unsigned int running: 1;
218 int eso; /* final ESO value for channel */
219 int count; /* runtime->period_size */
224 void (*private_free)(void *private_data);
228 typedef struct snd_stru_alidev {
230 snd_ali_voice_t voices[ALI_CHANNELS];
232 unsigned int chcnt; /* num of opened channels */
233 unsigned int chmap; /* bitmap for opened channels */
234 unsigned int synthcount;
240 #define ALI_GLOBAL_REGS 56
241 #define ALI_CHANNEL_REGS 8
242 typedef struct snd_ali_image {
243 unsigned long regs[ALI_GLOBAL_REGS];
244 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
249 struct snd_stru_ali {
252 unsigned char revision;
254 unsigned int hw_initialized: 1;
255 unsigned int spdif_support: 1;
258 struct pci_dev *pci_m1533;
259 struct pci_dev *pci_m7101;
262 snd_pcm_t *pcm[MAX_CODECS];
264 snd_ali_channel_control_t chregs;
267 unsigned int spdif_mask;
269 unsigned int spurious_irq_count;
270 unsigned int spurious_irq_max_delta;
272 unsigned int num_of_codecs;
274 ac97_bus_t *ac97_bus;
275 ac97_t *ac97[MAX_CODECS];
276 unsigned short ac97_ext_id;
277 unsigned short ac97_ext_status;
280 spinlock_t voice_alloc;
287 static struct pci_device_id snd_ali_ids[] = {
288 {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
291 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
293 static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
294 static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
295 static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
303 static void ali_read_regs(ali_t *codec, int channel)
308 printk("channel %d registers map:\n", channel);
309 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
313 printk("%2.2x ", j*4);
316 for (i=0; i<=0xf8/4;i++) {
318 printk("%2.2x ", (i*4/0x10)*0x10);
319 dwVal = inl(ALI_REG(codec,i*4));
320 printk("%8.8x ", dwVal);
326 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
329 struct pci_dev *pci_dev = NULL;
333 pci_dev = pci_find_device(vendor, deviceid, pci_dev);
337 printk("\nM%x PCI CFG\n", deviceid);
347 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
348 printk("%8.8x ", dwVal);
353 static void ali_read_ac97regs(ali_t *codec, int secondary)
358 printk("\ncodec %d registers map:\n", secondary);
362 printk("%2.2x ",j*2);
365 for (i=0; i<64;i++) {
367 printk("%2.2x ", (i/8)*0x10);
368 wVal = snd_ali_codec_peek(codec, secondary, i*2);
369 printk("%4.4x ", wVal);
382 static inline unsigned int snd_ali_5451_peek(ali_t *codec,
385 return (unsigned int)inl(ALI_REG(codec, port));
388 static inline void snd_ali_5451_poke( ali_t *codec,
392 outl((unsigned int)val, ALI_REG(codec, port));
395 static int snd_ali_codec_ready( ali_t *codec,
399 unsigned long end_time;
402 end_time = jiffies + 10 * msecs_to_jiffies(250);
404 res = snd_ali_5451_peek(codec,port);
405 if (! (res & 0x8000))
408 set_current_state(TASK_UNINTERRUPTIBLE);
411 } while (time_after_eq(end_time, jiffies));
412 snd_ali_5451_poke(codec, port, res & ~0x8000);
413 snd_printdd("ali_codec_ready: codec is not ready.\n ");
417 static int snd_ali_stimer_ready(ali_t *codec, int sched)
419 unsigned long end_time;
420 unsigned long dwChk1,dwChk2;
422 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
423 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
425 end_time = jiffies + 10 * msecs_to_jiffies(250);
427 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
428 if (dwChk2 != dwChk1)
431 set_current_state(TASK_UNINTERRUPTIBLE);
434 } while (time_after_eq(end_time, jiffies));
435 snd_printk("ali_stimer_read: stimer is not ready.\n");
439 static void snd_ali_codec_poke(ali_t *codec,int secondary,
443 unsigned int dwVal = 0;
444 unsigned int port = 0;
447 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
451 port = codec->chregs.regs.ac97write;
453 if (snd_ali_codec_ready(codec, port, 0) < 0)
455 if (snd_ali_stimer_ready(codec, 0) < 0)
458 dwVal = (unsigned int) (reg & 0xff);
459 dwVal |= 0x8000 | (val << 16);
460 if (secondary) dwVal |= 0x0080;
461 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
463 snd_ali_5451_poke(codec,port,dwVal);
468 static unsigned short snd_ali_codec_peek( ali_t *codec,
472 unsigned int dwVal = 0;
473 unsigned int port = 0;
476 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
480 port = codec->chregs.regs.ac97read;
482 if (snd_ali_codec_ready(codec, port, 0) < 0)
484 if (snd_ali_stimer_ready(codec, 0) < 0)
487 dwVal = (unsigned int) (reg & 0xff);
488 dwVal |= 0x8000; /* bit 15*/
489 if (secondary) dwVal |= 0x0080;
491 snd_ali_5451_poke(codec, port, dwVal);
493 if (snd_ali_stimer_ready(codec, 0) < 0)
495 if (snd_ali_codec_ready(codec, port, 0) < 0)
498 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
501 static void snd_ali_codec_write(ac97_t *ac97,
505 ali_t *codec = ac97->private_data;
507 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
508 if(reg == AC97_GPIO_STATUS) {
509 outl((val << ALI_AC97_GPIO_DATA_SHIFT)|ALI_AC97_GPIO_ENABLE,
510 ALI_REG(codec, ALI_AC97_GPIO));
513 snd_ali_codec_poke(codec, ac97->num, reg, val);
518 static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
520 ali_t *codec = ac97->private_data;
522 snd_ali_printk("codec_read reg=%xh.\n", reg);
523 return (snd_ali_codec_peek(codec, ac97->num, reg));
530 static int snd_ali_reset_5451(ali_t *codec)
532 struct pci_dev *pci_dev = NULL;
533 unsigned short wCount, wReg;
536 if ((pci_dev = codec->pci_m1533) != NULL) {
537 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
538 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
540 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
541 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
545 pci_dev = codec->pci;
546 pci_read_config_dword(pci_dev, 0x44, &dwVal);
547 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
549 pci_read_config_dword(pci_dev, 0x44, &dwVal);
550 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
555 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
556 if((wReg & 0x000f) == 0x000f)
561 /* non-fatal if you have a non PM capable codec */
562 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
568 static int snd_ali_reset_codec(ali_t *codec)
570 struct pci_dev *pci_dev = NULL;
571 unsigned char bVal = 0;
573 unsigned short wCount, wReg;
575 pci_dev = codec->pci_m1533;
577 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
578 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
580 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
581 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
584 bVal = inb(ALI_REG(codec,ALI_SCTRL));
586 outb(ALI_REG(codec,ALI_SCTRL),bVal);
588 bVal = inb(ALI_REG(codec,ALI_SCTRL));
590 outb(ALI_REG(codec,ALI_SCTRL),bVal);
595 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
596 if((wReg & 0x000f) == 0x000f)
606 * ALI 5451 Controller
609 static void snd_ali_enable_special_channel(ali_t *codec, unsigned int channel)
611 unsigned long dwVal = 0;
613 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
614 dwVal |= 1 << (channel & 0x0000001f);
615 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
618 static void snd_ali_disable_special_channel(ali_t *codec, unsigned int channel)
620 unsigned long dwVal = 0;
622 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
623 dwVal &= ~(1 << (channel & 0x0000001f));
624 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
627 static void snd_ali_enable_address_interrupt(ali_t * codec)
631 gc = inl(ALI_REG(codec, ALI_GC_CIR));
634 outl( gc, ALI_REG(codec, ALI_GC_CIR));
637 static void snd_ali_disable_address_interrupt(ali_t * codec)
641 gc = inl(ALI_REG(codec, ALI_GC_CIR));
644 outl(gc, ALI_REG(codec, ALI_GC_CIR));
648 static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
651 snd_ali_channel_control_t *pchregs = &(codec->chregs);
653 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
655 mask = 1 << (channel & 0x1f);
656 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
657 pchregs->data.ainten |= mask;
658 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
662 static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
665 snd_ali_channel_control_t *pchregs = &(codec->chregs);
667 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
669 mask = 1 << (channel & 0x1f);
670 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
671 pchregs->data.ainten &= ~mask;
672 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
675 static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
677 unsigned int idx = channel & 0x1f;
679 if (codec->synth.chcnt >= ALI_CHANNELS){
680 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
684 if (!(codec->synth.chmap & (1 << idx))) {
685 codec->synth.chmap |= 1 << idx;
686 codec->synth.chcnt++;
687 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
693 static int snd_ali_find_free_channel(ali_t * codec, int rec)
698 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
702 if (codec->spdif_support &&
703 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
704 idx = ALI_SPDIF_IN_CHANNEL;
706 idx = ALI_PCM_IN_CHANNEL;
708 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
711 snd_printk("ali_find_free_channel: record channel is busy now.\n");
717 if (codec->spdif_support &&
718 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
719 idx = ALI_SPDIF_OUT_CHANNEL;
720 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
723 snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
727 for (idx = 0; idx < ALI_CHANNELS; idx++) {
728 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
731 snd_printk("ali_find_free_channel: no free channels.\n");
735 static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
737 unsigned int idx = channel & 0x0000001f;
739 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
741 if (channel < 0 || channel >= ALI_CHANNELS)
744 if (!(codec->synth.chmap & (1 << idx))) {
745 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
748 codec->synth.chmap &= ~(1 << idx);
749 codec->synth.chcnt--;
754 static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
756 unsigned int mask = 1 << (channel & 0x1f);
758 snd_ali_printk("start_voice: channel=%d\n",channel);
759 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
763 static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
765 unsigned int mask = 1 << (channel & 0x1f);
767 snd_ali_printk("stop_voice: channel=%d\n",channel);
768 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
775 static void snd_ali_delay(ali_t *codec,int interval)
777 unsigned long begintimer,currenttimer;
779 begintimer = inl(ALI_REG(codec, ALI_STIMER));
780 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
782 while (currenttimer < begintimer + interval) {
783 if(snd_ali_stimer_ready(codec, 1) < 0)
785 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
789 static void snd_ali_detect_spdif_rate(ali_t *codec)
793 u8 bval = 0, R1 = 0, R2 = 0;
795 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
797 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
799 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
801 snd_ali_delay(codec, 6);
802 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
807 snd_printk("ali_detect_spdif_rate: timeout!\n");
812 while (count++ <= 50000) {
813 snd_ali_delay(codec, 6);
814 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
816 if (R2 != R1) R1 = R2; else break;
820 snd_printk("ali_detect_spdif_rate: timeout!\n");
824 if (R2 >= 0x0b && R2 <= 0x0e) {
825 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
827 wval |= (u16)0x09 << 8 | (u16)0x05;
828 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
830 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
831 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
832 } else if (R2 == 0x12) {
833 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
835 wval |= (u16)0x0E << 8 | (u16)0x08;
836 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
838 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
839 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
843 static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
848 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
851 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
853 snd_ali_detect_spdif_rate(codec);
855 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
858 if (bval == 0) dwRate = 44100;
859 if (bval == 1) dwRate = 48000;
860 if (bval == 2) dwRate = 32000;
865 static void snd_ali_enable_spdif_in(ali_t *codec)
869 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
870 dwVal |= ALI_SPDIF_IN_SUPPORT;
871 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
873 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
875 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
877 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
880 static void snd_ali_disable_spdif_in(ali_t *codec)
884 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
885 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
886 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
888 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
892 static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
895 unsigned int dwRate = 0;
897 if (rate == 32000) dwRate = 0x300;
898 if (rate == 44100) dwRate = 0;
899 if (rate == 48000) dwRate = 0x200;
901 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
902 bVal &= (unsigned char)(~(1<<6));
904 bVal |= 0x80; //select right
905 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
906 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
908 bVal &= (~0x80); //select left
909 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
910 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
913 static void snd_ali_enable_spdif_out(ali_t *codec)
918 struct pci_dev *pci_dev = NULL;
920 pci_dev = codec->pci_m1533;
923 pci_read_config_byte(pci_dev, 0x61, &bVal);
925 pci_write_config_byte(pci_dev, 0x61, bVal);
926 pci_read_config_byte(pci_dev, 0x7d, &bVal);
928 pci_write_config_byte(pci_dev, 0x7d, bVal);
930 pci_read_config_byte(pci_dev, 0x7e, &bVal);
933 pci_write_config_byte(pci_dev, 0x7e, bVal);
935 bVal = inb(ALI_REG(codec, ALI_SCTRL));
936 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
938 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
939 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
942 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
943 wVal |= ALI_SPDIF_OUT_SEL_PCM;
944 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
945 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
949 static void snd_ali_enable_spdif_chnout(ali_t *codec)
951 unsigned short wVal = 0;
953 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
954 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
955 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
957 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
958 if (flag & ALI_SPDIF_OUT_NON_PCM)
962 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
964 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
967 static void snd_ali_disable_spdif_chnout(ali_t *codec)
969 unsigned short wVal = 0;
970 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
971 wVal |= ALI_SPDIF_OUT_SEL_PCM;
972 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
974 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
977 static void snd_ali_disable_spdif_out(ali_t *codec)
981 bVal = inb(ALI_REG(codec, ALI_SCTRL));
982 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
984 snd_ali_disable_spdif_chnout(codec);
987 static void snd_ali_update_ptr(ali_t *codec,int channel)
989 snd_ali_voice_t *pvoice = NULL;
990 snd_pcm_runtime_t *runtime;
991 snd_ali_channel_control_t *pchregs = NULL;
992 unsigned int old, mask;
994 unsigned int temp, cspf;
997 pchregs = &(codec->chregs);
999 // check if interrupt occurred for channel
1000 old = pchregs->data.aint;
1001 mask = ((unsigned int) 1L) << (channel & 0x1f);
1006 pvoice = &codec->synth.voices[channel];
1007 runtime = pvoice->substream->runtime;
1010 spin_lock(&codec->reg_lock);
1012 if (pvoice->pcm && pvoice->substream) {
1015 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
1016 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1017 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
1019 if (pvoice->running) {
1020 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1021 spin_unlock(&codec->reg_lock);
1022 snd_pcm_period_elapsed(pvoice->substream);
1023 spin_lock(&codec->reg_lock);
1025 snd_ali_stop_voice(codec, channel);
1026 snd_ali_disable_voice_irq(codec, channel);
1028 } else if (codec->synth.voices[channel].synth) {
1029 /* synth interrupt */
1030 } else if (codec->synth.voices[channel].midi) {
1031 /* midi interrupt */
1033 /* unknown interrupt */
1034 snd_ali_stop_voice(codec, channel);
1035 snd_ali_disable_voice_irq(codec, channel);
1037 spin_unlock(&codec->reg_lock);
1038 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1039 pchregs->data.aint = old & (~mask);
1042 static void snd_ali_interrupt(ali_t * codec)
1045 unsigned int audio_int;
1046 snd_ali_channel_control_t *pchregs = NULL;
1047 pchregs = &(codec->chregs);
1049 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1050 if (audio_int & ADDRESS_IRQ) {
1051 // get interrupt status for all channels
1052 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1053 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1054 snd_ali_update_ptr(codec, channel);
1057 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1058 ALI_REG(codec,ALI_MISCINT));
1062 static irqreturn_t snd_ali_card_interrupt(int irq,
1064 struct pt_regs *regs)
1066 ali_t *codec = dev_id;
1070 snd_ali_interrupt(codec);
1075 static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec, int channel)
1077 snd_ali_voice_t *pvoice = NULL;
1078 unsigned long flags;
1081 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1083 spin_lock_irqsave(&codec->voice_alloc, flags);
1084 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1085 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1086 snd_ali_find_free_channel(codec,rec);
1088 snd_printk("ali_alloc_voice: err.\n");
1089 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1092 pvoice = &(codec->synth.voices[idx]);
1096 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1099 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1104 static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1106 unsigned long flags;
1107 void (*private_free)(void *);
1110 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1111 if (pvoice == NULL || !pvoice->use)
1113 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1114 spin_lock_irqsave(&codec->voice_alloc, flags);
1115 private_free = pvoice->private_free;
1116 private_data = pvoice->private_data;
1117 pvoice->private_free = NULL;
1118 pvoice->private_data = NULL;
1120 snd_ali_free_channel_pcm(codec, pvoice->number);
1122 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1123 pvoice->substream = NULL;
1124 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1126 private_free(private_data);
1130 static void snd_ali_clear_voices(ali_t * codec,
1136 for (i = v_min; i <= v_max; i++) {
1137 snd_ali_stop_voice(codec, i);
1138 snd_ali_disable_voice_irq(codec, i);
1142 static void snd_ali_write_voice_regs(ali_t * codec,
1143 unsigned int Channel,
1148 unsigned int ALPHA_FMS,
1155 unsigned int ctlcmds[4];
1157 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1159 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1161 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1162 ctlcmds[3] = (GVSEL << 31) |
1163 ((PAN & 0x0000007f) << 24) |
1164 ((VOL & 0x000000ff) << 16) |
1165 ((CTRL & 0x0000000f) << 12) |
1168 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1170 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1171 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1172 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1173 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1175 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1176 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1179 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1183 if (rate < 4000) rate = 4000;
1184 if (rate > 48000) rate = 48000;
1189 else if (rate == 8000)
1191 else if (rate == 48000)
1194 delta = ((48000 << 12) / rate) & 0x0000ffff;
1198 else if (rate == 8000)
1200 else if (rate == 48000)
1203 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1209 static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1212 snd_pcm_runtime_t *runtime = substream->runtime;
1215 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1218 if (snd_pcm_format_width(runtime->format) == 16)
1219 CTRL |= 0x00000008; // 16-bit data
1220 if (!snd_pcm_format_unsigned(runtime->format))
1221 CTRL |= 0x00000002; // signed data
1222 if (runtime->channels > 1)
1223 CTRL |= 0x00000004; // stereo data
1231 static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1232 unsigned int cmd, void *arg)
1234 return snd_pcm_lib_ioctl(substream, cmd, arg);
1237 static int snd_ali_trigger(snd_pcm_substream_t *substream,
1241 ali_t *codec = snd_pcm_substream_chip(substream);
1242 struct list_head *pos;
1243 snd_pcm_substream_t *s;
1244 unsigned int what, whati, capture_flag;
1245 snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1250 case SNDRV_PCM_TRIGGER_START:
1251 case SNDRV_PCM_TRIGGER_RESUME:
1252 do_start = 1; break;
1253 case SNDRV_PCM_TRIGGER_STOP:
1254 case SNDRV_PCM_TRIGGER_SUSPEND:
1255 do_start = 0; break;
1260 what = whati = capture_flag = 0;
1261 snd_pcm_group_for_each(pos, substream) {
1262 s = snd_pcm_group_substream_entry(pos);
1263 if ((ali_t *) snd_pcm_substream_chip(s) == codec) {
1264 pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1265 evoice = pvoice->extra;
1266 what |= 1 << (pvoice->number & 0x1f);
1267 if (evoice == NULL) {
1268 whati |= 1 << (pvoice->number & 0x1f);
1270 whati |= 1 << (evoice->number & 0x1f);
1271 what |= 1 << (evoice->number & 0x1f);
1274 pvoice->running = 1;
1276 evoice->running = 1;
1278 pvoice->running = 0;
1280 evoice->running = 0;
1282 snd_pcm_trigger_done(s, substream);
1287 spin_lock(&codec->reg_lock);
1289 outl(what, ALI_REG(codec, ALI_STOP));
1291 val = inl(ALI_REG(codec, ALI_AINTEN));
1297 outl(val, ALI_REG(codec, ALI_AINTEN));
1299 outl(what, ALI_REG(codec, ALI_START));
1301 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1302 spin_unlock(&codec->reg_lock);
1307 static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1308 snd_pcm_hw_params_t * hw_params)
1310 ali_t *codec = snd_pcm_substream_chip(substream);
1311 snd_pcm_runtime_t *runtime = substream->runtime;
1312 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1313 snd_ali_voice_t *evoice = pvoice->extra;
1315 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1316 if (err < 0) return err;
1318 /* voice management */
1320 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1321 if (evoice == NULL) {
1322 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0, -1);
1325 pvoice->extra = evoice;
1326 evoice->substream = substream;
1329 if (evoice != NULL) {
1330 snd_ali_free_voice(codec, evoice);
1331 pvoice->extra = evoice = NULL;
1338 static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1340 ali_t *codec = snd_pcm_substream_chip(substream);
1341 snd_pcm_runtime_t *runtime = substream->runtime;
1342 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1343 snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1345 snd_pcm_lib_free_pages(substream);
1346 if (evoice != NULL) {
1347 snd_ali_free_voice(codec, evoice);
1348 pvoice->extra = NULL;
1353 static int snd_ali_hw_params(snd_pcm_substream_t * substream,
1354 snd_pcm_hw_params_t * hw_params)
1356 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1359 static int snd_ali_hw_free(snd_pcm_substream_t * substream)
1361 return snd_pcm_lib_free_pages(substream);
1364 static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1366 ali_t *codec = snd_pcm_substream_chip(substream);
1367 snd_pcm_runtime_t *runtime = substream->runtime;
1368 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1369 snd_ali_voice_t *evoice = pvoice->extra;
1370 unsigned long flags;
1381 snd_ali_printk("playback_prepare ...\n");
1383 spin_lock_irqsave(&codec->reg_lock, flags);
1385 /* set Delta (rate) value */
1386 Delta = snd_ali_convert_rate(runtime->rate, 0);
1388 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1389 (pvoice->number == ALI_PCM_IN_CHANNEL))
1390 snd_ali_disable_special_channel(codec, pvoice->number);
1391 else if (codec->spdif_support &&
1392 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1393 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1394 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1398 /* set Loop Back Address */
1399 LBA = runtime->dma_addr;
1401 /* set interrupt count size */
1402 pvoice->count = runtime->period_size;
1404 /* set target ESO for channel */
1405 pvoice->eso = runtime->buffer_size;
1407 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1409 /* set ESO to capture first MIDLP interrupt */
1410 ESO = pvoice->eso -1;
1412 CTRL = snd_ali_control_mode(substream);
1418 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);
1419 snd_ali_write_voice_regs( codec,
1431 if (evoice != NULL) {
1432 evoice->count = pvoice->count;
1433 evoice->eso = pvoice->count << 1;
1434 ESO = evoice->eso - 1;
1435 snd_ali_write_voice_regs(codec,
1444 (unsigned int)0x3ff,
1448 spin_unlock_irqrestore(&codec->reg_lock, flags);
1453 static int snd_ali_prepare(snd_pcm_substream_t * substream)
1455 ali_t *codec = snd_pcm_substream_chip(substream);
1456 snd_pcm_runtime_t *runtime = substream->runtime;
1457 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1458 unsigned long flags;
1469 spin_lock_irqsave(&codec->reg_lock, flags);
1471 snd_ali_printk("ali_prepare...\n");
1473 snd_ali_enable_special_channel(codec,pvoice->number);
1475 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1476 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1477 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1479 // Prepare capture intr channel
1480 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1484 if (codec->revision != ALI_5451_V02) {
1485 spin_unlock_irqrestore(&codec->reg_lock, flags);
1488 rate = snd_ali_get_spdif_in_rate(codec);
1490 snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1493 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1494 if (bValue & 0x10) {
1495 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1496 printk("clear SPDIF parity error flag.\n");
1500 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1503 // set target ESO for channel
1504 pvoice->eso = runtime->buffer_size;
1506 // set interrupt count size
1507 pvoice->count = runtime->period_size;
1509 // set Loop Back Address
1510 LBA = runtime->dma_addr;
1512 // set ESO to capture first MIDLP interrupt
1513 ESO = pvoice->eso - 1;
1514 CTRL = snd_ali_control_mode(substream);
1520 snd_ali_write_voice_regs( codec,
1534 spin_unlock_irqrestore(&codec->reg_lock, flags);
1540 static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1542 ali_t *codec = snd_pcm_substream_chip(substream);
1543 snd_pcm_runtime_t *runtime = substream->runtime;
1544 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1547 spin_lock(&codec->reg_lock);
1548 if (!pvoice->running) {
1549 spin_unlock(&codec->reg_lock);
1552 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1553 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1554 spin_unlock(&codec->reg_lock);
1555 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1561 static snd_pcm_uframes_t snd_ali_pointer(snd_pcm_substream_t *substream)
1563 ali_t *codec = snd_pcm_substream_chip(substream);
1564 snd_pcm_runtime_t *runtime = substream->runtime;
1565 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1567 unsigned long flags;
1569 spin_lock_irqsave(&codec->reg_lock, flags);
1570 if (!pvoice->running) {
1571 spin_unlock_irqrestore(&codec->reg_lock, flags);
1574 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1575 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1576 spin_unlock_irqrestore(&codec->reg_lock, flags);
1581 static snd_pcm_hardware_t snd_ali_playback =
1583 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1584 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1585 SNDRV_PCM_INFO_MMAP_VALID |
1586 SNDRV_PCM_INFO_RESUME |
1587 SNDRV_PCM_INFO_SYNC_START),
1588 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1589 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1590 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1595 .buffer_bytes_max = (256*1024),
1596 .period_bytes_min = 64,
1597 .period_bytes_max = (256*1024),
1599 .periods_max = 1024,
1604 * Capture support device description
1607 static snd_pcm_hardware_t snd_ali_capture =
1609 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1610 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1611 SNDRV_PCM_INFO_MMAP_VALID |
1612 SNDRV_PCM_INFO_RESUME |
1613 SNDRV_PCM_INFO_SYNC_START),
1614 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1615 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1616 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1621 .buffer_bytes_max = (128*1024),
1622 .period_bytes_min = 64,
1623 .period_bytes_max = (128*1024),
1625 .periods_max = 1024,
1629 static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1631 unsigned long flags;
1632 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1636 codec = pvoice->codec;
1637 spin_lock_irqsave(&codec->reg_lock, flags);
1638 snd_ali_free_voice(pvoice->codec, pvoice);
1639 spin_unlock_irqrestore(&codec->reg_lock, flags);
1643 static int snd_ali_open(snd_pcm_substream_t * substream, int rec, int channel,
1644 snd_pcm_hardware_t *phw)
1646 ali_t *codec = snd_pcm_substream_chip(substream);
1647 snd_pcm_runtime_t *runtime = substream->runtime;
1648 snd_ali_voice_t *pvoice;
1649 unsigned long flags = 0;
1651 spin_lock_irqsave(&codec->reg_lock, flags);
1652 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec, channel);
1653 if (pvoice == NULL) {
1654 spin_unlock_irqrestore(&codec->reg_lock, flags);
1657 pvoice->codec = codec;
1658 spin_unlock_irqrestore(&codec->reg_lock, flags);
1660 pvoice->substream = substream;
1661 runtime->private_data = pvoice;
1662 runtime->private_free = snd_ali_pcm_free_substream;
1665 snd_pcm_set_sync(substream);
1666 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1670 static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1672 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1675 static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1677 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1680 static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1685 static int snd_ali_close(snd_pcm_substream_t * substream)
1687 ali_t *codec = snd_pcm_substream_chip(substream);
1688 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) substream->runtime->private_data;
1690 snd_ali_disable_special_channel(codec,pvoice->number);
1695 static snd_pcm_ops_t snd_ali_playback_ops = {
1696 .open = snd_ali_playback_open,
1697 .close = snd_ali_playback_close,
1698 .ioctl = snd_ali_ioctl,
1699 .hw_params = snd_ali_playback_hw_params,
1700 .hw_free = snd_ali_playback_hw_free,
1701 .prepare = snd_ali_playback_prepare,
1702 .trigger = snd_ali_trigger,
1703 .pointer = snd_ali_playback_pointer,
1706 static snd_pcm_ops_t snd_ali_capture_ops = {
1707 .open = snd_ali_capture_open,
1708 .close = snd_ali_close,
1709 .ioctl = snd_ali_ioctl,
1710 .hw_params = snd_ali_hw_params,
1711 .hw_free = snd_ali_hw_free,
1712 .prepare = snd_ali_prepare,
1713 .trigger = snd_ali_trigger,
1714 .pointer = snd_ali_pointer,
1721 static int snd_ali_modem_hw_params(snd_pcm_substream_t * substream,
1722 snd_pcm_hw_params_t * hw_params)
1724 ali_t *chip = snd_pcm_substream_chip(substream);
1725 unsigned int modem_num = chip->num_of_codecs - 1;
1726 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE, params_rate(hw_params));
1727 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1728 return snd_ali_hw_params(substream, hw_params);
1731 static snd_pcm_hardware_t snd_ali_modem =
1733 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1734 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1735 SNDRV_PCM_INFO_MMAP_VALID |
1736 SNDRV_PCM_INFO_RESUME |
1737 SNDRV_PCM_INFO_SYNC_START),
1738 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1739 .rates = SNDRV_PCM_RATE_KNOT|SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000,
1744 .buffer_bytes_max = (256*1024),
1745 .period_bytes_min = 64,
1746 .period_bytes_max = (256*1024),
1748 .periods_max = 1024,
1752 static int snd_ali_modem_open(snd_pcm_substream_t * substream, int rec, int channel)
1754 static unsigned int rates [] = {8000,9600,12000,16000};
1755 static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
1756 .count = ARRAY_SIZE(rates),
1760 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1763 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1764 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1767 static int snd_ali_modem_playback_open(snd_pcm_substream_t * substream)
1769 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1772 static int snd_ali_modem_capture_open(snd_pcm_substream_t * substream)
1774 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1777 static snd_pcm_ops_t snd_ali_modem_playback_ops = {
1778 .open = snd_ali_modem_playback_open,
1779 .close = snd_ali_close,
1780 .ioctl = snd_pcm_lib_ioctl,
1781 .hw_params = snd_ali_modem_hw_params,
1782 .hw_free = snd_ali_hw_free,
1783 .prepare = snd_ali_prepare,
1784 .trigger = snd_ali_trigger,
1785 .pointer = snd_ali_pointer,
1788 static snd_pcm_ops_t snd_ali_modem_capture_ops = {
1789 .open = snd_ali_modem_capture_open,
1790 .close = snd_ali_close,
1791 .ioctl = snd_pcm_lib_ioctl,
1792 .hw_params = snd_ali_modem_hw_params,
1793 .hw_free = snd_ali_hw_free,
1794 .prepare = snd_ali_prepare,
1795 .trigger = snd_ali_trigger,
1796 .pointer = snd_ali_pointer,
1800 struct ali_pcm_description {
1802 unsigned int playback_num;
1803 unsigned int capture_num;
1804 snd_pcm_ops_t *playback_ops;
1805 snd_pcm_ops_t *capture_ops;
1809 static void snd_ali_pcm_free(snd_pcm_t *pcm)
1811 ali_t *codec = pcm->private_data;
1812 codec->pcm[pcm->device] = NULL;
1816 static int __devinit snd_ali_pcm(ali_t * codec, int device, struct ali_pcm_description *desc)
1821 err = snd_pcm_new(codec->card, desc->name, device,
1822 desc->playback_num, desc->capture_num, &pcm);
1824 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1827 pcm->private_data = codec;
1828 pcm->private_free = snd_ali_pcm_free;
1829 pcm->info_flags = 0;
1830 if (desc->playback_ops)
1831 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, desc->playback_ops);
1832 if (desc->capture_ops)
1833 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, desc->capture_ops);
1835 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1836 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1838 pcm->info_flags = 0;
1839 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1840 strcpy(pcm->name, desc->name);
1841 codec->pcm[0] = pcm;
1845 struct ali_pcm_description ali_pcms[] = {
1846 { "ALI 5451", ALI_CHANNELS, 1, &snd_ali_playback_ops, &snd_ali_capture_ops },
1847 { "ALI 5451 modem", 1, 1, &snd_ali_modem_playback_ops, &snd_ali_modem_capture_ops }
1850 static int __devinit snd_ali_build_pcms(ali_t *codec)
1853 for(i = 0 ; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms) ; i++)
1854 if((err = snd_ali_pcm(codec, i, &ali_pcms[i])) < 0)
1860 #define ALI5451_SPDIF(xname, xindex, value) \
1861 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1862 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1863 .put = snd_ali5451_spdif_put, .private_value = value}
1865 static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1867 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1869 uinfo->value.integer.min = 0;
1870 uinfo->value.integer.max = 1;
1874 static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1876 unsigned long flags;
1877 ali_t *codec = kcontrol->private_data;
1878 unsigned int enable;
1880 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1882 spin_lock_irqsave(&codec->reg_lock, flags);
1883 switch(kcontrol->private_value) {
1885 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1888 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1891 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1896 ucontrol->value.integer.value[0] = enable;
1897 spin_unlock_irqrestore(&codec->reg_lock, flags);
1901 static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1903 unsigned long flags;
1904 ali_t *codec = kcontrol->private_data;
1905 unsigned int change = 0, enable = 0;
1907 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1909 spin_lock_irqsave(&codec->reg_lock, flags);
1910 switch (kcontrol->private_value) {
1912 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1913 change = change ^ enable;
1916 codec->spdif_mask |= 0x02;
1917 snd_ali_enable_spdif_out(codec);
1919 codec->spdif_mask &= ~(0x02);
1920 codec->spdif_mask &= ~(0x04);
1921 snd_ali_disable_spdif_out(codec);
1926 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1927 change = change ^ enable;
1928 if (change && (codec->spdif_mask & 0x02)) {
1930 codec->spdif_mask |= 0x04;
1931 snd_ali_enable_spdif_chnout(codec);
1933 codec->spdif_mask &= ~(0x04);
1934 snd_ali_disable_spdif_chnout(codec);
1939 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1940 change = change ^ enable;
1943 codec->spdif_mask |= 0x01;
1944 snd_ali_enable_spdif_in(codec);
1946 codec->spdif_mask &= ~(0x01);
1947 snd_ali_disable_spdif_in(codec);
1954 spin_unlock_irqrestore(&codec->reg_lock, flags);
1959 static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1960 /* spdif aplayback switch */
1961 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1962 ALI5451_SPDIF("IEC958 Output switch", 0, 0),
1963 /* spdif out to spdif channel */
1964 ALI5451_SPDIF("IEC958 Channel Output Switch", 0, 1),
1965 /* spdif in from spdif channel */
1966 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1969 static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1971 ali_t *codec = bus->private_data;
1972 codec->ac97_bus = NULL;
1975 static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1977 ali_t *codec = ac97->private_data;
1978 codec->ac97[ac97->num] = NULL;
1981 static int __devinit snd_ali_mixer(ali_t * codec)
1983 ac97_template_t ac97;
1986 static ac97_bus_ops_t ops = {
1987 .write = snd_ali_codec_write,
1988 .read = snd_ali_codec_read,
1991 if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1993 codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1995 memset(&ac97, 0, sizeof(ac97));
1996 ac97.private_data = codec;
1997 ac97.private_free = snd_ali_mixer_free_ac97;
1999 for ( i = 0 ; i < codec->num_of_codecs ; i++) {
2001 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i])) < 0) {
2002 snd_printk("ali mixer %d creating error.\n", i);
2008 if (codec->spdif_support) {
2009 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
2010 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
2011 if (err < 0) return err;
2018 static int ali_suspend(snd_card_t *card, pm_message_t state)
2020 ali_t *chip = card->pm_private_data;
2028 for(i = 0 ; i < chip->num_of_codecs ; i++) {
2030 snd_pcm_suspend_all(chip->pcm[i]);
2032 snd_ac97_suspend(chip->ac97[i]);
2035 spin_lock_irq(&chip->reg_lock);
2037 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
2038 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
2039 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
2041 // disable all IRQ bits
2042 outl(0, ALI_REG(chip, ALI_MISCINT));
2044 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2045 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
2047 im->regs[i] = inl(ALI_REG(chip, i*4));
2050 for (i = 0; i < ALI_CHANNELS; i++) {
2051 outb(i, ALI_REG(chip, ALI_GC_CIR));
2052 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2053 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
2056 // stop all HW channel
2057 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
2059 spin_unlock_irq(&chip->reg_lock);
2060 pci_disable_device(chip->pci);
2064 static int ali_resume(snd_card_t *card)
2066 ali_t *chip = card->pm_private_data;
2074 pci_enable_device(chip->pci);
2076 spin_lock_irq(&chip->reg_lock);
2078 for (i = 0; i < ALI_CHANNELS; i++) {
2079 outb(i, ALI_REG(chip, ALI_GC_CIR));
2080 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2081 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2084 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2085 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
2087 outl(im->regs[i], ALI_REG(chip, i*4));
2091 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2092 // restore IRQ enable bits
2093 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2095 spin_unlock_irq(&chip->reg_lock);
2097 for(i = 0 ; i < chip->num_of_codecs ; i++)
2099 snd_ac97_resume(chip->ac97[i]);
2103 #endif /* CONFIG_PM */
2105 static int snd_ali_free(ali_t * codec)
2107 if (codec->hw_initialized)
2108 snd_ali_disable_address_interrupt(codec);
2109 if (codec->irq >= 0) {
2110 synchronize_irq(codec->irq);
2111 free_irq(codec->irq, (void *)codec);
2114 pci_release_regions(codec->pci);
2115 pci_disable_device(codec->pci);
2117 kfree(codec->image);
2123 static int snd_ali_chip_init(ali_t *codec)
2125 unsigned int legacy;
2127 struct pci_dev *pci_dev = NULL;
2129 snd_ali_printk("chip initializing ... \n");
2131 if (snd_ali_reset_5451(codec)) {
2132 snd_printk("ali_chip_init: reset 5451 error.\n");
2136 if (codec->revision == ALI_5451_V02) {
2137 pci_dev = codec->pci_m1533;
2138 pci_read_config_byte(pci_dev, 0x59, &temp);
2140 pci_write_config_byte(pci_dev, 0x59, temp);
2142 pci_dev = codec->pci_m7101;
2143 pci_read_config_byte(pci_dev, 0xb8, &temp);
2145 pci_write_config_byte(pci_dev, 0xB8, temp);
2148 pci_read_config_dword(codec->pci, 0x44, &legacy);
2149 legacy &= 0xff00ff00;
2150 legacy |= 0x000800aa;
2151 pci_write_config_dword(codec->pci, 0x44, legacy);
2153 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2154 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2155 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2156 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2157 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2159 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2160 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2161 if (codec->spdif_support) {
2162 snd_ali_enable_spdif_out(codec);
2163 codec->spdif_mask = 0x00000002;
2166 codec->num_of_codecs = 1;
2168 /* secondary codec - modem */
2169 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2170 codec->num_of_codecs++;
2171 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2172 (ALI_SCTRL_LINE_IN2|ALI_SCTRL_GPIO_IN2|ALI_SCTRL_LINE_OUT_EN),
2173 ALI_REG(codec, ALI_SCTRL));
2176 snd_ali_printk("chip initialize succeed.\n");
2181 /* proc for register dump */
2182 static void snd_ali_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buf)
2184 ali_t *codec = entry->private_data;
2186 for(i = 0 ; i < 256 ; i+= 4)
2187 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2190 static void __devinit snd_ali_proc_init(ali_t *codec)
2192 snd_info_entry_t *entry;
2193 if(!snd_card_proc_new(codec->card, "ali5451", &entry))
2194 snd_info_set_text_ops(entry, codec, 1024, snd_ali_proc_read);
2197 static int __devinit snd_ali_resources(ali_t *codec)
2201 snd_ali_printk("resouces allocation ...\n");
2202 if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2204 codec->port = pci_resource_start(codec->pci, 0);
2206 if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2207 snd_printk("Unable to request irq.\n");
2210 codec->irq = codec->pci->irq;
2211 snd_ali_printk("resouces allocated.\n");
2214 static int snd_ali_dev_free(snd_device_t *device)
2216 ali_t *codec=device->device_data;
2217 snd_ali_free(codec);
2221 static int __devinit snd_ali_create(snd_card_t * card,
2222 struct pci_dev *pci,
2229 unsigned short cmdw = 0;
2230 struct pci_dev *pci_dev = NULL;
2231 static snd_device_ops_t ops = {
2232 (snd_dev_free_t *)snd_ali_dev_free,
2239 snd_ali_printk("creating ...\n");
2241 /* enable PCI device */
2242 if ((err = pci_enable_device(pci)) < 0)
2244 /* check, if we can restrict PCI DMA transfers to 31 bits */
2245 if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
2246 pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2247 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2248 pci_disable_device(pci);
2252 if ((codec = kcalloc(1, sizeof(*codec), GFP_KERNEL)) == NULL) {
2253 pci_disable_device(pci);
2257 spin_lock_init(&codec->reg_lock);
2258 spin_lock_init(&codec->voice_alloc);
2263 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2264 codec->spdif_support = spdif_support;
2266 if (pcm_streams < 1)
2268 if (pcm_streams > 32)
2271 pci_set_master(pci);
2272 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2273 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2274 cmdw |= PCI_COMMAND_IO;
2275 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2277 pci_set_master(pci);
2279 if (snd_ali_resources(codec)) {
2280 snd_ali_free(codec);
2284 synchronize_irq(pci->irq);
2286 codec->synth.chmap = 0;
2287 codec->synth.chcnt = 0;
2288 codec->spdif_mask = 0;
2289 codec->synth.synthcount = 0;
2291 if (codec->revision == ALI_5451_V02)
2292 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2294 codec->chregs.regs.ac97read = ALI_AC97_READ;
2295 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2297 codec->chregs.regs.start = ALI_START;
2298 codec->chregs.regs.stop = ALI_STOP;
2299 codec->chregs.regs.aint = ALI_AINT;
2300 codec->chregs.regs.ainten = ALI_AINTEN;
2302 codec->chregs.data.start = 0x00;
2303 codec->chregs.data.stop = 0x00;
2304 codec->chregs.data.aint = 0x00;
2305 codec->chregs.data.ainten = 0x00;
2307 /* M1533: southbridge */
2308 pci_dev = pci_find_device(0x10b9, 0x1533, NULL);
2309 codec->pci_m1533 = pci_dev;
2310 if (! codec->pci_m1533) {
2311 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2312 snd_ali_free(codec);
2315 /* M7101: power management */
2316 pci_dev = pci_find_device(0x10b9, 0x7101, NULL);
2317 codec->pci_m7101 = pci_dev;
2318 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2319 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2320 snd_ali_free(codec);
2324 snd_ali_printk("snd_device_new is called.\n");
2325 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2326 snd_ali_free(codec);
2330 /* initialise synth voices*/
2331 for (i = 0; i < ALI_CHANNELS; i++ ) {
2332 codec->synth.voices[i].number = i;
2335 if ((err = snd_ali_chip_init(codec)) < 0) {
2336 snd_printk("ali create: chip init error.\n");
2341 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2343 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2345 snd_card_set_pm_callback(card, ali_suspend, ali_resume, codec);
2348 snd_ali_enable_address_interrupt(codec);
2349 codec->hw_initialized = 1;
2352 snd_ali_printk("created.\n");
2356 static int __devinit snd_ali_probe(struct pci_dev *pci,
2357 const struct pci_device_id *pci_id)
2364 snd_ali_printk("probe ...\n");
2366 if (dev >= SNDRV_CARDS)
2373 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2377 if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2378 snd_card_free(card);
2382 snd_ali_printk("mixer building ...\n");
2383 if ((err = snd_ali_mixer(codec)) < 0) {
2384 snd_card_free(card);
2388 snd_ali_printk("pcm building ...\n");
2389 if ((err = snd_ali_build_pcms(codec)) < 0) {
2390 snd_card_free(card);
2394 snd_ali_proc_init(codec);
2396 strcpy(card->driver, "ALI5451");
2397 strcpy(card->shortname, "ALI 5451");
2399 sprintf(card->longname, "%s at 0x%lx, irq %li",
2400 card->shortname, codec->port, codec->irq);
2402 snd_ali_printk("register card.\n");
2403 if ((err = snd_card_register(card)) < 0) {
2404 snd_card_free(card);
2407 pci_set_drvdata(pci, card);
2412 static void __devexit snd_ali_remove(struct pci_dev *pci)
2414 snd_card_free(pci_get_drvdata(pci));
2415 pci_set_drvdata(pci, NULL);
2418 static struct pci_driver driver = {
2420 .id_table = snd_ali_ids,
2421 .probe = snd_ali_probe,
2422 .remove = __devexit_p(snd_ali_remove),
2423 SND_PCI_PM_CALLBACKS
2426 static int __init alsa_card_ali_init(void)
2428 return pci_register_driver(&driver);
2431 static void __exit alsa_card_ali_exit(void)
2433 pci_unregister_driver(&driver);
2436 module_init(alsa_card_ali_init)
2437 module_exit(alsa_card_ali_exit)