powerpc/ps3: Make ps3av_set_video_mode mode ID signed
[linux/fpc-iii.git] / sound / pci / ali5451 / ali5451.c
blob1a0fd65ec2809478d71f037f310f6a3f36a1823b
1 /*
2 * Matt Wu <Matt_Wu@acersoftech.com.cn>
3 * Apr 26, 2001
4 * Routines for control of ALi pci audio M5451
6 * BUGS:
7 * --
9 * TODO:
10 * --
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 <asm/io.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/init.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/moduleparam.h>
35 #include <linux/dma-mapping.h>
36 #include <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/info.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/mpu401.h>
41 #include <sound/initval.h>
43 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
44 MODULE_DESCRIPTION("ALI M5451");
45 MODULE_LICENSE("GPL");
46 MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
48 static int index = SNDRV_DEFAULT_IDX1; /* Index */
49 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
50 static int pcm_channels = 32;
51 static int spdif;
53 module_param(index, int, 0444);
54 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
55 module_param(id, charp, 0444);
56 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
57 module_param(pcm_channels, int, 0444);
58 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
59 module_param(spdif, bool, 0444);
60 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
62 /* just for backward compatibility */
63 static int enable;
64 module_param(enable, bool, 0444);
68 * Debug part definitions
71 /* #define ALI_DEBUG */
73 #ifdef ALI_DEBUG
74 #define snd_ali_printk(format, args...) printk(KERN_DEBUG format, ##args);
75 #else
76 #define snd_ali_printk(format, args...)
77 #endif
80 * Constants definition
83 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
86 #define ALI_CHANNELS 32
88 #define ALI_PCM_IN_CHANNEL 31
89 #define ALI_SPDIF_IN_CHANNEL 19
90 #define ALI_SPDIF_OUT_CHANNEL 15
91 #define ALI_CENTER_CHANNEL 24
92 #define ALI_LEF_CHANNEL 23
93 #define ALI_SURR_LEFT_CHANNEL 26
94 #define ALI_SURR_RIGHT_CHANNEL 25
95 #define ALI_MODEM_IN_CHANNEL 21
96 #define ALI_MODEM_OUT_CHANNEL 20
98 #define SNDRV_ALI_VOICE_TYPE_PCM 01
99 #define SNDRV_ALI_VOICE_TYPE_OTH 02
101 #define ALI_5451_V02 0x02
104 * Direct Registers
107 #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
108 #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
109 #define ALI_LEGACY_DMAR11 0x0b /* MOD */
110 #define ALI_LEGACY_DMAR15 0x0f /* MMR */
111 #define ALI_MPUR0 0x20
112 #define ALI_MPUR1 0x21
113 #define ALI_MPUR2 0x22
114 #define ALI_MPUR3 0x23
116 #define ALI_AC97_WRITE 0x40
117 #define ALI_AC97_READ 0x44
119 #define ALI_SCTRL 0x48
120 #define ALI_SPDIF_OUT_ENABLE 0x20
121 #define ALI_SCTRL_LINE_IN2 (1 << 9)
122 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
123 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
124 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
125 #define ALI_SCTRL_CODEC1_READY (1 << 24)
126 #define ALI_SCTRL_CODEC2_READY (1 << 25)
127 #define ALI_AC97_GPIO 0x4c
128 #define ALI_AC97_GPIO_ENABLE 0x8000
129 #define ALI_AC97_GPIO_DATA_SHIFT 16
130 #define ALI_SPDIF_CS 0x70
131 #define ALI_SPDIF_CTRL 0x74
132 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
133 #define ALI_SPDIF_IN_CH_STATUS 0x40
134 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
135 #define ALI_START 0x80
136 #define ALI_STOP 0x84
137 #define ALI_CSPF 0x90
138 #define ALI_AINT 0x98
139 #define ALI_GC_CIR 0xa0
140 #define ENDLP_IE 0x00001000
141 #define MIDLP_IE 0x00002000
142 #define ALI_AINTEN 0xa4
143 #define ALI_VOLUME 0xa8
144 #define ALI_SBDELTA_DELTA_R 0xac
145 #define ALI_MISCINT 0xb0
146 #define ADDRESS_IRQ 0x00000020
147 #define TARGET_REACHED 0x00008000
148 #define MIXER_OVERFLOW 0x00000800
149 #define MIXER_UNDERFLOW 0x00000400
150 #define GPIO_IRQ 0x01000000
151 #define ALI_SBBL_SBCL 0xc0
152 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
153 #define ALI_STIMER 0xc8
154 #define ALI_GLOBAL_CONTROL 0xd4
155 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
156 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
157 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
158 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
159 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
161 #define ALI_CSO_ALPHA_FMS 0xe0
162 #define ALI_LBA 0xe4
163 #define ALI_ESO_DELTA 0xe8
164 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
165 #define ALI_EBUF1 0xf4
166 #define ALI_EBUF2 0xf8
168 #define ALI_REG(codec, x) ((codec)->port + x)
170 #define MAX_CODECS 2
173 struct snd_ali;
174 struct snd_ali_voice;
176 struct snd_ali_channel_control {
177 /* register data */
178 struct REGDATA {
179 unsigned int start;
180 unsigned int stop;
181 unsigned int aint;
182 unsigned int ainten;
183 } data;
185 /* register addresses */
186 struct REGS {
187 unsigned int start;
188 unsigned int stop;
189 unsigned int aint;
190 unsigned int ainten;
191 unsigned int ac97read;
192 unsigned int ac97write;
193 } regs;
197 struct snd_ali_voice {
198 unsigned int number;
199 unsigned int use :1,
200 pcm :1,
201 midi :1,
202 mode :1,
203 synth :1,
204 running :1;
206 /* PCM data */
207 struct snd_ali *codec;
208 struct snd_pcm_substream *substream;
209 struct snd_ali_voice *extra;
211 int eso; /* final ESO value for channel */
212 int count; /* runtime->period_size */
214 /* --- */
216 void *private_data;
217 void (*private_free)(void *private_data);
221 struct snd_alidev {
223 struct snd_ali_voice voices[ALI_CHANNELS];
225 unsigned int chcnt; /* num of opened channels */
226 unsigned int chmap; /* bitmap for opened channels */
227 unsigned int synthcount;
232 #define ALI_GLOBAL_REGS 56
233 #define ALI_CHANNEL_REGS 8
234 struct snd_ali_image {
235 u32 regs[ALI_GLOBAL_REGS];
236 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
240 struct snd_ali {
241 int irq;
242 unsigned long port;
243 unsigned char revision;
245 unsigned int hw_initialized :1;
246 unsigned int spdif_support :1;
248 struct pci_dev *pci;
249 struct pci_dev *pci_m1533;
250 struct pci_dev *pci_m7101;
252 struct snd_card *card;
253 struct snd_pcm *pcm[MAX_CODECS];
254 struct snd_alidev synth;
255 struct snd_ali_channel_control chregs;
257 /* S/PDIF Mask */
258 unsigned int spdif_mask;
260 unsigned int spurious_irq_count;
261 unsigned int spurious_irq_max_delta;
263 unsigned int num_of_codecs;
265 struct snd_ac97_bus *ac97_bus;
266 struct snd_ac97 *ac97[MAX_CODECS];
267 unsigned short ac97_ext_id;
268 unsigned short ac97_ext_status;
270 spinlock_t reg_lock;
271 spinlock_t voice_alloc;
273 #ifdef CONFIG_PM
274 struct snd_ali_image *image;
275 #endif
278 static struct pci_device_id snd_ali_ids[] = {
279 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
280 {0, }
282 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
284 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
285 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
286 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
287 unsigned short);
290 * AC97 ACCESS
293 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
294 unsigned int port)
296 return (unsigned int)inl(ALI_REG(codec, port));
299 static inline void snd_ali_5451_poke(struct snd_ali *codec,
300 unsigned int port,
301 unsigned int val)
303 outl((unsigned int)val, ALI_REG(codec, port));
306 static int snd_ali_codec_ready(struct snd_ali *codec,
307 unsigned int port)
309 unsigned long end_time;
310 unsigned int res;
312 end_time = jiffies + msecs_to_jiffies(250);
313 do {
314 res = snd_ali_5451_peek(codec,port);
315 if (!(res & 0x8000))
316 return 0;
317 schedule_timeout_uninterruptible(1);
318 } while (time_after_eq(end_time, jiffies));
319 snd_ali_5451_poke(codec, port, res & ~0x8000);
320 snd_printdd("ali_codec_ready: codec is not ready.\n ");
321 return -EIO;
324 static int snd_ali_stimer_ready(struct snd_ali *codec)
326 unsigned long end_time;
327 unsigned long dwChk1,dwChk2;
329 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
330 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
332 end_time = jiffies + msecs_to_jiffies(250);
333 do {
334 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
335 if (dwChk2 != dwChk1)
336 return 0;
337 schedule_timeout_uninterruptible(1);
338 } while (time_after_eq(end_time, jiffies));
339 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
340 return -EIO;
343 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
344 unsigned short reg,
345 unsigned short val)
347 unsigned int dwVal;
348 unsigned int port;
350 if (reg >= 0x80) {
351 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
352 return;
355 port = codec->chregs.regs.ac97write;
357 if (snd_ali_codec_ready(codec, port) < 0)
358 return;
359 if (snd_ali_stimer_ready(codec) < 0)
360 return;
362 dwVal = (unsigned int) (reg & 0xff);
363 dwVal |= 0x8000 | (val << 16);
364 if (secondary)
365 dwVal |= 0x0080;
366 if (codec->revision == ALI_5451_V02)
367 dwVal |= 0x0100;
369 snd_ali_5451_poke(codec, port, dwVal);
371 return ;
374 static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
375 int secondary,
376 unsigned short reg)
378 unsigned int dwVal;
379 unsigned int port;
381 if (reg >= 0x80) {
382 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
383 return ~0;
386 port = codec->chregs.regs.ac97read;
388 if (snd_ali_codec_ready(codec, port) < 0)
389 return ~0;
390 if (snd_ali_stimer_ready(codec) < 0)
391 return ~0;
393 dwVal = (unsigned int) (reg & 0xff);
394 dwVal |= 0x8000; /* bit 15*/
395 if (secondary)
396 dwVal |= 0x0080;
398 snd_ali_5451_poke(codec, port, dwVal);
400 if (snd_ali_stimer_ready(codec) < 0)
401 return ~0;
402 if (snd_ali_codec_ready(codec, port) < 0)
403 return ~0;
405 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
408 static void snd_ali_codec_write(struct snd_ac97 *ac97,
409 unsigned short reg,
410 unsigned short val )
412 struct snd_ali *codec = ac97->private_data;
414 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
415 if (reg == AC97_GPIO_STATUS) {
416 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
417 ALI_REG(codec, ALI_AC97_GPIO));
418 return;
420 snd_ali_codec_poke(codec, ac97->num, reg, val);
421 return ;
425 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
426 unsigned short reg)
428 struct snd_ali *codec = ac97->private_data;
430 snd_ali_printk("codec_read reg=%xh.\n", reg);
431 return snd_ali_codec_peek(codec, ac97->num, reg);
435 * AC97 Reset
438 static int snd_ali_reset_5451(struct snd_ali *codec)
440 struct pci_dev *pci_dev;
441 unsigned short wCount, wReg;
442 unsigned int dwVal;
444 pci_dev = codec->pci_m1533;
445 if (pci_dev) {
446 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
447 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
448 udelay(5000);
449 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
450 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
451 udelay(5000);
454 pci_dev = codec->pci;
455 pci_read_config_dword(pci_dev, 0x44, &dwVal);
456 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
457 udelay(500);
458 pci_read_config_dword(pci_dev, 0x44, &dwVal);
459 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
460 udelay(5000);
462 wCount = 200;
463 while(wCount--) {
464 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
465 if ((wReg & 0x000f) == 0x000f)
466 return 0;
467 udelay(5000);
470 /* non-fatal if you have a non PM capable codec */
471 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
472 return 0;
475 #ifdef CODEC_RESET
477 static int snd_ali_reset_codec(struct snd_ali *codec)
479 struct pci_dev *pci_dev;
480 unsigned char bVal;
481 unsigned int dwVal;
482 unsigned short wCount, wReg;
484 pci_dev = codec->pci_m1533;
486 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
487 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
488 udelay(5000);
489 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
490 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
491 udelay(5000);
493 bVal = inb(ALI_REG(codec,ALI_SCTRL));
494 bVal |= 0x02;
495 outb(ALI_REG(codec,ALI_SCTRL),bVal);
496 udelay(5000);
497 bVal = inb(ALI_REG(codec,ALI_SCTRL));
498 bVal &= 0xfd;
499 outb(ALI_REG(codec,ALI_SCTRL),bVal);
500 udelay(15000);
502 wCount = 200;
503 while (wCount--) {
504 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
505 if ((wReg & 0x000f) == 0x000f)
506 return 0;
507 udelay(5000);
509 return -1;
512 #endif
515 * ALI 5451 Controller
518 static void snd_ali_enable_special_channel(struct snd_ali *codec,
519 unsigned int channel)
521 unsigned long dwVal;
523 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
524 dwVal |= 1 << (channel & 0x0000001f);
525 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
528 static void snd_ali_disable_special_channel(struct snd_ali *codec,
529 unsigned int channel)
531 unsigned long dwVal;
533 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
534 dwVal &= ~(1 << (channel & 0x0000001f));
535 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
538 static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
540 unsigned int gc;
542 gc = inl(ALI_REG(codec, ALI_GC_CIR));
543 gc |= ENDLP_IE;
544 gc |= MIDLP_IE;
545 outl( gc, ALI_REG(codec, ALI_GC_CIR));
548 static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
550 unsigned int gc;
552 gc = inl(ALI_REG(codec, ALI_GC_CIR));
553 gc &= ~ENDLP_IE;
554 gc &= ~MIDLP_IE;
555 outl(gc, ALI_REG(codec, ALI_GC_CIR));
558 #if 0 /* not used */
559 static void snd_ali_enable_voice_irq(struct snd_ali *codec,
560 unsigned int channel)
562 unsigned int mask;
563 struct snd_ali_channel_control *pchregs = &(codec->chregs);
565 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
567 mask = 1 << (channel & 0x1f);
568 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
569 pchregs->data.ainten |= mask;
570 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
572 #endif
574 static void snd_ali_disable_voice_irq(struct snd_ali *codec,
575 unsigned int channel)
577 unsigned int mask;
578 struct snd_ali_channel_control *pchregs = &(codec->chregs);
580 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
582 mask = 1 << (channel & 0x1f);
583 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
584 pchregs->data.ainten &= ~mask;
585 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
588 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
590 unsigned int idx = channel & 0x1f;
592 if (codec->synth.chcnt >= ALI_CHANNELS){
593 snd_printk(KERN_ERR
594 "ali_alloc_pcm_channel: no free channels.\n");
595 return -1;
598 if (!(codec->synth.chmap & (1 << idx))) {
599 codec->synth.chmap |= 1 << idx;
600 codec->synth.chcnt++;
601 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
602 return idx;
604 return -1;
607 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
609 int idx;
610 int result = -1;
612 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
614 /* recording */
615 if (rec) {
616 if (codec->spdif_support &&
617 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
618 ALI_SPDIF_IN_SUPPORT))
619 idx = ALI_SPDIF_IN_CHANNEL;
620 else
621 idx = ALI_PCM_IN_CHANNEL;
623 result = snd_ali_alloc_pcm_channel(codec, idx);
624 if (result >= 0)
625 return result;
626 else {
627 snd_printk(KERN_ERR "ali_find_free_channel: "
628 "record channel is busy now.\n");
629 return -1;
633 /* playback... */
634 if (codec->spdif_support &&
635 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
636 ALI_SPDIF_OUT_CH_ENABLE)) {
637 idx = ALI_SPDIF_OUT_CHANNEL;
638 result = snd_ali_alloc_pcm_channel(codec, idx);
639 if (result >= 0)
640 return result;
641 else
642 snd_printk(KERN_ERR "ali_find_free_channel: "
643 "S/PDIF out channel is in busy now.\n");
646 for (idx = 0; idx < ALI_CHANNELS; idx++) {
647 result = snd_ali_alloc_pcm_channel(codec, idx);
648 if (result >= 0)
649 return result;
651 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
652 return -1;
655 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
657 unsigned int idx = channel & 0x0000001f;
659 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
661 if (channel < 0 || channel >= ALI_CHANNELS)
662 return;
664 if (!(codec->synth.chmap & (1 << idx))) {
665 snd_printk(KERN_ERR "ali_free_channel_pcm: "
666 "channel %d is not in use.\n", channel);
667 return;
668 } else {
669 codec->synth.chmap &= ~(1 << idx);
670 codec->synth.chcnt--;
674 #if 0 /* not used */
675 static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
677 unsigned int mask = 1 << (channel & 0x1f);
679 snd_ali_printk("start_voice: channel=%d\n",channel);
680 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
682 #endif
684 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
686 unsigned int mask = 1 << (channel & 0x1f);
688 snd_ali_printk("stop_voice: channel=%d\n",channel);
689 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
693 * S/PDIF Part
696 static void snd_ali_delay(struct snd_ali *codec,int interval)
698 unsigned long begintimer,currenttimer;
700 begintimer = inl(ALI_REG(codec, ALI_STIMER));
701 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
703 while (currenttimer < begintimer + interval) {
704 if (snd_ali_stimer_ready(codec) < 0)
705 break;
706 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
707 cpu_relax();
711 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
713 u16 wval;
714 u16 count = 0;
715 u8 bval, R1 = 0, R2;
717 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
718 bval |= 0x1F;
719 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
721 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
722 count ++;
723 snd_ali_delay(codec, 6);
724 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
725 R1 = bval & 0x1F;
728 if (count > 50000) {
729 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
730 return;
733 for (count = 0; count <= 50000; count++) {
734 snd_ali_delay(codec, 6);
735 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
736 R2 = bval & 0x1F;
737 if (R2 != R1)
738 R1 = R2;
739 else
740 break;
743 if (count > 50000) {
744 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
745 return;
748 if (R2 >= 0x0b && R2 <= 0x0e) {
749 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
750 wval &= 0xe0f0;
751 wval |= (0x09 << 8) | 0x05;
752 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
754 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
755 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
756 } else if (R2 == 0x12) {
757 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
758 wval &= 0xe0f0;
759 wval |= (0x0e << 8) | 0x08;
760 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
762 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
763 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
767 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
769 u32 dwRate;
770 u8 bval;
772 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
773 bval &= 0x7f;
774 bval |= 0x40;
775 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
777 snd_ali_detect_spdif_rate(codec);
779 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
780 bval &= 0x0f;
782 switch (bval) {
783 case 0: dwRate = 44100; break;
784 case 1: dwRate = 48000; break;
785 case 2: dwRate = 32000; break;
786 default: dwRate = 0; break;
789 return dwRate;
792 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
794 unsigned int dwVal;
796 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
797 dwVal |= ALI_SPDIF_IN_SUPPORT;
798 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
800 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
801 dwVal |= 0x02;
802 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
804 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
807 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
809 unsigned int dwVal;
811 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
812 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
813 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
815 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
819 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
821 unsigned char bVal;
822 unsigned int dwRate;
824 switch (rate) {
825 case 32000: dwRate = 0x300; break;
826 case 48000: dwRate = 0x200; break;
827 default: dwRate = 0; break;
830 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
831 bVal &= (unsigned char)(~(1<<6));
833 bVal |= 0x80; /* select right */
834 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
835 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
837 bVal &= ~0x80; /* select left */
838 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
839 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
842 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
844 unsigned short wVal;
845 unsigned char bVal;
846 struct pci_dev *pci_dev;
848 pci_dev = codec->pci_m1533;
849 if (pci_dev == NULL)
850 return;
851 pci_read_config_byte(pci_dev, 0x61, &bVal);
852 bVal |= 0x40;
853 pci_write_config_byte(pci_dev, 0x61, bVal);
854 pci_read_config_byte(pci_dev, 0x7d, &bVal);
855 bVal |= 0x01;
856 pci_write_config_byte(pci_dev, 0x7d, bVal);
858 pci_read_config_byte(pci_dev, 0x7e, &bVal);
859 bVal &= (~0x20);
860 bVal |= 0x10;
861 pci_write_config_byte(pci_dev, 0x7e, bVal);
863 bVal = inb(ALI_REG(codec, ALI_SCTRL));
864 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
866 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
867 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
869 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
870 wVal |= ALI_SPDIF_OUT_SEL_PCM;
871 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
872 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
875 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
877 unsigned short wVal;
879 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
880 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
881 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
883 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
884 if (flag & ALI_SPDIF_OUT_NON_PCM)
885 wVal |= 0x0002;
886 else
887 wVal &= (~0x0002);
888 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
890 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
893 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
895 unsigned short wVal;
897 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
898 wVal |= ALI_SPDIF_OUT_SEL_PCM;
899 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
901 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
904 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
906 unsigned char bVal;
908 bVal = inb(ALI_REG(codec, ALI_SCTRL));
909 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
911 snd_ali_disable_spdif_chnout(codec);
914 static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
916 struct snd_ali_voice *pvoice;
917 struct snd_pcm_runtime *runtime;
918 struct snd_ali_channel_control *pchregs;
919 unsigned int old, mask;
920 #ifdef ALI_DEBUG
921 unsigned int temp, cspf;
922 #endif
924 pchregs = &(codec->chregs);
926 /* check if interrupt occurred for channel */
927 old = pchregs->data.aint;
928 mask = 1U << (channel & 0x1f);
930 if (!(old & mask))
931 return;
933 pvoice = &codec->synth.voices[channel];
934 runtime = pvoice->substream->runtime;
936 udelay(100);
937 spin_lock(&codec->reg_lock);
939 if (pvoice->pcm && pvoice->substream) {
940 /* pcm interrupt */
941 #ifdef ALI_DEBUG
942 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
943 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
944 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
945 #endif
946 if (pvoice->running) {
947 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
948 (u16)temp, cspf);
949 spin_unlock(&codec->reg_lock);
950 snd_pcm_period_elapsed(pvoice->substream);
951 spin_lock(&codec->reg_lock);
952 } else {
953 snd_ali_stop_voice(codec, channel);
954 snd_ali_disable_voice_irq(codec, channel);
956 } else if (codec->synth.voices[channel].synth) {
957 /* synth interrupt */
958 } else if (codec->synth.voices[channel].midi) {
959 /* midi interrupt */
960 } else {
961 /* unknown interrupt */
962 snd_ali_stop_voice(codec, channel);
963 snd_ali_disable_voice_irq(codec, channel);
965 spin_unlock(&codec->reg_lock);
966 outl(mask,ALI_REG(codec,pchregs->regs.aint));
967 pchregs->data.aint = old & (~mask);
970 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
972 struct snd_ali *codec = dev_id;
973 int channel;
974 unsigned int audio_int;
975 struct snd_ali_channel_control *pchregs;
977 if (codec == NULL || !codec->hw_initialized)
978 return IRQ_NONE;
980 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
981 if (!audio_int)
982 return IRQ_NONE;
984 pchregs = &(codec->chregs);
985 if (audio_int & ADDRESS_IRQ) {
986 /* get interrupt status for all channels */
987 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
988 for (channel = 0; channel < ALI_CHANNELS; channel++)
989 snd_ali_update_ptr(codec, channel);
991 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
992 ALI_REG(codec, ALI_MISCINT));
994 return IRQ_HANDLED;
998 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
999 int type, int rec, int channel)
1001 struct snd_ali_voice *pvoice;
1002 int idx;
1004 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
1006 spin_lock_irq(&codec->voice_alloc);
1007 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1008 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1009 snd_ali_find_free_channel(codec,rec);
1010 if (idx < 0) {
1011 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
1012 spin_unlock_irq(&codec->voice_alloc);
1013 return NULL;
1015 pvoice = &(codec->synth.voices[idx]);
1016 pvoice->codec = codec;
1017 pvoice->use = 1;
1018 pvoice->pcm = 1;
1019 pvoice->mode = rec;
1020 spin_unlock_irq(&codec->voice_alloc);
1021 return pvoice;
1023 spin_unlock_irq(&codec->voice_alloc);
1024 return NULL;
1028 static void snd_ali_free_voice(struct snd_ali * codec,
1029 struct snd_ali_voice *pvoice)
1031 void (*private_free)(void *);
1032 void *private_data;
1034 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1035 if (pvoice == NULL || !pvoice->use)
1036 return;
1037 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1038 spin_lock_irq(&codec->voice_alloc);
1039 private_free = pvoice->private_free;
1040 private_data = pvoice->private_data;
1041 pvoice->private_free = NULL;
1042 pvoice->private_data = NULL;
1043 if (pvoice->pcm)
1044 snd_ali_free_channel_pcm(codec, pvoice->number);
1045 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1046 pvoice->substream = NULL;
1047 spin_unlock_irq(&codec->voice_alloc);
1048 if (private_free)
1049 private_free(private_data);
1053 static void snd_ali_clear_voices(struct snd_ali *codec,
1054 unsigned int v_min,
1055 unsigned int v_max)
1057 unsigned int i;
1059 for (i = v_min; i <= v_max; i++) {
1060 snd_ali_stop_voice(codec, i);
1061 snd_ali_disable_voice_irq(codec, i);
1065 static void snd_ali_write_voice_regs(struct snd_ali *codec,
1066 unsigned int Channel,
1067 unsigned int LBA,
1068 unsigned int CSO,
1069 unsigned int ESO,
1070 unsigned int DELTA,
1071 unsigned int ALPHA_FMS,
1072 unsigned int GVSEL,
1073 unsigned int PAN,
1074 unsigned int VOL,
1075 unsigned int CTRL,
1076 unsigned int EC)
1078 unsigned int ctlcmds[4];
1080 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
1082 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1083 ctlcmds[1] = LBA;
1084 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1085 ctlcmds[3] = (GVSEL << 31) |
1086 ((PAN & 0x0000007f) << 24) |
1087 ((VOL & 0x000000ff) << 16) |
1088 ((CTRL & 0x0000000f) << 12) |
1089 (EC & 0x00000fff);
1091 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1093 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1094 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1095 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1096 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1098 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1099 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1102 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1104 unsigned int delta;
1106 if (rate < 4000)
1107 rate = 4000;
1108 if (rate > 48000)
1109 rate = 48000;
1111 if (rec) {
1112 if (rate == 44100)
1113 delta = 0x116a;
1114 else if (rate == 8000)
1115 delta = 0x6000;
1116 else if (rate == 48000)
1117 delta = 0x1000;
1118 else
1119 delta = ((48000 << 12) / rate) & 0x0000ffff;
1120 } else {
1121 if (rate == 44100)
1122 delta = 0xeb3;
1123 else if (rate == 8000)
1124 delta = 0x2ab;
1125 else if (rate == 48000)
1126 delta = 0x1000;
1127 else
1128 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1131 return delta;
1134 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1136 unsigned int CTRL;
1137 struct snd_pcm_runtime *runtime = substream->runtime;
1139 /* set ctrl mode
1140 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1142 CTRL = 0x00000001;
1143 if (snd_pcm_format_width(runtime->format) == 16)
1144 CTRL |= 0x00000008; /* 16-bit data */
1145 if (!snd_pcm_format_unsigned(runtime->format))
1146 CTRL |= 0x00000002; /* signed data */
1147 if (runtime->channels > 1)
1148 CTRL |= 0x00000004; /* stereo data */
1149 return CTRL;
1153 * PCM part
1156 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1157 int cmd)
1160 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1161 struct snd_pcm_substream *s;
1162 unsigned int what, whati, capture_flag;
1163 struct snd_ali_voice *pvoice, *evoice;
1164 unsigned int val;
1165 int do_start;
1167 switch (cmd) {
1168 case SNDRV_PCM_TRIGGER_START:
1169 case SNDRV_PCM_TRIGGER_RESUME:
1170 do_start = 1;
1171 break;
1172 case SNDRV_PCM_TRIGGER_STOP:
1173 case SNDRV_PCM_TRIGGER_SUSPEND:
1174 do_start = 0;
1175 break;
1176 default:
1177 return -EINVAL;
1180 what = whati = capture_flag = 0;
1181 snd_pcm_group_for_each_entry(s, substream) {
1182 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1183 pvoice = s->runtime->private_data;
1184 evoice = pvoice->extra;
1185 what |= 1 << (pvoice->number & 0x1f);
1186 if (evoice == NULL)
1187 whati |= 1 << (pvoice->number & 0x1f);
1188 else {
1189 whati |= 1 << (evoice->number & 0x1f);
1190 what |= 1 << (evoice->number & 0x1f);
1192 if (do_start) {
1193 pvoice->running = 1;
1194 if (evoice != NULL)
1195 evoice->running = 1;
1196 } else {
1197 pvoice->running = 0;
1198 if (evoice != NULL)
1199 evoice->running = 0;
1201 snd_pcm_trigger_done(s, substream);
1202 if (pvoice->mode)
1203 capture_flag = 1;
1206 spin_lock(&codec->reg_lock);
1207 if (!do_start)
1208 outl(what, ALI_REG(codec, ALI_STOP));
1209 val = inl(ALI_REG(codec, ALI_AINTEN));
1210 if (do_start)
1211 val |= whati;
1212 else
1213 val &= ~whati;
1214 outl(val, ALI_REG(codec, ALI_AINTEN));
1215 if (do_start)
1216 outl(what, ALI_REG(codec, ALI_START));
1217 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
1218 spin_unlock(&codec->reg_lock);
1220 return 0;
1223 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1224 struct snd_pcm_hw_params *hw_params)
1226 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1227 struct snd_pcm_runtime *runtime = substream->runtime;
1228 struct snd_ali_voice *pvoice = runtime->private_data;
1229 struct snd_ali_voice *evoice = pvoice->extra;
1230 int err;
1232 err = snd_pcm_lib_malloc_pages(substream,
1233 params_buffer_bytes(hw_params));
1234 if (err < 0)
1235 return err;
1237 /* voice management */
1239 if (params_buffer_size(hw_params) / 2 !=
1240 params_period_size(hw_params)) {
1241 if (!evoice) {
1242 evoice = snd_ali_alloc_voice(codec,
1243 SNDRV_ALI_VOICE_TYPE_PCM,
1244 0, -1);
1245 if (!evoice)
1246 return -ENOMEM;
1247 pvoice->extra = evoice;
1248 evoice->substream = substream;
1250 } else {
1251 if (evoice) {
1252 snd_ali_free_voice(codec, evoice);
1253 pvoice->extra = evoice = NULL;
1257 return 0;
1260 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1262 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1263 struct snd_pcm_runtime *runtime = substream->runtime;
1264 struct snd_ali_voice *pvoice = runtime->private_data;
1265 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1267 snd_pcm_lib_free_pages(substream);
1268 if (evoice) {
1269 snd_ali_free_voice(codec, evoice);
1270 pvoice->extra = NULL;
1272 return 0;
1275 static int snd_ali_hw_params(struct snd_pcm_substream *substream,
1276 struct snd_pcm_hw_params *hw_params)
1278 return snd_pcm_lib_malloc_pages(substream,
1279 params_buffer_bytes(hw_params));
1282 static int snd_ali_hw_free(struct snd_pcm_substream *substream)
1284 return snd_pcm_lib_free_pages(substream);
1287 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1289 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1290 struct snd_pcm_runtime *runtime = substream->runtime;
1291 struct snd_ali_voice *pvoice = runtime->private_data;
1292 struct snd_ali_voice *evoice = pvoice->extra;
1294 unsigned int LBA;
1295 unsigned int Delta;
1296 unsigned int ESO;
1297 unsigned int CTRL;
1298 unsigned int GVSEL;
1299 unsigned int PAN;
1300 unsigned int VOL;
1301 unsigned int EC;
1303 snd_ali_printk("playback_prepare ...\n");
1305 spin_lock_irq(&codec->reg_lock);
1307 /* set Delta (rate) value */
1308 Delta = snd_ali_convert_rate(runtime->rate, 0);
1310 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1311 pvoice->number == ALI_PCM_IN_CHANNEL)
1312 snd_ali_disable_special_channel(codec, pvoice->number);
1313 else if (codec->spdif_support &&
1314 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1315 ALI_SPDIF_OUT_CH_ENABLE)
1316 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1317 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1318 Delta = 0x1000;
1321 /* set Loop Back Address */
1322 LBA = runtime->dma_addr;
1324 /* set interrupt count size */
1325 pvoice->count = runtime->period_size;
1327 /* set target ESO for channel */
1328 pvoice->eso = runtime->buffer_size;
1330 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1331 pvoice->eso, pvoice->count);
1333 /* set ESO to capture first MIDLP interrupt */
1334 ESO = pvoice->eso -1;
1335 /* set ctrl mode */
1336 CTRL = snd_ali_control_mode(substream);
1338 GVSEL = 1;
1339 PAN = 0;
1340 VOL = 0;
1341 EC = 0;
1342 snd_ali_printk("playback_prepare:\n");
1343 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1344 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1345 snd_ali_write_voice_regs(codec,
1346 pvoice->number,
1347 LBA,
1348 0, /* cso */
1349 ESO,
1350 Delta,
1351 0, /* alpha */
1352 GVSEL,
1353 PAN,
1354 VOL,
1355 CTRL,
1356 EC);
1357 if (evoice) {
1358 evoice->count = pvoice->count;
1359 evoice->eso = pvoice->count << 1;
1360 ESO = evoice->eso - 1;
1361 snd_ali_write_voice_regs(codec,
1362 evoice->number,
1363 LBA,
1364 0, /* cso */
1365 ESO,
1366 Delta,
1367 0, /* alpha */
1368 GVSEL,
1369 0x7f,
1370 0x3ff,
1371 CTRL,
1372 EC);
1374 spin_unlock_irq(&codec->reg_lock);
1375 return 0;
1379 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1381 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1382 struct snd_pcm_runtime *runtime = substream->runtime;
1383 struct snd_ali_voice *pvoice = runtime->private_data;
1384 unsigned int LBA;
1385 unsigned int Delta;
1386 unsigned int ESO;
1387 unsigned int CTRL;
1388 unsigned int GVSEL;
1389 unsigned int PAN;
1390 unsigned int VOL;
1391 unsigned int EC;
1392 u8 bValue;
1394 spin_lock_irq(&codec->reg_lock);
1396 snd_ali_printk("ali_prepare...\n");
1398 snd_ali_enable_special_channel(codec,pvoice->number);
1400 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1401 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1402 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1404 /* Prepare capture intr channel */
1405 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1407 unsigned int rate;
1409 spin_unlock_irq(&codec->reg_lock);
1410 if (codec->revision != ALI_5451_V02)
1411 return -1;
1413 rate = snd_ali_get_spdif_in_rate(codec);
1414 if (rate == 0) {
1415 snd_printk(KERN_WARNING "ali_capture_preapre: "
1416 "spdif rate detect err!\n");
1417 rate = 48000;
1419 spin_lock_irq(&codec->reg_lock);
1420 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1421 if (bValue & 0x10) {
1422 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1423 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
1426 if (rate != 48000)
1427 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1430 /* set target ESO for channel */
1431 pvoice->eso = runtime->buffer_size;
1433 /* set interrupt count size */
1434 pvoice->count = runtime->period_size;
1436 /* set Loop Back Address */
1437 LBA = runtime->dma_addr;
1439 /* set ESO to capture first MIDLP interrupt */
1440 ESO = pvoice->eso - 1;
1441 CTRL = snd_ali_control_mode(substream);
1442 GVSEL = 0;
1443 PAN = 0x00;
1444 VOL = 0x00;
1445 EC = 0;
1447 snd_ali_write_voice_regs( codec,
1448 pvoice->number,
1449 LBA,
1450 0, /* cso */
1451 ESO,
1452 Delta,
1453 0, /* alpha */
1454 GVSEL,
1455 PAN,
1456 VOL,
1457 CTRL,
1458 EC);
1460 spin_unlock_irq(&codec->reg_lock);
1462 return 0;
1466 static snd_pcm_uframes_t
1467 snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1469 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1470 struct snd_pcm_runtime *runtime = substream->runtime;
1471 struct snd_ali_voice *pvoice = runtime->private_data;
1472 unsigned int cso;
1474 spin_lock(&codec->reg_lock);
1475 if (!pvoice->running) {
1476 spin_unlock(&codec->reg_lock);
1477 return 0;
1479 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1480 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1481 spin_unlock(&codec->reg_lock);
1482 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1484 return cso;
1488 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1490 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1491 struct snd_pcm_runtime *runtime = substream->runtime;
1492 struct snd_ali_voice *pvoice = runtime->private_data;
1493 unsigned int cso;
1495 spin_lock(&codec->reg_lock);
1496 if (!pvoice->running) {
1497 spin_unlock_irq(&codec->reg_lock);
1498 return 0;
1500 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1501 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1502 spin_unlock(&codec->reg_lock);
1504 return cso;
1507 static struct snd_pcm_hardware snd_ali_playback =
1509 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1510 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1511 SNDRV_PCM_INFO_MMAP_VALID |
1512 SNDRV_PCM_INFO_RESUME |
1513 SNDRV_PCM_INFO_SYNC_START),
1514 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1515 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1516 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1517 .rate_min = 4000,
1518 .rate_max = 48000,
1519 .channels_min = 1,
1520 .channels_max = 2,
1521 .buffer_bytes_max = (256*1024),
1522 .period_bytes_min = 64,
1523 .period_bytes_max = (256*1024),
1524 .periods_min = 1,
1525 .periods_max = 1024,
1526 .fifo_size = 0,
1530 * Capture support device description
1533 static struct snd_pcm_hardware snd_ali_capture =
1535 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1536 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1537 SNDRV_PCM_INFO_MMAP_VALID |
1538 SNDRV_PCM_INFO_RESUME |
1539 SNDRV_PCM_INFO_SYNC_START),
1540 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1541 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1542 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1543 .rate_min = 4000,
1544 .rate_max = 48000,
1545 .channels_min = 1,
1546 .channels_max = 2,
1547 .buffer_bytes_max = (128*1024),
1548 .period_bytes_min = 64,
1549 .period_bytes_max = (128*1024),
1550 .periods_min = 1,
1551 .periods_max = 1024,
1552 .fifo_size = 0,
1555 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1557 struct snd_ali_voice *pvoice = runtime->private_data;
1558 struct snd_ali *codec;
1560 if (pvoice) {
1561 codec = pvoice->codec;
1562 snd_ali_free_voice(pvoice->codec, pvoice);
1566 static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1567 int channel, struct snd_pcm_hardware *phw)
1569 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1570 struct snd_pcm_runtime *runtime = substream->runtime;
1571 struct snd_ali_voice *pvoice;
1573 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1574 channel);
1575 if (!pvoice)
1576 return -EAGAIN;
1578 pvoice->substream = substream;
1579 runtime->private_data = pvoice;
1580 runtime->private_free = snd_ali_pcm_free_substream;
1582 runtime->hw = *phw;
1583 snd_pcm_set_sync(substream);
1584 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1585 0, 64*1024);
1586 return 0;
1589 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1591 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1594 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1596 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1599 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1601 return 0;
1604 static int snd_ali_close(struct snd_pcm_substream *substream)
1606 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1607 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1609 snd_ali_disable_special_channel(codec,pvoice->number);
1611 return 0;
1614 static struct snd_pcm_ops snd_ali_playback_ops = {
1615 .open = snd_ali_playback_open,
1616 .close = snd_ali_playback_close,
1617 .ioctl = snd_pcm_lib_ioctl,
1618 .hw_params = snd_ali_playback_hw_params,
1619 .hw_free = snd_ali_playback_hw_free,
1620 .prepare = snd_ali_playback_prepare,
1621 .trigger = snd_ali_trigger,
1622 .pointer = snd_ali_playback_pointer,
1625 static struct snd_pcm_ops snd_ali_capture_ops = {
1626 .open = snd_ali_capture_open,
1627 .close = snd_ali_close,
1628 .ioctl = snd_pcm_lib_ioctl,
1629 .hw_params = snd_ali_hw_params,
1630 .hw_free = snd_ali_hw_free,
1631 .prepare = snd_ali_prepare,
1632 .trigger = snd_ali_trigger,
1633 .pointer = snd_ali_pointer,
1637 * Modem PCM
1640 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1641 struct snd_pcm_hw_params *hw_params)
1643 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1644 unsigned int modem_num = chip->num_of_codecs - 1;
1645 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1646 params_rate(hw_params));
1647 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1648 return snd_ali_hw_params(substream, hw_params);
1651 static struct snd_pcm_hardware snd_ali_modem =
1653 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1654 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1655 SNDRV_PCM_INFO_MMAP_VALID |
1656 SNDRV_PCM_INFO_RESUME |
1657 SNDRV_PCM_INFO_SYNC_START),
1658 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1659 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1660 SNDRV_PCM_RATE_16000),
1661 .rate_min = 8000,
1662 .rate_max = 16000,
1663 .channels_min = 1,
1664 .channels_max = 1,
1665 .buffer_bytes_max = (256*1024),
1666 .period_bytes_min = 64,
1667 .period_bytes_max = (256*1024),
1668 .periods_min = 1,
1669 .periods_max = 1024,
1670 .fifo_size = 0,
1673 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1674 int channel)
1676 static unsigned int rates[] = {8000, 9600, 12000, 16000};
1677 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1678 .count = ARRAY_SIZE(rates),
1679 .list = rates,
1680 .mask = 0,
1682 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1684 if (err)
1685 return err;
1686 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1687 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1690 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1692 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1695 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1697 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1700 static struct snd_pcm_ops snd_ali_modem_playback_ops = {
1701 .open = snd_ali_modem_playback_open,
1702 .close = snd_ali_close,
1703 .ioctl = snd_pcm_lib_ioctl,
1704 .hw_params = snd_ali_modem_hw_params,
1705 .hw_free = snd_ali_hw_free,
1706 .prepare = snd_ali_prepare,
1707 .trigger = snd_ali_trigger,
1708 .pointer = snd_ali_pointer,
1711 static struct snd_pcm_ops snd_ali_modem_capture_ops = {
1712 .open = snd_ali_modem_capture_open,
1713 .close = snd_ali_close,
1714 .ioctl = snd_pcm_lib_ioctl,
1715 .hw_params = snd_ali_modem_hw_params,
1716 .hw_free = snd_ali_hw_free,
1717 .prepare = snd_ali_prepare,
1718 .trigger = snd_ali_trigger,
1719 .pointer = snd_ali_pointer,
1723 struct ali_pcm_description {
1724 char *name;
1725 unsigned int playback_num;
1726 unsigned int capture_num;
1727 struct snd_pcm_ops *playback_ops;
1728 struct snd_pcm_ops *capture_ops;
1729 unsigned short class;
1733 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1735 struct snd_ali *codec = pcm->private_data;
1736 codec->pcm[pcm->device] = NULL;
1740 static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
1741 struct ali_pcm_description *desc)
1743 struct snd_pcm *pcm;
1744 int err;
1746 err = snd_pcm_new(codec->card, desc->name, device,
1747 desc->playback_num, desc->capture_num, &pcm);
1748 if (err < 0) {
1749 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
1750 return err;
1752 pcm->private_data = codec;
1753 pcm->private_free = snd_ali_pcm_free;
1754 if (desc->playback_ops)
1755 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1756 desc->playback_ops);
1757 if (desc->capture_ops)
1758 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1759 desc->capture_ops);
1761 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1762 snd_dma_pci_data(codec->pci),
1763 64*1024, 128*1024);
1765 pcm->info_flags = 0;
1766 pcm->dev_class = desc->class;
1767 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1768 strcpy(pcm->name, desc->name);
1769 codec->pcm[0] = pcm;
1770 return 0;
1773 static struct ali_pcm_description ali_pcms[] = {
1774 { .name = "ALI 5451",
1775 .playback_num = ALI_CHANNELS,
1776 .capture_num = 1,
1777 .playback_ops = &snd_ali_playback_ops,
1778 .capture_ops = &snd_ali_capture_ops
1780 { .name = "ALI 5451 modem",
1781 .playback_num = 1,
1782 .capture_num = 1,
1783 .playback_ops = &snd_ali_modem_playback_ops,
1784 .capture_ops = &snd_ali_modem_capture_ops,
1785 .class = SNDRV_PCM_CLASS_MODEM
1789 static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
1791 int i, err;
1792 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1793 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1794 if (err < 0)
1795 return err;
1797 return 0;
1801 #define ALI5451_SPDIF(xname, xindex, value) \
1802 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1803 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1804 .put = snd_ali5451_spdif_put, .private_value = value}
1806 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
1808 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1811 struct snd_ali *codec = kcontrol->private_data;
1812 unsigned int spdif_enable;
1814 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1816 spin_lock_irq(&codec->reg_lock);
1817 switch (kcontrol->private_value) {
1818 case 0:
1819 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1820 break;
1821 case 1:
1822 spdif_enable = ((codec->spdif_mask & 0x02) &&
1823 (codec->spdif_mask & 0x04)) ? 1 : 0;
1824 break;
1825 case 2:
1826 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1827 break;
1828 default:
1829 break;
1831 ucontrol->value.integer.value[0] = spdif_enable;
1832 spin_unlock_irq(&codec->reg_lock);
1833 return 0;
1836 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1837 struct snd_ctl_elem_value *ucontrol)
1839 struct snd_ali *codec = kcontrol->private_data;
1840 unsigned int change = 0, spdif_enable = 0;
1842 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1844 spin_lock_irq(&codec->reg_lock);
1845 switch (kcontrol->private_value) {
1846 case 0:
1847 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1848 change = change ^ spdif_enable;
1849 if (change) {
1850 if (spdif_enable) {
1851 codec->spdif_mask |= 0x02;
1852 snd_ali_enable_spdif_out(codec);
1853 } else {
1854 codec->spdif_mask &= ~(0x02);
1855 codec->spdif_mask &= ~(0x04);
1856 snd_ali_disable_spdif_out(codec);
1859 break;
1860 case 1:
1861 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1862 change = change ^ spdif_enable;
1863 if (change && (codec->spdif_mask & 0x02)) {
1864 if (spdif_enable) {
1865 codec->spdif_mask |= 0x04;
1866 snd_ali_enable_spdif_chnout(codec);
1867 } else {
1868 codec->spdif_mask &= ~(0x04);
1869 snd_ali_disable_spdif_chnout(codec);
1872 break;
1873 case 2:
1874 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1875 change = change ^ spdif_enable;
1876 if (change) {
1877 if (spdif_enable) {
1878 codec->spdif_mask |= 0x01;
1879 snd_ali_enable_spdif_in(codec);
1880 } else {
1881 codec->spdif_mask &= ~(0x01);
1882 snd_ali_disable_spdif_in(codec);
1885 break;
1886 default:
1887 break;
1889 spin_unlock_irq(&codec->reg_lock);
1891 return change;
1894 static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
1895 /* spdif aplayback switch */
1896 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1897 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1898 /* spdif out to spdif channel */
1899 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1900 /* spdif in from spdif channel */
1901 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1904 static int __devinit snd_ali_mixer(struct snd_ali * codec)
1906 struct snd_ac97_template ac97;
1907 unsigned int idx;
1908 int i, err;
1909 static struct snd_ac97_bus_ops ops = {
1910 .write = snd_ali_codec_write,
1911 .read = snd_ali_codec_read,
1914 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1915 if (err < 0)
1916 return err;
1918 memset(&ac97, 0, sizeof(ac97));
1919 ac97.private_data = codec;
1921 for (i = 0; i < codec->num_of_codecs; i++) {
1922 ac97.num = i;
1923 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1924 if (err < 0) {
1925 snd_printk(KERN_ERR
1926 "ali mixer %d creating error.\n", i);
1927 if (i == 0)
1928 return err;
1929 codec->num_of_codecs = 1;
1930 break;
1934 if (codec->spdif_support) {
1935 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1936 err = snd_ctl_add(codec->card,
1937 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1938 if (err < 0)
1939 return err;
1942 return 0;
1945 #ifdef CONFIG_PM
1946 static int ali_suspend(struct pci_dev *pci, pm_message_t state)
1948 struct snd_card *card = pci_get_drvdata(pci);
1949 struct snd_ali *chip = card->private_data;
1950 struct snd_ali_image *im;
1951 int i, j;
1953 im = chip->image;
1954 if (!im)
1955 return 0;
1957 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1958 for (i = 0; i < chip->num_of_codecs; i++) {
1959 snd_pcm_suspend_all(chip->pcm[i]);
1960 snd_ac97_suspend(chip->ac97[i]);
1963 spin_lock_irq(&chip->reg_lock);
1965 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1966 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1967 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1969 /* disable all IRQ bits */
1970 outl(0, ALI_REG(chip, ALI_MISCINT));
1972 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1973 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1974 continue;
1975 im->regs[i] = inl(ALI_REG(chip, i*4));
1978 for (i = 0; i < ALI_CHANNELS; i++) {
1979 outb(i, ALI_REG(chip, ALI_GC_CIR));
1980 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1981 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1984 /* stop all HW channel */
1985 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1987 spin_unlock_irq(&chip->reg_lock);
1989 pci_disable_device(pci);
1990 pci_save_state(pci);
1991 pci_set_power_state(pci, pci_choose_state(pci, state));
1992 return 0;
1995 static int ali_resume(struct pci_dev *pci)
1997 struct snd_card *card = pci_get_drvdata(pci);
1998 struct snd_ali *chip = card->private_data;
1999 struct snd_ali_image *im;
2000 int i, j;
2002 im = chip->image;
2003 if (!im)
2004 return 0;
2006 pci_set_power_state(pci, PCI_D0);
2007 pci_restore_state(pci);
2008 if (pci_enable_device(pci) < 0) {
2009 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2010 "disabling device\n");
2011 snd_card_disconnect(card);
2012 return -EIO;
2014 pci_set_master(pci);
2016 spin_lock_irq(&chip->reg_lock);
2018 for (i = 0; i < ALI_CHANNELS; i++) {
2019 outb(i, ALI_REG(chip, ALI_GC_CIR));
2020 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2021 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2024 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2025 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
2026 (i*4 == ALI_START))
2027 continue;
2028 outl(im->regs[i], ALI_REG(chip, i*4));
2031 /* start HW channel */
2032 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2033 /* restore IRQ enable bits */
2034 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2036 spin_unlock_irq(&chip->reg_lock);
2038 for (i = 0 ; i < chip->num_of_codecs; i++)
2039 snd_ac97_resume(chip->ac97[i]);
2041 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2042 return 0;
2044 #endif /* CONFIG_PM */
2046 static int snd_ali_free(struct snd_ali * codec)
2048 if (codec->hw_initialized)
2049 snd_ali_disable_address_interrupt(codec);
2050 if (codec->irq >= 0)
2051 free_irq(codec->irq, codec);
2052 if (codec->port)
2053 pci_release_regions(codec->pci);
2054 pci_disable_device(codec->pci);
2055 #ifdef CONFIG_PM
2056 kfree(codec->image);
2057 #endif
2058 pci_dev_put(codec->pci_m1533);
2059 pci_dev_put(codec->pci_m7101);
2060 kfree(codec);
2061 return 0;
2064 static int snd_ali_chip_init(struct snd_ali *codec)
2066 unsigned int legacy;
2067 unsigned char temp;
2068 struct pci_dev *pci_dev;
2070 snd_ali_printk("chip initializing ... \n");
2072 if (snd_ali_reset_5451(codec)) {
2073 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
2074 return -1;
2077 if (codec->revision == ALI_5451_V02) {
2078 pci_dev = codec->pci_m1533;
2079 pci_read_config_byte(pci_dev, 0x59, &temp);
2080 temp |= 0x80;
2081 pci_write_config_byte(pci_dev, 0x59, temp);
2083 pci_dev = codec->pci_m7101;
2084 pci_read_config_byte(pci_dev, 0xb8, &temp);
2085 temp |= 0x20;
2086 pci_write_config_byte(pci_dev, 0xB8, temp);
2089 pci_read_config_dword(codec->pci, 0x44, &legacy);
2090 legacy &= 0xff00ff00;
2091 legacy |= 0x000800aa;
2092 pci_write_config_dword(codec->pci, 0x44, legacy);
2094 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2095 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2096 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2097 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2098 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2100 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2101 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2102 AC97_EXTENDED_STATUS);
2103 if (codec->spdif_support) {
2104 snd_ali_enable_spdif_out(codec);
2105 codec->spdif_mask = 0x00000002;
2108 codec->num_of_codecs = 1;
2110 /* secondary codec - modem */
2111 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2112 codec->num_of_codecs++;
2113 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2114 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2115 ALI_SCTRL_LINE_OUT_EN),
2116 ALI_REG(codec, ALI_SCTRL));
2119 snd_ali_printk("chip initialize succeed.\n");
2120 return 0;
2124 /* proc for register dump */
2125 static void snd_ali_proc_read(struct snd_info_entry *entry,
2126 struct snd_info_buffer *buf)
2128 struct snd_ali *codec = entry->private_data;
2129 int i;
2130 for (i = 0; i < 256 ; i+= 4)
2131 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2134 static void __devinit snd_ali_proc_init(struct snd_ali *codec)
2136 struct snd_info_entry *entry;
2137 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
2138 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
2141 static int __devinit snd_ali_resources(struct snd_ali *codec)
2143 int err;
2145 snd_ali_printk("resouces allocation ...\n");
2146 err = pci_request_regions(codec->pci, "ALI 5451");
2147 if (err < 0)
2148 return err;
2149 codec->port = pci_resource_start(codec->pci, 0);
2151 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2152 IRQF_SHARED, "ALI 5451", codec)) {
2153 snd_printk(KERN_ERR "Unable to request irq.\n");
2154 return -EBUSY;
2156 codec->irq = codec->pci->irq;
2157 snd_ali_printk("resouces allocated.\n");
2158 return 0;
2160 static int snd_ali_dev_free(struct snd_device *device)
2162 struct snd_ali *codec = device->device_data;
2163 snd_ali_free(codec);
2164 return 0;
2167 static int __devinit snd_ali_create(struct snd_card *card,
2168 struct pci_dev *pci,
2169 int pcm_streams,
2170 int spdif_support,
2171 struct snd_ali ** r_ali)
2173 struct snd_ali *codec;
2174 int i, err;
2175 unsigned short cmdw;
2176 static struct snd_device_ops ops = {
2177 .dev_free = snd_ali_dev_free,
2180 *r_ali = NULL;
2182 snd_ali_printk("creating ...\n");
2184 /* enable PCI device */
2185 err = pci_enable_device(pci);
2186 if (err < 0)
2187 return err;
2188 /* check, if we can restrict PCI DMA transfers to 31 bits */
2189 if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
2190 pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
2191 snd_printk(KERN_ERR "architecture does not support "
2192 "31bit PCI busmaster DMA\n");
2193 pci_disable_device(pci);
2194 return -ENXIO;
2197 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2198 if (!codec) {
2199 pci_disable_device(pci);
2200 return -ENOMEM;
2203 spin_lock_init(&codec->reg_lock);
2204 spin_lock_init(&codec->voice_alloc);
2206 codec->card = card;
2207 codec->pci = pci;
2208 codec->irq = -1;
2209 codec->revision = pci->revision;
2210 codec->spdif_support = spdif_support;
2212 if (pcm_streams < 1)
2213 pcm_streams = 1;
2214 if (pcm_streams > 32)
2215 pcm_streams = 32;
2217 pci_set_master(pci);
2218 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2219 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2220 cmdw |= PCI_COMMAND_IO;
2221 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2223 pci_set_master(pci);
2225 if (snd_ali_resources(codec)) {
2226 snd_ali_free(codec);
2227 return -EBUSY;
2230 synchronize_irq(pci->irq);
2232 codec->synth.chmap = 0;
2233 codec->synth.chcnt = 0;
2234 codec->spdif_mask = 0;
2235 codec->synth.synthcount = 0;
2237 if (codec->revision == ALI_5451_V02)
2238 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2239 else
2240 codec->chregs.regs.ac97read = ALI_AC97_READ;
2241 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2243 codec->chregs.regs.start = ALI_START;
2244 codec->chregs.regs.stop = ALI_STOP;
2245 codec->chregs.regs.aint = ALI_AINT;
2246 codec->chregs.regs.ainten = ALI_AINTEN;
2248 codec->chregs.data.start = 0x00;
2249 codec->chregs.data.stop = 0x00;
2250 codec->chregs.data.aint = 0x00;
2251 codec->chregs.data.ainten = 0x00;
2253 /* M1533: southbridge */
2254 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2255 if (!codec->pci_m1533) {
2256 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2257 snd_ali_free(codec);
2258 return -ENODEV;
2260 /* M7101: power management */
2261 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2262 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2263 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2264 snd_ali_free(codec);
2265 return -ENODEV;
2268 snd_ali_printk("snd_device_new is called.\n");
2269 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2270 if (err < 0) {
2271 snd_ali_free(codec);
2272 return err;
2275 snd_card_set_dev(card, &pci->dev);
2277 /* initialise synth voices*/
2278 for (i = 0; i < ALI_CHANNELS; i++)
2279 codec->synth.voices[i].number = i;
2281 err = snd_ali_chip_init(codec);
2282 if (err < 0) {
2283 snd_printk(KERN_ERR "ali create: chip init error.\n");
2284 return err;
2287 #ifdef CONFIG_PM
2288 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2289 if (!codec->image)
2290 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2291 #endif
2293 snd_ali_enable_address_interrupt(codec);
2294 codec->hw_initialized = 1;
2296 *r_ali = codec;
2297 snd_ali_printk("created.\n");
2298 return 0;
2301 static int __devinit snd_ali_probe(struct pci_dev *pci,
2302 const struct pci_device_id *pci_id)
2304 struct snd_card *card;
2305 struct snd_ali *codec;
2306 int err;
2308 snd_ali_printk("probe ...\n");
2310 card = snd_card_new(index, id, THIS_MODULE, 0);
2311 if (!card)
2312 return -ENOMEM;
2314 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2315 if (err < 0)
2316 goto error;
2317 card->private_data = codec;
2319 snd_ali_printk("mixer building ...\n");
2320 err = snd_ali_mixer(codec);
2321 if (err < 0)
2322 goto error;
2324 snd_ali_printk("pcm building ...\n");
2325 err = snd_ali_build_pcms(codec);
2326 if (err < 0)
2327 goto error;
2329 snd_ali_proc_init(codec);
2331 strcpy(card->driver, "ALI5451");
2332 strcpy(card->shortname, "ALI 5451");
2334 sprintf(card->longname, "%s at 0x%lx, irq %i",
2335 card->shortname, codec->port, codec->irq);
2337 snd_ali_printk("register card.\n");
2338 err = snd_card_register(card);
2339 if (err < 0)
2340 goto error;
2342 pci_set_drvdata(pci, card);
2343 return 0;
2345 error:
2346 snd_card_free(card);
2347 return err;
2350 static void __devexit snd_ali_remove(struct pci_dev *pci)
2352 snd_card_free(pci_get_drvdata(pci));
2353 pci_set_drvdata(pci, NULL);
2356 static struct pci_driver driver = {
2357 .name = "ALI 5451",
2358 .id_table = snd_ali_ids,
2359 .probe = snd_ali_probe,
2360 .remove = __devexit_p(snd_ali_remove),
2361 #ifdef CONFIG_PM
2362 .suspend = ali_suspend,
2363 .resume = ali_resume,
2364 #endif
2367 static int __init alsa_card_ali_init(void)
2369 return pci_register_driver(&driver);
2372 static void __exit alsa_card_ali_exit(void)
2374 pci_unregister_driver(&driver);
2377 module_init(alsa_card_ali_init)
2378 module_exit(alsa_card_ali_exit)