xref: /linux/sound/pci/ymfpci/ymfpci.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 /*
2  *  The driver for the Yamaha's DS1/DS1E cards
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/time.h>
25 #include <linux/module.h>
26 #include <sound/core.h>
27 #include "ymfpci.h"
28 #include <sound/mpu401.h>
29 #include <sound/opl3.h>
30 #include <sound/initval.h>
31 
32 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
33 MODULE_DESCRIPTION("Yamaha DS-1 PCI");
34 MODULE_LICENSE("GPL");
35 MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF724},"
36 		"{Yamaha,YMF724F},"
37 		"{Yamaha,YMF740},"
38 		"{Yamaha,YMF740C},"
39 		"{Yamaha,YMF744},"
40 		"{Yamaha,YMF754}}");
41 
42 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
43 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
44 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
45 static long fm_port[SNDRV_CARDS];
46 static long mpu_port[SNDRV_CARDS];
47 #ifdef SUPPORT_JOYSTICK
48 static long joystick_port[SNDRV_CARDS];
49 #endif
50 static bool rear_switch[SNDRV_CARDS];
51 
52 module_param_array(index, int, NULL, 0444);
53 MODULE_PARM_DESC(index, "Index value for the Yamaha DS-1 PCI soundcard.");
54 module_param_array(id, charp, NULL, 0444);
55 MODULE_PARM_DESC(id, "ID string for the Yamaha DS-1 PCI soundcard.");
56 module_param_array(enable, bool, NULL, 0444);
57 MODULE_PARM_DESC(enable, "Enable Yamaha DS-1 soundcard.");
58 module_param_array(mpu_port, long, NULL, 0444);
59 MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
60 module_param_array(fm_port, long, NULL, 0444);
61 MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
62 #ifdef SUPPORT_JOYSTICK
63 module_param_array(joystick_port, long, NULL, 0444);
64 MODULE_PARM_DESC(joystick_port, "Joystick port address");
65 #endif
66 module_param_array(rear_switch, bool, NULL, 0444);
67 MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
68 
69 static DEFINE_PCI_DEVICE_TABLE(snd_ymfpci_ids) = {
70 	{ PCI_VDEVICE(YAMAHA, 0x0004), 0, },   /* YMF724 */
71 	{ PCI_VDEVICE(YAMAHA, 0x000d), 0, },   /* YMF724F */
72 	{ PCI_VDEVICE(YAMAHA, 0x000a), 0, },   /* YMF740 */
73 	{ PCI_VDEVICE(YAMAHA, 0x000c), 0, },   /* YMF740C */
74 	{ PCI_VDEVICE(YAMAHA, 0x0010), 0, },   /* YMF744 */
75 	{ PCI_VDEVICE(YAMAHA, 0x0012), 0, },   /* YMF754 */
76 	{ 0, }
77 };
78 
79 MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
80 
81 #ifdef SUPPORT_JOYSTICK
82 static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
83 				      int legacy_ctrl, int legacy_ctrl2)
84 {
85 	struct gameport *gp;
86 	struct resource *r = NULL;
87 	int io_port = joystick_port[dev];
88 
89 	if (!io_port)
90 		return -ENODEV;
91 
92 	if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
93 
94 		if (io_port == 1) {
95 			/* auto-detect */
96 			if (!(io_port = pci_resource_start(chip->pci, 2)))
97 				return -ENODEV;
98 		}
99 	} else {
100 		if (io_port == 1) {
101 			/* auto-detect */
102 			for (io_port = 0x201; io_port <= 0x205; io_port++) {
103 				if (io_port == 0x203)
104 					continue;
105 				if ((r = request_region(io_port, 1, "YMFPCI gameport")) != NULL)
106 					break;
107 			}
108 			if (!r) {
109 				printk(KERN_ERR "ymfpci: no gameport ports available\n");
110 				return -EBUSY;
111 			}
112 		}
113 		switch (io_port) {
114 		case 0x201: legacy_ctrl2 |= 0 << 6; break;
115 		case 0x202: legacy_ctrl2 |= 1 << 6; break;
116 		case 0x204: legacy_ctrl2 |= 2 << 6; break;
117 		case 0x205: legacy_ctrl2 |= 3 << 6; break;
118 		default:
119 			printk(KERN_ERR "ymfpci: invalid joystick port %#x", io_port);
120 			return -EINVAL;
121 		}
122 	}
123 
124 	if (!r && !(r = request_region(io_port, 1, "YMFPCI gameport"))) {
125 		printk(KERN_ERR "ymfpci: joystick port %#x is in use.\n", io_port);
126 		return -EBUSY;
127 	}
128 
129 	chip->gameport = gp = gameport_allocate_port();
130 	if (!gp) {
131 		printk(KERN_ERR "ymfpci: cannot allocate memory for gameport\n");
132 		release_and_free_resource(r);
133 		return -ENOMEM;
134 	}
135 
136 
137 	gameport_set_name(gp, "Yamaha YMF Gameport");
138 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
139 	gameport_set_dev_parent(gp, &chip->pci->dev);
140 	gp->io = io_port;
141 	gameport_set_port_data(gp, r);
142 
143 	if (chip->pci->device >= 0x0010) /* YMF 744/754 */
144 		pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
145 
146 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
147 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
148 
149 	gameport_register_port(chip->gameport);
150 
151 	return 0;
152 }
153 
154 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
155 {
156 	if (chip->gameport) {
157 		struct resource *r = gameport_get_port_data(chip->gameport);
158 
159 		gameport_unregister_port(chip->gameport);
160 		chip->gameport = NULL;
161 
162 		release_and_free_resource(r);
163 	}
164 }
165 #else
166 static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
167 void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
168 #endif /* SUPPORT_JOYSTICK */
169 
170 static int snd_card_ymfpci_probe(struct pci_dev *pci,
171 				 const struct pci_device_id *pci_id)
172 {
173 	static int dev;
174 	struct snd_card *card;
175 	struct resource *fm_res = NULL;
176 	struct resource *mpu_res = NULL;
177 	struct snd_ymfpci *chip;
178 	struct snd_opl3 *opl3;
179 	const char *str, *model;
180 	int err;
181 	u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
182 
183 	if (dev >= SNDRV_CARDS)
184 		return -ENODEV;
185 	if (!enable[dev]) {
186 		dev++;
187 		return -ENOENT;
188 	}
189 
190 	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
191 	if (err < 0)
192 		return err;
193 
194 	switch (pci_id->device) {
195 	case 0x0004: str = "YMF724";  model = "DS-1"; break;
196 	case 0x000d: str = "YMF724F"; model = "DS-1"; break;
197 	case 0x000a: str = "YMF740";  model = "DS-1L"; break;
198 	case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
199 	case 0x0010: str = "YMF744";  model = "DS-1S"; break;
200 	case 0x0012: str = "YMF754";  model = "DS-1E"; break;
201 	default: model = str = "???"; break;
202 	}
203 
204 	legacy_ctrl = 0;
205 	legacy_ctrl2 = 0x0800;	/* SBEN = 0, SMOD = 01, LAD = 0 */
206 
207 	if (pci_id->device >= 0x0010) { /* YMF 744/754 */
208 		if (fm_port[dev] == 1) {
209 			/* auto-detect */
210 			fm_port[dev] = pci_resource_start(pci, 1);
211 		}
212 		if (fm_port[dev] > 0 &&
213 		    (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
214 			legacy_ctrl |= YMFPCI_LEGACY_FMEN;
215 			pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
216 		}
217 		if (mpu_port[dev] == 1) {
218 			/* auto-detect */
219 			mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
220 		}
221 		if (mpu_port[dev] > 0 &&
222 		    (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
223 			legacy_ctrl |= YMFPCI_LEGACY_MEN;
224 			pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
225 		}
226 	} else {
227 		switch (fm_port[dev]) {
228 		case 0x388: legacy_ctrl2 |= 0; break;
229 		case 0x398: legacy_ctrl2 |= 1; break;
230 		case 0x3a0: legacy_ctrl2 |= 2; break;
231 		case 0x3a8: legacy_ctrl2 |= 3; break;
232 		default: fm_port[dev] = 0; break;
233 		}
234 		if (fm_port[dev] > 0 &&
235 		    (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
236 			legacy_ctrl |= YMFPCI_LEGACY_FMEN;
237 		} else {
238 			legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
239 			fm_port[dev] = 0;
240 		}
241 		switch (mpu_port[dev]) {
242 		case 0x330: legacy_ctrl2 |= 0 << 4; break;
243 		case 0x300: legacy_ctrl2 |= 1 << 4; break;
244 		case 0x332: legacy_ctrl2 |= 2 << 4; break;
245 		case 0x334: legacy_ctrl2 |= 3 << 4; break;
246 		default: mpu_port[dev] = 0; break;
247 		}
248 		if (mpu_port[dev] > 0 &&
249 		    (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
250 			legacy_ctrl |= YMFPCI_LEGACY_MEN;
251 		} else {
252 			legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
253 			mpu_port[dev] = 0;
254 		}
255 	}
256 	if (mpu_res) {
257 		legacy_ctrl |= YMFPCI_LEGACY_MIEN;
258 		legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
259 	}
260 	pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
261 	pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
262 	pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
263 	if ((err = snd_ymfpci_create(card, pci,
264 				     old_legacy_ctrl,
265 			 	     &chip)) < 0) {
266 		snd_card_free(card);
267 		release_and_free_resource(mpu_res);
268 		release_and_free_resource(fm_res);
269 		return err;
270 	}
271 	chip->fm_res = fm_res;
272 	chip->mpu_res = mpu_res;
273 	card->private_data = chip;
274 
275 	strcpy(card->driver, str);
276 	sprintf(card->shortname, "Yamaha %s (%s)", model, str);
277 	sprintf(card->longname, "%s at 0x%lx, irq %i",
278 		card->shortname,
279 		chip->reg_area_phys,
280 		chip->irq);
281 	if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
282 		snd_card_free(card);
283 		return err;
284 	}
285 	if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
286 		snd_card_free(card);
287 		return err;
288 	}
289 	err = snd_ymfpci_mixer(chip, rear_switch[dev]);
290 	if (err < 0) {
291 		snd_card_free(card);
292 		return err;
293 	}
294 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
295 		err = snd_ymfpci_pcm_4ch(chip, 2, NULL);
296 		if (err < 0) {
297 			snd_card_free(card);
298 			return err;
299 		}
300 		err = snd_ymfpci_pcm2(chip, 3, NULL);
301 		if (err < 0) {
302 			snd_card_free(card);
303 			return err;
304 		}
305 	}
306 	if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
307 		snd_card_free(card);
308 		return err;
309 	}
310 	if (chip->mpu_res) {
311 		if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
312 					       mpu_port[dev],
313 					       MPU401_INFO_INTEGRATED |
314 					       MPU401_INFO_IRQ_HOOK,
315 					       -1, &chip->rawmidi)) < 0) {
316 			printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
317 			legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
318 			pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
319 		}
320 	}
321 	if (chip->fm_res) {
322 		if ((err = snd_opl3_create(card,
323 					   fm_port[dev],
324 					   fm_port[dev] + 2,
325 					   OPL3_HW_OPL3, 1, &opl3)) < 0) {
326 			printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
327 			legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
328 			pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
329 		} else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
330 			snd_card_free(card);
331 			snd_printk(KERN_ERR "cannot create opl3 hwdep\n");
332 			return err;
333 		}
334 	}
335 
336 	snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
337 
338 	if ((err = snd_card_register(card)) < 0) {
339 		snd_card_free(card);
340 		return err;
341 	}
342 	pci_set_drvdata(pci, card);
343 	dev++;
344 	return 0;
345 }
346 
347 static void snd_card_ymfpci_remove(struct pci_dev *pci)
348 {
349 	snd_card_free(pci_get_drvdata(pci));
350 }
351 
352 static struct pci_driver ymfpci_driver = {
353 	.name = KBUILD_MODNAME,
354 	.id_table = snd_ymfpci_ids,
355 	.probe = snd_card_ymfpci_probe,
356 	.remove = snd_card_ymfpci_remove,
357 #ifdef CONFIG_PM_SLEEP
358 	.driver = {
359 		.pm = &snd_ymfpci_pm,
360 	},
361 #endif
362 };
363 
364 module_pci_driver(ymfpci_driver);
365