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");
21 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
22 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
23 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
24 static long fm_port[SNDRV_CARDS];
25 static long mpu_port[SNDRV_CARDS];
26 #ifdef SUPPORT_JOYSTICK
27 static long joystick_port[SNDRV_CARDS];
29 static bool rear_switch[SNDRV_CARDS];
31 module_param_array(index, int, NULL, 0444);
32 MODULE_PARM_DESC(index, "Index value for the Yamaha DS-1 PCI soundcard.");
33 module_param_array(id, charp, NULL, 0444);
34 MODULE_PARM_DESC(id, "ID string for the Yamaha DS-1 PCI soundcard.");
35 module_param_array(enable, bool, NULL, 0444);
36 MODULE_PARM_DESC(enable, "Enable Yamaha DS-1 soundcard.");
37 module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
38 MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
39 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
40 MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
41 #ifdef SUPPORT_JOYSTICK
42 module_param_hw_array(joystick_port, long, ioport, NULL, 0444);
43 MODULE_PARM_DESC(joystick_port, "Joystick port address");
45 module_param_array(rear_switch, bool, NULL, 0444);
46 MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
48 static const struct pci_device_id snd_ymfpci_ids[] = {
49 { PCI_VDEVICE(YAMAHA, 0x0004), 0, }, /* YMF724 */
50 { PCI_VDEVICE(YAMAHA, 0x000d), 0, }, /* YMF724F */
51 { PCI_VDEVICE(YAMAHA, 0x000a), 0, }, /* YMF740 */
52 { PCI_VDEVICE(YAMAHA, 0x000c), 0, }, /* YMF740C */
53 { PCI_VDEVICE(YAMAHA, 0x0010), 0, }, /* YMF744 */
54 { PCI_VDEVICE(YAMAHA, 0x0012), 0, }, /* YMF754 */
58 MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
60 #ifdef SUPPORT_JOYSTICK
61 static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
62 int legacy_ctrl, int legacy_ctrl2)
65 struct resource *r = NULL;
66 int io_port = joystick_port[dev];
71 if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
75 io_port = pci_resource_start(chip->pci, 2);
82 for (io_port = 0x201; io_port <= 0x205; io_port++) {
85 r = request_region(io_port, 1, "YMFPCI gameport");
90 dev_err(chip->card->dev,
91 "no gameport ports available\n");
96 case 0x201: legacy_ctrl2 |= 0 << 6; break;
97 case 0x202: legacy_ctrl2 |= 1 << 6; break;
98 case 0x204: legacy_ctrl2 |= 2 << 6; break;
99 case 0x205: legacy_ctrl2 |= 3 << 6; break;
101 dev_err(chip->card->dev,
102 "invalid joystick port %#x", io_port);
108 r = devm_request_region(&chip->pci->dev, io_port, 1,
111 dev_err(chip->card->dev,
112 "joystick port %#x is in use.\n", io_port);
117 chip->gameport = gp = gameport_allocate_port();
119 dev_err(chip->card->dev,
120 "cannot allocate memory for gameport\n");
125 gameport_set_name(gp, "Yamaha YMF Gameport");
126 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
127 gameport_set_dev_parent(gp, &chip->pci->dev);
130 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
131 pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
133 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
134 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
136 gameport_register_port(chip->gameport);
141 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
143 if (chip->gameport) {
144 gameport_unregister_port(chip->gameport);
145 chip->gameport = NULL;
149 static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
150 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
151 #endif /* SUPPORT_JOYSTICK */
153 static int snd_card_ymfpci_probe(struct pci_dev *pci,
154 const struct pci_device_id *pci_id)
157 struct snd_card *card;
158 struct resource *fm_res = NULL;
159 struct resource *mpu_res = NULL;
160 struct snd_ymfpci *chip;
161 struct snd_opl3 *opl3;
162 const char *str, *model;
164 u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
166 if (dev >= SNDRV_CARDS)
173 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
174 sizeof(*chip), &card);
177 chip = card->private_data;
179 switch (pci_id->device) {
180 case 0x0004: str = "YMF724"; model = "DS-1"; break;
181 case 0x000d: str = "YMF724F"; model = "DS-1"; break;
182 case 0x000a: str = "YMF740"; model = "DS-1L"; break;
183 case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
184 case 0x0010: str = "YMF744"; model = "DS-1S"; break;
185 case 0x0012: str = "YMF754"; model = "DS-1E"; break;
186 default: model = str = "???"; break;
190 legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
192 if (pci_id->device >= 0x0010) { /* YMF 744/754 */
193 if (fm_port[dev] == 1) {
195 fm_port[dev] = pci_resource_start(pci, 1);
197 if (fm_port[dev] > 0)
198 fm_res = devm_request_region(&pci->dev, fm_port[dev],
201 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
202 pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
204 if (mpu_port[dev] == 1) {
206 mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
208 if (mpu_port[dev] > 0)
209 mpu_res = devm_request_region(&pci->dev, mpu_port[dev],
212 legacy_ctrl |= YMFPCI_LEGACY_MEN;
213 pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
216 switch (fm_port[dev]) {
217 case 0x388: legacy_ctrl2 |= 0; break;
218 case 0x398: legacy_ctrl2 |= 1; break;
219 case 0x3a0: legacy_ctrl2 |= 2; break;
220 case 0x3a8: legacy_ctrl2 |= 3; break;
221 default: fm_port[dev] = 0; break;
223 if (fm_port[dev] > 0)
224 fm_res = devm_request_region(&pci->dev, fm_port[dev],
227 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
229 legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
232 switch (mpu_port[dev]) {
233 case 0x330: legacy_ctrl2 |= 0 << 4; break;
234 case 0x300: legacy_ctrl2 |= 1 << 4; break;
235 case 0x332: legacy_ctrl2 |= 2 << 4; break;
236 case 0x334: legacy_ctrl2 |= 3 << 4; break;
237 default: mpu_port[dev] = 0; break;
239 if (mpu_port[dev] > 0)
240 mpu_res = devm_request_region(&pci->dev, mpu_port[dev],
243 legacy_ctrl |= YMFPCI_LEGACY_MEN;
245 legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
250 legacy_ctrl |= YMFPCI_LEGACY_MIEN;
251 legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
253 pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
254 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
255 pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
256 err = snd_ymfpci_create(card, pci, old_legacy_ctrl);
260 strcpy(card->driver, str);
261 sprintf(card->shortname, "Yamaha %s (%s)", model, str);
262 sprintf(card->longname, "%s at 0x%lx, irq %i",
266 err = snd_ymfpci_pcm(chip, 0);
270 err = snd_ymfpci_pcm_spdif(chip, 1);
274 err = snd_ymfpci_mixer(chip, rear_switch[dev]);
278 if (chip->ac97->ext_id & AC97_EI_SDAC) {
279 err = snd_ymfpci_pcm_4ch(chip, 2);
283 err = snd_ymfpci_pcm2(chip, 3);
287 err = snd_ymfpci_timer(chip, 0);
292 err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
294 MPU401_INFO_INTEGRATED |
295 MPU401_INFO_IRQ_HOOK,
299 "cannot initialize MPU401 at 0x%lx, skipping...\n",
301 legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
302 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
306 err = snd_opl3_create(card,
309 OPL3_HW_OPL3, 1, &opl3);
312 "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
314 legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
315 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
317 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
319 dev_err(card->dev, "cannot create opl3 hwdep\n");
325 snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
327 err = snd_card_register(card);
331 pci_set_drvdata(pci, card);
336 static struct pci_driver ymfpci_driver = {
337 .name = KBUILD_MODNAME,
338 .id_table = snd_ymfpci_ids,
339 .probe = snd_card_ymfpci_probe,
340 #ifdef CONFIG_PM_SLEEP
342 .pm = &snd_ymfpci_pm,
347 module_pci_driver(ymfpci_driver);