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 if (!(io_port = pci_resource_start(chip->pci, 2)))
81 for (io_port = 0x201; io_port <= 0x205; io_port++) {
84 if ((r = request_region(io_port, 1, "YMFPCI gameport")) != NULL)
88 dev_err(chip->card->dev,
89 "no gameport ports available\n");
94 case 0x201: legacy_ctrl2 |= 0 << 6; break;
95 case 0x202: legacy_ctrl2 |= 1 << 6; break;
96 case 0x204: legacy_ctrl2 |= 2 << 6; break;
97 case 0x205: legacy_ctrl2 |= 3 << 6; break;
99 dev_err(chip->card->dev,
100 "invalid joystick port %#x", io_port);
105 if (!r && !(r = request_region(io_port, 1, "YMFPCI gameport"))) {
106 dev_err(chip->card->dev,
107 "joystick port %#x is in use.\n", io_port);
111 chip->gameport = gp = gameport_allocate_port();
113 dev_err(chip->card->dev,
114 "cannot allocate memory for gameport\n");
115 release_and_free_resource(r);
120 gameport_set_name(gp, "Yamaha YMF Gameport");
121 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
122 gameport_set_dev_parent(gp, &chip->pci->dev);
124 gameport_set_port_data(gp, r);
126 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
127 pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
129 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
130 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
132 gameport_register_port(chip->gameport);
137 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
139 if (chip->gameport) {
140 struct resource *r = gameport_get_port_data(chip->gameport);
142 gameport_unregister_port(chip->gameport);
143 chip->gameport = NULL;
145 release_and_free_resource(r);
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,
178 switch (pci_id->device) {
179 case 0x0004: str = "YMF724"; model = "DS-1"; break;
180 case 0x000d: str = "YMF724F"; model = "DS-1"; break;
181 case 0x000a: str = "YMF740"; model = "DS-1L"; break;
182 case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
183 case 0x0010: str = "YMF744"; model = "DS-1S"; break;
184 case 0x0012: str = "YMF754"; model = "DS-1E"; break;
185 default: model = str = "???"; break;
189 legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
191 if (pci_id->device >= 0x0010) { /* YMF 744/754 */
192 if (fm_port[dev] == 1) {
194 fm_port[dev] = pci_resource_start(pci, 1);
196 if (fm_port[dev] > 0 &&
197 (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
198 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
199 pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
201 if (mpu_port[dev] == 1) {
203 mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
205 if (mpu_port[dev] > 0 &&
206 (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
207 legacy_ctrl |= YMFPCI_LEGACY_MEN;
208 pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
211 switch (fm_port[dev]) {
212 case 0x388: legacy_ctrl2 |= 0; break;
213 case 0x398: legacy_ctrl2 |= 1; break;
214 case 0x3a0: legacy_ctrl2 |= 2; break;
215 case 0x3a8: legacy_ctrl2 |= 3; break;
216 default: fm_port[dev] = 0; break;
218 if (fm_port[dev] > 0 &&
219 (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
220 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
222 legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
225 switch (mpu_port[dev]) {
226 case 0x330: legacy_ctrl2 |= 0 << 4; break;
227 case 0x300: legacy_ctrl2 |= 1 << 4; break;
228 case 0x332: legacy_ctrl2 |= 2 << 4; break;
229 case 0x334: legacy_ctrl2 |= 3 << 4; break;
230 default: mpu_port[dev] = 0; break;
232 if (mpu_port[dev] > 0 &&
233 (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
234 legacy_ctrl |= YMFPCI_LEGACY_MEN;
236 legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
241 legacy_ctrl |= YMFPCI_LEGACY_MIEN;
242 legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
244 pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
245 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
246 pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
247 if ((err = snd_ymfpci_create(card, pci,
250 release_and_free_resource(mpu_res);
251 release_and_free_resource(fm_res);
254 chip->fm_res = fm_res;
255 chip->mpu_res = mpu_res;
256 card->private_data = chip;
258 strcpy(card->driver, str);
259 sprintf(card->shortname, "Yamaha %s (%s)", model, str);
260 sprintf(card->longname, "%s at 0x%lx, irq %i",
264 err = snd_ymfpci_pcm(chip, 0);
268 err = snd_ymfpci_pcm_spdif(chip, 1);
272 err = snd_ymfpci_mixer(chip, rear_switch[dev]);
276 if (chip->ac97->ext_id & AC97_EI_SDAC) {
277 err = snd_ymfpci_pcm_4ch(chip, 2);
281 err = snd_ymfpci_pcm2(chip, 3);
285 err = snd_ymfpci_timer(chip, 0);
290 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
292 MPU401_INFO_INTEGRATED |
293 MPU401_INFO_IRQ_HOOK,
294 -1, &chip->rawmidi)) < 0) {
296 "cannot initialize MPU401 at 0x%lx, skipping...\n",
298 legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
299 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
303 if ((err = snd_opl3_create(card,
306 OPL3_HW_OPL3, 1, &opl3)) < 0) {
308 "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
310 legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
311 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
312 } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
313 dev_err(card->dev, "cannot create opl3 hwdep\n");
318 snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
320 err = snd_card_register(card);
324 pci_set_drvdata(pci, card);
333 static void snd_card_ymfpci_remove(struct pci_dev *pci)
335 snd_card_free(pci_get_drvdata(pci));
338 static struct pci_driver ymfpci_driver = {
339 .name = KBUILD_MODNAME,
340 .id_table = snd_ymfpci_ids,
341 .probe = snd_card_ymfpci_probe,
342 .remove = snd_card_ymfpci_remove,
343 #ifdef CONFIG_PM_SLEEP
345 .pm = &snd_ymfpci_pm,
350 module_pci_driver(ymfpci_driver);