1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Matt Wu <Matt_Wu@acersoftech.com.cn>
5 * Routines for control of ALi pci audio M5451
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/dma-mapping.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/info.h>
25 #include <sound/ac97_codec.h>
26 #include <sound/mpu401.h>
27 #include <sound/initval.h>
29 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
30 MODULE_DESCRIPTION("ALI M5451");
31 MODULE_LICENSE("GPL");
33 static int index = SNDRV_DEFAULT_IDX1; /* Index */
34 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
35 static int pcm_channels = 32;
38 module_param(index, int, 0444);
39 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
40 module_param(id, charp, 0444);
41 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
42 module_param(pcm_channels, int, 0444);
43 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
44 module_param(spdif, bool, 0444);
45 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
47 /* just for backward compatibility */
49 module_param(enable, bool, 0444);
53 * Constants definition
56 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
59 #define ALI_CHANNELS 32
61 #define ALI_PCM_IN_CHANNEL 31
62 #define ALI_SPDIF_IN_CHANNEL 19
63 #define ALI_SPDIF_OUT_CHANNEL 15
64 #define ALI_CENTER_CHANNEL 24
65 #define ALI_LEF_CHANNEL 23
66 #define ALI_SURR_LEFT_CHANNEL 26
67 #define ALI_SURR_RIGHT_CHANNEL 25
68 #define ALI_MODEM_IN_CHANNEL 21
69 #define ALI_MODEM_OUT_CHANNEL 20
71 #define SNDRV_ALI_VOICE_TYPE_PCM 01
72 #define SNDRV_ALI_VOICE_TYPE_OTH 02
74 #define ALI_5451_V02 0x02
80 #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
81 #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
82 #define ALI_LEGACY_DMAR11 0x0b /* MOD */
83 #define ALI_LEGACY_DMAR15 0x0f /* MMR */
84 #define ALI_MPUR0 0x20
85 #define ALI_MPUR1 0x21
86 #define ALI_MPUR2 0x22
87 #define ALI_MPUR3 0x23
89 #define ALI_AC97_WRITE 0x40
90 #define ALI_AC97_READ 0x44
92 #define ALI_SCTRL 0x48
93 #define ALI_SPDIF_OUT_ENABLE 0x20
94 #define ALI_SCTRL_LINE_IN2 (1 << 9)
95 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
96 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
97 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
98 #define ALI_SCTRL_CODEC1_READY (1 << 24)
99 #define ALI_SCTRL_CODEC2_READY (1 << 25)
100 #define ALI_AC97_GPIO 0x4c
101 #define ALI_AC97_GPIO_ENABLE 0x8000
102 #define ALI_AC97_GPIO_DATA_SHIFT 16
103 #define ALI_SPDIF_CS 0x70
104 #define ALI_SPDIF_CTRL 0x74
105 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
106 #define ALI_SPDIF_IN_CH_STATUS 0x40
107 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
108 #define ALI_START 0x80
109 #define ALI_STOP 0x84
110 #define ALI_CSPF 0x90
111 #define ALI_AINT 0x98
112 #define ALI_GC_CIR 0xa0
113 #define ENDLP_IE 0x00001000
114 #define MIDLP_IE 0x00002000
115 #define ALI_AINTEN 0xa4
116 #define ALI_VOLUME 0xa8
117 #define ALI_SBDELTA_DELTA_R 0xac
118 #define ALI_MISCINT 0xb0
119 #define ADDRESS_IRQ 0x00000020
120 #define TARGET_REACHED 0x00008000
121 #define MIXER_OVERFLOW 0x00000800
122 #define MIXER_UNDERFLOW 0x00000400
123 #define GPIO_IRQ 0x01000000
124 #define ALI_SBBL_SBCL 0xc0
125 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
126 #define ALI_STIMER 0xc8
127 #define ALI_GLOBAL_CONTROL 0xd4
128 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
129 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
130 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
131 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
132 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
134 #define ALI_CSO_ALPHA_FMS 0xe0
136 #define ALI_ESO_DELTA 0xe8
137 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
138 #define ALI_EBUF1 0xf4
139 #define ALI_EBUF2 0xf8
141 #define ALI_REG(codec, x) ((codec)->port + x)
147 struct snd_ali_voice;
149 struct snd_ali_channel_control {
158 /* register addresses */
164 unsigned int ac97read;
165 unsigned int ac97write;
170 struct snd_ali_voice {
180 struct snd_ali *codec;
181 struct snd_pcm_substream *substream;
182 struct snd_ali_voice *extra;
184 int eso; /* final ESO value for channel */
185 int count; /* runtime->period_size */
190 void (*private_free)(void *private_data);
196 struct snd_ali_voice voices[ALI_CHANNELS];
198 unsigned int chcnt; /* num of opened channels */
199 unsigned int chmap; /* bitmap for opened channels */
200 unsigned int synthcount;
205 #define ALI_GLOBAL_REGS 56
206 #define ALI_CHANNEL_REGS 8
207 struct snd_ali_image {
208 u32 regs[ALI_GLOBAL_REGS];
209 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
216 unsigned char revision;
218 unsigned int hw_initialized :1;
219 unsigned int spdif_support :1;
222 struct pci_dev *pci_m1533;
223 struct pci_dev *pci_m7101;
225 struct snd_card *card;
226 struct snd_pcm *pcm[MAX_CODECS];
227 struct snd_alidev synth;
228 struct snd_ali_channel_control chregs;
231 unsigned int spdif_mask;
233 unsigned int spurious_irq_count;
234 unsigned int spurious_irq_max_delta;
236 unsigned int num_of_codecs;
238 struct snd_ac97_bus *ac97_bus;
239 struct snd_ac97 *ac97[MAX_CODECS];
240 unsigned short ac97_ext_id;
241 unsigned short ac97_ext_status;
244 spinlock_t voice_alloc;
246 struct snd_ali_image image;
249 static const struct pci_device_id snd_ali_ids[] = {
250 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
253 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
255 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
256 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
257 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
264 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
267 return (unsigned int)inl(ALI_REG(codec, port));
270 static inline void snd_ali_5451_poke(struct snd_ali *codec,
274 outl((unsigned int)val, ALI_REG(codec, port));
277 static int snd_ali_codec_ready(struct snd_ali *codec,
280 unsigned long end_time;
283 end_time = jiffies + msecs_to_jiffies(250);
286 res = snd_ali_5451_peek(codec,port);
289 if (!time_after_eq(end_time, jiffies))
291 schedule_timeout_uninterruptible(1);
294 snd_ali_5451_poke(codec, port, res & ~0x8000);
295 dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n");
299 static int snd_ali_stimer_ready(struct snd_ali *codec)
301 unsigned long end_time;
302 unsigned long dwChk1,dwChk2;
304 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
305 end_time = jiffies + msecs_to_jiffies(250);
308 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
309 if (dwChk2 != dwChk1)
311 if (!time_after_eq(end_time, jiffies))
313 schedule_timeout_uninterruptible(1);
316 dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n");
320 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
328 dev_err(codec->card->dev,
329 "ali_codec_poke: reg(%xh) invalid.\n", reg);
333 port = codec->chregs.regs.ac97write;
335 if (snd_ali_codec_ready(codec, port) < 0)
337 if (snd_ali_stimer_ready(codec) < 0)
340 dwVal = (unsigned int) (reg & 0xff);
341 dwVal |= 0x8000 | (val << 16);
344 if (codec->revision == ALI_5451_V02)
347 snd_ali_5451_poke(codec, port, dwVal);
352 static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
360 dev_err(codec->card->dev,
361 "ali_codec_peek: reg(%xh) invalid.\n", reg);
365 port = codec->chregs.regs.ac97read;
367 if (snd_ali_codec_ready(codec, port) < 0)
369 if (snd_ali_stimer_ready(codec) < 0)
372 dwVal = (unsigned int) (reg & 0xff);
373 dwVal |= 0x8000; /* bit 15*/
377 snd_ali_5451_poke(codec, port, dwVal);
379 if (snd_ali_stimer_ready(codec) < 0)
381 if (snd_ali_codec_ready(codec, port) < 0)
384 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
387 static void snd_ali_codec_write(struct snd_ac97 *ac97,
391 struct snd_ali *codec = ac97->private_data;
393 dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val);
394 if (reg == AC97_GPIO_STATUS) {
395 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
396 ALI_REG(codec, ALI_AC97_GPIO));
399 snd_ali_codec_poke(codec, ac97->num, reg, val);
404 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
407 struct snd_ali *codec = ac97->private_data;
409 dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg);
410 return snd_ali_codec_peek(codec, ac97->num, reg);
417 static int snd_ali_reset_5451(struct snd_ali *codec)
419 struct pci_dev *pci_dev;
420 unsigned short wCount, wReg;
423 pci_dev = codec->pci_m1533;
425 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
426 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
428 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
429 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
433 pci_dev = codec->pci;
434 pci_read_config_dword(pci_dev, 0x44, &dwVal);
435 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
437 pci_read_config_dword(pci_dev, 0x44, &dwVal);
438 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
443 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
444 if ((wReg & 0x000f) == 0x000f)
449 /* non-fatal if you have a non PM capable codec */
450 /* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */
455 * ALI 5451 Controller
458 static void snd_ali_enable_special_channel(struct snd_ali *codec,
459 unsigned int channel)
463 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
464 dwVal |= 1 << (channel & 0x0000001f);
465 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
468 static void snd_ali_disable_special_channel(struct snd_ali *codec,
469 unsigned int channel)
473 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
474 dwVal &= ~(1 << (channel & 0x0000001f));
475 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
478 static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
482 gc = inl(ALI_REG(codec, ALI_GC_CIR));
485 outl( gc, ALI_REG(codec, ALI_GC_CIR));
488 static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
492 gc = inl(ALI_REG(codec, ALI_GC_CIR));
495 outl(gc, ALI_REG(codec, ALI_GC_CIR));
498 static void snd_ali_disable_voice_irq(struct snd_ali *codec,
499 unsigned int channel)
502 struct snd_ali_channel_control *pchregs = &(codec->chregs);
504 dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel);
506 mask = 1 << (channel & 0x1f);
507 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
508 pchregs->data.ainten &= ~mask;
509 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
512 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
514 unsigned int idx = channel & 0x1f;
516 if (codec->synth.chcnt >= ALI_CHANNELS){
517 dev_err(codec->card->dev,
518 "ali_alloc_pcm_channel: no free channels.\n");
522 if (!(codec->synth.chmap & (1 << idx))) {
523 codec->synth.chmap |= 1 << idx;
524 codec->synth.chcnt++;
525 dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx);
531 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
536 dev_dbg(codec->card->dev,
537 "find_free_channel: for %s\n", rec ? "rec" : "pcm");
541 if (codec->spdif_support &&
542 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
543 ALI_SPDIF_IN_SUPPORT))
544 idx = ALI_SPDIF_IN_CHANNEL;
546 idx = ALI_PCM_IN_CHANNEL;
548 result = snd_ali_alloc_pcm_channel(codec, idx);
552 dev_err(codec->card->dev,
553 "ali_find_free_channel: record channel is busy now.\n");
559 if (codec->spdif_support &&
560 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
561 ALI_SPDIF_OUT_CH_ENABLE)) {
562 idx = ALI_SPDIF_OUT_CHANNEL;
563 result = snd_ali_alloc_pcm_channel(codec, idx);
567 dev_err(codec->card->dev,
568 "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
571 for (idx = 0; idx < ALI_CHANNELS; idx++) {
572 result = snd_ali_alloc_pcm_channel(codec, idx);
576 dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n");
580 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
582 unsigned int idx = channel & 0x0000001f;
584 dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel);
586 if (channel < 0 || channel >= ALI_CHANNELS)
589 if (!(codec->synth.chmap & (1 << idx))) {
590 dev_err(codec->card->dev,
591 "ali_free_channel_pcm: channel %d is not in use.\n",
595 codec->synth.chmap &= ~(1 << idx);
596 codec->synth.chcnt--;
600 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
602 unsigned int mask = 1 << (channel & 0x1f);
604 dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel);
605 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
612 static void snd_ali_delay(struct snd_ali *codec,int interval)
614 unsigned long begintimer,currenttimer;
616 begintimer = inl(ALI_REG(codec, ALI_STIMER));
617 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
619 while (currenttimer < begintimer + interval) {
620 if (snd_ali_stimer_ready(codec) < 0)
622 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
627 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
633 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
635 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
637 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
639 snd_ali_delay(codec, 6);
640 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
645 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
649 for (count = 0; count <= 50000; count++) {
650 snd_ali_delay(codec, 6);
651 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
660 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
664 if (R2 >= 0x0b && R2 <= 0x0e) {
665 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
667 wval |= (0x09 << 8) | 0x05;
668 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
670 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
671 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
672 } else if (R2 == 0x12) {
673 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
675 wval |= (0x0e << 8) | 0x08;
676 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
678 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
679 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
683 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
688 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
691 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
693 snd_ali_detect_spdif_rate(codec);
695 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
699 case 0: dwRate = 44100; break;
700 case 1: dwRate = 48000; break;
701 case 2: dwRate = 32000; break;
702 default: dwRate = 0; break;
708 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
712 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
713 dwVal |= ALI_SPDIF_IN_SUPPORT;
714 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
716 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
718 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
720 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
723 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
727 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
728 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
729 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
731 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
735 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
741 case 32000: dwRate = 0x300; break;
742 case 48000: dwRate = 0x200; break;
743 default: dwRate = 0; break;
746 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
747 bVal &= (unsigned char)(~(1<<6));
749 bVal |= 0x80; /* select right */
750 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
751 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
753 bVal &= ~0x80; /* select left */
754 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
755 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
758 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
762 struct pci_dev *pci_dev;
764 pci_dev = codec->pci_m1533;
767 pci_read_config_byte(pci_dev, 0x61, &bVal);
769 pci_write_config_byte(pci_dev, 0x61, bVal);
770 pci_read_config_byte(pci_dev, 0x7d, &bVal);
772 pci_write_config_byte(pci_dev, 0x7d, bVal);
774 pci_read_config_byte(pci_dev, 0x7e, &bVal);
777 pci_write_config_byte(pci_dev, 0x7e, bVal);
779 bVal = inb(ALI_REG(codec, ALI_SCTRL));
780 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
782 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
783 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
785 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
786 wVal |= ALI_SPDIF_OUT_SEL_PCM;
787 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
788 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
791 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
795 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
796 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
797 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
799 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
800 if (flag & ALI_SPDIF_OUT_NON_PCM)
804 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
806 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
809 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
813 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
814 wVal |= ALI_SPDIF_OUT_SEL_PCM;
815 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
817 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
820 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
824 bVal = inb(ALI_REG(codec, ALI_SCTRL));
825 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
827 snd_ali_disable_spdif_chnout(codec);
830 static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
832 struct snd_ali_voice *pvoice;
833 struct snd_ali_channel_control *pchregs;
834 unsigned int old, mask;
836 pchregs = &(codec->chregs);
838 /* check if interrupt occurred for channel */
839 old = pchregs->data.aint;
840 mask = 1U << (channel & 0x1f);
845 pvoice = &codec->synth.voices[channel];
848 spin_lock(&codec->reg_lock);
850 if (pvoice->pcm && pvoice->substream) {
852 if (pvoice->running) {
853 dev_dbg(codec->card->dev,
854 "update_ptr: cso=%4.4x cspf=%d.\n",
855 inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)),
856 (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask);
857 spin_unlock(&codec->reg_lock);
858 snd_pcm_period_elapsed(pvoice->substream);
859 spin_lock(&codec->reg_lock);
861 snd_ali_stop_voice(codec, channel);
862 snd_ali_disable_voice_irq(codec, channel);
864 } else if (codec->synth.voices[channel].synth) {
865 /* synth interrupt */
866 } else if (codec->synth.voices[channel].midi) {
869 /* unknown interrupt */
870 snd_ali_stop_voice(codec, channel);
871 snd_ali_disable_voice_irq(codec, channel);
873 spin_unlock(&codec->reg_lock);
874 outl(mask,ALI_REG(codec,pchregs->regs.aint));
875 pchregs->data.aint = old & (~mask);
878 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
880 struct snd_ali *codec = dev_id;
882 unsigned int audio_int;
883 struct snd_ali_channel_control *pchregs;
885 if (codec == NULL || !codec->hw_initialized)
888 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
892 pchregs = &(codec->chregs);
893 if (audio_int & ADDRESS_IRQ) {
894 /* get interrupt status for all channels */
895 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
896 for (channel = 0; channel < ALI_CHANNELS; channel++)
897 snd_ali_update_ptr(codec, channel);
899 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
900 ALI_REG(codec, ALI_MISCINT));
906 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
907 int type, int rec, int channel)
909 struct snd_ali_voice *pvoice;
912 dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec);
914 spin_lock_irq(&codec->voice_alloc);
915 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
916 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
917 snd_ali_find_free_channel(codec,rec);
919 dev_err(codec->card->dev, "ali_alloc_voice: err.\n");
920 spin_unlock_irq(&codec->voice_alloc);
923 pvoice = &(codec->synth.voices[idx]);
924 pvoice->codec = codec;
928 spin_unlock_irq(&codec->voice_alloc);
931 spin_unlock_irq(&codec->voice_alloc);
936 static void snd_ali_free_voice(struct snd_ali * codec,
937 struct snd_ali_voice *pvoice)
939 void (*private_free)(void *);
942 dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number);
945 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
946 spin_lock_irq(&codec->voice_alloc);
947 private_free = pvoice->private_free;
948 private_data = pvoice->private_data;
949 pvoice->private_free = NULL;
950 pvoice->private_data = NULL;
952 snd_ali_free_channel_pcm(codec, pvoice->number);
953 pvoice->use = pvoice->pcm = pvoice->synth = 0;
954 pvoice->substream = NULL;
955 spin_unlock_irq(&codec->voice_alloc);
957 private_free(private_data);
961 static void snd_ali_clear_voices(struct snd_ali *codec,
967 for (i = v_min; i <= v_max; i++) {
968 snd_ali_stop_voice(codec, i);
969 snd_ali_disable_voice_irq(codec, i);
973 static void snd_ali_write_voice_regs(struct snd_ali *codec,
974 unsigned int Channel,
979 unsigned int ALPHA_FMS,
986 unsigned int ctlcmds[4];
988 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
990 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
992 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
993 ctlcmds[3] = (GVSEL << 31) |
994 ((PAN & 0x0000007f) << 24) |
995 ((VOL & 0x000000ff) << 16) |
996 ((CTRL & 0x0000000f) << 12) |
999 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1001 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1002 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1003 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1004 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1006 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1007 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1010 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1022 else if (rate == 8000)
1024 else if (rate == 48000)
1027 delta = ((48000 << 12) / rate) & 0x0000ffff;
1031 else if (rate == 8000)
1033 else if (rate == 48000)
1036 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1042 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1045 struct snd_pcm_runtime *runtime = substream->runtime;
1048 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1051 if (snd_pcm_format_width(runtime->format) == 16)
1052 CTRL |= 0x00000008; /* 16-bit data */
1053 if (!snd_pcm_format_unsigned(runtime->format))
1054 CTRL |= 0x00000002; /* signed data */
1055 if (runtime->channels > 1)
1056 CTRL |= 0x00000004; /* stereo data */
1064 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1068 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1069 struct snd_pcm_substream *s;
1070 unsigned int what, whati;
1071 struct snd_ali_voice *pvoice, *evoice;
1076 case SNDRV_PCM_TRIGGER_START:
1077 case SNDRV_PCM_TRIGGER_RESUME:
1080 case SNDRV_PCM_TRIGGER_STOP:
1081 case SNDRV_PCM_TRIGGER_SUSPEND:
1089 snd_pcm_group_for_each_entry(s, substream) {
1090 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1091 pvoice = s->runtime->private_data;
1092 evoice = pvoice->extra;
1093 what |= 1 << (pvoice->number & 0x1f);
1095 whati |= 1 << (pvoice->number & 0x1f);
1097 whati |= 1 << (evoice->number & 0x1f);
1098 what |= 1 << (evoice->number & 0x1f);
1101 pvoice->running = 1;
1103 evoice->running = 1;
1105 pvoice->running = 0;
1107 evoice->running = 0;
1109 snd_pcm_trigger_done(s, substream);
1112 spin_lock(&codec->reg_lock);
1114 outl(what, ALI_REG(codec, ALI_STOP));
1115 val = inl(ALI_REG(codec, ALI_AINTEN));
1120 outl(val, ALI_REG(codec, ALI_AINTEN));
1122 outl(what, ALI_REG(codec, ALI_START));
1123 dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati);
1124 spin_unlock(&codec->reg_lock);
1129 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1130 struct snd_pcm_hw_params *hw_params)
1132 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1133 struct snd_pcm_runtime *runtime = substream->runtime;
1134 struct snd_ali_voice *pvoice = runtime->private_data;
1135 struct snd_ali_voice *evoice = pvoice->extra;
1137 /* voice management */
1139 if (params_buffer_size(hw_params) / 2 !=
1140 params_period_size(hw_params)) {
1142 evoice = snd_ali_alloc_voice(codec,
1143 SNDRV_ALI_VOICE_TYPE_PCM,
1147 pvoice->extra = evoice;
1148 evoice->substream = substream;
1152 snd_ali_free_voice(codec, evoice);
1153 pvoice->extra = evoice = NULL;
1160 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1162 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1163 struct snd_pcm_runtime *runtime = substream->runtime;
1164 struct snd_ali_voice *pvoice = runtime->private_data;
1165 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1168 snd_ali_free_voice(codec, evoice);
1169 pvoice->extra = NULL;
1174 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1176 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1177 struct snd_pcm_runtime *runtime = substream->runtime;
1178 struct snd_ali_voice *pvoice = runtime->private_data;
1179 struct snd_ali_voice *evoice = pvoice->extra;
1190 dev_dbg(codec->card->dev, "playback_prepare ...\n");
1192 spin_lock_irq(&codec->reg_lock);
1194 /* set Delta (rate) value */
1195 Delta = snd_ali_convert_rate(runtime->rate, 0);
1197 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1198 pvoice->number == ALI_PCM_IN_CHANNEL)
1199 snd_ali_disable_special_channel(codec, pvoice->number);
1200 else if (codec->spdif_support &&
1201 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1202 ALI_SPDIF_OUT_CH_ENABLE)
1203 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1204 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1208 /* set Loop Back Address */
1209 LBA = runtime->dma_addr;
1211 /* set interrupt count size */
1212 pvoice->count = runtime->period_size;
1214 /* set target ESO for channel */
1215 pvoice->eso = runtime->buffer_size;
1217 dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n",
1218 pvoice->eso, pvoice->count);
1220 /* set ESO to capture first MIDLP interrupt */
1221 ESO = pvoice->eso -1;
1223 CTRL = snd_ali_control_mode(substream);
1229 dev_dbg(codec->card->dev, "playback_prepare:\n");
1230 dev_dbg(codec->card->dev,
1231 "ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1232 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1233 snd_ali_write_voice_regs(codec,
1246 evoice->count = pvoice->count;
1247 evoice->eso = pvoice->count << 1;
1248 ESO = evoice->eso - 1;
1249 snd_ali_write_voice_regs(codec,
1262 spin_unlock_irq(&codec->reg_lock);
1267 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1269 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1270 struct snd_pcm_runtime *runtime = substream->runtime;
1271 struct snd_ali_voice *pvoice = runtime->private_data;
1282 spin_lock_irq(&codec->reg_lock);
1284 dev_dbg(codec->card->dev, "ali_prepare...\n");
1286 snd_ali_enable_special_channel(codec,pvoice->number);
1288 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1289 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1290 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1292 /* Prepare capture intr channel */
1293 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1297 spin_unlock_irq(&codec->reg_lock);
1298 if (codec->revision != ALI_5451_V02)
1301 rate = snd_ali_get_spdif_in_rate(codec);
1303 dev_warn(codec->card->dev,
1304 "ali_capture_prepare: spdif rate detect err!\n");
1307 spin_lock_irq(&codec->reg_lock);
1308 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1309 if (bValue & 0x10) {
1310 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1311 dev_warn(codec->card->dev,
1312 "clear SPDIF parity error flag.\n");
1316 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1319 /* set target ESO for channel */
1320 pvoice->eso = runtime->buffer_size;
1322 /* set interrupt count size */
1323 pvoice->count = runtime->period_size;
1325 /* set Loop Back Address */
1326 LBA = runtime->dma_addr;
1328 /* set ESO to capture first MIDLP interrupt */
1329 ESO = pvoice->eso - 1;
1330 CTRL = snd_ali_control_mode(substream);
1336 snd_ali_write_voice_regs( codec,
1349 spin_unlock_irq(&codec->reg_lock);
1355 static snd_pcm_uframes_t
1356 snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1358 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1359 struct snd_pcm_runtime *runtime = substream->runtime;
1360 struct snd_ali_voice *pvoice = runtime->private_data;
1363 spin_lock(&codec->reg_lock);
1364 if (!pvoice->running) {
1365 spin_unlock(&codec->reg_lock);
1368 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1369 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1370 spin_unlock(&codec->reg_lock);
1371 dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso);
1373 cso %= runtime->buffer_size;
1378 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1380 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1381 struct snd_pcm_runtime *runtime = substream->runtime;
1382 struct snd_ali_voice *pvoice = runtime->private_data;
1385 spin_lock(&codec->reg_lock);
1386 if (!pvoice->running) {
1387 spin_unlock(&codec->reg_lock);
1390 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1391 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1392 spin_unlock(&codec->reg_lock);
1394 cso %= runtime->buffer_size;
1398 static const struct snd_pcm_hardware snd_ali_playback =
1400 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1401 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1402 SNDRV_PCM_INFO_MMAP_VALID |
1403 SNDRV_PCM_INFO_RESUME |
1404 SNDRV_PCM_INFO_SYNC_START),
1405 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1406 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1407 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1412 .buffer_bytes_max = (256*1024),
1413 .period_bytes_min = 64,
1414 .period_bytes_max = (256*1024),
1416 .periods_max = 1024,
1421 * Capture support device description
1424 static const struct snd_pcm_hardware snd_ali_capture =
1426 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1427 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1428 SNDRV_PCM_INFO_MMAP_VALID |
1429 SNDRV_PCM_INFO_RESUME |
1430 SNDRV_PCM_INFO_SYNC_START),
1431 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1432 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1433 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1438 .buffer_bytes_max = (128*1024),
1439 .period_bytes_min = 64,
1440 .period_bytes_max = (128*1024),
1442 .periods_max = 1024,
1446 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1448 struct snd_ali_voice *pvoice = runtime->private_data;
1451 snd_ali_free_voice(pvoice->codec, pvoice);
1454 static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1455 int channel, const struct snd_pcm_hardware *phw)
1457 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1458 struct snd_pcm_runtime *runtime = substream->runtime;
1459 struct snd_ali_voice *pvoice;
1461 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1466 pvoice->substream = substream;
1467 runtime->private_data = pvoice;
1468 runtime->private_free = snd_ali_pcm_free_substream;
1471 snd_pcm_set_sync(substream);
1472 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1477 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1479 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1482 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1484 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1487 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1492 static int snd_ali_close(struct snd_pcm_substream *substream)
1494 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1495 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1497 snd_ali_disable_special_channel(codec,pvoice->number);
1502 static const struct snd_pcm_ops snd_ali_playback_ops = {
1503 .open = snd_ali_playback_open,
1504 .close = snd_ali_playback_close,
1505 .hw_params = snd_ali_playback_hw_params,
1506 .hw_free = snd_ali_playback_hw_free,
1507 .prepare = snd_ali_playback_prepare,
1508 .trigger = snd_ali_trigger,
1509 .pointer = snd_ali_playback_pointer,
1512 static const struct snd_pcm_ops snd_ali_capture_ops = {
1513 .open = snd_ali_capture_open,
1514 .close = snd_ali_close,
1515 .prepare = snd_ali_prepare,
1516 .trigger = snd_ali_trigger,
1517 .pointer = snd_ali_pointer,
1524 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1525 struct snd_pcm_hw_params *hw_params)
1527 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1528 unsigned int modem_num = chip->num_of_codecs - 1;
1529 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1530 params_rate(hw_params));
1531 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1535 static const struct snd_pcm_hardware snd_ali_modem =
1537 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1538 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1539 SNDRV_PCM_INFO_MMAP_VALID |
1540 SNDRV_PCM_INFO_RESUME |
1541 SNDRV_PCM_INFO_SYNC_START),
1542 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1543 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1544 SNDRV_PCM_RATE_16000),
1549 .buffer_bytes_max = (256*1024),
1550 .period_bytes_min = 64,
1551 .period_bytes_max = (256*1024),
1553 .periods_max = 1024,
1557 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1560 static const unsigned int rates[] = {8000, 9600, 12000, 16000};
1561 static const struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1562 .count = ARRAY_SIZE(rates),
1566 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1570 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1571 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1574 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1576 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1579 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1581 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1584 static const struct snd_pcm_ops snd_ali_modem_playback_ops = {
1585 .open = snd_ali_modem_playback_open,
1586 .close = snd_ali_close,
1587 .hw_params = snd_ali_modem_hw_params,
1588 .prepare = snd_ali_prepare,
1589 .trigger = snd_ali_trigger,
1590 .pointer = snd_ali_pointer,
1593 static const struct snd_pcm_ops snd_ali_modem_capture_ops = {
1594 .open = snd_ali_modem_capture_open,
1595 .close = snd_ali_close,
1596 .hw_params = snd_ali_modem_hw_params,
1597 .prepare = snd_ali_prepare,
1598 .trigger = snd_ali_trigger,
1599 .pointer = snd_ali_pointer,
1603 struct ali_pcm_description {
1605 unsigned int playback_num;
1606 unsigned int capture_num;
1607 const struct snd_pcm_ops *playback_ops;
1608 const struct snd_pcm_ops *capture_ops;
1609 unsigned short class;
1613 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1615 struct snd_ali *codec = pcm->private_data;
1616 codec->pcm[pcm->device] = NULL;
1620 static int snd_ali_pcm(struct snd_ali *codec, int device,
1621 struct ali_pcm_description *desc)
1623 struct snd_pcm *pcm;
1626 err = snd_pcm_new(codec->card, desc->name, device,
1627 desc->playback_num, desc->capture_num, &pcm);
1629 dev_err(codec->card->dev,
1630 "snd_ali_pcm: err called snd_pcm_new.\n");
1633 pcm->private_data = codec;
1634 pcm->private_free = snd_ali_pcm_free;
1635 if (desc->playback_ops)
1636 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1637 desc->playback_ops);
1638 if (desc->capture_ops)
1639 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1642 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1643 &codec->pci->dev, 64*1024, 128*1024);
1645 pcm->info_flags = 0;
1646 pcm->dev_class = desc->class;
1647 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1648 strcpy(pcm->name, desc->name);
1649 codec->pcm[0] = pcm;
1653 static struct ali_pcm_description ali_pcms[] = {
1654 { .name = "ALI 5451",
1655 .playback_num = ALI_CHANNELS,
1657 .playback_ops = &snd_ali_playback_ops,
1658 .capture_ops = &snd_ali_capture_ops
1660 { .name = "ALI 5451 modem",
1663 .playback_ops = &snd_ali_modem_playback_ops,
1664 .capture_ops = &snd_ali_modem_capture_ops,
1665 .class = SNDRV_PCM_CLASS_MODEM
1669 static int snd_ali_build_pcms(struct snd_ali *codec)
1672 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1673 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1681 #define ALI5451_SPDIF(xname, xindex, value) \
1682 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1683 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1684 .put = snd_ali5451_spdif_put, .private_value = value}
1686 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
1688 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1689 struct snd_ctl_elem_value *ucontrol)
1691 struct snd_ali *codec = kcontrol->private_data;
1692 unsigned int spdif_enable;
1694 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1696 spin_lock_irq(&codec->reg_lock);
1697 switch (kcontrol->private_value) {
1699 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1702 spdif_enable = ((codec->spdif_mask & 0x02) &&
1703 (codec->spdif_mask & 0x04)) ? 1 : 0;
1706 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1711 ucontrol->value.integer.value[0] = spdif_enable;
1712 spin_unlock_irq(&codec->reg_lock);
1716 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_value *ucontrol)
1719 struct snd_ali *codec = kcontrol->private_data;
1720 unsigned int change = 0, spdif_enable = 0;
1722 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1724 spin_lock_irq(&codec->reg_lock);
1725 switch (kcontrol->private_value) {
1727 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1728 change = change ^ spdif_enable;
1731 codec->spdif_mask |= 0x02;
1732 snd_ali_enable_spdif_out(codec);
1734 codec->spdif_mask &= ~(0x02);
1735 codec->spdif_mask &= ~(0x04);
1736 snd_ali_disable_spdif_out(codec);
1741 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1742 change = change ^ spdif_enable;
1743 if (change && (codec->spdif_mask & 0x02)) {
1745 codec->spdif_mask |= 0x04;
1746 snd_ali_enable_spdif_chnout(codec);
1748 codec->spdif_mask &= ~(0x04);
1749 snd_ali_disable_spdif_chnout(codec);
1754 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1755 change = change ^ spdif_enable;
1758 codec->spdif_mask |= 0x01;
1759 snd_ali_enable_spdif_in(codec);
1761 codec->spdif_mask &= ~(0x01);
1762 snd_ali_disable_spdif_in(codec);
1769 spin_unlock_irq(&codec->reg_lock);
1774 static const struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
1775 /* spdif aplayback switch */
1776 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1777 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1778 /* spdif out to spdif channel */
1779 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1780 /* spdif in from spdif channel */
1781 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1784 static int snd_ali_mixer(struct snd_ali *codec)
1786 struct snd_ac97_template ac97;
1789 static const struct snd_ac97_bus_ops ops = {
1790 .write = snd_ali_codec_write,
1791 .read = snd_ali_codec_read,
1794 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1798 memset(&ac97, 0, sizeof(ac97));
1799 ac97.private_data = codec;
1801 for (i = 0; i < codec->num_of_codecs; i++) {
1803 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1805 dev_err(codec->card->dev,
1806 "ali mixer %d creating error.\n", i);
1809 codec->num_of_codecs = 1;
1814 if (codec->spdif_support) {
1815 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1816 err = snd_ctl_add(codec->card,
1817 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1825 static int ali_suspend(struct device *dev)
1827 struct snd_card *card = dev_get_drvdata(dev);
1828 struct snd_ali *chip = card->private_data;
1829 struct snd_ali_image *im = &chip->image;
1832 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1833 for (i = 0; i < chip->num_of_codecs; i++)
1834 snd_ac97_suspend(chip->ac97[i]);
1836 spin_lock_irq(&chip->reg_lock);
1838 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1839 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1840 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1842 /* disable all IRQ bits */
1843 outl(0, ALI_REG(chip, ALI_MISCINT));
1845 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1846 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1848 im->regs[i] = inl(ALI_REG(chip, i*4));
1851 for (i = 0; i < ALI_CHANNELS; i++) {
1852 outb(i, ALI_REG(chip, ALI_GC_CIR));
1853 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1854 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1857 /* stop all HW channel */
1858 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1860 spin_unlock_irq(&chip->reg_lock);
1864 static int ali_resume(struct device *dev)
1866 struct snd_card *card = dev_get_drvdata(dev);
1867 struct snd_ali *chip = card->private_data;
1868 struct snd_ali_image *im = &chip->image;
1871 spin_lock_irq(&chip->reg_lock);
1873 for (i = 0; i < ALI_CHANNELS; i++) {
1874 outb(i, ALI_REG(chip, ALI_GC_CIR));
1875 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1876 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1879 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1880 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1883 outl(im->regs[i], ALI_REG(chip, i*4));
1886 /* start HW channel */
1887 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1888 /* restore IRQ enable bits */
1889 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1891 spin_unlock_irq(&chip->reg_lock);
1893 for (i = 0 ; i < chip->num_of_codecs; i++)
1894 snd_ac97_resume(chip->ac97[i]);
1896 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1900 static DEFINE_SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
1902 static void snd_ali_free(struct snd_card *card)
1904 struct snd_ali *codec = card->private_data;
1906 if (codec->hw_initialized)
1907 snd_ali_disable_address_interrupt(codec);
1908 pci_dev_put(codec->pci_m1533);
1909 pci_dev_put(codec->pci_m7101);
1912 static int snd_ali_chip_init(struct snd_ali *codec)
1914 unsigned int legacy;
1916 struct pci_dev *pci_dev;
1918 dev_dbg(codec->card->dev, "chip initializing ...\n");
1920 if (snd_ali_reset_5451(codec)) {
1921 dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
1925 if (codec->revision == ALI_5451_V02) {
1926 pci_dev = codec->pci_m1533;
1927 pci_read_config_byte(pci_dev, 0x59, &temp);
1929 pci_write_config_byte(pci_dev, 0x59, temp);
1931 pci_dev = codec->pci_m7101;
1932 pci_read_config_byte(pci_dev, 0xb8, &temp);
1934 pci_write_config_byte(pci_dev, 0xB8, temp);
1937 pci_read_config_dword(codec->pci, 0x44, &legacy);
1938 legacy &= 0xff00ff00;
1939 legacy |= 0x000800aa;
1940 pci_write_config_dword(codec->pci, 0x44, legacy);
1942 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
1943 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
1944 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
1945 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
1946 outb(0x10, ALI_REG(codec, ALI_MPUR2));
1948 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
1949 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
1950 AC97_EXTENDED_STATUS);
1951 if (codec->spdif_support) {
1952 snd_ali_enable_spdif_out(codec);
1953 codec->spdif_mask = 0x00000002;
1956 codec->num_of_codecs = 1;
1958 /* secondary codec - modem */
1959 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
1960 codec->num_of_codecs++;
1961 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
1962 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
1963 ALI_SCTRL_LINE_OUT_EN),
1964 ALI_REG(codec, ALI_SCTRL));
1967 dev_dbg(codec->card->dev, "chip initialize succeed.\n");
1972 /* proc for register dump */
1973 static void snd_ali_proc_read(struct snd_info_entry *entry,
1974 struct snd_info_buffer *buf)
1976 struct snd_ali *codec = entry->private_data;
1978 for (i = 0; i < 256 ; i+= 4)
1979 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
1982 static void snd_ali_proc_init(struct snd_ali *codec)
1984 snd_card_ro_proc_new(codec->card, "ali5451", codec, snd_ali_proc_read);
1987 static int snd_ali_resources(struct snd_ali *codec)
1991 dev_dbg(codec->card->dev, "resources allocation ...\n");
1992 err = pci_request_regions(codec->pci, "ALI 5451");
1995 codec->port = pci_resource_start(codec->pci, 0);
1997 if (devm_request_irq(&codec->pci->dev, codec->pci->irq,
1998 snd_ali_card_interrupt,
1999 IRQF_SHARED, KBUILD_MODNAME, codec)) {
2000 dev_err(codec->card->dev, "Unable to request irq.\n");
2003 codec->irq = codec->pci->irq;
2004 codec->card->sync_irq = codec->irq;
2005 dev_dbg(codec->card->dev, "resources allocated.\n");
2009 static int snd_ali_create(struct snd_card *card,
2010 struct pci_dev *pci,
2014 struct snd_ali *codec = card->private_data;
2016 unsigned short cmdw;
2018 dev_dbg(card->dev, "creating ...\n");
2020 /* enable PCI device */
2021 err = pcim_enable_device(pci);
2024 /* check, if we can restrict PCI DMA transfers to 31 bits */
2025 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(31))) {
2027 "architecture does not support 31bit PCI busmaster DMA\n");
2031 spin_lock_init(&codec->reg_lock);
2032 spin_lock_init(&codec->voice_alloc);
2037 codec->revision = pci->revision;
2038 codec->spdif_support = spdif_support;
2040 if (pcm_streams < 1)
2042 if (pcm_streams > 32)
2045 pci_set_master(pci);
2046 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2047 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2048 cmdw |= PCI_COMMAND_IO;
2049 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2052 if (snd_ali_resources(codec))
2054 card->private_free = snd_ali_free;
2056 codec->synth.chmap = 0;
2057 codec->synth.chcnt = 0;
2058 codec->spdif_mask = 0;
2059 codec->synth.synthcount = 0;
2061 if (codec->revision == ALI_5451_V02)
2062 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2064 codec->chregs.regs.ac97read = ALI_AC97_READ;
2065 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2067 codec->chregs.regs.start = ALI_START;
2068 codec->chregs.regs.stop = ALI_STOP;
2069 codec->chregs.regs.aint = ALI_AINT;
2070 codec->chregs.regs.ainten = ALI_AINTEN;
2072 codec->chregs.data.start = 0x00;
2073 codec->chregs.data.stop = 0x00;
2074 codec->chregs.data.aint = 0x00;
2075 codec->chregs.data.ainten = 0x00;
2077 /* M1533: southbridge */
2078 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2079 if (!codec->pci_m1533) {
2080 dev_err(card->dev, "cannot find ALi 1533 chip.\n");
2083 /* M7101: power management */
2084 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2085 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2086 dev_err(card->dev, "cannot find ALi 7101 chip.\n");
2090 /* initialise synth voices*/
2091 for (i = 0; i < ALI_CHANNELS; i++)
2092 codec->synth.voices[i].number = i;
2094 err = snd_ali_chip_init(codec);
2096 dev_err(card->dev, "ali create: chip init error.\n");
2100 snd_ali_enable_address_interrupt(codec);
2101 codec->hw_initialized = 1;
2105 static int __snd_ali_probe(struct pci_dev *pci,
2106 const struct pci_device_id *pci_id)
2108 struct snd_card *card;
2109 struct snd_ali *codec;
2112 dev_dbg(&pci->dev, "probe ...\n");
2114 err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
2115 sizeof(*codec), &card);
2118 codec = card->private_data;
2120 err = snd_ali_create(card, pci, pcm_channels, spdif);
2124 dev_dbg(&pci->dev, "mixer building ...\n");
2125 err = snd_ali_mixer(codec);
2129 dev_dbg(&pci->dev, "pcm building ...\n");
2130 err = snd_ali_build_pcms(codec);
2134 snd_ali_proc_init(codec);
2136 strcpy(card->driver, "ALI5451");
2137 strcpy(card->shortname, "ALI 5451");
2139 sprintf(card->longname, "%s at 0x%lx, irq %i",
2140 card->shortname, codec->port, codec->irq);
2142 dev_dbg(&pci->dev, "register card.\n");
2143 err = snd_card_register(card);
2147 pci_set_drvdata(pci, card);
2151 static int snd_ali_probe(struct pci_dev *pci,
2152 const struct pci_device_id *pci_id)
2154 return snd_card_free_on_error(&pci->dev, __snd_ali_probe(pci, pci_id));
2157 static struct pci_driver ali5451_driver = {
2158 .name = KBUILD_MODNAME,
2159 .id_table = snd_ali_ids,
2160 .probe = snd_ali_probe,
2166 module_pci_driver(ali5451_driver);