2 * Support for VIA 82Cxxx Audio Codecs
3 * Copyright 1999,2000 Jeff Garzik <jgarzik@mandrakesoft.com>
5 * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
6 * See the "COPYING" file distributed with this software for more info.
8 * Documentation for this driver available as
9 * linux/Documentation/sound/via82cxxx.txt.
11 * Since the mixer is called from the OSS glue the kernel lock is always held
16 #define VIA_VERSION "1.1.2.1"
20 #include <linux/module.h>
21 #include <linux/config.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/proc_fs.h>
32 #include "sound_config.h"
33 #include "soundmodule.h"
39 #define SOUND_LOCK do {} while (0)
40 #define SOUND_LOCK_END do {} while (0)
43 #define VIA_DEBUG 0 /* define to 1 to enable debugging output and checks */
45 /* note: prints function name for you */
46 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
48 #define DPRINTK(fmt, args...)
51 #define VIA_NDEBUG 0 /* define to 1 to disable lightweight runtime checks */
55 #define assert(expr) \
57 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
58 #expr,__FILE__,__FUNCTION__,__LINE__); \
62 #define arraysize(x) (sizeof(x)/sizeof(*(x)))
68 #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
69 #define VIA_MODULE_NAME "via_audio"
70 #define PFX VIA_MODULE_NAME ": "
72 #define VIA_COUNTER_LIMIT 100000
74 /* 82C686 function 5 (audio codec) PCI configuration registers */
75 #define VIA_FUNC_ENABLE 0x42
76 #define VIA_PNP_CONTROL 0x43
77 #define VIA_AC97_CTRL 0x80
79 /* PCI configuration register bits and masks */
80 #define VIA_CR40_AC97_READY 0x01
81 #define VIA_CR40_AC97_LOW_POWER 0x02
82 #define VIA_CR40_SECONDARY_READY 0x04
84 #define VIA_CR41_ACLINK_ENABLE 0x80
86 #define VIA_CR42_SB_ENABLE 0x01
87 #define VIA_CR42_MIDI_ENABLE 0x02
88 #define VIA_CR42_FM_ENABLE 0x04
89 #define VIA_CR42_GAME_ENABLE 0x08
91 #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
92 #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
94 #define VIA_CR80_FIRST_CODEC 0
95 #define VIA_CR80_SECOND_CODEC (1 << 30)
96 #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
97 #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
98 #define VIA_CR80_BUSY (1 << 24)
99 #define VIA_CR80_READ_MODE (1 << 23)
100 #define VIA_CR80_WRITE_MODE 0
101 #define VIA_CR80_REG_IDX(idx) (((idx) & 0x7E) << 16)
104 struct address_info sb_data
;
105 struct address_info opl3_data
;
106 struct pci_dev
*pdev
;
107 struct ac97_hwint ac97
;
111 static struct via_info cards
[MAX_CARDS
];
112 static unsigned num_cards
= 0;
115 static const struct {
117 const char *rev_name
;
118 } via_chip_revs
[] __initdata
= {
127 static inline void via_ac97_write32 (struct pci_dev
*pdev
, int port
, u32 data
)
129 struct resource
*rsrc
= &pdev
->resource
[0];
130 outw ((u16
)data
,rsrc
->start
+port
);
131 outw ((u16
)(data
>>16),rsrc
->start
+port
+2);
134 static inline u32
via_ac97_read32 (struct pci_dev
*pdev
, int port
)
136 struct resource
*rsrc
= &pdev
->resource
[0];
138 ((u32
)inw (rsrc
->start
+port
)) |
139 (((u32
)inw (rsrc
->start
+port
+2)) << 16);
142 /****************************************************************
144 * Intel Audio Codec '97 interface
149 static inline void via_ac97_wait_idle (struct pci_dev
*pdev
)
152 int counter
= VIA_COUNTER_LIMIT
;
156 assert (pdev
!= NULL
);
159 tmp
= via_ac97_read32 (pdev
,VIA_AC97_CTRL
);
160 } while ((tmp
& VIA_CR80_BUSY
) && (counter
-- > 0));
162 DPRINTK ("EXIT%s\n", counter
> 0 ? "" : ", counter limit reached");
166 static int via_ac97_read_reg (struct ac97_hwint
*dev
, u8 reg
)
169 struct via_info
*card
;
170 struct pci_dev
*pdev
;
174 assert (dev
!= NULL
);
175 assert (dev
->driver_private
!= NULL
);
177 card
= (struct via_info
*) dev
->driver_private
;
179 assert (pdev
!= NULL
);
181 via_ac97_wait_idle (pdev
);
182 data
= VIA_CR80_FIRST_CODEC
| VIA_CR80_FIRST_CODEC_VALID
|
183 VIA_CR80_READ_MODE
| VIA_CR80_REG_IDX(reg
);
184 via_ac97_write32 (pdev
,VIA_AC97_CTRL
,data
);
185 via_ac97_wait_idle (pdev
);
186 data
= via_ac97_read32 (pdev
,VIA_AC97_CTRL
);
189 if (! (data
& VIA_CR80_FIRST_CODEC_VALID
)) {
190 DPRINTK ("EXIT, first codec not valid, returning -1\n");
195 DPRINTK ("EXIT, returning %d\n", data
& 0xFFFF);
196 return data
& 0xFFFF;
200 static int via_ac97_write_reg (struct ac97_hwint
*dev
, u8 reg
, u16 value
)
203 struct via_info
*card
;
204 struct pci_dev
*pdev
;
208 assert (dev
!= NULL
);
209 assert (dev
->driver_private
!= NULL
);
211 card
= (struct via_info
*) dev
->driver_private
;
213 assert (pdev
!= NULL
);
215 via_ac97_wait_idle (pdev
);
216 data
= VIA_CR80_FIRST_CODEC
| VIA_CR80_FIRST_CODEC_VALID
|
217 VIA_CR80_WRITE_MODE
| VIA_CR80_REG_IDX(reg
) | value
;
218 via_ac97_write32 (pdev
,VIA_AC97_CTRL
,data
);
221 if (! (data
& VIA_CR80_FIRST_CODEC_VALID
)) {
222 DPRINTK ("EXIT, first codec invalid, returning -1\n");
227 DPRINTK ("EXIT, returning 0\n");
232 static int via_ac97_reset (struct ac97_hwint
*dev
)
234 struct via_info
*card
;
235 struct pci_dev
*pdev
;
239 assert (dev
!= NULL
);
240 assert (dev
->driver_private
!= NULL
);
242 card
= (struct via_info
*) dev
->driver_private
;
244 assert (pdev
!= NULL
);
246 pci_write_config_word (pdev
, PCI_COMMAND
, PCI_COMMAND_IO
);
248 DPRINTK ("EXIT, returning 0\n");
253 static struct via_info
*via_ac97_find_card_for_mixer (int dev
)
259 for (x
= 0; x
< num_cards
; x
++)
260 if (cards
[x
].mixer_oss_dev
== dev
) {
261 DPRINTK ("EXIT, returning %p\n", cards
+ x
);
265 DPRINTK ("EXIT, returning 0\n");
271 via_ac97_default_mixer_ioctl (int dev
, unsigned int cmd
, caddr_t arg
)
274 struct via_info
*card
= via_ac97_find_card_for_mixer (dev
);
279 rc
= ac97_mixer_ioctl (&card
->ac97
, cmd
, arg
);
280 DPRINTK ("EXIT, returning %d\n", rc
);
284 DPRINTK ("EXIT, returning -ENODEV\n");
289 static struct mixer_operations via_ac97_mixer_operations
=
292 "via82cxxxAC97Mixer",
293 via_ac97_default_mixer_ioctl
296 static int __init
via_attach_ac97 (struct via_info
*card
)
299 struct ac97_hwint
*mdev
;
303 assert (card
!= NULL
);
307 memset (mdev
, 0, sizeof (*mdev
));
308 mdev
->reset_device
= via_ac97_reset
;
309 mdev
->read_reg
= via_ac97_read_reg
;
310 mdev
->write_reg
= via_ac97_write_reg
;
311 mdev
->driver_private
= (void *) card
;
313 if (ac97_init (mdev
)) {
314 printk (KERN_ERR PFX
"Unable to init AC97\n");
315 DPRINTK ("EXIT, returning -1\n");
318 mixer
= sound_alloc_mixerdev ();
319 if (mixer
< 0 || num_mixers
>= MAX_MIXER_DEV
) {
320 printk (KERN_ERR PFX
"Unable to alloc mixerdev\n");
321 DPRINTK ("EXIT, returning -1\n");
324 mixer_devs
[mixer
] = &via_ac97_mixer_operations
;
325 card
->mixer_oss_dev
= mixer
;
327 /* Some reasonable default values. */
328 ac97_set_mixer (mdev
, SOUND_MIXER_VOLUME
, (85 << 8) | 85);
329 ac97_set_mixer (mdev
, SOUND_MIXER_SPEAKER
, 100);
330 ac97_set_mixer (mdev
, SOUND_MIXER_PCM
, (65 << 8) | 65);
331 ac97_set_mixer (mdev
, SOUND_MIXER_CD
, (65 << 8) | 65);
333 printk (KERN_INFO PFX
"Initialized AC97 mixer\n");
335 card
->have_ac97
= mixer
;
337 DPRINTK ("EXIT, returning 0\n");
342 static void via_unload_ac97 (struct via_info
*card
)
346 assert (card
!= NULL
);
348 if (card
->have_ac97
>= 0)
349 sound_unload_mixerdev (card
->have_ac97
);
355 #ifdef CONFIG_PROC_FS
357 /****************************************************************
359 * /proc/driver/via82cxxx/info
364 static int via_info_read_proc (char *page
, char **start
, off_t off
,
365 int count
, int *eof
, void *data
)
367 #define YN(val,bit) (((val) & (bit)) ? "yes" : "no")
370 u8 r40
, r41
, r42
, r44
;
374 len
+= sprintf (page
+len
, VIA_CARD_NAME
"\n\n");
376 for (i
= 0; i
< num_cards
; i
++) {
377 pci_read_config_byte (cards
[i
].pdev
, 0x40, &r40
);
378 pci_read_config_byte (cards
[i
].pdev
, 0x42, &r41
);
379 pci_read_config_byte (cards
[i
].pdev
, 0x42, &r42
);
380 pci_read_config_byte (cards
[i
].pdev
, 0x44, &r44
);
382 len
+= sprintf (page
+len
,
383 "40 AC97 Codec Ready: %s\n"
384 " AC97 Codec Low-power: %s\n"
385 " Secondary Codec Ready: %s\n"
387 "41 AC-Link Interface Enable: %s\n"
389 "42 Game port enabled: %s\n"
390 " SoundBlaster enabled: %s\n"
392 " MIDI enabled: %s\n"
394 "44 AC-Link Interface Access: %s\n"
395 " Secondary Codec Support: %s\n"
399 YN (r40
, VIA_CR40_AC97_READY
),
400 YN (r40
, VIA_CR40_AC97_LOW_POWER
),
401 YN (r40
, VIA_CR40_SECONDARY_READY
),
403 YN (r41
, VIA_CR41_ACLINK_ENABLE
),
405 YN (r42
, VIA_CR42_GAME_ENABLE
),
406 YN (r42
, VIA_CR42_SB_ENABLE
),
407 YN (r42
, VIA_CR42_FM_ENABLE
),
408 YN (r42
, VIA_CR42_MIDI_ENABLE
),
410 YN (r44
, VIA_CR44_AC_LINK_ACCESS
),
411 YN (r44
, VIA_CR44_SECOND_CODEC_SUPPORT
)
416 DPRINTK("EXIT, returning %d\n", len
);
423 /****************************************************************
425 * /proc/driver/via82cxxx
430 static int __init
via_init_proc (void)
434 proc_mkdir ("driver/via_audio", 0);
435 create_proc_read_entry ("driver/via_audio/info", 0, 0, via_info_read_proc
, NULL
);
443 static void __exit
via_cleanup_proc (void)
446 remove_proc_entry ("driver/via_audio/info", NULL
);
447 remove_proc_entry ("driver/via_audio", NULL
);
454 static inline int via_init_proc (void) { return 0; }
455 static inline void via_cleanup_proc (void) {}
457 #endif /* CONFIG_PROC_FS */
460 /****************************************************************
462 * Legacy SoundBlaster Pro, FM support via OSS
467 static void __init
via_attach_sb(struct address_info
*hw_config
)
471 if(!sb_dsp_init(hw_config
))
472 hw_config
->slots
[0] = -1;
478 static int __init
via_probe_sb(struct address_info
*hw_config
)
482 if (check_region(hw_config
->io_base
, 16))
484 printk(KERN_DEBUG PFX
"SBPro port 0x%x is already in use\n",
488 DPRINTK("EXIT after sb_dsp_detect\n");
489 return sb_dsp_detect(hw_config
, 0, 0, NULL
);
493 static void __exit
via_unload_sb(struct address_info
*hw_config
)
497 if(hw_config
->slots
[0] != -1)
498 sb_dsp_unload(hw_config
, 1);
504 static const struct {
509 } via_pnp_data
[] __initdata
= {
510 { 5, 0, 0x300, 0x220 },
511 { 7, 1, 0x310, 0x240 },
512 { 9, 2, 0x320, 0x260 },
513 { 10,3, 0x330, 0x280 },
517 /****************************************************************
519 * Chip setup and kernel registration
524 static int __init
via82cxxx_install (struct pci_dev
*pcidev
)
531 struct via_info
*card
= &cards
[num_cards
];
536 card
->have_ac97
= -1;
538 /* turn off legacy features, if not already */
539 pci_read_config_byte (pcidev
, VIA_FUNC_ENABLE
, &tmp8
);
540 tmp8
&= ~(VIA_CR42_SB_ENABLE
| VIA_CR42_MIDI_ENABLE
|
542 pci_write_config_byte (pcidev
, VIA_FUNC_ENABLE
, tmp8
);
545 * try to init AC97 mixer device
547 rc
= via_attach_ac97 (card
);
549 printk (KERN_WARNING PFX
550 "AC97 init failed, SB legacy mode only\n");
553 /* turn on legacy features */
554 pci_read_config_byte (pcidev
, VIA_FUNC_ENABLE
, &tmp8
);
555 tmp8
|= VIA_CR42_SB_ENABLE
| VIA_CR42_MIDI_ENABLE
|
557 pci_write_config_byte (pcidev
, VIA_FUNC_ENABLE
, tmp8
);
559 /* read legacy PNP info byte */
560 pci_read_config_byte (pcidev
, VIA_PNP_CONTROL
, &tmp8
);
561 pci_write_config_byte (pcidev
, VIA_PNP_CONTROL
, tmp8
);
563 sb_irq
= via_pnp_data
[((tmp8
>> 6) & 0x03)].sb_irq
;
564 sb_dma
= via_pnp_data
[((tmp8
>> 4) & 0x03)].sb_dma
;
565 midi_base
= via_pnp_data
[((tmp8
>> 2) & 0x03)].midi_base
;
566 sb_io_base
= via_pnp_data
[(tmp8
& 0x03)].sb_io_base
;
570 printk(KERN_INFO PFX
"legacy "
571 "MIDI: 0x%X, SB: 0x%X / %d IRQ / %d DMA\n",
572 midi_base
, sb_io_base
, sb_irq
, sb_dma
);
574 card
->sb_data
.name
= VIA_CARD_NAME
;
575 card
->sb_data
.card_subtype
= MDL_SBPRO
;
576 card
->sb_data
.io_base
= sb_io_base
;
577 card
->sb_data
.irq
= sb_irq
;
578 card
->sb_data
.dma
= sb_dma
;
580 /* register legacy SoundBlaster Pro */
581 if (!via_probe_sb (&card
->sb_data
)) {
583 "SB probe @ 0x%X failed, aborting\n",
585 DPRINTK ("EXIT, returning -1\n");
588 via_attach_sb (&card
->sb_data
);
590 card
->opl3_data
.name
= card
->sb_data
.name
;
591 card
->opl3_data
.io_base
= midi_base
;
592 card
->opl3_data
.irq
= -1;
594 /* register legacy MIDI */
595 if (!probe_uart401 (&card
->opl3_data
)) {
596 printk (KERN_WARNING PFX
597 "MIDI probe @ 0x%X failed, continuing\n",
599 card
->opl3_data
.io_base
= 0;
601 attach_uart401 (&card
->opl3_data
);
605 DPRINTK ("EXIT, returning 0\n");
611 * This loop walks the PCI configuration database and finds where
612 * the sound cards are.
614 * Note - only a single PCI scan occurs, eliminating possibility
615 * of multiple audio chips
619 static int __init
probe_via82cxxx (void)
621 struct pci_dev
*pcidev
= NULL
;
625 pcidev
= pci_find_device (PCI_VENDOR_ID_VIA
,
626 PCI_DEVICE_ID_VIA_82C686_5
, NULL
);
628 if (!pcidev
|| via82cxxx_install (pcidev
) != 0) {
629 printk (KERN_ERR PFX
"audio init failed\n");
630 DPRINTK ("EXIT, returning -1\n");
634 DPRINTK ("EXIT, returning 0\n");
640 * This function is called when the user or kernel loads the
641 * module into memory.
645 static int __init
init_via82cxxx_module(void)
649 const char *rev
= "unknown!";
651 memset (cards
, 0, sizeof (cards
));
655 if (!pci_present ()) {
656 printk (KERN_DEBUG PFX
"PCI not present, exiting\n");
657 DPRINTK ("EXIT, returning -ENODEV\n");
661 if (probe_via82cxxx() != 0) {
662 printk(KERN_ERR PFX
"probe failed, aborting\n");
663 /* XXX unload cards registered so far, if any */
664 DPRINTK ("EXIT, returning -ENODEV\n");
668 pci_read_config_byte (cards
[0].pdev
, PCI_REVISION_ID
, &tmp
);
669 for (i
= 0; i
< arraysize(via_chip_revs
); i
++)
670 if (via_chip_revs
[i
].revision
== tmp
) {
671 rev
= via_chip_revs
[i
].rev_name
;
674 printk (KERN_INFO PFX VIA_CARD_NAME
" loaded\n");
675 printk (KERN_INFO PFX
"Chip rev %s. Features: SBPro compat%s%s\n",
677 cards
[0].opl3_data
.io_base
== 0 ? "" : ", MPU-401 MIDI",
678 cards
[0].have_ac97
== -1 ? "" : ", AC97 mixer");
680 if (via_init_proc () != 0) {
681 printk (KERN_WARNING PFX
682 "Unable to init experimental /proc, ignoring\n");
686 * Binds us to the sound subsystem
689 DPRINTK ("EXIT, returning 0\n");
694 * This is called when it is removed. It will only be removed
695 * when its use count is 0. For sound the SOUND_LOCK/SOUND_UNLOCK
696 * macros hide the entire work for this.
699 static void __exit
cleanup_via82cxxx_module(void)
703 if (cards
[0].opl3_data
.io_base
)
704 unload_uart401 (&cards
[0].opl3_data
);
706 via_unload_sb (&cards
[0].sb_data
);
708 via_unload_ac97 (&cards
[0]);
713 * Final clean up with the sound layer
720 module_init(init_via82cxxx_module
);
721 module_exit(cleanup_via82cxxx_module
);