cs4232.c 13 KB

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