cmi8328.c 13 KB

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