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
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/init.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/dma-mapping.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_DEFAULT_IDX1; /* Index */
49 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
50 static int pcm_channels = 32;
53 module_param(index, int, 0444);
54 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
55 module_param(id, charp, 0444);
56 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
57 module_param(pcm_channels, int, 0444);
58 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
59 module_param(spdif, bool, 0444);
60 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
62 /* just for backward compatibility */
64 module_param(enable, bool, 0444);
68 * Debug part definitions
71 /* #define ALI_DEBUG */
74 #define snd_ali_printk(format, args...) printk(KERN_DEBUG format, ##args);
76 #define snd_ali_printk(format, args...)
80 * Constants definition
83 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
86 #define ALI_CHANNELS 32
88 #define ALI_PCM_IN_CHANNEL 31
89 #define ALI_SPDIF_IN_CHANNEL 19
90 #define ALI_SPDIF_OUT_CHANNEL 15
91 #define ALI_CENTER_CHANNEL 24
92 #define ALI_LEF_CHANNEL 23
93 #define ALI_SURR_LEFT_CHANNEL 26
94 #define ALI_SURR_RIGHT_CHANNEL 25
95 #define ALI_MODEM_IN_CHANNEL 21
96 #define ALI_MODEM_OUT_CHANNEL 20
98 #define SNDRV_ALI_VOICE_TYPE_PCM 01
99 #define SNDRV_ALI_VOICE_TYPE_OTH 02
101 #define ALI_5451_V02 0x02
107 #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
108 #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
109 #define ALI_LEGACY_DMAR11 0x0b /* MOD */
110 #define ALI_LEGACY_DMAR15 0x0f /* MMR */
111 #define ALI_MPUR0 0x20
112 #define ALI_MPUR1 0x21
113 #define ALI_MPUR2 0x22
114 #define ALI_MPUR3 0x23
116 #define ALI_AC97_WRITE 0x40
117 #define ALI_AC97_READ 0x44
119 #define ALI_SCTRL 0x48
120 #define ALI_SPDIF_OUT_ENABLE 0x20
121 #define ALI_SCTRL_LINE_IN2 (1 << 9)
122 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
123 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
124 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
125 #define ALI_SCTRL_CODEC1_READY (1 << 24)
126 #define ALI_SCTRL_CODEC2_READY (1 << 25)
127 #define ALI_AC97_GPIO 0x4c
128 #define ALI_AC97_GPIO_ENABLE 0x8000
129 #define ALI_AC97_GPIO_DATA_SHIFT 16
130 #define ALI_SPDIF_CS 0x70
131 #define ALI_SPDIF_CTRL 0x74
132 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
133 #define ALI_SPDIF_IN_CH_STATUS 0x40
134 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
135 #define ALI_START 0x80
136 #define ALI_STOP 0x84
137 #define ALI_CSPF 0x90
138 #define ALI_AINT 0x98
139 #define ALI_GC_CIR 0xa0
140 #define ENDLP_IE 0x00001000
141 #define MIDLP_IE 0x00002000
142 #define ALI_AINTEN 0xa4
143 #define ALI_VOLUME 0xa8
144 #define ALI_SBDELTA_DELTA_R 0xac
145 #define ALI_MISCINT 0xb0
146 #define ADDRESS_IRQ 0x00000020
147 #define TARGET_REACHED 0x00008000
148 #define MIXER_OVERFLOW 0x00000800
149 #define MIXER_UNDERFLOW 0x00000400
150 #define GPIO_IRQ 0x01000000
151 #define ALI_SBBL_SBCL 0xc0
152 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
153 #define ALI_STIMER 0xc8
154 #define ALI_GLOBAL_CONTROL 0xd4
155 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
156 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
157 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
158 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
159 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
161 #define ALI_CSO_ALPHA_FMS 0xe0
163 #define ALI_ESO_DELTA 0xe8
164 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
165 #define ALI_EBUF1 0xf4
166 #define ALI_EBUF2 0xf8
168 #define ALI_REG(codec, x) ((codec)->port + x)
174 struct snd_ali_voice;
176 struct snd_ali_channel_control {
185 /* register addresses */
191 unsigned int ac97read;
192 unsigned int ac97write;
197 struct snd_ali_voice {
207 struct snd_ali *codec;
208 struct snd_pcm_substream *substream;
209 struct snd_ali_voice *extra;
211 int eso; /* final ESO value for channel */
212 int count; /* runtime->period_size */
217 void (*private_free)(void *private_data);
223 struct snd_ali_voice voices[ALI_CHANNELS];
225 unsigned int chcnt; /* num of opened channels */
226 unsigned int chmap; /* bitmap for opened channels */
227 unsigned int synthcount;
232 #define ALI_GLOBAL_REGS 56
233 #define ALI_CHANNEL_REGS 8
234 struct snd_ali_image {
235 u32 regs[ALI_GLOBAL_REGS];
236 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
243 unsigned char revision;
245 unsigned int hw_initialized :1;
246 unsigned int spdif_support :1;
249 struct pci_dev *pci_m1533;
250 struct pci_dev *pci_m7101;
252 struct snd_card *card;
253 struct snd_pcm *pcm[MAX_CODECS];
254 struct snd_alidev synth;
255 struct snd_ali_channel_control chregs;
258 unsigned int spdif_mask;
260 unsigned int spurious_irq_count;
261 unsigned int spurious_irq_max_delta;
263 unsigned int num_of_codecs;
265 struct snd_ac97_bus *ac97_bus;
266 struct snd_ac97 *ac97[MAX_CODECS];
267 unsigned short ac97_ext_id;
268 unsigned short ac97_ext_status;
271 spinlock_t voice_alloc;
273 #ifdef CONFIG_PM_SLEEP
274 struct snd_ali_image *image;
278 static DEFINE_PCI_DEVICE_TABLE(snd_ali_ids) = {
279 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
282 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
284 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
285 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
286 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
293 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
296 return (unsigned int)inl(ALI_REG(codec, port));
299 static inline void snd_ali_5451_poke(struct snd_ali *codec,
303 outl((unsigned int)val, ALI_REG(codec, port));
306 static int snd_ali_codec_ready(struct snd_ali *codec,
309 unsigned long end_time;
312 end_time = jiffies + msecs_to_jiffies(250);
315 res = snd_ali_5451_peek(codec,port);
318 if (!time_after_eq(end_time, jiffies))
320 schedule_timeout_uninterruptible(1);
323 snd_ali_5451_poke(codec, port, res & ~0x8000);
324 snd_printdd("ali_codec_ready: codec is not ready.\n ");
328 static int snd_ali_stimer_ready(struct snd_ali *codec)
330 unsigned long end_time;
331 unsigned long dwChk1,dwChk2;
333 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
334 end_time = jiffies + msecs_to_jiffies(250);
337 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
338 if (dwChk2 != dwChk1)
340 if (!time_after_eq(end_time, jiffies))
342 schedule_timeout_uninterruptible(1);
345 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
349 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
357 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
361 port = codec->chregs.regs.ac97write;
363 if (snd_ali_codec_ready(codec, port) < 0)
365 if (snd_ali_stimer_ready(codec) < 0)
368 dwVal = (unsigned int) (reg & 0xff);
369 dwVal |= 0x8000 | (val << 16);
372 if (codec->revision == ALI_5451_V02)
375 snd_ali_5451_poke(codec, port, dwVal);
380 static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
388 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
392 port = codec->chregs.regs.ac97read;
394 if (snd_ali_codec_ready(codec, port) < 0)
396 if (snd_ali_stimer_ready(codec) < 0)
399 dwVal = (unsigned int) (reg & 0xff);
400 dwVal |= 0x8000; /* bit 15*/
404 snd_ali_5451_poke(codec, port, dwVal);
406 if (snd_ali_stimer_ready(codec) < 0)
408 if (snd_ali_codec_ready(codec, port) < 0)
411 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
414 static void snd_ali_codec_write(struct snd_ac97 *ac97,
418 struct snd_ali *codec = ac97->private_data;
420 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
421 if (reg == AC97_GPIO_STATUS) {
422 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
423 ALI_REG(codec, ALI_AC97_GPIO));
426 snd_ali_codec_poke(codec, ac97->num, reg, val);
431 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
434 struct snd_ali *codec = ac97->private_data;
436 snd_ali_printk("codec_read reg=%xh.\n", reg);
437 return snd_ali_codec_peek(codec, ac97->num, reg);
444 static int snd_ali_reset_5451(struct snd_ali *codec)
446 struct pci_dev *pci_dev;
447 unsigned short wCount, wReg;
450 pci_dev = codec->pci_m1533;
452 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
453 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
455 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
456 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
460 pci_dev = codec->pci;
461 pci_read_config_dword(pci_dev, 0x44, &dwVal);
462 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
464 pci_read_config_dword(pci_dev, 0x44, &dwVal);
465 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
470 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
471 if ((wReg & 0x000f) == 0x000f)
476 /* non-fatal if you have a non PM capable codec */
477 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
482 * ALI 5451 Controller
485 static void snd_ali_enable_special_channel(struct snd_ali *codec,
486 unsigned int channel)
490 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
491 dwVal |= 1 << (channel & 0x0000001f);
492 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
495 static void snd_ali_disable_special_channel(struct snd_ali *codec,
496 unsigned int channel)
500 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
501 dwVal &= ~(1 << (channel & 0x0000001f));
502 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
505 static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
509 gc = inl(ALI_REG(codec, ALI_GC_CIR));
512 outl( gc, ALI_REG(codec, ALI_GC_CIR));
515 static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
519 gc = inl(ALI_REG(codec, ALI_GC_CIR));
522 outl(gc, ALI_REG(codec, ALI_GC_CIR));
525 static void snd_ali_disable_voice_irq(struct snd_ali *codec,
526 unsigned int channel)
529 struct snd_ali_channel_control *pchregs = &(codec->chregs);
531 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
533 mask = 1 << (channel & 0x1f);
534 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
535 pchregs->data.ainten &= ~mask;
536 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
539 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
541 unsigned int idx = channel & 0x1f;
543 if (codec->synth.chcnt >= ALI_CHANNELS){
545 "ali_alloc_pcm_channel: no free channels.\n");
549 if (!(codec->synth.chmap & (1 << idx))) {
550 codec->synth.chmap |= 1 << idx;
551 codec->synth.chcnt++;
552 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
558 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
563 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
567 if (codec->spdif_support &&
568 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
569 ALI_SPDIF_IN_SUPPORT))
570 idx = ALI_SPDIF_IN_CHANNEL;
572 idx = ALI_PCM_IN_CHANNEL;
574 result = snd_ali_alloc_pcm_channel(codec, idx);
578 snd_printk(KERN_ERR "ali_find_free_channel: "
579 "record channel is busy now.\n");
585 if (codec->spdif_support &&
586 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
587 ALI_SPDIF_OUT_CH_ENABLE)) {
588 idx = ALI_SPDIF_OUT_CHANNEL;
589 result = snd_ali_alloc_pcm_channel(codec, idx);
593 snd_printk(KERN_ERR "ali_find_free_channel: "
594 "S/PDIF out channel is in busy now.\n");
597 for (idx = 0; idx < ALI_CHANNELS; idx++) {
598 result = snd_ali_alloc_pcm_channel(codec, idx);
602 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
606 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
608 unsigned int idx = channel & 0x0000001f;
610 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
612 if (channel < 0 || channel >= ALI_CHANNELS)
615 if (!(codec->synth.chmap & (1 << idx))) {
616 snd_printk(KERN_ERR "ali_free_channel_pcm: "
617 "channel %d is not in use.\n", channel);
620 codec->synth.chmap &= ~(1 << idx);
621 codec->synth.chcnt--;
625 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
627 unsigned int mask = 1 << (channel & 0x1f);
629 snd_ali_printk("stop_voice: channel=%d\n",channel);
630 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
637 static void snd_ali_delay(struct snd_ali *codec,int interval)
639 unsigned long begintimer,currenttimer;
641 begintimer = inl(ALI_REG(codec, ALI_STIMER));
642 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
644 while (currenttimer < begintimer + interval) {
645 if (snd_ali_stimer_ready(codec) < 0)
647 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
652 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
658 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
660 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
662 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
664 snd_ali_delay(codec, 6);
665 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
670 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
674 for (count = 0; count <= 50000; count++) {
675 snd_ali_delay(codec, 6);
676 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
685 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
689 if (R2 >= 0x0b && R2 <= 0x0e) {
690 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
692 wval |= (0x09 << 8) | 0x05;
693 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
695 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
696 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
697 } else if (R2 == 0x12) {
698 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
700 wval |= (0x0e << 8) | 0x08;
701 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
703 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
704 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
708 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
713 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
716 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
718 snd_ali_detect_spdif_rate(codec);
720 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
724 case 0: dwRate = 44100; break;
725 case 1: dwRate = 48000; break;
726 case 2: dwRate = 32000; break;
727 default: dwRate = 0; break;
733 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
737 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
738 dwVal |= ALI_SPDIF_IN_SUPPORT;
739 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
741 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
743 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
745 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
748 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
752 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
753 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
754 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
756 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
760 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
766 case 32000: dwRate = 0x300; break;
767 case 48000: dwRate = 0x200; break;
768 default: dwRate = 0; break;
771 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
772 bVal &= (unsigned char)(~(1<<6));
774 bVal |= 0x80; /* select right */
775 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
776 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
778 bVal &= ~0x80; /* select left */
779 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
780 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
783 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
787 struct pci_dev *pci_dev;
789 pci_dev = codec->pci_m1533;
792 pci_read_config_byte(pci_dev, 0x61, &bVal);
794 pci_write_config_byte(pci_dev, 0x61, bVal);
795 pci_read_config_byte(pci_dev, 0x7d, &bVal);
797 pci_write_config_byte(pci_dev, 0x7d, bVal);
799 pci_read_config_byte(pci_dev, 0x7e, &bVal);
802 pci_write_config_byte(pci_dev, 0x7e, bVal);
804 bVal = inb(ALI_REG(codec, ALI_SCTRL));
805 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
807 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
808 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
810 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
811 wVal |= ALI_SPDIF_OUT_SEL_PCM;
812 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
813 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
816 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
820 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
821 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
822 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
824 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
825 if (flag & ALI_SPDIF_OUT_NON_PCM)
829 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
831 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
834 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
838 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
839 wVal |= ALI_SPDIF_OUT_SEL_PCM;
840 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
842 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
845 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
849 bVal = inb(ALI_REG(codec, ALI_SCTRL));
850 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
852 snd_ali_disable_spdif_chnout(codec);
855 static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
857 struct snd_ali_voice *pvoice;
858 struct snd_ali_channel_control *pchregs;
859 unsigned int old, mask;
861 unsigned int temp, cspf;
864 pchregs = &(codec->chregs);
866 /* check if interrupt occurred for channel */
867 old = pchregs->data.aint;
868 mask = 1U << (channel & 0x1f);
873 pvoice = &codec->synth.voices[channel];
876 spin_lock(&codec->reg_lock);
878 if (pvoice->pcm && pvoice->substream) {
881 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
882 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
883 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
885 if (pvoice->running) {
886 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
888 spin_unlock(&codec->reg_lock);
889 snd_pcm_period_elapsed(pvoice->substream);
890 spin_lock(&codec->reg_lock);
892 snd_ali_stop_voice(codec, channel);
893 snd_ali_disable_voice_irq(codec, channel);
895 } else if (codec->synth.voices[channel].synth) {
896 /* synth interrupt */
897 } else if (codec->synth.voices[channel].midi) {
900 /* unknown interrupt */
901 snd_ali_stop_voice(codec, channel);
902 snd_ali_disable_voice_irq(codec, channel);
904 spin_unlock(&codec->reg_lock);
905 outl(mask,ALI_REG(codec,pchregs->regs.aint));
906 pchregs->data.aint = old & (~mask);
909 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
911 struct snd_ali *codec = dev_id;
913 unsigned int audio_int;
914 struct snd_ali_channel_control *pchregs;
916 if (codec == NULL || !codec->hw_initialized)
919 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
923 pchregs = &(codec->chregs);
924 if (audio_int & ADDRESS_IRQ) {
925 /* get interrupt status for all channels */
926 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
927 for (channel = 0; channel < ALI_CHANNELS; channel++)
928 snd_ali_update_ptr(codec, channel);
930 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
931 ALI_REG(codec, ALI_MISCINT));
937 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
938 int type, int rec, int channel)
940 struct snd_ali_voice *pvoice;
943 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
945 spin_lock_irq(&codec->voice_alloc);
946 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
947 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
948 snd_ali_find_free_channel(codec,rec);
950 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
951 spin_unlock_irq(&codec->voice_alloc);
954 pvoice = &(codec->synth.voices[idx]);
955 pvoice->codec = codec;
959 spin_unlock_irq(&codec->voice_alloc);
962 spin_unlock_irq(&codec->voice_alloc);
967 static void snd_ali_free_voice(struct snd_ali * codec,
968 struct snd_ali_voice *pvoice)
970 void (*private_free)(void *);
973 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
976 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
977 spin_lock_irq(&codec->voice_alloc);
978 private_free = pvoice->private_free;
979 private_data = pvoice->private_data;
980 pvoice->private_free = NULL;
981 pvoice->private_data = NULL;
983 snd_ali_free_channel_pcm(codec, pvoice->number);
984 pvoice->use = pvoice->pcm = pvoice->synth = 0;
985 pvoice->substream = NULL;
986 spin_unlock_irq(&codec->voice_alloc);
988 private_free(private_data);
992 static void snd_ali_clear_voices(struct snd_ali *codec,
998 for (i = v_min; i <= v_max; i++) {
999 snd_ali_stop_voice(codec, i);
1000 snd_ali_disable_voice_irq(codec, i);
1004 static void snd_ali_write_voice_regs(struct snd_ali *codec,
1005 unsigned int Channel,
1010 unsigned int ALPHA_FMS,
1017 unsigned int ctlcmds[4];
1019 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
1021 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1023 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1024 ctlcmds[3] = (GVSEL << 31) |
1025 ((PAN & 0x0000007f) << 24) |
1026 ((VOL & 0x000000ff) << 16) |
1027 ((CTRL & 0x0000000f) << 12) |
1030 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1032 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1033 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1034 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1035 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1037 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1038 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1041 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1053 else if (rate == 8000)
1055 else if (rate == 48000)
1058 delta = ((48000 << 12) / rate) & 0x0000ffff;
1062 else if (rate == 8000)
1064 else if (rate == 48000)
1067 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1073 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1076 struct snd_pcm_runtime *runtime = substream->runtime;
1079 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1082 if (snd_pcm_format_width(runtime->format) == 16)
1083 CTRL |= 0x00000008; /* 16-bit data */
1084 if (!snd_pcm_format_unsigned(runtime->format))
1085 CTRL |= 0x00000002; /* signed data */
1086 if (runtime->channels > 1)
1087 CTRL |= 0x00000004; /* stereo data */
1095 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1099 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1100 struct snd_pcm_substream *s;
1101 unsigned int what, whati, capture_flag;
1102 struct snd_ali_voice *pvoice, *evoice;
1107 case SNDRV_PCM_TRIGGER_START:
1108 case SNDRV_PCM_TRIGGER_RESUME:
1111 case SNDRV_PCM_TRIGGER_STOP:
1112 case SNDRV_PCM_TRIGGER_SUSPEND:
1119 what = whati = capture_flag = 0;
1120 snd_pcm_group_for_each_entry(s, substream) {
1121 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1122 pvoice = s->runtime->private_data;
1123 evoice = pvoice->extra;
1124 what |= 1 << (pvoice->number & 0x1f);
1126 whati |= 1 << (pvoice->number & 0x1f);
1128 whati |= 1 << (evoice->number & 0x1f);
1129 what |= 1 << (evoice->number & 0x1f);
1132 pvoice->running = 1;
1134 evoice->running = 1;
1136 pvoice->running = 0;
1138 evoice->running = 0;
1140 snd_pcm_trigger_done(s, substream);
1145 spin_lock(&codec->reg_lock);
1147 outl(what, ALI_REG(codec, ALI_STOP));
1148 val = inl(ALI_REG(codec, ALI_AINTEN));
1153 outl(val, ALI_REG(codec, ALI_AINTEN));
1155 outl(what, ALI_REG(codec, ALI_START));
1156 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
1157 spin_unlock(&codec->reg_lock);
1162 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1163 struct snd_pcm_hw_params *hw_params)
1165 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1166 struct snd_pcm_runtime *runtime = substream->runtime;
1167 struct snd_ali_voice *pvoice = runtime->private_data;
1168 struct snd_ali_voice *evoice = pvoice->extra;
1171 err = snd_pcm_lib_malloc_pages(substream,
1172 params_buffer_bytes(hw_params));
1176 /* voice management */
1178 if (params_buffer_size(hw_params) / 2 !=
1179 params_period_size(hw_params)) {
1181 evoice = snd_ali_alloc_voice(codec,
1182 SNDRV_ALI_VOICE_TYPE_PCM,
1186 pvoice->extra = evoice;
1187 evoice->substream = substream;
1191 snd_ali_free_voice(codec, evoice);
1192 pvoice->extra = evoice = NULL;
1199 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1201 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1202 struct snd_pcm_runtime *runtime = substream->runtime;
1203 struct snd_ali_voice *pvoice = runtime->private_data;
1204 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1206 snd_pcm_lib_free_pages(substream);
1208 snd_ali_free_voice(codec, evoice);
1209 pvoice->extra = NULL;
1214 static int snd_ali_hw_params(struct snd_pcm_substream *substream,
1215 struct snd_pcm_hw_params *hw_params)
1217 return snd_pcm_lib_malloc_pages(substream,
1218 params_buffer_bytes(hw_params));
1221 static int snd_ali_hw_free(struct snd_pcm_substream *substream)
1223 return snd_pcm_lib_free_pages(substream);
1226 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1228 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1229 struct snd_pcm_runtime *runtime = substream->runtime;
1230 struct snd_ali_voice *pvoice = runtime->private_data;
1231 struct snd_ali_voice *evoice = pvoice->extra;
1242 snd_ali_printk("playback_prepare ...\n");
1244 spin_lock_irq(&codec->reg_lock);
1246 /* set Delta (rate) value */
1247 Delta = snd_ali_convert_rate(runtime->rate, 0);
1249 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1250 pvoice->number == ALI_PCM_IN_CHANNEL)
1251 snd_ali_disable_special_channel(codec, pvoice->number);
1252 else if (codec->spdif_support &&
1253 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1254 ALI_SPDIF_OUT_CH_ENABLE)
1255 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1256 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1260 /* set Loop Back Address */
1261 LBA = runtime->dma_addr;
1263 /* set interrupt count size */
1264 pvoice->count = runtime->period_size;
1266 /* set target ESO for channel */
1267 pvoice->eso = runtime->buffer_size;
1269 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1270 pvoice->eso, pvoice->count);
1272 /* set ESO to capture first MIDLP interrupt */
1273 ESO = pvoice->eso -1;
1275 CTRL = snd_ali_control_mode(substream);
1281 snd_ali_printk("playback_prepare:\n");
1282 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1283 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1284 snd_ali_write_voice_regs(codec,
1297 evoice->count = pvoice->count;
1298 evoice->eso = pvoice->count << 1;
1299 ESO = evoice->eso - 1;
1300 snd_ali_write_voice_regs(codec,
1313 spin_unlock_irq(&codec->reg_lock);
1318 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1320 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1321 struct snd_pcm_runtime *runtime = substream->runtime;
1322 struct snd_ali_voice *pvoice = runtime->private_data;
1333 spin_lock_irq(&codec->reg_lock);
1335 snd_ali_printk("ali_prepare...\n");
1337 snd_ali_enable_special_channel(codec,pvoice->number);
1339 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1340 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1341 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1343 /* Prepare capture intr channel */
1344 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1348 spin_unlock_irq(&codec->reg_lock);
1349 if (codec->revision != ALI_5451_V02)
1352 rate = snd_ali_get_spdif_in_rate(codec);
1354 snd_printk(KERN_WARNING "ali_capture_preapre: "
1355 "spdif rate detect err!\n");
1358 spin_lock_irq(&codec->reg_lock);
1359 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1360 if (bValue & 0x10) {
1361 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1362 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
1366 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1369 /* set target ESO for channel */
1370 pvoice->eso = runtime->buffer_size;
1372 /* set interrupt count size */
1373 pvoice->count = runtime->period_size;
1375 /* set Loop Back Address */
1376 LBA = runtime->dma_addr;
1378 /* set ESO to capture first MIDLP interrupt */
1379 ESO = pvoice->eso - 1;
1380 CTRL = snd_ali_control_mode(substream);
1386 snd_ali_write_voice_regs( codec,
1399 spin_unlock_irq(&codec->reg_lock);
1405 static snd_pcm_uframes_t
1406 snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1408 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1409 struct snd_pcm_runtime *runtime = substream->runtime;
1410 struct snd_ali_voice *pvoice = runtime->private_data;
1413 spin_lock(&codec->reg_lock);
1414 if (!pvoice->running) {
1415 spin_unlock(&codec->reg_lock);
1418 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1419 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1420 spin_unlock(&codec->reg_lock);
1421 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1427 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1429 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1430 struct snd_pcm_runtime *runtime = substream->runtime;
1431 struct snd_ali_voice *pvoice = runtime->private_data;
1434 spin_lock(&codec->reg_lock);
1435 if (!pvoice->running) {
1436 spin_unlock(&codec->reg_lock);
1439 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1440 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1441 spin_unlock(&codec->reg_lock);
1446 static struct snd_pcm_hardware snd_ali_playback =
1448 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1449 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1450 SNDRV_PCM_INFO_MMAP_VALID |
1451 SNDRV_PCM_INFO_RESUME |
1452 SNDRV_PCM_INFO_SYNC_START),
1453 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1454 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1455 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1460 .buffer_bytes_max = (256*1024),
1461 .period_bytes_min = 64,
1462 .period_bytes_max = (256*1024),
1464 .periods_max = 1024,
1469 * Capture support device description
1472 static struct snd_pcm_hardware snd_ali_capture =
1474 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1475 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1476 SNDRV_PCM_INFO_MMAP_VALID |
1477 SNDRV_PCM_INFO_RESUME |
1478 SNDRV_PCM_INFO_SYNC_START),
1479 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1480 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1481 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1486 .buffer_bytes_max = (128*1024),
1487 .period_bytes_min = 64,
1488 .period_bytes_max = (128*1024),
1490 .periods_max = 1024,
1494 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1496 struct snd_ali_voice *pvoice = runtime->private_data;
1497 struct snd_ali *codec;
1500 codec = pvoice->codec;
1501 snd_ali_free_voice(pvoice->codec, pvoice);
1505 static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1506 int channel, struct snd_pcm_hardware *phw)
1508 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1509 struct snd_pcm_runtime *runtime = substream->runtime;
1510 struct snd_ali_voice *pvoice;
1512 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1517 pvoice->substream = substream;
1518 runtime->private_data = pvoice;
1519 runtime->private_free = snd_ali_pcm_free_substream;
1522 snd_pcm_set_sync(substream);
1523 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1528 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1530 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1533 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1535 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1538 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1543 static int snd_ali_close(struct snd_pcm_substream *substream)
1545 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1546 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1548 snd_ali_disable_special_channel(codec,pvoice->number);
1553 static struct snd_pcm_ops snd_ali_playback_ops = {
1554 .open = snd_ali_playback_open,
1555 .close = snd_ali_playback_close,
1556 .ioctl = snd_pcm_lib_ioctl,
1557 .hw_params = snd_ali_playback_hw_params,
1558 .hw_free = snd_ali_playback_hw_free,
1559 .prepare = snd_ali_playback_prepare,
1560 .trigger = snd_ali_trigger,
1561 .pointer = snd_ali_playback_pointer,
1564 static struct snd_pcm_ops snd_ali_capture_ops = {
1565 .open = snd_ali_capture_open,
1566 .close = snd_ali_close,
1567 .ioctl = snd_pcm_lib_ioctl,
1568 .hw_params = snd_ali_hw_params,
1569 .hw_free = snd_ali_hw_free,
1570 .prepare = snd_ali_prepare,
1571 .trigger = snd_ali_trigger,
1572 .pointer = snd_ali_pointer,
1579 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1580 struct snd_pcm_hw_params *hw_params)
1582 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1583 unsigned int modem_num = chip->num_of_codecs - 1;
1584 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1585 params_rate(hw_params));
1586 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1587 return snd_ali_hw_params(substream, hw_params);
1590 static struct snd_pcm_hardware snd_ali_modem =
1592 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1593 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1594 SNDRV_PCM_INFO_MMAP_VALID |
1595 SNDRV_PCM_INFO_RESUME |
1596 SNDRV_PCM_INFO_SYNC_START),
1597 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1598 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1599 SNDRV_PCM_RATE_16000),
1604 .buffer_bytes_max = (256*1024),
1605 .period_bytes_min = 64,
1606 .period_bytes_max = (256*1024),
1608 .periods_max = 1024,
1612 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1615 static unsigned int rates[] = {8000, 9600, 12000, 16000};
1616 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1617 .count = ARRAY_SIZE(rates),
1621 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1625 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1626 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1629 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1631 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1634 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1636 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1639 static struct snd_pcm_ops snd_ali_modem_playback_ops = {
1640 .open = snd_ali_modem_playback_open,
1641 .close = snd_ali_close,
1642 .ioctl = snd_pcm_lib_ioctl,
1643 .hw_params = snd_ali_modem_hw_params,
1644 .hw_free = snd_ali_hw_free,
1645 .prepare = snd_ali_prepare,
1646 .trigger = snd_ali_trigger,
1647 .pointer = snd_ali_pointer,
1650 static struct snd_pcm_ops snd_ali_modem_capture_ops = {
1651 .open = snd_ali_modem_capture_open,
1652 .close = snd_ali_close,
1653 .ioctl = snd_pcm_lib_ioctl,
1654 .hw_params = snd_ali_modem_hw_params,
1655 .hw_free = snd_ali_hw_free,
1656 .prepare = snd_ali_prepare,
1657 .trigger = snd_ali_trigger,
1658 .pointer = snd_ali_pointer,
1662 struct ali_pcm_description {
1664 unsigned int playback_num;
1665 unsigned int capture_num;
1666 struct snd_pcm_ops *playback_ops;
1667 struct snd_pcm_ops *capture_ops;
1668 unsigned short class;
1672 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1674 struct snd_ali *codec = pcm->private_data;
1675 codec->pcm[pcm->device] = NULL;
1679 static int snd_ali_pcm(struct snd_ali *codec, int device,
1680 struct ali_pcm_description *desc)
1682 struct snd_pcm *pcm;
1685 err = snd_pcm_new(codec->card, desc->name, device,
1686 desc->playback_num, desc->capture_num, &pcm);
1688 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
1691 pcm->private_data = codec;
1692 pcm->private_free = snd_ali_pcm_free;
1693 if (desc->playback_ops)
1694 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1695 desc->playback_ops);
1696 if (desc->capture_ops)
1697 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1700 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1701 snd_dma_pci_data(codec->pci),
1704 pcm->info_flags = 0;
1705 pcm->dev_class = desc->class;
1706 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1707 strcpy(pcm->name, desc->name);
1708 codec->pcm[0] = pcm;
1712 static struct ali_pcm_description ali_pcms[] = {
1713 { .name = "ALI 5451",
1714 .playback_num = ALI_CHANNELS,
1716 .playback_ops = &snd_ali_playback_ops,
1717 .capture_ops = &snd_ali_capture_ops
1719 { .name = "ALI 5451 modem",
1722 .playback_ops = &snd_ali_modem_playback_ops,
1723 .capture_ops = &snd_ali_modem_capture_ops,
1724 .class = SNDRV_PCM_CLASS_MODEM
1728 static int snd_ali_build_pcms(struct snd_ali *codec)
1731 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1732 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1740 #define ALI5451_SPDIF(xname, xindex, value) \
1741 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1742 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1743 .put = snd_ali5451_spdif_put, .private_value = value}
1745 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
1747 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol)
1750 struct snd_ali *codec = kcontrol->private_data;
1751 unsigned int spdif_enable;
1753 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1755 spin_lock_irq(&codec->reg_lock);
1756 switch (kcontrol->private_value) {
1758 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1761 spdif_enable = ((codec->spdif_mask & 0x02) &&
1762 (codec->spdif_mask & 0x04)) ? 1 : 0;
1765 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1770 ucontrol->value.integer.value[0] = spdif_enable;
1771 spin_unlock_irq(&codec->reg_lock);
1775 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1776 struct snd_ctl_elem_value *ucontrol)
1778 struct snd_ali *codec = kcontrol->private_data;
1779 unsigned int change = 0, spdif_enable = 0;
1781 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1783 spin_lock_irq(&codec->reg_lock);
1784 switch (kcontrol->private_value) {
1786 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1787 change = change ^ spdif_enable;
1790 codec->spdif_mask |= 0x02;
1791 snd_ali_enable_spdif_out(codec);
1793 codec->spdif_mask &= ~(0x02);
1794 codec->spdif_mask &= ~(0x04);
1795 snd_ali_disable_spdif_out(codec);
1800 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1801 change = change ^ spdif_enable;
1802 if (change && (codec->spdif_mask & 0x02)) {
1804 codec->spdif_mask |= 0x04;
1805 snd_ali_enable_spdif_chnout(codec);
1807 codec->spdif_mask &= ~(0x04);
1808 snd_ali_disable_spdif_chnout(codec);
1813 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1814 change = change ^ spdif_enable;
1817 codec->spdif_mask |= 0x01;
1818 snd_ali_enable_spdif_in(codec);
1820 codec->spdif_mask &= ~(0x01);
1821 snd_ali_disable_spdif_in(codec);
1828 spin_unlock_irq(&codec->reg_lock);
1833 static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
1834 /* spdif aplayback switch */
1835 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1836 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1837 /* spdif out to spdif channel */
1838 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1839 /* spdif in from spdif channel */
1840 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1843 static int snd_ali_mixer(struct snd_ali *codec)
1845 struct snd_ac97_template ac97;
1848 static struct snd_ac97_bus_ops ops = {
1849 .write = snd_ali_codec_write,
1850 .read = snd_ali_codec_read,
1853 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1857 memset(&ac97, 0, sizeof(ac97));
1858 ac97.private_data = codec;
1860 for (i = 0; i < codec->num_of_codecs; i++) {
1862 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1865 "ali mixer %d creating error.\n", i);
1868 codec->num_of_codecs = 1;
1873 if (codec->spdif_support) {
1874 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1875 err = snd_ctl_add(codec->card,
1876 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1884 #ifdef CONFIG_PM_SLEEP
1885 static int ali_suspend(struct device *dev)
1887 struct pci_dev *pci = to_pci_dev(dev);
1888 struct snd_card *card = dev_get_drvdata(dev);
1889 struct snd_ali *chip = card->private_data;
1890 struct snd_ali_image *im;
1897 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1898 for (i = 0; i < chip->num_of_codecs; i++) {
1899 snd_pcm_suspend_all(chip->pcm[i]);
1900 snd_ac97_suspend(chip->ac97[i]);
1903 spin_lock_irq(&chip->reg_lock);
1905 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1906 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1907 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1909 /* disable all IRQ bits */
1910 outl(0, ALI_REG(chip, ALI_MISCINT));
1912 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1913 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1915 im->regs[i] = inl(ALI_REG(chip, i*4));
1918 for (i = 0; i < ALI_CHANNELS; i++) {
1919 outb(i, ALI_REG(chip, ALI_GC_CIR));
1920 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1921 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1924 /* stop all HW channel */
1925 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1927 spin_unlock_irq(&chip->reg_lock);
1929 pci_disable_device(pci);
1930 pci_save_state(pci);
1931 pci_set_power_state(pci, PCI_D3hot);
1935 static int ali_resume(struct device *dev)
1937 struct pci_dev *pci = to_pci_dev(dev);
1938 struct snd_card *card = dev_get_drvdata(dev);
1939 struct snd_ali *chip = card->private_data;
1940 struct snd_ali_image *im;
1947 pci_set_power_state(pci, PCI_D0);
1948 pci_restore_state(pci);
1949 if (pci_enable_device(pci) < 0) {
1950 printk(KERN_ERR "ali5451: pci_enable_device failed, "
1951 "disabling device\n");
1952 snd_card_disconnect(card);
1955 pci_set_master(pci);
1957 spin_lock_irq(&chip->reg_lock);
1959 for (i = 0; i < ALI_CHANNELS; i++) {
1960 outb(i, ALI_REG(chip, ALI_GC_CIR));
1961 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1962 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1965 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1966 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1969 outl(im->regs[i], ALI_REG(chip, i*4));
1972 /* start HW channel */
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);
1979 for (i = 0 ; i < chip->num_of_codecs; i++)
1980 snd_ac97_resume(chip->ac97[i]);
1982 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1986 static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
1987 #define ALI_PM_OPS &ali_pm
1989 #define ALI_PM_OPS NULL
1990 #endif /* CONFIG_PM_SLEEP */
1992 static int snd_ali_free(struct snd_ali * codec)
1994 if (codec->hw_initialized)
1995 snd_ali_disable_address_interrupt(codec);
1996 if (codec->irq >= 0)
1997 free_irq(codec->irq, codec);
1999 pci_release_regions(codec->pci);
2000 pci_disable_device(codec->pci);
2001 #ifdef CONFIG_PM_SLEEP
2002 kfree(codec->image);
2004 pci_dev_put(codec->pci_m1533);
2005 pci_dev_put(codec->pci_m7101);
2010 static int snd_ali_chip_init(struct snd_ali *codec)
2012 unsigned int legacy;
2014 struct pci_dev *pci_dev;
2016 snd_ali_printk("chip initializing ... \n");
2018 if (snd_ali_reset_5451(codec)) {
2019 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
2023 if (codec->revision == ALI_5451_V02) {
2024 pci_dev = codec->pci_m1533;
2025 pci_read_config_byte(pci_dev, 0x59, &temp);
2027 pci_write_config_byte(pci_dev, 0x59, temp);
2029 pci_dev = codec->pci_m7101;
2030 pci_read_config_byte(pci_dev, 0xb8, &temp);
2032 pci_write_config_byte(pci_dev, 0xB8, temp);
2035 pci_read_config_dword(codec->pci, 0x44, &legacy);
2036 legacy &= 0xff00ff00;
2037 legacy |= 0x000800aa;
2038 pci_write_config_dword(codec->pci, 0x44, legacy);
2040 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2041 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2042 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2043 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2044 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2046 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2047 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2048 AC97_EXTENDED_STATUS);
2049 if (codec->spdif_support) {
2050 snd_ali_enable_spdif_out(codec);
2051 codec->spdif_mask = 0x00000002;
2054 codec->num_of_codecs = 1;
2056 /* secondary codec - modem */
2057 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2058 codec->num_of_codecs++;
2059 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2060 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2061 ALI_SCTRL_LINE_OUT_EN),
2062 ALI_REG(codec, ALI_SCTRL));
2065 snd_ali_printk("chip initialize succeed.\n");
2070 /* proc for register dump */
2071 static void snd_ali_proc_read(struct snd_info_entry *entry,
2072 struct snd_info_buffer *buf)
2074 struct snd_ali *codec = entry->private_data;
2076 for (i = 0; i < 256 ; i+= 4)
2077 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2080 static void snd_ali_proc_init(struct snd_ali *codec)
2082 struct snd_info_entry *entry;
2083 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
2084 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
2087 static int snd_ali_resources(struct snd_ali *codec)
2091 snd_ali_printk("resources allocation ...\n");
2092 err = pci_request_regions(codec->pci, "ALI 5451");
2095 codec->port = pci_resource_start(codec->pci, 0);
2097 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2098 IRQF_SHARED, KBUILD_MODNAME, codec)) {
2099 snd_printk(KERN_ERR "Unable to request irq.\n");
2102 codec->irq = codec->pci->irq;
2103 snd_ali_printk("resources allocated.\n");
2106 static int snd_ali_dev_free(struct snd_device *device)
2108 struct snd_ali *codec = device->device_data;
2109 snd_ali_free(codec);
2113 static int snd_ali_create(struct snd_card *card,
2114 struct pci_dev *pci,
2117 struct snd_ali **r_ali)
2119 struct snd_ali *codec;
2121 unsigned short cmdw;
2122 static struct snd_device_ops ops = {
2123 .dev_free = snd_ali_dev_free,
2128 snd_ali_printk("creating ...\n");
2130 /* enable PCI device */
2131 err = pci_enable_device(pci);
2134 /* check, if we can restrict PCI DMA transfers to 31 bits */
2135 if (pci_set_dma_mask(pci, DMA_BIT_MASK(31)) < 0 ||
2136 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(31)) < 0) {
2137 snd_printk(KERN_ERR "architecture does not support "
2138 "31bit PCI busmaster DMA\n");
2139 pci_disable_device(pci);
2143 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2145 pci_disable_device(pci);
2149 spin_lock_init(&codec->reg_lock);
2150 spin_lock_init(&codec->voice_alloc);
2155 codec->revision = pci->revision;
2156 codec->spdif_support = spdif_support;
2158 if (pcm_streams < 1)
2160 if (pcm_streams > 32)
2163 pci_set_master(pci);
2164 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2165 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2166 cmdw |= PCI_COMMAND_IO;
2167 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2169 pci_set_master(pci);
2171 if (snd_ali_resources(codec)) {
2172 snd_ali_free(codec);
2176 synchronize_irq(pci->irq);
2178 codec->synth.chmap = 0;
2179 codec->synth.chcnt = 0;
2180 codec->spdif_mask = 0;
2181 codec->synth.synthcount = 0;
2183 if (codec->revision == ALI_5451_V02)
2184 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2186 codec->chregs.regs.ac97read = ALI_AC97_READ;
2187 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2189 codec->chregs.regs.start = ALI_START;
2190 codec->chregs.regs.stop = ALI_STOP;
2191 codec->chregs.regs.aint = ALI_AINT;
2192 codec->chregs.regs.ainten = ALI_AINTEN;
2194 codec->chregs.data.start = 0x00;
2195 codec->chregs.data.stop = 0x00;
2196 codec->chregs.data.aint = 0x00;
2197 codec->chregs.data.ainten = 0x00;
2199 /* M1533: southbridge */
2200 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2201 if (!codec->pci_m1533) {
2202 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2203 snd_ali_free(codec);
2206 /* M7101: power management */
2207 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2208 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2209 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2210 snd_ali_free(codec);
2214 snd_ali_printk("snd_device_new is called.\n");
2215 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2217 snd_ali_free(codec);
2221 snd_card_set_dev(card, &pci->dev);
2223 /* initialise synth voices*/
2224 for (i = 0; i < ALI_CHANNELS; i++)
2225 codec->synth.voices[i].number = i;
2227 err = snd_ali_chip_init(codec);
2229 snd_printk(KERN_ERR "ali create: chip init error.\n");
2233 #ifdef CONFIG_PM_SLEEP
2234 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2236 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2239 snd_ali_enable_address_interrupt(codec);
2240 codec->hw_initialized = 1;
2243 snd_ali_printk("created.\n");
2247 static int snd_ali_probe(struct pci_dev *pci,
2248 const struct pci_device_id *pci_id)
2250 struct snd_card *card;
2251 struct snd_ali *codec;
2254 snd_ali_printk("probe ...\n");
2256 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
2260 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2263 card->private_data = codec;
2265 snd_ali_printk("mixer building ...\n");
2266 err = snd_ali_mixer(codec);
2270 snd_ali_printk("pcm building ...\n");
2271 err = snd_ali_build_pcms(codec);
2275 snd_ali_proc_init(codec);
2277 strcpy(card->driver, "ALI5451");
2278 strcpy(card->shortname, "ALI 5451");
2280 sprintf(card->longname, "%s at 0x%lx, irq %i",
2281 card->shortname, codec->port, codec->irq);
2283 snd_ali_printk("register card.\n");
2284 err = snd_card_register(card);
2288 pci_set_drvdata(pci, card);
2292 snd_card_free(card);
2296 static void snd_ali_remove(struct pci_dev *pci)
2298 snd_card_free(pci_get_drvdata(pci));
2301 static struct pci_driver ali5451_driver = {
2302 .name = KBUILD_MODNAME,
2303 .id_table = snd_ali_ids,
2304 .probe = snd_ali_probe,
2305 .remove = snd_ali_remove,
2311 module_pci_driver(ali5451_driver);