2 * Matt Wu <Matt_Wu@acersoftech.com.cn>
4 * Routines for control of ALi pci audio M5451
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public Lcodecnse as published by
14 * the Free Software Foundation; either version 2 of the Lcodecnse, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public Lcodecnse for more details.
22 * You should have received a copy of the GNU General Public Lcodecnse
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sound/driver.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/slab.h>
35 #include <linux/moduleparam.h>
36 #include <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/info.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/mpu401.h>
41 #include <sound/initval.h>
43 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
44 MODULE_DESCRIPTION("ALI M5451");
45 MODULE_LICENSE("GPL");
46 MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
48 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
49 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
50 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
51 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 32};
52 static int spdif[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
54 module_param_array(index, int, NULL, 0444);
55 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
56 module_param_array(id, charp, NULL, 0444);
57 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
58 module_param_array(enable, bool, NULL, 0444);
59 MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
60 module_param_array(pcm_channels, int, NULL, 0444);
61 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
62 module_param_array(spdif, bool, NULL, 0444);
63 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
66 * Debug part definitions
72 #define snd_ali_printk(format, args...) printk(format, ##args);
74 #define snd_ali_printk(format, args...)
78 * Constants definition
81 #ifndef PCI_VENDOR_ID_ALI
82 #define PCI_VENDOR_ID_ALI 0x10b9
85 #ifndef PCI_DEVICE_ID_ALI_5451
86 #define PCI_DEVICE_ID_ALI_5451 0x5451
89 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_ALI<<16)|PCI_DEVICE_ID_ALI_5451)
92 #define ALI_CHANNELS 32
94 #define ALI_PCM_IN_CHANNEL 31
95 #define ALI_SPDIF_IN_CHANNEL 19
96 #define ALI_SPDIF_OUT_CHANNEL 15
97 #define ALI_CENTER_CHANNEL 24
98 #define ALI_LEF_CHANNEL 23
99 #define ALI_SURR_LEFT_CHANNEL 26
100 #define ALI_SURR_RIGHT_CHANNEL 25
102 #define SNDRV_ALI_VOICE_TYPE_PCM 01
103 #define SNDRV_ALI_VOICE_TYPE_OTH 02
105 #define ALI_5451_V02 0x02
111 #define ALI_LEGACY_DMAR0 0x00 // ADR0
112 #define ALI_LEGACY_DMAR4 0x04 // CNT0
113 #define ALI_LEGACY_DMAR11 0x0b // MOD
114 #define ALI_LEGACY_DMAR15 0x0f // MMR
115 #define ALI_MPUR0 0x20
116 #define ALI_MPUR1 0x21
117 #define ALI_MPUR2 0x22
118 #define ALI_MPUR3 0x23
120 #define ALI_AC97_WRITE 0x40
121 #define ALI_AC97_READ 0x44
123 #define ALI_SCTRL 0x48
124 #define ALI_SPDIF_OUT_ENABLE 0x20
125 #define ALI_AC97_GPIO 0x4c
126 #define ALI_SPDIF_CS 0x70
127 #define ALI_SPDIF_CTRL 0x74
128 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
129 #define ALI_SPDIF_IN_CH_STATUS 0x40
130 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
131 #define ALI_START 0x80
132 #define ALI_STOP 0x84
133 #define ALI_CSPF 0x90
134 #define ALI_AINT 0x98
135 #define ALI_GC_CIR 0xa0
136 #define ENDLP_IE 0x00001000
137 #define MIDLP_IE 0x00002000
138 #define ALI_AINTEN 0xa4
139 #define ALI_VOLUME 0xa8
140 #define ALI_SBDELTA_DELTA_R 0xac
141 #define ALI_MISCINT 0xb0
142 #define ADDRESS_IRQ 0x00000020
143 #define TARGET_REACHED 0x00008000
144 #define MIXER_OVERFLOW 0x00000800
145 #define MIXER_UNDERFLOW 0x00000400
146 #define ALI_SBBL_SBCL 0xc0
147 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
148 #define ALI_STIMER 0xc8
149 #define ALI_GLOBAL_CONTROL 0xd4
150 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
151 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
152 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
153 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
154 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
156 #define ALI_CSO_ALPHA_FMS 0xe0
158 #define ALI_ESO_DELTA 0xe8
159 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
160 #define ALI_EBUF1 0xf4
161 #define ALI_EBUF2 0xf8
163 #define ALI_REG(codec, x) ((codec)->port + x)
165 typedef struct snd_stru_ali ali_t;
166 typedef struct snd_ali_stru_voice snd_ali_voice_t;
168 typedef struct snd_ali_channel_control {
177 // register addresses
183 unsigned int ac97read;
184 unsigned int ac97write;
187 } snd_ali_channel_control_t;
189 struct snd_ali_stru_voice {
199 snd_pcm_substream_t *substream;
200 snd_ali_voice_t *extra;
204 int eso; /* final ESO value for channel */
205 int count; /* runtime->period_size */
210 void (*private_free)(void *private_data);
214 typedef struct snd_stru_alidev {
216 snd_ali_voice_t voices[ALI_CHANNELS];
218 unsigned int chcnt; /* num of opened channels */
219 unsigned int chmap; /* bitmap for opened channels */
220 unsigned int synthcount;
226 #define ALI_GLOBAL_REGS 56
227 #define ALI_CHANNEL_REGS 8
228 typedef struct snd_ali_image {
229 unsigned long regs[ALI_GLOBAL_REGS];
230 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
235 struct snd_stru_ali {
238 unsigned char revision;
240 unsigned int hw_initialized: 1;
241 unsigned int spdif_support: 1;
244 struct pci_dev *pci_m1533;
245 struct pci_dev *pci_m7101;
250 snd_ali_channel_control_t chregs;
253 unsigned int spdif_mask;
255 unsigned int spurious_irq_count;
256 unsigned int spurious_irq_max_delta;
258 ac97_bus_t *ac97_bus;
260 unsigned short ac97_ext_id;
261 unsigned short ac97_ext_status;
264 spinlock_t voice_alloc;
271 static struct pci_device_id snd_ali_ids[] = {
272 {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
275 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
277 static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
278 static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
279 static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
287 static void ali_read_regs(ali_t *codec, int channel)
292 printk("channel %d registers map:\n", channel);
293 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
297 printk("%2.2x ", j*4);
300 for (i=0; i<=0xf8/4;i++) {
302 printk("%2.2x ", (i*4/0x10)*0x10);
303 dwVal = inl(ALI_REG(codec,i*4));
304 printk("%8.8x ", dwVal);
310 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
313 struct pci_dev *pci_dev = NULL;
317 pci_dev = pci_find_device(vendor, deviceid, pci_dev);
321 printk("\nM%x PCI CFG\n", deviceid);
331 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
332 printk("%8.8x ", dwVal);
337 static void ali_read_ac97regs(ali_t *codec, int secondary)
342 printk("\ncodec %d registers map:\n", secondary);
346 printk("%2.2x ",j*2);
349 for (i=0; i<64;i++) {
351 printk("%2.2x ", (i/8)*0x10);
352 wVal = snd_ali_codec_peek(codec, secondary, i*2);
353 printk("%4.4x ", wVal);
366 static inline unsigned int snd_ali_5451_peek(ali_t *codec,
369 return (unsigned int)inl(ALI_REG(codec, port));
372 static inline void snd_ali_5451_poke( ali_t *codec,
376 outl((unsigned int)val, ALI_REG(codec, port));
379 static int snd_ali_codec_ready( ali_t *codec,
383 unsigned long end_time;
386 end_time = jiffies + 10 * (HZ >> 2);
388 res = snd_ali_5451_peek(codec,port);
389 if (! (res & 0x8000))
392 set_current_state(TASK_UNINTERRUPTIBLE);
395 } while (time_after_eq(end_time, jiffies));
396 snd_ali_5451_poke(codec, port, res & ~0x8000);
397 snd_printdd("ali_codec_ready: codec is not ready.\n ");
401 static int snd_ali_stimer_ready(ali_t *codec, int sched)
403 unsigned long end_time;
404 unsigned long dwChk1,dwChk2;
406 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
407 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
409 end_time = jiffies + 10 * (HZ >> 2);
411 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
412 if (dwChk2 != dwChk1)
415 set_current_state(TASK_UNINTERRUPTIBLE);
418 } while (time_after_eq(end_time, jiffies));
419 snd_printk("ali_stimer_read: stimer is not ready.\n");
423 static void snd_ali_codec_poke(ali_t *codec,int secondary,
427 unsigned int dwVal = 0;
428 unsigned int port = 0;
431 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
435 port = codec->chregs.regs.ac97write;
437 if (snd_ali_codec_ready(codec, port, 0) < 0)
439 if (snd_ali_stimer_ready(codec, 0) < 0)
442 dwVal = (unsigned int) (reg & 0xff);
443 dwVal |= 0x8000 | (val << 16);
444 if (secondary) dwVal |= 0x0080;
445 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
447 snd_ali_5451_poke(codec,port,dwVal);
452 static unsigned short snd_ali_codec_peek( ali_t *codec,
456 unsigned int dwVal = 0;
457 unsigned int port = 0;
460 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
464 port = codec->chregs.regs.ac97read;
466 if (snd_ali_codec_ready(codec, port, 0) < 0)
468 if (snd_ali_stimer_ready(codec, 0) < 0)
471 dwVal = (unsigned int) (reg & 0xff);
472 dwVal |= 0x8000; /* bit 15*/
473 if (secondary) dwVal |= 0x0080;
475 snd_ali_5451_poke(codec, port, dwVal);
477 if (snd_ali_stimer_ready(codec, 0) < 0)
479 if (snd_ali_codec_ready(codec, port, 0) < 0)
482 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
485 static void snd_ali_codec_write(ac97_t *ac97,
489 ali_t *codec = ac97->private_data;
491 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
492 snd_ali_codec_poke(codec, 0, reg, val);
497 static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
499 ali_t *codec = ac97->private_data;
501 snd_ali_printk("codec_read reg=%xh.\n", reg);
502 return (snd_ali_codec_peek(codec, 0, reg));
509 static int snd_ali_reset_5451(ali_t *codec)
511 struct pci_dev *pci_dev = NULL;
512 unsigned short wCount, wReg;
515 if ((pci_dev = codec->pci_m1533) != NULL) {
516 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
517 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
519 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
520 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
524 pci_dev = codec->pci;
525 pci_read_config_dword(pci_dev, 0x44, &dwVal);
526 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
528 pci_read_config_dword(pci_dev, 0x44, &dwVal);
529 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
534 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
535 if((wReg & 0x000f) == 0x000f)
540 /* non-fatal if you have a non PM capable codec */
541 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
547 static int snd_ali_reset_codec(ali_t *codec)
549 struct pci_dev *pci_dev = NULL;
550 unsigned char bVal = 0;
552 unsigned short wCount, wReg;
554 pci_dev = codec->pci_m1533;
556 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
557 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
559 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
560 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
563 bVal = inb(ALI_REG(codec,ALI_SCTRL));
565 outb(ALI_REG(codec,ALI_SCTRL),bVal);
567 bVal = inb(ALI_REG(codec,ALI_SCTRL));
569 outb(ALI_REG(codec,ALI_SCTRL),bVal);
574 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
575 if((wReg & 0x000f) == 0x000f)
585 * ALI 5451 Controller
588 static void snd_ali_enable_special_channel(ali_t *codec, unsigned int channel)
590 unsigned long dwVal = 0;
592 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
593 dwVal |= 1 << (channel & 0x0000001f);
594 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
597 static void snd_ali_disable_special_channel(ali_t *codec, unsigned int channel)
599 unsigned long dwVal = 0;
601 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
602 dwVal &= ~(1 << (channel & 0x0000001f));
603 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
606 static void snd_ali_enable_address_interrupt(ali_t * codec)
610 gc = inl(ALI_REG(codec, ALI_GC_CIR));
613 outl( gc, ALI_REG(codec, ALI_GC_CIR));
616 static void snd_ali_disable_address_interrupt(ali_t * codec)
620 gc = inl(ALI_REG(codec, ALI_GC_CIR));
623 outl(gc, ALI_REG(codec, ALI_GC_CIR));
627 static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
630 snd_ali_channel_control_t *pchregs = &(codec->chregs);
632 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
634 mask = 1 << (channel & 0x1f);
635 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
636 pchregs->data.ainten |= mask;
637 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
641 static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
644 snd_ali_channel_control_t *pchregs = &(codec->chregs);
646 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
648 mask = 1 << (channel & 0x1f);
649 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
650 pchregs->data.ainten &= ~mask;
651 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
654 static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
656 unsigned int idx = channel & 0x1f;
658 if (codec->synth.chcnt >= ALI_CHANNELS){
659 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
663 if (!(codec->synth.chmap & (1 << idx))) {
664 codec->synth.chmap |= 1 << idx;
665 codec->synth.chcnt++;
666 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
672 static int snd_ali_find_free_channel(ali_t * codec, int rec)
677 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
681 if (codec->spdif_support &&
682 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
683 idx = ALI_SPDIF_IN_CHANNEL;
685 idx = ALI_PCM_IN_CHANNEL;
687 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
690 snd_printk("ali_find_free_channel: record channel is busy now.\n");
696 if (codec->spdif_support &&
697 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
698 idx = ALI_SPDIF_OUT_CHANNEL;
699 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
702 snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
706 for (idx = 0; idx < ALI_CHANNELS; idx++) {
707 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
710 snd_printk("ali_find_free_channel: no free channels.\n");
714 static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
716 unsigned int idx = channel & 0x0000001f;
718 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
720 if (channel < 0 || channel >= ALI_CHANNELS)
723 if (!(codec->synth.chmap & (1 << idx))) {
724 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
727 codec->synth.chmap &= ~(1 << idx);
728 codec->synth.chcnt--;
733 static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
735 unsigned int mask = 1 << (channel & 0x1f);
737 snd_ali_printk("start_voice: channel=%d\n",channel);
738 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
742 static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
744 unsigned int mask = 1 << (channel & 0x1f);
746 snd_ali_printk("stop_voice: channel=%d\n",channel);
747 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
754 static void snd_ali_delay(ali_t *codec,int interval)
756 unsigned long begintimer,currenttimer;
758 begintimer = inl(ALI_REG(codec, ALI_STIMER));
759 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
761 while (currenttimer < begintimer + interval) {
762 if(snd_ali_stimer_ready(codec, 1) < 0)
764 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
768 static void snd_ali_detect_spdif_rate(ali_t *codec)
772 u8 bval = 0, R1 = 0, R2 = 0;
774 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
776 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
778 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
780 snd_ali_delay(codec, 6);
781 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
786 snd_printk("ali_detect_spdif_rate: timeout!\n");
791 while (count++ <= 50000) {
792 snd_ali_delay(codec, 6);
793 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
795 if (R2 != R1) R1 = R2; else break;
799 snd_printk("ali_detect_spdif_rate: timeout!\n");
803 if (R2 >= 0x0b && R2 <= 0x0e) {
804 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
806 wval |= (u16)0x09 << 8 | (u16)0x05;
807 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
809 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
810 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
811 } else if (R2 == 0x12) {
812 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
814 wval |= (u16)0x0E << 8 | (u16)0x08;
815 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
817 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
818 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
822 static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
827 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
830 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
832 snd_ali_detect_spdif_rate(codec);
834 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
837 if (bval == 0) dwRate = 44100;
838 if (bval == 1) dwRate = 48000;
839 if (bval == 2) dwRate = 32000;
844 static void snd_ali_enable_spdif_in(ali_t *codec)
848 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
849 dwVal |= ALI_SPDIF_IN_SUPPORT;
850 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
852 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
854 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
856 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
859 static void snd_ali_disable_spdif_in(ali_t *codec)
863 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
864 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
865 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
867 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
871 static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
874 unsigned int dwRate = 0;
876 if (rate == 32000) dwRate = 0x300;
877 if (rate == 44100) dwRate = 0;
878 if (rate == 48000) dwRate = 0x200;
880 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
881 bVal &= (unsigned char)(~(1<<6));
883 bVal |= 0x80; //select right
884 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
885 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
887 bVal &= (~0x80); //select left
888 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
889 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
892 static void snd_ali_enable_spdif_out(ali_t *codec)
897 struct pci_dev *pci_dev = NULL;
899 pci_dev = codec->pci_m1533;
902 pci_read_config_byte(pci_dev, 0x61, &bVal);
904 pci_write_config_byte(pci_dev, 0x61, bVal);
905 pci_read_config_byte(pci_dev, 0x7d, &bVal);
907 pci_write_config_byte(pci_dev, 0x7d, bVal);
909 pci_read_config_byte(pci_dev, 0x7e, &bVal);
912 pci_write_config_byte(pci_dev, 0x7e, bVal);
914 bVal = inb(ALI_REG(codec, ALI_SCTRL));
915 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
917 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
918 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
921 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
922 wVal |= ALI_SPDIF_OUT_SEL_PCM;
923 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
924 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
928 static void snd_ali_enable_spdif_chnout(ali_t *codec)
930 unsigned short wVal = 0;
932 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
933 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
934 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
936 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
937 if (flag & ALI_SPDIF_OUT_NON_PCM)
941 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
943 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
946 static void snd_ali_disable_spdif_chnout(ali_t *codec)
948 unsigned short wVal = 0;
949 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
950 wVal |= ALI_SPDIF_OUT_SEL_PCM;
951 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
953 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
956 static void snd_ali_disable_spdif_out(ali_t *codec)
960 bVal = inb(ALI_REG(codec, ALI_SCTRL));
961 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
963 snd_ali_disable_spdif_chnout(codec);
966 static void snd_ali_update_ptr(ali_t *codec,int channel)
968 snd_ali_voice_t *pvoice = NULL;
969 snd_pcm_runtime_t *runtime;
970 snd_ali_channel_control_t *pchregs = NULL;
971 unsigned int old, mask;
973 unsigned int temp, cspf;
976 pchregs = &(codec->chregs);
978 // check if interrupt occurred for channel
979 old = pchregs->data.aint;
980 mask = ((unsigned int) 1L) << (channel & 0x1f);
985 pvoice = &codec->synth.voices[channel];
986 runtime = pvoice->substream->runtime;
989 spin_lock(&codec->reg_lock);
991 if (pvoice->pcm && pvoice->substream) {
994 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
995 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
996 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
998 if (pvoice->running) {
999 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1000 spin_unlock(&codec->reg_lock);
1001 snd_pcm_period_elapsed(pvoice->substream);
1002 spin_lock(&codec->reg_lock);
1004 snd_ali_stop_voice(codec, channel);
1005 snd_ali_disable_voice_irq(codec, channel);
1007 } else if (codec->synth.voices[channel].synth) {
1008 /* synth interrupt */
1009 } else if (codec->synth.voices[channel].midi) {
1010 /* midi interrupt */
1012 /* unknown interrupt */
1013 snd_ali_stop_voice(codec, channel);
1014 snd_ali_disable_voice_irq(codec, channel);
1016 spin_unlock(&codec->reg_lock);
1017 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1018 pchregs->data.aint = old & (~mask);
1021 static void snd_ali_interrupt(ali_t * codec)
1024 unsigned int audio_int;
1025 snd_ali_channel_control_t *pchregs = NULL;
1026 pchregs = &(codec->chregs);
1028 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1029 if (audio_int & ADDRESS_IRQ) {
1030 // get interrupt status for all channels
1031 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1032 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1033 snd_ali_update_ptr(codec, channel);
1036 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1037 ALI_REG(codec,ALI_MISCINT));
1041 static irqreturn_t snd_ali_card_interrupt(int irq,
1043 struct pt_regs *regs)
1045 ali_t *codec = dev_id;
1049 snd_ali_interrupt(codec);
1054 static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec)
1056 snd_ali_voice_t *pvoice = NULL;
1057 unsigned long flags;
1060 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1062 spin_lock_irqsave(&codec->voice_alloc, flags);
1063 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1064 idx = snd_ali_find_free_channel(codec,rec);
1066 snd_printk("ali_alloc_voice: err.\n");
1067 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1070 pvoice = &(codec->synth.voices[idx]);
1074 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1077 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1082 static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1084 unsigned long flags;
1085 void (*private_free)(void *);
1088 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1089 if (pvoice == NULL || !pvoice->use)
1091 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1092 spin_lock_irqsave(&codec->voice_alloc, flags);
1093 private_free = pvoice->private_free;
1094 private_data = pvoice->private_data;
1095 pvoice->private_free = NULL;
1096 pvoice->private_data = NULL;
1098 snd_ali_free_channel_pcm(codec, pvoice->number);
1100 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1101 pvoice->substream = NULL;
1102 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1104 private_free(private_data);
1108 static void snd_ali_clear_voices(ali_t * codec,
1114 for (i = v_min; i <= v_max; i++) {
1115 snd_ali_stop_voice(codec, i);
1116 snd_ali_disable_voice_irq(codec, i);
1120 static void snd_ali_write_voice_regs(ali_t * codec,
1121 unsigned int Channel,
1126 unsigned int ALPHA_FMS,
1133 unsigned int ctlcmds[4];
1135 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1137 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1139 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1140 ctlcmds[3] = (GVSEL << 31) |
1141 ((PAN & 0x0000007f) << 24) |
1142 ((VOL & 0x000000ff) << 16) |
1143 ((CTRL & 0x0000000f) << 12) |
1146 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1148 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1149 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1150 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1151 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1153 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1154 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1157 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1161 if (rate < 4000) rate = 4000;
1162 if (rate > 48000) rate = 48000;
1167 else if (rate == 8000)
1169 else if (rate == 48000)
1172 delta = ((48000 << 12) / rate) & 0x0000ffff;
1176 else if (rate == 8000)
1178 else if (rate == 48000)
1181 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1187 static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1190 snd_pcm_runtime_t *runtime = substream->runtime;
1193 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1196 if (snd_pcm_format_width(runtime->format) == 16)
1197 CTRL |= 0x00000008; // 16-bit data
1198 if (!snd_pcm_format_unsigned(runtime->format))
1199 CTRL |= 0x00000002; // signed data
1200 if (runtime->channels > 1)
1201 CTRL |= 0x00000004; // stereo data
1209 static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1210 unsigned int cmd, void *arg)
1212 return snd_pcm_lib_ioctl(substream, cmd, arg);
1215 static int snd_ali_trigger(snd_pcm_substream_t *substream,
1219 ali_t *codec = snd_pcm_substream_chip(substream);
1220 struct list_head *pos;
1221 snd_pcm_substream_t *s;
1222 unsigned int what, whati, capture_flag;
1223 snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1228 case SNDRV_PCM_TRIGGER_START:
1229 case SNDRV_PCM_TRIGGER_RESUME:
1230 do_start = 1; break;
1231 case SNDRV_PCM_TRIGGER_STOP:
1232 case SNDRV_PCM_TRIGGER_SUSPEND:
1233 do_start = 0; break;
1238 what = whati = capture_flag = 0;
1239 snd_pcm_group_for_each(pos, substream) {
1240 s = snd_pcm_group_substream_entry(pos);
1241 if ((ali_t *) snd_pcm_substream_chip(s) == codec) {
1242 pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1243 evoice = pvoice->extra;
1244 what |= 1 << (pvoice->number & 0x1f);
1245 if (evoice == NULL) {
1246 whati |= 1 << (pvoice->number & 0x1f);
1248 whati |= 1 << (evoice->number & 0x1f);
1249 what |= 1 << (evoice->number & 0x1f);
1252 pvoice->running = 1;
1254 evoice->running = 1;
1256 pvoice->running = 0;
1258 evoice->running = 0;
1260 snd_pcm_trigger_done(s, substream);
1265 spin_lock(&codec->reg_lock);
1267 outl(what, ALI_REG(codec, ALI_STOP));
1269 val = inl(ALI_REG(codec, ALI_AINTEN));
1275 outl(val, ALI_REG(codec, ALI_AINTEN));
1277 outl(what, ALI_REG(codec, ALI_START));
1279 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1280 spin_unlock(&codec->reg_lock);
1285 static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1286 snd_pcm_hw_params_t * hw_params)
1288 ali_t *codec = snd_pcm_substream_chip(substream);
1289 snd_pcm_runtime_t *runtime = substream->runtime;
1290 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1291 snd_ali_voice_t *evoice = pvoice->extra;
1293 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1294 if (err < 0) return err;
1296 /* voice management */
1298 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1299 if (evoice == NULL) {
1300 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1303 pvoice->extra = evoice;
1304 evoice->substream = substream;
1307 if (evoice != NULL) {
1308 snd_ali_free_voice(codec, evoice);
1309 pvoice->extra = evoice = NULL;
1316 static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1318 ali_t *codec = snd_pcm_substream_chip(substream);
1319 snd_pcm_runtime_t *runtime = substream->runtime;
1320 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1321 snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1323 snd_pcm_lib_free_pages(substream);
1324 if (evoice != NULL) {
1325 snd_ali_free_voice(codec, evoice);
1326 pvoice->extra = NULL;
1331 static int snd_ali_capture_hw_params(snd_pcm_substream_t * substream,
1332 snd_pcm_hw_params_t * hw_params)
1334 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1337 static int snd_ali_capture_hw_free(snd_pcm_substream_t * substream)
1339 return snd_pcm_lib_free_pages(substream);
1342 static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1344 ali_t *codec = snd_pcm_substream_chip(substream);
1345 snd_pcm_runtime_t *runtime = substream->runtime;
1346 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1347 snd_ali_voice_t *evoice = pvoice->extra;
1348 unsigned long flags;
1359 snd_ali_printk("playback_prepare ...\n");
1361 spin_lock_irqsave(&codec->reg_lock, flags);
1363 /* set Delta (rate) value */
1364 Delta = snd_ali_convert_rate(runtime->rate, 0);
1366 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1367 (pvoice->number == ALI_PCM_IN_CHANNEL))
1368 snd_ali_disable_special_channel(codec, pvoice->number);
1369 else if (codec->spdif_support &&
1370 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1371 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1372 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1376 /* set Loop Back Address */
1377 LBA = runtime->dma_addr;
1379 /* set interrupt count size */
1380 pvoice->count = runtime->period_size;
1382 /* set target ESO for channel */
1383 pvoice->eso = runtime->buffer_size;
1385 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1387 /* set ESO to capture first MIDLP interrupt */
1388 ESO = pvoice->eso -1;
1390 CTRL = snd_ali_control_mode(substream);
1396 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);
1397 snd_ali_write_voice_regs( codec,
1409 if (evoice != NULL) {
1410 evoice->count = pvoice->count;
1411 evoice->eso = pvoice->count << 1;
1412 ESO = evoice->eso - 1;
1413 snd_ali_write_voice_regs(codec,
1422 (unsigned int)0x3ff,
1426 spin_unlock_irqrestore(&codec->reg_lock, flags);
1431 static int snd_ali_capture_prepare(snd_pcm_substream_t * substream)
1433 ali_t *codec = snd_pcm_substream_chip(substream);
1434 snd_pcm_runtime_t *runtime = substream->runtime;
1435 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1436 unsigned long flags;
1447 spin_lock_irqsave(&codec->reg_lock, flags);
1449 snd_ali_printk("capture_prepare...\n");
1451 snd_ali_enable_special_channel(codec,pvoice->number);
1453 Delta = snd_ali_convert_rate(runtime->rate, 1);
1455 // Prepare capture intr channel
1456 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1460 if (codec->revision != ALI_5451_V02) {
1461 spin_unlock_irqrestore(&codec->reg_lock, flags);
1464 rate = snd_ali_get_spdif_in_rate(codec);
1466 snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1469 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1470 if (bValue & 0x10) {
1471 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1472 printk("clear SPDIF parity error flag.\n");
1476 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1479 // set target ESO for channel
1480 pvoice->eso = runtime->buffer_size;
1482 // set interrupt count size
1483 pvoice->count = runtime->period_size;
1485 // set Loop Back Address
1486 LBA = runtime->dma_addr;
1488 // set ESO to capture first MIDLP interrupt
1489 ESO = pvoice->eso - 1;
1490 CTRL = snd_ali_control_mode(substream);
1496 snd_ali_write_voice_regs( codec,
1510 spin_unlock_irqrestore(&codec->reg_lock, flags);
1516 static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1518 ali_t *codec = snd_pcm_substream_chip(substream);
1519 snd_pcm_runtime_t *runtime = substream->runtime;
1520 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1523 spin_lock(&codec->reg_lock);
1524 if (!pvoice->running) {
1525 spin_unlock(&codec->reg_lock);
1528 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1529 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1530 spin_unlock(&codec->reg_lock);
1531 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1537 static snd_pcm_uframes_t snd_ali_capture_pointer(snd_pcm_substream_t *substream)
1539 ali_t *codec = snd_pcm_substream_chip(substream);
1540 snd_pcm_runtime_t *runtime = substream->runtime;
1541 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1543 unsigned long flags;
1545 spin_lock_irqsave(&codec->reg_lock, flags);
1546 if (!pvoice->running) {
1547 spin_unlock_irqrestore(&codec->reg_lock, flags);
1550 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1551 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1552 spin_unlock_irqrestore(&codec->reg_lock, flags);
1557 static snd_pcm_hardware_t snd_ali_playback =
1559 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1560 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1561 SNDRV_PCM_INFO_MMAP_VALID |
1562 SNDRV_PCM_INFO_RESUME |
1563 SNDRV_PCM_INFO_SYNC_START),
1564 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1565 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1566 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1571 .buffer_bytes_max = (256*1024),
1572 .period_bytes_min = 64,
1573 .period_bytes_max = (256*1024),
1575 .periods_max = 1024,
1580 * Capture support device description
1583 static snd_pcm_hardware_t snd_ali_capture =
1585 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1586 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1587 SNDRV_PCM_INFO_MMAP_VALID |
1588 SNDRV_PCM_INFO_RESUME |
1589 SNDRV_PCM_INFO_SYNC_START),
1590 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1591 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1592 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1597 .buffer_bytes_max = (128*1024),
1598 .period_bytes_min = 64,
1599 .period_bytes_max = (128*1024),
1601 .periods_max = 1024,
1605 static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1607 unsigned long flags;
1608 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1612 codec = pvoice->codec;
1613 spin_lock_irqsave(&codec->reg_lock, flags);
1614 snd_ali_free_voice(pvoice->codec, pvoice);
1615 spin_unlock_irqrestore(&codec->reg_lock, flags);
1619 static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1621 ali_t *codec = snd_pcm_substream_chip(substream);
1622 snd_pcm_runtime_t *runtime = substream->runtime;
1623 snd_ali_voice_t *pvoice;
1624 unsigned long flags = 0;
1626 spin_lock_irqsave(&codec->reg_lock, flags);
1627 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1628 if (pvoice == NULL) {
1629 spin_unlock_irqrestore(&codec->reg_lock, flags);
1632 pvoice->codec = codec;
1633 spin_unlock_irqrestore(&codec->reg_lock, flags);
1635 pvoice->substream = substream;
1636 runtime->private_data = pvoice;
1637 runtime->private_free = snd_ali_pcm_free_substream;
1639 runtime->hw = snd_ali_playback;
1640 snd_pcm_set_sync(substream);
1641 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1646 static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1648 ali_t *codec = snd_pcm_substream_chip(substream);
1649 snd_pcm_runtime_t *runtime = substream->runtime;
1650 snd_ali_voice_t *pvoice;
1651 unsigned long flags;
1653 spin_lock_irqsave(&codec->reg_lock, flags);
1654 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 1);
1655 if (pvoice == NULL) {
1656 spin_unlock_irqrestore(&codec->reg_lock, flags);
1659 pvoice->codec = codec;
1660 spin_unlock_irqrestore(&codec->reg_lock, flags);
1662 pvoice->substream = substream;
1663 runtime->private_data = pvoice;
1664 runtime->private_free = snd_ali_pcm_free_substream;
1665 runtime->hw = snd_ali_capture;
1666 snd_pcm_set_sync(substream);
1667 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1672 static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1677 static int snd_ali_capture_close(snd_pcm_substream_t * substream)
1679 ali_t *codec = snd_pcm_substream_chip(substream);
1680 snd_pcm_runtime_t *runtime = substream->runtime;
1681 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1683 snd_ali_disable_special_channel(codec,pvoice->number);
1688 static snd_pcm_ops_t snd_ali_playback_ops = {
1689 .open = snd_ali_playback_open,
1690 .close = snd_ali_playback_close,
1691 .ioctl = snd_ali_ioctl,
1692 .hw_params = snd_ali_playback_hw_params,
1693 .hw_free = snd_ali_playback_hw_free,
1694 .prepare = snd_ali_playback_prepare,
1695 .trigger = snd_ali_trigger,
1696 .pointer = snd_ali_playback_pointer,
1699 static snd_pcm_ops_t snd_ali_capture_ops = {
1700 .open = snd_ali_capture_open,
1701 .close = snd_ali_capture_close,
1702 .ioctl = snd_ali_ioctl,
1703 .hw_params = snd_ali_capture_hw_params,
1704 .hw_free = snd_ali_capture_hw_free,
1705 .prepare = snd_ali_capture_prepare,
1706 .trigger = snd_ali_trigger,
1707 .pointer = snd_ali_capture_pointer,
1711 static void snd_ali_pcm_free(snd_pcm_t *pcm)
1713 ali_t *codec = pcm->private_data;
1717 static int __devinit snd_ali_pcm(ali_t * codec, int device, snd_pcm_t ** rpcm)
1722 if (rpcm) *rpcm = NULL;
1723 err = snd_pcm_new(codec->card, "ALI 5451", device, ALI_CHANNELS, 1, &pcm);
1725 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1728 pcm->private_data = codec;
1729 pcm->private_free = snd_ali_pcm_free;
1730 pcm->info_flags = 0;
1731 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ali_playback_ops);
1732 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ali_capture_ops);
1734 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1735 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1737 pcm->info_flags = 0;
1738 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1739 strcpy(pcm->name, "ALI 5451");
1741 if (rpcm) *rpcm = pcm;
1745 #define ALI5451_SPDIF(xname, xindex, value) \
1746 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1747 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1748 .put = snd_ali5451_spdif_put, .private_value = value}
1750 static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1752 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1754 uinfo->value.integer.min = 0;
1755 uinfo->value.integer.max = 1;
1759 static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1761 unsigned long flags;
1762 ali_t *codec = kcontrol->private_data;
1763 unsigned int enable;
1765 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1767 spin_lock_irqsave(&codec->reg_lock, flags);
1768 switch(kcontrol->private_value) {
1770 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1773 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1776 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1781 ucontrol->value.integer.value[0] = enable;
1782 spin_unlock_irqrestore(&codec->reg_lock, flags);
1786 static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1788 unsigned long flags;
1789 ali_t *codec = kcontrol->private_data;
1790 unsigned int change = 0, enable = 0;
1792 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1794 spin_lock_irqsave(&codec->reg_lock, flags);
1795 switch (kcontrol->private_value) {
1797 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1798 change = change ^ enable;
1801 codec->spdif_mask |= 0x02;
1802 snd_ali_enable_spdif_out(codec);
1804 codec->spdif_mask &= ~(0x02);
1805 codec->spdif_mask &= ~(0x04);
1806 snd_ali_disable_spdif_out(codec);
1811 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1812 change = change ^ enable;
1813 if (change && (codec->spdif_mask & 0x02)) {
1815 codec->spdif_mask |= 0x04;
1816 snd_ali_enable_spdif_chnout(codec);
1818 codec->spdif_mask &= ~(0x04);
1819 snd_ali_disable_spdif_chnout(codec);
1824 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1825 change = change ^ enable;
1828 codec->spdif_mask |= 0x01;
1829 snd_ali_enable_spdif_in(codec);
1831 codec->spdif_mask &= ~(0x01);
1832 snd_ali_disable_spdif_in(codec);
1839 spin_unlock_irqrestore(&codec->reg_lock, flags);
1844 static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1845 /* spdif aplayback switch */
1846 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1847 ALI5451_SPDIF("IEC958 Output switch", 0, 0),
1848 /* spdif out to spdif channel */
1849 ALI5451_SPDIF("IEC958 Channel Output Switch", 0, 1),
1850 /* spdif in from spdif channel */
1851 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1854 static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1856 ali_t *codec = bus->private_data;
1857 codec->ac97_bus = NULL;
1860 static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1862 ali_t *codec = ac97->private_data;
1866 static int __devinit snd_ali_mixer(ali_t * codec)
1868 ac97_template_t ac97;
1871 static ac97_bus_ops_t ops = {
1872 .write = snd_ali_codec_write,
1873 .read = snd_ali_codec_read,
1876 if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1878 codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1880 memset(&ac97, 0, sizeof(ac97));
1881 ac97.private_data = codec;
1882 ac97.private_free = snd_ali_mixer_free_ac97;
1883 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97)) < 0) {
1884 snd_printk("ali mixer creating error.\n");
1887 if (codec->spdif_support) {
1888 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1889 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1890 if (err < 0) return err;
1897 static int ali_suspend(snd_card_t *card, unsigned int state)
1899 ali_t *chip = card->pm_private_data;
1907 snd_pcm_suspend_all(chip->pcm);
1908 snd_ac97_suspend(chip->ac97);
1910 spin_lock_irq(&chip->reg_lock);
1912 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1913 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
1914 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1916 // disable all IRQ bits
1917 outl(0, ALI_REG(chip, ALI_MISCINT));
1919 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1920 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1922 im->regs[i] = inl(ALI_REG(chip, i*4));
1925 for (i = 0; i < ALI_CHANNELS; i++) {
1926 outb(i, ALI_REG(chip, ALI_GC_CIR));
1927 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1928 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1931 // stop all HW channel
1932 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1934 spin_unlock_irq(&chip->reg_lock);
1935 pci_disable_device(chip->pci);
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);
1990 pci_disable_device(codec->pci);
1993 kfree(codec->image);
1999 static int snd_ali_chip_init(ali_t *codec)
2001 unsigned int legacy;
2003 struct pci_dev *pci_dev = NULL;
2005 snd_ali_printk("chip initializing ... \n");
2007 if (snd_ali_reset_5451(codec)) {
2008 snd_printk("ali_chip_init: reset 5451 error.\n");
2012 if (codec->revision == ALI_5451_V02) {
2013 pci_dev = codec->pci_m1533;
2014 pci_read_config_byte(pci_dev, 0x59, &temp);
2016 pci_write_config_byte(pci_dev, 0x59, temp);
2018 pci_dev = codec->pci_m7101;
2019 pci_read_config_byte(pci_dev, 0xb8, &temp);
2021 pci_write_config_byte(pci_dev, 0xB8, temp);
2024 pci_read_config_dword(codec->pci, 0x44, &legacy);
2025 legacy &= 0xff00ff00;
2026 legacy |= 0x000800aa;
2027 pci_write_config_dword(codec->pci, 0x44, legacy);
2029 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2030 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2031 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2032 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2033 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2035 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2036 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2037 if (codec->spdif_support) {
2038 snd_ali_enable_spdif_out(codec);
2039 codec->spdif_mask = 0x00000002;
2042 snd_ali_printk("chip initialize succeed.\n");
2047 static int __devinit snd_ali_resources(ali_t *codec)
2051 snd_ali_printk("resouces allocation ...\n");
2052 if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2054 codec->port = pci_resource_start(codec->pci, 0);
2056 if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2057 snd_printk("Unable to request irq.\n");
2060 codec->irq = codec->pci->irq;
2061 snd_ali_printk("resouces allocated.\n");
2064 static int snd_ali_dev_free(snd_device_t *device)
2066 ali_t *codec=device->device_data;
2067 snd_ali_free(codec);
2071 static int __devinit snd_ali_create(snd_card_t * card,
2072 struct pci_dev *pci,
2079 unsigned short cmdw = 0;
2080 struct pci_dev *pci_dev = NULL;
2081 static snd_device_ops_t ops = {
2082 (snd_dev_free_t *)snd_ali_dev_free,
2089 snd_ali_printk("creating ...\n");
2091 /* enable PCI device */
2092 if ((err = pci_enable_device(pci)) < 0)
2094 /* check, if we can restrict PCI DMA transfers to 31 bits */
2095 if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
2096 pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2097 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2098 pci_disable_device(pci);
2102 if ((codec = kcalloc(1, sizeof(*codec), GFP_KERNEL)) == NULL) {
2103 pci_disable_device(pci);
2107 spin_lock_init(&codec->reg_lock);
2108 spin_lock_init(&codec->voice_alloc);
2113 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2114 codec->spdif_support = spdif_support;
2116 if (pcm_streams < 1)
2118 if (pcm_streams > 32)
2121 pci_set_master(pci);
2122 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2123 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2124 cmdw |= PCI_COMMAND_IO;
2125 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2127 pci_set_master(pci);
2129 if (snd_ali_resources(codec)) {
2130 snd_ali_free(codec);
2134 synchronize_irq(pci->irq);
2136 codec->synth.chmap = 0;
2137 codec->synth.chcnt = 0;
2138 codec->spdif_mask = 0;
2139 codec->synth.synthcount = 0;
2141 if (codec->revision == ALI_5451_V02)
2142 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2144 codec->chregs.regs.ac97read = ALI_AC97_READ;
2145 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2147 codec->chregs.regs.start = ALI_START;
2148 codec->chregs.regs.stop = ALI_STOP;
2149 codec->chregs.regs.aint = ALI_AINT;
2150 codec->chregs.regs.ainten = ALI_AINTEN;
2152 codec->chregs.data.start = 0x00;
2153 codec->chregs.data.stop = 0x00;
2154 codec->chregs.data.aint = 0x00;
2155 codec->chregs.data.ainten = 0x00;
2157 /* M1533: southbridge */
2158 pci_dev = pci_find_device(0x10b9, 0x1533, NULL);
2159 codec->pci_m1533 = pci_dev;
2160 if (! codec->pci_m1533) {
2161 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2162 snd_ali_free(codec);
2165 /* M7101: power management */
2166 pci_dev = pci_find_device(0x10b9, 0x7101, NULL);
2167 codec->pci_m7101 = pci_dev;
2168 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2169 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2170 snd_ali_free(codec);
2174 snd_ali_printk("snd_device_new is called.\n");
2175 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2176 snd_ali_free(codec);
2180 /* initialise synth voices*/
2181 for (i = 0; i < ALI_CHANNELS; i++ ) {
2182 codec->synth.voices[i].number = i;
2185 if ((err = snd_ali_chip_init(codec)) < 0) {
2186 snd_printk("ali create: chip init error.\n");
2191 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2193 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2195 snd_card_set_pm_callback(card, ali_suspend, ali_resume, codec);
2198 snd_ali_enable_address_interrupt(codec);
2199 codec->hw_initialized = 1;
2202 snd_ali_printk("created.\n");
2206 static int __devinit snd_ali_probe(struct pci_dev *pci,
2207 const struct pci_device_id *pci_id)
2214 snd_ali_printk("probe ...\n");
2216 if (dev >= SNDRV_CARDS)
2223 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2227 if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2228 snd_card_free(card);
2232 snd_ali_printk("mixer building ...\n");
2233 if ((err = snd_ali_mixer(codec)) < 0) {
2234 snd_card_free(card);
2238 snd_ali_printk("pcm building ...\n");
2239 if ((err = snd_ali_pcm(codec, 0, NULL)) < 0) {
2240 snd_card_free(card);
2244 strcpy(card->driver, "ALI5451");
2245 strcpy(card->shortname, "ALI 5451");
2247 sprintf(card->longname, "%s at 0x%lx, irq %li",
2248 card->shortname, codec->port, codec->irq);
2250 snd_ali_printk("register card.\n");
2251 if ((err = snd_card_register(card)) < 0) {
2252 snd_card_free(card);
2255 pci_set_drvdata(pci, card);
2260 static void __devexit snd_ali_remove(struct pci_dev *pci)
2262 snd_card_free(pci_get_drvdata(pci));
2263 pci_set_drvdata(pci, NULL);
2266 static struct pci_driver driver = {
2268 .id_table = snd_ali_ids,
2269 .probe = snd_ali_probe,
2270 .remove = __devexit_p(snd_ali_remove),
2271 SND_PCI_PM_CALLBACKS
2274 static int __init alsa_card_ali_init(void)
2276 return pci_module_init(&driver);
2279 static void __exit alsa_card_ali_exit(void)
2281 pci_unregister_driver(&driver);
2284 module_init(alsa_card_ali_init)
2285 module_exit(alsa_card_ali_exit)