1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * The driver for the Yamaha's DS1/DS1E cards
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/init.h>
9 #include <linux/time.h>
10 #include <linux/module.h>
11 #include <sound/core.h>
13 #include <sound/mpu401.h>
14 #include <sound/opl3.h>
15 #include <sound/initval.h>
17 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
18 MODULE_DESCRIPTION("Yamaha DS-1 PCI");
19 MODULE_LICENSE("GPL");
20 MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF724},"
27 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
28 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
29 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
30 static long fm_port[SNDRV_CARDS];
31 static long mpu_port[SNDRV_CARDS];
32 #ifdef SUPPORT_JOYSTICK
33 static long joystick_port[SNDRV_CARDS];
35 static bool rear_switch[SNDRV_CARDS];
37 module_param_array(index, int, NULL, 0444);
38 MODULE_PARM_DESC(index, "Index value for the Yamaha DS-1 PCI soundcard.");
39 module_param_array(id, charp, NULL, 0444);
40 MODULE_PARM_DESC(id, "ID string for the Yamaha DS-1 PCI soundcard.");
41 module_param_array(enable, bool, NULL, 0444);
42 MODULE_PARM_DESC(enable, "Enable Yamaha DS-1 soundcard.");
43 module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
44 MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
45 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
46 MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
47 #ifdef SUPPORT_JOYSTICK
48 module_param_hw_array(joystick_port, long, ioport, NULL, 0444);
49 MODULE_PARM_DESC(joystick_port, "Joystick port address");
51 module_param_array(rear_switch, bool, NULL, 0444);
52 MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
54 static const struct pci_device_id snd_ymfpci_ids[] = {
55 { PCI_VDEVICE(YAMAHA, 0x0004), 0, }, /* YMF724 */
56 { PCI_VDEVICE(YAMAHA, 0x000d), 0, }, /* YMF724F */
57 { PCI_VDEVICE(YAMAHA, 0x000a), 0, }, /* YMF740 */
58 { PCI_VDEVICE(YAMAHA, 0x000c), 0, }, /* YMF740C */
59 { PCI_VDEVICE(YAMAHA, 0x0010), 0, }, /* YMF744 */
60 { PCI_VDEVICE(YAMAHA, 0x0012), 0, }, /* YMF754 */
64 MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
66 #ifdef SUPPORT_JOYSTICK
67 static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
68 int legacy_ctrl, int legacy_ctrl2)
71 struct resource *r = NULL;
72 int io_port = joystick_port[dev];
77 if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
81 io_port = pci_resource_start(chip->pci, 2);
88 for (io_port = 0x201; io_port <= 0x205; io_port++) {
91 r = request_region(io_port, 1, "YMFPCI gameport");
96 dev_err(chip->card->dev,
97 "no gameport ports available\n");
102 case 0x201: legacy_ctrl2 |= 0 << 6; break;
103 case 0x202: legacy_ctrl2 |= 1 << 6; break;
104 case 0x204: legacy_ctrl2 |= 2 << 6; break;
105 case 0x205: legacy_ctrl2 |= 3 << 6; break;
107 dev_err(chip->card->dev,
108 "invalid joystick port %#x", io_port);
114 r = request_region(io_port, 1, "YMFPCI gameport");
116 dev_err(chip->card->dev,
117 "joystick port %#x is in use.\n", io_port);
122 chip->gameport = gp = gameport_allocate_port();
124 dev_err(chip->card->dev,
125 "cannot allocate memory for gameport\n");
126 release_and_free_resource(r);
131 gameport_set_name(gp, "Yamaha YMF Gameport");
132 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
133 gameport_set_dev_parent(gp, &chip->pci->dev);
135 gameport_set_port_data(gp, r);
137 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
138 pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
140 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
141 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
143 gameport_register_port(chip->gameport);
148 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
150 if (chip->gameport) {
151 struct resource *r = gameport_get_port_data(chip->gameport);
153 gameport_unregister_port(chip->gameport);
154 chip->gameport = NULL;
156 release_and_free_resource(r);
160 static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
161 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
162 #endif /* SUPPORT_JOYSTICK */
164 static int snd_card_ymfpci_probe(struct pci_dev *pci,
165 const struct pci_device_id *pci_id)
168 struct snd_card *card;
169 struct resource *fm_res = NULL;
170 struct resource *mpu_res = NULL;
171 struct snd_ymfpci *chip;
172 struct snd_opl3 *opl3;
173 const char *str, *model;
175 u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
177 if (dev >= SNDRV_CARDS)
184 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
189 switch (pci_id->device) {
190 case 0x0004: str = "YMF724"; model = "DS-1"; break;
191 case 0x000d: str = "YMF724F"; model = "DS-1"; break;
192 case 0x000a: str = "YMF740"; model = "DS-1L"; break;
193 case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
194 case 0x0010: str = "YMF744"; model = "DS-1S"; break;
195 case 0x0012: str = "YMF754"; model = "DS-1E"; break;
196 default: model = str = "???"; break;
200 legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
202 if (pci_id->device >= 0x0010) { /* YMF 744/754 */
203 if (fm_port[dev] == 1) {
205 fm_port[dev] = pci_resource_start(pci, 1);
207 if (fm_port[dev] > 0)
208 fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3");
210 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
211 pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
213 if (mpu_port[dev] == 1) {
215 mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
217 if (mpu_port[dev] > 0)
218 mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401");
220 legacy_ctrl |= YMFPCI_LEGACY_MEN;
221 pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
224 switch (fm_port[dev]) {
225 case 0x388: legacy_ctrl2 |= 0; break;
226 case 0x398: legacy_ctrl2 |= 1; break;
227 case 0x3a0: legacy_ctrl2 |= 2; break;
228 case 0x3a8: legacy_ctrl2 |= 3; break;
229 default: fm_port[dev] = 0; break;
231 if (fm_port[dev] > 0)
232 fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3");
234 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
236 legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
239 switch (mpu_port[dev]) {
240 case 0x330: legacy_ctrl2 |= 0 << 4; break;
241 case 0x300: legacy_ctrl2 |= 1 << 4; break;
242 case 0x332: legacy_ctrl2 |= 2 << 4; break;
243 case 0x334: legacy_ctrl2 |= 3 << 4; break;
244 default: mpu_port[dev] = 0; break;
246 if (mpu_port[dev] > 0)
247 mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401");
249 legacy_ctrl |= YMFPCI_LEGACY_MEN;
251 legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
256 legacy_ctrl |= YMFPCI_LEGACY_MIEN;
257 legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
259 pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
260 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
261 pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
262 err = snd_ymfpci_create(card, pci, old_legacy_ctrl, &chip);
264 release_and_free_resource(mpu_res);
265 release_and_free_resource(fm_res);
268 chip->fm_res = fm_res;
269 chip->mpu_res = mpu_res;
270 card->private_data = chip;
272 strcpy(card->driver, str);
273 sprintf(card->shortname, "Yamaha %s (%s)", model, str);
274 sprintf(card->longname, "%s at 0x%lx, irq %i",
278 err = snd_ymfpci_pcm(chip, 0);
282 err = snd_ymfpci_pcm_spdif(chip, 1);
286 err = snd_ymfpci_mixer(chip, rear_switch[dev]);
290 if (chip->ac97->ext_id & AC97_EI_SDAC) {
291 err = snd_ymfpci_pcm_4ch(chip, 2);
295 err = snd_ymfpci_pcm2(chip, 3);
299 err = snd_ymfpci_timer(chip, 0);
304 err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
306 MPU401_INFO_INTEGRATED |
307 MPU401_INFO_IRQ_HOOK,
311 "cannot initialize MPU401 at 0x%lx, skipping...\n",
313 legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
314 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
318 err = snd_opl3_create(card,
321 OPL3_HW_OPL3, 1, &opl3);
324 "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
326 legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
327 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
329 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
331 dev_err(card->dev, "cannot create opl3 hwdep\n");
337 snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
339 err = snd_card_register(card);
343 pci_set_drvdata(pci, card);
352 static void snd_card_ymfpci_remove(struct pci_dev *pci)
354 snd_card_free(pci_get_drvdata(pci));
357 static struct pci_driver ymfpci_driver = {
358 .name = KBUILD_MODNAME,
359 .id_table = snd_ymfpci_ids,
360 .probe = snd_card_ymfpci_probe,
361 .remove = snd_card_ymfpci_remove,
362 #ifdef CONFIG_PM_SLEEP
364 .pm = &snd_ymfpci_pm,
369 module_pci_driver(ymfpci_driver);