1 /*
2 * cs5530.c - Initialisation code for Cyrix/NatSemi VSA1 softaudio
3 *
4 * (C) Copyright 2007 Ash Willis <ashwillis@programmer.net>
5 * (C) Copyright 2003 Red Hat Inc <alan@lxorguk.ukuu.org.uk>
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
39 #include <linux/delay.h>
40 #include <linux/module.h>
41 #include <linux/pci.h>
42 #include <linux/slab.h>
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;
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.");
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
69 static DEFINE_PCI_DEVICE_TABLE(snd_cs5530_ids) = {
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
snd_cs5530_free(struct snd_cs5530 * chip)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
snd_cs5530_dev_free(struct snd_device * device)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
snd_cs5530_remove(struct pci_dev * pci)91 static void __devexit snd_cs5530_remove(struct pci_dev *pci)
92 {
93 snd_card_free(pci_get_drvdata(pci));
94 pci_set_drvdata(pci, NULL);
95 }
96
snd_cs5530_mixer_read(unsigned long io,u8 reg)97 static u8 __devinit snd_cs5530_mixer_read(unsigned long io, u8 reg)
98 {
99 outb(reg, io + 4);
100 udelay(20);
101 reg = inb(io + 5);
102 udelay(20);
103 return reg;
104 }
105
snd_cs5530_create(struct snd_card * card,struct pci_dev * pci,struct snd_cs5530 ** rchip)106 static int __devinit snd_cs5530_create(struct snd_card *card,
107 struct pci_dev *pci,
108 struct snd_cs5530 **rchip)
109 {
110 struct snd_cs5530 *chip;
111 unsigned long sb_base;
112 u8 irq, dma8, dma16 = 0;
113 u16 map;
114 void __iomem *mem;
115 int err;
116
117 static struct snd_device_ops ops = {
118 .dev_free = snd_cs5530_dev_free,
119 };
120 *rchip = NULL;
121
122 err = pci_enable_device(pci);
123 if (err < 0)
124 return err;
125
126 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
127 if (chip == NULL) {
128 pci_disable_device(pci);
129 return -ENOMEM;
130 }
131
132 chip->card = card;
133 chip->pci = pci;
134
135 err = pci_request_regions(pci, "CS5530");
136 if (err < 0) {
137 kfree(chip);
138 pci_disable_device(pci);
139 return err;
140 }
141 chip->pci_base = pci_resource_start(pci, 0);
142
143 mem = pci_ioremap_bar(pci, 0);
144 if (mem == NULL) {
145 kfree(chip);
146 pci_disable_device(pci);
147 return -EBUSY;
148 }
149
150 map = readw(mem + 0x18);
151 iounmap(mem);
152
153 /* Map bits
154 0:1 * 0x20 + 0x200 = sb base
155 2 sb enable
156 3 adlib enable
157 5 MPU enable 0x330
158 6 MPU enable 0x300
159
160 The other bits may be used internally so must be masked */
161
162 sb_base = 0x220 + 0x20 * (map & 3);
163
164 if (map & (1<<2))
165 printk(KERN_INFO "CS5530: XpressAudio at 0x%lx\n", sb_base);
166 else {
167 printk(KERN_ERR "Could not find XpressAudio!\n");
168 snd_cs5530_free(chip);
169 return -ENODEV;
170 }
171
172 if (map & (1<<5))
173 printk(KERN_INFO "CS5530: MPU at 0x300\n");
174 else if (map & (1<<6))
175 printk(KERN_INFO "CS5530: MPU at 0x330\n");
176
177 irq = snd_cs5530_mixer_read(sb_base, 0x80) & 0x0F;
178 dma8 = snd_cs5530_mixer_read(sb_base, 0x81);
179
180 if (dma8 & 0x20)
181 dma16 = 5;
182 else if (dma8 & 0x40)
183 dma16 = 6;
184 else if (dma8 & 0x80)
185 dma16 = 7;
186 else {
187 printk(KERN_ERR "CS5530: No 16bit DMA enabled\n");
188 snd_cs5530_free(chip);
189 return -ENODEV;
190 }
191
192 if (dma8 & 0x01)
193 dma8 = 0;
194 else if (dma8 & 02)
195 dma8 = 1;
196 else if (dma8 & 0x08)
197 dma8 = 3;
198 else {
199 printk(KERN_ERR "CS5530: No 8bit DMA enabled\n");
200 snd_cs5530_free(chip);
201 return -ENODEV;
202 }
203
204 if (irq & 1)
205 irq = 9;
206 else if (irq & 2)
207 irq = 5;
208 else if (irq & 4)
209 irq = 7;
210 else if (irq & 8)
211 irq = 10;
212 else {
213 printk(KERN_ERR "CS5530: SoundBlaster IRQ not set\n");
214 snd_cs5530_free(chip);
215 return -ENODEV;
216 }
217
218 printk(KERN_INFO "CS5530: IRQ: %d DMA8: %d DMA16: %d\n", irq, dma8,
219 dma16);
220
221 err = snd_sbdsp_create(card, sb_base, irq, snd_sb16dsp_interrupt, dma8,
222 dma16, SB_HW_CS5530, &chip->sb);
223 if (err < 0) {
224 printk(KERN_ERR "CS5530: Could not create SoundBlaster\n");
225 snd_cs5530_free(chip);
226 return err;
227 }
228
229 err = snd_sb16dsp_pcm(chip->sb, 0, &chip->sb->pcm);
230 if (err < 0) {
231 printk(KERN_ERR "CS5530: Could not create PCM\n");
232 snd_cs5530_free(chip);
233 return err;
234 }
235
236 err = snd_sbmixer_new(chip->sb);
237 if (err < 0) {
238 printk(KERN_ERR "CS5530: Could not create Mixer\n");
239 snd_cs5530_free(chip);
240 return err;
241 }
242
243 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
244 if (err < 0) {
245 snd_cs5530_free(chip);
246 return err;
247 }
248
249 snd_card_set_dev(card, &pci->dev);
250 *rchip = chip;
251 return 0;
252 }
253
snd_cs5530_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)254 static int __devinit snd_cs5530_probe(struct pci_dev *pci,
255 const struct pci_device_id *pci_id)
256 {
257 static int dev;
258 struct snd_card *card;
259 struct snd_cs5530 *chip = NULL;
260 int err;
261
262 if (dev >= SNDRV_CARDS)
263 return -ENODEV;
264 if (!enable[dev]) {
265 dev++;
266 return -ENOENT;
267 }
268
269 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
270
271 if (err < 0)
272 return err;
273
274 err = snd_cs5530_create(card, pci, &chip);
275 if (err < 0) {
276 snd_card_free(card);
277 return err;
278 }
279
280 strcpy(card->driver, "CS5530");
281 strcpy(card->shortname, "CS5530 Audio");
282 sprintf(card->longname, "%s at 0x%lx", card->shortname, chip->pci_base);
283
284 err = snd_card_register(card);
285 if (err < 0) {
286 snd_card_free(card);
287 return err;
288 }
289 pci_set_drvdata(pci, card);
290 dev++;
291 return 0;
292 }
293
294 static struct pci_driver driver = {
295 .name = KBUILD_MODNAME,
296 .id_table = snd_cs5530_ids,
297 .probe = snd_cs5530_probe,
298 .remove = __devexit_p(snd_cs5530_remove),
299 };
300
alsa_card_cs5530_init(void)301 static int __init alsa_card_cs5530_init(void)
302 {
303 return pci_register_driver(&driver);
304 }
305
alsa_card_cs5530_exit(void)306 static void __exit alsa_card_cs5530_exit(void)
307 {
308 pci_unregister_driver(&driver);
309 }
310
311 module_init(alsa_card_cs5530_init)
312 module_exit(alsa_card_cs5530_exit)
313
314