blob: c7f86f09c28da1979a5c0e6de7cfde38fd1309d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) by Hannu Savolainen 1993-1997
3 *
4 * cs4232.c
5 *
6 * The low level driver for Crystal CS4232 based cards. The CS4232 is
7 * a PnP compatible chip which contains a CS4231A codec, SB emulation,
8 * a MPU401 compatible MIDI port, joystick and synthesizer and IDE CD-ROM
9 * interfaces. This is just a temporary driver until full PnP support
10 * gets implemented. Just the WSS codec, FM synth and the MIDI ports are
11 * supported. Other interfaces are left uninitialized.
12 *
13 * ifdef ...WAVEFRONT...
14 *
15 * Support is provided for initializing the WaveFront synth
16 * interface as well, which is logical device #4. Note that if
17 * you have a Tropez+ card, you probably don't need to setup
18 * the CS4232-supported MIDI interface, since it corresponds to
19 * the internal 26-pin header that's hard to access. Using this
20 * requires an additional IRQ, a resource none too plentiful in
21 * this environment. Just don't set module parameters mpuio and
22 * mpuirq, and the MIDI port will be left uninitialized. You can
23 * still use the ICS2115 hosted MIDI interface which corresponds
24 * to the 9-pin D connector on the back of the card.
25 *
26 * endif ...WAVEFRONT...
27 *
28 * Supported chips are:
29 * CS4232
30 * CS4236
31 * CS4236B
32 *
33 * Note: You will need a PnP config setup to initialise some CS4232 boards
34 * anyway.
35 *
36 * Changes
37 * John Rood Added Bose Sound System Support.
38 * Toshio Spoor
39 * Alan Cox Modularisation, Basic cleanups.
40 * Paul Barton-Davis Separated MPU configuration, added
41 * Tropez+ (WaveFront) support
42 * Christoph Hellwig Adapted to module_init/module_exit,
43 * simple cleanups
44 * Arnaldo C. de Melo got rid of attach_uart401
45 * Bartlomiej Zolnierkiewicz
46 * Added some __init/__initdata/__exit
47 * Marcus Meissner Added ISA PnP support.
48 */
49
50#include <linux/config.h>
51#include <linux/pnp.h>
52#include <linux/module.h>
53#include <linux/init.h>
54
55#include "sound_config.h"
56
57#include "ad1848.h"
58#include "mpu401.h"
59
60#define KEY_PORT 0x279 /* Same as LPT1 status port */
61#define CSN_NUM 0x99 /* Just a random number */
62#define INDEX_ADDRESS 0x00 /* (R0) Index Address Register */
63#define INDEX_DATA 0x01 /* (R1) Indexed Data Register */
64#define PIN_CONTROL 0x0a /* (I10) Pin Control */
65#define ENABLE_PINS 0xc0 /* XCTRL0/XCTRL1 enable */
66
67static void CS_OUT(unsigned char a)
68{
69 outb(a, KEY_PORT);
70}
71
72#define CS_OUT2(a, b) {CS_OUT(a);CS_OUT(b);}
73#define CS_OUT3(a, b, c) {CS_OUT(a);CS_OUT(b);CS_OUT(c);}
74
75static int __initdata bss = 0;
76static int mpu_base, mpu_irq;
77static int synth_base, synth_irq;
78static int mpu_detected;
79
80static int probe_cs4232_mpu(struct address_info *hw_config)
81{
82 /*
83 * Just write down the config values.
84 */
85
86 mpu_base = hw_config->io_base;
87 mpu_irq = hw_config->irq;
88
89 return 1;
90}
91
92static unsigned char crystal_key[] = /* A 32 byte magic key sequence */
93{
94 0x96, 0x35, 0x9a, 0xcd, 0xe6, 0xf3, 0x79, 0xbc,
95 0x5e, 0xaf, 0x57, 0x2b, 0x15, 0x8a, 0xc5, 0xe2,
96 0xf1, 0xf8, 0x7c, 0x3e, 0x9f, 0x4f, 0x27, 0x13,
97 0x09, 0x84, 0x42, 0xa1, 0xd0, 0x68, 0x34, 0x1a
98};
99
100static void sleep(unsigned howlong)
101{
102 current->state = TASK_INTERRUPTIBLE;
103 schedule_timeout(howlong);
104}
105
106static void enable_xctrl(int baseio)
107{
108 unsigned char regd;
109
110 /*
111 * Some IBM Aptiva's have the Bose Sound System. By default
112 * the Bose Amplifier is disabled. The amplifier will be
113 * activated, by setting the XCTRL0 and XCTRL1 bits.
114 * Volume of the monitor bose speakers/woofer, can then
115 * be set by changing the PCM volume.
116 *
117 */
118
119 printk("cs4232: enabling Bose Sound System Amplifier.\n");
120
121 /* Switch to Pin Control Address */
122 regd = inb(baseio + INDEX_ADDRESS) & 0xe0;
123 outb(((unsigned char) (PIN_CONTROL | regd)), baseio + INDEX_ADDRESS );
124
125 /* Activate the XCTRL0 and XCTRL1 Pins */
126 regd = inb(baseio + INDEX_DATA);
127 outb(((unsigned char) (ENABLE_PINS | regd)), baseio + INDEX_DATA );
128}
129
130static int __init probe_cs4232(struct address_info *hw_config, int isapnp_configured)
131{
132 int i, n;
133 int base = hw_config->io_base, irq = hw_config->irq;
134 int dma1 = hw_config->dma, dma2 = hw_config->dma2;
135 struct resource *ports;
136
137 if (base == -1 || irq == -1 || dma1 == -1) {
138 printk(KERN_ERR "cs4232: dma, irq and io must be set.\n");
139 return 0;
140 }
141
142 /*
143 * Verify that the I/O port range is free.
144 */
145
146 ports = request_region(base, 4, "ad1848");
147 if (!ports) {
148 printk(KERN_ERR "cs4232.c: I/O port 0x%03x not free\n", base);
149 return 0;
150 }
151 if (ad1848_detect(ports, NULL, hw_config->osp)) {
152 goto got_it; /* The card is already active */
153 }
154 if (isapnp_configured) {
155 printk(KERN_ERR "cs4232.c: ISA PnP configured, but not detected?\n");
156 goto fail;
157 }
158
159 /*
160 * This version of the driver doesn't use the PnP method when configuring
161 * the card but a simplified method defined by Crystal. This means that
162 * just one CS4232 compatible device can exist on the system. Also this
163 * method conflicts with possible PnP support in the OS. For this reason
164 * driver is just a temporary kludge.
165 *
166 * Also the Cirrus/Crystal method doesn't always work. Try ISA PnP first ;)
167 */
168
169 /*
170 * Repeat initialization few times since it doesn't always succeed in
171 * first time.
172 */
173
174 for (n = 0; n < 4; n++)
175 {
176 /*
177 * Wake up the card by sending a 32 byte Crystal key to the key port.
178 */
179
180 for (i = 0; i < 32; i++)
181 CS_OUT(crystal_key[i]);
182
183 sleep(HZ / 10);
184
185 /*
186 * Now set the CSN (Card Select Number).
187 */
188
189 CS_OUT2(0x06, CSN_NUM);
190
191 /*
192 * Then set some config bytes. First logical device 0
193 */
194
195 CS_OUT2(0x15, 0x00); /* Select logical device 0 (WSS/SB/FM) */
196 CS_OUT3(0x47, (base >> 8) & 0xff, base & 0xff); /* WSS base */
197
Jeff Garzikd61780c02005-10-30 15:01:51 -0800198 if (!request_region(0x388, 4, "FM")) /* Not free */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 CS_OUT3(0x48, 0x00, 0x00) /* FM base off */
Jeff Garzikd61780c02005-10-30 15:01:51 -0800200 else {
201 release_region(0x388, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 CS_OUT3(0x48, 0x03, 0x88); /* FM base 0x388 */
Jeff Garzikd61780c02005-10-30 15:01:51 -0800203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 CS_OUT3(0x42, 0x00, 0x00); /* SB base off */
206 CS_OUT2(0x22, irq); /* SB+WSS IRQ */
207 CS_OUT2(0x2a, dma1); /* SB+WSS DMA */
208
209 if (dma2 != -1)
210 CS_OUT2(0x25, dma2) /* WSS DMA2 */
211 else
212 CS_OUT2(0x25, 4); /* No WSS DMA2 */
213
214 CS_OUT2(0x33, 0x01); /* Activate logical dev 0 */
215
216 sleep(HZ / 10);
217
218 /*
219 * Initialize logical device 3 (MPU)
220 */
221
222 if (mpu_base != 0 && mpu_irq != 0)
223 {
224 CS_OUT2(0x15, 0x03); /* Select logical device 3 (MPU) */
225 CS_OUT3(0x47, (mpu_base >> 8) & 0xff, mpu_base & 0xff); /* MPU base */
226 CS_OUT2(0x22, mpu_irq); /* MPU IRQ */
227 CS_OUT2(0x33, 0x01); /* Activate logical dev 3 */
228 }
229
230 if(synth_base != 0)
231 {
232 CS_OUT2 (0x15, 0x04); /* logical device 4 (WaveFront) */
233 CS_OUT3 (0x47, (synth_base >> 8) & 0xff,
234 synth_base & 0xff); /* base */
235 CS_OUT2 (0x22, synth_irq); /* IRQ */
236 CS_OUT2 (0x33, 0x01); /* Activate logical dev 4 */
237 }
238
239 /*
240 * Finally activate the chip
241 */
242
243 CS_OUT(0x79);
244
245 sleep(HZ / 5);
246
247 /*
248 * Then try to detect the codec part of the chip
249 */
250
251 if (ad1848_detect(ports, NULL, hw_config->osp))
252 goto got_it;
253
254 sleep(HZ);
255 }
256fail:
257 release_region(base, 4);
258 return 0;
259
260got_it:
261 if (dma2 == -1)
262 dma2 = dma1;
263
264 hw_config->slots[0] = ad1848_init("Crystal audio controller", ports,
265 irq,
266 dma1, /* Playback DMA */
267 dma2, /* Capture DMA */
268 0,
269 hw_config->osp,
270 THIS_MODULE);
271
272 if (hw_config->slots[0] != -1 &&
273 audio_devs[hw_config->slots[0]]->mixer_dev!=-1)
274 {
275 /* Assume the mixer map is as suggested in the CS4232 databook */
276 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_LINE);
277 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
278 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_SYNTH); /* FM synth */
279 }
280 if (mpu_base != 0 && mpu_irq != 0)
281 {
282 static struct address_info hw_config2 = {
283 0
284 }; /* Ensure it's initialized */
285
286 hw_config2.io_base = mpu_base;
287 hw_config2.irq = mpu_irq;
288 hw_config2.dma = -1;
289 hw_config2.dma2 = -1;
290 hw_config2.always_detect = 0;
291 hw_config2.name = NULL;
292 hw_config2.driver_use_1 = 0;
293 hw_config2.driver_use_2 = 0;
294 hw_config2.card_subtype = 0;
295
296 if (probe_uart401(&hw_config2, THIS_MODULE))
297 {
298 mpu_detected = 1;
299 }
300 else
301 {
302 mpu_base = mpu_irq = 0;
303 }
304 hw_config->slots[1] = hw_config2.slots[1];
305 }
306
307 if (bss)
308 enable_xctrl(base);
309
310 return 1;
311}
312
313static void __devexit unload_cs4232(struct address_info *hw_config)
314{
315 int base = hw_config->io_base, irq = hw_config->irq;
316 int dma1 = hw_config->dma, dma2 = hw_config->dma2;
317
318 if (dma2 == -1)
319 dma2 = dma1;
320
321 ad1848_unload(base,
322 irq,
323 dma1, /* Playback DMA */
324 dma2, /* Capture DMA */
325 0);
326
327 sound_unload_audiodev(hw_config->slots[0]);
328 if (mpu_base != 0 && mpu_irq != 0 && mpu_detected)
329 {
330 static struct address_info hw_config2 =
331 {
332 0
333 }; /* Ensure it's initialized */
334
335 hw_config2.io_base = mpu_base;
336 hw_config2.irq = mpu_irq;
337 hw_config2.dma = -1;
338 hw_config2.dma2 = -1;
339 hw_config2.always_detect = 0;
340 hw_config2.name = NULL;
341 hw_config2.driver_use_1 = 0;
342 hw_config2.driver_use_2 = 0;
343 hw_config2.card_subtype = 0;
344 hw_config2.slots[1] = hw_config->slots[1];
345
346 unload_uart401(&hw_config2);
347 }
348}
349
350static struct address_info cfg;
351static struct address_info cfg_mpu;
352
353static int __initdata io = -1;
354static int __initdata irq = -1;
355static int __initdata dma = -1;
356static int __initdata dma2 = -1;
357static int __initdata mpuio = -1;
358static int __initdata mpuirq = -1;
359static int __initdata synthio = -1;
360static int __initdata synthirq = -1;
361static int __initdata isapnp = 1;
362
Bjorn Helgaas070c6992006-03-27 01:17:07 -0800363static unsigned int cs4232_devices;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365MODULE_DESCRIPTION("CS4232 based soundcard driver");
366MODULE_AUTHOR("Hannu Savolainen, Paul Barton-Davis");
367MODULE_LICENSE("GPL");
368
369module_param(io, int, 0);
370MODULE_PARM_DESC(io,"base I/O port for AD1848");
371module_param(irq, int, 0);
372MODULE_PARM_DESC(irq,"IRQ for AD1848 chip");
373module_param(dma, int, 0);
374MODULE_PARM_DESC(dma,"8 bit DMA for AD1848 chip");
375module_param(dma2, int, 0);
376MODULE_PARM_DESC(dma2,"16 bit DMA for AD1848 chip");
377module_param(mpuio, int, 0);
378MODULE_PARM_DESC(mpuio,"MPU 401 base address");
379module_param(mpuirq, int, 0);
380MODULE_PARM_DESC(mpuirq,"MPU 401 IRQ");
381module_param(synthio, int, 0);
382MODULE_PARM_DESC(synthio,"Maui WaveTable base I/O port");
383module_param(synthirq, int, 0);
384MODULE_PARM_DESC(synthirq,"Maui WaveTable IRQ");
385module_param(isapnp, bool, 0);
386MODULE_PARM_DESC(isapnp,"Enable ISAPnP probing (default 1)");
387module_param(bss, bool, 0);
388MODULE_PARM_DESC(bss,"Enable Bose Sound System Support (default 0)");
389
390/*
391 * Install a CS4232 based card. Need to have ad1848 and mpu401
392 * loaded ready.
393 */
394
395/* All cs4232 based cards have the main ad1848 card either as CSC0000 or
396 * CSC0100. */
397static const struct pnp_device_id cs4232_pnp_table[] = {
398 { .id = "CSC0100", .driver_data = 0 },
399 { .id = "CSC0000", .driver_data = 0 },
400 /* Guillemot Turtlebeach something appears to be cs4232 compatible
401 * (untested) */
402 { .id = "GIM0100", .driver_data = 0 },
403 { .id = ""}
404};
405
406MODULE_DEVICE_TABLE(pnp, cs4232_pnp_table);
407
408static int cs4232_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
409{
410 struct address_info *isapnpcfg;
411
412 isapnpcfg=(struct address_info*)kmalloc(sizeof(*isapnpcfg),GFP_KERNEL);
413 if (!isapnpcfg)
414 return -ENOMEM;
415
416 isapnpcfg->irq = pnp_irq(dev, 0);
417 isapnpcfg->dma = pnp_dma(dev, 0);
418 isapnpcfg->dma2 = pnp_dma(dev, 1);
419 isapnpcfg->io_base = pnp_port_start(dev, 0);
420 if (probe_cs4232(isapnpcfg,TRUE) == 0) {
421 printk(KERN_ERR "cs4232: ISA PnP card found, but not detected?\n");
422 kfree(isapnpcfg);
423 return -ENODEV;
424 }
425 pnp_set_drvdata(dev,isapnpcfg);
Bjorn Helgaas070c6992006-03-27 01:17:07 -0800426 cs4232_devices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
429
430static void __devexit cs4232_pnp_remove(struct pnp_dev *dev)
431{
432 struct address_info *cfg = pnp_get_drvdata(dev);
433 if (cfg) {
434 unload_cs4232(cfg);
435 kfree(cfg);
436 }
437}
438
439static struct pnp_driver cs4232_driver = {
440 .name = "cs4232",
441 .id_table = cs4232_pnp_table,
442 .probe = cs4232_pnp_probe,
443 .remove = __devexit_p(cs4232_pnp_remove),
444};
445
446static int __init init_cs4232(void)
447{
448#ifdef CONFIG_SOUND_WAVEFRONT_MODULE
449 if(synthio == -1)
450 printk(KERN_INFO "cs4232: set synthio and synthirq to use the wavefront facilities.\n");
451 else {
452 synth_base = synthio;
453 synth_irq = synthirq;
454 }
455#else
456 if(synthio != -1)
457 printk(KERN_WARNING "cs4232: wavefront support not enabled in this driver.\n");
458#endif
459 cfg.irq = -1;
460
Bjorn Helgaas070c6992006-03-27 01:17:07 -0800461 if (isapnp) {
462 pnp_register_driver(&cs4232_driver);
463 if (cs4232_devices)
464 return 0;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 if(io==-1||irq==-1||dma==-1)
468 {
469 printk(KERN_ERR "cs4232: Must set io, irq and dma.\n");
470 return -ENODEV;
471 }
472
473 cfg.io_base = io;
474 cfg.irq = irq;
475 cfg.dma = dma;
476 cfg.dma2 = dma2;
477
478 cfg_mpu.io_base = -1;
479 cfg_mpu.irq = -1;
480
481 if (mpuio != -1 && mpuirq != -1) {
482 cfg_mpu.io_base = mpuio;
483 cfg_mpu.irq = mpuirq;
484 probe_cs4232_mpu(&cfg_mpu); /* Bug always returns 0 not OK -- AC */
485 }
486
487 if (probe_cs4232(&cfg,FALSE) == 0)
488 return -ENODEV;
489
490 return 0;
491}
492
493static void __exit cleanup_cs4232(void)
494{
495 pnp_unregister_driver(&cs4232_driver);
496 if (cfg.irq != -1)
497 unload_cs4232(&cfg); /* Unloads global MPU as well, if needed */
498}
499
500module_init(init_cs4232);
501module_exit(cleanup_cs4232);
502
503#ifndef MODULE
504static int __init setup_cs4232(char *str)
505{
506 /* io, irq, dma, dma2 mpuio, mpuirq*/
507 int ints[7];
508
509 /* If we have isapnp cards, no need for options */
Bjorn Helgaas070c6992006-03-27 01:17:07 -0800510 pnp_register_driver(&cs4232_driver);
511 if (cs4232_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 1;
513
514 str = get_options(str, ARRAY_SIZE(ints), ints);
515
516 io = ints[1];
517 irq = ints[2];
518 dma = ints[3];
519 dma2 = ints[4];
520 mpuio = ints[5];
521 mpuirq = ints[6];
522
523 return 1;
524}
525
526__setup("cs4232=", setup_cs4232);
527#endif