1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * The driver for the Yamaha's DS1/DS1E cards
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/init.h>
9 #include <linux/time.h>
10 #include <linux/module.h>
11 #include <sound/core.h>
13 #include <sound/mpu401.h>
14 #include <sound/opl3.h>
15 #include <sound/initval.h>
17 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
18 MODULE_DESCRIPTION("Yamaha DS-1 PCI");
19 MODULE_LICENSE("GPL");
20 MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF724},"
27 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-MAX */
28 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
29 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
; /* Enable this card */
30 static long fm_port
[SNDRV_CARDS
];
31 static long mpu_port
[SNDRV_CARDS
];
32 #ifdef SUPPORT_JOYSTICK
33 static long joystick_port
[SNDRV_CARDS
];
35 static bool rear_switch
[SNDRV_CARDS
];
37 module_param_array(index
, int, NULL
, 0444);
38 MODULE_PARM_DESC(index
, "Index value for the Yamaha DS-1 PCI soundcard.");
39 module_param_array(id
, charp
, NULL
, 0444);
40 MODULE_PARM_DESC(id
, "ID string for the Yamaha DS-1 PCI soundcard.");
41 module_param_array(enable
, bool, NULL
, 0444);
42 MODULE_PARM_DESC(enable
, "Enable Yamaha DS-1 soundcard.");
43 module_param_hw_array(mpu_port
, long, ioport
, NULL
, 0444);
44 MODULE_PARM_DESC(mpu_port
, "MPU-401 Port.");
45 module_param_hw_array(fm_port
, long, ioport
, NULL
, 0444);
46 MODULE_PARM_DESC(fm_port
, "FM OPL-3 Port.");
47 #ifdef SUPPORT_JOYSTICK
48 module_param_hw_array(joystick_port
, long, ioport
, NULL
, 0444);
49 MODULE_PARM_DESC(joystick_port
, "Joystick port address");
51 module_param_array(rear_switch
, bool, NULL
, 0444);
52 MODULE_PARM_DESC(rear_switch
, "Enable shared rear/line-in switch");
54 static const struct pci_device_id snd_ymfpci_ids
[] = {
55 { PCI_VDEVICE(YAMAHA
, 0x0004), 0, }, /* YMF724 */
56 { PCI_VDEVICE(YAMAHA
, 0x000d), 0, }, /* YMF724F */
57 { PCI_VDEVICE(YAMAHA
, 0x000a), 0, }, /* YMF740 */
58 { PCI_VDEVICE(YAMAHA
, 0x000c), 0, }, /* YMF740C */
59 { PCI_VDEVICE(YAMAHA
, 0x0010), 0, }, /* YMF744 */
60 { PCI_VDEVICE(YAMAHA
, 0x0012), 0, }, /* YMF754 */
64 MODULE_DEVICE_TABLE(pci
, snd_ymfpci_ids
);
66 #ifdef SUPPORT_JOYSTICK
67 static int snd_ymfpci_create_gameport(struct snd_ymfpci
*chip
, int dev
,
68 int legacy_ctrl
, int legacy_ctrl2
)
71 struct resource
*r
= NULL
;
72 int io_port
= joystick_port
[dev
];
77 if (chip
->pci
->device
>= 0x0010) { /* YMF 744/754 */
81 if (!(io_port
= pci_resource_start(chip
->pci
, 2)))
87 for (io_port
= 0x201; io_port
<= 0x205; io_port
++) {
90 if ((r
= request_region(io_port
, 1, "YMFPCI gameport")) != NULL
)
94 dev_err(chip
->card
->dev
,
95 "no gameport ports available\n");
100 case 0x201: legacy_ctrl2
|= 0 << 6; break;
101 case 0x202: legacy_ctrl2
|= 1 << 6; break;
102 case 0x204: legacy_ctrl2
|= 2 << 6; break;
103 case 0x205: legacy_ctrl2
|= 3 << 6; break;
105 dev_err(chip
->card
->dev
,
106 "invalid joystick port %#x", io_port
);
111 if (!r
&& !(r
= request_region(io_port
, 1, "YMFPCI gameport"))) {
112 dev_err(chip
->card
->dev
,
113 "joystick port %#x is in use.\n", io_port
);
117 chip
->gameport
= gp
= gameport_allocate_port();
119 dev_err(chip
->card
->dev
,
120 "cannot allocate memory for gameport\n");
121 release_and_free_resource(r
);
126 gameport_set_name(gp
, "Yamaha YMF Gameport");
127 gameport_set_phys(gp
, "pci%s/gameport0", pci_name(chip
->pci
));
128 gameport_set_dev_parent(gp
, &chip
->pci
->dev
);
130 gameport_set_port_data(gp
, r
);
132 if (chip
->pci
->device
>= 0x0010) /* YMF 744/754 */
133 pci_write_config_word(chip
->pci
, PCIR_DSXG_JOYBASE
, io_port
);
135 pci_write_config_word(chip
->pci
, PCIR_DSXG_LEGACY
, legacy_ctrl
| YMFPCI_LEGACY_JPEN
);
136 pci_write_config_word(chip
->pci
, PCIR_DSXG_ELEGACY
, legacy_ctrl2
);
138 gameport_register_port(chip
->gameport
);
143 void snd_ymfpci_free_gameport(struct snd_ymfpci
*chip
)
145 if (chip
->gameport
) {
146 struct resource
*r
= gameport_get_port_data(chip
->gameport
);
148 gameport_unregister_port(chip
->gameport
);
149 chip
->gameport
= NULL
;
151 release_and_free_resource(r
);
155 static inline int snd_ymfpci_create_gameport(struct snd_ymfpci
*chip
, int dev
, int l
, int l2
) { return -ENOSYS
; }
156 void snd_ymfpci_free_gameport(struct snd_ymfpci
*chip
) { }
157 #endif /* SUPPORT_JOYSTICK */
159 static int snd_card_ymfpci_probe(struct pci_dev
*pci
,
160 const struct pci_device_id
*pci_id
)
163 struct snd_card
*card
;
164 struct resource
*fm_res
= NULL
;
165 struct resource
*mpu_res
= NULL
;
166 struct snd_ymfpci
*chip
;
167 struct snd_opl3
*opl3
;
168 const char *str
, *model
;
170 u16 legacy_ctrl
, legacy_ctrl2
, old_legacy_ctrl
;
172 if (dev
>= SNDRV_CARDS
)
179 err
= snd_card_new(&pci
->dev
, index
[dev
], id
[dev
], THIS_MODULE
,
184 switch (pci_id
->device
) {
185 case 0x0004: str
= "YMF724"; model
= "DS-1"; break;
186 case 0x000d: str
= "YMF724F"; model
= "DS-1"; break;
187 case 0x000a: str
= "YMF740"; model
= "DS-1L"; break;
188 case 0x000c: str
= "YMF740C"; model
= "DS-1L"; break;
189 case 0x0010: str
= "YMF744"; model
= "DS-1S"; break;
190 case 0x0012: str
= "YMF754"; model
= "DS-1E"; break;
191 default: model
= str
= "???"; break;
195 legacy_ctrl2
= 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
197 if (pci_id
->device
>= 0x0010) { /* YMF 744/754 */
198 if (fm_port
[dev
] == 1) {
200 fm_port
[dev
] = pci_resource_start(pci
, 1);
202 if (fm_port
[dev
] > 0 &&
203 (fm_res
= request_region(fm_port
[dev
], 4, "YMFPCI OPL3")) != NULL
) {
204 legacy_ctrl
|= YMFPCI_LEGACY_FMEN
;
205 pci_write_config_word(pci
, PCIR_DSXG_FMBASE
, fm_port
[dev
]);
207 if (mpu_port
[dev
] == 1) {
209 mpu_port
[dev
] = pci_resource_start(pci
, 1) + 0x20;
211 if (mpu_port
[dev
] > 0 &&
212 (mpu_res
= request_region(mpu_port
[dev
], 2, "YMFPCI MPU401")) != NULL
) {
213 legacy_ctrl
|= YMFPCI_LEGACY_MEN
;
214 pci_write_config_word(pci
, PCIR_DSXG_MPU401BASE
, mpu_port
[dev
]);
217 switch (fm_port
[dev
]) {
218 case 0x388: legacy_ctrl2
|= 0; break;
219 case 0x398: legacy_ctrl2
|= 1; break;
220 case 0x3a0: legacy_ctrl2
|= 2; break;
221 case 0x3a8: legacy_ctrl2
|= 3; break;
222 default: fm_port
[dev
] = 0; break;
224 if (fm_port
[dev
] > 0 &&
225 (fm_res
= request_region(fm_port
[dev
], 4, "YMFPCI OPL3")) != NULL
) {
226 legacy_ctrl
|= YMFPCI_LEGACY_FMEN
;
228 legacy_ctrl2
&= ~YMFPCI_LEGACY2_FMIO
;
231 switch (mpu_port
[dev
]) {
232 case 0x330: legacy_ctrl2
|= 0 << 4; break;
233 case 0x300: legacy_ctrl2
|= 1 << 4; break;
234 case 0x332: legacy_ctrl2
|= 2 << 4; break;
235 case 0x334: legacy_ctrl2
|= 3 << 4; break;
236 default: mpu_port
[dev
] = 0; break;
238 if (mpu_port
[dev
] > 0 &&
239 (mpu_res
= request_region(mpu_port
[dev
], 2, "YMFPCI MPU401")) != NULL
) {
240 legacy_ctrl
|= YMFPCI_LEGACY_MEN
;
242 legacy_ctrl2
&= ~YMFPCI_LEGACY2_MPUIO
;
247 legacy_ctrl
|= YMFPCI_LEGACY_MIEN
;
248 legacy_ctrl2
|= YMFPCI_LEGACY2_IMOD
;
250 pci_read_config_word(pci
, PCIR_DSXG_LEGACY
, &old_legacy_ctrl
);
251 pci_write_config_word(pci
, PCIR_DSXG_LEGACY
, legacy_ctrl
);
252 pci_write_config_word(pci
, PCIR_DSXG_ELEGACY
, legacy_ctrl2
);
253 if ((err
= snd_ymfpci_create(card
, pci
,
256 release_and_free_resource(mpu_res
);
257 release_and_free_resource(fm_res
);
260 chip
->fm_res
= fm_res
;
261 chip
->mpu_res
= mpu_res
;
262 card
->private_data
= chip
;
264 strcpy(card
->driver
, str
);
265 sprintf(card
->shortname
, "Yamaha %s (%s)", model
, str
);
266 sprintf(card
->longname
, "%s at 0x%lx, irq %i",
270 err
= snd_ymfpci_pcm(chip
, 0);
274 err
= snd_ymfpci_pcm_spdif(chip
, 1);
278 err
= snd_ymfpci_mixer(chip
, rear_switch
[dev
]);
282 if (chip
->ac97
->ext_id
& AC97_EI_SDAC
) {
283 err
= snd_ymfpci_pcm_4ch(chip
, 2);
287 err
= snd_ymfpci_pcm2(chip
, 3);
291 err
= snd_ymfpci_timer(chip
, 0);
296 if ((err
= snd_mpu401_uart_new(card
, 0, MPU401_HW_YMFPCI
,
298 MPU401_INFO_INTEGRATED
|
299 MPU401_INFO_IRQ_HOOK
,
300 -1, &chip
->rawmidi
)) < 0) {
302 "cannot initialize MPU401 at 0x%lx, skipping...\n",
304 legacy_ctrl
&= ~YMFPCI_LEGACY_MIEN
; /* disable MPU401 irq */
305 pci_write_config_word(pci
, PCIR_DSXG_LEGACY
, legacy_ctrl
);
309 if ((err
= snd_opl3_create(card
,
312 OPL3_HW_OPL3
, 1, &opl3
)) < 0) {
314 "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
316 legacy_ctrl
&= ~YMFPCI_LEGACY_FMEN
;
317 pci_write_config_word(pci
, PCIR_DSXG_LEGACY
, legacy_ctrl
);
318 } else if ((err
= snd_opl3_hwdep_new(opl3
, 0, 1, NULL
)) < 0) {
319 dev_err(card
->dev
, "cannot create opl3 hwdep\n");
324 snd_ymfpci_create_gameport(chip
, dev
, legacy_ctrl
, legacy_ctrl2
);
326 err
= snd_card_register(card
);
330 pci_set_drvdata(pci
, card
);
339 static void snd_card_ymfpci_remove(struct pci_dev
*pci
)
341 snd_card_free(pci_get_drvdata(pci
));
344 static struct pci_driver ymfpci_driver
= {
345 .name
= KBUILD_MODNAME
,
346 .id_table
= snd_ymfpci_ids
,
347 .probe
= snd_card_ymfpci_probe
,
348 .remove
= snd_card_ymfpci_remove
,
349 #ifdef CONFIG_PM_SLEEP
351 .pm
= &snd_ymfpci_pm
,
356 module_pci_driver(ymfpci_driver
);