1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for generic ESS AudioDrive ESx688 soundcards
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/init.h>
10 #include <linux/isapnp.h>
11 #include <linux/time.h>
12 #include <linux/wait.h>
13 #include <linux/module.h>
15 #include <sound/core.h>
16 #include <sound/es1688.h>
17 #include <sound/mpu401.h>
18 #include <sound/opl3.h>
19 #define SNDRV_LEGACY_FIND_FREE_IRQ
20 #define SNDRV_LEGACY_FIND_FREE_DMA
21 #include <sound/initval.h>
23 #define CRD_NAME "Generic ESS ES1688/ES688 AudioDrive"
24 #define DEV_NAME "es1688"
26 MODULE_DESCRIPTION(CRD_NAME);
27 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
28 MODULE_LICENSE("GPL");
29 MODULE_ALIAS("snd_es968");
31 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
32 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
34 static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
36 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
37 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
38 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* Usually 0x388 */
39 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
40 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
41 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
42 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
44 module_param_array(index, int, NULL, 0444);
45 MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
46 module_param_array(id, charp, NULL, 0444);
47 MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
48 module_param_array(enable, bool, NULL, 0444);
50 module_param_array(isapnp, bool, NULL, 0444);
51 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
53 MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
54 module_param_hw_array(port, long, ioport, NULL, 0444);
55 MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
56 module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
57 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
58 module_param_hw_array(irq, int, irq, NULL, 0444);
59 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
60 MODULE_PARM_DESC(fm_port, "FM port # for ES1688 driver.");
61 MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
62 module_param_hw_array(mpu_irq, int, irq, NULL, 0444);
63 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
64 module_param_hw_array(dma8, int, dma, NULL, 0444);
65 MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver.");
68 #define is_isapnp_selected(dev) isapnp[dev]
70 #define is_isapnp_selected(dev) 0
73 static int snd_es1688_match(struct device *dev, unsigned int n)
75 return enable[n] && !is_isapnp_selected(n);
78 static int snd_es1688_legacy_create(struct snd_card *card,
79 struct device *dev, unsigned int n)
81 struct snd_es1688 *chip = card->private_data;
82 static const long possible_ports[] = {0x220, 0x240, 0x260};
83 static const int possible_irqs[] = {5, 9, 10, 7, -1};
84 static const int possible_dmas[] = {1, 3, 0, -1};
88 if (irq[n] == SNDRV_AUTO_IRQ) {
89 irq[n] = snd_legacy_find_free_irq(possible_irqs);
91 dev_err(dev, "unable to find a free IRQ\n");
95 if (dma8[n] == SNDRV_AUTO_DMA) {
96 dma8[n] = snd_legacy_find_free_dma(possible_dmas);
98 dev_err(dev, "unable to find a free DMA\n");
103 if (port[n] != SNDRV_AUTO_PORT)
104 return snd_es1688_create(card, chip, port[n], mpu_port[n],
105 irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
109 port[n] = possible_ports[i];
110 error = snd_es1688_create(card, chip, port[n], mpu_port[n],
111 irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
112 } while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
117 static int snd_es1688_probe(struct snd_card *card, unsigned int n)
119 struct snd_es1688 *chip = card->private_data;
120 struct snd_opl3 *opl3;
123 error = snd_es1688_pcm(card, chip, 0);
127 error = snd_es1688_mixer(card, chip);
131 strscpy(card->driver, "ES1688", sizeof(card->driver));
132 strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname));
133 snprintf(card->longname, sizeof(card->longname),
134 "%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port,
135 chip->irq, chip->dma8);
137 if (fm_port[n] == SNDRV_AUTO_PORT)
138 fm_port[n] = port[n]; /* share the same port */
140 if (fm_port[n] > 0) {
141 if (snd_opl3_create(card, fm_port[n], fm_port[n] + 2,
142 OPL3_HW_OPL3, 0, &opl3) < 0)
144 "opl3 not detected at 0x%lx\n", fm_port[n]);
146 error = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
152 if (mpu_irq[n] >= 0 && mpu_irq[n] != SNDRV_AUTO_IRQ &&
153 chip->mpu_port > 0) {
154 error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
161 return snd_card_register(card);
164 static int snd_es1688_isa_probe(struct device *dev, unsigned int n)
166 struct snd_card *card;
169 error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE,
170 sizeof(struct snd_es1688), &card);
174 error = snd_es1688_legacy_create(card, dev, n);
178 error = snd_es1688_probe(card, n);
182 dev_set_drvdata(dev, card);
187 static struct isa_driver snd_es1688_driver = {
188 .match = snd_es1688_match,
189 .probe = snd_es1688_isa_probe,
191 .suspend = snd_es1688_suspend,
192 .resume = snd_es1688_resume,
199 static int snd_es968_pnp_is_probed;
202 static int snd_card_es968_pnp(struct snd_card *card, unsigned int n,
203 struct pnp_card_link *pcard,
204 const struct pnp_card_device_id *pid)
206 struct snd_es1688 *chip = card->private_data;
207 struct pnp_dev *pdev;
210 pdev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
214 error = pnp_activate_dev(pdev);
216 snd_printk(KERN_ERR "ES968 pnp configure failure\n");
219 port[n] = pnp_port_start(pdev, 0);
220 dma8[n] = pnp_dma(pdev, 0);
221 irq[n] = pnp_irq(pdev, 0);
223 return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n],
224 mpu_irq[n], dma8[n], ES1688_HW_AUTO);
227 static int snd_es968_pnp_detect(struct pnp_card_link *pcard,
228 const struct pnp_card_device_id *pid)
230 struct snd_card *card;
231 static unsigned int dev;
234 if (snd_es968_pnp_is_probed)
236 for ( ; dev < SNDRV_CARDS; dev++) {
237 if (enable[dev] && isapnp[dev])
240 if (dev == SNDRV_CARDS)
243 error = snd_devm_card_new(&pcard->card->dev,
244 index[dev], id[dev], THIS_MODULE,
245 sizeof(struct snd_es1688), &card);
249 error = snd_card_es968_pnp(card, dev, pcard, pid);
252 error = snd_es1688_probe(card, dev);
255 pnp_set_card_drvdata(pcard, card);
256 snd_es968_pnp_is_probed = 1;
260 static void snd_es968_pnp_remove(struct pnp_card_link *pcard)
262 snd_es968_pnp_is_probed = 0;
266 static int snd_es968_pnp_suspend(struct pnp_card_link *pcard,
269 struct snd_card *card = pnp_get_card_drvdata(pcard);
271 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
275 static int snd_es968_pnp_resume(struct pnp_card_link *pcard)
277 struct snd_card *card = pnp_get_card_drvdata(pcard);
278 struct snd_es1688 *chip = card->private_data;
280 snd_es1688_reset(chip);
281 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
286 static const struct pnp_card_device_id snd_es968_pnpids[] = {
287 { .id = "ESS0968", .devs = { { "@@@0968" }, } },
288 { .id = "ESS0968", .devs = { { "ESS0968" }, } },
289 { .id = "", } /* end */
292 MODULE_DEVICE_TABLE(pnp_card, snd_es968_pnpids);
294 static struct pnp_card_driver es968_pnpc_driver = {
295 .flags = PNP_DRIVER_RES_DISABLE,
296 .name = DEV_NAME " PnP",
297 .id_table = snd_es968_pnpids,
298 .probe = snd_es968_pnp_detect,
299 .remove = snd_es968_pnp_remove,
301 .suspend = snd_es968_pnp_suspend,
302 .resume = snd_es968_pnp_resume,
307 static int __init alsa_card_es1688_init(void)
310 pnp_register_card_driver(&es968_pnpc_driver);
311 if (snd_es968_pnp_is_probed)
313 pnp_unregister_card_driver(&es968_pnpc_driver);
315 return isa_register_driver(&snd_es1688_driver, SNDRV_CARDS);
318 static void __exit alsa_card_es1688_exit(void)
320 if (!snd_es968_pnp_is_probed) {
321 isa_unregister_driver(&snd_es1688_driver);
325 pnp_unregister_card_driver(&es968_pnpc_driver);
329 module_init(alsa_card_es1688_init);
330 module_exit(alsa_card_es1688_exit);