xref: /openbmc/linux/sound/pci/ymfpci/ymfpci.c (revision 35267cea)
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 			io_port = pci_resource_start(chip->pci, 2);
76 			if (!io_port)
77 				return -ENODEV;
78 		}
79 	} else {
80 		if (io_port == 1) {
81 			/* auto-detect */
82 			for (io_port = 0x201; io_port <= 0x205; io_port++) {
83 				if (io_port == 0x203)
84 					continue;
85 				r = request_region(io_port, 1, "YMFPCI gameport");
86 				if (r)
87 					break;
88 			}
89 			if (!r) {
90 				dev_err(chip->card->dev,
91 					"no gameport ports available\n");
92 				return -EBUSY;
93 			}
94 		}
95 		switch (io_port) {
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;
100 		default:
101 			dev_err(chip->card->dev,
102 				"invalid joystick port %#x", io_port);
103 			return -EINVAL;
104 		}
105 	}
106 
107 	if (!r) {
108 		r = request_region(io_port, 1, "YMFPCI gameport");
109 		if (!r) {
110 			dev_err(chip->card->dev,
111 				"joystick port %#x is in use.\n", io_port);
112 			return -EBUSY;
113 		}
114 	}
115 
116 	chip->gameport = gp = gameport_allocate_port();
117 	if (!gp) {
118 		dev_err(chip->card->dev,
119 			"cannot allocate memory for gameport\n");
120 		release_and_free_resource(r);
121 		return -ENOMEM;
122 	}
123 
124 
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);
128 	gp->io = io_port;
129 	gameport_set_port_data(gp, r);
130 
131 	if (chip->pci->device >= 0x0010) /* YMF 744/754 */
132 		pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
133 
134 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
135 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
136 
137 	gameport_register_port(chip->gameport);
138 
139 	return 0;
140 }
141 
142 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
143 {
144 	if (chip->gameport) {
145 		struct resource *r = gameport_get_port_data(chip->gameport);
146 
147 		gameport_unregister_port(chip->gameport);
148 		chip->gameport = NULL;
149 
150 		release_and_free_resource(r);
151 	}
152 }
153 #else
154 static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
155 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
156 #endif /* SUPPORT_JOYSTICK */
157 
158 static int snd_card_ymfpci_probe(struct pci_dev *pci,
159 				 const struct pci_device_id *pci_id)
160 {
161 	static int dev;
162 	struct snd_card *card;
163 	struct resource *fm_res = NULL;
164 	struct resource *mpu_res = NULL;
165 	struct snd_ymfpci *chip;
166 	struct snd_opl3 *opl3;
167 	const char *str, *model;
168 	int err;
169 	u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
170 
171 	if (dev >= SNDRV_CARDS)
172 		return -ENODEV;
173 	if (!enable[dev]) {
174 		dev++;
175 		return -ENOENT;
176 	}
177 
178 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
179 			   0, &card);
180 	if (err < 0)
181 		return err;
182 
183 	switch (pci_id->device) {
184 	case 0x0004: str = "YMF724";  model = "DS-1"; break;
185 	case 0x000d: str = "YMF724F"; model = "DS-1"; break;
186 	case 0x000a: str = "YMF740";  model = "DS-1L"; break;
187 	case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
188 	case 0x0010: str = "YMF744";  model = "DS-1S"; break;
189 	case 0x0012: str = "YMF754";  model = "DS-1E"; break;
190 	default: model = str = "???"; break;
191 	}
192 
193 	legacy_ctrl = 0;
194 	legacy_ctrl2 = 0x0800;	/* SBEN = 0, SMOD = 01, LAD = 0 */
195 
196 	if (pci_id->device >= 0x0010) { /* YMF 744/754 */
197 		if (fm_port[dev] == 1) {
198 			/* auto-detect */
199 			fm_port[dev] = pci_resource_start(pci, 1);
200 		}
201 		if (fm_port[dev] > 0)
202 			fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3");
203 		if (fm_res) {
204 			legacy_ctrl |= YMFPCI_LEGACY_FMEN;
205 			pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
206 		}
207 		if (mpu_port[dev] == 1) {
208 			/* auto-detect */
209 			mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
210 		}
211 		if (mpu_port[dev] > 0)
212 			mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401");
213 		if (mpu_res) {
214 			legacy_ctrl |= YMFPCI_LEGACY_MEN;
215 			pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
216 		}
217 	} else {
218 		switch (fm_port[dev]) {
219 		case 0x388: legacy_ctrl2 |= 0; break;
220 		case 0x398: legacy_ctrl2 |= 1; break;
221 		case 0x3a0: legacy_ctrl2 |= 2; break;
222 		case 0x3a8: legacy_ctrl2 |= 3; break;
223 		default: fm_port[dev] = 0; break;
224 		}
225 		if (fm_port[dev] > 0)
226 			fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3");
227 		if (fm_res) {
228 			legacy_ctrl |= YMFPCI_LEGACY_FMEN;
229 		} else {
230 			legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
231 			fm_port[dev] = 0;
232 		}
233 		switch (mpu_port[dev]) {
234 		case 0x330: legacy_ctrl2 |= 0 << 4; break;
235 		case 0x300: legacy_ctrl2 |= 1 << 4; break;
236 		case 0x332: legacy_ctrl2 |= 2 << 4; break;
237 		case 0x334: legacy_ctrl2 |= 3 << 4; break;
238 		default: mpu_port[dev] = 0; break;
239 		}
240 		if (mpu_port[dev] > 0)
241 			mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401");
242 		if (mpu_res) {
243 			legacy_ctrl |= YMFPCI_LEGACY_MEN;
244 		} else {
245 			legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
246 			mpu_port[dev] = 0;
247 		}
248 	}
249 	if (mpu_res) {
250 		legacy_ctrl |= YMFPCI_LEGACY_MIEN;
251 		legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
252 	}
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, &chip);
257 	if (err  < 0) {
258 		release_and_free_resource(mpu_res);
259 		release_and_free_resource(fm_res);
260 		goto free_card;
261 	}
262 	chip->fm_res = fm_res;
263 	chip->mpu_res = mpu_res;
264 	card->private_data = chip;
265 
266 	strcpy(card->driver, str);
267 	sprintf(card->shortname, "Yamaha %s (%s)", model, str);
268 	sprintf(card->longname, "%s at 0x%lx, irq %i",
269 		card->shortname,
270 		chip->reg_area_phys,
271 		chip->irq);
272 	err = snd_ymfpci_pcm(chip, 0);
273 	if (err < 0)
274 		goto free_card;
275 
276 	err = snd_ymfpci_pcm_spdif(chip, 1);
277 	if (err < 0)
278 		goto free_card;
279 
280 	err = snd_ymfpci_mixer(chip, rear_switch[dev]);
281 	if (err < 0)
282 		goto free_card;
283 
284 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
285 		err = snd_ymfpci_pcm_4ch(chip, 2);
286 		if (err < 0)
287 			goto free_card;
288 
289 		err = snd_ymfpci_pcm2(chip, 3);
290 		if (err < 0)
291 			goto free_card;
292 	}
293 	err = snd_ymfpci_timer(chip, 0);
294 	if (err < 0)
295 		goto free_card;
296 
297 	if (chip->mpu_res) {
298 		err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
299 					  mpu_port[dev],
300 					  MPU401_INFO_INTEGRATED |
301 					  MPU401_INFO_IRQ_HOOK,
302 					  -1, &chip->rawmidi);
303 		if (err < 0) {
304 			dev_warn(card->dev,
305 				 "cannot initialize MPU401 at 0x%lx, skipping...\n",
306 				 mpu_port[dev]);
307 			legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
308 			pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
309 		}
310 	}
311 	if (chip->fm_res) {
312 		err = snd_opl3_create(card,
313 				      fm_port[dev],
314 				      fm_port[dev] + 2,
315 				      OPL3_HW_OPL3, 1, &opl3);
316 		if (err < 0) {
317 			dev_warn(card->dev,
318 				 "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
319 				 fm_port[dev]);
320 			legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
321 			pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
322 		} else {
323 			err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
324 			if (err < 0) {
325 				dev_err(card->dev, "cannot create opl3 hwdep\n");
326 				goto free_card;
327 			}
328 		}
329 	}
330 
331 	snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
332 
333 	err = snd_card_register(card);
334 	if (err < 0)
335 		goto free_card;
336 
337 	pci_set_drvdata(pci, card);
338 	dev++;
339 	return 0;
340 
341 free_card:
342 	snd_card_free(card);
343 	return err;
344 }
345 
346 static void snd_card_ymfpci_remove(struct pci_dev *pci)
347 {
348 	snd_card_free(pci_get_drvdata(pci));
349 }
350 
351 static struct pci_driver ymfpci_driver = {
352 	.name = KBUILD_MODNAME,
353 	.id_table = snd_ymfpci_ids,
354 	.probe = snd_card_ymfpci_probe,
355 	.remove = snd_card_ymfpci_remove,
356 #ifdef CONFIG_PM_SLEEP
357 	.driver = {
358 		.pm = &snd_ymfpci_pm,
359 	},
360 #endif
361 };
362 
363 module_pci_driver(ymfpci_driver);
364