2 * Matt Wu <Matt_Wu@acersoftech.com.cn>
4 * Routines for control of ALi pci audio M5451
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public Lcodecnse as published by
14 * the Free Software Foundation; either version 2 of the Lcodecnse, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public Lcodecnse for more details.
22 * You should have received a copy of the GNU General Public Lcodecnse
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sound/driver.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/slab.h>
35 #include <linux/moduleparam.h>
36 #include <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
72 #define snd_ali_printk(format, args...) printk(format, ##args);
74 #define snd_ali_printk(format, args...)
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
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
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)
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
{
183 // register addresses
189 unsigned int ac97read
;
190 unsigned int ac97write
;
193 } snd_ali_channel_control_t
;
195 struct snd_ali_stru_voice
{
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 */
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
;
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
];
241 struct snd_stru_ali
{
244 unsigned char revision
;
246 unsigned int hw_initialized
: 1;
247 unsigned int spdif_support
: 1;
250 struct pci_dev
*pci_m1533
;
251 struct pci_dev
*pci_m7101
;
254 snd_pcm_t
*pcm
[MAX_CODECS
];
256 snd_ali_channel_control_t chregs
;
259 unsigned int spdif_mask
;
261 unsigned int spurious_irq_count
;
262 unsigned int spurious_irq_max_delta
;
264 unsigned int num_of_codecs
;
266 ac97_bus_t
*ac97_bus
;
267 ac97_t
*ac97
[MAX_CODECS
];
268 unsigned short ac97_ext_id
;
269 unsigned short ac97_ext_status
;
272 spinlock_t voice_alloc
;
279 static struct pci_device_id snd_ali_ids
[] = {
280 {0x10b9, 0x5451, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 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);
295 static void ali_read_regs(ali_t
*codec
, int channel
)
300 printk("channel %d registers map:\n", channel
);
301 outb((unsigned char)(channel
& 0x001f), ALI_REG(codec
,ALI_GC_CIR
));
305 printk("%2.2x ", j
*4);
308 for (i
=0; i
<=0xf8/4;i
++) {
310 printk("%2.2x ", (i
*4/0x10)*0x10);
311 dwVal
= inl(ALI_REG(codec
,i
*4));
312 printk("%8.8x ", dwVal
);
318 static void ali_read_cfg(unsigned int vendor
, unsigned deviceid
)
321 struct pci_dev
*pci_dev
;
324 pci_dev
= pci_get_device(vendor
, deviceid
, NULL
);
328 printk("\nM%x PCI CFG\n", deviceid
);
338 pci_read_config_dword(pci_dev
, i
*0x20+j
*4, &dwVal
);
339 printk("%8.8x ", dwVal
);
343 pci_dev_put(pci_dev
);
345 static void ali_read_ac97regs(ali_t
*codec
, int secondary
)
350 printk("\ncodec %d registers map:\n", secondary
);
354 printk("%2.2x ",j
*2);
357 for (i
=0; i
<64;i
++) {
359 printk("%2.2x ", (i
/8)*0x10);
360 wVal
= snd_ali_codec_peek(codec
, secondary
, i
*2);
361 printk("%4.4x ", wVal
);
374 static inline unsigned int snd_ali_5451_peek(ali_t
*codec
,
377 return (unsigned int)inl(ALI_REG(codec
, port
));
380 static inline void snd_ali_5451_poke( ali_t
*codec
,
384 outl((unsigned int)val
, ALI_REG(codec
, port
));
387 static int snd_ali_codec_ready( ali_t
*codec
,
391 unsigned long end_time
;
394 end_time
= jiffies
+ 10 * msecs_to_jiffies(250);
396 res
= snd_ali_5451_peek(codec
,port
);
397 if (! (res
& 0x8000))
400 set_current_state(TASK_UNINTERRUPTIBLE
);
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 ");
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);
419 dwChk2
= snd_ali_5451_peek(codec
, ALI_STIMER
);
420 if (dwChk2
!= dwChk1
)
423 set_current_state(TASK_UNINTERRUPTIBLE
);
426 } while (time_after_eq(end_time
, jiffies
));
427 snd_printk("ali_stimer_read: stimer is not ready.\n");
431 static void snd_ali_codec_poke(ali_t
*codec
,int secondary
,
435 unsigned int dwVal
= 0;
436 unsigned int port
= 0;
439 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg
);
443 port
= codec
->chregs
.regs
.ac97write
;
445 if (snd_ali_codec_ready(codec
, port
, 0) < 0)
447 if (snd_ali_stimer_ready(codec
, 0) < 0)
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
);
460 static unsigned short snd_ali_codec_peek( ali_t
*codec
,
464 unsigned int dwVal
= 0;
465 unsigned int port
= 0;
468 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg
);
472 port
= codec
->chregs
.regs
.ac97read
;
474 if (snd_ali_codec_ready(codec
, port
, 0) < 0)
476 if (snd_ali_stimer_ready(codec
, 0) < 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)
487 if (snd_ali_codec_ready(codec
, port
, 0) < 0)
490 return (snd_ali_5451_peek(codec
, port
) & 0xffff0000)>>16;
493 static void snd_ali_codec_write(ac97_t
*ac97
,
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
));
505 snd_ali_codec_poke(codec
, ac97
->num
, reg
, val
);
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
));
522 static int snd_ali_reset_5451(ali_t
*codec
)
524 struct pci_dev
*pci_dev
= NULL
;
525 unsigned short wCount
, wReg
;
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);
532 pci_read_config_dword(pci_dev
, 0x7c, &dwVal
);
533 pci_write_config_dword(pci_dev
, 0x7c, dwVal
& 0xf7ffffff);
537 pci_dev
= codec
->pci
;
538 pci_read_config_dword(pci_dev
, 0x44, &dwVal
);
539 pci_write_config_dword(pci_dev
, 0x44, dwVal
| 0x000c0000);
541 pci_read_config_dword(pci_dev
, 0x44, &dwVal
);
542 pci_write_config_dword(pci_dev
, 0x44, dwVal
& 0xfffbffff);
547 wReg
= snd_ali_codec_peek(codec
, 0, AC97_POWERDOWN
);
548 if((wReg
& 0x000f) == 0x000f)
553 /* non-fatal if you have a non PM capable codec */
554 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
560 static int snd_ali_reset_codec(ali_t
*codec
)
562 struct pci_dev
*pci_dev
= NULL
;
563 unsigned char bVal
= 0;
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);
572 pci_read_config_dword(pci_dev
, 0x7c, &dwVal
);
573 pci_write_config_dword(pci_dev
, 0x7c, dwVal
& 0xf7ffffff);
576 bVal
= inb(ALI_REG(codec
,ALI_SCTRL
));
578 outb(ALI_REG(codec
,ALI_SCTRL
),bVal
);
580 bVal
= inb(ALI_REG(codec
,ALI_SCTRL
));
582 outb(ALI_REG(codec
,ALI_SCTRL
),bVal
);
587 wReg
= snd_ali_codec_read(codec
->ac97
, AC97_POWERDOWN
);
588 if((wReg
& 0x000f) == 0x000f)
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
)
623 gc
= inl(ALI_REG(codec
, ALI_GC_CIR
));
626 outl( gc
, ALI_REG(codec
, ALI_GC_CIR
));
629 static void snd_ali_disable_address_interrupt(ali_t
* codec
)
633 gc
= inl(ALI_REG(codec
, ALI_GC_CIR
));
636 outl(gc
, ALI_REG(codec
, ALI_GC_CIR
));
640 static void snd_ali_enable_voice_irq(ali_t
*codec
, unsigned int channel
)
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
));
654 static void snd_ali_disable_voice_irq(ali_t
*codec
, unsigned int channel
)
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");
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
);
685 static int snd_ali_find_free_channel(ali_t
* codec
, int rec
)
690 snd_ali_printk("find_free_channel: for %s\n",rec
? "rec" : "pcm");
694 if (codec
->spdif_support
&&
695 (inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
)) & ALI_SPDIF_IN_SUPPORT
))
696 idx
= ALI_SPDIF_IN_CHANNEL
;
698 idx
= ALI_PCM_IN_CHANNEL
;
700 if ((result
= snd_ali_alloc_pcm_channel(codec
,idx
)) >= 0) {
703 snd_printk("ali_find_free_channel: record channel is busy now.\n");
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) {
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)
723 snd_printk("ali_find_free_channel: no free channels.\n");
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
)
736 if (!(codec
->synth
.chmap
& (1 << idx
))) {
737 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel
);
740 codec
->synth
.chmap
&= ~(1 << idx
);
741 codec
->synth
.chcnt
--;
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
));
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
));
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)
777 currenttimer
= inl(ALI_REG(codec
, ALI_STIMER
));
781 static void snd_ali_detect_spdif_rate(ali_t
*codec
)
785 u8 bval
= 0, R1
= 0, R2
= 0;
787 bval
= inb(ALI_REG(codec
,ALI_SPDIF_CTRL
+ 1));
789 outb(bval
,ALI_REG(codec
,ALI_SPDIF_CTRL
+ 1));
791 while (((R1
< 0x0B )||(R1
> 0x0E)) && (R1
!= 0x12) && count
<= 50000) {
793 snd_ali_delay(codec
, 6);
794 bval
= inb(ALI_REG(codec
,ALI_SPDIF_CTRL
+ 1));
799 snd_printk("ali_detect_spdif_rate: timeout!\n");
804 while (count
++ <= 50000) {
805 snd_ali_delay(codec
, 6);
806 bval
= inb(ALI_REG(codec
,ALI_SPDIF_CTRL
+ 1));
808 if (R2
!= R1
) R1
= R2
; else break;
812 snd_printk("ali_detect_spdif_rate: timeout!\n");
816 if (R2
>= 0x0b && R2
<= 0x0e) {
817 wval
= inw(ALI_REG(codec
,ALI_SPDIF_CTRL
+ 2));
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));
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
)
840 bval
= inb(ALI_REG(codec
,ALI_SPDIF_CTRL
));
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));
850 if (bval
== 0) dwRate
= 44100;
851 if (bval
== 1) dwRate
= 48000;
852 if (bval
== 2) dwRate
= 32000;
857 static void snd_ali_enable_spdif_in(ali_t
*codec
)
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
));
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
)
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
)
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
)
910 struct pci_dev
*pci_dev
= NULL
;
912 pci_dev
= codec
->pci_m1533
;
915 pci_read_config_byte(pci_dev
, 0x61, &bVal
);
917 pci_write_config_byte(pci_dev
, 0x61, bVal
);
918 pci_read_config_byte(pci_dev
, 0x7d, &bVal
);
920 pci_write_config_byte(pci_dev
, 0x7d, bVal
);
922 pci_read_config_byte(pci_dev
, 0x7e, &bVal
);
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)
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
)
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
;
986 unsigned int temp
, cspf
;
989 pchregs
= &(codec
->chregs
);
991 // check if interrupt occurred for channel
992 old
= pchregs
->data
.aint
;
993 mask
= ((unsigned int) 1L) << (channel
& 0x1f);
998 pvoice
= &codec
->synth
.voices
[channel
];
999 runtime
= pvoice
->substream
->runtime
;
1002 spin_lock(&codec
->reg_lock
);
1004 if (pvoice
->pcm
&& pvoice
->substream
) {
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
;
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
);
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 */
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
)
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
,
1056 struct pt_regs
*regs
)
1058 ali_t
*codec
= dev_id
;
1062 snd_ali_interrupt(codec
);
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
;
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
);
1080 snd_printk("ali_alloc_voice: err.\n");
1081 spin_unlock_irqrestore(&codec
->voice_alloc
, flags
);
1084 pvoice
= &(codec
->synth
.voices
[idx
]);
1088 spin_unlock_irqrestore(&codec
->voice_alloc
, flags
);
1091 spin_unlock_irqrestore(&codec
->voice_alloc
, flags
);
1096 static void snd_ali_free_voice(ali_t
* codec
, snd_ali_voice_t
*pvoice
)
1098 unsigned long flags
;
1099 void (*private_free
)(void *);
1102 snd_ali_printk("free_voice: channel=%d\n",pvoice
->number
);
1103 if (pvoice
== NULL
|| !pvoice
->use
)
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
;
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
);
1118 private_free(private_data
);
1122 static void snd_ali_clear_voices(ali_t
* codec
,
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
,
1140 unsigned int ALPHA_FMS
,
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);
1153 ctlcmds
[2] = (ESO
<< 16) | (DELTA
& 0x0ffff);
1154 ctlcmds
[3] = (GVSEL
<< 31) |
1155 ((PAN
& 0x0000007f) << 24) |
1156 ((VOL
& 0x000000ff) << 16) |
1157 ((CTRL
& 0x0000000f) << 12) |
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
)
1175 if (rate
< 4000) rate
= 4000;
1176 if (rate
> 48000) rate
= 48000;
1181 else if (rate
== 8000)
1183 else if (rate
== 48000)
1186 delta
= ((48000 << 12) / rate
) & 0x0000ffff;
1190 else if (rate
== 8000)
1192 else if (rate
== 48000)
1195 delta
= (((rate
<< 12) + rate
) / 48000) & 0x0000ffff;
1201 static unsigned int snd_ali_control_mode(snd_pcm_substream_t
*substream
)
1204 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1207 CTRL default: 8-bit (unsigned) mono, loop mode enabled
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
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
,
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
;
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;
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);
1262 whati
|= 1 << (evoice
->number
& 0x1f);
1263 what
|= 1 << (evoice
->number
& 0x1f);
1266 pvoice
->running
= 1;
1268 evoice
->running
= 1;
1270 pvoice
->running
= 0;
1272 evoice
->running
= 0;
1274 snd_pcm_trigger_done(s
, substream
);
1279 spin_lock(&codec
->reg_lock
);
1281 outl(what
, ALI_REG(codec
, ALI_STOP
));
1283 val
= inl(ALI_REG(codec
, ALI_AINTEN
));
1289 outl(val
, ALI_REG(codec
, ALI_AINTEN
));
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
);
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
;
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);
1317 pvoice
->extra
= evoice
;
1318 evoice
->substream
= substream
;
1321 if (evoice
!= NULL
) {
1322 snd_ali_free_voice(codec
, evoice
);
1323 pvoice
->extra
= evoice
= NULL
;
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
;
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
;
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
);
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;
1404 CTRL
= snd_ali_control_mode(substream
);
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
,
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
,
1436 (unsigned int)0x3ff,
1440 spin_unlock_irqrestore(&codec
->reg_lock
, flags
);
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
;
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
) {
1476 if (codec
->revision
!= ALI_5451_V02
) {
1477 spin_unlock_irqrestore(&codec
->reg_lock
, flags
);
1480 rate
= snd_ali_get_spdif_in_rate(codec
);
1482 snd_printk("ali_capture_preapre: spdif rate detect err!\n");
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");
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
);
1512 snd_ali_write_voice_regs( codec
,
1526 spin_unlock_irqrestore(&codec
->reg_lock
, flags
);
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
;
1539 spin_lock(&codec
->reg_lock
);
1540 if (!pvoice
->running
) {
1541 spin_unlock(&codec
->reg_lock
);
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
);
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
;
1559 unsigned long flags
;
1561 spin_lock_irqsave(&codec
->reg_lock
, flags
);
1562 if (!pvoice
->running
) {
1563 spin_unlock_irqrestore(&codec
->reg_lock
, flags
);
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
);
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
,
1587 .buffer_bytes_max
= (256*1024),
1588 .period_bytes_min
= 64,
1589 .period_bytes_max
= (256*1024),
1591 .periods_max
= 1024,
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
,
1613 .buffer_bytes_max
= (128*1024),
1614 .period_bytes_min
= 64,
1615 .period_bytes_max
= (128*1024),
1617 .periods_max
= 1024,
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
;
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
);
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
;
1657 snd_pcm_set_sync(substream
);
1658 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, 0, 64*1024);
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
)
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
);
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
,
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
,
1736 .buffer_bytes_max
= (256*1024),
1737 .period_bytes_min
= 64,
1738 .period_bytes_max
= (256*1024),
1740 .periods_max
= 1024,
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
),
1752 int err
= snd_ali_open(substream
, rec
, channel
, &snd_ali_modem
);
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
{
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
)
1813 err
= snd_pcm_new(codec
->card
, desc
->name
, device
,
1814 desc
->playback_num
, desc
->capture_num
, &pcm
);
1816 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
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
;
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
)
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)
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
;
1861 uinfo
->value
.integer
.min
= 0;
1862 uinfo
->value
.integer
.max
= 1;
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
) {
1877 enable
= (codec
->spdif_mask
& 0x02) ? 1 : 0;
1880 enable
= ((codec
->spdif_mask
& 0x02) && (codec
->spdif_mask
& 0x04)) ? 1 : 0;
1883 enable
= (codec
->spdif_mask
& 0x01) ? 1 : 0;
1888 ucontrol
->value
.integer
.value
[0] = enable
;
1889 spin_unlock_irqrestore(&codec
->reg_lock
, flags
);
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
) {
1904 change
= (codec
->spdif_mask
& 0x02) ? 1 : 0;
1905 change
= change
^ enable
;
1908 codec
->spdif_mask
|= 0x02;
1909 snd_ali_enable_spdif_out(codec
);
1911 codec
->spdif_mask
&= ~(0x02);
1912 codec
->spdif_mask
&= ~(0x04);
1913 snd_ali_disable_spdif_out(codec
);
1918 change
= (codec
->spdif_mask
& 0x04) ? 1 : 0;
1919 change
= change
^ enable
;
1920 if (change
&& (codec
->spdif_mask
& 0x02)) {
1922 codec
->spdif_mask
|= 0x04;
1923 snd_ali_enable_spdif_chnout(codec
);
1925 codec
->spdif_mask
&= ~(0x04);
1926 snd_ali_disable_spdif_chnout(codec
);
1931 change
= (codec
->spdif_mask
& 0x01) ? 1 : 0;
1932 change
= change
^ enable
;
1935 codec
->spdif_mask
|= 0x01;
1936 snd_ali_enable_spdif_in(codec
);
1938 codec
->spdif_mask
&= ~(0x01);
1939 snd_ali_disable_spdif_in(codec
);
1946 spin_unlock_irqrestore(&codec
->reg_lock
, flags
);
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
;
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)
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
++) {
1993 if ((err
= snd_ac97_mixer(codec
->ac97_bus
, &ac97
, &codec
->ac97
[i
])) < 0) {
1994 snd_printk("ali mixer %d creating error.\n", i
);
1997 codec
->num_of_codecs
= 1;
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
;
2012 static int ali_suspend(snd_card_t
*card
, pm_message_t state
)
2014 ali_t
*chip
= card
->pm_private_data
;
2022 for(i
= 0 ; i
< chip
->num_of_codecs
; i
++) {
2024 snd_pcm_suspend_all(chip
->pcm
[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
))
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
);
2058 static int ali_resume(snd_card_t
*card
)
2060 ali_t
*chip
= card
->pm_private_data
;
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
))
2081 outl(im
->regs
[i
], ALI_REG(chip
, i
*4));
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
++)
2093 snd_ac97_resume(chip
->ac97
[i
]);
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
);
2108 pci_release_regions(codec
->pci
);
2109 pci_disable_device(codec
->pci
);
2111 kfree(codec
->image
);
2113 pci_dev_put(codec
->pci_m1533
);
2114 pci_dev_put(codec
->pci_m7101
);
2119 static int snd_ali_chip_init(ali_t
*codec
)
2121 unsigned int legacy
;
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");
2132 if (codec
->revision
== ALI_5451_V02
) {
2133 pci_dev
= codec
->pci_m1533
;
2134 pci_read_config_byte(pci_dev
, 0x59, &temp
);
2136 pci_write_config_byte(pci_dev
, 0x59, temp
);
2138 pci_dev
= codec
->pci_m7101
;
2139 pci_read_config_byte(pci_dev
, 0xb8, &temp
);
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");
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
;
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
)
2197 snd_ali_printk("resouces allocation ...\n");
2198 if ((err
= pci_request_regions(codec
->pci
, "ALI 5451")) < 0)
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");
2206 codec
->irq
= codec
->pci
->irq
;
2207 snd_ali_printk("resouces allocated.\n");
2210 static int snd_ali_dev_free(snd_device_t
*device
)
2212 ali_t
*codec
=device
->device_data
;
2213 snd_ali_free(codec
);
2217 static int __devinit
snd_ali_create(snd_card_t
* card
,
2218 struct pci_dev
*pci
,
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
,
2235 snd_ali_printk("creating ...\n");
2237 /* enable PCI device */
2238 if ((err
= pci_enable_device(pci
)) < 0)
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
);
2248 if ((codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
)) == NULL
) {
2249 pci_disable_device(pci
);
2253 spin_lock_init(&codec
->reg_lock
);
2254 spin_lock_init(&codec
->voice_alloc
);
2259 pci_read_config_byte(pci
, PCI_REVISION_ID
, &codec
->revision
);
2260 codec
->spdif_support
= spdif_support
;
2262 if (pcm_streams
< 1)
2264 if (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
);
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
;
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
);
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
);
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
);
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");
2337 codec
->image
= kmalloc(sizeof(*codec
->image
), GFP_KERNEL
);
2339 snd_printk(KERN_WARNING
"can't allocate apm buffer\n");
2341 snd_card_set_pm_callback(card
, ali_suspend
, ali_resume
, codec
);
2344 snd_ali_enable_address_interrupt(codec
);
2345 codec
->hw_initialized
= 1;
2348 snd_ali_printk("created.\n");
2352 static int __devinit
snd_ali_probe(struct pci_dev
*pci
,
2353 const struct pci_device_id
*pci_id
)
2360 snd_ali_printk("probe ...\n");
2362 if (dev
>= SNDRV_CARDS
)
2369 card
= snd_card_new(index
[dev
], id
[dev
], THIS_MODULE
, 0);
2373 if ((err
= snd_ali_create(card
, pci
, pcm_channels
[dev
], spdif
[dev
], &codec
)) < 0) {
2374 snd_card_free(card
);
2378 snd_ali_printk("mixer building ...\n");
2379 if ((err
= snd_ali_mixer(codec
)) < 0) {
2380 snd_card_free(card
);
2384 snd_ali_printk("pcm building ...\n");
2385 if ((err
= snd_ali_build_pcms(codec
)) < 0) {
2386 snd_card_free(card
);
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
);
2403 pci_set_drvdata(pci
, card
);
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
= {
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
)