[ALSA] ali5451 - Don't build non-existing modem PCM
[linux-2.6/linux-2.6-openrd.git] / sound / pci / ali5451 / ali5451.c
blobf35b558c29b2474b2e4bd8168c00c4b0bffd5841
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 <sound/driver.h>
29 #include <asm/io.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/slab.h>
35 #include <linux/moduleparam.h>
36 #include <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_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
49 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
50 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
51 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 32};
52 static int spdif[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
54 module_param_array(index, int, NULL, 0444);
55 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
56 module_param_array(id, charp, NULL, 0444);
57 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
58 module_param_array(enable, bool, NULL, 0444);
59 MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
60 module_param_array(pcm_channels, int, NULL, 0444);
61 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
62 module_param_array(spdif, bool, NULL, 0444);
63 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
66 * Debug part definitions
69 //#define ALI_DEBUG
71 #ifdef ALI_DEBUG
72 #define snd_ali_printk(format, args...) printk(format, ##args);
73 #else
74 #define snd_ali_printk(format, args...)
75 #endif
78 * Constants definition
81 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
84 #define ALI_CHANNELS 32
86 #define ALI_PCM_IN_CHANNEL 31
87 #define ALI_SPDIF_IN_CHANNEL 19
88 #define ALI_SPDIF_OUT_CHANNEL 15
89 #define ALI_CENTER_CHANNEL 24
90 #define ALI_LEF_CHANNEL 23
91 #define ALI_SURR_LEFT_CHANNEL 26
92 #define ALI_SURR_RIGHT_CHANNEL 25
93 #define ALI_MODEM_IN_CHANNEL 21
94 #define ALI_MODEM_OUT_CHANNEL 20
96 #define SNDRV_ALI_VOICE_TYPE_PCM 01
97 #define SNDRV_ALI_VOICE_TYPE_OTH 02
99 #define ALI_5451_V02 0x02
102 * Direct Registers
105 #define ALI_LEGACY_DMAR0 0x00 // ADR0
106 #define ALI_LEGACY_DMAR4 0x04 // CNT0
107 #define ALI_LEGACY_DMAR11 0x0b // MOD
108 #define ALI_LEGACY_DMAR15 0x0f // MMR
109 #define ALI_MPUR0 0x20
110 #define ALI_MPUR1 0x21
111 #define ALI_MPUR2 0x22
112 #define ALI_MPUR3 0x23
114 #define ALI_AC97_WRITE 0x40
115 #define ALI_AC97_READ 0x44
117 #define ALI_SCTRL 0x48
118 #define ALI_SPDIF_OUT_ENABLE 0x20
119 #define ALI_SCTRL_LINE_IN2 (1 << 9)
120 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
121 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
122 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
123 #define ALI_SCTRL_CODEC1_READY (1 << 24)
124 #define ALI_SCTRL_CODEC2_READY (1 << 25)
125 #define ALI_AC97_GPIO 0x4c
126 #define ALI_AC97_GPIO_ENABLE 0x8000
127 #define ALI_AC97_GPIO_DATA_SHIFT 16
128 #define ALI_SPDIF_CS 0x70
129 #define ALI_SPDIF_CTRL 0x74
130 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
131 #define ALI_SPDIF_IN_CH_STATUS 0x40
132 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
133 #define ALI_START 0x80
134 #define ALI_STOP 0x84
135 #define ALI_CSPF 0x90
136 #define ALI_AINT 0x98
137 #define ALI_GC_CIR 0xa0
138 #define ENDLP_IE 0x00001000
139 #define MIDLP_IE 0x00002000
140 #define ALI_AINTEN 0xa4
141 #define ALI_VOLUME 0xa8
142 #define ALI_SBDELTA_DELTA_R 0xac
143 #define ALI_MISCINT 0xb0
144 #define ADDRESS_IRQ 0x00000020
145 #define TARGET_REACHED 0x00008000
146 #define MIXER_OVERFLOW 0x00000800
147 #define MIXER_UNDERFLOW 0x00000400
148 #define GPIO_IRQ 0x01000000
149 #define ALI_SBBL_SBCL 0xc0
150 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
151 #define ALI_STIMER 0xc8
152 #define ALI_GLOBAL_CONTROL 0xd4
153 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
154 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
155 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
156 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
157 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
159 #define ALI_CSO_ALPHA_FMS 0xe0
160 #define ALI_LBA 0xe4
161 #define ALI_ESO_DELTA 0xe8
162 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
163 #define ALI_EBUF1 0xf4
164 #define ALI_EBUF2 0xf8
166 #define ALI_REG(codec, x) ((codec)->port + x)
168 #define MAX_CODECS 2
171 typedef struct snd_stru_ali ali_t;
172 typedef struct snd_ali_stru_voice snd_ali_voice_t;
174 typedef struct snd_ali_channel_control {
175 // register data
176 struct REGDATA {
177 unsigned int start;
178 unsigned int stop;
179 unsigned int aint;
180 unsigned int ainten;
181 } data;
183 // register addresses
184 struct REGS {
185 unsigned int start;
186 unsigned int stop;
187 unsigned int aint;
188 unsigned int ainten;
189 unsigned int ac97read;
190 unsigned int ac97write;
191 } regs;
193 } snd_ali_channel_control_t;
195 struct snd_ali_stru_voice {
196 unsigned int number;
197 unsigned int use: 1,
198 pcm: 1,
199 midi: 1,
200 mode: 1,
201 synth: 1;
203 /* PCM data */
204 ali_t *codec;
205 snd_pcm_substream_t *substream;
206 snd_ali_voice_t *extra;
208 unsigned int running: 1;
210 int eso; /* final ESO value for channel */
211 int count; /* runtime->period_size */
213 /* --- */
215 void *private_data;
216 void (*private_free)(void *private_data);
220 typedef struct snd_stru_alidev {
222 snd_ali_voice_t voices[ALI_CHANNELS];
224 unsigned int chcnt; /* num of opened channels */
225 unsigned int chmap; /* bitmap for opened channels */
226 unsigned int synthcount;
228 } alidev_t;
231 #ifdef CONFIG_PM
232 #define ALI_GLOBAL_REGS 56
233 #define ALI_CHANNEL_REGS 8
234 typedef struct snd_ali_image {
235 unsigned long regs[ALI_GLOBAL_REGS];
236 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
237 } ali_image_t;
238 #endif
241 struct snd_stru_ali {
242 unsigned long irq;
243 unsigned long port;
244 unsigned char revision;
246 unsigned int hw_initialized: 1;
247 unsigned int spdif_support: 1;
249 struct pci_dev *pci;
250 struct pci_dev *pci_m1533;
251 struct pci_dev *pci_m7101;
253 snd_card_t *card;
254 snd_pcm_t *pcm[MAX_CODECS];
255 alidev_t synth;
256 snd_ali_channel_control_t chregs;
258 /* S/PDIF Mask */
259 unsigned int spdif_mask;
261 unsigned int spurious_irq_count;
262 unsigned int spurious_irq_max_delta;
264 unsigned int num_of_codecs;
266 ac97_bus_t *ac97_bus;
267 ac97_t *ac97[MAX_CODECS];
268 unsigned short ac97_ext_id;
269 unsigned short ac97_ext_status;
271 spinlock_t reg_lock;
272 spinlock_t voice_alloc;
274 #ifdef CONFIG_PM
275 ali_image_t *image;
276 #endif
279 static struct pci_device_id snd_ali_ids[] = {
280 {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
281 {0, }
283 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
285 static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
286 static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
287 static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
290 * Debug Part
293 #ifdef ALI_DEBUG
295 static void ali_read_regs(ali_t *codec, int channel)
297 int i,j;
298 unsigned int dwVal;
300 printk("channel %d registers map:\n", channel);
301 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
303 printk(" ");
304 for(j=0;j<8;j++)
305 printk("%2.2x ", j*4);
306 printk("\n");
308 for (i=0; i<=0xf8/4;i++) {
309 if(i%8 == 0)
310 printk("%2.2x ", (i*4/0x10)*0x10);
311 dwVal = inl(ALI_REG(codec,i*4));
312 printk("%8.8x ", dwVal);
313 if ((i+1)%8 == 0)
314 printk("\n");
316 printk("\n");
318 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
320 unsigned int dwVal;
321 struct pci_dev *pci_dev;
322 int i,j;
324 pci_dev = pci_get_device(vendor, deviceid, NULL);
325 if (pci_dev == NULL)
326 return ;
328 printk("\nM%x PCI CFG\n", deviceid);
329 printk(" ");
330 for(j=0;j<8;j++)
331 printk("%d ",j);
332 printk("\n");
334 for(i=0;i<8;i++) {
335 printk("%d ",i);
336 for(j=0;j<8;j++)
338 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
339 printk("%8.8x ", dwVal);
341 printk("\n");
343 pci_dev_put(pci_dev);
345 static void ali_read_ac97regs(ali_t *codec, int secondary)
347 unsigned short i,j;
348 unsigned short wVal;
350 printk("\ncodec %d registers map:\n", secondary);
352 printk(" ");
353 for(j=0;j<8;j++)
354 printk("%2.2x ",j*2);
355 printk("\n");
357 for (i=0; i<64;i++) {
358 if(i%8 == 0)
359 printk("%2.2x ", (i/8)*0x10);
360 wVal = snd_ali_codec_peek(codec, secondary, i*2);
361 printk("%4.4x ", wVal);
362 if ((i+1)%8 == 0)
363 printk("\n");
365 printk("\n");
368 #endif
371 * AC97 ACCESS
374 static inline unsigned int snd_ali_5451_peek(ali_t *codec,
375 unsigned int port )
377 return (unsigned int)inl(ALI_REG(codec, port));
380 static inline void snd_ali_5451_poke( ali_t *codec,
381 unsigned int port,
382 unsigned int val )
384 outl((unsigned int)val, ALI_REG(codec, port));
387 static int snd_ali_codec_ready( ali_t *codec,
388 unsigned int port,
389 int sched )
391 unsigned long end_time;
392 unsigned int res;
394 end_time = jiffies + 10 * msecs_to_jiffies(250);
395 do {
396 res = snd_ali_5451_peek(codec,port);
397 if (! (res & 0x8000))
398 return 0;
399 if (sched) {
400 set_current_state(TASK_UNINTERRUPTIBLE);
401 schedule_timeout(1);
403 } while (time_after_eq(end_time, jiffies));
404 snd_ali_5451_poke(codec, port, res & ~0x8000);
405 snd_printdd("ali_codec_ready: codec is not ready.\n ");
406 return -EIO;
409 static int snd_ali_stimer_ready(ali_t *codec, int sched)
411 unsigned long end_time;
412 unsigned long dwChk1,dwChk2;
414 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
415 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
417 end_time = jiffies + 10 * msecs_to_jiffies(250);
418 do {
419 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
420 if (dwChk2 != dwChk1)
421 return 0;
422 if (sched) {
423 set_current_state(TASK_UNINTERRUPTIBLE);
424 schedule_timeout(1);
426 } while (time_after_eq(end_time, jiffies));
427 snd_printk("ali_stimer_read: stimer is not ready.\n");
428 return -EIO;
431 static void snd_ali_codec_poke(ali_t *codec,int secondary,
432 unsigned short reg,
433 unsigned short val)
435 unsigned int dwVal = 0;
436 unsigned int port = 0;
438 if (reg >= 0x80) {
439 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
440 return;
443 port = codec->chregs.regs.ac97write;
445 if (snd_ali_codec_ready(codec, port, 0) < 0)
446 return;
447 if (snd_ali_stimer_ready(codec, 0) < 0)
448 return;
450 dwVal = (unsigned int) (reg & 0xff);
451 dwVal |= 0x8000 | (val << 16);
452 if (secondary) dwVal |= 0x0080;
453 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
455 snd_ali_5451_poke(codec,port,dwVal);
457 return ;
460 static unsigned short snd_ali_codec_peek( ali_t *codec,
461 int secondary,
462 unsigned short reg)
464 unsigned int dwVal = 0;
465 unsigned int port = 0;
467 if (reg >= 0x80) {
468 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
469 return ~0;
472 port = codec->chregs.regs.ac97read;
474 if (snd_ali_codec_ready(codec, port, 0) < 0)
475 return ~0;
476 if (snd_ali_stimer_ready(codec, 0) < 0)
477 return ~0;
479 dwVal = (unsigned int) (reg & 0xff);
480 dwVal |= 0x8000; /* bit 15*/
481 if (secondary) dwVal |= 0x0080;
483 snd_ali_5451_poke(codec, port, dwVal);
485 if (snd_ali_stimer_ready(codec, 0) < 0)
486 return ~0;
487 if (snd_ali_codec_ready(codec, port, 0) < 0)
488 return ~0;
490 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
493 static void snd_ali_codec_write(ac97_t *ac97,
494 unsigned short reg,
495 unsigned short val )
497 ali_t *codec = ac97->private_data;
499 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
500 if(reg == AC97_GPIO_STATUS) {
501 outl((val << ALI_AC97_GPIO_DATA_SHIFT)|ALI_AC97_GPIO_ENABLE,
502 ALI_REG(codec, ALI_AC97_GPIO));
503 return;
505 snd_ali_codec_poke(codec, ac97->num, reg, val);
506 return ;
510 static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
512 ali_t *codec = ac97->private_data;
514 snd_ali_printk("codec_read reg=%xh.\n", reg);
515 return (snd_ali_codec_peek(codec, ac97->num, reg));
519 * AC97 Reset
522 static int snd_ali_reset_5451(ali_t *codec)
524 struct pci_dev *pci_dev = NULL;
525 unsigned short wCount, wReg;
526 unsigned int dwVal;
528 if ((pci_dev = codec->pci_m1533) != NULL) {
529 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
530 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
531 udelay(5000);
532 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
533 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
534 udelay(5000);
537 pci_dev = codec->pci;
538 pci_read_config_dword(pci_dev, 0x44, &dwVal);
539 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
540 udelay(500);
541 pci_read_config_dword(pci_dev, 0x44, &dwVal);
542 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
543 udelay(5000);
545 wCount = 200;
546 while(wCount--) {
547 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
548 if((wReg & 0x000f) == 0x000f)
549 return 0;
550 udelay(5000);
553 /* non-fatal if you have a non PM capable codec */
554 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
555 return 0;
558 #ifdef CODEC_RESET
560 static int snd_ali_reset_codec(ali_t *codec)
562 struct pci_dev *pci_dev = NULL;
563 unsigned char bVal = 0;
564 unsigned int dwVal;
565 unsigned short wCount, wReg;
567 pci_dev = codec->pci_m1533;
569 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
570 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
571 udelay(5000);
572 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
573 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
574 udelay(5000);
576 bVal = inb(ALI_REG(codec,ALI_SCTRL));
577 bVal |= 0x02;
578 outb(ALI_REG(codec,ALI_SCTRL),bVal);
579 udelay(5000);
580 bVal = inb(ALI_REG(codec,ALI_SCTRL));
581 bVal &= 0xfd;
582 outb(ALI_REG(codec,ALI_SCTRL),bVal);
583 udelay(15000);
585 wCount = 200;
586 while(wCount--) {
587 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
588 if((wReg & 0x000f) == 0x000f)
589 return 0;
590 udelay(5000);
592 return -1;
595 #endif
598 * ALI 5451 Controller
601 static void snd_ali_enable_special_channel(ali_t *codec, unsigned int channel)
603 unsigned long dwVal = 0;
605 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
606 dwVal |= 1 << (channel & 0x0000001f);
607 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
610 static void snd_ali_disable_special_channel(ali_t *codec, unsigned int channel)
612 unsigned long dwVal = 0;
614 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
615 dwVal &= ~(1 << (channel & 0x0000001f));
616 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
619 static void snd_ali_enable_address_interrupt(ali_t * codec)
621 unsigned int gc;
623 gc = inl(ALI_REG(codec, ALI_GC_CIR));
624 gc |= ENDLP_IE;
625 gc |= MIDLP_IE;
626 outl( gc, ALI_REG(codec, ALI_GC_CIR));
629 static void snd_ali_disable_address_interrupt(ali_t * codec)
631 unsigned int gc;
633 gc = inl(ALI_REG(codec, ALI_GC_CIR));
634 gc &= ~ENDLP_IE;
635 gc &= ~MIDLP_IE;
636 outl(gc, ALI_REG(codec, ALI_GC_CIR));
639 #if 0 // not used
640 static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
642 unsigned int mask;
643 snd_ali_channel_control_t *pchregs = &(codec->chregs);
645 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
647 mask = 1 << (channel & 0x1f);
648 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
649 pchregs->data.ainten |= mask;
650 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
652 #endif
654 static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
656 unsigned int mask;
657 snd_ali_channel_control_t *pchregs = &(codec->chregs);
659 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
661 mask = 1 << (channel & 0x1f);
662 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
663 pchregs->data.ainten &= ~mask;
664 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
667 static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
669 unsigned int idx = channel & 0x1f;
671 if (codec->synth.chcnt >= ALI_CHANNELS){
672 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
673 return -1;
676 if (!(codec->synth.chmap & (1 << idx))) {
677 codec->synth.chmap |= 1 << idx;
678 codec->synth.chcnt++;
679 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
680 return idx;
682 return -1;
685 static int snd_ali_find_free_channel(ali_t * codec, int rec)
687 int idx;
688 int result = -1;
690 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
692 // recording
693 if (rec) {
694 if (codec->spdif_support &&
695 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
696 idx = ALI_SPDIF_IN_CHANNEL;
697 else
698 idx = ALI_PCM_IN_CHANNEL;
700 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
701 return result;
702 } else {
703 snd_printk("ali_find_free_channel: record channel is busy now.\n");
704 return -1;
708 //playback...
709 if (codec->spdif_support &&
710 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
711 idx = ALI_SPDIF_OUT_CHANNEL;
712 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
713 return result;
714 } else {
715 snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
719 for (idx = 0; idx < ALI_CHANNELS; idx++) {
720 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
721 return result;
723 snd_printk("ali_find_free_channel: no free channels.\n");
724 return -1;
727 static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
729 unsigned int idx = channel & 0x0000001f;
731 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
733 if (channel < 0 || channel >= ALI_CHANNELS)
734 return;
736 if (!(codec->synth.chmap & (1 << idx))) {
737 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
738 return;
739 } else {
740 codec->synth.chmap &= ~(1 << idx);
741 codec->synth.chcnt--;
745 #if 0 // not used
746 static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
748 unsigned int mask = 1 << (channel & 0x1f);
750 snd_ali_printk("start_voice: channel=%d\n",channel);
751 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
753 #endif
755 static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
757 unsigned int mask = 1 << (channel & 0x1f);
759 snd_ali_printk("stop_voice: channel=%d\n",channel);
760 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
764 * S/PDIF Part
767 static void snd_ali_delay(ali_t *codec,int interval)
769 unsigned long begintimer,currenttimer;
771 begintimer = inl(ALI_REG(codec, ALI_STIMER));
772 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
774 while (currenttimer < begintimer + interval) {
775 if(snd_ali_stimer_ready(codec, 1) < 0)
776 break;
777 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
781 static void snd_ali_detect_spdif_rate(ali_t *codec)
783 u16 wval = 0;
784 u16 count = 0;
785 u8 bval = 0, R1 = 0, R2 = 0;
787 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
788 bval |= 0x1F;
789 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
791 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
792 count ++;
793 snd_ali_delay(codec, 6);
794 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
795 R1 = bval & 0x1F;
798 if (count > 50000) {
799 snd_printk("ali_detect_spdif_rate: timeout!\n");
800 return;
803 count = 0;
804 while (count++ <= 50000) {
805 snd_ali_delay(codec, 6);
806 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
807 R2 = bval & 0x1F;
808 if (R2 != R1) R1 = R2; else break;
811 if (count > 50000) {
812 snd_printk("ali_detect_spdif_rate: timeout!\n");
813 return;
816 if (R2 >= 0x0b && R2 <= 0x0e) {
817 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
818 wval &= 0xE0F0;
819 wval |= (u16)0x09 << 8 | (u16)0x05;
820 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
822 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
823 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
824 } else if (R2 == 0x12) {
825 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
826 wval &= 0xE0F0;
827 wval |= (u16)0x0E << 8 | (u16)0x08;
828 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
830 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
831 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
835 static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
837 u32 dwRate = 0;
838 u8 bval = 0;
840 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
841 bval &= 0x7F;
842 bval |= 0x40;
843 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
845 snd_ali_detect_spdif_rate(codec);
847 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
848 bval &= 0x0F;
850 if (bval == 0) dwRate = 44100;
851 if (bval == 1) dwRate = 48000;
852 if (bval == 2) dwRate = 32000;
854 return dwRate;
857 static void snd_ali_enable_spdif_in(ali_t *codec)
859 unsigned int dwVal;
861 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
862 dwVal |= ALI_SPDIF_IN_SUPPORT;
863 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
865 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
866 dwVal |= 0x02;
867 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
869 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
872 static void snd_ali_disable_spdif_in(ali_t *codec)
874 unsigned int dwVal;
876 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
877 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
878 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
880 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
884 static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
886 unsigned char bVal;
887 unsigned int dwRate = 0;
889 if (rate == 32000) dwRate = 0x300;
890 if (rate == 44100) dwRate = 0;
891 if (rate == 48000) dwRate = 0x200;
893 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
894 bVal &= (unsigned char)(~(1<<6));
896 bVal |= 0x80; //select right
897 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
898 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
900 bVal &= (~0x80); //select left
901 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
902 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
905 static void snd_ali_enable_spdif_out(ali_t *codec)
907 unsigned short wVal;
908 unsigned char bVal;
910 struct pci_dev *pci_dev = NULL;
912 pci_dev = codec->pci_m1533;
913 if (pci_dev == NULL)
914 return;
915 pci_read_config_byte(pci_dev, 0x61, &bVal);
916 bVal |= 0x40;
917 pci_write_config_byte(pci_dev, 0x61, bVal);
918 pci_read_config_byte(pci_dev, 0x7d, &bVal);
919 bVal |= 0x01;
920 pci_write_config_byte(pci_dev, 0x7d, bVal);
922 pci_read_config_byte(pci_dev, 0x7e, &bVal);
923 bVal &= (~0x20);
924 bVal |= 0x10;
925 pci_write_config_byte(pci_dev, 0x7e, bVal);
927 bVal = inb(ALI_REG(codec, ALI_SCTRL));
928 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
930 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
931 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
934 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
935 wVal |= ALI_SPDIF_OUT_SEL_PCM;
936 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
937 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
941 static void snd_ali_enable_spdif_chnout(ali_t *codec)
943 unsigned short wVal = 0;
945 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
946 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
947 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
949 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
950 if (flag & ALI_SPDIF_OUT_NON_PCM)
951 wVal |= 0x0002;
952 else
953 wVal &= (~0x0002);
954 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
956 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
959 static void snd_ali_disable_spdif_chnout(ali_t *codec)
961 unsigned short wVal = 0;
962 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
963 wVal |= ALI_SPDIF_OUT_SEL_PCM;
964 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
966 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
969 static void snd_ali_disable_spdif_out(ali_t *codec)
971 unsigned char bVal;
973 bVal = inb(ALI_REG(codec, ALI_SCTRL));
974 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
976 snd_ali_disable_spdif_chnout(codec);
979 static void snd_ali_update_ptr(ali_t *codec,int channel)
981 snd_ali_voice_t *pvoice = NULL;
982 snd_pcm_runtime_t *runtime;
983 snd_ali_channel_control_t *pchregs = NULL;
984 unsigned int old, mask;
985 #ifdef ALI_DEBUG
986 unsigned int temp, cspf;
987 #endif
989 pchregs = &(codec->chregs);
991 // check if interrupt occurred for channel
992 old = pchregs->data.aint;
993 mask = ((unsigned int) 1L) << (channel & 0x1f);
995 if (!(old & mask))
996 return;
998 pvoice = &codec->synth.voices[channel];
999 runtime = pvoice->substream->runtime;
1001 udelay(100);
1002 spin_lock(&codec->reg_lock);
1004 if (pvoice->pcm && pvoice->substream) {
1005 /* pcm interrupt */
1006 #ifdef ALI_DEBUG
1007 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
1008 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1009 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
1010 #endif
1011 if (pvoice->running) {
1012 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1013 spin_unlock(&codec->reg_lock);
1014 snd_pcm_period_elapsed(pvoice->substream);
1015 spin_lock(&codec->reg_lock);
1016 } else {
1017 snd_ali_stop_voice(codec, channel);
1018 snd_ali_disable_voice_irq(codec, channel);
1020 } else if (codec->synth.voices[channel].synth) {
1021 /* synth interrupt */
1022 } else if (codec->synth.voices[channel].midi) {
1023 /* midi interrupt */
1024 } else {
1025 /* unknown interrupt */
1026 snd_ali_stop_voice(codec, channel);
1027 snd_ali_disable_voice_irq(codec, channel);
1029 spin_unlock(&codec->reg_lock);
1030 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1031 pchregs->data.aint = old & (~mask);
1034 static void snd_ali_interrupt(ali_t * codec)
1036 int channel;
1037 unsigned int audio_int;
1038 snd_ali_channel_control_t *pchregs = NULL;
1039 pchregs = &(codec->chregs);
1041 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1042 if (audio_int & ADDRESS_IRQ) {
1043 // get interrupt status for all channels
1044 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1045 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1046 snd_ali_update_ptr(codec, channel);
1049 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1050 ALI_REG(codec,ALI_MISCINT));
1054 static irqreturn_t snd_ali_card_interrupt(int irq,
1055 void *dev_id,
1056 struct pt_regs *regs)
1058 ali_t *codec = dev_id;
1060 if (codec == NULL)
1061 return IRQ_NONE;
1062 snd_ali_interrupt(codec);
1063 return IRQ_HANDLED;
1067 static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec, int channel)
1069 snd_ali_voice_t *pvoice = NULL;
1070 unsigned long flags;
1071 int idx;
1073 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1075 spin_lock_irqsave(&codec->voice_alloc, flags);
1076 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1077 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1078 snd_ali_find_free_channel(codec,rec);
1079 if(idx < 0) {
1080 snd_printk("ali_alloc_voice: err.\n");
1081 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1082 return NULL;
1084 pvoice = &(codec->synth.voices[idx]);
1085 pvoice->use = 1;
1086 pvoice->pcm = 1;
1087 pvoice->mode = rec;
1088 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1089 return pvoice;
1091 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1092 return NULL;
1096 static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1098 unsigned long flags;
1099 void (*private_free)(void *);
1100 void *private_data;
1102 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1103 if (pvoice == NULL || !pvoice->use)
1104 return;
1105 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1106 spin_lock_irqsave(&codec->voice_alloc, flags);
1107 private_free = pvoice->private_free;
1108 private_data = pvoice->private_data;
1109 pvoice->private_free = NULL;
1110 pvoice->private_data = NULL;
1111 if (pvoice->pcm) {
1112 snd_ali_free_channel_pcm(codec, pvoice->number);
1114 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1115 pvoice->substream = NULL;
1116 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1117 if (private_free)
1118 private_free(private_data);
1122 static void snd_ali_clear_voices(ali_t * codec,
1123 unsigned int v_min,
1124 unsigned int v_max)
1126 unsigned int i;
1128 for (i = v_min; i <= v_max; i++) {
1129 snd_ali_stop_voice(codec, i);
1130 snd_ali_disable_voice_irq(codec, i);
1134 static void snd_ali_write_voice_regs(ali_t * codec,
1135 unsigned int Channel,
1136 unsigned int LBA,
1137 unsigned int CSO,
1138 unsigned int ESO,
1139 unsigned int DELTA,
1140 unsigned int ALPHA_FMS,
1141 unsigned int GVSEL,
1142 unsigned int PAN,
1143 unsigned int VOL,
1144 unsigned int CTRL,
1145 unsigned int EC)
1147 unsigned int ctlcmds[4];
1149 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1151 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1152 ctlcmds[1] = LBA;
1153 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1154 ctlcmds[3] = (GVSEL << 31) |
1155 ((PAN & 0x0000007f) << 24) |
1156 ((VOL & 0x000000ff) << 16) |
1157 ((CTRL & 0x0000000f) << 12) |
1158 (EC & 0x00000fff);
1160 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1162 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1163 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1164 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1165 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1167 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1168 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1171 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1173 unsigned int delta;
1175 if (rate < 4000) rate = 4000;
1176 if (rate > 48000) rate = 48000;
1178 if (rec) {
1179 if (rate == 44100)
1180 delta = 0x116a;
1181 else if (rate == 8000)
1182 delta = 0x6000;
1183 else if (rate == 48000)
1184 delta = 0x1000;
1185 else
1186 delta = ((48000 << 12) / rate) & 0x0000ffff;
1187 } else {
1188 if (rate == 44100)
1189 delta = 0xeb3;
1190 else if (rate == 8000)
1191 delta = 0x2ab;
1192 else if (rate == 48000)
1193 delta = 0x1000;
1194 else
1195 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1198 return delta;
1201 static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1203 unsigned int CTRL;
1204 snd_pcm_runtime_t *runtime = substream->runtime;
1206 /* set ctrl mode
1207 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1209 CTRL = 0x00000001;
1210 if (snd_pcm_format_width(runtime->format) == 16)
1211 CTRL |= 0x00000008; // 16-bit data
1212 if (!snd_pcm_format_unsigned(runtime->format))
1213 CTRL |= 0x00000002; // signed data
1214 if (runtime->channels > 1)
1215 CTRL |= 0x00000004; // stereo data
1216 return CTRL;
1220 * PCM part
1223 static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1224 unsigned int cmd, void *arg)
1226 return snd_pcm_lib_ioctl(substream, cmd, arg);
1229 static int snd_ali_trigger(snd_pcm_substream_t *substream,
1230 int cmd)
1233 ali_t *codec = snd_pcm_substream_chip(substream);
1234 struct list_head *pos;
1235 snd_pcm_substream_t *s;
1236 unsigned int what, whati, capture_flag;
1237 snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1238 unsigned int val;
1239 int do_start;
1241 switch (cmd) {
1242 case SNDRV_PCM_TRIGGER_START:
1243 case SNDRV_PCM_TRIGGER_RESUME:
1244 do_start = 1; break;
1245 case SNDRV_PCM_TRIGGER_STOP:
1246 case SNDRV_PCM_TRIGGER_SUSPEND:
1247 do_start = 0; break;
1248 default:
1249 return -EINVAL;
1252 what = whati = capture_flag = 0;
1253 snd_pcm_group_for_each(pos, substream) {
1254 s = snd_pcm_group_substream_entry(pos);
1255 if ((ali_t *) snd_pcm_substream_chip(s) == codec) {
1256 pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1257 evoice = pvoice->extra;
1258 what |= 1 << (pvoice->number & 0x1f);
1259 if (evoice == NULL) {
1260 whati |= 1 << (pvoice->number & 0x1f);
1261 } else {
1262 whati |= 1 << (evoice->number & 0x1f);
1263 what |= 1 << (evoice->number & 0x1f);
1265 if (do_start) {
1266 pvoice->running = 1;
1267 if (evoice != NULL)
1268 evoice->running = 1;
1269 } else {
1270 pvoice->running = 0;
1271 if (evoice != NULL)
1272 evoice->running = 0;
1274 snd_pcm_trigger_done(s, substream);
1275 if (pvoice->mode)
1276 capture_flag = 1;
1279 spin_lock(&codec->reg_lock);
1280 if (! do_start) {
1281 outl(what, ALI_REG(codec, ALI_STOP));
1283 val = inl(ALI_REG(codec, ALI_AINTEN));
1284 if (do_start) {
1285 val |= whati;
1286 } else {
1287 val &= ~whati;
1289 outl(val, ALI_REG(codec, ALI_AINTEN));
1290 if (do_start) {
1291 outl(what, ALI_REG(codec, ALI_START));
1293 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1294 spin_unlock(&codec->reg_lock);
1296 return 0;
1299 static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1300 snd_pcm_hw_params_t * hw_params)
1302 ali_t *codec = snd_pcm_substream_chip(substream);
1303 snd_pcm_runtime_t *runtime = substream->runtime;
1304 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1305 snd_ali_voice_t *evoice = pvoice->extra;
1306 int err;
1307 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1308 if (err < 0) return err;
1310 /* voice management */
1312 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1313 if (evoice == NULL) {
1314 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0, -1);
1315 if (evoice == NULL)
1316 return -ENOMEM;
1317 pvoice->extra = evoice;
1318 evoice->substream = substream;
1320 } else {
1321 if (evoice != NULL) {
1322 snd_ali_free_voice(codec, evoice);
1323 pvoice->extra = evoice = NULL;
1327 return 0;
1330 static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1332 ali_t *codec = snd_pcm_substream_chip(substream);
1333 snd_pcm_runtime_t *runtime = substream->runtime;
1334 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1335 snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1337 snd_pcm_lib_free_pages(substream);
1338 if (evoice != NULL) {
1339 snd_ali_free_voice(codec, evoice);
1340 pvoice->extra = NULL;
1342 return 0;
1345 static int snd_ali_hw_params(snd_pcm_substream_t * substream,
1346 snd_pcm_hw_params_t * hw_params)
1348 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1351 static int snd_ali_hw_free(snd_pcm_substream_t * substream)
1353 return snd_pcm_lib_free_pages(substream);
1356 static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1358 ali_t *codec = snd_pcm_substream_chip(substream);
1359 snd_pcm_runtime_t *runtime = substream->runtime;
1360 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1361 snd_ali_voice_t *evoice = pvoice->extra;
1362 unsigned long flags;
1364 unsigned int LBA;
1365 unsigned int Delta;
1366 unsigned int ESO;
1367 unsigned int CTRL;
1368 unsigned int GVSEL;
1369 unsigned int PAN;
1370 unsigned int VOL;
1371 unsigned int EC;
1373 snd_ali_printk("playback_prepare ...\n");
1375 spin_lock_irqsave(&codec->reg_lock, flags);
1377 /* set Delta (rate) value */
1378 Delta = snd_ali_convert_rate(runtime->rate, 0);
1380 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1381 (pvoice->number == ALI_PCM_IN_CHANNEL))
1382 snd_ali_disable_special_channel(codec, pvoice->number);
1383 else if (codec->spdif_support &&
1384 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1385 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1386 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1387 Delta = 0x1000;
1390 /* set Loop Back Address */
1391 LBA = runtime->dma_addr;
1393 /* set interrupt count size */
1394 pvoice->count = runtime->period_size;
1396 /* set target ESO for channel */
1397 pvoice->eso = runtime->buffer_size;
1399 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1401 /* set ESO to capture first MIDLP interrupt */
1402 ESO = pvoice->eso -1;
1403 /* set ctrl mode */
1404 CTRL = snd_ali_control_mode(substream);
1406 GVSEL = 1;
1407 PAN = 0;
1408 VOL = 0;
1409 EC = 0;
1410 snd_ali_printk("playback_prepare:\n ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1411 snd_ali_write_voice_regs( codec,
1412 pvoice->number,
1413 LBA,
1414 0, /* cso */
1415 ESO,
1416 Delta,
1417 0, /* alpha */
1418 GVSEL,
1419 PAN,
1420 VOL,
1421 CTRL,
1422 EC);
1423 if (evoice != NULL) {
1424 evoice->count = pvoice->count;
1425 evoice->eso = pvoice->count << 1;
1426 ESO = evoice->eso - 1;
1427 snd_ali_write_voice_regs(codec,
1428 evoice->number,
1429 LBA,
1430 0, /* cso */
1431 ESO,
1432 Delta,
1433 0, /* alpha */
1434 GVSEL,
1435 (unsigned int)0x7f,
1436 (unsigned int)0x3ff,
1437 CTRL,
1438 EC);
1440 spin_unlock_irqrestore(&codec->reg_lock, flags);
1441 return 0;
1445 static int snd_ali_prepare(snd_pcm_substream_t * substream)
1447 ali_t *codec = snd_pcm_substream_chip(substream);
1448 snd_pcm_runtime_t *runtime = substream->runtime;
1449 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1450 unsigned long flags;
1451 unsigned int LBA;
1452 unsigned int Delta;
1453 unsigned int ESO;
1454 unsigned int CTRL;
1455 unsigned int GVSEL;
1456 unsigned int PAN;
1457 unsigned int VOL;
1458 unsigned int EC;
1459 u8 bValue;
1461 spin_lock_irqsave(&codec->reg_lock, flags);
1463 snd_ali_printk("ali_prepare...\n");
1465 snd_ali_enable_special_channel(codec,pvoice->number);
1467 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1468 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1469 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1471 // Prepare capture intr channel
1472 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1474 unsigned int rate;
1476 if (codec->revision != ALI_5451_V02) {
1477 spin_unlock_irqrestore(&codec->reg_lock, flags);
1478 return -1;
1480 rate = snd_ali_get_spdif_in_rate(codec);
1481 if (rate == 0) {
1482 snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1483 rate = 48000;
1485 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1486 if (bValue & 0x10) {
1487 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1488 printk("clear SPDIF parity error flag.\n");
1491 if (rate != 48000)
1492 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1495 // set target ESO for channel
1496 pvoice->eso = runtime->buffer_size;
1498 // set interrupt count size
1499 pvoice->count = runtime->period_size;
1501 // set Loop Back Address
1502 LBA = runtime->dma_addr;
1504 // set ESO to capture first MIDLP interrupt
1505 ESO = pvoice->eso - 1;
1506 CTRL = snd_ali_control_mode(substream);
1507 GVSEL = 0;
1508 PAN = 0x00;
1509 VOL = 0x00;
1510 EC = 0;
1512 snd_ali_write_voice_regs( codec,
1513 pvoice->number,
1514 LBA,
1515 0, /* cso */
1516 ESO,
1517 Delta,
1518 0, /* alpha */
1519 GVSEL,
1520 PAN,
1521 VOL,
1522 CTRL,
1523 EC);
1526 spin_unlock_irqrestore(&codec->reg_lock, flags);
1528 return 0;
1532 static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1534 ali_t *codec = snd_pcm_substream_chip(substream);
1535 snd_pcm_runtime_t *runtime = substream->runtime;
1536 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1537 unsigned int cso;
1539 spin_lock(&codec->reg_lock);
1540 if (!pvoice->running) {
1541 spin_unlock(&codec->reg_lock);
1542 return 0;
1544 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1545 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1546 spin_unlock(&codec->reg_lock);
1547 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1549 return cso;
1553 static snd_pcm_uframes_t snd_ali_pointer(snd_pcm_substream_t *substream)
1555 ali_t *codec = snd_pcm_substream_chip(substream);
1556 snd_pcm_runtime_t *runtime = substream->runtime;
1557 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1558 unsigned int cso;
1559 unsigned long flags;
1561 spin_lock_irqsave(&codec->reg_lock, flags);
1562 if (!pvoice->running) {
1563 spin_unlock_irqrestore(&codec->reg_lock, flags);
1564 return 0;
1566 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1567 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1568 spin_unlock_irqrestore(&codec->reg_lock, flags);
1570 return cso;
1573 static snd_pcm_hardware_t snd_ali_playback =
1575 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1576 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1577 SNDRV_PCM_INFO_MMAP_VALID |
1578 SNDRV_PCM_INFO_RESUME |
1579 SNDRV_PCM_INFO_SYNC_START),
1580 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1581 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1582 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1583 .rate_min = 4000,
1584 .rate_max = 48000,
1585 .channels_min = 1,
1586 .channels_max = 2,
1587 .buffer_bytes_max = (256*1024),
1588 .period_bytes_min = 64,
1589 .period_bytes_max = (256*1024),
1590 .periods_min = 1,
1591 .periods_max = 1024,
1592 .fifo_size = 0,
1596 * Capture support device description
1599 static snd_pcm_hardware_t snd_ali_capture =
1601 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1602 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1603 SNDRV_PCM_INFO_MMAP_VALID |
1604 SNDRV_PCM_INFO_RESUME |
1605 SNDRV_PCM_INFO_SYNC_START),
1606 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1607 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1608 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1609 .rate_min = 4000,
1610 .rate_max = 48000,
1611 .channels_min = 1,
1612 .channels_max = 2,
1613 .buffer_bytes_max = (128*1024),
1614 .period_bytes_min = 64,
1615 .period_bytes_max = (128*1024),
1616 .periods_min = 1,
1617 .periods_max = 1024,
1618 .fifo_size = 0,
1621 static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1623 unsigned long flags;
1624 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1625 ali_t *codec;
1627 if (pvoice) {
1628 codec = pvoice->codec;
1629 spin_lock_irqsave(&codec->reg_lock, flags);
1630 snd_ali_free_voice(pvoice->codec, pvoice);
1631 spin_unlock_irqrestore(&codec->reg_lock, flags);
1635 static int snd_ali_open(snd_pcm_substream_t * substream, int rec, int channel,
1636 snd_pcm_hardware_t *phw)
1638 ali_t *codec = snd_pcm_substream_chip(substream);
1639 snd_pcm_runtime_t *runtime = substream->runtime;
1640 snd_ali_voice_t *pvoice;
1641 unsigned long flags = 0;
1643 spin_lock_irqsave(&codec->reg_lock, flags);
1644 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec, channel);
1645 if (pvoice == NULL) {
1646 spin_unlock_irqrestore(&codec->reg_lock, flags);
1647 return -EAGAIN;
1649 pvoice->codec = codec;
1650 spin_unlock_irqrestore(&codec->reg_lock, flags);
1652 pvoice->substream = substream;
1653 runtime->private_data = pvoice;
1654 runtime->private_free = snd_ali_pcm_free_substream;
1656 runtime->hw = *phw;
1657 snd_pcm_set_sync(substream);
1658 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1659 return 0;
1662 static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1664 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1667 static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1669 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1672 static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1674 return 0;
1677 static int snd_ali_close(snd_pcm_substream_t * substream)
1679 ali_t *codec = snd_pcm_substream_chip(substream);
1680 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) substream->runtime->private_data;
1682 snd_ali_disable_special_channel(codec,pvoice->number);
1684 return 0;
1687 static snd_pcm_ops_t snd_ali_playback_ops = {
1688 .open = snd_ali_playback_open,
1689 .close = snd_ali_playback_close,
1690 .ioctl = snd_ali_ioctl,
1691 .hw_params = snd_ali_playback_hw_params,
1692 .hw_free = snd_ali_playback_hw_free,
1693 .prepare = snd_ali_playback_prepare,
1694 .trigger = snd_ali_trigger,
1695 .pointer = snd_ali_playback_pointer,
1698 static snd_pcm_ops_t snd_ali_capture_ops = {
1699 .open = snd_ali_capture_open,
1700 .close = snd_ali_close,
1701 .ioctl = snd_ali_ioctl,
1702 .hw_params = snd_ali_hw_params,
1703 .hw_free = snd_ali_hw_free,
1704 .prepare = snd_ali_prepare,
1705 .trigger = snd_ali_trigger,
1706 .pointer = snd_ali_pointer,
1710 * Modem PCM
1713 static int snd_ali_modem_hw_params(snd_pcm_substream_t * substream,
1714 snd_pcm_hw_params_t * hw_params)
1716 ali_t *chip = snd_pcm_substream_chip(substream);
1717 unsigned int modem_num = chip->num_of_codecs - 1;
1718 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE, params_rate(hw_params));
1719 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1720 return snd_ali_hw_params(substream, hw_params);
1723 static snd_pcm_hardware_t snd_ali_modem =
1725 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1726 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1727 SNDRV_PCM_INFO_MMAP_VALID |
1728 SNDRV_PCM_INFO_RESUME |
1729 SNDRV_PCM_INFO_SYNC_START),
1730 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1731 .rates = SNDRV_PCM_RATE_KNOT|SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000,
1732 .rate_min = 8000,
1733 .rate_max = 16000,
1734 .channels_min = 1,
1735 .channels_max = 1,
1736 .buffer_bytes_max = (256*1024),
1737 .period_bytes_min = 64,
1738 .period_bytes_max = (256*1024),
1739 .periods_min = 1,
1740 .periods_max = 1024,
1741 .fifo_size = 0,
1744 static int snd_ali_modem_open(snd_pcm_substream_t * substream, int rec, int channel)
1746 static unsigned int rates [] = {8000,9600,12000,16000};
1747 static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
1748 .count = ARRAY_SIZE(rates),
1749 .list = rates,
1750 .mask = 0,
1752 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1753 if (err)
1754 return err;
1755 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1756 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1759 static int snd_ali_modem_playback_open(snd_pcm_substream_t * substream)
1761 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1764 static int snd_ali_modem_capture_open(snd_pcm_substream_t * substream)
1766 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1769 static snd_pcm_ops_t snd_ali_modem_playback_ops = {
1770 .open = snd_ali_modem_playback_open,
1771 .close = snd_ali_close,
1772 .ioctl = snd_pcm_lib_ioctl,
1773 .hw_params = snd_ali_modem_hw_params,
1774 .hw_free = snd_ali_hw_free,
1775 .prepare = snd_ali_prepare,
1776 .trigger = snd_ali_trigger,
1777 .pointer = snd_ali_pointer,
1780 static snd_pcm_ops_t snd_ali_modem_capture_ops = {
1781 .open = snd_ali_modem_capture_open,
1782 .close = snd_ali_close,
1783 .ioctl = snd_pcm_lib_ioctl,
1784 .hw_params = snd_ali_modem_hw_params,
1785 .hw_free = snd_ali_hw_free,
1786 .prepare = snd_ali_prepare,
1787 .trigger = snd_ali_trigger,
1788 .pointer = snd_ali_pointer,
1792 struct ali_pcm_description {
1793 char *name;
1794 unsigned int playback_num;
1795 unsigned int capture_num;
1796 snd_pcm_ops_t *playback_ops;
1797 snd_pcm_ops_t *capture_ops;
1801 static void snd_ali_pcm_free(snd_pcm_t *pcm)
1803 ali_t *codec = pcm->private_data;
1804 codec->pcm[pcm->device] = NULL;
1808 static int __devinit snd_ali_pcm(ali_t * codec, int device, struct ali_pcm_description *desc)
1810 snd_pcm_t *pcm;
1811 int err;
1813 err = snd_pcm_new(codec->card, desc->name, device,
1814 desc->playback_num, desc->capture_num, &pcm);
1815 if (err < 0) {
1816 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1817 return err;
1819 pcm->private_data = codec;
1820 pcm->private_free = snd_ali_pcm_free;
1821 pcm->info_flags = 0;
1822 if (desc->playback_ops)
1823 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, desc->playback_ops);
1824 if (desc->capture_ops)
1825 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, desc->capture_ops);
1827 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1828 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1830 pcm->info_flags = 0;
1831 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1832 strcpy(pcm->name, desc->name);
1833 codec->pcm[0] = pcm;
1834 return 0;
1837 static struct ali_pcm_description ali_pcms[] = {
1838 { "ALI 5451", ALI_CHANNELS, 1, &snd_ali_playback_ops, &snd_ali_capture_ops },
1839 { "ALI 5451 modem", 1, 1, &snd_ali_modem_playback_ops, &snd_ali_modem_capture_ops }
1842 static int __devinit snd_ali_build_pcms(ali_t *codec)
1844 int i, err;
1845 for(i = 0 ; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms) ; i++)
1846 if((err = snd_ali_pcm(codec, i, &ali_pcms[i])) < 0)
1847 return err;
1848 return 0;
1852 #define ALI5451_SPDIF(xname, xindex, value) \
1853 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1854 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1855 .put = snd_ali5451_spdif_put, .private_value = value}
1857 static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1859 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1860 uinfo->count = 1;
1861 uinfo->value.integer.min = 0;
1862 uinfo->value.integer.max = 1;
1863 return 0;
1866 static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1868 unsigned long flags;
1869 ali_t *codec = kcontrol->private_data;
1870 unsigned int enable;
1872 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1874 spin_lock_irqsave(&codec->reg_lock, flags);
1875 switch(kcontrol->private_value) {
1876 case 0:
1877 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1878 break;
1879 case 1:
1880 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1881 break;
1882 case 2:
1883 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1884 break;
1885 default:
1886 break;
1888 ucontrol->value.integer.value[0] = enable;
1889 spin_unlock_irqrestore(&codec->reg_lock, flags);
1890 return 0;
1893 static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1895 unsigned long flags;
1896 ali_t *codec = kcontrol->private_data;
1897 unsigned int change = 0, enable = 0;
1899 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1901 spin_lock_irqsave(&codec->reg_lock, flags);
1902 switch (kcontrol->private_value) {
1903 case 0:
1904 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1905 change = change ^ enable;
1906 if (change) {
1907 if (enable) {
1908 codec->spdif_mask |= 0x02;
1909 snd_ali_enable_spdif_out(codec);
1910 } else {
1911 codec->spdif_mask &= ~(0x02);
1912 codec->spdif_mask &= ~(0x04);
1913 snd_ali_disable_spdif_out(codec);
1916 break;
1917 case 1:
1918 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1919 change = change ^ enable;
1920 if (change && (codec->spdif_mask & 0x02)) {
1921 if (enable) {
1922 codec->spdif_mask |= 0x04;
1923 snd_ali_enable_spdif_chnout(codec);
1924 } else {
1925 codec->spdif_mask &= ~(0x04);
1926 snd_ali_disable_spdif_chnout(codec);
1929 break;
1930 case 2:
1931 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1932 change = change ^ enable;
1933 if (change) {
1934 if (enable) {
1935 codec->spdif_mask |= 0x01;
1936 snd_ali_enable_spdif_in(codec);
1937 } else {
1938 codec->spdif_mask &= ~(0x01);
1939 snd_ali_disable_spdif_in(codec);
1942 break;
1943 default:
1944 break;
1946 spin_unlock_irqrestore(&codec->reg_lock, flags);
1948 return change;
1951 static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1952 /* spdif aplayback switch */
1953 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1954 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1955 /* spdif out to spdif channel */
1956 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1957 /* spdif in from spdif channel */
1958 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1961 static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1963 ali_t *codec = bus->private_data;
1964 codec->ac97_bus = NULL;
1967 static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1969 ali_t *codec = ac97->private_data;
1970 codec->ac97[ac97->num] = NULL;
1973 static int __devinit snd_ali_mixer(ali_t * codec)
1975 ac97_template_t ac97;
1976 unsigned int idx;
1977 int i, err;
1978 static ac97_bus_ops_t ops = {
1979 .write = snd_ali_codec_write,
1980 .read = snd_ali_codec_read,
1983 if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1984 return err;
1985 codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1987 memset(&ac97, 0, sizeof(ac97));
1988 ac97.private_data = codec;
1989 ac97.private_free = snd_ali_mixer_free_ac97;
1991 for ( i = 0 ; i < codec->num_of_codecs ; i++) {
1992 ac97.num = i;
1993 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i])) < 0) {
1994 snd_printk("ali mixer %d creating error.\n", i);
1995 if(i == 0)
1996 return err;
1997 codec->num_of_codecs = 1;
1998 break;
2002 if (codec->spdif_support) {
2003 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
2004 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
2005 if (err < 0) return err;
2008 return 0;
2011 #ifdef CONFIG_PM
2012 static int ali_suspend(snd_card_t *card, pm_message_t state)
2014 ali_t *chip = card->pm_private_data;
2015 ali_image_t *im;
2016 int i, j;
2018 im = chip->image;
2019 if (! im)
2020 return 0;
2022 for(i = 0 ; i < chip->num_of_codecs ; i++) {
2023 if (chip->pcm[i])
2024 snd_pcm_suspend_all(chip->pcm[i]);
2025 if(chip->ac97[i])
2026 snd_ac97_suspend(chip->ac97[i]);
2029 spin_lock_irq(&chip->reg_lock);
2031 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
2032 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
2033 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
2035 // disable all IRQ bits
2036 outl(0, ALI_REG(chip, ALI_MISCINT));
2038 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2039 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
2040 continue;
2041 im->regs[i] = inl(ALI_REG(chip, i*4));
2044 for (i = 0; i < ALI_CHANNELS; i++) {
2045 outb(i, ALI_REG(chip, ALI_GC_CIR));
2046 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2047 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
2050 // stop all HW channel
2051 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
2053 spin_unlock_irq(&chip->reg_lock);
2054 pci_disable_device(chip->pci);
2055 return 0;
2058 static int ali_resume(snd_card_t *card)
2060 ali_t *chip = card->pm_private_data;
2061 ali_image_t *im;
2062 int i, j;
2064 im = chip->image;
2065 if (! im)
2066 return 0;
2068 pci_enable_device(chip->pci);
2070 spin_lock_irq(&chip->reg_lock);
2072 for (i = 0; i < ALI_CHANNELS; i++) {
2073 outb(i, ALI_REG(chip, ALI_GC_CIR));
2074 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2075 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2078 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2079 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
2080 continue;
2081 outl(im->regs[i], ALI_REG(chip, i*4));
2084 // start HW channel
2085 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2086 // restore IRQ enable bits
2087 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2089 spin_unlock_irq(&chip->reg_lock);
2091 for(i = 0 ; i < chip->num_of_codecs ; i++)
2092 if(chip->ac97[i])
2093 snd_ac97_resume(chip->ac97[i]);
2095 return 0;
2097 #endif /* CONFIG_PM */
2099 static int snd_ali_free(ali_t * codec)
2101 if (codec->hw_initialized)
2102 snd_ali_disable_address_interrupt(codec);
2103 if (codec->irq >= 0) {
2104 synchronize_irq(codec->irq);
2105 free_irq(codec->irq, (void *)codec);
2107 if (codec->port)
2108 pci_release_regions(codec->pci);
2109 pci_disable_device(codec->pci);
2110 #ifdef CONFIG_PM
2111 kfree(codec->image);
2112 #endif
2113 pci_dev_put(codec->pci_m1533);
2114 pci_dev_put(codec->pci_m7101);
2115 kfree(codec);
2116 return 0;
2119 static int snd_ali_chip_init(ali_t *codec)
2121 unsigned int legacy;
2122 unsigned char temp;
2123 struct pci_dev *pci_dev = NULL;
2125 snd_ali_printk("chip initializing ... \n");
2127 if (snd_ali_reset_5451(codec)) {
2128 snd_printk("ali_chip_init: reset 5451 error.\n");
2129 return -1;
2132 if (codec->revision == ALI_5451_V02) {
2133 pci_dev = codec->pci_m1533;
2134 pci_read_config_byte(pci_dev, 0x59, &temp);
2135 temp |= 0x80;
2136 pci_write_config_byte(pci_dev, 0x59, temp);
2138 pci_dev = codec->pci_m7101;
2139 pci_read_config_byte(pci_dev, 0xb8, &temp);
2140 temp |= 0x20;
2141 pci_write_config_byte(pci_dev, 0xB8, temp);
2144 pci_read_config_dword(codec->pci, 0x44, &legacy);
2145 legacy &= 0xff00ff00;
2146 legacy |= 0x000800aa;
2147 pci_write_config_dword(codec->pci, 0x44, legacy);
2149 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2150 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2151 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2152 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2153 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2155 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2156 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2157 if (codec->spdif_support) {
2158 snd_ali_enable_spdif_out(codec);
2159 codec->spdif_mask = 0x00000002;
2162 codec->num_of_codecs = 1;
2164 /* secondary codec - modem */
2165 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2166 codec->num_of_codecs++;
2167 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2168 (ALI_SCTRL_LINE_IN2|ALI_SCTRL_GPIO_IN2|ALI_SCTRL_LINE_OUT_EN),
2169 ALI_REG(codec, ALI_SCTRL));
2172 snd_ali_printk("chip initialize succeed.\n");
2173 return 0;
2177 /* proc for register dump */
2178 static void snd_ali_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buf)
2180 ali_t *codec = entry->private_data;
2181 int i;
2182 for(i = 0 ; i < 256 ; i+= 4)
2183 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2186 static void __devinit snd_ali_proc_init(ali_t *codec)
2188 snd_info_entry_t *entry;
2189 if(!snd_card_proc_new(codec->card, "ali5451", &entry))
2190 snd_info_set_text_ops(entry, codec, 1024, snd_ali_proc_read);
2193 static int __devinit snd_ali_resources(ali_t *codec)
2195 int err;
2197 snd_ali_printk("resouces allocation ...\n");
2198 if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2199 return err;
2200 codec->port = pci_resource_start(codec->pci, 0);
2202 if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2203 snd_printk("Unable to request irq.\n");
2204 return -EBUSY;
2206 codec->irq = codec->pci->irq;
2207 snd_ali_printk("resouces allocated.\n");
2208 return 0;
2210 static int snd_ali_dev_free(snd_device_t *device)
2212 ali_t *codec=device->device_data;
2213 snd_ali_free(codec);
2214 return 0;
2217 static int __devinit snd_ali_create(snd_card_t * card,
2218 struct pci_dev *pci,
2219 int pcm_streams,
2220 int spdif_support,
2221 ali_t ** r_ali)
2223 ali_t *codec;
2224 int i, err;
2225 unsigned short cmdw = 0;
2226 struct pci_dev *pci_dev = NULL;
2227 static snd_device_ops_t ops = {
2228 (snd_dev_free_t *)snd_ali_dev_free,
2229 NULL,
2230 NULL
2233 *r_ali = NULL;
2235 snd_ali_printk("creating ...\n");
2237 /* enable PCI device */
2238 if ((err = pci_enable_device(pci)) < 0)
2239 return err;
2240 /* check, if we can restrict PCI DMA transfers to 31 bits */
2241 if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
2242 pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2243 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2244 pci_disable_device(pci);
2245 return -ENXIO;
2248 if ((codec = kzalloc(sizeof(*codec), GFP_KERNEL)) == NULL) {
2249 pci_disable_device(pci);
2250 return -ENOMEM;
2253 spin_lock_init(&codec->reg_lock);
2254 spin_lock_init(&codec->voice_alloc);
2256 codec->card = card;
2257 codec->pci = pci;
2258 codec->irq = -1;
2259 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2260 codec->spdif_support = spdif_support;
2262 if (pcm_streams < 1)
2263 pcm_streams = 1;
2264 if (pcm_streams > 32)
2265 pcm_streams = 32;
2267 pci_set_master(pci);
2268 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2269 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2270 cmdw |= PCI_COMMAND_IO;
2271 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2273 pci_set_master(pci);
2275 if (snd_ali_resources(codec)) {
2276 snd_ali_free(codec);
2277 return -EBUSY;
2280 synchronize_irq(pci->irq);
2282 codec->synth.chmap = 0;
2283 codec->synth.chcnt = 0;
2284 codec->spdif_mask = 0;
2285 codec->synth.synthcount = 0;
2287 if (codec->revision == ALI_5451_V02)
2288 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2289 else
2290 codec->chregs.regs.ac97read = ALI_AC97_READ;
2291 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2293 codec->chregs.regs.start = ALI_START;
2294 codec->chregs.regs.stop = ALI_STOP;
2295 codec->chregs.regs.aint = ALI_AINT;
2296 codec->chregs.regs.ainten = ALI_AINTEN;
2298 codec->chregs.data.start = 0x00;
2299 codec->chregs.data.stop = 0x00;
2300 codec->chregs.data.aint = 0x00;
2301 codec->chregs.data.ainten = 0x00;
2303 /* M1533: southbridge */
2304 pci_dev = pci_get_device(0x10b9, 0x1533, NULL);
2305 codec->pci_m1533 = pci_dev;
2306 if (! codec->pci_m1533) {
2307 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2308 snd_ali_free(codec);
2309 return -ENODEV;
2311 /* M7101: power management */
2312 pci_dev = pci_get_device(0x10b9, 0x7101, NULL);
2313 codec->pci_m7101 = pci_dev;
2314 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2315 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2316 snd_ali_free(codec);
2317 return -ENODEV;
2320 snd_ali_printk("snd_device_new is called.\n");
2321 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2322 snd_ali_free(codec);
2323 return err;
2326 /* initialise synth voices*/
2327 for (i = 0; i < ALI_CHANNELS; i++ ) {
2328 codec->synth.voices[i].number = i;
2331 if ((err = snd_ali_chip_init(codec)) < 0) {
2332 snd_printk("ali create: chip init error.\n");
2333 return err;
2336 #ifdef CONFIG_PM
2337 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2338 if (! codec->image)
2339 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
2340 else
2341 snd_card_set_pm_callback(card, ali_suspend, ali_resume, codec);
2342 #endif
2344 snd_ali_enable_address_interrupt(codec);
2345 codec->hw_initialized = 1;
2347 *r_ali = codec;
2348 snd_ali_printk("created.\n");
2349 return 0;
2352 static int __devinit snd_ali_probe(struct pci_dev *pci,
2353 const struct pci_device_id *pci_id)
2355 static int dev;
2356 snd_card_t *card;
2357 ali_t *codec;
2358 int err;
2360 snd_ali_printk("probe ...\n");
2362 if (dev >= SNDRV_CARDS)
2363 return -ENODEV;
2364 if (!enable[dev]) {
2365 dev++;
2366 return -ENOENT;
2369 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2370 if (card == NULL)
2371 return -ENOMEM;
2373 if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2374 snd_card_free(card);
2375 return err;
2378 snd_ali_printk("mixer building ...\n");
2379 if ((err = snd_ali_mixer(codec)) < 0) {
2380 snd_card_free(card);
2381 return err;
2384 snd_ali_printk("pcm building ...\n");
2385 if ((err = snd_ali_build_pcms(codec)) < 0) {
2386 snd_card_free(card);
2387 return err;
2390 snd_ali_proc_init(codec);
2392 strcpy(card->driver, "ALI5451");
2393 strcpy(card->shortname, "ALI 5451");
2395 sprintf(card->longname, "%s at 0x%lx, irq %li",
2396 card->shortname, codec->port, codec->irq);
2398 snd_ali_printk("register card.\n");
2399 if ((err = snd_card_register(card)) < 0) {
2400 snd_card_free(card);
2401 return err;
2403 pci_set_drvdata(pci, card);
2404 dev++;
2405 return 0;
2408 static void __devexit snd_ali_remove(struct pci_dev *pci)
2410 snd_card_free(pci_get_drvdata(pci));
2411 pci_set_drvdata(pci, NULL);
2414 static struct pci_driver driver = {
2415 .name = "ALI 5451",
2416 .owner = THIS_MODULE,
2417 .id_table = snd_ali_ids,
2418 .probe = snd_ali_probe,
2419 .remove = __devexit_p(snd_ali_remove),
2420 SND_PCI_PM_CALLBACKS
2423 static int __init alsa_card_ali_init(void)
2425 return pci_register_driver(&driver);
2428 static void __exit alsa_card_ali_exit(void)
2430 pci_unregister_driver(&driver);
2433 module_init(alsa_card_ali_init)
2434 module_exit(alsa_card_ali_exit)