cmi8328.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. #ifdef CONFIG_PM
  117. static void snd_cmi8328_cfg_save(u16 port, u8 cfg[])
  118. {
  119. cfg[0] = snd_cmi8328_cfg_read(port, CFG1);
  120. cfg[1] = snd_cmi8328_cfg_read(port, CFG2);
  121. cfg[2] = snd_cmi8328_cfg_read(port, CFG3);
  122. }
  123. static void snd_cmi8328_cfg_restore(u16 port, u8 cfg[])
  124. {
  125. snd_cmi8328_cfg_write(port, CFG1, cfg[0]);
  126. snd_cmi8328_cfg_write(port, CFG2, cfg[1]);
  127. snd_cmi8328_cfg_write(port, CFG3, cfg[2]);
  128. }
  129. #endif /* CONFIG_PM */
  130. static int snd_cmi8328_mixer(struct snd_wss *chip)
  131. {
  132. struct snd_card *card;
  133. struct snd_ctl_elem_id id1, id2;
  134. int err;
  135. card = chip->card;
  136. memset(&id1, 0, sizeof(id1));
  137. memset(&id2, 0, sizeof(id2));
  138. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  139. /* rename AUX0 switch to CD */
  140. strcpy(id1.name, "Aux Playback Switch");
  141. strcpy(id2.name, "CD Playback Switch");
  142. err = snd_ctl_rename_id(card, &id1, &id2);
  143. if (err < 0) {
  144. snd_printk(KERN_ERR "error renaming control\n");
  145. return err;
  146. }
  147. /* rename AUX0 volume to CD */
  148. strcpy(id1.name, "Aux Playback Volume");
  149. strcpy(id2.name, "CD Playback Volume");
  150. err = snd_ctl_rename_id(card, &id1, &id2);
  151. if (err < 0) {
  152. snd_printk(KERN_ERR "error renaming control\n");
  153. return err;
  154. }
  155. /* rename AUX1 switch to Synth */
  156. strcpy(id1.name, "Aux Playback Switch");
  157. id1.index = 1;
  158. strcpy(id2.name, "Synth Playback Switch");
  159. err = snd_ctl_rename_id(card, &id1, &id2);
  160. if (err < 0) {
  161. snd_printk(KERN_ERR "error renaming control\n");
  162. return err;
  163. }
  164. /* rename AUX1 volume to Synth */
  165. strcpy(id1.name, "Aux Playback Volume");
  166. id1.index = 1;
  167. strcpy(id2.name, "Synth Playback Volume");
  168. err = snd_ctl_rename_id(card, &id1, &id2);
  169. if (err < 0) {
  170. snd_printk(KERN_ERR "error renaming control\n");
  171. return err;
  172. }
  173. return 0;
  174. }
  175. /* find index of an item in "-1"-ended array */
  176. int array_find(int array[], int item)
  177. {
  178. int i;
  179. for (i = 0; array[i] != -1; i++)
  180. if (array[i] == item)
  181. return i;
  182. return -1;
  183. }
  184. /* the same for long */
  185. int array_find_l(long array[], long item)
  186. {
  187. int i;
  188. for (i = 0; array[i] != -1; i++)
  189. if (array[i] == item)
  190. return i;
  191. return -1;
  192. }
  193. static int snd_cmi8328_probe(struct device *pdev, unsigned int ndev)
  194. {
  195. struct snd_card *card;
  196. struct snd_opl3 *opl3;
  197. struct snd_cmi8328 *cmi;
  198. #ifdef SUPPORT_JOYSTICK
  199. struct resource *res;
  200. #endif
  201. int err, pos;
  202. static long mpu_ports[] = { 0x330, 0x300, 0x310, 0x320, 0x332, 0x334,
  203. 0x336, -1 };
  204. static u8 mpu_port_bits[] = { 3, 0, 1, 2, 4, 5, 6 };
  205. static int mpu_irqs[] = { 9, 7, 5, 3, -1 };
  206. static u8 mpu_irq_bits[] = { 3, 2, 1, 0 };
  207. static int irqs[] = { 9, 10, 11, 7, -1 };
  208. static u8 irq_bits[] = { 2, 3, 4, 1 };
  209. static int dma1s[] = { 3, 1, 0, -1 };
  210. static u8 dma_bits[] = { 3, 2, 1 };
  211. static int dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} };
  212. u16 port = cmi8328_ports[ndev];
  213. u8 val;
  214. /* 0xff is invalid configuration (but settable - hope it isn't set) */
  215. if (snd_cmi8328_cfg_read(port, CFG1) == 0xff)
  216. return -ENODEV;
  217. /* the SB disable bit must NEVER EVER be cleared or the WSS dies */
  218. snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE);
  219. if (snd_cmi8328_cfg_read(port, CFG1) != CFG1_SB_DISABLE)
  220. return -ENODEV;
  221. /* disable everything first */
  222. snd_cmi8328_cfg_write(port, CFG2, 0); /* disable CDROM and MPU401 */
  223. snd_cmi8328_cfg_write(port, CFG3, 0); /* disable CDROM IRQ and DMA */
  224. if (irq[ndev] == SNDRV_AUTO_IRQ) {
  225. irq[ndev] = snd_legacy_find_free_irq(irqs);
  226. if (irq[ndev] < 0) {
  227. snd_printk(KERN_ERR "unable to find a free IRQ\n");
  228. return -EBUSY;
  229. }
  230. }
  231. if (dma1[ndev] == SNDRV_AUTO_DMA) {
  232. dma1[ndev] = snd_legacy_find_free_dma(dma1s);
  233. if (dma1[ndev] < 0) {
  234. snd_printk(KERN_ERR "unable to find a free DMA1\n");
  235. return -EBUSY;
  236. }
  237. }
  238. if (dma2[ndev] == SNDRV_AUTO_DMA) {
  239. dma2[ndev] = snd_legacy_find_free_dma(dma2s[dma1[ndev] % 4]);
  240. if (dma2[ndev] < 0) {
  241. snd_printk(KERN_WARNING "unable to find a free DMA2, full-duplex will not work\n");
  242. dma2[ndev] = -1;
  243. }
  244. }
  245. /* configure WSS IRQ... */
  246. pos = array_find(irqs, irq[ndev]);
  247. if (pos < 0) {
  248. snd_printk(KERN_ERR "invalid IRQ %d\n", irq[ndev]);
  249. return -EINVAL;
  250. }
  251. val = irq_bits[pos] << 3;
  252. /* ...and DMA... */
  253. pos = array_find(dma1s, dma1[ndev]);
  254. if (pos < 0) {
  255. snd_printk(KERN_ERR "invalid DMA1 %d\n", dma1[ndev]);
  256. return -EINVAL;
  257. }
  258. val |= dma_bits[pos];
  259. /* ...and DMA2 */
  260. if (dma2[ndev] >= 0 && dma1[ndev] != dma2[ndev]) {
  261. pos = array_find(dma2s[dma1[ndev]], dma2[ndev]);
  262. if (pos < 0) {
  263. snd_printk(KERN_ERR "invalid DMA2 %d\n", dma2[ndev]);
  264. return -EINVAL;
  265. }
  266. val |= 0x04; /* enable separate capture DMA */
  267. }
  268. outb(val, port);
  269. err = snd_card_new(pdev, index[ndev], id[ndev], THIS_MODULE,
  270. sizeof(struct snd_cmi8328), &card);
  271. if (err < 0)
  272. return err;
  273. cmi = card->private_data;
  274. cmi->card = card;
  275. cmi->port = port;
  276. cmi->wss_cfg = val;
  277. err = snd_wss_create(card, port + 4, -1, irq[ndev], dma1[ndev],
  278. dma2[ndev], WSS_HW_DETECT, 0, &cmi->wss);
  279. if (err < 0)
  280. goto error;
  281. err = snd_wss_pcm(cmi->wss, 0);
  282. if (err < 0)
  283. goto error;
  284. err = snd_wss_mixer(cmi->wss);
  285. if (err < 0)
  286. goto error;
  287. err = snd_cmi8328_mixer(cmi->wss);
  288. if (err < 0)
  289. goto error;
  290. if (snd_wss_timer(cmi->wss, 0) < 0)
  291. snd_printk(KERN_WARNING "error initializing WSS timer\n");
  292. if (mpuport[ndev] == SNDRV_AUTO_PORT) {
  293. mpuport[ndev] = snd_legacy_find_free_ioport(mpu_ports, 2);
  294. if (mpuport[ndev] < 0)
  295. snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
  296. }
  297. if (mpuirq[ndev] == SNDRV_AUTO_IRQ) {
  298. mpuirq[ndev] = snd_legacy_find_free_irq(mpu_irqs);
  299. if (mpuirq[ndev] < 0)
  300. snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
  301. }
  302. /* enable and configure MPU401 */
  303. if (mpuport[ndev] > 0 && mpuirq[ndev] > 0) {
  304. val = CFG2_MPU_ENABLE;
  305. pos = array_find_l(mpu_ports, mpuport[ndev]);
  306. if (pos < 0)
  307. snd_printk(KERN_WARNING "invalid MPU401 port 0x%lx\n",
  308. mpuport[ndev]);
  309. else {
  310. val |= mpu_port_bits[pos] << 5;
  311. pos = array_find(mpu_irqs, mpuirq[ndev]);
  312. if (pos < 0)
  313. snd_printk(KERN_WARNING "invalid MPU401 IRQ %d\n",
  314. mpuirq[ndev]);
  315. else {
  316. val |= mpu_irq_bits[pos] << 3;
  317. snd_cmi8328_cfg_write(port, CFG2, val);
  318. if (snd_mpu401_uart_new(card, 0,
  319. MPU401_HW_MPU401, mpuport[ndev],
  320. 0, mpuirq[ndev], NULL) < 0)
  321. snd_printk(KERN_ERR "error initializing MPU401\n");
  322. }
  323. }
  324. }
  325. /* OPL3 is hardwired to 0x388 and cannot be disabled */
  326. if (snd_opl3_create(card, 0x388, 0x38a, OPL3_HW_AUTO, 0, &opl3) < 0)
  327. snd_printk(KERN_ERR "error initializing OPL3\n");
  328. else
  329. if (snd_opl3_hwdep_new(opl3, 0, 1, NULL) < 0)
  330. snd_printk(KERN_WARNING "error initializing OPL3 hwdep\n");
  331. strcpy(card->driver, "CMI8328");
  332. strcpy(card->shortname, "C-Media CMI8328");
  333. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d,%d",
  334. card->shortname, cmi->wss->port, irq[ndev], dma1[ndev],
  335. (dma2[ndev] >= 0) ? dma2[ndev] : dma1[ndev]);
  336. dev_set_drvdata(pdev, card);
  337. err = snd_card_register(card);
  338. if (err < 0)
  339. goto error;
  340. #ifdef SUPPORT_JOYSTICK
  341. if (!gameport[ndev])
  342. return 0;
  343. /* gameport is hardwired to 0x200 */
  344. res = request_region(0x200, 8, "CMI8328 gameport");
  345. if (!res)
  346. snd_printk(KERN_WARNING "unable to allocate gameport I/O port\n");
  347. else {
  348. struct gameport *gp = cmi->gameport = gameport_allocate_port();
  349. if (!cmi->gameport)
  350. release_and_free_resource(res);
  351. else {
  352. gameport_set_name(gp, "CMI8328 Gameport");
  353. gameport_set_phys(gp, "%s/gameport0", dev_name(pdev));
  354. gameport_set_dev_parent(gp, pdev);
  355. gp->io = 0x200;
  356. gameport_set_port_data(gp, res);
  357. /* Enable gameport */
  358. snd_cmi8328_cfg_write(port, CFG1,
  359. CFG1_SB_DISABLE | CFG1_GAMEPORT);
  360. gameport_register_port(gp);
  361. }
  362. }
  363. #endif
  364. return 0;
  365. error:
  366. snd_card_free(card);
  367. return err;
  368. }
  369. static int snd_cmi8328_remove(struct device *pdev, unsigned int dev)
  370. {
  371. struct snd_card *card = dev_get_drvdata(pdev);
  372. struct snd_cmi8328 *cmi = card->private_data;
  373. #ifdef SUPPORT_JOYSTICK
  374. if (cmi->gameport) {
  375. struct resource *res = gameport_get_port_data(cmi->gameport);
  376. gameport_unregister_port(cmi->gameport);
  377. release_and_free_resource(res);
  378. }
  379. #endif
  380. /* disable everything */
  381. snd_cmi8328_cfg_write(cmi->port, CFG1, CFG1_SB_DISABLE);
  382. snd_cmi8328_cfg_write(cmi->port, CFG2, 0);
  383. snd_cmi8328_cfg_write(cmi->port, CFG3, 0);
  384. snd_card_free(card);
  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)