Merge with Linux 2.3.99-pre4.
[linux-2.6/linux-mips.git] / drivers / sound / via82cxxx_audio.c
blob7f88b8177987110ef64724acf7070032ea7d3df9
1 /*
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
12 * on our AC97 mixing
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>
24 #include <linux/fs.h>
25 #include <linux/kernel.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/proc_fs.h>
30 #include <asm/io.h>
32 #include "sound_config.h"
33 #include "soundmodule.h"
34 #include "sb.h"
35 #include "ac97.h"
36 #include "mpu401.h"
38 #ifndef SOUND_LOCK
39 #define SOUND_LOCK do {} while (0)
40 #define SOUND_LOCK_END do {} while (0)
41 #endif
43 #define VIA_DEBUG 0 /* define to 1 to enable debugging output and checks */
44 #if VIA_DEBUG
45 /* note: prints function name for you */
46 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
47 #else
48 #define DPRINTK(fmt, args...)
49 #endif
51 #define VIA_NDEBUG 0 /* define to 1 to disable lightweight runtime checks */
52 #if VIA_NDEBUG
53 #define assert(expr)
54 #else
55 #define assert(expr) \
56 if(!(expr)) { \
57 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
58 #expr,__FILE__,__FUNCTION__,__LINE__); \
60 #endif
62 #define arraysize(x) (sizeof(x)/sizeof(*(x)))
64 #define MAX_CARDS 2
66 #define LINE_SIZE 10
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)
103 struct via_info {
104 struct address_info sb_data;
105 struct address_info opl3_data;
106 struct pci_dev *pdev;
107 struct ac97_hwint ac97;
108 int mixer_oss_dev;
109 int have_ac97;
111 static struct via_info cards [MAX_CARDS];
112 static unsigned num_cards = 0;
115 static const struct {
116 int revision;
117 const char *rev_name;
118 } via_chip_revs[] __initdata = {
119 { 0x10, "A" },
120 { 0x11, "B" },
121 { 0x12, "C" },
122 { 0x13, "D" },
123 { 0x14, "E" },
124 { 0x20, "H" },
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];
137 return
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)
151 u32 tmp;
152 int counter = VIA_COUNTER_LIMIT;
154 DPRINTK ("ENTER\n");
156 assert (pdev != NULL);
158 do {
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)
168 u32 data;
169 struct via_info *card;
170 struct pci_dev *pdev;
172 DPRINTK ("ENTER\n");
174 assert (dev != NULL);
175 assert (dev->driver_private != NULL);
177 card = (struct via_info *) dev->driver_private;
178 pdev = card->pdev;
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);
188 #if 0
189 if (! (data & VIA_CR80_FIRST_CODEC_VALID)) {
190 DPRINTK ("EXIT, first codec not valid, returning -1\n");
191 return -1;
193 #endif
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)
202 u32 data;
203 struct via_info *card;
204 struct pci_dev *pdev;
206 DPRINTK ("ENTER\n");
208 assert (dev != NULL);
209 assert (dev->driver_private != NULL);
211 card = (struct via_info *) dev->driver_private;
212 pdev = card->pdev;
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);
220 #if 0
221 if (! (data & VIA_CR80_FIRST_CODEC_VALID)) {
222 DPRINTK ("EXIT, first codec invalid, returning -1\n");
223 return -1;
225 #endif
227 DPRINTK ("EXIT, returning 0\n");
228 return 0;
232 static int via_ac97_reset (struct ac97_hwint *dev)
234 struct via_info *card;
235 struct pci_dev *pdev;
237 DPRINTK ("ENTER\n");
239 assert (dev != NULL);
240 assert (dev->driver_private != NULL);
242 card = (struct via_info *) dev->driver_private;
243 pdev = card->pdev;
244 assert (pdev != NULL);
246 pci_write_config_word (pdev, PCI_COMMAND, PCI_COMMAND_IO);
248 DPRINTK ("EXIT, returning 0\n");
249 return 0;
253 static struct via_info *via_ac97_find_card_for_mixer (int dev)
255 int x;
257 DPRINTK ("ENTER\n");
259 for (x = 0; x < num_cards; x++)
260 if (cards[x].mixer_oss_dev == dev) {
261 DPRINTK ("EXIT, returning %p\n", cards + x);
262 return cards + x;
265 DPRINTK ("EXIT, returning 0\n");
266 return NULL;
270 static int
271 via_ac97_default_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
273 int rc;
274 struct via_info *card = via_ac97_find_card_for_mixer (dev);
276 DPRINTK ("ENTER\n");
278 if (card != NULL) {
279 rc = ac97_mixer_ioctl (&card->ac97, cmd, arg);
280 DPRINTK ("EXIT, returning %d\n", rc);
281 return rc;
284 DPRINTK ("EXIT, returning -ENODEV\n");
285 return -ENODEV;
289 static struct mixer_operations via_ac97_mixer_operations =
291 "VIA82Cxxx",
292 "via82cxxxAC97Mixer",
293 via_ac97_default_mixer_ioctl
296 static int __init via_attach_ac97 (struct via_info *card)
298 int mixer;
299 struct ac97_hwint *mdev;
301 DPRINTK ("ENTER\n");
303 assert (card != NULL);
305 mdev = &card->ac97;
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");
316 return -1;
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");
322 return -1;
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");
338 return 0;
342 static void via_unload_ac97 (struct via_info *card)
344 DPRINTK ("ENTER\n");
346 assert (card != NULL);
348 if (card->have_ac97 >= 0)
349 sound_unload_mixerdev (card->have_ac97);
351 DPRINTK ("EXIT\n");
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")
369 int len = 0, i;
370 u8 r40, r41, r42, r44;
372 DPRINTK ("ENTER\n");
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"
391 " FM enabled: %s\n"
392 " MIDI enabled: %s\n"
394 "44 AC-Link Interface Access: %s\n"
395 " Secondary Codec Support: %s\n"
397 "\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);
417 return len;
419 #undef YN
423 /****************************************************************
425 * /proc/driver/via82cxxx
430 static int __init via_init_proc (void)
432 DPRINTK ("ENTER\n");
434 proc_mkdir ("driver/via_audio", 0);
435 create_proc_read_entry ("driver/via_audio/info", 0, 0, via_info_read_proc, NULL);
437 DPRINTK("EXIT\n");
438 return 0;
443 static void __exit via_cleanup_proc (void)
445 DPRINTK ("ENTER\n");
446 remove_proc_entry ("driver/via_audio/info", NULL);
447 remove_proc_entry ("driver/via_audio", NULL);
448 DPRINTK("EXIT\n");
452 #else
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)
469 DPRINTK ("ENTER\n");
471 if(!sb_dsp_init(hw_config))
472 hw_config->slots[0] = -1;
474 DPRINTK("EXIT\n");
478 static int __init via_probe_sb(struct address_info *hw_config)
480 DPRINTK ("ENTER\n");
482 if (check_region(hw_config->io_base, 16))
484 printk(KERN_DEBUG PFX "SBPro port 0x%x is already in use\n",
485 hw_config->io_base);
486 return 0;
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)
495 DPRINTK ("ENTER\n");
497 if(hw_config->slots[0] != -1)
498 sb_dsp_unload(hw_config, 1);
500 DPRINTK("EXIT\n");
504 static const struct {
505 int sb_irq,
506 sb_dma,
507 midi_base,
508 sb_io_base;
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)
526 int sb_io_base;
527 int sb_irq;
528 int sb_dma;
529 int midi_base, rc;
530 u8 tmp8;
531 struct via_info *card = &cards[num_cards];
533 DPRINTK ("ENTER\n");
535 card->pdev = pcidev;
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 |
541 VIA_CR42_FM_ENABLE);
542 pci_write_config_byte (pcidev, VIA_FUNC_ENABLE, tmp8);
545 * try to init AC97 mixer device
547 rc = via_attach_ac97 (card);
548 if (rc) {
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 |
556 VIA_CR42_FM_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;
568 udelay(100);
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)) {
582 printk (KERN_ERR PFX
583 "SB probe @ 0x%X failed, aborting\n",
584 sb_io_base);
585 DPRINTK ("EXIT, returning -1\n");
586 return -1;
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",
598 midi_base);
599 card->opl3_data.io_base = 0;
600 } else {
601 attach_uart401 (&card->opl3_data);
604 num_cards++;
605 DPRINTK ("EXIT, returning 0\n");
606 return 0;
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;
623 DPRINTK ("ENTER\n");
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");
631 return -1;
634 DPRINTK ("EXIT, returning 0\n");
635 return 0;
640 * This function is called when the user or kernel loads the
641 * module into memory.
645 static int __init init_via82cxxx_module(void)
647 u8 tmp;
648 int i;
649 const char *rev = "unknown!";
651 memset (cards, 0, sizeof (cards));
653 DPRINTK ("ENTER\n");
655 if (!pci_present ()) {
656 printk (KERN_DEBUG PFX "PCI not present, exiting\n");
657 DPRINTK ("EXIT, returning -ENODEV\n");
658 return -ENODEV;
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");
665 return -ENODEV;
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;
672 break;
674 printk (KERN_INFO PFX VIA_CARD_NAME " loaded\n");
675 printk (KERN_INFO PFX "Chip rev %s. Features: SBPro compat%s%s\n",
676 rev,
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
688 SOUND_LOCK;
689 DPRINTK ("EXIT, returning 0\n");
690 return 0;
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)
701 DPRINTK("ENTER\n");
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]);
710 via_cleanup_proc ();
713 * Final clean up with the sound layer
715 SOUND_LOCK_END;
717 DPRINTK("EXIT\n");
720 module_init(init_via82cxxx_module);
721 module_exit(cleanup_via82cxxx_module);