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 #define __SNDRV_OSS_COMPAT__
30 #include <sound/driver.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
34 #include <linux/init.h>
35 #include <linux/pci.h>
36 #include <linux/slab.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/info.h>
40 #include <sound/ac97_codec.h>
41 #include <sound/mpu401.h>
43 #include <sound/initval.h>
45 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
46 MODULE_DESCRIPTION("ALI M5451");
47 MODULE_LICENSE("GPL");
48 MODULE_CLASSES("{sound}");
49 MODULE_DEVICES("{{ALI,M5451,pci},{ALI,M5451}}");
51 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
52 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
53 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
54 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 32};
55 static int spdif[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
57 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
58 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
59 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
60 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
61 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
62 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
63 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
64 MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
65 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
66 MODULE_PARM(pcm_channels, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
67 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
68 MODULE_PARM_SYNTAX(pcm_channels, SNDRV_ENABLED ",default:32,allows:{{1,32}}");
69 MODULE_PARM(spdif, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
70 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
71 MODULE_PARM_SYNTAX(spdif, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
74 * Debug part definitions
80 #define snd_ali_printk(format, args...) printk(format, ##args);
82 #define snd_ali_printk(format, args...)
86 * Constants definition
89 #ifndef PCI_VENDOR_ID_ALI
90 #define PCI_VENDOR_ID_ALI 0x10b9
93 #ifndef PCI_DEVICE_ID_ALI_5451
94 #define PCI_DEVICE_ID_ALI_5451 0x5451
97 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_ALI<<16)|PCI_DEVICE_ID_ALI_5451)
100 #define ALI_CHANNELS 32
102 #define ALI_PCM_IN_CHANNEL 31
103 #define ALI_SPDIF_IN_CHANNEL 19
104 #define ALI_SPDIF_OUT_CHANNEL 15
105 #define ALI_CENTER_CHANNEL 24
106 #define ALI_LEF_CHANNEL 23
107 #define ALI_SURR_LEFT_CHANNEL 26
108 #define ALI_SURR_RIGHT_CHANNEL 25
110 #define SNDRV_ALI_VOICE_TYPE_PCM 01
111 #define SNDRV_ALI_VOICE_TYPE_OTH 02
113 #define ALI_5451_V02 0x02
119 #define ALI_LEGACY_DMAR0 0x00 // ADR0
120 #define ALI_LEGACY_DMAR4 0x04 // CNT0
121 #define ALI_LEGACY_DMAR11 0x0b // MOD
122 #define ALI_LEGACY_DMAR15 0x0f // MMR
123 #define ALI_MPUR0 0x20
124 #define ALI_MPUR1 0x21
125 #define ALI_MPUR2 0x22
126 #define ALI_MPUR3 0x23
128 #define ALI_AC97_WRITE 0x40
129 #define ALI_AC97_READ 0x44
131 #define ALI_SCTRL 0x48
132 #define ALI_SPDIF_OUT_ENABLE 0x20
133 #define ALI_AC97_GPIO 0x4c
134 #define ALI_SPDIF_CS 0x70
135 #define ALI_SPDIF_CTRL 0x74
136 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
137 #define ALI_SPDIF_IN_CH_STATUS 0x40
138 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
139 #define ALI_START 0x80
140 #define ALI_STOP 0x84
141 #define ALI_CSPF 0x90
142 #define ALI_AINT 0x98
143 #define ALI_GC_CIR 0xa0
144 #define ENDLP_IE 0x00001000
145 #define MIDLP_IE 0x00002000
146 #define ALI_AINTEN 0xa4
147 #define ALI_VOLUME 0xa8
148 #define ALI_SBDELTA_DELTA_R 0xac
149 #define ALI_MISCINT 0xb0
150 #define ADDRESS_IRQ 0x00000020
151 #define TARGET_REACHED 0x00008000
152 #define MIXER_OVERFLOW 0x00000800
153 #define MIXER_UNDERFLOW 0x00000400
154 #define ALI_SBBL_SBCL 0xc0
155 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
156 #define ALI_STIMER 0xc8
157 #define ALI_GLOBAL_CONTROL 0xd4
158 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
159 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
160 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
161 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
162 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
164 #define ALI_CSO_ALPHA_FMS 0xe0
166 #define ALI_ESO_DELTA 0xe8
167 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
168 #define ALI_EBUF1 0xf4
169 #define ALI_EBUF2 0xf8
171 #define ALI_REG(codec, x) ((codec)->port + x)
173 typedef struct snd_stru_ali ali_t;
174 typedef struct snd_ali_stru_voice snd_ali_voice_t;
177 typedef struct snd_ali_channel_control {
186 // register addresses
192 unsigned int ac97read;
193 unsigned int ac97write;
196 } snd_ali_channel_control_t;
198 struct snd_ali_stru_voice {
208 snd_pcm_substream_t *substream;
209 snd_ali_voice_t *extra;
213 int eso; /* final ESO value for channel */
214 int count; /* runtime->period_size */
219 void (*private_free)(void *private_data);
223 typedef struct snd_stru_alidev {
225 snd_ali_voice_t voices[ALI_CHANNELS];
227 unsigned int chcnt; /* num of opened channels */
228 unsigned int chmap; /* bitmap for opened channels */
229 unsigned int synthcount;
235 #define ALI_GLOBAL_REGS 56
236 #define ALI_CHANNEL_REGS 8
237 typedef struct snd_ali_image {
238 unsigned long regs[ALI_GLOBAL_REGS];
239 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
244 struct snd_stru_ali {
247 unsigned char revision;
249 unsigned int hw_initialized: 1;
250 unsigned int spdif_support: 1;
251 struct resource *res_port;
254 struct pci_dev *pci_m1533;
255 struct pci_dev *pci_m7101;
260 snd_ali_channel_control_t chregs;
263 unsigned int spdif_mask;
265 unsigned int spurious_irq_count;
266 unsigned int spurious_irq_max_delta;
268 ac97_bus_t *ac97_bus;
270 unsigned short ac97_ext_id;
271 unsigned short ac97_ext_status;
274 spinlock_t voice_alloc;
281 static struct pci_device_id snd_ali_ids[] = {
282 {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
285 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
287 static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
288 static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
289 static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
297 static void ali_read_regs(ali_t *codec, int channel)
302 printk("channel %d registers map:\n", channel);
303 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
307 printk("%2.2x ", j*4);
310 for (i=0; i<=0xf8/4;i++) {
312 printk("%2.2x ", (i*4/0x10)*0x10);
313 dwVal = inl(ALI_REG(codec,i*4));
314 printk("%8.8x ", dwVal);
320 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
323 struct pci_dev *pci_dev = NULL;
327 pci_dev = pci_find_device(vendor, deviceid, pci_dev);
331 printk("\nM%x PCI CFG\n", deviceid);
341 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
342 printk("%8.8x ", dwVal);
347 static void ali_read_ac97regs(ali_t *codec, int secondary)
352 printk("\ncodec %d registers map:\n", secondary);
356 printk("%2.2x ",j*2);
359 for (i=0; i<64;i++) {
361 printk("%2.2x ", (i/8)*0x10);
362 wVal = snd_ali_codec_peek(codec, secondary, i*2);
363 printk("%4.4x ", wVal);
376 static inline unsigned int snd_ali_5451_peek(ali_t *codec,
379 return (unsigned int)inl(ALI_REG(codec, port));
382 static inline void snd_ali_5451_poke( ali_t *codec,
386 outl((unsigned int)val, ALI_REG(codec, port));
389 static int snd_ali_codec_ready( ali_t *codec,
393 unsigned long end_time;
396 end_time = jiffies + 10 * (HZ >> 2);
398 res = snd_ali_5451_peek(codec,port);
399 if (! (res & 0x8000))
402 set_current_state(TASK_UNINTERRUPTIBLE);
405 } while (time_after_eq(end_time, jiffies));
406 snd_ali_5451_poke(codec, port, res & ~0x8000);
407 snd_printdd("ali_codec_ready: codec is not ready.\n ");
411 static int snd_ali_stimer_ready(ali_t *codec, int sched)
413 unsigned long end_time;
414 unsigned long dwChk1,dwChk2;
416 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
417 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
419 end_time = jiffies + 10 * (HZ >> 2);
421 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
422 if (dwChk2 != dwChk1)
425 set_current_state(TASK_UNINTERRUPTIBLE);
428 } while (time_after_eq(end_time, jiffies));
429 snd_printk("ali_stimer_read: stimer is not ready.\n");
433 static void snd_ali_codec_poke(ali_t *codec,int secondary,
437 unsigned int dwVal = 0;
438 unsigned int port = 0;
441 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
445 port = codec->chregs.regs.ac97write;
447 if (snd_ali_codec_ready(codec, port, 0) < 0)
449 if (snd_ali_stimer_ready(codec, 0) < 0)
452 dwVal = (unsigned int) (reg & 0xff);
453 dwVal |= 0x8000 | (val << 16);
454 if (secondary) dwVal |= 0x0080;
455 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
457 snd_ali_5451_poke(codec,port,dwVal);
462 static unsigned short snd_ali_codec_peek( ali_t *codec,
466 unsigned int dwVal = 0;
467 unsigned int port = 0;
470 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
474 port = codec->chregs.regs.ac97read;
476 if (snd_ali_codec_ready(codec, port, 0) < 0)
478 if (snd_ali_stimer_ready(codec, 0) < 0)
481 dwVal = (unsigned int) (reg & 0xff);
482 dwVal |= 0x8000; /* bit 15*/
483 if (secondary) dwVal |= 0x0080;
485 snd_ali_5451_poke(codec, port, dwVal);
487 if (snd_ali_stimer_ready(codec, 0) < 0)
489 if (snd_ali_codec_ready(codec, port, 0) < 0)
492 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
495 static void snd_ali_codec_write(ac97_t *ac97,
499 ali_t *codec = snd_magic_cast(ali_t, ac97->private_data, return);
501 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
502 snd_ali_codec_poke(codec, 0, reg, val);
507 static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
509 ali_t *codec = snd_magic_cast(ali_t, ac97->private_data, return -ENXIO);
511 snd_ali_printk("codec_read reg=%xh.\n", reg);
512 return (snd_ali_codec_peek(codec, 0, reg));
519 static int snd_ali_reset_5451(ali_t *codec)
521 struct pci_dev *pci_dev = NULL;
522 unsigned short wCount, wReg;
525 if ((pci_dev = codec->pci_m1533) != NULL) {
526 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
527 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
529 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
530 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
534 pci_dev = codec->pci;
535 pci_read_config_dword(pci_dev, 0x44, &dwVal);
536 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
538 pci_read_config_dword(pci_dev, 0x44, &dwVal);
539 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
544 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
545 if((wReg & 0x000f) == 0x000f)
550 /* non-fatal if you have a non PM capable codec */
551 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
557 static int snd_ali_reset_codec(ali_t *codec)
559 struct pci_dev *pci_dev = NULL;
560 unsigned char bVal = 0;
562 unsigned short wCount, wReg;
564 pci_dev = codec->pci_m1533;
566 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
567 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
569 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
570 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
573 bVal = inb(ALI_REG(codec,ALI_SCTRL));
575 outb(ALI_REG(codec,ALI_SCTRL),bVal);
577 bVal = inb(ALI_REG(codec,ALI_SCTRL));
579 outb(ALI_REG(codec,ALI_SCTRL),bVal);
584 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
585 if((wReg & 0x000f) == 0x000f)
595 * ALI 5451 Controller
598 static void snd_ali_enable_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_disable_special_channel(ali_t *codec, unsigned int channel)
609 unsigned long dwVal = 0;
611 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
612 dwVal &= ~(1 << (channel & 0x0000001f));
613 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
616 static void snd_ali_enable_address_interrupt(ali_t * codec)
620 gc = inl(ALI_REG(codec, ALI_GC_CIR));
623 outl( gc, ALI_REG(codec, ALI_GC_CIR));
626 static void snd_ali_disable_address_interrupt(ali_t * codec)
630 gc = inl(ALI_REG(codec, ALI_GC_CIR));
633 outl(gc, ALI_REG(codec, ALI_GC_CIR));
637 static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
640 snd_ali_channel_control_t *pchregs = &(codec->chregs);
642 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
644 mask = 1 << (channel & 0x1f);
645 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
646 pchregs->data.ainten |= mask;
647 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
651 static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
654 snd_ali_channel_control_t *pchregs = &(codec->chregs);
656 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
658 mask = 1 << (channel & 0x1f);
659 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
660 pchregs->data.ainten &= ~mask;
661 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
664 static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
666 unsigned int idx = channel & 0x1f;
668 if (codec->synth.chcnt >= ALI_CHANNELS){
669 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
673 if (!(codec->synth.chmap & (1 << idx))) {
674 codec->synth.chmap |= 1 << idx;
675 codec->synth.chcnt++;
676 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
682 static int snd_ali_find_free_channel(ali_t * codec, int rec)
687 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
691 if (codec->spdif_support &&
692 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
693 idx = ALI_SPDIF_IN_CHANNEL;
695 idx = ALI_PCM_IN_CHANNEL;
697 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
700 snd_printk("ali_find_free_channel: record channel is busy now.\n");
706 if (codec->spdif_support &&
707 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
708 idx = ALI_SPDIF_OUT_CHANNEL;
709 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
712 snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
716 for (idx = 0; idx < ALI_CHANNELS; idx++) {
717 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
720 snd_printk("ali_find_free_channel: no free channels.\n");
724 static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
726 unsigned int idx = channel & 0x0000001f;
728 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
730 if (channel < 0 || channel >= ALI_CHANNELS)
733 if (!(codec->synth.chmap & (1 << idx))) {
734 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
737 codec->synth.chmap &= ~(1 << idx);
738 codec->synth.chcnt--;
743 static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
745 unsigned int mask = 1 << (channel & 0x1f);
747 snd_ali_printk("start_voice: channel=%d\n",channel);
748 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
752 static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
754 unsigned int mask = 1 << (channel & 0x1f);
756 snd_ali_printk("stop_voice: channel=%d\n",channel);
757 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
764 static void snd_ali_delay(ali_t *codec,int interval)
766 unsigned long begintimer,currenttimer;
768 begintimer = inl(ALI_REG(codec, ALI_STIMER));
769 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
771 while (currenttimer < begintimer + interval) {
772 if(snd_ali_stimer_ready(codec, 1) < 0)
774 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
778 static void snd_ali_detect_spdif_rate(ali_t *codec)
782 u8 bval = 0, R1 = 0, R2 = 0;
784 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
786 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
788 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
790 snd_ali_delay(codec, 6);
791 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
796 snd_printk("ali_detect_spdif_rate: timeout!\n");
801 while (count++ <= 50000) {
802 snd_ali_delay(codec, 6);
803 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
805 if (R2 != R1) R1 = R2; else break;
809 snd_printk("ali_detect_spdif_rate: timeout!\n");
813 if (R2 >= 0x0b && R2 <= 0x0e) {
814 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
816 wval |= (u16)0x09 << 8 | (u16)0x05;
817 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
819 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
820 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
821 } else if (R2 == 0x12) {
822 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
824 wval |= (u16)0x0E << 8 | (u16)0x08;
825 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
827 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
828 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
832 static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
837 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
840 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
842 snd_ali_detect_spdif_rate(codec);
844 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
847 if (bval == 0) dwRate = 44100;
848 if (bval == 1) dwRate = 48000;
849 if (bval == 2) dwRate = 32000;
854 static void snd_ali_enable_spdif_in(ali_t *codec)
858 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
859 dwVal |= ALI_SPDIF_IN_SUPPORT;
860 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
862 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
864 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
866 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
869 static void snd_ali_disable_spdif_in(ali_t *codec)
873 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
874 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
875 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
877 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
881 static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
884 unsigned int dwRate = 0;
886 if (rate == 32000) dwRate = 0x300;
887 if (rate == 44100) dwRate = 0;
888 if (rate == 48000) dwRate = 0x200;
890 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
891 bVal &= (unsigned char)(~(1<<6));
893 bVal |= 0x80; //select right
894 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
895 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
897 bVal &= (~0x80); //select left
898 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
899 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
902 static void snd_ali_enable_spdif_out(ali_t *codec)
907 struct pci_dev *pci_dev = NULL;
909 pci_dev = codec->pci_m1533;
912 pci_read_config_byte(pci_dev, 0x61, &bVal);
914 pci_write_config_byte(pci_dev, 0x61, bVal);
915 pci_read_config_byte(pci_dev, 0x7d, &bVal);
917 pci_write_config_byte(pci_dev, 0x7d, bVal);
919 pci_read_config_byte(pci_dev, 0x7e, &bVal);
922 pci_write_config_byte(pci_dev, 0x7e, bVal);
924 bVal = inb(ALI_REG(codec, ALI_SCTRL));
925 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
927 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
928 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
931 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
932 wVal |= ALI_SPDIF_OUT_SEL_PCM;
933 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
934 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
938 static void snd_ali_enable_spdif_chnout(ali_t *codec)
940 unsigned short wVal = 0;
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));
946 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
947 if (flag & ALI_SPDIF_OUT_NON_PCM)
951 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
953 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
956 static void snd_ali_disable_spdif_chnout(ali_t *codec)
958 unsigned short wVal = 0;
959 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
960 wVal |= ALI_SPDIF_OUT_SEL_PCM;
961 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
963 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
966 static void snd_ali_disable_spdif_out(ali_t *codec)
970 bVal = inb(ALI_REG(codec, ALI_SCTRL));
971 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
973 snd_ali_disable_spdif_chnout(codec);
976 static void snd_ali_update_ptr(ali_t *codec,int channel)
978 snd_ali_voice_t *pvoice = NULL;
979 snd_pcm_runtime_t *runtime;
980 snd_ali_channel_control_t *pchregs = NULL;
981 unsigned int old, mask;
983 unsigned int temp, cspf;
986 pchregs = &(codec->chregs);
988 // check if interrupt occurred for channel
989 old = pchregs->data.aint;
990 mask = ((unsigned int) 1L) << (channel & 0x1f);
995 pvoice = &codec->synth.voices[channel];
996 runtime = pvoice->substream->runtime;
999 spin_lock(&codec->reg_lock);
1001 if (pvoice->pcm && pvoice->substream) {
1004 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
1005 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1006 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
1008 if (pvoice->running) {
1009 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1010 spin_unlock(&codec->reg_lock);
1011 snd_pcm_period_elapsed(pvoice->substream);
1012 spin_lock(&codec->reg_lock);
1014 snd_ali_stop_voice(codec, channel);
1015 snd_ali_disable_voice_irq(codec, channel);
1017 } else if (codec->synth.voices[channel].synth) {
1018 /* synth interrupt */
1019 } else if (codec->synth.voices[channel].midi) {
1020 /* midi interrupt */
1022 /* unknown interrupt */
1023 snd_ali_stop_voice(codec, channel);
1024 snd_ali_disable_voice_irq(codec, channel);
1026 spin_unlock(&codec->reg_lock);
1027 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1028 pchregs->data.aint = old & (~mask);
1031 static void snd_ali_interrupt(ali_t * codec)
1034 unsigned int audio_int;
1035 snd_ali_channel_control_t *pchregs = NULL;
1036 pchregs = &(codec->chregs);
1038 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1039 if (audio_int & ADDRESS_IRQ) {
1040 // get interrupt status for all channels
1041 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1042 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1043 snd_ali_update_ptr(codec, channel);
1046 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1047 ALI_REG(codec,ALI_MISCINT));
1051 static irqreturn_t snd_ali_card_interrupt(int irq,
1053 struct pt_regs *regs)
1055 ali_t *codec = snd_magic_cast(ali_t, dev_id, return IRQ_NONE);
1059 snd_ali_interrupt(codec);
1064 static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec)
1066 snd_ali_voice_t *pvoice = NULL;
1067 unsigned long flags;
1070 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1072 spin_lock_irqsave(&codec->voice_alloc, flags);
1073 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1074 idx = snd_ali_find_free_channel(codec,rec);
1076 snd_printk("ali_alloc_voice: err.\n");
1077 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1080 pvoice = &(codec->synth.voices[idx]);
1084 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1087 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1092 static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1094 unsigned long flags;
1095 void (*private_free)(void *);
1098 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1099 if (pvoice == NULL || !pvoice->use)
1101 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1102 spin_lock_irqsave(&codec->voice_alloc, flags);
1103 private_free = pvoice->private_free;
1104 private_data = pvoice->private_data;
1105 pvoice->private_free = NULL;
1106 pvoice->private_data = NULL;
1108 snd_ali_free_channel_pcm(codec, pvoice->number);
1110 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1111 pvoice->substream = NULL;
1112 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1114 private_free(private_data);
1118 static void snd_ali_clear_voices(ali_t * codec,
1124 for (i = v_min; i <= v_max; i++) {
1125 snd_ali_stop_voice(codec, i);
1126 snd_ali_disable_voice_irq(codec, i);
1130 static void snd_ali_write_voice_regs(ali_t * codec,
1131 unsigned int Channel,
1136 unsigned int ALPHA_FMS,
1143 unsigned int ctlcmds[4];
1145 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1147 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1149 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1150 ctlcmds[3] = (GVSEL << 31) |
1151 ((PAN & 0x0000007f) << 24) |
1152 ((VOL & 0x000000ff) << 16) |
1153 ((CTRL & 0x0000000f) << 12) |
1156 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1158 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1159 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1160 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1161 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1163 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1164 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1167 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1171 if (rate < 4000) rate = 4000;
1172 if (rate > 48000) rate = 48000;
1177 else if (rate == 8000)
1179 else if (rate == 48000)
1182 delta = ((48000 << 12) / rate) & 0x0000ffff;
1186 else if (rate == 8000)
1188 else if (rate == 48000)
1191 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1197 static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1200 snd_pcm_runtime_t *runtime = substream->runtime;
1203 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1206 if (snd_pcm_format_width(runtime->format) == 16)
1207 CTRL |= 0x00000008; // 16-bit data
1208 if (!snd_pcm_format_unsigned(runtime->format))
1209 CTRL |= 0x00000002; // signed data
1210 if (runtime->channels > 1)
1211 CTRL |= 0x00000004; // stereo data
1219 static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1220 unsigned int cmd, void *arg)
1222 return snd_pcm_lib_ioctl(substream, cmd, arg);
1225 static int snd_ali_trigger(snd_pcm_substream_t *substream,
1229 ali_t *codec = snd_pcm_substream_chip(substream);
1230 struct list_head *pos;
1231 snd_pcm_substream_t *s;
1232 unsigned int what, whati, capture_flag;
1233 snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1238 case SNDRV_PCM_TRIGGER_START:
1239 case SNDRV_PCM_TRIGGER_RESUME:
1240 do_start = 1; break;
1241 case SNDRV_PCM_TRIGGER_STOP:
1242 case SNDRV_PCM_TRIGGER_SUSPEND:
1243 do_start = 0; break;
1248 what = whati = capture_flag = 0;
1249 snd_pcm_group_for_each(pos, substream) {
1250 s = snd_pcm_group_substream_entry(pos);
1251 if ((ali_t *) _snd_pcm_chip(s->pcm) == codec) {
1252 pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1253 evoice = pvoice->extra;
1254 what |= 1 << (pvoice->number & 0x1f);
1255 if (evoice == NULL) {
1256 whati |= 1 << (pvoice->number & 0x1f);
1258 whati |= 1 << (evoice->number & 0x1f);
1259 what |= 1 << (evoice->number & 0x1f);
1262 pvoice->running = 1;
1264 evoice->running = 1;
1266 pvoice->running = 0;
1268 evoice->running = 0;
1270 snd_pcm_trigger_done(s, substream);
1275 spin_lock(&codec->reg_lock);
1277 outl(what, ALI_REG(codec, ALI_STOP));
1279 val = inl(ALI_REG(codec, ALI_AINTEN));
1285 outl(val, ALI_REG(codec, ALI_AINTEN));
1287 outl(what, ALI_REG(codec, ALI_START));
1289 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1290 spin_unlock(&codec->reg_lock);
1295 static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1296 snd_pcm_hw_params_t * hw_params)
1298 ali_t *codec = snd_pcm_substream_chip(substream);
1299 snd_pcm_runtime_t *runtime = substream->runtime;
1300 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1301 snd_ali_voice_t *evoice = pvoice->extra;
1303 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1304 if (err < 0) return err;
1306 /* voice management */
1308 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1309 if (evoice == NULL) {
1310 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1313 pvoice->extra = evoice;
1314 evoice->substream = substream;
1317 if (evoice != NULL) {
1318 snd_ali_free_voice(codec, evoice);
1319 pvoice->extra = evoice = NULL;
1326 static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1328 ali_t *codec = snd_pcm_substream_chip(substream);
1329 snd_pcm_runtime_t *runtime = substream->runtime;
1330 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1331 snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1333 snd_pcm_lib_free_pages(substream);
1334 if (evoice != NULL) {
1335 snd_ali_free_voice(codec, evoice);
1336 pvoice->extra = NULL;
1341 static int snd_ali_capture_hw_params(snd_pcm_substream_t * substream,
1342 snd_pcm_hw_params_t * hw_params)
1344 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1347 static int snd_ali_capture_hw_free(snd_pcm_substream_t * substream)
1349 return snd_pcm_lib_free_pages(substream);
1352 static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1354 ali_t *codec = snd_pcm_substream_chip(substream);
1355 snd_pcm_runtime_t *runtime = substream->runtime;
1356 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1357 snd_ali_voice_t *evoice = pvoice->extra;
1358 unsigned long flags;
1369 snd_ali_printk("playback_prepare ...\n");
1371 spin_lock_irqsave(&codec->reg_lock, flags);
1373 /* set Delta (rate) value */
1374 Delta = snd_ali_convert_rate(runtime->rate, 0);
1376 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1377 (pvoice->number == ALI_PCM_IN_CHANNEL))
1378 snd_ali_disable_special_channel(codec, pvoice->number);
1379 else if (codec->spdif_support &&
1380 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1381 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1382 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1386 /* set Loop Back Address */
1387 LBA = runtime->dma_addr;
1389 /* set interrupt count size */
1390 pvoice->count = runtime->period_size;
1392 /* set target ESO for channel */
1393 pvoice->eso = runtime->buffer_size;
1395 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1397 /* set ESO to capture first MIDLP interrupt */
1398 ESO = pvoice->eso -1;
1400 CTRL = snd_ali_control_mode(substream);
1406 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);
1407 snd_ali_write_voice_regs( codec,
1419 if (evoice != NULL) {
1420 evoice->count = pvoice->count;
1421 evoice->eso = pvoice->count << 1;
1422 ESO = evoice->eso - 1;
1423 snd_ali_write_voice_regs(codec,
1432 (unsigned int)0x3ff,
1436 spin_unlock_irqrestore(&codec->reg_lock, flags);
1441 static int snd_ali_capture_prepare(snd_pcm_substream_t * substream)
1443 ali_t *codec = snd_pcm_substream_chip(substream);
1444 snd_pcm_runtime_t *runtime = substream->runtime;
1445 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1446 unsigned long flags;
1457 spin_lock_irqsave(&codec->reg_lock, flags);
1459 snd_ali_printk("capture_prepare...\n");
1461 snd_ali_enable_special_channel(codec,pvoice->number);
1463 Delta = snd_ali_convert_rate(runtime->rate, 1);
1465 // Prepare capture intr channel
1466 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1470 if (codec->revision != ALI_5451_V02) {
1471 spin_unlock_irqrestore(&codec->reg_lock, flags);
1474 rate = snd_ali_get_spdif_in_rate(codec);
1476 snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1479 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1480 if (bValue & 0x10) {
1481 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1482 printk("clear SPDIF parity error flag.\n");
1486 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1489 // set target ESO for channel
1490 pvoice->eso = runtime->buffer_size;
1492 // set interrupt count size
1493 pvoice->count = runtime->period_size;
1495 // set Loop Back Address
1496 LBA = runtime->dma_addr;
1498 // set ESO to capture first MIDLP interrupt
1499 ESO = pvoice->eso - 1;
1500 CTRL = snd_ali_control_mode(substream);
1506 snd_ali_write_voice_regs( codec,
1520 spin_unlock_irqrestore(&codec->reg_lock, flags);
1526 static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1528 ali_t *codec = snd_pcm_substream_chip(substream);
1529 snd_pcm_runtime_t *runtime = substream->runtime;
1530 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1532 unsigned long flags;
1534 spin_lock_irqsave(&codec->reg_lock, flags);
1535 if (!pvoice->running) {
1536 spin_unlock_irqrestore(&codec->reg_lock, flags);
1539 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1540 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1541 spin_unlock_irqrestore(&codec->reg_lock, flags);
1542 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1548 static snd_pcm_uframes_t snd_ali_capture_pointer(snd_pcm_substream_t *substream)
1550 ali_t *codec = snd_pcm_substream_chip(substream);
1551 snd_pcm_runtime_t *runtime = substream->runtime;
1552 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1554 unsigned long flags;
1556 spin_lock_irqsave(&codec->reg_lock, flags);
1557 if (!pvoice->running) {
1558 spin_unlock_irqrestore(&codec->reg_lock, flags);
1561 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1562 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1563 spin_unlock_irqrestore(&codec->reg_lock, flags);
1568 static snd_pcm_hardware_t snd_ali_playback =
1570 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1571 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1572 SNDRV_PCM_INFO_MMAP_VALID |
1573 SNDRV_PCM_INFO_RESUME |
1574 SNDRV_PCM_INFO_SYNC_START),
1575 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1576 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1577 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1582 .buffer_bytes_max = (256*1024),
1583 .period_bytes_min = 64,
1584 .period_bytes_max = (256*1024),
1586 .periods_max = 1024,
1591 * Capture support device description
1594 static snd_pcm_hardware_t snd_ali_capture =
1596 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1597 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1598 SNDRV_PCM_INFO_MMAP_VALID |
1599 SNDRV_PCM_INFO_RESUME |
1600 SNDRV_PCM_INFO_SYNC_START),
1601 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1602 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1603 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1608 .buffer_bytes_max = (128*1024),
1609 .period_bytes_min = 64,
1610 .period_bytes_max = (128*1024),
1612 .periods_max = 1024,
1616 static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1618 unsigned long flags;
1619 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1623 codec = pvoice->codec;
1624 spin_lock_irqsave(&codec->reg_lock, flags);
1625 snd_ali_free_voice(pvoice->codec, pvoice);
1626 spin_unlock_irqrestore(&codec->reg_lock, flags);
1630 static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1632 ali_t *codec = snd_pcm_substream_chip(substream);
1633 snd_pcm_runtime_t *runtime = substream->runtime;
1634 snd_ali_voice_t *pvoice;
1635 unsigned long flags = 0;
1637 spin_lock_irqsave(&codec->reg_lock, flags);
1638 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1639 if (pvoice == NULL) {
1640 spin_unlock_irqrestore(&codec->reg_lock, flags);
1643 pvoice->codec = codec;
1644 spin_unlock_irqrestore(&codec->reg_lock, flags);
1646 pvoice->substream = substream;
1647 runtime->private_data = pvoice;
1648 runtime->private_free = snd_ali_pcm_free_substream;
1650 runtime->hw = snd_ali_playback;
1651 snd_pcm_set_sync(substream);
1652 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1657 static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1659 ali_t *codec = snd_pcm_substream_chip(substream);
1660 snd_pcm_runtime_t *runtime = substream->runtime;
1661 snd_ali_voice_t *pvoice;
1662 unsigned long flags;
1664 spin_lock_irqsave(&codec->reg_lock, flags);
1665 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 1);
1666 if (pvoice == NULL) {
1667 spin_unlock_irqrestore(&codec->reg_lock, flags);
1670 pvoice->codec = codec;
1671 spin_unlock_irqrestore(&codec->reg_lock, flags);
1673 pvoice->substream = substream;
1674 runtime->private_data = pvoice;
1675 runtime->private_free = snd_ali_pcm_free_substream;
1676 runtime->hw = snd_ali_capture;
1677 snd_pcm_set_sync(substream);
1678 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1683 static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1688 static int snd_ali_capture_close(snd_pcm_substream_t * substream)
1690 ali_t *codec = snd_pcm_substream_chip(substream);
1691 snd_pcm_runtime_t *runtime = substream->runtime;
1692 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1694 snd_ali_disable_special_channel(codec,pvoice->number);
1699 static snd_pcm_ops_t snd_ali_playback_ops = {
1700 .open = snd_ali_playback_open,
1701 .close = snd_ali_playback_close,
1702 .ioctl = snd_ali_ioctl,
1703 .hw_params = snd_ali_playback_hw_params,
1704 .hw_free = snd_ali_playback_hw_free,
1705 .prepare = snd_ali_playback_prepare,
1706 .trigger = snd_ali_trigger,
1707 .pointer = snd_ali_playback_pointer,
1710 static snd_pcm_ops_t snd_ali_capture_ops = {
1711 .open = snd_ali_capture_open,
1712 .close = snd_ali_capture_close,
1713 .ioctl = snd_ali_ioctl,
1714 .hw_params = snd_ali_capture_hw_params,
1715 .hw_free = snd_ali_capture_hw_free,
1716 .prepare = snd_ali_capture_prepare,
1717 .trigger = snd_ali_trigger,
1718 .pointer = snd_ali_capture_pointer,
1722 static void snd_ali_pcm_free(snd_pcm_t *pcm)
1724 ali_t *codec = snd_magic_cast(ali_t, pcm->private_data, return);
1728 static int __devinit snd_ali_pcm(ali_t * codec, int device, snd_pcm_t ** rpcm)
1733 if (rpcm) *rpcm = NULL;
1734 err = snd_pcm_new(codec->card, "ALI 5451", device, ALI_CHANNELS, 1, &pcm);
1736 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1739 pcm->private_data = codec;
1740 pcm->private_free = snd_ali_pcm_free;
1741 pcm->info_flags = 0;
1742 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ali_playback_ops);
1743 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ali_capture_ops);
1745 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1746 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1748 pcm->info_flags = 0;
1749 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1750 strcpy(pcm->name, "ALI 5451");
1752 if (rpcm) *rpcm = pcm;
1756 #define ALI5451_SPDIF(xname, xindex, value) \
1757 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1758 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1759 .put = snd_ali5451_spdif_put, .private_value = value}
1761 static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1763 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1765 uinfo->value.integer.min = 0;
1766 uinfo->value.integer.max = 1;
1770 static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1772 unsigned long flags;
1773 ali_t *codec = snd_magic_cast(ali_t, kcontrol->private_data, -ENXIO);
1774 unsigned int enable;
1776 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1778 spin_lock_irqsave(&codec->reg_lock, flags);
1779 switch(kcontrol->private_value) {
1781 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1784 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1787 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1792 ucontrol->value.integer.value[0] = enable;
1793 spin_unlock_irqrestore(&codec->reg_lock, flags);
1797 static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1799 unsigned long flags;
1800 ali_t *codec = snd_magic_cast(ali_t, kcontrol->private_data, -ENXIO);
1801 unsigned int change = 0, enable = 0;
1803 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1805 spin_lock_irqsave(&codec->reg_lock, flags);
1806 switch (kcontrol->private_value) {
1808 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1809 change = change ^ enable;
1812 codec->spdif_mask |= 0x02;
1813 snd_ali_enable_spdif_out(codec);
1815 codec->spdif_mask &= ~(0x02);
1816 codec->spdif_mask &= ~(0x04);
1817 snd_ali_disable_spdif_out(codec);
1822 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1823 change = change ^ enable;
1824 if (change && (codec->spdif_mask & 0x02)) {
1826 codec->spdif_mask |= 0x04;
1827 snd_ali_enable_spdif_chnout(codec);
1829 codec->spdif_mask &= ~(0x04);
1830 snd_ali_disable_spdif_chnout(codec);
1835 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1836 change = change ^ enable;
1839 codec->spdif_mask |= 0x01;
1840 snd_ali_enable_spdif_in(codec);
1842 codec->spdif_mask &= ~(0x01);
1843 snd_ali_disable_spdif_in(codec);
1850 spin_unlock_irqrestore(&codec->reg_lock, flags);
1855 static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1856 /* spdif aplayback switch */
1857 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1858 ALI5451_SPDIF("IEC958 Output switch", 0, 0),
1859 /* spdif out to spdif channel */
1860 ALI5451_SPDIF("IEC958 Channel Output Switch", 0, 1),
1861 /* spdif in from spdif channel */
1862 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1865 static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1867 ali_t *codec = snd_magic_cast(ali_t, bus->private_data, return);
1868 codec->ac97_bus = NULL;
1871 static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1873 ali_t *codec = snd_magic_cast(ali_t, ac97->private_data, return);
1877 static int __devinit snd_ali_mixer(ali_t * codec)
1884 memset(&bus, 0, sizeof(bus));
1885 bus.write = snd_ali_codec_write;
1886 bus.read = snd_ali_codec_read;
1887 bus.private_data = codec;
1888 bus.private_free = snd_ali_mixer_free_ac97_bus;
1889 if ((err = snd_ac97_bus(codec->card, &bus, &codec->ac97_bus)) < 0)
1892 memset(&ac97, 0, sizeof(ac97));
1893 ac97.private_data = codec;
1894 ac97.private_free = snd_ali_mixer_free_ac97;
1895 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97)) < 0) {
1896 snd_printk("ali mixer creating error.\n");
1899 if (codec->spdif_support) {
1900 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1901 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1902 if (err < 0) return err;
1909 static void ali_suspend(ali_t *chip)
1918 spin_lock_irq(&chip->reg_lock);
1920 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1921 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
1922 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1924 // disable all IRQ bits
1925 outl(0, ALI_REG(chip, ALI_MISCINT));
1927 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1928 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1930 im->regs[i] = inl(ALI_REG(chip, i*4));
1933 for (i = 0; i < ALI_CHANNELS; i++) {
1934 outb(i, ALI_REG(chip, ALI_GC_CIR));
1935 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1936 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1939 // stop all HW channel
1940 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1942 spin_unlock_irq(&chip->reg_lock);
1945 static void ali_resume(ali_t *chip)
1954 pci_enable_device(chip->pci);
1956 spin_lock_irq(&chip->reg_lock);
1958 for (i = 0; i < ALI_CHANNELS; i++) {
1959 outb(i, ALI_REG(chip, ALI_GC_CIR));
1960 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1961 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1964 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1965 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
1967 outl(im->regs[i], ALI_REG(chip, i*4));
1970 snd_ac97_resume(chip->ac97);
1973 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1974 // restore IRQ enable bits
1975 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1977 spin_unlock_irq(&chip->reg_lock);
1981 static int snd_ali_suspend(struct pci_dev *dev, u32 state)
1983 ali_t *chip = snd_magic_cast(ali_t, pci_get_drvdata(dev), return -ENXIO);
1987 static int snd_ali_resume(struct pci_dev *dev)
1989 ali_t *chip = snd_magic_cast(ali_t, pci_get_drvdata(dev), return -ENXIO);
1993 #endif /* CONFIG_PM */
1995 static int snd_ali_free(ali_t * codec)
1997 if (codec->hw_initialized)
1998 snd_ali_disable_address_interrupt(codec);
1999 if (codec->irq >= 0) {
2000 synchronize_irq(codec->irq);
2001 free_irq(codec->irq, (void *)codec);
2003 if (codec->res_port) {
2004 release_resource(codec->res_port);
2005 kfree_nocheck(codec->res_port);
2009 kfree(codec->image);
2011 snd_magic_kfree(codec);
2015 static int snd_ali_chip_init(ali_t *codec)
2017 unsigned int legacy;
2019 struct pci_dev *pci_dev = NULL;
2021 snd_ali_printk("chip initializing ... \n");
2023 if (snd_ali_reset_5451(codec)) {
2024 snd_printk("ali_chip_init: reset 5451 error.\n");
2028 if (codec->revision == ALI_5451_V02) {
2029 pci_dev = codec->pci_m1533;
2030 pci_read_config_byte(pci_dev, 0x59, &temp);
2032 pci_write_config_byte(pci_dev, 0x59, temp);
2034 pci_dev = codec->pci_m7101;
2035 pci_read_config_byte(pci_dev, 0xb8, &temp);
2037 pci_write_config_byte(pci_dev, 0xB8, temp);
2040 pci_read_config_dword(codec->pci, 0x44, &legacy);
2041 legacy &= 0xff00ff00;
2042 legacy |= 0x000800aa;
2043 pci_write_config_dword(codec->pci, 0x44, legacy);
2045 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2046 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2047 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2048 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2049 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2051 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2052 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2053 if (codec->spdif_support) {
2054 snd_ali_enable_spdif_out(codec);
2055 codec->spdif_mask = 0x00000002;
2058 snd_ali_printk("chip initialize succeed.\n");
2063 static int __devinit snd_ali_resources(ali_t *codec)
2065 snd_ali_printk("resouces allocation ...\n");
2066 if ((codec->res_port = request_region(codec->port, 0x100, "ALI 5451")) == NULL) {
2067 snd_printk("Unalbe to request io ports.\n");
2071 if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2072 snd_printk("Unable to request irq.\n");
2075 codec->irq = codec->pci->irq;
2076 snd_ali_printk("resouces allocated.\n");
2079 static int snd_ali_dev_free(snd_device_t *device)
2081 ali_t *codec=snd_magic_cast(ali_t, device->device_data, return -ENXIO);
2082 snd_ali_free(codec);
2086 static int __devinit snd_ali_create(snd_card_t * card,
2087 struct pci_dev *pci,
2094 unsigned short cmdw = 0;
2095 struct pci_dev *pci_dev = NULL;
2096 static snd_device_ops_t ops = {
2097 (snd_dev_free_t *)snd_ali_dev_free,
2104 snd_ali_printk("creating ...\n");
2106 /* enable PCI device */
2107 if ((err = pci_enable_device(pci)) < 0)
2109 /* check, if we can restrict PCI DMA transfers to 31 bits */
2110 if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
2111 pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2112 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2116 if ((codec = snd_magic_kcalloc(ali_t, 0, GFP_KERNEL)) == NULL)
2119 spin_lock_init(&codec->reg_lock);
2120 spin_lock_init(&codec->voice_alloc);
2125 codec->port = pci_resource_start(pci, 0);
2126 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2127 codec->spdif_support = spdif_support;
2129 if (pcm_streams < 1)
2131 if (pcm_streams > 32)
2134 pci_set_master(pci);
2135 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2136 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2137 cmdw |= PCI_COMMAND_IO;
2138 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2140 pci_set_master(pci);
2142 if (snd_ali_resources(codec)) {
2143 snd_ali_free(codec);
2147 synchronize_irq(pci->irq);
2149 codec->synth.chmap = 0;
2150 codec->synth.chcnt = 0;
2151 codec->spdif_mask = 0;
2152 codec->synth.synthcount = 0;
2154 if (codec->revision == ALI_5451_V02)
2155 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2157 codec->chregs.regs.ac97read = ALI_AC97_READ;
2158 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2160 codec->chregs.regs.start = ALI_START;
2161 codec->chregs.regs.stop = ALI_STOP;
2162 codec->chregs.regs.aint = ALI_AINT;
2163 codec->chregs.regs.ainten = ALI_AINTEN;
2165 codec->chregs.data.start = 0x00;
2166 codec->chregs.data.stop = 0x00;
2167 codec->chregs.data.aint = 0x00;
2168 codec->chregs.data.ainten = 0x00;
2170 /* M1533: southbridge */
2171 pci_dev = pci_find_device(0x10b9, 0x1533, NULL);
2172 codec->pci_m1533 = pci_dev;
2173 if (! codec->pci_m1533) {
2174 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2175 snd_ali_free(codec);
2178 /* M7101: power management */
2179 pci_dev = pci_find_device(0x10b9, 0x7101, NULL);
2180 codec->pci_m7101 = pci_dev;
2181 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2182 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2183 snd_ali_free(codec);
2187 snd_ali_printk("snd_device_new is called.\n");
2188 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2189 snd_ali_free(codec);
2193 /* initialise synth voices*/
2194 for (i = 0; i < ALI_CHANNELS; i++ ) {
2195 codec->synth.voices[i].number = i;
2198 if ((err = snd_ali_chip_init(codec)) < 0) {
2199 snd_printk("ali create: chip init error.\n");
2204 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2206 snd_printk("can't allocate apm buffer\n");
2209 snd_ali_enable_address_interrupt(codec);
2210 codec->hw_initialized = 1;
2213 snd_ali_printk("created.\n");
2217 static int __devinit snd_ali_probe(struct pci_dev *pci,
2218 const struct pci_device_id *pci_id)
2225 snd_ali_printk("probe ...\n");
2227 if (dev >= SNDRV_CARDS)
2234 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2238 if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2239 snd_card_free(card);
2243 snd_ali_printk("mixer building ...\n");
2244 if ((err = snd_ali_mixer(codec)) < 0) {
2245 snd_card_free(card);
2249 snd_ali_printk("pcm building ...\n");
2250 if ((err = snd_ali_pcm(codec, 0, NULL)) < 0) {
2251 snd_card_free(card);
2255 strcpy(card->driver, "ALI5451");
2256 strcpy(card->shortname, "ALI 5451");
2258 sprintf(card->longname, "%s at 0x%lx, irq %li",
2259 card->shortname, codec->port, codec->irq);
2261 snd_ali_printk("register card.\n");
2262 if ((err = snd_card_register(card)) < 0) {
2263 snd_card_free(card);
2266 pci_set_drvdata(pci, codec);
2271 static void __devexit snd_ali_remove(struct pci_dev *pci)
2273 ali_t *chip = snd_magic_cast(ali_t, pci_get_drvdata(pci), return);
2275 snd_card_free(chip->card);
2276 pci_set_drvdata(pci, NULL);
2279 static struct pci_driver driver = {
2281 .id_table = snd_ali_ids,
2282 .probe = snd_ali_probe,
2283 .remove = __devexit_p(snd_ali_remove),
2285 .suspend = snd_ali_suspend,
2286 .resume = snd_ali_resume,
2290 static int __init alsa_card_ali_init(void)
2294 if ((err = pci_module_init(&driver)) < 0) {
2296 printk(KERN_ERR "ALi pci audio not found or device busy.\n");
2303 static void __exit alsa_card_ali_exit(void)
2305 pci_unregister_driver(&driver);
2308 module_init(alsa_card_ali_init)
2309 module_exit(alsa_card_ali_exit)
2313 /* format is: snd-ali5451=enable,index,id,pcm_channels */
2315 static int __init alsa_card_ali_setup(char *str)
2317 static unsigned __initdata nr_dev = 0;
2319 if (nr_dev >= SNDRV_CARDS)
2321 (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2322 get_option(&str,&index[nr_dev]) == 2 &&
2323 get_id(&str,&id[nr_dev]) == 2 &&
2324 get_option(&str,&pcm_channels[nr_dev]) == 2);
2329 __setup("snd-ali5451=", alsa_card_ali_setup);