xref: /openbmc/linux/sound/pci/ymfpci/ymfpci.c (revision d91cbe83)
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 <perex@perex.cz>
5  */
6 
7 #include <linux/init.h>
8 #include <linux/pci.h>
9 #include <linux/time.h>
10 #include <linux/module.h>
11 #include <sound/core.h>
12 #include "ymfpci.h"
13 #include <sound/mpu401.h>
14 #include <sound/opl3.h>
15 #include <sound/initval.h>
16 
17 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
18 MODULE_DESCRIPTION("Yamaha DS-1 PCI");
19 MODULE_LICENSE("GPL");
20 
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];
28 #endif
29 static bool rear_switch[SNDRV_CARDS];
30 
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");
44 #endif
45 module_param_array(rear_switch, bool, NULL, 0444);
46 MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
47 
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 */
55 	{ 0, }
56 };
57 
58 MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
59 
60 #ifdef SUPPORT_JOYSTICK
61 static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
62 				      int legacy_ctrl, int legacy_ctrl2)
63 {
64 	struct gameport *gp;
65 	struct resource *r = NULL;
66 	int io_port = joystick_port[dev];
67 
68 	if (!io_port)
69 		return -ENODEV;
70 
71 	if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
72 
73 		if (io_port == 1) {
74 			/* auto-detect */
75 			if (!(io_port = pci_resource_start(chip->pci, 2)))
76 				return -ENODEV;
77 		}
78 	} else {
79 		if (io_port == 1) {
80 			/* auto-detect */
81 			for (io_port = 0x201; io_port <= 0x205; io_port++) {
82 				if (io_port == 0x203)
83 					continue;
84 				if ((r = request_region(io_port, 1, "YMFPCI gameport")) != NULL)
85 					break;
86 			}
87 			if (!r) {
88 				dev_err(chip->card->dev,
89 					"no gameport ports available\n");
90 				return -EBUSY;
91 			}
92 		}
93 		switch (io_port) {
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;
98 		default:
99 			dev_err(chip->card->dev,
100 				"invalid joystick port %#x", io_port);
101 			return -EINVAL;
102 		}
103 	}
104 
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);
108 		return -EBUSY;
109 	}
110 
111 	chip->gameport = gp = gameport_allocate_port();
112 	if (!gp) {
113 		dev_err(chip->card->dev,
114 			"cannot allocate memory for gameport\n");
115 		release_and_free_resource(r);
116 		return -ENOMEM;
117 	}
118 
119 
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);
123 	gp->io = io_port;
124 	gameport_set_port_data(gp, r);
125 
126 	if (chip->pci->device >= 0x0010) /* YMF 744/754 */
127 		pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
128 
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);
131 
132 	gameport_register_port(chip->gameport);
133 
134 	return 0;
135 }
136 
137 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
138 {
139 	if (chip->gameport) {
140 		struct resource *r = gameport_get_port_data(chip->gameport);
141 
142 		gameport_unregister_port(chip->gameport);
143 		chip->gameport = NULL;
144 
145 		release_and_free_resource(r);
146 	}
147 }
148 #else
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 */
152 
153 static int snd_card_ymfpci_probe(struct pci_dev *pci,
154 				 const struct pci_device_id *pci_id)
155 {
156 	static int dev;
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;
163 	int err;
164 	u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
165 
166 	if (dev >= SNDRV_CARDS)
167 		return -ENODEV;
168 	if (!enable[dev]) {
169 		dev++;
170 		return -ENOENT;
171 	}
172 
173 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
174 			   0, &card);
175 	if (err < 0)
176 		return err;
177 
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;
186 	}
187 
188 	legacy_ctrl = 0;
189 	legacy_ctrl2 = 0x0800;	/* SBEN = 0, SMOD = 01, LAD = 0 */
190 
191 	if (pci_id->device >= 0x0010) { /* YMF 744/754 */
192 		if (fm_port[dev] == 1) {
193 			/* auto-detect */
194 			fm_port[dev] = pci_resource_start(pci, 1);
195 		}
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]);
200 		}
201 		if (mpu_port[dev] == 1) {
202 			/* auto-detect */
203 			mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
204 		}
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]);
209 		}
210 	} else {
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;
217 		}
218 		if (fm_port[dev] > 0 &&
219 		    (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
220 			legacy_ctrl |= YMFPCI_LEGACY_FMEN;
221 		} else {
222 			legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
223 			fm_port[dev] = 0;
224 		}
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;
231 		}
232 		if (mpu_port[dev] > 0 &&
233 		    (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
234 			legacy_ctrl |= YMFPCI_LEGACY_MEN;
235 		} else {
236 			legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
237 			mpu_port[dev] = 0;
238 		}
239 	}
240 	if (mpu_res) {
241 		legacy_ctrl |= YMFPCI_LEGACY_MIEN;
242 		legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
243 	}
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,
248 				     old_legacy_ctrl,
249 			 	     &chip)) < 0) {
250 		release_and_free_resource(mpu_res);
251 		release_and_free_resource(fm_res);
252 		goto free_card;
253 	}
254 	chip->fm_res = fm_res;
255 	chip->mpu_res = mpu_res;
256 	card->private_data = chip;
257 
258 	strcpy(card->driver, str);
259 	sprintf(card->shortname, "Yamaha %s (%s)", model, str);
260 	sprintf(card->longname, "%s at 0x%lx, irq %i",
261 		card->shortname,
262 		chip->reg_area_phys,
263 		chip->irq);
264 	err = snd_ymfpci_pcm(chip, 0);
265 	if (err < 0)
266 		goto free_card;
267 
268 	err = snd_ymfpci_pcm_spdif(chip, 1);
269 	if (err < 0)
270 		goto free_card;
271 
272 	err = snd_ymfpci_mixer(chip, rear_switch[dev]);
273 	if (err < 0)
274 		goto free_card;
275 
276 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
277 		err = snd_ymfpci_pcm_4ch(chip, 2);
278 		if (err < 0)
279 			goto free_card;
280 
281 		err = snd_ymfpci_pcm2(chip, 3);
282 		if (err < 0)
283 			goto free_card;
284 	}
285 	err = snd_ymfpci_timer(chip, 0);
286 	if (err < 0)
287 		goto free_card;
288 
289 	if (chip->mpu_res) {
290 		if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
291 					       mpu_port[dev],
292 					       MPU401_INFO_INTEGRATED |
293 					       MPU401_INFO_IRQ_HOOK,
294 					       -1, &chip->rawmidi)) < 0) {
295 			dev_warn(card->dev,
296 				 "cannot initialize MPU401 at 0x%lx, skipping...\n",
297 				 mpu_port[dev]);
298 			legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
299 			pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
300 		}
301 	}
302 	if (chip->fm_res) {
303 		if ((err = snd_opl3_create(card,
304 					   fm_port[dev],
305 					   fm_port[dev] + 2,
306 					   OPL3_HW_OPL3, 1, &opl3)) < 0) {
307 			dev_warn(card->dev,
308 				 "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
309 				 fm_port[dev]);
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");
314 			goto free_card;
315 		}
316 	}
317 
318 	snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
319 
320 	err = snd_card_register(card);
321 	if (err < 0)
322 		goto free_card;
323 
324 	pci_set_drvdata(pci, card);
325 	dev++;
326 	return 0;
327 
328 free_card:
329 	snd_card_free(card);
330 	return err;
331 }
332 
333 static void snd_card_ymfpci_remove(struct pci_dev *pci)
334 {
335 	snd_card_free(pci_get_drvdata(pci));
336 }
337 
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
344 	.driver = {
345 		.pm = &snd_ymfpci_pm,
346 	},
347 #endif
348 };
349 
350 module_pci_driver(ymfpci_driver);
351