blob: 65ebf5f1933a4639b4f3b0cd870516e00ed3557c [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>");
Clemens Ladisch22fb2a72005-12-12 09:27:14 +010034MODULE_DESCRIPTION("Yamaha DS-1 PCI");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035MODULE_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];
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +010052static int rear_swap[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54module_param_array(index, int, NULL, 0444);
Clemens Ladisch22fb2a72005-12-12 09:27:14 +010055MODULE_PARM_DESC(index, "Index value for the Yamaha DS-1 PCI soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056module_param_array(id, charp, NULL, 0444);
Clemens Ladisch22fb2a72005-12-12 09:27:14 +010057MODULE_PARM_DESC(id, "ID string for the Yamaha DS-1 PCI soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058module_param_array(enable, bool, NULL, 0444);
Clemens Ladisch22fb2a72005-12-12 09:27:14 +010059MODULE_PARM_DESC(enable, "Enable Yamaha DS-1 soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_array(mpu_port, long, NULL, 0444);
61MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
62module_param_array(fm_port, long, NULL, 0444);
63MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
64#ifdef SUPPORT_JOYSTICK
65module_param_array(joystick_port, long, NULL, 0444);
66MODULE_PARM_DESC(joystick_port, "Joystick port address");
67#endif
68module_param_array(rear_switch, bool, NULL, 0444);
69MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +010070module_param_array(rear_swap, bool, NULL, 0444);
71MODULE_PARM_DESC(rear_swap, "Swap rear channels (must be enabled for correct IEC958 (S/PDIF)) output");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Henrik Kretzschmar396c9b92006-04-24 15:59:04 +020073static struct pci_device_id snd_ymfpci_ids[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 { 0x1073, 0x0004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724 */
75 { 0x1073, 0x000d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724F */
76 { 0x1073, 0x000a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740 */
77 { 0x1073, 0x000c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740C */
78 { 0x1073, 0x0010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF744 */
79 { 0x1073, 0x0012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF754 */
80 { 0, }
81};
82
83MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
84
85#ifdef SUPPORT_JOYSTICK
Takashi Iwai208a1b42005-11-17 14:53:41 +010086static int __devinit snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int legacy_ctrl, int legacy_ctrl2)
88{
89 struct gameport *gp;
90 struct resource *r = NULL;
91 int io_port = joystick_port[dev];
92
93 if (!io_port)
94 return -ENODEV;
95
96 if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
97
98 if (io_port == 1) {
99 /* auto-detect */
100 if (!(io_port = pci_resource_start(chip->pci, 2)))
101 return -ENODEV;
102 }
103 } else {
104 if (io_port == 1) {
105 /* auto-detect */
106 for (io_port = 0x201; io_port <= 0x205; io_port++) {
107 if (io_port == 0x203)
108 continue;
109 if ((r = request_region(io_port, 1, "YMFPCI gameport")) != NULL)
110 break;
111 }
112 if (!r) {
113 printk(KERN_ERR "ymfpci: no gameport ports available\n");
114 return -EBUSY;
115 }
116 }
117 switch (io_port) {
118 case 0x201: legacy_ctrl2 |= 0 << 6; break;
119 case 0x202: legacy_ctrl2 |= 1 << 6; break;
120 case 0x204: legacy_ctrl2 |= 2 << 6; break;
121 case 0x205: legacy_ctrl2 |= 3 << 6; break;
122 default:
123 printk(KERN_ERR "ymfpci: invalid joystick port %#x", io_port);
124 return -EINVAL;
125 }
126 }
127
128 if (!r && !(r = request_region(io_port, 1, "YMFPCI gameport"))) {
129 printk(KERN_ERR "ymfpci: joystick port %#x is in use.\n", io_port);
130 return -EBUSY;
131 }
132
133 chip->gameport = gp = gameport_allocate_port();
134 if (!gp) {
135 printk(KERN_ERR "ymfpci: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +0200136 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return -ENOMEM;
138 }
139
140
141 gameport_set_name(gp, "Yamaha YMF Gameport");
142 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
143 gameport_set_dev_parent(gp, &chip->pci->dev);
144 gp->io = io_port;
145 gameport_set_port_data(gp, r);
146
147 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
148 pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
149
150 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
151 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
152
153 gameport_register_port(chip->gameport);
154
155 return 0;
156}
157
Takashi Iwai208a1b42005-11-17 14:53:41 +0100158void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 if (chip->gameport) {
161 struct resource *r = gameport_get_port_data(chip->gameport);
162
163 gameport_unregister_port(chip->gameport);
164 chip->gameport = NULL;
165
Takashi Iwaib1d57762005-10-10 11:56:31 +0200166 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168}
169#else
Takashi Iwai208a1b42005-11-17 14:53:41 +0100170static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
171void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif /* SUPPORT_JOYSTICK */
173
174static int __devinit snd_card_ymfpci_probe(struct pci_dev *pci,
175 const struct pci_device_id *pci_id)
176{
177 static int dev;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100178 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct resource *fm_res = NULL;
180 struct resource *mpu_res = NULL;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100181 struct snd_ymfpci *chip;
182 struct snd_opl3 *opl3;
Clemens Ladisch22fb2a72005-12-12 09:27:14 +0100183 const char *str, *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int err;
185 u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
186
187 if (dev >= SNDRV_CARDS)
188 return -ENODEV;
189 if (!enable[dev]) {
190 dev++;
191 return -ENOENT;
192 }
193
194 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
195 if (card == NULL)
196 return -ENOMEM;
197
198 switch (pci_id->device) {
Clemens Ladisch22fb2a72005-12-12 09:27:14 +0100199 case 0x0004: str = "YMF724"; model = "DS-1"; break;
200 case 0x000d: str = "YMF724F"; model = "DS-1"; break;
201 case 0x000a: str = "YMF740"; model = "DS-1L"; break;
202 case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
203 case 0x0010: str = "YMF744"; model = "DS-1S"; break;
204 case 0x0012: str = "YMF754"; model = "DS-1E"; break;
205 default: model = str = "???"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 legacy_ctrl = 0;
209 legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
210
211 if (pci_id->device >= 0x0010) { /* YMF 744/754 */
212 if (fm_port[dev] == 1) {
213 /* auto-detect */
214 fm_port[dev] = pci_resource_start(pci, 1);
215 }
216 if (fm_port[dev] > 0 &&
217 (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
218 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
219 pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
220 }
221 if (mpu_port[dev] == 1) {
222 /* auto-detect */
223 mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
224 }
225 if (mpu_port[dev] > 0 &&
226 (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
227 legacy_ctrl |= YMFPCI_LEGACY_MEN;
228 pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
229 }
230 } else {
231 switch (fm_port[dev]) {
232 case 0x388: legacy_ctrl2 |= 0; break;
233 case 0x398: legacy_ctrl2 |= 1; break;
234 case 0x3a0: legacy_ctrl2 |= 2; break;
235 case 0x3a8: legacy_ctrl2 |= 3; break;
236 default: fm_port[dev] = 0; break;
237 }
238 if (fm_port[dev] > 0 &&
239 (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
240 legacy_ctrl |= YMFPCI_LEGACY_FMEN;
241 } else {
242 legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
243 fm_port[dev] = 0;
244 }
245 switch (mpu_port[dev]) {
246 case 0x330: legacy_ctrl2 |= 0 << 4; break;
247 case 0x300: legacy_ctrl2 |= 1 << 4; break;
248 case 0x332: legacy_ctrl2 |= 2 << 4; break;
249 case 0x334: legacy_ctrl2 |= 3 << 4; break;
250 default: mpu_port[dev] = 0; break;
251 }
252 if (mpu_port[dev] > 0 &&
253 (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
254 legacy_ctrl |= YMFPCI_LEGACY_MEN;
255 } else {
256 legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
257 mpu_port[dev] = 0;
258 }
259 }
260 if (mpu_res) {
261 legacy_ctrl |= YMFPCI_LEGACY_MIEN;
262 legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
263 }
264 pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
265 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
266 pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
267 if ((err = snd_ymfpci_create(card, pci,
268 old_legacy_ctrl,
269 &chip)) < 0) {
270 snd_card_free(card);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200271 release_and_free_resource(mpu_res);
272 release_and_free_resource(fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return err;
274 }
275 chip->fm_res = fm_res;
276 chip->mpu_res = mpu_res;
Takashi Iwaided46232005-11-17 16:09:43 +0100277 card->private_data = chip;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 strcpy(card->driver, str);
Clemens Ladisch22fb2a72005-12-12 09:27:14 +0100280 sprintf(card->shortname, "Yamaha %s (%s)", model, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sprintf(card->longname, "%s at 0x%lx, irq %i",
282 card->shortname,
283 chip->reg_area_phys,
284 chip->irq);
285 if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
286 snd_card_free(card);
287 return err;
288 }
289 if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
290 snd_card_free(card);
291 return err;
292 }
293 if ((err = snd_ymfpci_pcm_4ch(chip, 2, NULL)) < 0) {
294 snd_card_free(card);
295 return err;
296 }
297 if ((err = snd_ymfpci_pcm2(chip, 3, NULL)) < 0) {
298 snd_card_free(card);
299 return err;
300 }
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100301 if ((err = snd_ymfpci_mixer(chip, rear_switch[dev], rear_swap[dev])) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 snd_card_free(card);
303 return err;
304 }
305 if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
306 snd_card_free(card);
307 return err;
308 }
309 if (chip->mpu_res) {
310 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
311 mpu_port[dev], 1,
312 pci->irq, 0, &chip->rawmidi)) < 0) {
313 printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
314 legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
315 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
316 }
317 }
318 if (chip->fm_res) {
319 if ((err = snd_opl3_create(card,
320 fm_port[dev],
321 fm_port[dev] + 2,
322 OPL3_HW_OPL3, 1, &opl3)) < 0) {
323 printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
324 legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
325 pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
326 } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
327 snd_card_free(card);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200328 snd_printk(KERN_ERR "cannot create opl3 hwdep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return err;
330 }
331 }
332
333 snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
334
335 if ((err = snd_card_register(card)) < 0) {
336 snd_card_free(card);
337 return err;
338 }
339 pci_set_drvdata(pci, card);
340 dev++;
341 return 0;
342}
343
344static void __devexit snd_card_ymfpci_remove(struct pci_dev *pci)
345{
346 snd_card_free(pci_get_drvdata(pci));
347 pci_set_drvdata(pci, NULL);
348}
349
350static struct pci_driver driver = {
Clemens Ladisch22fb2a72005-12-12 09:27:14 +0100351 .name = "Yamaha DS-1 PCI",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 .id_table = snd_ymfpci_ids,
353 .probe = snd_card_ymfpci_probe,
354 .remove = __devexit_p(snd_card_ymfpci_remove),
Takashi Iwaided46232005-11-17 16:09:43 +0100355#ifdef CONFIG_PM
356 .suspend = snd_ymfpci_suspend,
357 .resume = snd_ymfpci_resume,
358#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359};
360
361static int __init alsa_card_ymfpci_init(void)
362{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200363 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366static void __exit alsa_card_ymfpci_exit(void)
367{
368 pci_unregister_driver(&driver);
369}
370
371module_init(alsa_card_ymfpci_init)
372module_exit(alsa_card_ymfpci_exit)