cmi8328.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Driver for C-Media CMI8328-based soundcards, such as AudioExcel AV500
  3. * Copyright (c) 2012 Ondrej Zary
  4. *
  5. * AudioExcel AV500 card consists of:
  6. * - CMI8328 - main chip (SB Pro emulation, gameport, OPL3, MPU401, CD-ROM)
  7. * - CS4231A - WSS codec
  8. * - Dream SAM9233+GMS950400+RAM+ROM: Wavetable MIDI, connected to MPU401
  9. */
  10. #include <linux/init.h>
  11. #include <linux/isa.h>
  12. #include <linux/module.h>
  13. #include <linux/gameport.h>
  14. #include <asm/dma.h>
  15. #include <sound/core.h>
  16. #include <sound/wss.h>
  17. #include <sound/opl3.h>
  18. #include <sound/mpu401.h>
  19. #define SNDRV_LEGACY_FIND_FREE_IOPORT
  20. #define SNDRV_LEGACY_FIND_FREE_IRQ
  21. #define SNDRV_LEGACY_FIND_FREE_DMA
  22. #include <sound/initval.h>
  23. MODULE_AUTHOR("Ondrej Zary <linux@rainbow-software.org>");
  24. MODULE_DESCRIPTION("C-Media CMI8328");
  25. MODULE_LICENSE("GPL");
  26. #if defined(CONFIG_GAMEPORT) || defined(CONFIG_GAMEPORT_MODULE)
  27. #define SUPPORT_JOYSTICK 1
  28. #endif
  29. /* I/O port is configured by jumpers on the card to one of these */
  30. static int cmi8328_ports[] = { 0x530, 0xe80, 0xf40, 0x604 };
  31. #define CMI8328_MAX ARRAY_SIZE(cmi8328_ports)
  32. static int index[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = -1};
  33. static char *id[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = NULL};
  34. static long port[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
  35. static int irq[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
  36. static int dma1[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
  37. static int dma2[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
  38. static long mpuport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
  39. static int mpuirq[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
  40. #ifdef SUPPORT_JOYSTICK
  41. static bool gameport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = true};
  42. #endif
  43. module_param_array(index, int, NULL, 0444);
  44. MODULE_PARM_DESC(index, "Index value for CMI8328 soundcard.");
  45. module_param_array(id, charp, NULL, 0444);
  46. MODULE_PARM_DESC(id, "ID string for CMI8328 soundcard.");
  47. module_param_array(port, long, NULL, 0444);
  48. MODULE_PARM_DESC(port, "Port # for CMI8328 driver.");
  49. module_param_array(irq, int, NULL, 0444);
  50. MODULE_PARM_DESC(irq, "IRQ # for CMI8328 driver.");
  51. module_param_array(dma1, int, NULL, 0444);
  52. MODULE_PARM_DESC(dma1, "DMA1 for CMI8328 driver.");
  53. module_param_array(dma2, int, NULL, 0444);
  54. MODULE_PARM_DESC(dma2, "DMA2 for CMI8328 driver.");
  55. module_param_array(mpuport, long, NULL, 0444);
  56. MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8328 driver.");
  57. module_param_array(mpuirq, int, NULL, 0444);
  58. MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8328 MPU-401 port.");
  59. #ifdef SUPPORT_JOYSTICK
  60. module_param_array(gameport, bool, NULL, 0444);
  61. MODULE_PARM_DESC(gameport, "Enable gameport.");
  62. #endif
  63. struct snd_cmi8328 {
  64. u16 port;
  65. u8 cfg[3];
  66. u8 wss_cfg;
  67. struct snd_card *card;
  68. struct snd_wss *wss;
  69. #ifdef SUPPORT_JOYSTICK
  70. struct gameport *gameport;
  71. #endif
  72. };
  73. /* CMI8328 configuration registers */
  74. #define CFG1 0x61
  75. #define CFG1_SB_DISABLE (1 << 0)
  76. #define CFG1_GAMEPORT (1 << 1)
  77. /*
  78. * bit 0: SB: 0=enabled, 1=disabled
  79. * bit 1: gameport: 0=disabled, 1=enabled
  80. * bits 2-4: SB IRQ: 001=3, 010=5, 011=7, 100=9, 101=10, 110=11
  81. * bits 5-6: SB DMA: 00=disabled (when SB disabled), 01=DMA0, 10=DMA1, 11=DMA3
  82. * bit 7: SB port: 0=0x220, 1=0x240
  83. */
  84. #define CFG2 0x62
  85. #define CFG2_MPU_ENABLE (1 << 2)
  86. /*
  87. * bits 0-1: CD-ROM mode: 00=disabled, 01=Panasonic, 10=Sony/Mitsumi/Wearnes,
  88. 11=IDE
  89. * bit 2: MPU401: 0=disabled, 1=enabled
  90. * bits 3-4: MPU401 IRQ: 00=3, 01=5, 10=7, 11=9,
  91. * bits 5-7: MPU401 port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x332,
  92. 101=0x334, 110=0x336
  93. */
  94. #define CFG3 0x63
  95. /*
  96. * bits 0-2: CD-ROM IRQ: 000=disabled, 001=3, 010=5, 011=7, 100=9, 101=10,
  97. 110=11
  98. * bits 3-4: CD-ROM DMA: 00=disabled, 01=DMA0, 10=DMA1, 11=DMA3
  99. * bits 5-7: CD-ROM port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x340,
  100. 101=0x350, 110=0x360, 111=0x370
  101. */
  102. static u8 snd_cmi8328_cfg_read(u16 port, u8 reg)
  103. {
  104. outb(0x43, port + 3);
  105. outb(0x21, port + 3);
  106. outb(reg, port + 3);
  107. return inb(port);
  108. }
  109. static void snd_cmi8328_cfg_write(u16 port, u8 reg, u8 val)
  110. {
  111. outb(0x43, port + 3);
  112. outb(0x21, port + 3);
  113. outb(reg, port + 3);
  114. outb(val, port + 3); /* yes, value goes to the same port as index */
  115. }
  116. static void snd_cmi8328_cfg_save(u16 port, u8 cfg[])
  117. {
  118. cfg[0] = snd_cmi8328_cfg_read(port, CFG1);
  119. cfg[1] = snd_cmi8328_cfg_read(port, CFG2);
  120. cfg[2] = snd_cmi8328_cfg_read(port, CFG3);
  121. }
  122. static void snd_cmi8328_cfg_restore(u16 port, u8 cfg[])
  123. {
  124. snd_cmi8328_cfg_write(port, CFG1, cfg[0]);
  125. snd_cmi8328_cfg_write(port, CFG2, cfg[1]);
  126. snd_cmi8328_cfg_write(port, CFG3, cfg[2]);
  127. }
  128. static int snd_cmi8328_mixer(struct snd_wss *chip)
  129. {
  130. struct snd_card *card;
  131. struct snd_ctl_elem_id id1, id2;
  132. int err;
  133. card = chip->card;
  134. memset(&id1, 0, sizeof(id1));
  135. memset(&id2, 0, sizeof(id2));
  136. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  137. /* rename AUX0 switch to CD */
  138. strcpy(id1.name, "Aux Playback Switch");
  139. strcpy(id2.name, "CD Playback Switch");
  140. err = snd_ctl_rename_id(card, &id1, &id2);
  141. if (err < 0) {
  142. snd_printk(KERN_ERR "error renaming control\n");
  143. return err;
  144. }
  145. /* rename AUX0 volume to CD */
  146. strcpy(id1.name, "Aux Playback Volume");
  147. strcpy(id2.name, "CD Playback Volume");
  148. err = snd_ctl_rename_id(card, &id1, &id2);
  149. if (err < 0) {
  150. snd_printk(KERN_ERR "error renaming control\n");
  151. return err;
  152. }
  153. /* rename AUX1 switch to Synth */
  154. strcpy(id1.name, "Aux Playback Switch");
  155. id1.index = 1;
  156. strcpy(id2.name, "Synth Playback Switch");
  157. err = snd_ctl_rename_id(card, &id1, &id2);
  158. if (err < 0) {
  159. snd_printk(KERN_ERR "error renaming control\n");
  160. return err;
  161. }
  162. /* rename AUX1 volume to Synth */
  163. strcpy(id1.name, "Aux Playback Volume");
  164. id1.index = 1;
  165. strcpy(id2.name, "Synth Playback Volume");
  166. err = snd_ctl_rename_id(card, &id1, &id2);
  167. if (err < 0) {
  168. snd_printk(KERN_ERR "error renaming control\n");
  169. return err;
  170. }
  171. return 0;
  172. }
  173. /* find index of an item in "-1"-ended array */
  174. int array_find(int array[], int item)
  175. {
  176. int i;
  177. for (i = 0; array[i] != -1; i++)
  178. if (array[i] == item)
  179. return i;
  180. return -1;
  181. }
  182. /* the same for long */
  183. int array_find_l(long array[], long item)
  184. {
  185. int i;
  186. for (i = 0; array[i] != -1; i++)
  187. if (array[i] == item)
  188. return i;
  189. return -1;
  190. }
  191. static int snd_cmi8328_probe(struct device *pdev, unsigned int ndev)
  192. {
  193. struct snd_card *card;
  194. struct snd_opl3 *opl3;
  195. struct snd_cmi8328 *cmi;
  196. #ifdef SUPPORT_JOYSTICK
  197. struct resource *res;
  198. #endif
  199. int err, pos;
  200. static long mpu_ports[] = { 0x330, 0x300, 0x310, 0x320, 0x332, 0x334,
  201. 0x336, -1 };
  202. static u8 mpu_port_bits[] = { 3, 0, 1, 2, 4, 5, 6 };
  203. static int mpu_irqs[] = { 9, 7, 5, 3, -1 };
  204. static u8 mpu_irq_bits[] = { 3, 2, 1, 0 };
  205. static int irqs[] = { 9, 10, 11, 7, -1 };
  206. static u8 irq_bits[] = { 2, 3, 4, 1 };
  207. static int dma1s[] = { 3, 1, 0, -1 };
  208. static u8 dma_bits[] = { 3, 2, 1 };
  209. static int dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} };
  210. u16 port = cmi8328_ports[ndev];
  211. u8 val;
  212. /* 0xff is invalid configuration (but settable - hope it isn't set) */
  213. if (snd_cmi8328_cfg_read(port, CFG1) == 0xff)
  214. return -ENODEV;
  215. /* the SB disable bit must NEVER EVER be cleared or the WSS dies */
  216. snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE);
  217. if (snd_cmi8328_cfg_read(port, CFG1) != CFG1_SB_DISABLE)
  218. return -ENODEV;
  219. /* disable everything first */
  220. snd_cmi8328_cfg_write(port, CFG2, 0); /* disable CDROM and MPU401 */
  221. snd_cmi8328_cfg_write(port, CFG3, 0); /* disable CDROM IRQ and DMA */
  222. if (irq[ndev] == SNDRV_AUTO_IRQ) {
  223. irq[ndev] = snd_legacy_find_free_irq(irqs);
  224. if (irq[ndev] < 0) {
  225. snd_printk(KERN_ERR "unable to find a free IRQ\n");
  226. return -EBUSY;
  227. }
  228. }
  229. if (dma1[ndev] == SNDRV_AUTO_DMA) {
  230. dma1[ndev] = snd_legacy_find_free_dma(dma1s);
  231. if (dma1[ndev] < 0) {
  232. snd_printk(KERN_ERR "unable to find a free DMA1\n");
  233. return -EBUSY;
  234. }
  235. }
  236. if (dma2[ndev] == SNDRV_AUTO_DMA) {
  237. dma2[ndev] = snd_legacy_find_free_dma(dma2s[dma1[ndev] % 4]);
  238. if (dma2[ndev] < 0) {
  239. snd_printk(KERN_WARNING "unable to find a free DMA2, full-duplex will not work\n");
  240. dma2[ndev] = -1;
  241. }
  242. }
  243. /* configure WSS IRQ... */
  244. pos = array_find(irqs, irq[ndev]);
  245. if (pos < 0) {
  246. snd_printk(KERN_ERR "invalid IRQ %d\n", irq[ndev]);
  247. return -EINVAL;
  248. }
  249. val = irq_bits[pos] << 3;
  250. /* ...and DMA... */
  251. pos = array_find(dma1s, dma1[ndev]);
  252. if (pos < 0) {
  253. snd_printk(KERN_ERR "invalid DMA1 %d\n", dma1[ndev]);
  254. return -EINVAL;
  255. }
  256. val |= dma_bits[pos];
  257. /* ...and DMA2 */
  258. if (dma2[ndev] >= 0 && dma1[ndev] != dma2[ndev]) {
  259. pos = array_find(dma2s[dma1[ndev]], dma2[ndev]);
  260. if (pos < 0) {
  261. snd_printk(KERN_ERR "invalid DMA2 %d\n", dma2[ndev]);
  262. return -EINVAL;
  263. }
  264. val |= 0x04; /* enable separate capture DMA */
  265. }
  266. outb(val, port);
  267. err = snd_card_create(index[ndev], id[ndev], THIS_MODULE,
  268. sizeof(struct snd_cmi8328), &card);
  269. if (err < 0)
  270. return err;
  271. cmi = card->private_data;
  272. cmi->card = card;
  273. cmi->port = port;
  274. cmi->wss_cfg = val;
  275. snd_card_set_dev(card, pdev);
  276. err = snd_wss_create(card, port + 4, -1, irq[ndev], dma1[ndev],
  277. dma2[ndev], WSS_HW_DETECT, 0, &cmi->wss);
  278. if (err < 0)
  279. goto error;
  280. err = snd_wss_pcm(cmi->wss, 0, NULL);
  281. if (err < 0)
  282. goto error;
  283. err = snd_wss_mixer(cmi->wss);
  284. if (err < 0)
  285. goto error;
  286. err = snd_cmi8328_mixer(cmi->wss);
  287. if (err < 0)
  288. goto error;
  289. if (snd_wss_timer(cmi->wss, 0, NULL) < 0)
  290. snd_printk(KERN_WARNING "error initializing WSS timer\n");
  291. if (mpuport[ndev] == SNDRV_AUTO_PORT) {
  292. mpuport[ndev] = snd_legacy_find_free_ioport(mpu_ports, 2);
  293. if (mpuport[ndev] < 0)
  294. snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
  295. }
  296. if (mpuirq[ndev] == SNDRV_AUTO_IRQ) {
  297. mpuirq[ndev] = snd_legacy_find_free_irq(mpu_irqs);
  298. if (mpuirq[ndev] < 0)
  299. snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
  300. }
  301. /* enable and configure MPU401 */
  302. if (mpuport[ndev] > 0 && mpuirq[ndev] > 0) {
  303. val = CFG2_MPU_ENABLE;
  304. pos = array_find_l(mpu_ports, mpuport[ndev]);
  305. if (pos < 0)
  306. snd_printk(KERN_WARNING "invalid MPU401 port 0x%lx\n",
  307. mpuport[ndev]);
  308. else {
  309. val |= mpu_port_bits[pos] << 5;
  310. pos = array_find(mpu_irqs, mpuirq[ndev]);
  311. if (pos < 0)
  312. snd_printk(KERN_WARNING "invalid MPU401 IRQ %d\n",
  313. mpuirq[ndev]);
  314. else {
  315. val |= mpu_irq_bits[pos] << 3;
  316. snd_cmi8328_cfg_write(port, CFG2, val);
  317. if (snd_mpu401_uart_new(card, 0,
  318. MPU401_HW_MPU401, mpuport[ndev],
  319. 0, mpuirq[ndev], NULL) < 0)
  320. snd_printk(KERN_ERR "error initializing MPU401\n");
  321. }
  322. }
  323. }
  324. /* OPL3 is hardwired to 0x388 and cannot be disabled */
  325. if (snd_opl3_create(card, 0x388, 0x38a, OPL3_HW_AUTO, 0, &opl3) < 0)
  326. snd_printk(KERN_ERR "error initializing OPL3\n");
  327. else
  328. if (snd_opl3_hwdep_new(opl3, 0, 1, NULL) < 0)
  329. snd_printk(KERN_WARNING "error initializing OPL3 hwdep\n");
  330. strcpy(card->driver, "CMI8328");
  331. strcpy(card->shortname, "C-Media CMI8328");
  332. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d,%d",
  333. card->shortname, cmi->wss->port, irq[ndev], dma1[ndev],
  334. (dma2[ndev] >= 0) ? dma2[ndev] : dma1[ndev]);
  335. dev_set_drvdata(pdev, card);
  336. err = snd_card_register(card);
  337. if (err < 0)
  338. goto error;
  339. #ifdef SUPPORT_JOYSTICK
  340. if (!gameport[ndev])
  341. return 0;
  342. /* gameport is hardwired to 0x200 */
  343. res = request_region(0x200, 8, "CMI8328 gameport");
  344. if (!res)
  345. snd_printk(KERN_WARNING "unable to allocate gameport I/O port\n");
  346. else {
  347. struct gameport *gp = cmi->gameport = gameport_allocate_port();
  348. if (!cmi->gameport)
  349. release_and_free_resource(res);
  350. else {
  351. gameport_set_name(gp, "CMI8328 Gameport");
  352. gameport_set_phys(gp, "%s/gameport0", dev_name(pdev));
  353. gameport_set_dev_parent(gp, pdev);
  354. gp->io = 0x200;
  355. gameport_set_port_data(gp, res);
  356. /* Enable gameport */
  357. snd_cmi8328_cfg_write(port, CFG1,
  358. CFG1_SB_DISABLE | CFG1_GAMEPORT);
  359. gameport_register_port(gp);
  360. }
  361. }
  362. #endif
  363. return 0;
  364. error:
  365. snd_card_free(card);
  366. return err;
  367. }
  368. static int snd_cmi8328_remove(struct device *pdev, unsigned int dev)
  369. {
  370. struct snd_card *card = dev_get_drvdata(pdev);
  371. struct snd_cmi8328 *cmi = card->private_data;
  372. #ifdef SUPPORT_JOYSTICK
  373. if (cmi->gameport) {
  374. struct resource *res = gameport_get_port_data(cmi->gameport);
  375. gameport_unregister_port(cmi->gameport);
  376. release_and_free_resource(res);
  377. }
  378. #endif
  379. /* disable everything */
  380. snd_cmi8328_cfg_write(cmi->port, CFG1, CFG1_SB_DISABLE);
  381. snd_cmi8328_cfg_write(cmi->port, CFG2, 0);
  382. snd_cmi8328_cfg_write(cmi->port, CFG3, 0);
  383. snd_card_free(card);
  384. dev_set_drvdata(pdev, NULL);
  385. return 0;
  386. }
  387. #ifdef CONFIG_PM
  388. static int snd_cmi8328_suspend(struct device *pdev, unsigned int n,
  389. pm_message_t state)
  390. {
  391. struct snd_card *card = dev_get_drvdata(pdev);
  392. struct snd_cmi8328 *cmi;
  393. if (!card) /* ignore absent devices */
  394. return 0;
  395. cmi = card->private_data;
  396. snd_cmi8328_cfg_save(cmi->port, cmi->cfg);
  397. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  398. snd_pcm_suspend_all(cmi->wss->pcm);
  399. cmi->wss->suspend(cmi->wss);
  400. return 0;
  401. }
  402. static int snd_cmi8328_resume(struct device *pdev, unsigned int n)
  403. {
  404. struct snd_card *card = dev_get_drvdata(pdev);
  405. struct snd_cmi8328 *cmi;
  406. if (!card) /* ignore absent devices */
  407. return 0;
  408. cmi = card->private_data;
  409. snd_cmi8328_cfg_restore(cmi->port, cmi->cfg);
  410. outb(cmi->wss_cfg, cmi->port);
  411. cmi->wss->resume(cmi->wss);
  412. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  413. return 0;
  414. }
  415. #endif
  416. static struct isa_driver snd_cmi8328_driver = {
  417. .probe = snd_cmi8328_probe,
  418. .remove = snd_cmi8328_remove,
  419. #ifdef CONFIG_PM
  420. .suspend = snd_cmi8328_suspend,
  421. .resume = snd_cmi8328_resume,
  422. #endif
  423. .driver = {
  424. .name = "cmi8328"
  425. },
  426. };
  427. static int __init alsa_card_cmi8328_init(void)
  428. {
  429. return isa_register_driver(&snd_cmi8328_driver, CMI8328_MAX);
  430. }
  431. static void __exit alsa_card_cmi8328_exit(void)
  432. {
  433. isa_unregister_driver(&snd_cmi8328_driver);
  434. }
  435. module_init(alsa_card_cmi8328_init)
  436. module_exit(alsa_card_cmi8328_exit)