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};
55 module_param_array(index, int, boot_devs, 0444);
56 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
57 module_param_array(id, charp, boot_devs, 0444);
58 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
59 module_param_array(enable, bool, boot_devs, 0444);
60 MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
61 module_param_array(pcm_channels, int, boot_devs, 0444);
62 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
63 module_param_array(spdif, bool, boot_devs, 0444);
64 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
67 * Debug part definitions
73 #define snd_ali_printk(format, args...) printk(format, ##args);
75 #define snd_ali_printk(format, args...)
79 * Constants definition
82 #ifndef PCI_VENDOR_ID_ALI
83 #define PCI_VENDOR_ID_ALI 0x10b9
86 #ifndef PCI_DEVICE_ID_ALI_5451
87 #define PCI_DEVICE_ID_ALI_5451 0x5451
90 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_ALI<<16)|PCI_DEVICE_ID_ALI_5451)
93 #define ALI_CHANNELS 32
95 #define ALI_PCM_IN_CHANNEL 31
96 #define ALI_SPDIF_IN_CHANNEL 19
97 #define ALI_SPDIF_OUT_CHANNEL 15
98 #define ALI_CENTER_CHANNEL 24
99 #define ALI_LEF_CHANNEL 23
100 #define ALI_SURR_LEFT_CHANNEL 26
101 #define ALI_SURR_RIGHT_CHANNEL 25
103 #define SNDRV_ALI_VOICE_TYPE_PCM 01
104 #define SNDRV_ALI_VOICE_TYPE_OTH 02
106 #define ALI_5451_V02 0x02
112 #define ALI_LEGACY_DMAR0 0x00 // ADR0
113 #define ALI_LEGACY_DMAR4 0x04 // CNT0
114 #define ALI_LEGACY_DMAR11 0x0b // MOD
115 #define ALI_LEGACY_DMAR15 0x0f // MMR
116 #define ALI_MPUR0 0x20
117 #define ALI_MPUR1 0x21
118 #define ALI_MPUR2 0x22
119 #define ALI_MPUR3 0x23
121 #define ALI_AC97_WRITE 0x40
122 #define ALI_AC97_READ 0x44
124 #define ALI_SCTRL 0x48
125 #define ALI_SPDIF_OUT_ENABLE 0x20
126 #define ALI_AC97_GPIO 0x4c
127 #define ALI_SPDIF_CS 0x70
128 #define ALI_SPDIF_CTRL 0x74
129 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
130 #define ALI_SPDIF_IN_CH_STATUS 0x40
131 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
132 #define ALI_START 0x80
133 #define ALI_STOP 0x84
134 #define ALI_CSPF 0x90
135 #define ALI_AINT 0x98
136 #define ALI_GC_CIR 0xa0
137 #define ENDLP_IE 0x00001000
138 #define MIDLP_IE 0x00002000
139 #define ALI_AINTEN 0xa4
140 #define ALI_VOLUME 0xa8
141 #define ALI_SBDELTA_DELTA_R 0xac
142 #define ALI_MISCINT 0xb0
143 #define ADDRESS_IRQ 0x00000020
144 #define TARGET_REACHED 0x00008000
145 #define MIXER_OVERFLOW 0x00000800
146 #define MIXER_UNDERFLOW 0x00000400
147 #define ALI_SBBL_SBCL 0xc0
148 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
149 #define ALI_STIMER 0xc8
150 #define ALI_GLOBAL_CONTROL 0xd4
151 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
152 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
153 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
154 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
155 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
157 #define ALI_CSO_ALPHA_FMS 0xe0
159 #define ALI_ESO_DELTA 0xe8
160 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
161 #define ALI_EBUF1 0xf4
162 #define ALI_EBUF2 0xf8
164 #define ALI_REG(codec, x) ((codec)->port + x)
166 typedef struct snd_stru_ali ali_t;
167 typedef struct snd_ali_stru_voice snd_ali_voice_t;
169 typedef struct snd_ali_channel_control {
178 // register addresses
184 unsigned int ac97read;
185 unsigned int ac97write;
188 } snd_ali_channel_control_t;
190 struct snd_ali_stru_voice {
200 snd_pcm_substream_t *substream;
201 snd_ali_voice_t *extra;
205 int eso; /* final ESO value for channel */
206 int count; /* runtime->period_size */
211 void (*private_free)(void *private_data);
215 typedef struct snd_stru_alidev {
217 snd_ali_voice_t voices[ALI_CHANNELS];
219 unsigned int chcnt; /* num of opened channels */
220 unsigned int chmap; /* bitmap for opened channels */
221 unsigned int synthcount;
227 #define ALI_GLOBAL_REGS 56
228 #define ALI_CHANNEL_REGS 8
229 typedef struct snd_ali_image {
230 unsigned long regs[ALI_GLOBAL_REGS];
231 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
236 struct snd_stru_ali {
239 unsigned char revision;
241 unsigned int hw_initialized: 1;
242 unsigned int spdif_support: 1;
245 struct pci_dev *pci_m1533;
246 struct pci_dev *pci_m7101;
251 snd_ali_channel_control_t chregs;
254 unsigned int spdif_mask;
256 unsigned int spurious_irq_count;
257 unsigned int spurious_irq_max_delta;
259 ac97_bus_t *ac97_bus;
261 unsigned short ac97_ext_id;
262 unsigned short ac97_ext_status;
265 spinlock_t voice_alloc;
272 static struct pci_device_id snd_ali_ids[] = {
273 {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
276 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
278 static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
279 static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
280 static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
288 static void ali_read_regs(ali_t *codec, int channel)
293 printk("channel %d registers map:\n", channel);
294 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
298 printk("%2.2x ", j*4);
301 for (i=0; i<=0xf8/4;i++) {
303 printk("%2.2x ", (i*4/0x10)*0x10);
304 dwVal = inl(ALI_REG(codec,i*4));
305 printk("%8.8x ", dwVal);
311 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
314 struct pci_dev *pci_dev = NULL;
318 pci_dev = pci_find_device(vendor, deviceid, pci_dev);
322 printk("\nM%x PCI CFG\n", deviceid);
332 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
333 printk("%8.8x ", dwVal);
338 static void ali_read_ac97regs(ali_t *codec, int secondary)
343 printk("\ncodec %d registers map:\n", secondary);
347 printk("%2.2x ",j*2);
350 for (i=0; i<64;i++) {
352 printk("%2.2x ", (i/8)*0x10);
353 wVal = snd_ali_codec_peek(codec, secondary, i*2);
354 printk("%4.4x ", wVal);
367 static inline unsigned int snd_ali_5451_peek(ali_t *codec,
370 return (unsigned int)inl(ALI_REG(codec, port));
373 static inline void snd_ali_5451_poke( ali_t *codec,
377 outl((unsigned int)val, ALI_REG(codec, port));
380 static int snd_ali_codec_ready( ali_t *codec,
384 unsigned long end_time;
387 end_time = jiffies + 10 * (HZ >> 2);
389 res = snd_ali_5451_peek(codec,port);
390 if (! (res & 0x8000))
393 set_current_state(TASK_UNINTERRUPTIBLE);
396 } while (time_after_eq(end_time, jiffies));
397 snd_ali_5451_poke(codec, port, res & ~0x8000);
398 snd_printdd("ali_codec_ready: codec is not ready.\n ");
402 static int snd_ali_stimer_ready(ali_t *codec, int sched)
404 unsigned long end_time;
405 unsigned long dwChk1,dwChk2;
407 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
408 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
410 end_time = jiffies + 10 * (HZ >> 2);
412 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
413 if (dwChk2 != dwChk1)
416 set_current_state(TASK_UNINTERRUPTIBLE);
419 } while (time_after_eq(end_time, jiffies));
420 snd_printk("ali_stimer_read: stimer is not ready.\n");
424 static void snd_ali_codec_poke(ali_t *codec,int secondary,
428 unsigned int dwVal = 0;
429 unsigned int port = 0;
432 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
436 port = codec->chregs.regs.ac97write;
438 if (snd_ali_codec_ready(codec, port, 0) < 0)
440 if (snd_ali_stimer_ready(codec, 0) < 0)
443 dwVal = (unsigned int) (reg & 0xff);
444 dwVal |= 0x8000 | (val << 16);
445 if (secondary) dwVal |= 0x0080;
446 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
448 snd_ali_5451_poke(codec,port,dwVal);
453 static unsigned short snd_ali_codec_peek( ali_t *codec,
457 unsigned int dwVal = 0;
458 unsigned int port = 0;
461 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
465 port = codec->chregs.regs.ac97read;
467 if (snd_ali_codec_ready(codec, port, 0) < 0)
469 if (snd_ali_stimer_ready(codec, 0) < 0)
472 dwVal = (unsigned int) (reg & 0xff);
473 dwVal |= 0x8000; /* bit 15*/
474 if (secondary) dwVal |= 0x0080;
476 snd_ali_5451_poke(codec, port, dwVal);
478 if (snd_ali_stimer_ready(codec, 0) < 0)
480 if (snd_ali_codec_ready(codec, port, 0) < 0)
483 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
486 static void snd_ali_codec_write(ac97_t *ac97,
490 ali_t *codec = ac97->private_data;
492 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
493 snd_ali_codec_poke(codec, 0, reg, val);
498 static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
500 ali_t *codec = ac97->private_data;
502 snd_ali_printk("codec_read reg=%xh.\n", reg);
503 return (snd_ali_codec_peek(codec, 0, reg));
510 static int snd_ali_reset_5451(ali_t *codec)
512 struct pci_dev *pci_dev = NULL;
513 unsigned short wCount, wReg;
516 if ((pci_dev = codec->pci_m1533) != NULL) {
517 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
518 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
520 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
521 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
525 pci_dev = codec->pci;
526 pci_read_config_dword(pci_dev, 0x44, &dwVal);
527 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
529 pci_read_config_dword(pci_dev, 0x44, &dwVal);
530 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
535 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
536 if((wReg & 0x000f) == 0x000f)
541 /* non-fatal if you have a non PM capable codec */
542 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
548 static int snd_ali_reset_codec(ali_t *codec)
550 struct pci_dev *pci_dev = NULL;
551 unsigned char bVal = 0;
553 unsigned short wCount, wReg;
555 pci_dev = codec->pci_m1533;
557 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
558 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
560 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
561 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
564 bVal = inb(ALI_REG(codec,ALI_SCTRL));
566 outb(ALI_REG(codec,ALI_SCTRL),bVal);
568 bVal = inb(ALI_REG(codec,ALI_SCTRL));
570 outb(ALI_REG(codec,ALI_SCTRL),bVal);
575 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
576 if((wReg & 0x000f) == 0x000f)
586 * ALI 5451 Controller
589 static void snd_ali_enable_special_channel(ali_t *codec, unsigned int channel)
591 unsigned long dwVal = 0;
593 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
594 dwVal |= 1 << (channel & 0x0000001f);
595 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
598 static void snd_ali_disable_special_channel(ali_t *codec, unsigned int channel)
600 unsigned long dwVal = 0;
602 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
603 dwVal &= ~(1 << (channel & 0x0000001f));
604 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
607 static void snd_ali_enable_address_interrupt(ali_t * codec)
611 gc = inl(ALI_REG(codec, ALI_GC_CIR));
614 outl( gc, ALI_REG(codec, ALI_GC_CIR));
617 static void snd_ali_disable_address_interrupt(ali_t * codec)
621 gc = inl(ALI_REG(codec, ALI_GC_CIR));
624 outl(gc, ALI_REG(codec, ALI_GC_CIR));
628 static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
631 snd_ali_channel_control_t *pchregs = &(codec->chregs);
633 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
635 mask = 1 << (channel & 0x1f);
636 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
637 pchregs->data.ainten |= mask;
638 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
642 static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
645 snd_ali_channel_control_t *pchregs = &(codec->chregs);
647 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
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));
655 static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
657 unsigned int idx = channel & 0x1f;
659 if (codec->synth.chcnt >= ALI_CHANNELS){
660 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
664 if (!(codec->synth.chmap & (1 << idx))) {
665 codec->synth.chmap |= 1 << idx;
666 codec->synth.chcnt++;
667 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
673 static int snd_ali_find_free_channel(ali_t * codec, int rec)
678 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
682 if (codec->spdif_support &&
683 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
684 idx = ALI_SPDIF_IN_CHANNEL;
686 idx = ALI_PCM_IN_CHANNEL;
688 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
691 snd_printk("ali_find_free_channel: record channel is busy now.\n");
697 if (codec->spdif_support &&
698 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
699 idx = ALI_SPDIF_OUT_CHANNEL;
700 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
703 snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
707 for (idx = 0; idx < ALI_CHANNELS; idx++) {
708 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
711 snd_printk("ali_find_free_channel: no free channels.\n");
715 static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
717 unsigned int idx = channel & 0x0000001f;
719 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
721 if (channel < 0 || channel >= ALI_CHANNELS)
724 if (!(codec->synth.chmap & (1 << idx))) {
725 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
728 codec->synth.chmap &= ~(1 << idx);
729 codec->synth.chcnt--;
734 static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
736 unsigned int mask = 1 << (channel & 0x1f);
738 snd_ali_printk("start_voice: channel=%d\n",channel);
739 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
743 static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
745 unsigned int mask = 1 << (channel & 0x1f);
747 snd_ali_printk("stop_voice: channel=%d\n",channel);
748 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
755 static void snd_ali_delay(ali_t *codec,int interval)
757 unsigned long begintimer,currenttimer;
759 begintimer = inl(ALI_REG(codec, ALI_STIMER));
760 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
762 while (currenttimer < begintimer + interval) {
763 if(snd_ali_stimer_ready(codec, 1) < 0)
765 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
769 static void snd_ali_detect_spdif_rate(ali_t *codec)
773 u8 bval = 0, R1 = 0, R2 = 0;
775 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
777 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
779 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
781 snd_ali_delay(codec, 6);
782 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
787 snd_printk("ali_detect_spdif_rate: timeout!\n");
792 while (count++ <= 50000) {
793 snd_ali_delay(codec, 6);
794 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
796 if (R2 != R1) R1 = R2; else break;
800 snd_printk("ali_detect_spdif_rate: timeout!\n");
804 if (R2 >= 0x0b && R2 <= 0x0e) {
805 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
807 wval |= (u16)0x09 << 8 | (u16)0x05;
808 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
810 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
811 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
812 } else if (R2 == 0x12) {
813 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
815 wval |= (u16)0x0E << 8 | (u16)0x08;
816 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
818 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
819 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
823 static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
828 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
831 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
833 snd_ali_detect_spdif_rate(codec);
835 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
838 if (bval == 0) dwRate = 44100;
839 if (bval == 1) dwRate = 48000;
840 if (bval == 2) dwRate = 32000;
845 static void snd_ali_enable_spdif_in(ali_t *codec)
849 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
850 dwVal |= ALI_SPDIF_IN_SUPPORT;
851 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
853 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
855 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
857 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
860 static void snd_ali_disable_spdif_in(ali_t *codec)
864 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
865 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
866 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
868 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
872 static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
875 unsigned int dwRate = 0;
877 if (rate == 32000) dwRate = 0x300;
878 if (rate == 44100) dwRate = 0;
879 if (rate == 48000) dwRate = 0x200;
881 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
882 bVal &= (unsigned char)(~(1<<6));
884 bVal |= 0x80; //select right
885 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
886 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
888 bVal &= (~0x80); //select left
889 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
890 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
893 static void snd_ali_enable_spdif_out(ali_t *codec)
898 struct pci_dev *pci_dev = NULL;
900 pci_dev = codec->pci_m1533;
903 pci_read_config_byte(pci_dev, 0x61, &bVal);
905 pci_write_config_byte(pci_dev, 0x61, bVal);
906 pci_read_config_byte(pci_dev, 0x7d, &bVal);
908 pci_write_config_byte(pci_dev, 0x7d, bVal);
910 pci_read_config_byte(pci_dev, 0x7e, &bVal);
913 pci_write_config_byte(pci_dev, 0x7e, bVal);
915 bVal = inb(ALI_REG(codec, ALI_SCTRL));
916 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
918 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
919 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
922 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
923 wVal |= ALI_SPDIF_OUT_SEL_PCM;
924 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
925 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
929 static void snd_ali_enable_spdif_chnout(ali_t *codec)
931 unsigned short wVal = 0;
933 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
934 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
935 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
937 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
938 if (flag & ALI_SPDIF_OUT_NON_PCM)
942 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
944 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
947 static void snd_ali_disable_spdif_chnout(ali_t *codec)
949 unsigned short wVal = 0;
950 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
951 wVal |= ALI_SPDIF_OUT_SEL_PCM;
952 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
954 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
957 static void snd_ali_disable_spdif_out(ali_t *codec)
961 bVal = inb(ALI_REG(codec, ALI_SCTRL));
962 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
964 snd_ali_disable_spdif_chnout(codec);
967 static void snd_ali_update_ptr(ali_t *codec,int channel)
969 snd_ali_voice_t *pvoice = NULL;
970 snd_pcm_runtime_t *runtime;
971 snd_ali_channel_control_t *pchregs = NULL;
972 unsigned int old, mask;
974 unsigned int temp, cspf;
977 pchregs = &(codec->chregs);
979 // check if interrupt occurred for channel
980 old = pchregs->data.aint;
981 mask = ((unsigned int) 1L) << (channel & 0x1f);
986 pvoice = &codec->synth.voices[channel];
987 runtime = pvoice->substream->runtime;
990 spin_lock(&codec->reg_lock);
992 if (pvoice->pcm && pvoice->substream) {
995 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
996 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
997 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
999 if (pvoice->running) {
1000 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1001 spin_unlock(&codec->reg_lock);
1002 snd_pcm_period_elapsed(pvoice->substream);
1003 spin_lock(&codec->reg_lock);
1005 snd_ali_stop_voice(codec, channel);
1006 snd_ali_disable_voice_irq(codec, channel);
1008 } else if (codec->synth.voices[channel].synth) {
1009 /* synth interrupt */
1010 } else if (codec->synth.voices[channel].midi) {
1011 /* midi interrupt */
1013 /* unknown interrupt */
1014 snd_ali_stop_voice(codec, channel);
1015 snd_ali_disable_voice_irq(codec, channel);
1017 spin_unlock(&codec->reg_lock);
1018 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1019 pchregs->data.aint = old & (~mask);
1022 static void snd_ali_interrupt(ali_t * codec)
1025 unsigned int audio_int;
1026 snd_ali_channel_control_t *pchregs = NULL;
1027 pchregs = &(codec->chregs);
1029 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1030 if (audio_int & ADDRESS_IRQ) {
1031 // get interrupt status for all channels
1032 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1033 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1034 snd_ali_update_ptr(codec, channel);
1037 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1038 ALI_REG(codec,ALI_MISCINT));
1042 static irqreturn_t snd_ali_card_interrupt(int irq,
1044 struct pt_regs *regs)
1046 ali_t *codec = dev_id;
1050 snd_ali_interrupt(codec);
1055 static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec)
1057 snd_ali_voice_t *pvoice = NULL;
1058 unsigned long flags;
1061 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1063 spin_lock_irqsave(&codec->voice_alloc, flags);
1064 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1065 idx = snd_ali_find_free_channel(codec,rec);
1067 snd_printk("ali_alloc_voice: err.\n");
1068 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1071 pvoice = &(codec->synth.voices[idx]);
1075 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1078 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1083 static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1085 unsigned long flags;
1086 void (*private_free)(void *);
1089 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1090 if (pvoice == NULL || !pvoice->use)
1092 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1093 spin_lock_irqsave(&codec->voice_alloc, flags);
1094 private_free = pvoice->private_free;
1095 private_data = pvoice->private_data;
1096 pvoice->private_free = NULL;
1097 pvoice->private_data = NULL;
1099 snd_ali_free_channel_pcm(codec, pvoice->number);
1101 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1102 pvoice->substream = NULL;
1103 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1105 private_free(private_data);
1109 static void snd_ali_clear_voices(ali_t * codec,
1115 for (i = v_min; i <= v_max; i++) {
1116 snd_ali_stop_voice(codec, i);
1117 snd_ali_disable_voice_irq(codec, i);
1121 static void snd_ali_write_voice_regs(ali_t * codec,
1122 unsigned int Channel,
1127 unsigned int ALPHA_FMS,
1134 unsigned int ctlcmds[4];
1136 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1138 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1140 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1141 ctlcmds[3] = (GVSEL << 31) |
1142 ((PAN & 0x0000007f) << 24) |
1143 ((VOL & 0x000000ff) << 16) |
1144 ((CTRL & 0x0000000f) << 12) |
1147 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1149 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1150 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1151 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1152 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1154 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1155 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1158 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1162 if (rate < 4000) rate = 4000;
1163 if (rate > 48000) rate = 48000;
1168 else if (rate == 8000)
1170 else if (rate == 48000)
1173 delta = ((48000 << 12) / rate) & 0x0000ffff;
1177 else if (rate == 8000)
1179 else if (rate == 48000)
1182 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1188 static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1191 snd_pcm_runtime_t *runtime = substream->runtime;
1194 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1197 if (snd_pcm_format_width(runtime->format) == 16)
1198 CTRL |= 0x00000008; // 16-bit data
1199 if (!snd_pcm_format_unsigned(runtime->format))
1200 CTRL |= 0x00000002; // signed data
1201 if (runtime->channels > 1)
1202 CTRL |= 0x00000004; // stereo data
1210 static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1211 unsigned int cmd, void *arg)
1213 return snd_pcm_lib_ioctl(substream, cmd, arg);
1216 static int snd_ali_trigger(snd_pcm_substream_t *substream,
1220 ali_t *codec = snd_pcm_substream_chip(substream);
1221 struct list_head *pos;
1222 snd_pcm_substream_t *s;
1223 unsigned int what, whati, capture_flag;
1224 snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1229 case SNDRV_PCM_TRIGGER_START:
1230 case SNDRV_PCM_TRIGGER_RESUME:
1231 do_start = 1; break;
1232 case SNDRV_PCM_TRIGGER_STOP:
1233 case SNDRV_PCM_TRIGGER_SUSPEND:
1234 do_start = 0; break;
1239 what = whati = capture_flag = 0;
1240 snd_pcm_group_for_each(pos, substream) {
1241 s = snd_pcm_group_substream_entry(pos);
1242 if ((ali_t *) snd_pcm_substream_chip(s) == codec) {
1243 pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1244 evoice = pvoice->extra;
1245 what |= 1 << (pvoice->number & 0x1f);
1246 if (evoice == NULL) {
1247 whati |= 1 << (pvoice->number & 0x1f);
1249 whati |= 1 << (evoice->number & 0x1f);
1250 what |= 1 << (evoice->number & 0x1f);
1253 pvoice->running = 1;
1255 evoice->running = 1;
1257 pvoice->running = 0;
1259 evoice->running = 0;
1261 snd_pcm_trigger_done(s, substream);
1266 spin_lock(&codec->reg_lock);
1268 outl(what, ALI_REG(codec, ALI_STOP));
1270 val = inl(ALI_REG(codec, ALI_AINTEN));
1276 outl(val, ALI_REG(codec, ALI_AINTEN));
1278 outl(what, ALI_REG(codec, ALI_START));
1280 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1281 spin_unlock(&codec->reg_lock);
1286 static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1287 snd_pcm_hw_params_t * hw_params)
1289 ali_t *codec = snd_pcm_substream_chip(substream);
1290 snd_pcm_runtime_t *runtime = substream->runtime;
1291 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1292 snd_ali_voice_t *evoice = pvoice->extra;
1294 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1295 if (err < 0) return err;
1297 /* voice management */
1299 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1300 if (evoice == NULL) {
1301 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1304 pvoice->extra = evoice;
1305 evoice->substream = substream;
1308 if (evoice != NULL) {
1309 snd_ali_free_voice(codec, evoice);
1310 pvoice->extra = evoice = NULL;
1317 static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1319 ali_t *codec = snd_pcm_substream_chip(substream);
1320 snd_pcm_runtime_t *runtime = substream->runtime;
1321 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1322 snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1324 snd_pcm_lib_free_pages(substream);
1325 if (evoice != NULL) {
1326 snd_ali_free_voice(codec, evoice);
1327 pvoice->extra = NULL;
1332 static int snd_ali_capture_hw_params(snd_pcm_substream_t * substream,
1333 snd_pcm_hw_params_t * hw_params)
1335 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1338 static int snd_ali_capture_hw_free(snd_pcm_substream_t * substream)
1340 return snd_pcm_lib_free_pages(substream);
1343 static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1345 ali_t *codec = snd_pcm_substream_chip(substream);
1346 snd_pcm_runtime_t *runtime = substream->runtime;
1347 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1348 snd_ali_voice_t *evoice = pvoice->extra;
1349 unsigned long flags;
1360 snd_ali_printk("playback_prepare ...\n");
1362 spin_lock_irqsave(&codec->reg_lock, flags);
1364 /* set Delta (rate) value */
1365 Delta = snd_ali_convert_rate(runtime->rate, 0);
1367 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1368 (pvoice->number == ALI_PCM_IN_CHANNEL))
1369 snd_ali_disable_special_channel(codec, pvoice->number);
1370 else if (codec->spdif_support &&
1371 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1372 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1373 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1377 /* set Loop Back Address */
1378 LBA = runtime->dma_addr;
1380 /* set interrupt count size */
1381 pvoice->count = runtime->period_size;
1383 /* set target ESO for channel */
1384 pvoice->eso = runtime->buffer_size;
1386 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1388 /* set ESO to capture first MIDLP interrupt */
1389 ESO = pvoice->eso -1;
1391 CTRL = snd_ali_control_mode(substream);
1397 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);
1398 snd_ali_write_voice_regs( codec,
1410 if (evoice != NULL) {
1411 evoice->count = pvoice->count;
1412 evoice->eso = pvoice->count << 1;
1413 ESO = evoice->eso - 1;
1414 snd_ali_write_voice_regs(codec,
1423 (unsigned int)0x3ff,
1427 spin_unlock_irqrestore(&codec->reg_lock, flags);
1432 static int snd_ali_capture_prepare(snd_pcm_substream_t * substream)
1434 ali_t *codec = snd_pcm_substream_chip(substream);
1435 snd_pcm_runtime_t *runtime = substream->runtime;
1436 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1437 unsigned long flags;
1448 spin_lock_irqsave(&codec->reg_lock, flags);
1450 snd_ali_printk("capture_prepare...\n");
1452 snd_ali_enable_special_channel(codec,pvoice->number);
1454 Delta = snd_ali_convert_rate(runtime->rate, 1);
1456 // Prepare capture intr channel
1457 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1461 if (codec->revision != ALI_5451_V02) {
1462 spin_unlock_irqrestore(&codec->reg_lock, flags);
1465 rate = snd_ali_get_spdif_in_rate(codec);
1467 snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1470 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1471 if (bValue & 0x10) {
1472 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1473 printk("clear SPDIF parity error flag.\n");
1477 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1480 // set target ESO for channel
1481 pvoice->eso = runtime->buffer_size;
1483 // set interrupt count size
1484 pvoice->count = runtime->period_size;
1486 // set Loop Back Address
1487 LBA = runtime->dma_addr;
1489 // set ESO to capture first MIDLP interrupt
1490 ESO = pvoice->eso - 1;
1491 CTRL = snd_ali_control_mode(substream);
1497 snd_ali_write_voice_regs( codec,
1511 spin_unlock_irqrestore(&codec->reg_lock, flags);
1517 static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1519 ali_t *codec = snd_pcm_substream_chip(substream);
1520 snd_pcm_runtime_t *runtime = substream->runtime;
1521 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1524 spin_lock(&codec->reg_lock);
1525 if (!pvoice->running) {
1526 spin_unlock(&codec->reg_lock);
1529 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1530 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1531 spin_unlock(&codec->reg_lock);
1532 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1538 static snd_pcm_uframes_t snd_ali_capture_pointer(snd_pcm_substream_t *substream)
1540 ali_t *codec = snd_pcm_substream_chip(substream);
1541 snd_pcm_runtime_t *runtime = substream->runtime;
1542 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1544 unsigned long flags;
1546 spin_lock_irqsave(&codec->reg_lock, flags);
1547 if (!pvoice->running) {
1548 spin_unlock_irqrestore(&codec->reg_lock, flags);
1551 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1552 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1553 spin_unlock_irqrestore(&codec->reg_lock, flags);
1558 static snd_pcm_hardware_t snd_ali_playback =
1560 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1561 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1562 SNDRV_PCM_INFO_MMAP_VALID |
1563 SNDRV_PCM_INFO_RESUME |
1564 SNDRV_PCM_INFO_SYNC_START),
1565 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1566 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1567 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1572 .buffer_bytes_max = (256*1024),
1573 .period_bytes_min = 64,
1574 .period_bytes_max = (256*1024),
1576 .periods_max = 1024,
1581 * Capture support device description
1584 static snd_pcm_hardware_t snd_ali_capture =
1586 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1587 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1588 SNDRV_PCM_INFO_MMAP_VALID |
1589 SNDRV_PCM_INFO_RESUME |
1590 SNDRV_PCM_INFO_SYNC_START),
1591 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1592 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1593 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1598 .buffer_bytes_max = (128*1024),
1599 .period_bytes_min = 64,
1600 .period_bytes_max = (128*1024),
1602 .periods_max = 1024,
1606 static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1608 unsigned long flags;
1609 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1613 codec = pvoice->codec;
1614 spin_lock_irqsave(&codec->reg_lock, flags);
1615 snd_ali_free_voice(pvoice->codec, pvoice);
1616 spin_unlock_irqrestore(&codec->reg_lock, flags);
1620 static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1622 ali_t *codec = snd_pcm_substream_chip(substream);
1623 snd_pcm_runtime_t *runtime = substream->runtime;
1624 snd_ali_voice_t *pvoice;
1625 unsigned long flags = 0;
1627 spin_lock_irqsave(&codec->reg_lock, flags);
1628 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1629 if (pvoice == NULL) {
1630 spin_unlock_irqrestore(&codec->reg_lock, flags);
1633 pvoice->codec = codec;
1634 spin_unlock_irqrestore(&codec->reg_lock, flags);
1636 pvoice->substream = substream;
1637 runtime->private_data = pvoice;
1638 runtime->private_free = snd_ali_pcm_free_substream;
1640 runtime->hw = snd_ali_playback;
1641 snd_pcm_set_sync(substream);
1642 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1647 static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1649 ali_t *codec = snd_pcm_substream_chip(substream);
1650 snd_pcm_runtime_t *runtime = substream->runtime;
1651 snd_ali_voice_t *pvoice;
1652 unsigned long flags;
1654 spin_lock_irqsave(&codec->reg_lock, flags);
1655 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 1);
1656 if (pvoice == NULL) {
1657 spin_unlock_irqrestore(&codec->reg_lock, flags);
1660 pvoice->codec = codec;
1661 spin_unlock_irqrestore(&codec->reg_lock, flags);
1663 pvoice->substream = substream;
1664 runtime->private_data = pvoice;
1665 runtime->private_free = snd_ali_pcm_free_substream;
1666 runtime->hw = snd_ali_capture;
1667 snd_pcm_set_sync(substream);
1668 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1673 static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1678 static int snd_ali_capture_close(snd_pcm_substream_t * substream)
1680 ali_t *codec = snd_pcm_substream_chip(substream);
1681 snd_pcm_runtime_t *runtime = substream->runtime;
1682 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1684 snd_ali_disable_special_channel(codec,pvoice->number);
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,
1700 static snd_pcm_ops_t snd_ali_capture_ops = {
1701 .open = snd_ali_capture_open,
1702 .close = snd_ali_capture_close,
1703 .ioctl = snd_ali_ioctl,
1704 .hw_params = snd_ali_capture_hw_params,
1705 .hw_free = snd_ali_capture_hw_free,
1706 .prepare = snd_ali_capture_prepare,
1707 .trigger = snd_ali_trigger,
1708 .pointer = snd_ali_capture_pointer,
1712 static void snd_ali_pcm_free(snd_pcm_t *pcm)
1714 ali_t *codec = pcm->private_data;
1718 static int __devinit snd_ali_pcm(ali_t * codec, int device, snd_pcm_t ** rpcm)
1723 if (rpcm) *rpcm = NULL;
1724 err = snd_pcm_new(codec->card, "ALI 5451", device, ALI_CHANNELS, 1, &pcm);
1726 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1729 pcm->private_data = codec;
1730 pcm->private_free = snd_ali_pcm_free;
1731 pcm->info_flags = 0;
1732 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ali_playback_ops);
1733 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ali_capture_ops);
1735 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1736 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1738 pcm->info_flags = 0;
1739 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1740 strcpy(pcm->name, "ALI 5451");
1742 if (rpcm) *rpcm = pcm;
1746 #define ALI5451_SPDIF(xname, xindex, value) \
1747 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1748 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1749 .put = snd_ali5451_spdif_put, .private_value = value}
1751 static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1753 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1755 uinfo->value.integer.min = 0;
1756 uinfo->value.integer.max = 1;
1760 static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1762 unsigned long flags;
1763 ali_t *codec = kcontrol->private_data;
1764 unsigned int enable;
1766 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1768 spin_lock_irqsave(&codec->reg_lock, flags);
1769 switch(kcontrol->private_value) {
1771 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1774 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1777 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1782 ucontrol->value.integer.value[0] = enable;
1783 spin_unlock_irqrestore(&codec->reg_lock, flags);
1787 static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1789 unsigned long flags;
1790 ali_t *codec = kcontrol->private_data;
1791 unsigned int change = 0, enable = 0;
1793 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1795 spin_lock_irqsave(&codec->reg_lock, flags);
1796 switch (kcontrol->private_value) {
1798 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1799 change = change ^ enable;
1802 codec->spdif_mask |= 0x02;
1803 snd_ali_enable_spdif_out(codec);
1805 codec->spdif_mask &= ~(0x02);
1806 codec->spdif_mask &= ~(0x04);
1807 snd_ali_disable_spdif_out(codec);
1812 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1813 change = change ^ enable;
1814 if (change && (codec->spdif_mask & 0x02)) {
1816 codec->spdif_mask |= 0x04;
1817 snd_ali_enable_spdif_chnout(codec);
1819 codec->spdif_mask &= ~(0x04);
1820 snd_ali_disable_spdif_chnout(codec);
1825 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1826 change = change ^ enable;
1829 codec->spdif_mask |= 0x01;
1830 snd_ali_enable_spdif_in(codec);
1832 codec->spdif_mask &= ~(0x01);
1833 snd_ali_disable_spdif_in(codec);
1840 spin_unlock_irqrestore(&codec->reg_lock, flags);
1845 static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1846 /* spdif aplayback switch */
1847 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1848 ALI5451_SPDIF("IEC958 Output switch", 0, 0),
1849 /* spdif out to spdif channel */
1850 ALI5451_SPDIF("IEC958 Channel Output Switch", 0, 1),
1851 /* spdif in from spdif channel */
1852 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1855 static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1857 ali_t *codec = bus->private_data;
1858 codec->ac97_bus = NULL;
1861 static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1863 ali_t *codec = ac97->private_data;
1867 static int __devinit snd_ali_mixer(ali_t * codec)
1869 ac97_template_t ac97;
1872 static ac97_bus_ops_t ops = {
1873 .write = snd_ali_codec_write,
1874 .read = snd_ali_codec_read,
1877 if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1879 codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1881 memset(&ac97, 0, sizeof(ac97));
1882 ac97.private_data = codec;
1883 ac97.private_free = snd_ali_mixer_free_ac97;
1884 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97)) < 0) {
1885 snd_printk("ali mixer creating error.\n");
1888 if (codec->spdif_support) {
1889 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1890 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1891 if (err < 0) return err;
1898 static int ali_suspend(snd_card_t *card, unsigned int state)
1900 ali_t *chip = card->pm_private_data;
1908 snd_pcm_suspend_all(chip->pcm);
1909 snd_ac97_suspend(chip->ac97);
1911 spin_lock_irq(&chip->reg_lock);
1913 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1914 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
1915 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1917 // disable all IRQ bits
1918 outl(0, ALI_REG(chip, ALI_MISCINT));
1920 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1921 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1923 im->regs[i] = inl(ALI_REG(chip, i*4));
1926 for (i = 0; i < ALI_CHANNELS; i++) {
1927 outb(i, ALI_REG(chip, ALI_GC_CIR));
1928 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1929 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1932 // stop all HW channel
1933 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1935 spin_unlock_irq(&chip->reg_lock);
1936 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1940 static int ali_resume(snd_card_t *card, unsigned int state)
1942 ali_t *chip = card->pm_private_data;
1950 pci_enable_device(chip->pci);
1952 spin_lock_irq(&chip->reg_lock);
1954 for (i = 0; i < ALI_CHANNELS; i++) {
1955 outb(i, ALI_REG(chip, ALI_GC_CIR));
1956 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1957 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1960 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1961 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
1963 outl(im->regs[i], ALI_REG(chip, i*4));
1967 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1968 // restore IRQ enable bits
1969 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1971 spin_unlock_irq(&chip->reg_lock);
1973 snd_ac97_resume(chip->ac97);
1974 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1978 #endif /* CONFIG_PM */
1980 static int snd_ali_free(ali_t * codec)
1982 if (codec->hw_initialized)
1983 snd_ali_disable_address_interrupt(codec);
1984 if (codec->irq >= 0) {
1985 synchronize_irq(codec->irq);
1986 free_irq(codec->irq, (void *)codec);
1989 pci_release_regions(codec->pci);
1992 kfree(codec->image);
1998 static int snd_ali_chip_init(ali_t *codec)
2000 unsigned int legacy;
2002 struct pci_dev *pci_dev = NULL;
2004 snd_ali_printk("chip initializing ... \n");
2006 if (snd_ali_reset_5451(codec)) {
2007 snd_printk("ali_chip_init: reset 5451 error.\n");
2011 if (codec->revision == ALI_5451_V02) {
2012 pci_dev = codec->pci_m1533;
2013 pci_read_config_byte(pci_dev, 0x59, &temp);
2015 pci_write_config_byte(pci_dev, 0x59, temp);
2017 pci_dev = codec->pci_m7101;
2018 pci_read_config_byte(pci_dev, 0xb8, &temp);
2020 pci_write_config_byte(pci_dev, 0xB8, temp);
2023 pci_read_config_dword(codec->pci, 0x44, &legacy);
2024 legacy &= 0xff00ff00;
2025 legacy |= 0x000800aa;
2026 pci_write_config_dword(codec->pci, 0x44, legacy);
2028 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2029 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2030 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2031 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2032 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2034 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2035 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2036 if (codec->spdif_support) {
2037 snd_ali_enable_spdif_out(codec);
2038 codec->spdif_mask = 0x00000002;
2041 snd_ali_printk("chip initialize succeed.\n");
2046 static int __devinit snd_ali_resources(ali_t *codec)
2050 snd_ali_printk("resouces allocation ...\n");
2051 if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2053 codec->port = pci_resource_start(codec->pci, 0);
2055 if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2056 snd_printk("Unable to request irq.\n");
2059 codec->irq = codec->pci->irq;
2060 snd_ali_printk("resouces allocated.\n");
2063 static int snd_ali_dev_free(snd_device_t *device)
2065 ali_t *codec=device->device_data;
2066 snd_ali_free(codec);
2070 static int __devinit snd_ali_create(snd_card_t * card,
2071 struct pci_dev *pci,
2078 unsigned short cmdw = 0;
2079 struct pci_dev *pci_dev = NULL;
2080 static snd_device_ops_t ops = {
2081 (snd_dev_free_t *)snd_ali_dev_free,
2088 snd_ali_printk("creating ...\n");
2090 /* enable PCI device */
2091 if ((err = pci_enable_device(pci)) < 0)
2093 /* check, if we can restrict PCI DMA transfers to 31 bits */
2094 if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
2095 pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2096 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2100 if ((codec = kcalloc(1, sizeof(*codec), GFP_KERNEL)) == NULL)
2103 spin_lock_init(&codec->reg_lock);
2104 spin_lock_init(&codec->voice_alloc);
2109 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2110 codec->spdif_support = spdif_support;
2112 if (pcm_streams < 1)
2114 if (pcm_streams > 32)
2117 pci_set_master(pci);
2118 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2119 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2120 cmdw |= PCI_COMMAND_IO;
2121 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2123 pci_set_master(pci);
2125 if (snd_ali_resources(codec)) {
2126 snd_ali_free(codec);
2130 synchronize_irq(pci->irq);
2132 codec->synth.chmap = 0;
2133 codec->synth.chcnt = 0;
2134 codec->spdif_mask = 0;
2135 codec->synth.synthcount = 0;
2137 if (codec->revision == ALI_5451_V02)
2138 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2140 codec->chregs.regs.ac97read = ALI_AC97_READ;
2141 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2143 codec->chregs.regs.start = ALI_START;
2144 codec->chregs.regs.stop = ALI_STOP;
2145 codec->chregs.regs.aint = ALI_AINT;
2146 codec->chregs.regs.ainten = ALI_AINTEN;
2148 codec->chregs.data.start = 0x00;
2149 codec->chregs.data.stop = 0x00;
2150 codec->chregs.data.aint = 0x00;
2151 codec->chregs.data.ainten = 0x00;
2153 /* M1533: southbridge */
2154 pci_dev = pci_find_device(0x10b9, 0x1533, NULL);
2155 codec->pci_m1533 = pci_dev;
2156 if (! codec->pci_m1533) {
2157 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2158 snd_ali_free(codec);
2161 /* M7101: power management */
2162 pci_dev = pci_find_device(0x10b9, 0x7101, NULL);
2163 codec->pci_m7101 = pci_dev;
2164 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2165 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2166 snd_ali_free(codec);
2170 snd_ali_printk("snd_device_new is called.\n");
2171 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2172 snd_ali_free(codec);
2176 /* initialise synth voices*/
2177 for (i = 0; i < ALI_CHANNELS; i++ ) {
2178 codec->synth.voices[i].number = i;
2181 if ((err = snd_ali_chip_init(codec)) < 0) {
2182 snd_printk("ali create: chip init error.\n");
2187 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2189 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2191 snd_card_set_pm_callback(card, ali_suspend, ali_resume, codec);
2194 snd_ali_enable_address_interrupt(codec);
2195 codec->hw_initialized = 1;
2198 snd_ali_printk("created.\n");
2202 static int __devinit snd_ali_probe(struct pci_dev *pci,
2203 const struct pci_device_id *pci_id)
2210 snd_ali_printk("probe ...\n");
2212 if (dev >= SNDRV_CARDS)
2219 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2223 if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2224 snd_card_free(card);
2228 snd_ali_printk("mixer building ...\n");
2229 if ((err = snd_ali_mixer(codec)) < 0) {
2230 snd_card_free(card);
2234 snd_ali_printk("pcm building ...\n");
2235 if ((err = snd_ali_pcm(codec, 0, NULL)) < 0) {
2236 snd_card_free(card);
2240 strcpy(card->driver, "ALI5451");
2241 strcpy(card->shortname, "ALI 5451");
2243 sprintf(card->longname, "%s at 0x%lx, irq %li",
2244 card->shortname, codec->port, codec->irq);
2246 snd_ali_printk("register card.\n");
2247 if ((err = snd_card_register(card)) < 0) {
2248 snd_card_free(card);
2251 pci_set_drvdata(pci, card);
2256 static void __devexit snd_ali_remove(struct pci_dev *pci)
2258 snd_card_free(pci_get_drvdata(pci));
2259 pci_set_drvdata(pci, NULL);
2262 static struct pci_driver driver = {
2264 .id_table = snd_ali_ids,
2265 .probe = snd_ali_probe,
2266 .remove = __devexit_p(snd_ali_remove),
2267 SND_PCI_PM_CALLBACKS
2270 static int __init alsa_card_ali_init(void)
2272 return pci_module_init(&driver);
2275 static void __exit alsa_card_ali_exit(void)
2277 pci_unregister_driver(&driver);
2280 module_init(alsa_card_ali_init)
2281 module_exit(alsa_card_ali_exit)