xref: /openbmc/linux/sound/pci/cs5530.c (revision 05bcf503)
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 
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 
91 static void snd_cs5530_remove(struct pci_dev *pci)
92 {
93 	snd_card_free(pci_get_drvdata(pci));
94 	pci_set_drvdata(pci, NULL);
95 }
96 
97 static u8 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 
106 static int 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 		snd_cs5530_free(chip);
146 		return -EBUSY;
147 	}
148 
149 	map = readw(mem + 0x18);
150 	iounmap(mem);
151 
152 	/* Map bits
153 		0:1	* 0x20 + 0x200 = sb base
154 		2	sb enable
155 		3	adlib enable
156 		5	MPU enable 0x330
157 		6	MPU enable 0x300
158 
159 	   The other bits may be used internally so must be masked */
160 
161 	sb_base = 0x220 + 0x20 * (map & 3);
162 
163 	if (map & (1<<2))
164 		printk(KERN_INFO "CS5530: XpressAudio at 0x%lx\n", sb_base);
165 	else {
166 		printk(KERN_ERR "Could not find XpressAudio!\n");
167 		snd_cs5530_free(chip);
168 		return -ENODEV;
169 	}
170 
171 	if (map & (1<<5))
172 		printk(KERN_INFO "CS5530: MPU at 0x300\n");
173 	else if (map & (1<<6))
174 		printk(KERN_INFO "CS5530: MPU at 0x330\n");
175 
176 	irq = snd_cs5530_mixer_read(sb_base, 0x80) & 0x0F;
177 	dma8 = snd_cs5530_mixer_read(sb_base, 0x81);
178 
179 	if (dma8 & 0x20)
180 		dma16 = 5;
181 	else if (dma8 & 0x40)
182 		dma16 = 6;
183 	else if (dma8 & 0x80)
184 		dma16 = 7;
185 	else {
186 		printk(KERN_ERR "CS5530: No 16bit DMA enabled\n");
187 		snd_cs5530_free(chip);
188 		return -ENODEV;
189 	}
190 
191 	if (dma8 & 0x01)
192 		dma8 = 0;
193 	else if (dma8 & 02)
194 		dma8 = 1;
195 	else if (dma8 & 0x08)
196 		dma8 = 3;
197 	else {
198 		printk(KERN_ERR "CS5530: No 8bit DMA enabled\n");
199 		snd_cs5530_free(chip);
200 		return -ENODEV;
201 	}
202 
203 	if (irq & 1)
204 		irq = 9;
205 	else if (irq & 2)
206 		irq = 5;
207 	else if (irq & 4)
208 		irq = 7;
209 	else if (irq & 8)
210 		irq = 10;
211 	else {
212 		printk(KERN_ERR "CS5530: SoundBlaster IRQ not set\n");
213 		snd_cs5530_free(chip);
214 		return -ENODEV;
215 	}
216 
217 	printk(KERN_INFO "CS5530: IRQ: %d DMA8: %d DMA16: %d\n", irq, dma8,
218 									dma16);
219 
220 	err = snd_sbdsp_create(card, sb_base, irq, snd_sb16dsp_interrupt, dma8,
221 						dma16, SB_HW_CS5530, &chip->sb);
222 	if (err < 0) {
223 		printk(KERN_ERR "CS5530: Could not create SoundBlaster\n");
224 		snd_cs5530_free(chip);
225 		return err;
226 	}
227 
228 	err = snd_sb16dsp_pcm(chip->sb, 0, &chip->sb->pcm);
229 	if (err < 0) {
230 		printk(KERN_ERR "CS5530: Could not create PCM\n");
231 		snd_cs5530_free(chip);
232 		return err;
233 	}
234 
235 	err = snd_sbmixer_new(chip->sb);
236 	if (err < 0) {
237 		printk(KERN_ERR "CS5530: Could not create Mixer\n");
238 		snd_cs5530_free(chip);
239 		return err;
240 	}
241 
242 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
243 	if (err < 0) {
244 		snd_cs5530_free(chip);
245 		return err;
246 	}
247 
248 	snd_card_set_dev(card, &pci->dev);
249 	*rchip = chip;
250 	return 0;
251 }
252 
253 static int snd_cs5530_probe(struct pci_dev *pci,
254 			    const struct pci_device_id *pci_id)
255 {
256 	static int dev;
257 	struct snd_card *card;
258 	struct snd_cs5530 *chip = NULL;
259 	int err;
260 
261 	if (dev >= SNDRV_CARDS)
262 		return -ENODEV;
263 	if (!enable[dev]) {
264 		dev++;
265 		return -ENOENT;
266 	}
267 
268 	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
269 
270 	if (err < 0)
271 		return err;
272 
273 	err = snd_cs5530_create(card, pci, &chip);
274 	if (err < 0) {
275 		snd_card_free(card);
276 		return err;
277 	}
278 
279 	strcpy(card->driver, "CS5530");
280 	strcpy(card->shortname, "CS5530 Audio");
281 	sprintf(card->longname, "%s at 0x%lx", card->shortname, chip->pci_base);
282 
283 	err = snd_card_register(card);
284 	if (err < 0) {
285 		snd_card_free(card);
286 		return err;
287 	}
288 	pci_set_drvdata(pci, card);
289 	dev++;
290 	return 0;
291 }
292 
293 static struct pci_driver cs5530_driver = {
294 	.name = KBUILD_MODNAME,
295 	.id_table = snd_cs5530_ids,
296 	.probe = snd_cs5530_probe,
297 	.remove = snd_cs5530_remove,
298 };
299 
300 module_pci_driver(cs5530_driver);
301