cs4232.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  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. #include <linux/pnp.h>
  50. #include <linux/module.h>
  51. #include <linux/init.h>
  52. #include "sound_config.h"
  53. #include "ad1848.h"
  54. #include "mpu401.h"
  55. #define KEY_PORT 0x279 /* Same as LPT1 status port */
  56. #define CSN_NUM 0x99 /* Just a random number */
  57. #define INDEX_ADDRESS 0x00 /* (R0) Index Address Register */
  58. #define INDEX_DATA 0x01 /* (R1) Indexed Data Register */
  59. #define PIN_CONTROL 0x0a /* (I10) Pin Control */
  60. #define ENABLE_PINS 0xc0 /* XCTRL0/XCTRL1 enable */
  61. static void CS_OUT(unsigned char a)
  62. {
  63. outb(a, KEY_PORT);
  64. }
  65. #define CS_OUT2(a, b) {CS_OUT(a);CS_OUT(b);}
  66. #define CS_OUT3(a, b, c) {CS_OUT(a);CS_OUT(b);CS_OUT(c);}
  67. static int __initdata bss = 0;
  68. static int mpu_base, mpu_irq;
  69. static int synth_base, synth_irq;
  70. static int mpu_detected;
  71. static int probe_cs4232_mpu(struct address_info *hw_config)
  72. {
  73. /*
  74. * Just write down the config values.
  75. */
  76. mpu_base = hw_config->io_base;
  77. mpu_irq = hw_config->irq;
  78. return 1;
  79. }
  80. static unsigned char crystal_key[] = /* A 32 byte magic key sequence */
  81. {
  82. 0x96, 0x35, 0x9a, 0xcd, 0xe6, 0xf3, 0x79, 0xbc,
  83. 0x5e, 0xaf, 0x57, 0x2b, 0x15, 0x8a, 0xc5, 0xe2,
  84. 0xf1, 0xf8, 0x7c, 0x3e, 0x9f, 0x4f, 0x27, 0x13,
  85. 0x09, 0x84, 0x42, 0xa1, 0xd0, 0x68, 0x34, 0x1a
  86. };
  87. static void sleep(unsigned howlong)
  88. {
  89. current->state = TASK_INTERRUPTIBLE;
  90. schedule_timeout(howlong);
  91. }
  92. static void enable_xctrl(int baseio)
  93. {
  94. unsigned char regd;
  95. /*
  96. * Some IBM Aptiva's have the Bose Sound System. By default
  97. * the Bose Amplifier is disabled. The amplifier will be
  98. * activated, by setting the XCTRL0 and XCTRL1 bits.
  99. * Volume of the monitor bose speakers/woofer, can then
  100. * be set by changing the PCM volume.
  101. *
  102. */
  103. printk("cs4232: enabling Bose Sound System Amplifier.\n");
  104. /* Switch to Pin Control Address */
  105. regd = inb(baseio + INDEX_ADDRESS) & 0xe0;
  106. outb(((unsigned char) (PIN_CONTROL | regd)), baseio + INDEX_ADDRESS );
  107. /* Activate the XCTRL0 and XCTRL1 Pins */
  108. regd = inb(baseio + INDEX_DATA);
  109. outb(((unsigned char) (ENABLE_PINS | regd)), baseio + INDEX_DATA );
  110. }
  111. static int __init probe_cs4232(struct address_info *hw_config, int isapnp_configured)
  112. {
  113. int i, n;
  114. int base = hw_config->io_base, irq = hw_config->irq;
  115. int dma1 = hw_config->dma, dma2 = hw_config->dma2;
  116. struct resource *ports;
  117. if (base == -1 || irq == -1 || dma1 == -1) {
  118. printk(KERN_ERR "cs4232: dma, irq and io must be set.\n");
  119. return 0;
  120. }
  121. /*
  122. * Verify that the I/O port range is free.
  123. */
  124. ports = request_region(base, 4, "ad1848");
  125. if (!ports) {
  126. printk(KERN_ERR "cs4232.c: I/O port 0x%03x not free\n", base);
  127. return 0;
  128. }
  129. if (ad1848_detect(ports, NULL, hw_config->osp)) {
  130. goto got_it; /* The card is already active */
  131. }
  132. if (isapnp_configured) {
  133. printk(KERN_ERR "cs4232.c: ISA PnP configured, but not detected?\n");
  134. goto fail;
  135. }
  136. /*
  137. * This version of the driver doesn't use the PnP method when configuring
  138. * the card but a simplified method defined by Crystal. This means that
  139. * just one CS4232 compatible device can exist on the system. Also this
  140. * method conflicts with possible PnP support in the OS. For this reason
  141. * driver is just a temporary kludge.
  142. *
  143. * Also the Cirrus/Crystal method doesn't always work. Try ISA PnP first ;)
  144. */
  145. /*
  146. * Repeat initialization few times since it doesn't always succeed in
  147. * first time.
  148. */
  149. for (n = 0; n < 4; n++)
  150. {
  151. /*
  152. * Wake up the card by sending a 32 byte Crystal key to the key port.
  153. */
  154. for (i = 0; i < 32; i++)
  155. CS_OUT(crystal_key[i]);
  156. sleep(HZ / 10);
  157. /*
  158. * Now set the CSN (Card Select Number).
  159. */
  160. CS_OUT2(0x06, CSN_NUM);
  161. /*
  162. * Then set some config bytes. First logical device 0
  163. */
  164. CS_OUT2(0x15, 0x00); /* Select logical device 0 (WSS/SB/FM) */
  165. CS_OUT3(0x47, (base >> 8) & 0xff, base & 0xff); /* WSS base */
  166. if (!request_region(0x388, 4, "FM")) /* Not free */
  167. CS_OUT3(0x48, 0x00, 0x00) /* FM base off */
  168. else {
  169. release_region(0x388, 4);
  170. CS_OUT3(0x48, 0x03, 0x88); /* FM base 0x388 */
  171. }
  172. CS_OUT3(0x42, 0x00, 0x00); /* SB base off */
  173. CS_OUT2(0x22, irq); /* SB+WSS IRQ */
  174. CS_OUT2(0x2a, dma1); /* SB+WSS DMA */
  175. if (dma2 != -1)
  176. CS_OUT2(0x25, dma2) /* WSS DMA2 */
  177. else
  178. CS_OUT2(0x25, 4); /* No WSS DMA2 */
  179. CS_OUT2(0x33, 0x01); /* Activate logical dev 0 */
  180. sleep(HZ / 10);
  181. /*
  182. * Initialize logical device 3 (MPU)
  183. */
  184. if (mpu_base != 0 && mpu_irq != 0)
  185. {
  186. CS_OUT2(0x15, 0x03); /* Select logical device 3 (MPU) */
  187. CS_OUT3(0x47, (mpu_base >> 8) & 0xff, mpu_base & 0xff); /* MPU base */
  188. CS_OUT2(0x22, mpu_irq); /* MPU IRQ */
  189. CS_OUT2(0x33, 0x01); /* Activate logical dev 3 */
  190. }
  191. if(synth_base != 0)
  192. {
  193. CS_OUT2 (0x15, 0x04); /* logical device 4 (WaveFront) */
  194. CS_OUT3 (0x47, (synth_base >> 8) & 0xff,
  195. synth_base & 0xff); /* base */
  196. CS_OUT2 (0x22, synth_irq); /* IRQ */
  197. CS_OUT2 (0x33, 0x01); /* Activate logical dev 4 */
  198. }
  199. /*
  200. * Finally activate the chip
  201. */
  202. CS_OUT(0x79);
  203. sleep(HZ / 5);
  204. /*
  205. * Then try to detect the codec part of the chip
  206. */
  207. if (ad1848_detect(ports, NULL, hw_config->osp))
  208. goto got_it;
  209. sleep(HZ);
  210. }
  211. fail:
  212. release_region(base, 4);
  213. return 0;
  214. got_it:
  215. if (dma2 == -1)
  216. dma2 = dma1;
  217. hw_config->slots[0] = ad1848_init("Crystal audio controller", ports,
  218. irq,
  219. dma1, /* Playback DMA */
  220. dma2, /* Capture DMA */
  221. 0,
  222. hw_config->osp,
  223. THIS_MODULE);
  224. if (hw_config->slots[0] != -1 &&
  225. audio_devs[hw_config->slots[0]]->mixer_dev!=-1)
  226. {
  227. /* Assume the mixer map is as suggested in the CS4232 databook */
  228. AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_LINE);
  229. AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
  230. AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_SYNTH); /* FM synth */
  231. }
  232. if (mpu_base != 0 && mpu_irq != 0)
  233. {
  234. static struct address_info hw_config2 = {
  235. 0
  236. }; /* Ensure it's initialized */
  237. hw_config2.io_base = mpu_base;
  238. hw_config2.irq = mpu_irq;
  239. hw_config2.dma = -1;
  240. hw_config2.dma2 = -1;
  241. hw_config2.always_detect = 0;
  242. hw_config2.name = NULL;
  243. hw_config2.driver_use_1 = 0;
  244. hw_config2.driver_use_2 = 0;
  245. hw_config2.card_subtype = 0;
  246. if (probe_uart401(&hw_config2, THIS_MODULE))
  247. {
  248. mpu_detected = 1;
  249. }
  250. else
  251. {
  252. mpu_base = mpu_irq = 0;
  253. }
  254. hw_config->slots[1] = hw_config2.slots[1];
  255. }
  256. if (bss)
  257. enable_xctrl(base);
  258. return 1;
  259. }
  260. static void __devexit unload_cs4232(struct address_info *hw_config)
  261. {
  262. int base = hw_config->io_base, irq = hw_config->irq;
  263. int dma1 = hw_config->dma, dma2 = hw_config->dma2;
  264. if (dma2 == -1)
  265. dma2 = dma1;
  266. ad1848_unload(base,
  267. irq,
  268. dma1, /* Playback DMA */
  269. dma2, /* Capture DMA */
  270. 0);
  271. sound_unload_audiodev(hw_config->slots[0]);
  272. if (mpu_base != 0 && mpu_irq != 0 && mpu_detected)
  273. {
  274. static struct address_info hw_config2 =
  275. {
  276. 0
  277. }; /* Ensure it's initialized */
  278. hw_config2.io_base = mpu_base;
  279. hw_config2.irq = mpu_irq;
  280. hw_config2.dma = -1;
  281. hw_config2.dma2 = -1;
  282. hw_config2.always_detect = 0;
  283. hw_config2.name = NULL;
  284. hw_config2.driver_use_1 = 0;
  285. hw_config2.driver_use_2 = 0;
  286. hw_config2.card_subtype = 0;
  287. hw_config2.slots[1] = hw_config->slots[1];
  288. unload_uart401(&hw_config2);
  289. }
  290. }
  291. static struct address_info cfg;
  292. static struct address_info cfg_mpu;
  293. static int __initdata io = -1;
  294. static int __initdata irq = -1;
  295. static int __initdata dma = -1;
  296. static int __initdata dma2 = -1;
  297. static int __initdata mpuio = -1;
  298. static int __initdata mpuirq = -1;
  299. static int __initdata synthio = -1;
  300. static int __initdata synthirq = -1;
  301. static int __initdata isapnp = 1;
  302. static unsigned int cs4232_devices;
  303. MODULE_DESCRIPTION("CS4232 based soundcard driver");
  304. MODULE_AUTHOR("Hannu Savolainen, Paul Barton-Davis");
  305. MODULE_LICENSE("GPL");
  306. module_param(io, int, 0);
  307. MODULE_PARM_DESC(io,"base I/O port for AD1848");
  308. module_param(irq, int, 0);
  309. MODULE_PARM_DESC(irq,"IRQ for AD1848 chip");
  310. module_param(dma, int, 0);
  311. MODULE_PARM_DESC(dma,"8 bit DMA for AD1848 chip");
  312. module_param(dma2, int, 0);
  313. MODULE_PARM_DESC(dma2,"16 bit DMA for AD1848 chip");
  314. module_param(mpuio, int, 0);
  315. MODULE_PARM_DESC(mpuio,"MPU 401 base address");
  316. module_param(mpuirq, int, 0);
  317. MODULE_PARM_DESC(mpuirq,"MPU 401 IRQ");
  318. module_param(synthio, int, 0);
  319. MODULE_PARM_DESC(synthio,"Maui WaveTable base I/O port");
  320. module_param(synthirq, int, 0);
  321. MODULE_PARM_DESC(synthirq,"Maui WaveTable IRQ");
  322. module_param(isapnp, bool, 0);
  323. MODULE_PARM_DESC(isapnp,"Enable ISAPnP probing (default 1)");
  324. module_param(bss, bool, 0);
  325. MODULE_PARM_DESC(bss,"Enable Bose Sound System Support (default 0)");
  326. /*
  327. * Install a CS4232 based card. Need to have ad1848 and mpu401
  328. * loaded ready.
  329. */
  330. /* All cs4232 based cards have the main ad1848 card either as CSC0000 or
  331. * CSC0100. */
  332. static const struct pnp_device_id cs4232_pnp_table[] = {
  333. { .id = "CSC0100", .driver_data = 0 },
  334. { .id = "CSC0000", .driver_data = 0 },
  335. /* Guillemot Turtlebeach something appears to be cs4232 compatible
  336. * (untested) */
  337. { .id = "GIM0100", .driver_data = 0 },
  338. { .id = ""}
  339. };
  340. MODULE_DEVICE_TABLE(pnp, cs4232_pnp_table);
  341. static int __init cs4232_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  342. {
  343. struct address_info *isapnpcfg;
  344. isapnpcfg = kmalloc(sizeof(*isapnpcfg),GFP_KERNEL);
  345. if (!isapnpcfg)
  346. return -ENOMEM;
  347. isapnpcfg->irq = pnp_irq(dev, 0);
  348. isapnpcfg->dma = pnp_dma(dev, 0);
  349. isapnpcfg->dma2 = pnp_dma(dev, 1);
  350. isapnpcfg->io_base = pnp_port_start(dev, 0);
  351. if (probe_cs4232(isapnpcfg,TRUE) == 0) {
  352. printk(KERN_ERR "cs4232: ISA PnP card found, but not detected?\n");
  353. kfree(isapnpcfg);
  354. return -ENODEV;
  355. }
  356. pnp_set_drvdata(dev,isapnpcfg);
  357. cs4232_devices++;
  358. return 0;
  359. }
  360. static void __devexit cs4232_pnp_remove(struct pnp_dev *dev)
  361. {
  362. struct address_info *cfg = pnp_get_drvdata(dev);
  363. if (cfg) {
  364. unload_cs4232(cfg);
  365. kfree(cfg);
  366. }
  367. }
  368. static struct pnp_driver cs4232_driver = {
  369. .name = "cs4232",
  370. .id_table = cs4232_pnp_table,
  371. .probe = cs4232_pnp_probe,
  372. .remove = __devexit_p(cs4232_pnp_remove),
  373. };
  374. static int __init init_cs4232(void)
  375. {
  376. #ifdef CONFIG_SOUND_WAVEFRONT_MODULE
  377. if(synthio == -1)
  378. printk(KERN_INFO "cs4232: set synthio and synthirq to use the wavefront facilities.\n");
  379. else {
  380. synth_base = synthio;
  381. synth_irq = synthirq;
  382. }
  383. #else
  384. if(synthio != -1)
  385. printk(KERN_WARNING "cs4232: wavefront support not enabled in this driver.\n");
  386. #endif
  387. cfg.irq = -1;
  388. if (isapnp) {
  389. pnp_register_driver(&cs4232_driver);
  390. if (cs4232_devices)
  391. return 0;
  392. }
  393. if(io==-1||irq==-1||dma==-1)
  394. {
  395. printk(KERN_ERR "cs4232: Must set io, irq and dma.\n");
  396. return -ENODEV;
  397. }
  398. cfg.io_base = io;
  399. cfg.irq = irq;
  400. cfg.dma = dma;
  401. cfg.dma2 = dma2;
  402. cfg_mpu.io_base = -1;
  403. cfg_mpu.irq = -1;
  404. if (mpuio != -1 && mpuirq != -1) {
  405. cfg_mpu.io_base = mpuio;
  406. cfg_mpu.irq = mpuirq;
  407. probe_cs4232_mpu(&cfg_mpu); /* Bug always returns 0 not OK -- AC */
  408. }
  409. if (probe_cs4232(&cfg,FALSE) == 0)
  410. return -ENODEV;
  411. return 0;
  412. }
  413. static void __exit cleanup_cs4232(void)
  414. {
  415. pnp_unregister_driver(&cs4232_driver);
  416. if (cfg.irq != -1)
  417. unload_cs4232(&cfg); /* Unloads global MPU as well, if needed */
  418. }
  419. module_init(init_cs4232);
  420. module_exit(cleanup_cs4232);
  421. #ifndef MODULE
  422. static int __init setup_cs4232(char *str)
  423. {
  424. /* io, irq, dma, dma2 mpuio, mpuirq*/
  425. int ints[7];
  426. /* If we have isapnp cards, no need for options */
  427. pnp_register_driver(&cs4232_driver);
  428. if (cs4232_devices)
  429. return 1;
  430. str = get_options(str, ARRAY_SIZE(ints), ints);
  431. io = ints[1];
  432. irq = ints[2];
  433. dma = ints[3];
  434. dma2 = ints[4];
  435. mpuio = ints[5];
  436. mpuirq = ints[6];
  437. return 1;
  438. }
  439. __setup("cs4232=", setup_cs4232);
  440. #endif