2 * Matt Wu <Matt_Wu@acersoftech.com.cn>
4 * Routines for control of ALi pci audio M5451
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public Lcodecnse as published by
14 * the Free Software Foundation; either version 2 of the Lcodecnse, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public Lcodecnse for more details.
22 * You should have received a copy of the GNU General Public Lcodecnse
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sound/driver.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/slab.h>
35 #include <linux/moduleparam.h>
36 #include <linux/dma-mapping.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/info.h>
40 #include <sound/ac97_codec.h>
41 #include <sound/mpu401.h>
42 #include <sound/initval.h>
44 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
45 MODULE_DESCRIPTION("ALI M5451");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
49 static int index = SNDRV_DEFAULT_IDX1; /* Index */
50 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
51 static int pcm_channels = 32;
54 module_param(index, int, 0444);
55 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
56 module_param(id, charp, 0444);
57 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
58 module_param(pcm_channels, int, 0444);
59 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
60 module_param(spdif, bool, 0444);
61 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
63 /* just for backward compatibility */
65 module_param(enable, bool, 0444);
69 * Debug part definitions
72 /* #define ALI_DEBUG */
75 #define snd_ali_printk(format, args...) printk(KERN_DEBUG format, ##args);
77 #define snd_ali_printk(format, args...)
81 * Constants definition
84 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
87 #define ALI_CHANNELS 32
89 #define ALI_PCM_IN_CHANNEL 31
90 #define ALI_SPDIF_IN_CHANNEL 19
91 #define ALI_SPDIF_OUT_CHANNEL 15
92 #define ALI_CENTER_CHANNEL 24
93 #define ALI_LEF_CHANNEL 23
94 #define ALI_SURR_LEFT_CHANNEL 26
95 #define ALI_SURR_RIGHT_CHANNEL 25
96 #define ALI_MODEM_IN_CHANNEL 21
97 #define ALI_MODEM_OUT_CHANNEL 20
99 #define SNDRV_ALI_VOICE_TYPE_PCM 01
100 #define SNDRV_ALI_VOICE_TYPE_OTH 02
102 #define ALI_5451_V02 0x02
108 #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
109 #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
110 #define ALI_LEGACY_DMAR11 0x0b /* MOD */
111 #define ALI_LEGACY_DMAR15 0x0f /* MMR */
112 #define ALI_MPUR0 0x20
113 #define ALI_MPUR1 0x21
114 #define ALI_MPUR2 0x22
115 #define ALI_MPUR3 0x23
117 #define ALI_AC97_WRITE 0x40
118 #define ALI_AC97_READ 0x44
120 #define ALI_SCTRL 0x48
121 #define ALI_SPDIF_OUT_ENABLE 0x20
122 #define ALI_SCTRL_LINE_IN2 (1 << 9)
123 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
124 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
125 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
126 #define ALI_SCTRL_CODEC1_READY (1 << 24)
127 #define ALI_SCTRL_CODEC2_READY (1 << 25)
128 #define ALI_AC97_GPIO 0x4c
129 #define ALI_AC97_GPIO_ENABLE 0x8000
130 #define ALI_AC97_GPIO_DATA_SHIFT 16
131 #define ALI_SPDIF_CS 0x70
132 #define ALI_SPDIF_CTRL 0x74
133 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
134 #define ALI_SPDIF_IN_CH_STATUS 0x40
135 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
136 #define ALI_START 0x80
137 #define ALI_STOP 0x84
138 #define ALI_CSPF 0x90
139 #define ALI_AINT 0x98
140 #define ALI_GC_CIR 0xa0
141 #define ENDLP_IE 0x00001000
142 #define MIDLP_IE 0x00002000
143 #define ALI_AINTEN 0xa4
144 #define ALI_VOLUME 0xa8
145 #define ALI_SBDELTA_DELTA_R 0xac
146 #define ALI_MISCINT 0xb0
147 #define ADDRESS_IRQ 0x00000020
148 #define TARGET_REACHED 0x00008000
149 #define MIXER_OVERFLOW 0x00000800
150 #define MIXER_UNDERFLOW 0x00000400
151 #define GPIO_IRQ 0x01000000
152 #define ALI_SBBL_SBCL 0xc0
153 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
154 #define ALI_STIMER 0xc8
155 #define ALI_GLOBAL_CONTROL 0xd4
156 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
157 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
158 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
159 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
160 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
162 #define ALI_CSO_ALPHA_FMS 0xe0
164 #define ALI_ESO_DELTA 0xe8
165 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
166 #define ALI_EBUF1 0xf4
167 #define ALI_EBUF2 0xf8
169 #define ALI_REG(codec, x) ((codec)->port + x)
175 struct snd_ali_voice;
177 struct snd_ali_channel_control {
186 /* register addresses */
192 unsigned int ac97read;
193 unsigned int ac97write;
198 struct snd_ali_voice {
208 struct snd_ali *codec;
209 struct snd_pcm_substream *substream;
210 struct snd_ali_voice *extra;
212 int eso; /* final ESO value for channel */
213 int count; /* runtime->period_size */
218 void (*private_free)(void *private_data);
224 struct snd_ali_voice voices[ALI_CHANNELS];
226 unsigned int chcnt; /* num of opened channels */
227 unsigned int chmap; /* bitmap for opened channels */
228 unsigned int synthcount;
233 #define ALI_GLOBAL_REGS 56
234 #define ALI_CHANNEL_REGS 8
235 struct snd_ali_image {
236 u32 regs[ALI_GLOBAL_REGS];
237 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
244 unsigned char revision;
246 unsigned int hw_initialized :1;
247 unsigned int spdif_support :1;
250 struct pci_dev *pci_m1533;
251 struct pci_dev *pci_m7101;
253 struct snd_card *card;
254 struct snd_pcm *pcm[MAX_CODECS];
255 struct snd_alidev synth;
256 struct snd_ali_channel_control chregs;
259 unsigned int spdif_mask;
261 unsigned int spurious_irq_count;
262 unsigned int spurious_irq_max_delta;
264 unsigned int num_of_codecs;
266 struct snd_ac97_bus *ac97_bus;
267 struct snd_ac97 *ac97[MAX_CODECS];
268 unsigned short ac97_ext_id;
269 unsigned short ac97_ext_status;
272 spinlock_t voice_alloc;
275 struct snd_ali_image *image;
279 static struct pci_device_id snd_ali_ids[] = {
280 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
283 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
285 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
286 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
287 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
294 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
297 return (unsigned int)inl(ALI_REG(codec, port));
300 static inline void snd_ali_5451_poke(struct snd_ali *codec,
304 outl((unsigned int)val, ALI_REG(codec, port));
307 static int snd_ali_codec_ready(struct snd_ali *codec,
310 unsigned long end_time;
313 end_time = jiffies + msecs_to_jiffies(250);
315 res = snd_ali_5451_peek(codec,port);
318 schedule_timeout_uninterruptible(1);
319 } while (time_after_eq(end_time, jiffies));
320 snd_ali_5451_poke(codec, port, res & ~0x8000);
321 snd_printdd("ali_codec_ready: codec is not ready.\n ");
325 static int snd_ali_stimer_ready(struct snd_ali *codec)
327 unsigned long end_time;
328 unsigned long dwChk1,dwChk2;
330 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
331 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
333 end_time = jiffies + msecs_to_jiffies(250);
335 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
336 if (dwChk2 != dwChk1)
338 schedule_timeout_uninterruptible(1);
339 } while (time_after_eq(end_time, jiffies));
340 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
344 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
352 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
356 port = codec->chregs.regs.ac97write;
358 if (snd_ali_codec_ready(codec, port) < 0)
360 if (snd_ali_stimer_ready(codec) < 0)
363 dwVal = (unsigned int) (reg & 0xff);
364 dwVal |= 0x8000 | (val << 16);
367 if (codec->revision == ALI_5451_V02)
370 snd_ali_5451_poke(codec, port, dwVal);
375 static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
383 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
387 port = codec->chregs.regs.ac97read;
389 if (snd_ali_codec_ready(codec, port) < 0)
391 if (snd_ali_stimer_ready(codec) < 0)
394 dwVal = (unsigned int) (reg & 0xff);
395 dwVal |= 0x8000; /* bit 15*/
399 snd_ali_5451_poke(codec, port, dwVal);
401 if (snd_ali_stimer_ready(codec) < 0)
403 if (snd_ali_codec_ready(codec, port) < 0)
406 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
409 static void snd_ali_codec_write(struct snd_ac97 *ac97,
413 struct snd_ali *codec = ac97->private_data;
415 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
416 if (reg == AC97_GPIO_STATUS) {
417 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
418 ALI_REG(codec, ALI_AC97_GPIO));
421 snd_ali_codec_poke(codec, ac97->num, reg, val);
426 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
429 struct snd_ali *codec = ac97->private_data;
431 snd_ali_printk("codec_read reg=%xh.\n", reg);
432 return snd_ali_codec_peek(codec, ac97->num, reg);
439 static int snd_ali_reset_5451(struct snd_ali *codec)
441 struct pci_dev *pci_dev;
442 unsigned short wCount, wReg;
445 pci_dev = codec->pci_m1533;
447 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
448 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
450 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
451 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
455 pci_dev = codec->pci;
456 pci_read_config_dword(pci_dev, 0x44, &dwVal);
457 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
459 pci_read_config_dword(pci_dev, 0x44, &dwVal);
460 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
465 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
466 if ((wReg & 0x000f) == 0x000f)
471 /* non-fatal if you have a non PM capable codec */
472 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
478 static int snd_ali_reset_codec(struct snd_ali *codec)
480 struct pci_dev *pci_dev;
483 unsigned short wCount, wReg;
485 pci_dev = codec->pci_m1533;
487 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
488 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
490 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
491 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
494 bVal = inb(ALI_REG(codec,ALI_SCTRL));
496 outb(ALI_REG(codec,ALI_SCTRL),bVal);
498 bVal = inb(ALI_REG(codec,ALI_SCTRL));
500 outb(ALI_REG(codec,ALI_SCTRL),bVal);
505 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
506 if ((wReg & 0x000f) == 0x000f)
516 * ALI 5451 Controller
519 static void snd_ali_enable_special_channel(struct snd_ali *codec,
520 unsigned int channel)
524 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
525 dwVal |= 1 << (channel & 0x0000001f);
526 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
529 static void snd_ali_disable_special_channel(struct snd_ali *codec,
530 unsigned int channel)
534 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
535 dwVal &= ~(1 << (channel & 0x0000001f));
536 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
539 static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
543 gc = inl(ALI_REG(codec, ALI_GC_CIR));
546 outl( gc, ALI_REG(codec, ALI_GC_CIR));
549 static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
553 gc = inl(ALI_REG(codec, ALI_GC_CIR));
556 outl(gc, ALI_REG(codec, ALI_GC_CIR));
560 static void snd_ali_enable_voice_irq(struct snd_ali *codec,
561 unsigned int channel)
564 struct snd_ali_channel_control *pchregs = &(codec->chregs);
566 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
568 mask = 1 << (channel & 0x1f);
569 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
570 pchregs->data.ainten |= mask;
571 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
575 static void snd_ali_disable_voice_irq(struct snd_ali *codec,
576 unsigned int channel)
579 struct snd_ali_channel_control *pchregs = &(codec->chregs);
581 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
583 mask = 1 << (channel & 0x1f);
584 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
585 pchregs->data.ainten &= ~mask;
586 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
589 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
591 unsigned int idx = channel & 0x1f;
593 if (codec->synth.chcnt >= ALI_CHANNELS){
595 "ali_alloc_pcm_channel: no free channels.\n");
599 if (!(codec->synth.chmap & (1 << idx))) {
600 codec->synth.chmap |= 1 << idx;
601 codec->synth.chcnt++;
602 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
608 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
613 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
617 if (codec->spdif_support &&
618 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
619 ALI_SPDIF_IN_SUPPORT))
620 idx = ALI_SPDIF_IN_CHANNEL;
622 idx = ALI_PCM_IN_CHANNEL;
624 result = snd_ali_alloc_pcm_channel(codec, idx);
628 snd_printk(KERN_ERR "ali_find_free_channel: "
629 "record channel is busy now.\n");
635 if (codec->spdif_support &&
636 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
637 ALI_SPDIF_OUT_CH_ENABLE)) {
638 idx = ALI_SPDIF_OUT_CHANNEL;
639 result = snd_ali_alloc_pcm_channel(codec, idx);
643 snd_printk(KERN_ERR "ali_find_free_channel: "
644 "S/PDIF out channel is in busy now.\n");
647 for (idx = 0; idx < ALI_CHANNELS; idx++) {
648 result = snd_ali_alloc_pcm_channel(codec, idx);
652 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
656 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
658 unsigned int idx = channel & 0x0000001f;
660 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
662 if (channel < 0 || channel >= ALI_CHANNELS)
665 if (!(codec->synth.chmap & (1 << idx))) {
666 snd_printk(KERN_ERR "ali_free_channel_pcm: "
667 "channel %d is not in use.\n", channel);
670 codec->synth.chmap &= ~(1 << idx);
671 codec->synth.chcnt--;
676 static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
678 unsigned int mask = 1 << (channel & 0x1f);
680 snd_ali_printk("start_voice: channel=%d\n",channel);
681 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
685 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
687 unsigned int mask = 1 << (channel & 0x1f);
689 snd_ali_printk("stop_voice: channel=%d\n",channel);
690 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
697 static void snd_ali_delay(struct snd_ali *codec,int interval)
699 unsigned long begintimer,currenttimer;
701 begintimer = inl(ALI_REG(codec, ALI_STIMER));
702 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
704 while (currenttimer < begintimer + interval) {
705 if (snd_ali_stimer_ready(codec) < 0)
707 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
712 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
718 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
720 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
722 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
724 snd_ali_delay(codec, 6);
725 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
730 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
734 for (count = 0; count <= 50000; count++) {
735 snd_ali_delay(codec, 6);
736 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
745 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
749 if (R2 >= 0x0b && R2 <= 0x0e) {
750 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
752 wval |= (0x09 << 8) | 0x05;
753 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
755 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
756 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
757 } else if (R2 == 0x12) {
758 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
760 wval |= (0x0e << 8) | 0x08;
761 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
763 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
764 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
768 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
773 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
776 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
778 snd_ali_detect_spdif_rate(codec);
780 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
784 case 0: dwRate = 44100; break;
785 case 1: dwRate = 48000; break;
786 case 2: dwRate = 32000; break;
787 default: dwRate = 0; break;
793 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
797 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
798 dwVal |= ALI_SPDIF_IN_SUPPORT;
799 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
801 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
803 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
805 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
808 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
812 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
813 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
814 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
816 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
820 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
826 case 32000: dwRate = 0x300; break;
827 case 48000: dwRate = 0x200; break;
828 default: dwRate = 0; break;
831 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
832 bVal &= (unsigned char)(~(1<<6));
834 bVal |= 0x80; /* select right */
835 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
836 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
838 bVal &= ~0x80; /* select left */
839 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
840 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
843 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
847 struct pci_dev *pci_dev;
849 pci_dev = codec->pci_m1533;
852 pci_read_config_byte(pci_dev, 0x61, &bVal);
854 pci_write_config_byte(pci_dev, 0x61, bVal);
855 pci_read_config_byte(pci_dev, 0x7d, &bVal);
857 pci_write_config_byte(pci_dev, 0x7d, bVal);
859 pci_read_config_byte(pci_dev, 0x7e, &bVal);
862 pci_write_config_byte(pci_dev, 0x7e, bVal);
864 bVal = inb(ALI_REG(codec, ALI_SCTRL));
865 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
867 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
868 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
870 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
871 wVal |= ALI_SPDIF_OUT_SEL_PCM;
872 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
873 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
876 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
880 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
881 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
882 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
884 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
885 if (flag & ALI_SPDIF_OUT_NON_PCM)
889 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
891 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
894 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
898 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
899 wVal |= ALI_SPDIF_OUT_SEL_PCM;
900 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
902 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
905 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
909 bVal = inb(ALI_REG(codec, ALI_SCTRL));
910 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
912 snd_ali_disable_spdif_chnout(codec);
915 static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
917 struct snd_ali_voice *pvoice;
918 struct snd_pcm_runtime *runtime;
919 struct snd_ali_channel_control *pchregs;
920 unsigned int old, mask;
922 unsigned int temp, cspf;
925 pchregs = &(codec->chregs);
927 /* check if interrupt occurred for channel */
928 old = pchregs->data.aint;
929 mask = 1U << (channel & 0x1f);
934 pvoice = &codec->synth.voices[channel];
935 runtime = pvoice->substream->runtime;
938 spin_lock(&codec->reg_lock);
940 if (pvoice->pcm && pvoice->substream) {
943 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
944 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
945 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
947 if (pvoice->running) {
948 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
950 spin_unlock(&codec->reg_lock);
951 snd_pcm_period_elapsed(pvoice->substream);
952 spin_lock(&codec->reg_lock);
954 snd_ali_stop_voice(codec, channel);
955 snd_ali_disable_voice_irq(codec, channel);
957 } else if (codec->synth.voices[channel].synth) {
958 /* synth interrupt */
959 } else if (codec->synth.voices[channel].midi) {
962 /* unknown interrupt */
963 snd_ali_stop_voice(codec, channel);
964 snd_ali_disable_voice_irq(codec, channel);
966 spin_unlock(&codec->reg_lock);
967 outl(mask,ALI_REG(codec,pchregs->regs.aint));
968 pchregs->data.aint = old & (~mask);
971 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
973 struct snd_ali *codec = dev_id;
975 unsigned int audio_int;
976 struct snd_ali_channel_control *pchregs;
978 if (codec == NULL || !codec->hw_initialized)
981 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
985 pchregs = &(codec->chregs);
986 if (audio_int & ADDRESS_IRQ) {
987 /* get interrupt status for all channels */
988 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
989 for (channel = 0; channel < ALI_CHANNELS; channel++)
990 snd_ali_update_ptr(codec, channel);
992 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
993 ALI_REG(codec, ALI_MISCINT));
999 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
1000 int type, int rec, int channel)
1002 struct snd_ali_voice *pvoice;
1005 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
1007 spin_lock_irq(&codec->voice_alloc);
1008 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1009 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1010 snd_ali_find_free_channel(codec,rec);
1012 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
1013 spin_unlock_irq(&codec->voice_alloc);
1016 pvoice = &(codec->synth.voices[idx]);
1017 pvoice->codec = codec;
1021 spin_unlock_irq(&codec->voice_alloc);
1024 spin_unlock_irq(&codec->voice_alloc);
1029 static void snd_ali_free_voice(struct snd_ali * codec,
1030 struct snd_ali_voice *pvoice)
1032 void (*private_free)(void *);
1035 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1036 if (pvoice == NULL || !pvoice->use)
1038 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1039 spin_lock_irq(&codec->voice_alloc);
1040 private_free = pvoice->private_free;
1041 private_data = pvoice->private_data;
1042 pvoice->private_free = NULL;
1043 pvoice->private_data = NULL;
1045 snd_ali_free_channel_pcm(codec, pvoice->number);
1046 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1047 pvoice->substream = NULL;
1048 spin_unlock_irq(&codec->voice_alloc);
1050 private_free(private_data);
1054 static void snd_ali_clear_voices(struct snd_ali *codec,
1060 for (i = v_min; i <= v_max; i++) {
1061 snd_ali_stop_voice(codec, i);
1062 snd_ali_disable_voice_irq(codec, i);
1066 static void snd_ali_write_voice_regs(struct snd_ali *codec,
1067 unsigned int Channel,
1072 unsigned int ALPHA_FMS,
1079 unsigned int ctlcmds[4];
1081 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
1083 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1085 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1086 ctlcmds[3] = (GVSEL << 31) |
1087 ((PAN & 0x0000007f) << 24) |
1088 ((VOL & 0x000000ff) << 16) |
1089 ((CTRL & 0x0000000f) << 12) |
1092 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1094 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1095 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1096 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1097 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1099 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1100 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1103 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1115 else if (rate == 8000)
1117 else if (rate == 48000)
1120 delta = ((48000 << 12) / rate) & 0x0000ffff;
1124 else if (rate == 8000)
1126 else if (rate == 48000)
1129 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1135 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1138 struct snd_pcm_runtime *runtime = substream->runtime;
1141 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1144 if (snd_pcm_format_width(runtime->format) == 16)
1145 CTRL |= 0x00000008; /* 16-bit data */
1146 if (!snd_pcm_format_unsigned(runtime->format))
1147 CTRL |= 0x00000002; /* signed data */
1148 if (runtime->channels > 1)
1149 CTRL |= 0x00000004; /* stereo data */
1157 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1161 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1162 struct snd_pcm_substream *s;
1163 unsigned int what, whati, capture_flag;
1164 struct snd_ali_voice *pvoice, *evoice;
1169 case SNDRV_PCM_TRIGGER_START:
1170 case SNDRV_PCM_TRIGGER_RESUME:
1173 case SNDRV_PCM_TRIGGER_STOP:
1174 case SNDRV_PCM_TRIGGER_SUSPEND:
1181 what = whati = capture_flag = 0;
1182 snd_pcm_group_for_each_entry(s, substream) {
1183 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1184 pvoice = s->runtime->private_data;
1185 evoice = pvoice->extra;
1186 what |= 1 << (pvoice->number & 0x1f);
1188 whati |= 1 << (pvoice->number & 0x1f);
1190 whati |= 1 << (evoice->number & 0x1f);
1191 what |= 1 << (evoice->number & 0x1f);
1194 pvoice->running = 1;
1196 evoice->running = 1;
1198 pvoice->running = 0;
1200 evoice->running = 0;
1202 snd_pcm_trigger_done(s, substream);
1207 spin_lock(&codec->reg_lock);
1209 outl(what, ALI_REG(codec, ALI_STOP));
1210 val = inl(ALI_REG(codec, ALI_AINTEN));
1215 outl(val, ALI_REG(codec, ALI_AINTEN));
1217 outl(what, ALI_REG(codec, ALI_START));
1218 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
1219 spin_unlock(&codec->reg_lock);
1224 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1225 struct snd_pcm_hw_params *hw_params)
1227 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1228 struct snd_pcm_runtime *runtime = substream->runtime;
1229 struct snd_ali_voice *pvoice = runtime->private_data;
1230 struct snd_ali_voice *evoice = pvoice->extra;
1233 err = snd_pcm_lib_malloc_pages(substream,
1234 params_buffer_bytes(hw_params));
1238 /* voice management */
1240 if (params_buffer_size(hw_params) / 2 !=
1241 params_period_size(hw_params)) {
1243 evoice = snd_ali_alloc_voice(codec,
1244 SNDRV_ALI_VOICE_TYPE_PCM,
1248 pvoice->extra = evoice;
1249 evoice->substream = substream;
1253 snd_ali_free_voice(codec, evoice);
1254 pvoice->extra = evoice = NULL;
1261 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1263 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1264 struct snd_pcm_runtime *runtime = substream->runtime;
1265 struct snd_ali_voice *pvoice = runtime->private_data;
1266 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1268 snd_pcm_lib_free_pages(substream);
1270 snd_ali_free_voice(codec, evoice);
1271 pvoice->extra = NULL;
1276 static int snd_ali_hw_params(struct snd_pcm_substream *substream,
1277 struct snd_pcm_hw_params *hw_params)
1279 return snd_pcm_lib_malloc_pages(substream,
1280 params_buffer_bytes(hw_params));
1283 static int snd_ali_hw_free(struct snd_pcm_substream *substream)
1285 return snd_pcm_lib_free_pages(substream);
1288 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1290 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1291 struct snd_pcm_runtime *runtime = substream->runtime;
1292 struct snd_ali_voice *pvoice = runtime->private_data;
1293 struct snd_ali_voice *evoice = pvoice->extra;
1304 snd_ali_printk("playback_prepare ...\n");
1306 spin_lock_irq(&codec->reg_lock);
1308 /* set Delta (rate) value */
1309 Delta = snd_ali_convert_rate(runtime->rate, 0);
1311 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1312 pvoice->number == ALI_PCM_IN_CHANNEL)
1313 snd_ali_disable_special_channel(codec, pvoice->number);
1314 else if (codec->spdif_support &&
1315 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1316 ALI_SPDIF_OUT_CH_ENABLE)
1317 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1318 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1322 /* set Loop Back Address */
1323 LBA = runtime->dma_addr;
1325 /* set interrupt count size */
1326 pvoice->count = runtime->period_size;
1328 /* set target ESO for channel */
1329 pvoice->eso = runtime->buffer_size;
1331 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1332 pvoice->eso, pvoice->count);
1334 /* set ESO to capture first MIDLP interrupt */
1335 ESO = pvoice->eso -1;
1337 CTRL = snd_ali_control_mode(substream);
1343 snd_ali_printk("playback_prepare:\n");
1344 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1345 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1346 snd_ali_write_voice_regs(codec,
1359 evoice->count = pvoice->count;
1360 evoice->eso = pvoice->count << 1;
1361 ESO = evoice->eso - 1;
1362 snd_ali_write_voice_regs(codec,
1375 spin_unlock_irq(&codec->reg_lock);
1380 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1382 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1383 struct snd_pcm_runtime *runtime = substream->runtime;
1384 struct snd_ali_voice *pvoice = runtime->private_data;
1395 spin_lock_irq(&codec->reg_lock);
1397 snd_ali_printk("ali_prepare...\n");
1399 snd_ali_enable_special_channel(codec,pvoice->number);
1401 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1402 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1403 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1405 /* Prepare capture intr channel */
1406 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1410 spin_unlock_irq(&codec->reg_lock);
1411 if (codec->revision != ALI_5451_V02)
1414 rate = snd_ali_get_spdif_in_rate(codec);
1416 snd_printk(KERN_WARNING "ali_capture_preapre: "
1417 "spdif rate detect err!\n");
1420 spin_lock_irq(&codec->reg_lock);
1421 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1422 if (bValue & 0x10) {
1423 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1424 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
1428 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1431 /* set target ESO for channel */
1432 pvoice->eso = runtime->buffer_size;
1434 /* set interrupt count size */
1435 pvoice->count = runtime->period_size;
1437 /* set Loop Back Address */
1438 LBA = runtime->dma_addr;
1440 /* set ESO to capture first MIDLP interrupt */
1441 ESO = pvoice->eso - 1;
1442 CTRL = snd_ali_control_mode(substream);
1448 snd_ali_write_voice_regs( codec,
1461 spin_unlock_irq(&codec->reg_lock);
1467 static snd_pcm_uframes_t
1468 snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1470 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1471 struct snd_pcm_runtime *runtime = substream->runtime;
1472 struct snd_ali_voice *pvoice = runtime->private_data;
1475 spin_lock(&codec->reg_lock);
1476 if (!pvoice->running) {
1477 spin_unlock(&codec->reg_lock);
1480 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1481 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1482 spin_unlock(&codec->reg_lock);
1483 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1489 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1491 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1492 struct snd_pcm_runtime *runtime = substream->runtime;
1493 struct snd_ali_voice *pvoice = runtime->private_data;
1496 spin_lock(&codec->reg_lock);
1497 if (!pvoice->running) {
1498 spin_unlock_irq(&codec->reg_lock);
1501 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1502 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1503 spin_unlock(&codec->reg_lock);
1508 static struct snd_pcm_hardware snd_ali_playback =
1510 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1511 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1512 SNDRV_PCM_INFO_MMAP_VALID |
1513 SNDRV_PCM_INFO_RESUME |
1514 SNDRV_PCM_INFO_SYNC_START),
1515 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1516 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1517 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1522 .buffer_bytes_max = (256*1024),
1523 .period_bytes_min = 64,
1524 .period_bytes_max = (256*1024),
1526 .periods_max = 1024,
1531 * Capture support device description
1534 static struct snd_pcm_hardware snd_ali_capture =
1536 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1537 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1538 SNDRV_PCM_INFO_MMAP_VALID |
1539 SNDRV_PCM_INFO_RESUME |
1540 SNDRV_PCM_INFO_SYNC_START),
1541 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1542 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1543 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1548 .buffer_bytes_max = (128*1024),
1549 .period_bytes_min = 64,
1550 .period_bytes_max = (128*1024),
1552 .periods_max = 1024,
1556 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1558 struct snd_ali_voice *pvoice = runtime->private_data;
1559 struct snd_ali *codec;
1562 codec = pvoice->codec;
1563 snd_ali_free_voice(pvoice->codec, pvoice);
1567 static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1568 int channel, struct snd_pcm_hardware *phw)
1570 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1571 struct snd_pcm_runtime *runtime = substream->runtime;
1572 struct snd_ali_voice *pvoice;
1574 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1579 pvoice->substream = substream;
1580 runtime->private_data = pvoice;
1581 runtime->private_free = snd_ali_pcm_free_substream;
1584 snd_pcm_set_sync(substream);
1585 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1590 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1592 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1595 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1597 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1600 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1605 static int snd_ali_close(struct snd_pcm_substream *substream)
1607 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1608 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1610 snd_ali_disable_special_channel(codec,pvoice->number);
1615 static struct snd_pcm_ops snd_ali_playback_ops = {
1616 .open = snd_ali_playback_open,
1617 .close = snd_ali_playback_close,
1618 .ioctl = snd_pcm_lib_ioctl,
1619 .hw_params = snd_ali_playback_hw_params,
1620 .hw_free = snd_ali_playback_hw_free,
1621 .prepare = snd_ali_playback_prepare,
1622 .trigger = snd_ali_trigger,
1623 .pointer = snd_ali_playback_pointer,
1626 static struct snd_pcm_ops snd_ali_capture_ops = {
1627 .open = snd_ali_capture_open,
1628 .close = snd_ali_close,
1629 .ioctl = snd_pcm_lib_ioctl,
1630 .hw_params = snd_ali_hw_params,
1631 .hw_free = snd_ali_hw_free,
1632 .prepare = snd_ali_prepare,
1633 .trigger = snd_ali_trigger,
1634 .pointer = snd_ali_pointer,
1641 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1642 struct snd_pcm_hw_params *hw_params)
1644 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1645 unsigned int modem_num = chip->num_of_codecs - 1;
1646 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1647 params_rate(hw_params));
1648 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1649 return snd_ali_hw_params(substream, hw_params);
1652 static struct snd_pcm_hardware snd_ali_modem =
1654 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1655 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1656 SNDRV_PCM_INFO_MMAP_VALID |
1657 SNDRV_PCM_INFO_RESUME |
1658 SNDRV_PCM_INFO_SYNC_START),
1659 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1660 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1661 SNDRV_PCM_RATE_16000),
1666 .buffer_bytes_max = (256*1024),
1667 .period_bytes_min = 64,
1668 .period_bytes_max = (256*1024),
1670 .periods_max = 1024,
1674 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1677 static unsigned int rates[] = {8000, 9600, 12000, 16000};
1678 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1679 .count = ARRAY_SIZE(rates),
1683 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1687 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1688 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1691 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1693 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1696 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1698 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1701 static struct snd_pcm_ops snd_ali_modem_playback_ops = {
1702 .open = snd_ali_modem_playback_open,
1703 .close = snd_ali_close,
1704 .ioctl = snd_pcm_lib_ioctl,
1705 .hw_params = snd_ali_modem_hw_params,
1706 .hw_free = snd_ali_hw_free,
1707 .prepare = snd_ali_prepare,
1708 .trigger = snd_ali_trigger,
1709 .pointer = snd_ali_pointer,
1712 static struct snd_pcm_ops snd_ali_modem_capture_ops = {
1713 .open = snd_ali_modem_capture_open,
1714 .close = snd_ali_close,
1715 .ioctl = snd_pcm_lib_ioctl,
1716 .hw_params = snd_ali_modem_hw_params,
1717 .hw_free = snd_ali_hw_free,
1718 .prepare = snd_ali_prepare,
1719 .trigger = snd_ali_trigger,
1720 .pointer = snd_ali_pointer,
1724 struct ali_pcm_description {
1726 unsigned int playback_num;
1727 unsigned int capture_num;
1728 struct snd_pcm_ops *playback_ops;
1729 struct snd_pcm_ops *capture_ops;
1730 unsigned short class;
1734 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1736 struct snd_ali *codec = pcm->private_data;
1737 codec->pcm[pcm->device] = NULL;
1741 static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
1742 struct ali_pcm_description *desc)
1744 struct snd_pcm *pcm;
1747 err = snd_pcm_new(codec->card, desc->name, device,
1748 desc->playback_num, desc->capture_num, &pcm);
1750 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
1753 pcm->private_data = codec;
1754 pcm->private_free = snd_ali_pcm_free;
1755 if (desc->playback_ops)
1756 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1757 desc->playback_ops);
1758 if (desc->capture_ops)
1759 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1762 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1763 snd_dma_pci_data(codec->pci),
1766 pcm->info_flags = 0;
1767 pcm->dev_class = desc->class;
1768 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1769 strcpy(pcm->name, desc->name);
1770 codec->pcm[0] = pcm;
1774 static struct ali_pcm_description ali_pcms[] = {
1775 { .name = "ALI 5451",
1776 .playback_num = ALI_CHANNELS,
1778 .playback_ops = &snd_ali_playback_ops,
1779 .capture_ops = &snd_ali_capture_ops
1781 { .name = "ALI 5451 modem",
1784 .playback_ops = &snd_ali_modem_playback_ops,
1785 .capture_ops = &snd_ali_modem_capture_ops,
1786 .class = SNDRV_PCM_CLASS_MODEM
1790 static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
1793 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1794 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1802 #define ALI5451_SPDIF(xname, xindex, value) \
1803 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1804 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1805 .put = snd_ali5451_spdif_put, .private_value = value}
1807 static int snd_ali5451_spdif_info(struct snd_kcontrol *kcontrol,
1808 struct snd_ctl_elem_info *uinfo)
1810 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1812 uinfo->value.integer.min = 0;
1813 uinfo->value.integer.max = 1;
1817 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1818 struct snd_ctl_elem_value *ucontrol)
1820 struct snd_ali *codec = kcontrol->private_data;
1821 unsigned int enable;
1823 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1825 spin_lock_irq(&codec->reg_lock);
1826 switch (kcontrol->private_value) {
1828 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1831 enable = ((codec->spdif_mask & 0x02) &&
1832 (codec->spdif_mask & 0x04)) ? 1 : 0;
1835 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1840 ucontrol->value.integer.value[0] = enable;
1841 spin_unlock_irq(&codec->reg_lock);
1845 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_value *ucontrol)
1848 struct snd_ali *codec = kcontrol->private_data;
1849 unsigned int change = 0, enable = 0;
1851 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1853 spin_lock_irq(&codec->reg_lock);
1854 switch (kcontrol->private_value) {
1856 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1857 change = change ^ enable;
1860 codec->spdif_mask |= 0x02;
1861 snd_ali_enable_spdif_out(codec);
1863 codec->spdif_mask &= ~(0x02);
1864 codec->spdif_mask &= ~(0x04);
1865 snd_ali_disable_spdif_out(codec);
1870 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1871 change = change ^ enable;
1872 if (change && (codec->spdif_mask & 0x02)) {
1874 codec->spdif_mask |= 0x04;
1875 snd_ali_enable_spdif_chnout(codec);
1877 codec->spdif_mask &= ~(0x04);
1878 snd_ali_disable_spdif_chnout(codec);
1883 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1884 change = change ^ enable;
1887 codec->spdif_mask |= 0x01;
1888 snd_ali_enable_spdif_in(codec);
1890 codec->spdif_mask &= ~(0x01);
1891 snd_ali_disable_spdif_in(codec);
1898 spin_unlock_irq(&codec->reg_lock);
1903 static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
1904 /* spdif aplayback switch */
1905 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1906 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1907 /* spdif out to spdif channel */
1908 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1909 /* spdif in from spdif channel */
1910 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1913 static int __devinit snd_ali_mixer(struct snd_ali * codec)
1915 struct snd_ac97_template ac97;
1918 static struct snd_ac97_bus_ops ops = {
1919 .write = snd_ali_codec_write,
1920 .read = snd_ali_codec_read,
1923 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1927 memset(&ac97, 0, sizeof(ac97));
1928 ac97.private_data = codec;
1930 for (i = 0; i < codec->num_of_codecs; i++) {
1932 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1935 "ali mixer %d creating error.\n", i);
1938 codec->num_of_codecs = 1;
1943 if (codec->spdif_support) {
1944 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1945 err = snd_ctl_add(codec->card,
1946 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1955 static int ali_suspend(struct pci_dev *pci, pm_message_t state)
1957 struct snd_card *card = pci_get_drvdata(pci);
1958 struct snd_ali *chip = card->private_data;
1959 struct snd_ali_image *im;
1966 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1967 for (i = 0; i < chip->num_of_codecs; i++) {
1968 snd_pcm_suspend_all(chip->pcm[i]);
1969 snd_ac97_suspend(chip->ac97[i]);
1972 spin_lock_irq(&chip->reg_lock);
1974 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1975 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1976 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1978 /* disable all IRQ bits */
1979 outl(0, ALI_REG(chip, ALI_MISCINT));
1981 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1982 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1984 im->regs[i] = inl(ALI_REG(chip, i*4));
1987 for (i = 0; i < ALI_CHANNELS; i++) {
1988 outb(i, ALI_REG(chip, ALI_GC_CIR));
1989 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1990 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1993 /* stop all HW channel */
1994 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1996 spin_unlock_irq(&chip->reg_lock);
1998 pci_disable_device(pci);
1999 pci_save_state(pci);
2000 pci_set_power_state(pci, pci_choose_state(pci, state));
2004 static int ali_resume(struct pci_dev *pci)
2006 struct snd_card *card = pci_get_drvdata(pci);
2007 struct snd_ali *chip = card->private_data;
2008 struct snd_ali_image *im;
2015 pci_set_power_state(pci, PCI_D0);
2016 pci_restore_state(pci);
2017 if (pci_enable_device(pci) < 0) {
2018 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2019 "disabling device\n");
2020 snd_card_disconnect(card);
2023 pci_set_master(pci);
2025 spin_lock_irq(&chip->reg_lock);
2027 for (i = 0; i < ALI_CHANNELS; i++) {
2028 outb(i, ALI_REG(chip, ALI_GC_CIR));
2029 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2030 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2033 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2034 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
2037 outl(im->regs[i], ALI_REG(chip, i*4));
2040 /* start HW channel */
2041 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2042 /* restore IRQ enable bits */
2043 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2045 spin_unlock_irq(&chip->reg_lock);
2047 for (i = 0 ; i < chip->num_of_codecs; i++)
2048 snd_ac97_resume(chip->ac97[i]);
2050 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2053 #endif /* CONFIG_PM */
2055 static int snd_ali_free(struct snd_ali * codec)
2057 if (codec->hw_initialized)
2058 snd_ali_disable_address_interrupt(codec);
2059 if (codec->irq >= 0) {
2060 synchronize_irq(codec->irq);
2061 free_irq(codec->irq, codec);
2064 pci_release_regions(codec->pci);
2065 pci_disable_device(codec->pci);
2067 kfree(codec->image);
2069 pci_dev_put(codec->pci_m1533);
2070 pci_dev_put(codec->pci_m7101);
2075 static int snd_ali_chip_init(struct snd_ali *codec)
2077 unsigned int legacy;
2079 struct pci_dev *pci_dev;
2081 snd_ali_printk("chip initializing ... \n");
2083 if (snd_ali_reset_5451(codec)) {
2084 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
2088 if (codec->revision == ALI_5451_V02) {
2089 pci_dev = codec->pci_m1533;
2090 pci_read_config_byte(pci_dev, 0x59, &temp);
2092 pci_write_config_byte(pci_dev, 0x59, temp);
2094 pci_dev = codec->pci_m7101;
2095 pci_read_config_byte(pci_dev, 0xb8, &temp);
2097 pci_write_config_byte(pci_dev, 0xB8, temp);
2100 pci_read_config_dword(codec->pci, 0x44, &legacy);
2101 legacy &= 0xff00ff00;
2102 legacy |= 0x000800aa;
2103 pci_write_config_dword(codec->pci, 0x44, legacy);
2105 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2106 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2107 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2108 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2109 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2111 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2112 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2113 AC97_EXTENDED_STATUS);
2114 if (codec->spdif_support) {
2115 snd_ali_enable_spdif_out(codec);
2116 codec->spdif_mask = 0x00000002;
2119 codec->num_of_codecs = 1;
2121 /* secondary codec - modem */
2122 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2123 codec->num_of_codecs++;
2124 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2125 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2126 ALI_SCTRL_LINE_OUT_EN),
2127 ALI_REG(codec, ALI_SCTRL));
2130 snd_ali_printk("chip initialize succeed.\n");
2135 /* proc for register dump */
2136 static void snd_ali_proc_read(struct snd_info_entry *entry,
2137 struct snd_info_buffer *buf)
2139 struct snd_ali *codec = entry->private_data;
2141 for (i = 0; i < 256 ; i+= 4)
2142 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2145 static void __devinit snd_ali_proc_init(struct snd_ali *codec)
2147 struct snd_info_entry *entry;
2148 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
2149 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
2152 static int __devinit snd_ali_resources(struct snd_ali *codec)
2156 snd_ali_printk("resouces allocation ...\n");
2157 err = pci_request_regions(codec->pci, "ALI 5451");
2160 codec->port = pci_resource_start(codec->pci, 0);
2162 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2163 IRQF_SHARED, "ALI 5451", codec)) {
2164 snd_printk(KERN_ERR "Unable to request irq.\n");
2167 codec->irq = codec->pci->irq;
2168 snd_ali_printk("resouces allocated.\n");
2171 static int snd_ali_dev_free(struct snd_device *device)
2173 struct snd_ali *codec = device->device_data;
2174 snd_ali_free(codec);
2178 static int __devinit snd_ali_create(struct snd_card *card,
2179 struct pci_dev *pci,
2182 struct snd_ali ** r_ali)
2184 struct snd_ali *codec;
2186 unsigned short cmdw;
2187 static struct snd_device_ops ops = {
2188 .dev_free = snd_ali_dev_free,
2193 snd_ali_printk("creating ...\n");
2195 /* enable PCI device */
2196 err = pci_enable_device(pci);
2199 /* check, if we can restrict PCI DMA transfers to 31 bits */
2200 if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
2201 pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
2202 snd_printk(KERN_ERR "architecture does not support "
2203 "31bit PCI busmaster DMA\n");
2204 pci_disable_device(pci);
2208 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2210 pci_disable_device(pci);
2214 spin_lock_init(&codec->reg_lock);
2215 spin_lock_init(&codec->voice_alloc);
2220 codec->revision = pci->revision;
2221 codec->spdif_support = spdif_support;
2223 if (pcm_streams < 1)
2225 if (pcm_streams > 32)
2228 pci_set_master(pci);
2229 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2230 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2231 cmdw |= PCI_COMMAND_IO;
2232 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2234 pci_set_master(pci);
2236 if (snd_ali_resources(codec)) {
2237 snd_ali_free(codec);
2241 synchronize_irq(pci->irq);
2243 codec->synth.chmap = 0;
2244 codec->synth.chcnt = 0;
2245 codec->spdif_mask = 0;
2246 codec->synth.synthcount = 0;
2248 if (codec->revision == ALI_5451_V02)
2249 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2251 codec->chregs.regs.ac97read = ALI_AC97_READ;
2252 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2254 codec->chregs.regs.start = ALI_START;
2255 codec->chregs.regs.stop = ALI_STOP;
2256 codec->chregs.regs.aint = ALI_AINT;
2257 codec->chregs.regs.ainten = ALI_AINTEN;
2259 codec->chregs.data.start = 0x00;
2260 codec->chregs.data.stop = 0x00;
2261 codec->chregs.data.aint = 0x00;
2262 codec->chregs.data.ainten = 0x00;
2264 /* M1533: southbridge */
2265 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2266 if (!codec->pci_m1533) {
2267 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2268 snd_ali_free(codec);
2271 /* M7101: power management */
2272 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2273 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2274 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2275 snd_ali_free(codec);
2279 snd_ali_printk("snd_device_new is called.\n");
2280 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2282 snd_ali_free(codec);
2286 snd_card_set_dev(card, &pci->dev);
2288 /* initialise synth voices*/
2289 for (i = 0; i < ALI_CHANNELS; i++)
2290 codec->synth.voices[i].number = i;
2292 err = snd_ali_chip_init(codec);
2294 snd_printk(KERN_ERR "ali create: chip init error.\n");
2299 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2301 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2304 snd_ali_enable_address_interrupt(codec);
2305 codec->hw_initialized = 1;
2308 snd_ali_printk("created.\n");
2312 static int __devinit snd_ali_probe(struct pci_dev *pci,
2313 const struct pci_device_id *pci_id)
2315 struct snd_card *card;
2316 struct snd_ali *codec;
2319 snd_ali_printk("probe ...\n");
2321 card = snd_card_new(index, id, THIS_MODULE, 0);
2325 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2328 card->private_data = codec;
2330 snd_ali_printk("mixer building ...\n");
2331 err = snd_ali_mixer(codec);
2335 snd_ali_printk("pcm building ...\n");
2336 err = snd_ali_build_pcms(codec);
2340 snd_ali_proc_init(codec);
2342 strcpy(card->driver, "ALI5451");
2343 strcpy(card->shortname, "ALI 5451");
2345 sprintf(card->longname, "%s at 0x%lx, irq %i",
2346 card->shortname, codec->port, codec->irq);
2348 snd_ali_printk("register card.\n");
2349 err = snd_card_register(card);
2353 pci_set_drvdata(pci, card);
2357 snd_card_free(card);
2361 static void __devexit snd_ali_remove(struct pci_dev *pci)
2363 snd_card_free(pci_get_drvdata(pci));
2364 pci_set_drvdata(pci, NULL);
2367 static struct pci_driver driver = {
2369 .id_table = snd_ali_ids,
2370 .probe = snd_ali_probe,
2371 .remove = __devexit_p(snd_ali_remove),
2373 .suspend = ali_suspend,
2374 .resume = ali_resume,
2378 static int __init alsa_card_ali_init(void)
2380 return pci_register_driver(&driver);
2383 static void __exit alsa_card_ali_exit(void)
2385 pci_unregister_driver(&driver);
2388 module_init(alsa_card_ali_init)
2389 module_exit(alsa_card_ali_exit)