ymfpci.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * The driver for the Yamaha's DS1/DS1E cards
  4. * Copyright (c) by Jaroslav Kysela <[email protected]>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/pci.h>
  8. #include <linux/time.h>
  9. #include <linux/module.h>
  10. #include <sound/core.h>
  11. #include "ymfpci.h"
  12. #include <sound/mpu401.h>
  13. #include <sound/opl3.h>
  14. #include <sound/initval.h>
  15. MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
  16. MODULE_DESCRIPTION("Yamaha DS-1 PCI");
  17. MODULE_LICENSE("GPL");
  18. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  19. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  20. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  21. static long fm_port[SNDRV_CARDS];
  22. static long mpu_port[SNDRV_CARDS];
  23. #ifdef SUPPORT_JOYSTICK
  24. static long joystick_port[SNDRV_CARDS];
  25. #endif
  26. static bool rear_switch[SNDRV_CARDS];
  27. module_param_array(index, int, NULL, 0444);
  28. MODULE_PARM_DESC(index, "Index value for the Yamaha DS-1 PCI soundcard.");
  29. module_param_array(id, charp, NULL, 0444);
  30. MODULE_PARM_DESC(id, "ID string for the Yamaha DS-1 PCI soundcard.");
  31. module_param_array(enable, bool, NULL, 0444);
  32. MODULE_PARM_DESC(enable, "Enable Yamaha DS-1 soundcard.");
  33. module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
  34. MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
  35. module_param_hw_array(fm_port, long, ioport, NULL, 0444);
  36. MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
  37. #ifdef SUPPORT_JOYSTICK
  38. module_param_hw_array(joystick_port, long, ioport, NULL, 0444);
  39. MODULE_PARM_DESC(joystick_port, "Joystick port address");
  40. #endif
  41. module_param_array(rear_switch, bool, NULL, 0444);
  42. MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
  43. static const struct pci_device_id snd_ymfpci_ids[] = {
  44. { PCI_VDEVICE(YAMAHA, 0x0004), 0, }, /* YMF724 */
  45. { PCI_VDEVICE(YAMAHA, 0x000d), 0, }, /* YMF724F */
  46. { PCI_VDEVICE(YAMAHA, 0x000a), 0, }, /* YMF740 */
  47. { PCI_VDEVICE(YAMAHA, 0x000c), 0, }, /* YMF740C */
  48. { PCI_VDEVICE(YAMAHA, 0x0010), 0, }, /* YMF744 */
  49. { PCI_VDEVICE(YAMAHA, 0x0012), 0, }, /* YMF754 */
  50. { 0, }
  51. };
  52. MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
  53. #ifdef SUPPORT_JOYSTICK
  54. static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
  55. int legacy_ctrl, int legacy_ctrl2)
  56. {
  57. struct gameport *gp;
  58. struct resource *r = NULL;
  59. int io_port = joystick_port[dev];
  60. if (!io_port)
  61. return -ENODEV;
  62. if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
  63. if (io_port == 1) {
  64. /* auto-detect */
  65. io_port = pci_resource_start(chip->pci, 2);
  66. if (!io_port)
  67. return -ENODEV;
  68. }
  69. } else {
  70. if (io_port == 1) {
  71. /* auto-detect */
  72. for (io_port = 0x201; io_port <= 0x205; io_port++) {
  73. if (io_port == 0x203)
  74. continue;
  75. r = request_region(io_port, 1, "YMFPCI gameport");
  76. if (r)
  77. break;
  78. }
  79. if (!r) {
  80. dev_err(chip->card->dev,
  81. "no gameport ports available\n");
  82. return -EBUSY;
  83. }
  84. }
  85. switch (io_port) {
  86. case 0x201: legacy_ctrl2 |= 0 << 6; break;
  87. case 0x202: legacy_ctrl2 |= 1 << 6; break;
  88. case 0x204: legacy_ctrl2 |= 2 << 6; break;
  89. case 0x205: legacy_ctrl2 |= 3 << 6; break;
  90. default:
  91. dev_err(chip->card->dev,
  92. "invalid joystick port %#x", io_port);
  93. return -EINVAL;
  94. }
  95. }
  96. if (!r) {
  97. r = devm_request_region(&chip->pci->dev, io_port, 1,
  98. "YMFPCI gameport");
  99. if (!r) {
  100. dev_err(chip->card->dev,
  101. "joystick port %#x is in use.\n", io_port);
  102. return -EBUSY;
  103. }
  104. }
  105. chip->gameport = gp = gameport_allocate_port();
  106. if (!gp) {
  107. dev_err(chip->card->dev,
  108. "cannot allocate memory for gameport\n");
  109. return -ENOMEM;
  110. }
  111. gameport_set_name(gp, "Yamaha YMF Gameport");
  112. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  113. gameport_set_dev_parent(gp, &chip->pci->dev);
  114. gp->io = io_port;
  115. if (chip->pci->device >= 0x0010) /* YMF 744/754 */
  116. pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
  117. pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
  118. pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  119. gameport_register_port(chip->gameport);
  120. return 0;
  121. }
  122. void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
  123. {
  124. if (chip->gameport) {
  125. gameport_unregister_port(chip->gameport);
  126. chip->gameport = NULL;
  127. }
  128. }
  129. #else
  130. static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
  131. void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
  132. #endif /* SUPPORT_JOYSTICK */
  133. static int __snd_card_ymfpci_probe(struct pci_dev *pci,
  134. const struct pci_device_id *pci_id)
  135. {
  136. static int dev;
  137. struct snd_card *card;
  138. struct resource *fm_res = NULL;
  139. struct resource *mpu_res = NULL;
  140. struct snd_ymfpci *chip;
  141. struct snd_opl3 *opl3;
  142. const char *str, *model;
  143. int err;
  144. u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
  145. if (dev >= SNDRV_CARDS)
  146. return -ENODEV;
  147. if (!enable[dev]) {
  148. dev++;
  149. return -ENOENT;
  150. }
  151. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  152. sizeof(*chip), &card);
  153. if (err < 0)
  154. return err;
  155. chip = card->private_data;
  156. switch (pci_id->device) {
  157. case 0x0004: str = "YMF724"; model = "DS-1"; break;
  158. case 0x000d: str = "YMF724F"; model = "DS-1"; break;
  159. case 0x000a: str = "YMF740"; model = "DS-1L"; break;
  160. case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
  161. case 0x0010: str = "YMF744"; model = "DS-1S"; break;
  162. case 0x0012: str = "YMF754"; model = "DS-1E"; break;
  163. default: model = str = "???"; break;
  164. }
  165. legacy_ctrl = 0;
  166. legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
  167. if (pci_id->device >= 0x0010) { /* YMF 744/754 */
  168. if (fm_port[dev] == 1) {
  169. /* auto-detect */
  170. fm_port[dev] = pci_resource_start(pci, 1);
  171. }
  172. if (fm_port[dev] > 0)
  173. fm_res = devm_request_region(&pci->dev, fm_port[dev],
  174. 4, "YMFPCI OPL3");
  175. if (fm_res) {
  176. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  177. pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
  178. }
  179. if (mpu_port[dev] == 1) {
  180. /* auto-detect */
  181. mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
  182. }
  183. if (mpu_port[dev] > 0)
  184. mpu_res = devm_request_region(&pci->dev, mpu_port[dev],
  185. 2, "YMFPCI MPU401");
  186. if (mpu_res) {
  187. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  188. pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
  189. }
  190. } else {
  191. switch (fm_port[dev]) {
  192. case 0x388: legacy_ctrl2 |= 0; break;
  193. case 0x398: legacy_ctrl2 |= 1; break;
  194. case 0x3a0: legacy_ctrl2 |= 2; break;
  195. case 0x3a8: legacy_ctrl2 |= 3; break;
  196. default: fm_port[dev] = 0; break;
  197. }
  198. if (fm_port[dev] > 0)
  199. fm_res = devm_request_region(&pci->dev, fm_port[dev],
  200. 4, "YMFPCI OPL3");
  201. if (fm_res) {
  202. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  203. } else {
  204. legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
  205. fm_port[dev] = 0;
  206. }
  207. switch (mpu_port[dev]) {
  208. case 0x330: legacy_ctrl2 |= 0 << 4; break;
  209. case 0x300: legacy_ctrl2 |= 1 << 4; break;
  210. case 0x332: legacy_ctrl2 |= 2 << 4; break;
  211. case 0x334: legacy_ctrl2 |= 3 << 4; break;
  212. default: mpu_port[dev] = 0; break;
  213. }
  214. if (mpu_port[dev] > 0)
  215. mpu_res = devm_request_region(&pci->dev, mpu_port[dev],
  216. 2, "YMFPCI MPU401");
  217. if (mpu_res) {
  218. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  219. } else {
  220. legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
  221. mpu_port[dev] = 0;
  222. }
  223. }
  224. if (mpu_res) {
  225. legacy_ctrl |= YMFPCI_LEGACY_MIEN;
  226. legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
  227. }
  228. pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
  229. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  230. pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  231. err = snd_ymfpci_create(card, pci, old_legacy_ctrl);
  232. if (err < 0)
  233. return err;
  234. strcpy(card->driver, str);
  235. sprintf(card->shortname, "Yamaha %s (%s)", model, str);
  236. sprintf(card->longname, "%s at 0x%lx, irq %i",
  237. card->shortname,
  238. chip->reg_area_phys,
  239. chip->irq);
  240. err = snd_ymfpci_pcm(chip, 0);
  241. if (err < 0)
  242. return err;
  243. err = snd_ymfpci_pcm_spdif(chip, 1);
  244. if (err < 0)
  245. return err;
  246. err = snd_ymfpci_mixer(chip, rear_switch[dev]);
  247. if (err < 0)
  248. return err;
  249. if (chip->ac97->ext_id & AC97_EI_SDAC) {
  250. err = snd_ymfpci_pcm_4ch(chip, 2);
  251. if (err < 0)
  252. return err;
  253. err = snd_ymfpci_pcm2(chip, 3);
  254. if (err < 0)
  255. return err;
  256. }
  257. err = snd_ymfpci_timer(chip, 0);
  258. if (err < 0)
  259. return err;
  260. if (mpu_res) {
  261. err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
  262. mpu_port[dev],
  263. MPU401_INFO_INTEGRATED |
  264. MPU401_INFO_IRQ_HOOK,
  265. -1, &chip->rawmidi);
  266. if (err < 0) {
  267. dev_warn(card->dev,
  268. "cannot initialize MPU401 at 0x%lx, skipping...\n",
  269. mpu_port[dev]);
  270. legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
  271. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  272. }
  273. }
  274. if (fm_res) {
  275. err = snd_opl3_create(card,
  276. fm_port[dev],
  277. fm_port[dev] + 2,
  278. OPL3_HW_OPL3, 1, &opl3);
  279. if (err < 0) {
  280. dev_warn(card->dev,
  281. "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
  282. fm_port[dev]);
  283. legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
  284. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  285. } else {
  286. err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  287. if (err < 0) {
  288. dev_err(card->dev, "cannot create opl3 hwdep\n");
  289. return err;
  290. }
  291. }
  292. }
  293. snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
  294. err = snd_card_register(card);
  295. if (err < 0)
  296. return err;
  297. pci_set_drvdata(pci, card);
  298. dev++;
  299. return 0;
  300. }
  301. static int snd_card_ymfpci_probe(struct pci_dev *pci,
  302. const struct pci_device_id *pci_id)
  303. {
  304. return snd_card_free_on_error(&pci->dev, __snd_card_ymfpci_probe(pci, pci_id));
  305. }
  306. static struct pci_driver ymfpci_driver = {
  307. .name = KBUILD_MODNAME,
  308. .id_table = snd_ymfpci_ids,
  309. .probe = snd_card_ymfpci_probe,
  310. #ifdef CONFIG_PM_SLEEP
  311. .driver = {
  312. .pm = &snd_ymfpci_pm,
  313. },
  314. #endif
  315. };
  316. module_pci_driver(ymfpci_driver);