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/moduleparam.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;
274 struct snd_ali_image *image;
278 static struct pci_device_id 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);
314 res = snd_ali_5451_peek(codec,port);
317 schedule_timeout_uninterruptible(1);
318 } while (time_after_eq(end_time, jiffies));
319 snd_ali_5451_poke(codec, port, res & ~0x8000);
320 snd_printdd("ali_codec_ready: codec is not ready.\n ");
324 static int snd_ali_stimer_ready(struct snd_ali *codec)
326 unsigned long end_time;
327 unsigned long dwChk1,dwChk2;
329 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
330 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
332 end_time = jiffies + msecs_to_jiffies(250);
334 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
335 if (dwChk2 != dwChk1)
337 schedule_timeout_uninterruptible(1);
338 } while (time_after_eq(end_time, jiffies));
339 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
343 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
351 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
355 port = codec->chregs.regs.ac97write;
357 if (snd_ali_codec_ready(codec, port) < 0)
359 if (snd_ali_stimer_ready(codec) < 0)
362 dwVal = (unsigned int) (reg & 0xff);
363 dwVal |= 0x8000 | (val << 16);
366 if (codec->revision == ALI_5451_V02)
369 snd_ali_5451_poke(codec, port, dwVal);
374 static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
382 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
386 port = codec->chregs.regs.ac97read;
388 if (snd_ali_codec_ready(codec, port) < 0)
390 if (snd_ali_stimer_ready(codec) < 0)
393 dwVal = (unsigned int) (reg & 0xff);
394 dwVal |= 0x8000; /* bit 15*/
398 snd_ali_5451_poke(codec, port, dwVal);
400 if (snd_ali_stimer_ready(codec) < 0)
402 if (snd_ali_codec_ready(codec, port) < 0)
405 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
408 static void snd_ali_codec_write(struct snd_ac97 *ac97,
412 struct snd_ali *codec = ac97->private_data;
414 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
415 if (reg == AC97_GPIO_STATUS) {
416 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
417 ALI_REG(codec, ALI_AC97_GPIO));
420 snd_ali_codec_poke(codec, ac97->num, reg, val);
425 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
428 struct snd_ali *codec = ac97->private_data;
430 snd_ali_printk("codec_read reg=%xh.\n", reg);
431 return snd_ali_codec_peek(codec, ac97->num, reg);
438 static int snd_ali_reset_5451(struct snd_ali *codec)
440 struct pci_dev *pci_dev;
441 unsigned short wCount, wReg;
444 pci_dev = codec->pci_m1533;
446 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
447 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
449 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
450 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
454 pci_dev = codec->pci;
455 pci_read_config_dword(pci_dev, 0x44, &dwVal);
456 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
458 pci_read_config_dword(pci_dev, 0x44, &dwVal);
459 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
464 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
465 if ((wReg & 0x000f) == 0x000f)
470 /* non-fatal if you have a non PM capable codec */
471 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
477 static int snd_ali_reset_codec(struct snd_ali *codec)
479 struct pci_dev *pci_dev;
482 unsigned short wCount, wReg;
484 pci_dev = codec->pci_m1533;
486 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
487 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
489 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
490 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
493 bVal = inb(ALI_REG(codec,ALI_SCTRL));
495 outb(ALI_REG(codec,ALI_SCTRL),bVal);
497 bVal = inb(ALI_REG(codec,ALI_SCTRL));
499 outb(ALI_REG(codec,ALI_SCTRL),bVal);
504 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
505 if ((wReg & 0x000f) == 0x000f)
515 * ALI 5451 Controller
518 static void snd_ali_enable_special_channel(struct snd_ali *codec,
519 unsigned int channel)
523 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
524 dwVal |= 1 << (channel & 0x0000001f);
525 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
528 static void snd_ali_disable_special_channel(struct snd_ali *codec,
529 unsigned int channel)
533 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
534 dwVal &= ~(1 << (channel & 0x0000001f));
535 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
538 static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
542 gc = inl(ALI_REG(codec, ALI_GC_CIR));
545 outl( gc, ALI_REG(codec, ALI_GC_CIR));
548 static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
552 gc = inl(ALI_REG(codec, ALI_GC_CIR));
555 outl(gc, ALI_REG(codec, ALI_GC_CIR));
559 static void snd_ali_enable_voice_irq(struct snd_ali *codec,
560 unsigned int channel)
563 struct snd_ali_channel_control *pchregs = &(codec->chregs);
565 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
567 mask = 1 << (channel & 0x1f);
568 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
569 pchregs->data.ainten |= mask;
570 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
574 static void snd_ali_disable_voice_irq(struct snd_ali *codec,
575 unsigned int channel)
578 struct snd_ali_channel_control *pchregs = &(codec->chregs);
580 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
582 mask = 1 << (channel & 0x1f);
583 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
584 pchregs->data.ainten &= ~mask;
585 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
588 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
590 unsigned int idx = channel & 0x1f;
592 if (codec->synth.chcnt >= ALI_CHANNELS){
594 "ali_alloc_pcm_channel: no free channels.\n");
598 if (!(codec->synth.chmap & (1 << idx))) {
599 codec->synth.chmap |= 1 << idx;
600 codec->synth.chcnt++;
601 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
607 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
612 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
616 if (codec->spdif_support &&
617 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
618 ALI_SPDIF_IN_SUPPORT))
619 idx = ALI_SPDIF_IN_CHANNEL;
621 idx = ALI_PCM_IN_CHANNEL;
623 result = snd_ali_alloc_pcm_channel(codec, idx);
627 snd_printk(KERN_ERR "ali_find_free_channel: "
628 "record channel is busy now.\n");
634 if (codec->spdif_support &&
635 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
636 ALI_SPDIF_OUT_CH_ENABLE)) {
637 idx = ALI_SPDIF_OUT_CHANNEL;
638 result = snd_ali_alloc_pcm_channel(codec, idx);
642 snd_printk(KERN_ERR "ali_find_free_channel: "
643 "S/PDIF out channel is in busy now.\n");
646 for (idx = 0; idx < ALI_CHANNELS; idx++) {
647 result = snd_ali_alloc_pcm_channel(codec, idx);
651 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
655 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
657 unsigned int idx = channel & 0x0000001f;
659 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
661 if (channel < 0 || channel >= ALI_CHANNELS)
664 if (!(codec->synth.chmap & (1 << idx))) {
665 snd_printk(KERN_ERR "ali_free_channel_pcm: "
666 "channel %d is not in use.\n", channel);
669 codec->synth.chmap &= ~(1 << idx);
670 codec->synth.chcnt--;
675 static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
677 unsigned int mask = 1 << (channel & 0x1f);
679 snd_ali_printk("start_voice: channel=%d\n",channel);
680 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
684 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
686 unsigned int mask = 1 << (channel & 0x1f);
688 snd_ali_printk("stop_voice: channel=%d\n",channel);
689 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
696 static void snd_ali_delay(struct snd_ali *codec,int interval)
698 unsigned long begintimer,currenttimer;
700 begintimer = inl(ALI_REG(codec, ALI_STIMER));
701 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
703 while (currenttimer < begintimer + interval) {
704 if (snd_ali_stimer_ready(codec) < 0)
706 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
711 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
717 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
719 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
721 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
723 snd_ali_delay(codec, 6);
724 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
729 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
733 for (count = 0; count <= 50000; count++) {
734 snd_ali_delay(codec, 6);
735 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
744 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
748 if (R2 >= 0x0b && R2 <= 0x0e) {
749 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
751 wval |= (0x09 << 8) | 0x05;
752 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
754 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
755 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
756 } else if (R2 == 0x12) {
757 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
759 wval |= (0x0e << 8) | 0x08;
760 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
762 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
763 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
767 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
772 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
775 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
777 snd_ali_detect_spdif_rate(codec);
779 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
783 case 0: dwRate = 44100; break;
784 case 1: dwRate = 48000; break;
785 case 2: dwRate = 32000; break;
786 default: dwRate = 0; break;
792 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
796 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
797 dwVal |= ALI_SPDIF_IN_SUPPORT;
798 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
800 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
802 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
804 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
807 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
811 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
812 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
813 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
815 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
819 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
825 case 32000: dwRate = 0x300; break;
826 case 48000: dwRate = 0x200; break;
827 default: dwRate = 0; break;
830 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
831 bVal &= (unsigned char)(~(1<<6));
833 bVal |= 0x80; /* select right */
834 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
835 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
837 bVal &= ~0x80; /* select left */
838 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
839 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
842 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
846 struct pci_dev *pci_dev;
848 pci_dev = codec->pci_m1533;
851 pci_read_config_byte(pci_dev, 0x61, &bVal);
853 pci_write_config_byte(pci_dev, 0x61, bVal);
854 pci_read_config_byte(pci_dev, 0x7d, &bVal);
856 pci_write_config_byte(pci_dev, 0x7d, bVal);
858 pci_read_config_byte(pci_dev, 0x7e, &bVal);
861 pci_write_config_byte(pci_dev, 0x7e, bVal);
863 bVal = inb(ALI_REG(codec, ALI_SCTRL));
864 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
866 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
867 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
869 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
870 wVal |= ALI_SPDIF_OUT_SEL_PCM;
871 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
872 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
875 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
879 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
880 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
881 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
883 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
884 if (flag & ALI_SPDIF_OUT_NON_PCM)
888 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
890 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
893 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
897 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
898 wVal |= ALI_SPDIF_OUT_SEL_PCM;
899 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
901 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
904 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
908 bVal = inb(ALI_REG(codec, ALI_SCTRL));
909 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
911 snd_ali_disable_spdif_chnout(codec);
914 static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
916 struct snd_ali_voice *pvoice;
917 struct snd_pcm_runtime *runtime;
918 struct snd_ali_channel_control *pchregs;
919 unsigned int old, mask;
921 unsigned int temp, cspf;
924 pchregs = &(codec->chregs);
926 /* check if interrupt occurred for channel */
927 old = pchregs->data.aint;
928 mask = 1U << (channel & 0x1f);
933 pvoice = &codec->synth.voices[channel];
934 runtime = pvoice->substream->runtime;
937 spin_lock(&codec->reg_lock);
939 if (pvoice->pcm && pvoice->substream) {
942 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
943 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
944 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
946 if (pvoice->running) {
947 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
949 spin_unlock(&codec->reg_lock);
950 snd_pcm_period_elapsed(pvoice->substream);
951 spin_lock(&codec->reg_lock);
953 snd_ali_stop_voice(codec, channel);
954 snd_ali_disable_voice_irq(codec, channel);
956 } else if (codec->synth.voices[channel].synth) {
957 /* synth interrupt */
958 } else if (codec->synth.voices[channel].midi) {
961 /* unknown interrupt */
962 snd_ali_stop_voice(codec, channel);
963 snd_ali_disable_voice_irq(codec, channel);
965 spin_unlock(&codec->reg_lock);
966 outl(mask,ALI_REG(codec,pchregs->regs.aint));
967 pchregs->data.aint = old & (~mask);
970 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
972 struct snd_ali *codec = dev_id;
974 unsigned int audio_int;
975 struct snd_ali_channel_control *pchregs;
977 if (codec == NULL || !codec->hw_initialized)
980 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
984 pchregs = &(codec->chregs);
985 if (audio_int & ADDRESS_IRQ) {
986 /* get interrupt status for all channels */
987 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
988 for (channel = 0; channel < ALI_CHANNELS; channel++)
989 snd_ali_update_ptr(codec, channel);
991 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
992 ALI_REG(codec, ALI_MISCINT));
998 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
999 int type, int rec, int channel)
1001 struct snd_ali_voice *pvoice;
1004 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
1006 spin_lock_irq(&codec->voice_alloc);
1007 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1008 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1009 snd_ali_find_free_channel(codec,rec);
1011 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
1012 spin_unlock_irq(&codec->voice_alloc);
1015 pvoice = &(codec->synth.voices[idx]);
1016 pvoice->codec = codec;
1020 spin_unlock_irq(&codec->voice_alloc);
1023 spin_unlock_irq(&codec->voice_alloc);
1028 static void snd_ali_free_voice(struct snd_ali * codec,
1029 struct snd_ali_voice *pvoice)
1031 void (*private_free)(void *);
1034 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1035 if (pvoice == NULL || !pvoice->use)
1037 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1038 spin_lock_irq(&codec->voice_alloc);
1039 private_free = pvoice->private_free;
1040 private_data = pvoice->private_data;
1041 pvoice->private_free = NULL;
1042 pvoice->private_data = NULL;
1044 snd_ali_free_channel_pcm(codec, pvoice->number);
1045 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1046 pvoice->substream = NULL;
1047 spin_unlock_irq(&codec->voice_alloc);
1049 private_free(private_data);
1053 static void snd_ali_clear_voices(struct snd_ali *codec,
1059 for (i = v_min; i <= v_max; i++) {
1060 snd_ali_stop_voice(codec, i);
1061 snd_ali_disable_voice_irq(codec, i);
1065 static void snd_ali_write_voice_regs(struct snd_ali *codec,
1066 unsigned int Channel,
1071 unsigned int ALPHA_FMS,
1078 unsigned int ctlcmds[4];
1080 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
1082 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1084 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1085 ctlcmds[3] = (GVSEL << 31) |
1086 ((PAN & 0x0000007f) << 24) |
1087 ((VOL & 0x000000ff) << 16) |
1088 ((CTRL & 0x0000000f) << 12) |
1091 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1093 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1094 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1095 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1096 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1098 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1099 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1102 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1114 else if (rate == 8000)
1116 else if (rate == 48000)
1119 delta = ((48000 << 12) / rate) & 0x0000ffff;
1123 else if (rate == 8000)
1125 else if (rate == 48000)
1128 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1134 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1137 struct snd_pcm_runtime *runtime = substream->runtime;
1140 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1143 if (snd_pcm_format_width(runtime->format) == 16)
1144 CTRL |= 0x00000008; /* 16-bit data */
1145 if (!snd_pcm_format_unsigned(runtime->format))
1146 CTRL |= 0x00000002; /* signed data */
1147 if (runtime->channels > 1)
1148 CTRL |= 0x00000004; /* stereo data */
1156 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1160 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1161 struct snd_pcm_substream *s;
1162 unsigned int what, whati, capture_flag;
1163 struct snd_ali_voice *pvoice, *evoice;
1168 case SNDRV_PCM_TRIGGER_START:
1169 case SNDRV_PCM_TRIGGER_RESUME:
1172 case SNDRV_PCM_TRIGGER_STOP:
1173 case SNDRV_PCM_TRIGGER_SUSPEND:
1180 what = whati = capture_flag = 0;
1181 snd_pcm_group_for_each_entry(s, substream) {
1182 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1183 pvoice = s->runtime->private_data;
1184 evoice = pvoice->extra;
1185 what |= 1 << (pvoice->number & 0x1f);
1187 whati |= 1 << (pvoice->number & 0x1f);
1189 whati |= 1 << (evoice->number & 0x1f);
1190 what |= 1 << (evoice->number & 0x1f);
1193 pvoice->running = 1;
1195 evoice->running = 1;
1197 pvoice->running = 0;
1199 evoice->running = 0;
1201 snd_pcm_trigger_done(s, substream);
1206 spin_lock(&codec->reg_lock);
1208 outl(what, ALI_REG(codec, ALI_STOP));
1209 val = inl(ALI_REG(codec, ALI_AINTEN));
1214 outl(val, ALI_REG(codec, ALI_AINTEN));
1216 outl(what, ALI_REG(codec, ALI_START));
1217 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
1218 spin_unlock(&codec->reg_lock);
1223 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1224 struct snd_pcm_hw_params *hw_params)
1226 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1227 struct snd_pcm_runtime *runtime = substream->runtime;
1228 struct snd_ali_voice *pvoice = runtime->private_data;
1229 struct snd_ali_voice *evoice = pvoice->extra;
1232 err = snd_pcm_lib_malloc_pages(substream,
1233 params_buffer_bytes(hw_params));
1237 /* voice management */
1239 if (params_buffer_size(hw_params) / 2 !=
1240 params_period_size(hw_params)) {
1242 evoice = snd_ali_alloc_voice(codec,
1243 SNDRV_ALI_VOICE_TYPE_PCM,
1247 pvoice->extra = evoice;
1248 evoice->substream = substream;
1252 snd_ali_free_voice(codec, evoice);
1253 pvoice->extra = evoice = NULL;
1260 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1262 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1263 struct snd_pcm_runtime *runtime = substream->runtime;
1264 struct snd_ali_voice *pvoice = runtime->private_data;
1265 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1267 snd_pcm_lib_free_pages(substream);
1269 snd_ali_free_voice(codec, evoice);
1270 pvoice->extra = NULL;
1275 static int snd_ali_hw_params(struct snd_pcm_substream *substream,
1276 struct snd_pcm_hw_params *hw_params)
1278 return snd_pcm_lib_malloc_pages(substream,
1279 params_buffer_bytes(hw_params));
1282 static int snd_ali_hw_free(struct snd_pcm_substream *substream)
1284 return snd_pcm_lib_free_pages(substream);
1287 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1289 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1290 struct snd_pcm_runtime *runtime = substream->runtime;
1291 struct snd_ali_voice *pvoice = runtime->private_data;
1292 struct snd_ali_voice *evoice = pvoice->extra;
1303 snd_ali_printk("playback_prepare ...\n");
1305 spin_lock_irq(&codec->reg_lock);
1307 /* set Delta (rate) value */
1308 Delta = snd_ali_convert_rate(runtime->rate, 0);
1310 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1311 pvoice->number == ALI_PCM_IN_CHANNEL)
1312 snd_ali_disable_special_channel(codec, pvoice->number);
1313 else if (codec->spdif_support &&
1314 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1315 ALI_SPDIF_OUT_CH_ENABLE)
1316 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1317 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1321 /* set Loop Back Address */
1322 LBA = runtime->dma_addr;
1324 /* set interrupt count size */
1325 pvoice->count = runtime->period_size;
1327 /* set target ESO for channel */
1328 pvoice->eso = runtime->buffer_size;
1330 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1331 pvoice->eso, pvoice->count);
1333 /* set ESO to capture first MIDLP interrupt */
1334 ESO = pvoice->eso -1;
1336 CTRL = snd_ali_control_mode(substream);
1342 snd_ali_printk("playback_prepare:\n");
1343 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1344 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1345 snd_ali_write_voice_regs(codec,
1358 evoice->count = pvoice->count;
1359 evoice->eso = pvoice->count << 1;
1360 ESO = evoice->eso - 1;
1361 snd_ali_write_voice_regs(codec,
1374 spin_unlock_irq(&codec->reg_lock);
1379 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1381 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1382 struct snd_pcm_runtime *runtime = substream->runtime;
1383 struct snd_ali_voice *pvoice = runtime->private_data;
1394 spin_lock_irq(&codec->reg_lock);
1396 snd_ali_printk("ali_prepare...\n");
1398 snd_ali_enable_special_channel(codec,pvoice->number);
1400 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1401 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1402 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1404 /* Prepare capture intr channel */
1405 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1409 spin_unlock_irq(&codec->reg_lock);
1410 if (codec->revision != ALI_5451_V02)
1413 rate = snd_ali_get_spdif_in_rate(codec);
1415 snd_printk(KERN_WARNING "ali_capture_preapre: "
1416 "spdif rate detect err!\n");
1419 spin_lock_irq(&codec->reg_lock);
1420 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1421 if (bValue & 0x10) {
1422 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1423 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
1427 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1430 /* set target ESO for channel */
1431 pvoice->eso = runtime->buffer_size;
1433 /* set interrupt count size */
1434 pvoice->count = runtime->period_size;
1436 /* set Loop Back Address */
1437 LBA = runtime->dma_addr;
1439 /* set ESO to capture first MIDLP interrupt */
1440 ESO = pvoice->eso - 1;
1441 CTRL = snd_ali_control_mode(substream);
1447 snd_ali_write_voice_regs( codec,
1460 spin_unlock_irq(&codec->reg_lock);
1466 static snd_pcm_uframes_t
1467 snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1469 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1470 struct snd_pcm_runtime *runtime = substream->runtime;
1471 struct snd_ali_voice *pvoice = runtime->private_data;
1474 spin_lock(&codec->reg_lock);
1475 if (!pvoice->running) {
1476 spin_unlock(&codec->reg_lock);
1479 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1480 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1481 spin_unlock(&codec->reg_lock);
1482 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1488 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1490 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1491 struct snd_pcm_runtime *runtime = substream->runtime;
1492 struct snd_ali_voice *pvoice = runtime->private_data;
1495 spin_lock(&codec->reg_lock);
1496 if (!pvoice->running) {
1497 spin_unlock_irq(&codec->reg_lock);
1500 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1501 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1502 spin_unlock(&codec->reg_lock);
1507 static struct snd_pcm_hardware snd_ali_playback =
1509 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1510 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1511 SNDRV_PCM_INFO_MMAP_VALID |
1512 SNDRV_PCM_INFO_RESUME |
1513 SNDRV_PCM_INFO_SYNC_START),
1514 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1515 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1516 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1521 .buffer_bytes_max = (256*1024),
1522 .period_bytes_min = 64,
1523 .period_bytes_max = (256*1024),
1525 .periods_max = 1024,
1530 * Capture support device description
1533 static struct snd_pcm_hardware snd_ali_capture =
1535 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1536 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1537 SNDRV_PCM_INFO_MMAP_VALID |
1538 SNDRV_PCM_INFO_RESUME |
1539 SNDRV_PCM_INFO_SYNC_START),
1540 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1541 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1542 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1547 .buffer_bytes_max = (128*1024),
1548 .period_bytes_min = 64,
1549 .period_bytes_max = (128*1024),
1551 .periods_max = 1024,
1555 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1557 struct snd_ali_voice *pvoice = runtime->private_data;
1558 struct snd_ali *codec;
1561 codec = pvoice->codec;
1562 snd_ali_free_voice(pvoice->codec, pvoice);
1566 static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1567 int channel, struct snd_pcm_hardware *phw)
1569 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1570 struct snd_pcm_runtime *runtime = substream->runtime;
1571 struct snd_ali_voice *pvoice;
1573 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1578 pvoice->substream = substream;
1579 runtime->private_data = pvoice;
1580 runtime->private_free = snd_ali_pcm_free_substream;
1583 snd_pcm_set_sync(substream);
1584 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1589 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1591 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1594 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1596 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1599 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1604 static int snd_ali_close(struct snd_pcm_substream *substream)
1606 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1607 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1609 snd_ali_disable_special_channel(codec,pvoice->number);
1614 static struct snd_pcm_ops snd_ali_playback_ops = {
1615 .open = snd_ali_playback_open,
1616 .close = snd_ali_playback_close,
1617 .ioctl = snd_pcm_lib_ioctl,
1618 .hw_params = snd_ali_playback_hw_params,
1619 .hw_free = snd_ali_playback_hw_free,
1620 .prepare = snd_ali_playback_prepare,
1621 .trigger = snd_ali_trigger,
1622 .pointer = snd_ali_playback_pointer,
1625 static struct snd_pcm_ops snd_ali_capture_ops = {
1626 .open = snd_ali_capture_open,
1627 .close = snd_ali_close,
1628 .ioctl = snd_pcm_lib_ioctl,
1629 .hw_params = snd_ali_hw_params,
1630 .hw_free = snd_ali_hw_free,
1631 .prepare = snd_ali_prepare,
1632 .trigger = snd_ali_trigger,
1633 .pointer = snd_ali_pointer,
1640 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1641 struct snd_pcm_hw_params *hw_params)
1643 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1644 unsigned int modem_num = chip->num_of_codecs - 1;
1645 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1646 params_rate(hw_params));
1647 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1648 return snd_ali_hw_params(substream, hw_params);
1651 static struct snd_pcm_hardware snd_ali_modem =
1653 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1654 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1655 SNDRV_PCM_INFO_MMAP_VALID |
1656 SNDRV_PCM_INFO_RESUME |
1657 SNDRV_PCM_INFO_SYNC_START),
1658 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1659 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1660 SNDRV_PCM_RATE_16000),
1665 .buffer_bytes_max = (256*1024),
1666 .period_bytes_min = 64,
1667 .period_bytes_max = (256*1024),
1669 .periods_max = 1024,
1673 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1676 static unsigned int rates[] = {8000, 9600, 12000, 16000};
1677 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1678 .count = ARRAY_SIZE(rates),
1682 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1686 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1687 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1690 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1692 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1695 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1697 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1700 static struct snd_pcm_ops snd_ali_modem_playback_ops = {
1701 .open = snd_ali_modem_playback_open,
1702 .close = snd_ali_close,
1703 .ioctl = snd_pcm_lib_ioctl,
1704 .hw_params = snd_ali_modem_hw_params,
1705 .hw_free = snd_ali_hw_free,
1706 .prepare = snd_ali_prepare,
1707 .trigger = snd_ali_trigger,
1708 .pointer = snd_ali_pointer,
1711 static struct snd_pcm_ops snd_ali_modem_capture_ops = {
1712 .open = snd_ali_modem_capture_open,
1713 .close = snd_ali_close,
1714 .ioctl = snd_pcm_lib_ioctl,
1715 .hw_params = snd_ali_modem_hw_params,
1716 .hw_free = snd_ali_hw_free,
1717 .prepare = snd_ali_prepare,
1718 .trigger = snd_ali_trigger,
1719 .pointer = snd_ali_pointer,
1723 struct ali_pcm_description {
1725 unsigned int playback_num;
1726 unsigned int capture_num;
1727 struct snd_pcm_ops *playback_ops;
1728 struct snd_pcm_ops *capture_ops;
1729 unsigned short class;
1733 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1735 struct snd_ali *codec = pcm->private_data;
1736 codec->pcm[pcm->device] = NULL;
1740 static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
1741 struct ali_pcm_description *desc)
1743 struct snd_pcm *pcm;
1746 err = snd_pcm_new(codec->card, desc->name, device,
1747 desc->playback_num, desc->capture_num, &pcm);
1749 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
1752 pcm->private_data = codec;
1753 pcm->private_free = snd_ali_pcm_free;
1754 if (desc->playback_ops)
1755 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1756 desc->playback_ops);
1757 if (desc->capture_ops)
1758 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1761 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1762 snd_dma_pci_data(codec->pci),
1765 pcm->info_flags = 0;
1766 pcm->dev_class = desc->class;
1767 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1768 strcpy(pcm->name, desc->name);
1769 codec->pcm[0] = pcm;
1773 static struct ali_pcm_description ali_pcms[] = {
1774 { .name = "ALI 5451",
1775 .playback_num = ALI_CHANNELS,
1777 .playback_ops = &snd_ali_playback_ops,
1778 .capture_ops = &snd_ali_capture_ops
1780 { .name = "ALI 5451 modem",
1783 .playback_ops = &snd_ali_modem_playback_ops,
1784 .capture_ops = &snd_ali_modem_capture_ops,
1785 .class = SNDRV_PCM_CLASS_MODEM
1789 static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
1792 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1793 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1801 #define ALI5451_SPDIF(xname, xindex, value) \
1802 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1803 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1804 .put = snd_ali5451_spdif_put, .private_value = value}
1806 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
1808 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1811 struct snd_ali *codec = kcontrol->private_data;
1812 unsigned int spdif_enable;
1814 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1816 spin_lock_irq(&codec->reg_lock);
1817 switch (kcontrol->private_value) {
1819 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1822 spdif_enable = ((codec->spdif_mask & 0x02) &&
1823 (codec->spdif_mask & 0x04)) ? 1 : 0;
1826 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1831 ucontrol->value.integer.value[0] = spdif_enable;
1832 spin_unlock_irq(&codec->reg_lock);
1836 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1837 struct snd_ctl_elem_value *ucontrol)
1839 struct snd_ali *codec = kcontrol->private_data;
1840 unsigned int change = 0, spdif_enable = 0;
1842 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1844 spin_lock_irq(&codec->reg_lock);
1845 switch (kcontrol->private_value) {
1847 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1848 change = change ^ spdif_enable;
1851 codec->spdif_mask |= 0x02;
1852 snd_ali_enable_spdif_out(codec);
1854 codec->spdif_mask &= ~(0x02);
1855 codec->spdif_mask &= ~(0x04);
1856 snd_ali_disable_spdif_out(codec);
1861 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1862 change = change ^ spdif_enable;
1863 if (change && (codec->spdif_mask & 0x02)) {
1865 codec->spdif_mask |= 0x04;
1866 snd_ali_enable_spdif_chnout(codec);
1868 codec->spdif_mask &= ~(0x04);
1869 snd_ali_disable_spdif_chnout(codec);
1874 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1875 change = change ^ spdif_enable;
1878 codec->spdif_mask |= 0x01;
1879 snd_ali_enable_spdif_in(codec);
1881 codec->spdif_mask &= ~(0x01);
1882 snd_ali_disable_spdif_in(codec);
1889 spin_unlock_irq(&codec->reg_lock);
1894 static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
1895 /* spdif aplayback switch */
1896 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1897 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1898 /* spdif out to spdif channel */
1899 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1900 /* spdif in from spdif channel */
1901 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1904 static int __devinit snd_ali_mixer(struct snd_ali * codec)
1906 struct snd_ac97_template ac97;
1909 static struct snd_ac97_bus_ops ops = {
1910 .write = snd_ali_codec_write,
1911 .read = snd_ali_codec_read,
1914 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1918 memset(&ac97, 0, sizeof(ac97));
1919 ac97.private_data = codec;
1921 for (i = 0; i < codec->num_of_codecs; i++) {
1923 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1926 "ali mixer %d creating error.\n", i);
1929 codec->num_of_codecs = 1;
1934 if (codec->spdif_support) {
1935 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1936 err = snd_ctl_add(codec->card,
1937 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1946 static int ali_suspend(struct pci_dev *pci, pm_message_t state)
1948 struct snd_card *card = pci_get_drvdata(pci);
1949 struct snd_ali *chip = card->private_data;
1950 struct snd_ali_image *im;
1957 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1958 for (i = 0; i < chip->num_of_codecs; i++) {
1959 snd_pcm_suspend_all(chip->pcm[i]);
1960 snd_ac97_suspend(chip->ac97[i]);
1963 spin_lock_irq(&chip->reg_lock);
1965 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1966 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1967 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1969 /* disable all IRQ bits */
1970 outl(0, ALI_REG(chip, ALI_MISCINT));
1972 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1973 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1975 im->regs[i] = inl(ALI_REG(chip, i*4));
1978 for (i = 0; i < ALI_CHANNELS; i++) {
1979 outb(i, ALI_REG(chip, ALI_GC_CIR));
1980 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1981 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1984 /* stop all HW channel */
1985 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1987 spin_unlock_irq(&chip->reg_lock);
1989 pci_disable_device(pci);
1990 pci_save_state(pci);
1991 pci_set_power_state(pci, pci_choose_state(pci, state));
1995 static int ali_resume(struct pci_dev *pci)
1997 struct snd_card *card = pci_get_drvdata(pci);
1998 struct snd_ali *chip = card->private_data;
1999 struct snd_ali_image *im;
2006 pci_set_power_state(pci, PCI_D0);
2007 pci_restore_state(pci);
2008 if (pci_enable_device(pci) < 0) {
2009 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2010 "disabling device\n");
2011 snd_card_disconnect(card);
2014 pci_set_master(pci);
2016 spin_lock_irq(&chip->reg_lock);
2018 for (i = 0; i < ALI_CHANNELS; i++) {
2019 outb(i, ALI_REG(chip, ALI_GC_CIR));
2020 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2021 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2024 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2025 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
2028 outl(im->regs[i], ALI_REG(chip, i*4));
2031 /* start HW channel */
2032 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2033 /* restore IRQ enable bits */
2034 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2036 spin_unlock_irq(&chip->reg_lock);
2038 for (i = 0 ; i < chip->num_of_codecs; i++)
2039 snd_ac97_resume(chip->ac97[i]);
2041 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2044 #endif /* CONFIG_PM */
2046 static int snd_ali_free(struct snd_ali * codec)
2048 if (codec->hw_initialized)
2049 snd_ali_disable_address_interrupt(codec);
2050 if (codec->irq >= 0)
2051 free_irq(codec->irq, codec);
2053 pci_release_regions(codec->pci);
2054 pci_disable_device(codec->pci);
2056 kfree(codec->image);
2058 pci_dev_put(codec->pci_m1533);
2059 pci_dev_put(codec->pci_m7101);
2064 static int snd_ali_chip_init(struct snd_ali *codec)
2066 unsigned int legacy;
2068 struct pci_dev *pci_dev;
2070 snd_ali_printk("chip initializing ... \n");
2072 if (snd_ali_reset_5451(codec)) {
2073 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
2077 if (codec->revision == ALI_5451_V02) {
2078 pci_dev = codec->pci_m1533;
2079 pci_read_config_byte(pci_dev, 0x59, &temp);
2081 pci_write_config_byte(pci_dev, 0x59, temp);
2083 pci_dev = codec->pci_m7101;
2084 pci_read_config_byte(pci_dev, 0xb8, &temp);
2086 pci_write_config_byte(pci_dev, 0xB8, temp);
2089 pci_read_config_dword(codec->pci, 0x44, &legacy);
2090 legacy &= 0xff00ff00;
2091 legacy |= 0x000800aa;
2092 pci_write_config_dword(codec->pci, 0x44, legacy);
2094 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2095 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2096 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2097 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2098 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2100 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2101 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2102 AC97_EXTENDED_STATUS);
2103 if (codec->spdif_support) {
2104 snd_ali_enable_spdif_out(codec);
2105 codec->spdif_mask = 0x00000002;
2108 codec->num_of_codecs = 1;
2110 /* secondary codec - modem */
2111 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2112 codec->num_of_codecs++;
2113 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2114 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2115 ALI_SCTRL_LINE_OUT_EN),
2116 ALI_REG(codec, ALI_SCTRL));
2119 snd_ali_printk("chip initialize succeed.\n");
2124 /* proc for register dump */
2125 static void snd_ali_proc_read(struct snd_info_entry *entry,
2126 struct snd_info_buffer *buf)
2128 struct snd_ali *codec = entry->private_data;
2130 for (i = 0; i < 256 ; i+= 4)
2131 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2134 static void __devinit snd_ali_proc_init(struct snd_ali *codec)
2136 struct snd_info_entry *entry;
2137 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
2138 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
2141 static int __devinit snd_ali_resources(struct snd_ali *codec)
2145 snd_ali_printk("resources allocation ...\n");
2146 err = pci_request_regions(codec->pci, "ALI 5451");
2149 codec->port = pci_resource_start(codec->pci, 0);
2151 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2152 IRQF_SHARED, "ALI 5451", codec)) {
2153 snd_printk(KERN_ERR "Unable to request irq.\n");
2156 codec->irq = codec->pci->irq;
2157 snd_ali_printk("resources allocated.\n");
2160 static int snd_ali_dev_free(struct snd_device *device)
2162 struct snd_ali *codec = device->device_data;
2163 snd_ali_free(codec);
2167 static int __devinit snd_ali_create(struct snd_card *card,
2168 struct pci_dev *pci,
2171 struct snd_ali ** r_ali)
2173 struct snd_ali *codec;
2175 unsigned short cmdw;
2176 static struct snd_device_ops ops = {
2177 .dev_free = snd_ali_dev_free,
2182 snd_ali_printk("creating ...\n");
2184 /* enable PCI device */
2185 err = pci_enable_device(pci);
2188 /* check, if we can restrict PCI DMA transfers to 31 bits */
2189 if (pci_set_dma_mask(pci, DMA_BIT_MASK(31)) < 0 ||
2190 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(31)) < 0) {
2191 snd_printk(KERN_ERR "architecture does not support "
2192 "31bit PCI busmaster DMA\n");
2193 pci_disable_device(pci);
2197 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2199 pci_disable_device(pci);
2203 spin_lock_init(&codec->reg_lock);
2204 spin_lock_init(&codec->voice_alloc);
2209 codec->revision = pci->revision;
2210 codec->spdif_support = spdif_support;
2212 if (pcm_streams < 1)
2214 if (pcm_streams > 32)
2217 pci_set_master(pci);
2218 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2219 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2220 cmdw |= PCI_COMMAND_IO;
2221 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2223 pci_set_master(pci);
2225 if (snd_ali_resources(codec)) {
2226 snd_ali_free(codec);
2230 synchronize_irq(pci->irq);
2232 codec->synth.chmap = 0;
2233 codec->synth.chcnt = 0;
2234 codec->spdif_mask = 0;
2235 codec->synth.synthcount = 0;
2237 if (codec->revision == ALI_5451_V02)
2238 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2240 codec->chregs.regs.ac97read = ALI_AC97_READ;
2241 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2243 codec->chregs.regs.start = ALI_START;
2244 codec->chregs.regs.stop = ALI_STOP;
2245 codec->chregs.regs.aint = ALI_AINT;
2246 codec->chregs.regs.ainten = ALI_AINTEN;
2248 codec->chregs.data.start = 0x00;
2249 codec->chregs.data.stop = 0x00;
2250 codec->chregs.data.aint = 0x00;
2251 codec->chregs.data.ainten = 0x00;
2253 /* M1533: southbridge */
2254 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2255 if (!codec->pci_m1533) {
2256 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2257 snd_ali_free(codec);
2260 /* M7101: power management */
2261 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2262 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2263 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2264 snd_ali_free(codec);
2268 snd_ali_printk("snd_device_new is called.\n");
2269 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2271 snd_ali_free(codec);
2275 snd_card_set_dev(card, &pci->dev);
2277 /* initialise synth voices*/
2278 for (i = 0; i < ALI_CHANNELS; i++)
2279 codec->synth.voices[i].number = i;
2281 err = snd_ali_chip_init(codec);
2283 snd_printk(KERN_ERR "ali create: chip init error.\n");
2288 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2290 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2293 snd_ali_enable_address_interrupt(codec);
2294 codec->hw_initialized = 1;
2297 snd_ali_printk("created.\n");
2301 static int __devinit snd_ali_probe(struct pci_dev *pci,
2302 const struct pci_device_id *pci_id)
2304 struct snd_card *card;
2305 struct snd_ali *codec;
2308 snd_ali_printk("probe ...\n");
2310 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
2314 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2317 card->private_data = codec;
2319 snd_ali_printk("mixer building ...\n");
2320 err = snd_ali_mixer(codec);
2324 snd_ali_printk("pcm building ...\n");
2325 err = snd_ali_build_pcms(codec);
2329 snd_ali_proc_init(codec);
2331 strcpy(card->driver, "ALI5451");
2332 strcpy(card->shortname, "ALI 5451");
2334 sprintf(card->longname, "%s at 0x%lx, irq %i",
2335 card->shortname, codec->port, codec->irq);
2337 snd_ali_printk("register card.\n");
2338 err = snd_card_register(card);
2342 pci_set_drvdata(pci, card);
2346 snd_card_free(card);
2350 static void __devexit snd_ali_remove(struct pci_dev *pci)
2352 snd_card_free(pci_get_drvdata(pci));
2353 pci_set_drvdata(pci, NULL);
2356 static struct pci_driver driver = {
2358 .id_table = snd_ali_ids,
2359 .probe = snd_ali_probe,
2360 .remove = __devexit_p(snd_ali_remove),
2362 .suspend = ali_suspend,
2363 .resume = ali_resume,
2367 static int __init alsa_card_ali_init(void)
2369 return pci_register_driver(&driver);
2372 static void __exit alsa_card_ali_exit(void)
2374 pci_unregister_driver(&driver);
2377 module_init(alsa_card_ali_init)
2378 module_exit(alsa_card_ali_exit)