blob: 9e30e2c76c7f99d838cbea2d91e8a4abfe860d42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The driver for the Yamaha's DS1/DS1E cards
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
23#include <linux/init.h>
24#include <linux/pci.h>
25#include <linux/time.h>
26#include <linux/moduleparam.h>
27#include <sound/core.h>
28#include <sound/ymfpci.h>
29#include <sound/mpu401.h>
30#include <sound/opl3.h>
31#include <sound/initval.h>
32
33MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
34MODULE_DESCRIPTION("Yamaha DS-XG PCI");
35MODULE_LICENSE("GPL");
36MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF724},"
37 "{Yamaha,YMF724F},"
38 "{Yamaha,YMF740},"
39 "{Yamaha,YMF740C},"
40 "{Yamaha,YMF744},"
41 "{Yamaha,YMF754}}");
42
43static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
44static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
45static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
46static long fm_port[SNDRV_CARDS];
47static long mpu_port[SNDRV_CARDS];
48#ifdef SUPPORT_JOYSTICK
49static long joystick_port[SNDRV_CARDS];
50#endif
51static int rear_switch[SNDRV_CARDS];
52
53module_param_array(index, int, NULL, 0444);
54MODULE_PARM_DESC(index, "Index value for the Yamaha DS-XG PCI soundcard.");
55module_param_array(id, charp, NULL, 0444);
56MODULE_PARM_DESC(id, "ID string for the Yamaha DS-XG PCI soundcard.");
57module_param_array(enable, bool, NULL, 0444);
58MODULE_PARM_DESC(enable, "Enable Yamaha DS-XG soundcard.");
59module_param_array(mpu_port, long, NULL, 0444);
60MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
61module_param_array(fm_port, long, NULL, 0444);
62MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
63#ifdef SUPPORT_JOYSTICK
64module_param_array(joystick_port, long, NULL, 0444);
65MODULE_PARM_DESC(joystick_port, "Joystick port address");
66#endif
67module_param_array(rear_switch, bool, NULL, 0444);
68MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
69
70static struct pci_device_id snd_ymfpci_ids[] = {
71 { 0x1073, 0x0004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724 */
72 { 0x1073, 0x000d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724F */
73 { 0x1073, 0x000a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740 */
74 { 0x1073, 0x000c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740C */
75 { 0x1073, 0x0010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF744 */
76 { 0x1073, 0x0012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF754 */
77 { 0, }
78};
79
80MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
81
82#ifdef SUPPORT_JOYSTICK
Takashi Iwai208a1b42005-11-17 14:53:41 +010083static int __devinit snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int legacy_ctrl, int legacy_ctrl2)
85{
86 struct gameport *gp;
87 struct resource *r = NULL;
88 int io_port = joystick_port[dev];
89
90 if (!io_port)
91 return -ENODEV;
92
93 if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
94
95 if (io_port == 1) {
96 /* auto-detect */
97 if (!(io_port = pci_resource_start(chip->pci, 2)))
98 return -ENODEV;
99 }
100 } else {
101 if (io_port == 1) {
102 /* auto-detect */
103 for (io_port = 0x201; io_port <= 0x205; io_port++) {
104 if (io_port == 0x203)
105 continue;
106 if ((r = request_region(io_port, 1, "YMFPCI gameport")) != NULL)
107 break;
108 }
109 if (!r) {
110 printk(KERN_ERR "ymfpci: no gameport ports available\n");
111 return -EBUSY;
112 }
113 }
114 switch (io_port) {
115 case 0x201: legacy_ctrl2 |= 0 << 6; break;
116 case 0x202: legacy_ctrl2 |= 1 << 6; break;
117 case 0x204: legacy_ctrl2 |= 2 << 6; break;
118 case 0x205: legacy_ctrl2 |= 3 << 6; break;
119 default:
120 printk(KERN_ERR "ymfpci: invalid joystick port %#x", io_port);
121 return -EINVAL;
122 }
123 }
124
125 if (!r && !(r = request_region(io_port, 1, "YMFPCI gameport"))) {
126 printk(KERN_ERR "ymfpci: joystick port %#x is in use.\n", io_port);
127 return -EBUSY;
128 }
129
130 chip->gameport = gp = gameport_allocate_port();
131 if (!gp) {
132 printk(KERN_ERR "ymfpci: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +0200133 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENOMEM;
135 }
136
137
138 gameport_set_name(gp, "Yamaha YMF Gameport");
139 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
140 gameport_set_dev_parent(gp, &chip->pci->dev);
141 gp->io = io_port;
142 gameport_set_port_data(gp, r);
143
144 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
145 pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
146
147 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
148 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
149
150 gameport_register_port(chip->gameport);
151
152 return 0;
153}
154
Takashi Iwai208a1b42005-11-17 14:53:41 +0100155void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 if (chip->gameport) {
158 struct resource *r = gameport_get_port_data(chip->gameport);
159
160 gameport_unregister_port(chip->gameport);
161 chip->gameport = NULL;
162
Takashi Iwaib1d57762005-10-10 11:56:31 +0200163 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165}
166#else
Takashi Iwai208a1b42005-11-17 14:53:41 +0100167static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
168void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#endif /* SUPPORT_JOYSTICK */
170
171static int __devinit snd_card_ymfpci_probe(struct pci_dev *pci,
172 const struct pci_device_id *pci_id)
173{
174 static int dev;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100175 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct resource *fm_res = NULL;
177 struct resource *mpu_res = NULL;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100178 struct snd_ymfpci *chip;
179 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 char *str;
181 int err;
182 u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
183
184 if (dev >= SNDRV_CARDS)
185 return -ENODEV;
186 if (!enable[dev]) {
187 dev++;
188 return -ENOENT;
189 }
190
191 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
192 if (card == NULL)
193 return -ENOMEM;
194
195 switch (pci_id->device) {
196 case 0x0004: str = "YMF724"; break;
197 case 0x000d: str = "YMF724F"; break;
198 case 0x000a: str = "YMF740"; break;
199 case 0x000c: str = "YMF740C"; break;
200 case 0x0010: str = "YMF744"; break;
201 case 0x0012: str = "YMF754"; break;
202 default: str = "???"; break;
203 }
204
205 legacy_ctrl = 0;
206 legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
207
208 if (pci_id->device >= 0x0010) { /* YMF 744/754 */
209 if (fm_port[dev] == 1) {
210 /* auto-detect */
211 fm_port[dev] = pci_resource_start(pci, 1);
212 }
213 if (fm_port[dev] > 0 &&
214 (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
215 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
216 pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
217 }
218 if (mpu_port[dev] == 1) {
219 /* auto-detect */
220 mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
221 }
222 if (mpu_port[dev] > 0 &&
223 (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
224 legacy_ctrl |= YMFPCI_LEGACY_MEN;
225 pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
226 }
227 } else {
228 switch (fm_port[dev]) {
229 case 0x388: legacy_ctrl2 |= 0; break;
230 case 0x398: legacy_ctrl2 |= 1; break;
231 case 0x3a0: legacy_ctrl2 |= 2; break;
232 case 0x3a8: legacy_ctrl2 |= 3; break;
233 default: fm_port[dev] = 0; break;
234 }
235 if (fm_port[dev] > 0 &&
236 (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
237 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
238 } else {
239 legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
240 fm_port[dev] = 0;
241 }
242 switch (mpu_port[dev]) {
243 case 0x330: legacy_ctrl2 |= 0 << 4; break;
244 case 0x300: legacy_ctrl2 |= 1 << 4; break;
245 case 0x332: legacy_ctrl2 |= 2 << 4; break;
246 case 0x334: legacy_ctrl2 |= 3 << 4; break;
247 default: mpu_port[dev] = 0; break;
248 }
249 if (mpu_port[dev] > 0 &&
250 (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
251 legacy_ctrl |= YMFPCI_LEGACY_MEN;
252 } else {
253 legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
254 mpu_port[dev] = 0;
255 }
256 }
257 if (mpu_res) {
258 legacy_ctrl |= YMFPCI_LEGACY_MIEN;
259 legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
260 }
261 pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
262 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
263 pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
264 if ((err = snd_ymfpci_create(card, pci,
265 old_legacy_ctrl,
266 &chip)) < 0) {
267 snd_card_free(card);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200268 release_and_free_resource(mpu_res);
269 release_and_free_resource(fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return err;
271 }
272 chip->fm_res = fm_res;
273 chip->mpu_res = mpu_res;
Takashi Iwaided46232005-11-17 16:09:43 +0100274 card->private_data = chip;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 strcpy(card->driver, str);
277 sprintf(card->shortname, "Yamaha DS-XG (%s)", str);
278 sprintf(card->longname, "%s at 0x%lx, irq %i",
279 card->shortname,
280 chip->reg_area_phys,
281 chip->irq);
282 if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
283 snd_card_free(card);
284 return err;
285 }
286 if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
287 snd_card_free(card);
288 return err;
289 }
290 if ((err = snd_ymfpci_pcm_4ch(chip, 2, NULL)) < 0) {
291 snd_card_free(card);
292 return err;
293 }
294 if ((err = snd_ymfpci_pcm2(chip, 3, NULL)) < 0) {
295 snd_card_free(card);
296 return err;
297 }
298 if ((err = snd_ymfpci_mixer(chip, rear_switch[dev])) < 0) {
299 snd_card_free(card);
300 return err;
301 }
302 if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
303 snd_card_free(card);
304 return err;
305 }
306 if (chip->mpu_res) {
307 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
308 mpu_port[dev], 1,
309 pci->irq, 0, &chip->rawmidi)) < 0) {
310 printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
311 legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
312 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
313 }
314 }
315 if (chip->fm_res) {
316 if ((err = snd_opl3_create(card,
317 fm_port[dev],
318 fm_port[dev] + 2,
319 OPL3_HW_OPL3, 1, &opl3)) < 0) {
320 printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
321 legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
322 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
323 } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
324 snd_card_free(card);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200325 snd_printk(KERN_ERR "cannot create opl3 hwdep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return err;
327 }
328 }
329
330 snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
331
332 if ((err = snd_card_register(card)) < 0) {
333 snd_card_free(card);
334 return err;
335 }
336 pci_set_drvdata(pci, card);
337 dev++;
338 return 0;
339}
340
341static void __devexit snd_card_ymfpci_remove(struct pci_dev *pci)
342{
343 snd_card_free(pci_get_drvdata(pci));
344 pci_set_drvdata(pci, NULL);
345}
346
347static struct pci_driver driver = {
348 .name = "Yamaha DS-XG PCI",
349 .id_table = snd_ymfpci_ids,
350 .probe = snd_card_ymfpci_probe,
351 .remove = __devexit_p(snd_card_ymfpci_remove),
Takashi Iwaided46232005-11-17 16:09:43 +0100352#ifdef CONFIG_PM
353 .suspend = snd_ymfpci_suspend,
354 .resume = snd_ymfpci_resume,
355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356};
357
358static int __init alsa_card_ymfpci_init(void)
359{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200360 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363static void __exit alsa_card_ymfpci_exit(void)
364{
365 pci_unregister_driver(&driver);
366}
367
368module_init(alsa_card_ymfpci_init)
369module_exit(alsa_card_ymfpci_exit)