Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cs5530.c - Initialisation code for Cyrix/NatSemi VSA1 softaudio |
| 3 | * |
| 4 | * (C) Copyright 2007 Ash Willis <ashwillis@programmer.net> |
Alan Cox | 2f1e593 | 2008-10-27 15:21:19 +0000 | [diff] [blame] | 5 | * (C) Copyright 2003 Red Hat Inc <alan@lxorguk.ukuu.org.uk> |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 6 | * |
| 7 | * This driver was ported (shamelessly ripped ;) from oss/kahlua.c but I did |
| 8 | * mess with it a bit. The chip seems to have to have trouble with full duplex |
| 9 | * mode. If we're recording in 8bit 8000kHz, say, and we then attempt to |
| 10 | * simultaneously play back audio at 16bit 44100kHz, the device actually plays |
| 11 | * back in the same format in which it is capturing. By forcing the chip to |
| 12 | * always play/capture in 16/44100, we can let alsa-lib convert the samples and |
| 13 | * that way we can hack up some full duplex audio. |
| 14 | * |
| 15 | * XpressAudio(tm) is used on the Cyrix MediaGX (now NatSemi Geode) systems. |
| 16 | * The older version (VSA1) provides fairly good soundblaster emulation |
| 17 | * although there are a couple of bugs: large DMA buffers break record, |
| 18 | * and the MPU event handling seems suspect. VSA2 allows the native driver |
| 19 | * to control the AC97 audio engine directly and requires a different driver. |
| 20 | * |
| 21 | * Thanks to National Semiconductor for providing the needed information |
| 22 | * on the XpressAudio(tm) internals. |
| 23 | * |
| 24 | * This program is free software; you can redistribute it and/or modify it |
| 25 | * under the terms of the GNU General Public License as published by the |
| 26 | * Free Software Foundation; either version 2, or (at your option) any |
| 27 | * later version. |
| 28 | * |
| 29 | * This program is distributed in the hope that it will be useful, but |
| 30 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 32 | * General Public License for more details. |
| 33 | * |
| 34 | * TO DO: |
| 35 | * Investigate whether we can portably support Cognac (5520) in the |
| 36 | * same manner. |
| 37 | */ |
| 38 | |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 39 | #include <linux/delay.h> |
Paul Gortmaker | 65a7721 | 2011-07-15 13:13:37 -0400 | [diff] [blame] | 40 | #include <linux/module.h> |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 41 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 43 | #include <sound/core.h> |
| 44 | #include <sound/sb.h> |
| 45 | #include <sound/initval.h> |
| 46 | |
| 47 | MODULE_AUTHOR("Ash Willis"); |
| 48 | MODULE_DESCRIPTION("CS5530 Audio"); |
| 49 | MODULE_LICENSE("GPL"); |
| 50 | |
| 51 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
| 52 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; |
Takashi Iwai | cde9448 | 2011-12-19 10:32:48 +0100 | [diff] [blame] | 53 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; |
| 54 | |
| 55 | module_param_array(index, int, NULL, 0444); |
| 56 | MODULE_PARM_DESC(index, "Index value for CS5530 Audio driver."); |
| 57 | module_param_array(id, charp, NULL, 0444); |
| 58 | MODULE_PARM_DESC(id, "ID string for CS5530 Audio driver."); |
| 59 | module_param_array(enable, bool, NULL, 0444); |
| 60 | MODULE_PARM_DESC(enable, "Enable CS5530 Audio driver."); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 61 | |
| 62 | struct snd_cs5530 { |
| 63 | struct snd_card *card; |
| 64 | struct pci_dev *pci; |
| 65 | struct snd_sb *sb; |
| 66 | unsigned long pci_base; |
| 67 | }; |
| 68 | |
Alexey Dobriyan | cebe41d | 2010-02-06 00:21:03 +0200 | [diff] [blame] | 69 | static DEFINE_PCI_DEVICE_TABLE(snd_cs5530_ids) = { |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 70 | {PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_AUDIO, PCI_ANY_ID, |
| 71 | PCI_ANY_ID, 0, 0}, |
| 72 | {0,} |
| 73 | }; |
| 74 | |
| 75 | MODULE_DEVICE_TABLE(pci, snd_cs5530_ids); |
| 76 | |
| 77 | static int snd_cs5530_free(struct snd_cs5530 *chip) |
| 78 | { |
| 79 | pci_release_regions(chip->pci); |
| 80 | pci_disable_device(chip->pci); |
| 81 | kfree(chip); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int snd_cs5530_dev_free(struct snd_device *device) |
| 86 | { |
| 87 | struct snd_cs5530 *chip = device->device_data; |
| 88 | return snd_cs5530_free(chip); |
| 89 | } |
| 90 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 91 | static void snd_cs5530_remove(struct pci_dev *pci) |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 92 | { |
| 93 | snd_card_free(pci_get_drvdata(pci)); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 96 | static u8 snd_cs5530_mixer_read(unsigned long io, u8 reg) |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 97 | { |
| 98 | outb(reg, io + 4); |
| 99 | udelay(20); |
| 100 | reg = inb(io + 5); |
| 101 | udelay(20); |
| 102 | return reg; |
| 103 | } |
| 104 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 105 | static int snd_cs5530_create(struct snd_card *card, |
| 106 | struct pci_dev *pci, |
| 107 | struct snd_cs5530 **rchip) |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 108 | { |
| 109 | struct snd_cs5530 *chip; |
| 110 | unsigned long sb_base; |
| 111 | u8 irq, dma8, dma16 = 0; |
| 112 | u16 map; |
| 113 | void __iomem *mem; |
| 114 | int err; |
| 115 | |
| 116 | static struct snd_device_ops ops = { |
| 117 | .dev_free = snd_cs5530_dev_free, |
| 118 | }; |
| 119 | *rchip = NULL; |
| 120 | |
| 121 | err = pci_enable_device(pci); |
| 122 | if (err < 0) |
| 123 | return err; |
| 124 | |
| 125 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
| 126 | if (chip == NULL) { |
| 127 | pci_disable_device(pci); |
| 128 | return -ENOMEM; |
| 129 | } |
| 130 | |
| 131 | chip->card = card; |
| 132 | chip->pci = pci; |
| 133 | |
| 134 | err = pci_request_regions(pci, "CS5530"); |
| 135 | if (err < 0) { |
| 136 | kfree(chip); |
| 137 | pci_disable_device(pci); |
| 138 | return err; |
| 139 | } |
| 140 | chip->pci_base = pci_resource_start(pci, 0); |
| 141 | |
Arjan van de Ven | 2f5ad54 | 2008-09-28 16:20:09 -0700 | [diff] [blame] | 142 | mem = pci_ioremap_bar(pci, 0); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 143 | if (mem == NULL) { |
Takashi Iwai | 5a79839 | 2012-08-30 13:21:00 -0700 | [diff] [blame] | 144 | snd_cs5530_free(chip); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 145 | return -EBUSY; |
| 146 | } |
| 147 | |
| 148 | map = readw(mem + 0x18); |
| 149 | iounmap(mem); |
| 150 | |
| 151 | /* Map bits |
| 152 | 0:1 * 0x20 + 0x200 = sb base |
| 153 | 2 sb enable |
| 154 | 3 adlib enable |
| 155 | 5 MPU enable 0x330 |
| 156 | 6 MPU enable 0x300 |
| 157 | |
| 158 | The other bits may be used internally so must be masked */ |
| 159 | |
| 160 | sb_base = 0x220 + 0x20 * (map & 3); |
| 161 | |
| 162 | if (map & (1<<2)) |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 163 | dev_info(card->dev, "XpressAudio at 0x%lx\n", sb_base); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 164 | else { |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 165 | dev_err(card->dev, "Could not find XpressAudio!\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 166 | snd_cs5530_free(chip); |
| 167 | return -ENODEV; |
| 168 | } |
| 169 | |
| 170 | if (map & (1<<5)) |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 171 | dev_info(card->dev, "MPU at 0x300\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 172 | else if (map & (1<<6)) |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 173 | dev_info(card->dev, "MPU at 0x330\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 174 | |
| 175 | irq = snd_cs5530_mixer_read(sb_base, 0x80) & 0x0F; |
| 176 | dma8 = snd_cs5530_mixer_read(sb_base, 0x81); |
| 177 | |
| 178 | if (dma8 & 0x20) |
| 179 | dma16 = 5; |
| 180 | else if (dma8 & 0x40) |
| 181 | dma16 = 6; |
| 182 | else if (dma8 & 0x80) |
| 183 | dma16 = 7; |
| 184 | else { |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 185 | dev_err(card->dev, "No 16bit DMA enabled\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 186 | snd_cs5530_free(chip); |
| 187 | return -ENODEV; |
| 188 | } |
| 189 | |
| 190 | if (dma8 & 0x01) |
| 191 | dma8 = 0; |
| 192 | else if (dma8 & 02) |
| 193 | dma8 = 1; |
| 194 | else if (dma8 & 0x08) |
| 195 | dma8 = 3; |
| 196 | else { |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 197 | dev_err(card->dev, "No 8bit DMA enabled\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 198 | snd_cs5530_free(chip); |
| 199 | return -ENODEV; |
| 200 | } |
| 201 | |
| 202 | if (irq & 1) |
| 203 | irq = 9; |
| 204 | else if (irq & 2) |
| 205 | irq = 5; |
| 206 | else if (irq & 4) |
| 207 | irq = 7; |
| 208 | else if (irq & 8) |
| 209 | irq = 10; |
| 210 | else { |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 211 | dev_err(card->dev, "SoundBlaster IRQ not set\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 212 | snd_cs5530_free(chip); |
| 213 | return -ENODEV; |
| 214 | } |
| 215 | |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 216 | dev_info(card->dev, "IRQ: %d DMA8: %d DMA16: %d\n", irq, dma8, dma16); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 217 | |
| 218 | err = snd_sbdsp_create(card, sb_base, irq, snd_sb16dsp_interrupt, dma8, |
| 219 | dma16, SB_HW_CS5530, &chip->sb); |
| 220 | if (err < 0) { |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 221 | dev_err(card->dev, "Could not create SoundBlaster\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 222 | snd_cs5530_free(chip); |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | err = snd_sb16dsp_pcm(chip->sb, 0, &chip->sb->pcm); |
| 227 | if (err < 0) { |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 228 | dev_err(card->dev, "Could not create PCM\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 229 | snd_cs5530_free(chip); |
| 230 | return err; |
| 231 | } |
| 232 | |
| 233 | err = snd_sbmixer_new(chip->sb); |
| 234 | if (err < 0) { |
Takashi Iwai | 132b387 | 2014-02-25 14:13:16 +0100 | [diff] [blame] | 235 | dev_err(card->dev, "Could not create Mixer\n"); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 236 | snd_cs5530_free(chip); |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); |
| 241 | if (err < 0) { |
| 242 | snd_cs5530_free(chip); |
| 243 | return err; |
| 244 | } |
| 245 | |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 246 | *rchip = chip; |
| 247 | return 0; |
| 248 | } |
| 249 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 250 | static int snd_cs5530_probe(struct pci_dev *pci, |
| 251 | const struct pci_device_id *pci_id) |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 252 | { |
| 253 | static int dev; |
| 254 | struct snd_card *card; |
| 255 | struct snd_cs5530 *chip = NULL; |
| 256 | int err; |
| 257 | |
| 258 | if (dev >= SNDRV_CARDS) |
| 259 | return -ENODEV; |
| 260 | if (!enable[dev]) { |
| 261 | dev++; |
| 262 | return -ENOENT; |
| 263 | } |
| 264 | |
Takashi Iwai | 60c5772 | 2014-01-29 14:20:19 +0100 | [diff] [blame] | 265 | err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, |
| 266 | 0, &card); |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 267 | |
Takashi Iwai | e58de7b | 2008-12-28 16:44:30 +0100 | [diff] [blame] | 268 | if (err < 0) |
| 269 | return err; |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 270 | |
| 271 | err = snd_cs5530_create(card, pci, &chip); |
| 272 | if (err < 0) { |
| 273 | snd_card_free(card); |
| 274 | return err; |
| 275 | } |
| 276 | |
| 277 | strcpy(card->driver, "CS5530"); |
| 278 | strcpy(card->shortname, "CS5530 Audio"); |
| 279 | sprintf(card->longname, "%s at 0x%lx", card->shortname, chip->pci_base); |
| 280 | |
| 281 | err = snd_card_register(card); |
| 282 | if (err < 0) { |
| 283 | snd_card_free(card); |
| 284 | return err; |
| 285 | } |
| 286 | pci_set_drvdata(pci, card); |
| 287 | dev++; |
| 288 | return 0; |
| 289 | } |
| 290 | |
Takashi Iwai | e9f66d9 | 2012-04-24 12:25:00 +0200 | [diff] [blame] | 291 | static struct pci_driver cs5530_driver = { |
Takashi Iwai | 3733e42 | 2011-06-10 16:20:20 +0200 | [diff] [blame] | 292 | .name = KBUILD_MODNAME, |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 293 | .id_table = snd_cs5530_ids, |
| 294 | .probe = snd_cs5530_probe, |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 295 | .remove = snd_cs5530_remove, |
Takashi Iwai | 621887a | 2007-05-24 18:46:54 +0200 | [diff] [blame] | 296 | }; |
| 297 | |
Takashi Iwai | e9f66d9 | 2012-04-24 12:25:00 +0200 | [diff] [blame] | 298 | module_pci_driver(cs5530_driver); |