blob: f935b56eeec7bfc1baceeb4285c49c3dadeb1c35 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Driver for generic ESS AudioDrive ESx688 soundcards
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/init.h>
Takashi Iwai2a571ed2005-11-17 17:13:30 +01008#include <linux/err.h>
Rene Hermanae596182007-02-14 13:26:17 +01009#include <linux/isa.h>
Krzysztof Helta20971b2010-05-10 09:47:32 +020010#include <linux/isapnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/time.h>
12#include <linux/wait.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040013#include <linux/module.h>
Takashi Iwai2a571ed2005-11-17 17:13:30 +010014#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <sound/core.h>
16#include <sound/es1688.h>
17#include <sound/mpu401.h>
18#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define SNDRV_LEGACY_FIND_FREE_IRQ
20#define SNDRV_LEGACY_FIND_FREE_DMA
21#include <sound/initval.h>
22
Rene Hermanae596182007-02-14 13:26:17 +010023#define CRD_NAME "Generic ESS ES1688/ES688 AudioDrive"
24#define DEV_NAME "es1688"
25
26MODULE_DESCRIPTION(CRD_NAME);
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020027MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070028MODULE_LICENSE("GPL");
Krzysztof Helta20971b2010-05-10 09:47:32 +020029MODULE_ALIAS("snd_es968");
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
32static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Krzysztof Helta20971b2010-05-10 09:47:32 +020033#ifdef CONFIG_PNP
Rusty Russella67ff6a2011-12-15 13:49:36 +103034static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
Krzysztof Helta20971b2010-05-10 09:47:32 +020035#endif
Rusty Russella67ff6a2011-12-15 13:49:36 +103036static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
Krzysztof Helt880abd42009-01-30 19:20:29 +010038static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* Usually 0x388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
40static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
41static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
42static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
43
44module_param_array(index, int, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010045MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046module_param_array(id, charp, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010047MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048module_param_array(enable, bool, NULL, 0444);
Krzysztof Helta20971b2010-05-10 09:47:32 +020049#ifdef CONFIG_PNP
50module_param_array(isapnp, bool, NULL, 0444);
51MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
52#endif
Rene Hermanae596182007-02-14 13:26:17 +010053MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
David Howellse992ef52017-04-04 16:54:30 +010054module_param_hw_array(port, long, ioport, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010055MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
David Howellse992ef52017-04-04 16:54:30 +010056module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010057MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
David Howellse992ef52017-04-04 16:54:30 +010058module_param_hw_array(irq, int, irq, NULL, 0444);
59module_param_hw_array(fm_port, long, ioport, NULL, 0444);
Krzysztof Helt880abd42009-01-30 19:20:29 +010060MODULE_PARM_DESC(fm_port, "FM port # for ES1688 driver.");
Rene Hermanae596182007-02-14 13:26:17 +010061MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
David Howellse992ef52017-04-04 16:54:30 +010062module_param_hw_array(mpu_irq, int, irq, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010063MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
David Howellse992ef52017-04-04 16:54:30 +010064module_param_hw_array(dma8, int, dma, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010065MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Krzysztof Helta20971b2010-05-10 09:47:32 +020067#ifdef CONFIG_PNP
68#define is_isapnp_selected(dev) isapnp[dev]
69#else
70#define is_isapnp_selected(dev) 0
71#endif
72
Bill Pemberton1bff2922012-12-06 12:35:21 -050073static int snd_es1688_match(struct device *dev, unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Krzysztof Helta20971b2010-05-10 09:47:32 +020075 return enable[n] && !is_isapnp_selected(n);
Rene Hermanae596182007-02-14 13:26:17 +010076}
77
Bill Pemberton1bff2922012-12-06 12:35:21 -050078static int snd_es1688_legacy_create(struct snd_card *card,
79 struct device *dev, unsigned int n)
Rene Hermanae596182007-02-14 13:26:17 +010080{
Krzysztof Helta20971b2010-05-10 09:47:32 +020081 struct snd_es1688 *chip = card->private_data;
Takashi Iwai748f5182020-01-05 15:48:02 +010082 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};
Rene Hermanae596182007-02-14 13:26:17 +010085
Rene Herman019608b2007-02-19 13:01:45 +010086 int i, error;
87
88 if (irq[n] == SNDRV_AUTO_IRQ) {
89 irq[n] = snd_legacy_find_free_irq(possible_irqs);
90 if (irq[n] < 0) {
Takashi Iwai0418ff02008-11-03 08:51:33 +010091 dev_err(dev, "unable to find a free IRQ\n");
Rene Herman019608b2007-02-19 13:01:45 +010092 return -EBUSY;
93 }
94 }
95 if (dma8[n] == SNDRV_AUTO_DMA) {
96 dma8[n] = snd_legacy_find_free_dma(possible_dmas);
97 if (dma8[n] < 0) {
Takashi Iwai0418ff02008-11-03 08:51:33 +010098 dev_err(dev, "unable to find a free DMA\n");
Rene Herman019608b2007-02-19 13:01:45 +010099 return -EBUSY;
100 }
101 }
102
103 if (port[n] != SNDRV_AUTO_PORT)
Krzysztof Helt396fa822010-05-09 20:35:44 +0200104 return snd_es1688_create(card, chip, port[n], mpu_port[n],
105 irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
Rene Herman019608b2007-02-19 13:01:45 +0100106
107 i = 0;
108 do {
109 port[n] = possible_ports[i];
Krzysztof Helt396fa822010-05-09 20:35:44 +0200110 error = snd_es1688_create(card, chip, port[n], mpu_port[n],
111 irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
Rene Herman019608b2007-02-19 13:01:45 +0100112 } while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
113
114 return error;
115}
116
Bill Pemberton1bff2922012-12-06 12:35:21 -0500117static int snd_es1688_probe(struct snd_card *card, unsigned int n)
Rene Herman019608b2007-02-19 13:01:45 +0100118{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200119 struct snd_es1688 *chip = card->private_data;
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100120 struct snd_opl3 *opl3;
Rene Hermanae596182007-02-14 13:26:17 +0100121 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Lars-Peter Clausen4b8ab882015-01-02 12:24:37 +0100123 error = snd_es1688_pcm(card, chip, 0);
Takashi Iwaic95eadd2008-12-28 16:43:35 +0100124 if (error < 0)
125 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Krzysztof Helt396fa822010-05-09 20:35:44 +0200127 error = snd_es1688_mixer(card, chip);
Rene Hermanae596182007-02-14 13:26:17 +0100128 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200129 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Joe Perches75b1a8f2021-01-04 09:17:34 -0800131 strscpy(card->driver, "ES1688", sizeof(card->driver));
132 strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname));
Dan Carpenterb0fb75a2010-05-14 16:48:28 +0200133 snprintf(card->longname, sizeof(card->longname),
Lars-Peter Clausen4b8ab882015-01-02 12:24:37 +0100134 "%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port,
Dan Carpenterb0fb75a2010-05-14 16:48:28 +0200135 chip->irq, chip->dma8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Krzysztof Helt880abd42009-01-30 19:20:29 +0100137 if (fm_port[n] == SNDRV_AUTO_PORT)
138 fm_port[n] = port[n]; /* share the same port */
139
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)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200143 dev_warn(card->dev,
Krzysztof Helt880abd42009-01-30 19:20:29 +0100144 "opl3 not detected at 0x%lx\n", fm_port[n]);
145 else {
146 error = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
147 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200148 return error;
Krzysztof Helt880abd42009-01-30 19:20:29 +0100149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Rene Herman019608b2007-02-19 13:01:45 +0100152 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,
155 chip->mpu_port, 0,
Clemens Ladischdba8b462011-09-13 11:24:41 +0200156 mpu_irq[n], NULL);
Rene Hermanae596182007-02-14 13:26:17 +0100157 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200158 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Takashi Iwai43bcd972005-09-05 17:19:20 +0200160
Krzysztof Helta20971b2010-05-10 09:47:32 +0200161 return snd_card_register(card);
162}
163
Bill Pemberton1bff2922012-12-06 12:35:21 -0500164static int snd_es1688_isa_probe(struct device *dev, unsigned int n)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200165{
166 struct snd_card *card;
167 int error;
168
Takashi Iwai1bb11c12021-07-15 09:59:24 +0200169 error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE,
170 sizeof(struct snd_es1688), &card);
Krzysztof Helta20971b2010-05-10 09:47:32 +0200171 if (error < 0)
172 return error;
173
174 error = snd_es1688_legacy_create(card, dev, n);
175 if (error < 0)
Takashi Iwai1bb11c12021-07-15 09:59:24 +0200176 return error;
Krzysztof Helta20971b2010-05-10 09:47:32 +0200177
Krzysztof Helta20971b2010-05-10 09:47:32 +0200178 error = snd_es1688_probe(card, n);
Rene Hermanae596182007-02-14 13:26:17 +0100179 if (error < 0)
Takashi Iwai1bb11c12021-07-15 09:59:24 +0200180 return error;
Takashi Iwai43bcd972005-09-05 17:19:20 +0200181
Rene Hermanae596182007-02-14 13:26:17 +0100182 dev_set_drvdata(dev, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Krzysztof Helta20971b2010-05-10 09:47:32 +0200184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Rene Hermanae596182007-02-14 13:26:17 +0100187static struct isa_driver snd_es1688_driver = {
188 .match = snd_es1688_match,
Krzysztof Helta20971b2010-05-10 09:47:32 +0200189 .probe = snd_es1688_isa_probe,
Rene Hermanae596182007-02-14 13:26:17 +0100190#if 0 /* FIXME */
191 .suspend = snd_es1688_suspend,
192 .resume = snd_es1688_resume,
193#endif
Takashi Iwai2a571ed2005-11-17 17:13:30 +0100194 .driver = {
Rene Hermanae596182007-02-14 13:26:17 +0100195 .name = DEV_NAME
196 }
Takashi Iwai2a571ed2005-11-17 17:13:30 +0100197};
198
Krzysztof Helta20971b2010-05-10 09:47:32 +0200199static int snd_es968_pnp_is_probed;
200
201#ifdef CONFIG_PNP
Bill Pemberton1bff2922012-12-06 12:35:21 -0500202static 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)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200205{
206 struct snd_es1688 *chip = card->private_data;
207 struct pnp_dev *pdev;
208 int error;
209
210 pdev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
211 if (pdev == NULL)
212 return -ENODEV;
213
214 error = pnp_activate_dev(pdev);
215 if (error < 0) {
216 snd_printk(KERN_ERR "ES968 pnp configure failure\n");
217 return error;
218 }
219 port[n] = pnp_port_start(pdev, 0);
220 dma8[n] = pnp_dma(pdev, 0);
221 irq[n] = pnp_irq(pdev, 0);
222
223 return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n],
224 mpu_irq[n], dma8[n], ES1688_HW_AUTO);
225}
226
Bill Pemberton1bff2922012-12-06 12:35:21 -0500227static int snd_es968_pnp_detect(struct pnp_card_link *pcard,
228 const struct pnp_card_device_id *pid)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200229{
230 struct snd_card *card;
231 static unsigned int dev;
232 int error;
Krzysztof Helta20971b2010-05-10 09:47:32 +0200233
234 if (snd_es968_pnp_is_probed)
235 return -EBUSY;
236 for ( ; dev < SNDRV_CARDS; dev++) {
237 if (enable[dev] && isapnp[dev])
238 break;
239 }
Dan Carpenterb0fb75a2010-05-14 16:48:28 +0200240 if (dev == SNDRV_CARDS)
241 return -ENODEV;
Krzysztof Helta20971b2010-05-10 09:47:32 +0200242
Takashi Iwai1bb11c12021-07-15 09:59:24 +0200243 error = snd_devm_card_new(&pcard->card->dev,
244 index[dev], id[dev], THIS_MODULE,
245 sizeof(struct snd_es1688), &card);
Krzysztof Helta20971b2010-05-10 09:47:32 +0200246 if (error < 0)
247 return error;
Krzysztof Helta20971b2010-05-10 09:47:32 +0200248
249 error = snd_card_es968_pnp(card, dev, pcard, pid);
Takashi Iwai1bb11c12021-07-15 09:59:24 +0200250 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200251 return error;
Krzysztof Helta20971b2010-05-10 09:47:32 +0200252 error = snd_es1688_probe(card, dev);
Takashi Iwai1bb11c12021-07-15 09:59:24 +0200253 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200254 return error;
255 pnp_set_card_drvdata(pcard, card);
256 snd_es968_pnp_is_probed = 1;
257 return 0;
258}
259
Bill Pemberton1bff2922012-12-06 12:35:21 -0500260static void snd_es968_pnp_remove(struct pnp_card_link *pcard)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200261{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200262 snd_es968_pnp_is_probed = 0;
263}
264
265#ifdef CONFIG_PM
266static int snd_es968_pnp_suspend(struct pnp_card_link *pcard,
267 pm_message_t state)
268{
269 struct snd_card *card = pnp_get_card_drvdata(pcard);
Krzysztof Helta20971b2010-05-10 09:47:32 +0200270
271 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Krzysztof Helta20971b2010-05-10 09:47:32 +0200272 return 0;
273}
274
275static int snd_es968_pnp_resume(struct pnp_card_link *pcard)
276{
277 struct snd_card *card = pnp_get_card_drvdata(pcard);
278 struct snd_es1688 *chip = card->private_data;
279
280 snd_es1688_reset(chip);
281 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
282 return 0;
283}
284#endif
285
Arvind Yadav4f624a12017-08-17 15:36:24 +0530286static const struct pnp_card_device_id snd_es968_pnpids[] = {
Krzysztof Helta20971b2010-05-10 09:47:32 +0200287 { .id = "ESS0968", .devs = { { "@@@0968" }, } },
288 { .id = "ESS0968", .devs = { { "ESS0968" }, } },
289 { .id = "", } /* end */
290};
291
292MODULE_DEVICE_TABLE(pnp_card, snd_es968_pnpids);
293
294static 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,
Bill Pemberton1bff2922012-12-06 12:35:21 -0500299 .remove = snd_es968_pnp_remove,
Krzysztof Helta20971b2010-05-10 09:47:32 +0200300#ifdef CONFIG_PM
301 .suspend = snd_es968_pnp_suspend,
302 .resume = snd_es968_pnp_resume,
303#endif
304};
305#endif
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static int __init alsa_card_es1688_init(void)
308{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200309#ifdef CONFIG_PNP
310 pnp_register_card_driver(&es968_pnpc_driver);
311 if (snd_es968_pnp_is_probed)
312 return 0;
313 pnp_unregister_card_driver(&es968_pnpc_driver);
314#endif
Rene Hermanae596182007-02-14 13:26:17 +0100315 return isa_register_driver(&snd_es1688_driver, SNDRV_CARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318static void __exit alsa_card_es1688_exit(void)
319{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200320 if (!snd_es968_pnp_is_probed) {
321 isa_unregister_driver(&snd_es1688_driver);
322 return;
323 }
324#ifdef CONFIG_PNP
325 pnp_unregister_card_driver(&es968_pnpc_driver);
326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Rene Hermanae596182007-02-14 13:26:17 +0100329module_init(alsa_card_es1688_init);
330module_exit(alsa_card_es1688_exit);