blob: 1901c2bb6c3bca0cb0ace9a95617f5137a6fa07a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for generic ESS AudioDrive ESx688 soundcards
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Takashi Iwai2a571ed2005-11-17 17:13:30 +010023#include <linux/err.h>
Rene Hermanae596182007-02-14 13:26:17 +010024#include <linux/isa.h>
Krzysztof Helta20971b2010-05-10 09:47:32 +020025#include <linux/isapnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/time.h>
27#include <linux/wait.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040028#include <linux/module.h>
Takashi Iwai2a571ed2005-11-17 17:13:30 +010029#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/es1688.h>
32#include <sound/mpu401.h>
33#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define SNDRV_LEGACY_FIND_FREE_IRQ
35#define SNDRV_LEGACY_FIND_FREE_DMA
36#include <sound/initval.h>
37
Rene Hermanae596182007-02-14 13:26:17 +010038#define CRD_NAME "Generic ESS ES1688/ES688 AudioDrive"
39#define DEV_NAME "es1688"
40
41MODULE_DESCRIPTION(CRD_NAME);
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020042MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043MODULE_LICENSE("GPL");
44MODULE_SUPPORTED_DEVICE("{{ESS,ES688 PnP AudioDrive,pnp:ESS0100},"
45 "{ESS,ES1688 PnP AudioDrive,pnp:ESS0102},"
46 "{ESS,ES688 AudioDrive,pnp:ESS6881},"
47 "{ESS,ES1688 AudioDrive,pnp:ESS1681}}");
48
Krzysztof Helta20971b2010-05-10 09:47:32 +020049MODULE_ALIAS("snd_es968");
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
52static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Krzysztof Helta20971b2010-05-10 09:47:32 +020053#ifdef CONFIG_PNP
Rusty Russella67ff6a2011-12-15 13:49:36 +103054static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
Krzysztof Helta20971b2010-05-10 09:47:32 +020055#endif
Rusty Russella67ff6a2011-12-15 13:49:36 +103056static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
Krzysztof Helt880abd42009-01-30 19:20:29 +010058static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* Usually 0x388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
60static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
61static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
62static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
63
64module_param_array(index, int, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010065MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param_array(id, charp, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010067MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068module_param_array(enable, bool, NULL, 0444);
Krzysztof Helta20971b2010-05-10 09:47:32 +020069#ifdef CONFIG_PNP
70module_param_array(isapnp, bool, NULL, 0444);
71MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
72#endif
Rene Hermanae596182007-02-14 13:26:17 +010073MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074module_param_array(port, long, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010075MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076module_param_array(mpu_port, long, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010077MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078module_param_array(irq, int, NULL, 0444);
Krzysztof Helt880abd42009-01-30 19:20:29 +010079module_param_array(fm_port, long, NULL, 0444);
80MODULE_PARM_DESC(fm_port, "FM port # for ES1688 driver.");
Rene Hermanae596182007-02-14 13:26:17 +010081MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082module_param_array(mpu_irq, int, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010083MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084module_param_array(dma8, int, NULL, 0444);
Rene Hermanae596182007-02-14 13:26:17 +010085MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Krzysztof Helta20971b2010-05-10 09:47:32 +020087#ifdef CONFIG_PNP
88#define is_isapnp_selected(dev) isapnp[dev]
89#else
90#define is_isapnp_selected(dev) 0
91#endif
92
Bill Pemberton1bff2922012-12-06 12:35:21 -050093static int snd_es1688_match(struct device *dev, unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Krzysztof Helta20971b2010-05-10 09:47:32 +020095 return enable[n] && !is_isapnp_selected(n);
Rene Hermanae596182007-02-14 13:26:17 +010096}
97
Bill Pemberton1bff2922012-12-06 12:35:21 -050098static int snd_es1688_legacy_create(struct snd_card *card,
99 struct device *dev, unsigned int n)
Rene Hermanae596182007-02-14 13:26:17 +0100100{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200101 struct snd_es1688 *chip = card->private_data;
Rene Herman019608b2007-02-19 13:01:45 +0100102 static long possible_ports[] = {0x220, 0x240, 0x260};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 static int possible_irqs[] = {5, 9, 10, 7, -1};
104 static int possible_dmas[] = {1, 3, 0, -1};
Rene Hermanae596182007-02-14 13:26:17 +0100105
Rene Herman019608b2007-02-19 13:01:45 +0100106 int i, error;
107
108 if (irq[n] == SNDRV_AUTO_IRQ) {
109 irq[n] = snd_legacy_find_free_irq(possible_irqs);
110 if (irq[n] < 0) {
Takashi Iwai0418ff02008-11-03 08:51:33 +0100111 dev_err(dev, "unable to find a free IRQ\n");
Rene Herman019608b2007-02-19 13:01:45 +0100112 return -EBUSY;
113 }
114 }
115 if (dma8[n] == SNDRV_AUTO_DMA) {
116 dma8[n] = snd_legacy_find_free_dma(possible_dmas);
117 if (dma8[n] < 0) {
Takashi Iwai0418ff02008-11-03 08:51:33 +0100118 dev_err(dev, "unable to find a free DMA\n");
Rene Herman019608b2007-02-19 13:01:45 +0100119 return -EBUSY;
120 }
121 }
122
123 if (port[n] != SNDRV_AUTO_PORT)
Krzysztof Helt396fa822010-05-09 20:35:44 +0200124 return snd_es1688_create(card, chip, port[n], mpu_port[n],
125 irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
Rene Herman019608b2007-02-19 13:01:45 +0100126
127 i = 0;
128 do {
129 port[n] = possible_ports[i];
Krzysztof Helt396fa822010-05-09 20:35:44 +0200130 error = snd_es1688_create(card, chip, port[n], mpu_port[n],
131 irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
Rene Herman019608b2007-02-19 13:01:45 +0100132 } while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
133
134 return error;
135}
136
Bill Pemberton1bff2922012-12-06 12:35:21 -0500137static int snd_es1688_probe(struct snd_card *card, unsigned int n)
Rene Herman019608b2007-02-19 13:01:45 +0100138{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200139 struct snd_es1688 *chip = card->private_data;
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100140 struct snd_opl3 *opl3;
Rene Hermanae596182007-02-14 13:26:17 +0100141 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Lars-Peter Clausen4b8ab882015-01-02 12:24:37 +0100143 error = snd_es1688_pcm(card, chip, 0);
Takashi Iwaic95eadd2008-12-28 16:43:35 +0100144 if (error < 0)
145 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Krzysztof Helt396fa822010-05-09 20:35:44 +0200147 error = snd_es1688_mixer(card, chip);
Rene Hermanae596182007-02-14 13:26:17 +0100148 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200149 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Dan Carpenterb0fb75a2010-05-14 16:48:28 +0200151 strlcpy(card->driver, "ES1688", sizeof(card->driver));
Lars-Peter Clausen4b8ab882015-01-02 12:24:37 +0100152 strlcpy(card->shortname, chip->pcm->name, sizeof(card->shortname));
Dan Carpenterb0fb75a2010-05-14 16:48:28 +0200153 snprintf(card->longname, sizeof(card->longname),
Lars-Peter Clausen4b8ab882015-01-02 12:24:37 +0100154 "%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port,
Dan Carpenterb0fb75a2010-05-14 16:48:28 +0200155 chip->irq, chip->dma8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Krzysztof Helt880abd42009-01-30 19:20:29 +0100157 if (fm_port[n] == SNDRV_AUTO_PORT)
158 fm_port[n] = port[n]; /* share the same port */
159
160 if (fm_port[n] > 0) {
161 if (snd_opl3_create(card, fm_port[n], fm_port[n] + 2,
162 OPL3_HW_OPL3, 0, &opl3) < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200163 dev_warn(card->dev,
Krzysztof Helt880abd42009-01-30 19:20:29 +0100164 "opl3 not detected at 0x%lx\n", fm_port[n]);
165 else {
166 error = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
167 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200168 return error;
Krzysztof Helt880abd42009-01-30 19:20:29 +0100169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Rene Herman019608b2007-02-19 13:01:45 +0100172 if (mpu_irq[n] >= 0 && mpu_irq[n] != SNDRV_AUTO_IRQ &&
173 chip->mpu_port > 0) {
174 error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
175 chip->mpu_port, 0,
Clemens Ladischdba8b462011-09-13 11:24:41 +0200176 mpu_irq[n], NULL);
Rene Hermanae596182007-02-14 13:26:17 +0100177 if (error < 0)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200178 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Takashi Iwai43bcd972005-09-05 17:19:20 +0200180
Krzysztof Helta20971b2010-05-10 09:47:32 +0200181 return snd_card_register(card);
182}
183
Bill Pemberton1bff2922012-12-06 12:35:21 -0500184static int snd_es1688_isa_probe(struct device *dev, unsigned int n)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200185{
186 struct snd_card *card;
187 int error;
188
Takashi Iwai4323cc42014-01-29 13:03:56 +0100189 error = snd_card_new(dev, index[n], id[n], THIS_MODULE,
190 sizeof(struct snd_es1688), &card);
Krzysztof Helta20971b2010-05-10 09:47:32 +0200191 if (error < 0)
192 return error;
193
194 error = snd_es1688_legacy_create(card, dev, n);
195 if (error < 0)
196 goto out;
197
Krzysztof Helta20971b2010-05-10 09:47:32 +0200198 error = snd_es1688_probe(card, n);
Rene Hermanae596182007-02-14 13:26:17 +0100199 if (error < 0)
200 goto out;
Takashi Iwai43bcd972005-09-05 17:19:20 +0200201
Rene Hermanae596182007-02-14 13:26:17 +0100202 dev_set_drvdata(dev, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Krzysztof Helta20971b2010-05-10 09:47:32 +0200204 return 0;
205out:
206 snd_card_free(card);
Rene Hermanae596182007-02-14 13:26:17 +0100207 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Bill Pemberton1bff2922012-12-06 12:35:21 -0500210static int snd_es1688_isa_remove(struct device *dev, unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Rene Hermanae596182007-02-14 13:26:17 +0100212 snd_card_free(dev_get_drvdata(dev));
Takashi Iwai2a571ed2005-11-17 17:13:30 +0100213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Rene Hermanae596182007-02-14 13:26:17 +0100216static struct isa_driver snd_es1688_driver = {
217 .match = snd_es1688_match,
Krzysztof Helta20971b2010-05-10 09:47:32 +0200218 .probe = snd_es1688_isa_probe,
Bill Pemberton1bff2922012-12-06 12:35:21 -0500219 .remove = snd_es1688_isa_remove,
Rene Hermanae596182007-02-14 13:26:17 +0100220#if 0 /* FIXME */
221 .suspend = snd_es1688_suspend,
222 .resume = snd_es1688_resume,
223#endif
Takashi Iwai2a571ed2005-11-17 17:13:30 +0100224 .driver = {
Rene Hermanae596182007-02-14 13:26:17 +0100225 .name = DEV_NAME
226 }
Takashi Iwai2a571ed2005-11-17 17:13:30 +0100227};
228
Krzysztof Helta20971b2010-05-10 09:47:32 +0200229static int snd_es968_pnp_is_probed;
230
231#ifdef CONFIG_PNP
Bill Pemberton1bff2922012-12-06 12:35:21 -0500232static int snd_card_es968_pnp(struct snd_card *card, unsigned int n,
233 struct pnp_card_link *pcard,
234 const struct pnp_card_device_id *pid)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200235{
236 struct snd_es1688 *chip = card->private_data;
237 struct pnp_dev *pdev;
238 int error;
239
240 pdev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
241 if (pdev == NULL)
242 return -ENODEV;
243
244 error = pnp_activate_dev(pdev);
245 if (error < 0) {
246 snd_printk(KERN_ERR "ES968 pnp configure failure\n");
247 return error;
248 }
249 port[n] = pnp_port_start(pdev, 0);
250 dma8[n] = pnp_dma(pdev, 0);
251 irq[n] = pnp_irq(pdev, 0);
252
253 return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n],
254 mpu_irq[n], dma8[n], ES1688_HW_AUTO);
255}
256
Bill Pemberton1bff2922012-12-06 12:35:21 -0500257static int snd_es968_pnp_detect(struct pnp_card_link *pcard,
258 const struct pnp_card_device_id *pid)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200259{
260 struct snd_card *card;
261 static unsigned int dev;
262 int error;
263 struct snd_es1688 *chip;
264
265 if (snd_es968_pnp_is_probed)
266 return -EBUSY;
267 for ( ; dev < SNDRV_CARDS; dev++) {
268 if (enable[dev] && isapnp[dev])
269 break;
270 }
Dan Carpenterb0fb75a2010-05-14 16:48:28 +0200271 if (dev == SNDRV_CARDS)
272 return -ENODEV;
Krzysztof Helta20971b2010-05-10 09:47:32 +0200273
Takashi Iwai4323cc42014-01-29 13:03:56 +0100274 error = snd_card_new(&pcard->card->dev,
275 index[dev], id[dev], THIS_MODULE,
276 sizeof(struct snd_es1688), &card);
Krzysztof Helta20971b2010-05-10 09:47:32 +0200277 if (error < 0)
278 return error;
279 chip = card->private_data;
280
281 error = snd_card_es968_pnp(card, dev, pcard, pid);
282 if (error < 0) {
283 snd_card_free(card);
284 return error;
285 }
Krzysztof Helta20971b2010-05-10 09:47:32 +0200286 error = snd_es1688_probe(card, dev);
287 if (error < 0)
288 return error;
289 pnp_set_card_drvdata(pcard, card);
290 snd_es968_pnp_is_probed = 1;
291 return 0;
292}
293
Bill Pemberton1bff2922012-12-06 12:35:21 -0500294static void snd_es968_pnp_remove(struct pnp_card_link *pcard)
Krzysztof Helta20971b2010-05-10 09:47:32 +0200295{
296 snd_card_free(pnp_get_card_drvdata(pcard));
297 pnp_set_card_drvdata(pcard, NULL);
298 snd_es968_pnp_is_probed = 0;
299}
300
301#ifdef CONFIG_PM
302static int snd_es968_pnp_suspend(struct pnp_card_link *pcard,
303 pm_message_t state)
304{
305 struct snd_card *card = pnp_get_card_drvdata(pcard);
306 struct snd_es1688 *chip = card->private_data;
307
308 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
309 snd_pcm_suspend_all(chip->pcm);
310 return 0;
311}
312
313static int snd_es968_pnp_resume(struct pnp_card_link *pcard)
314{
315 struct snd_card *card = pnp_get_card_drvdata(pcard);
316 struct snd_es1688 *chip = card->private_data;
317
318 snd_es1688_reset(chip);
319 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
320 return 0;
321}
322#endif
323
324static struct pnp_card_device_id snd_es968_pnpids[] = {
325 { .id = "ESS0968", .devs = { { "@@@0968" }, } },
326 { .id = "ESS0968", .devs = { { "ESS0968" }, } },
327 { .id = "", } /* end */
328};
329
330MODULE_DEVICE_TABLE(pnp_card, snd_es968_pnpids);
331
332static struct pnp_card_driver es968_pnpc_driver = {
333 .flags = PNP_DRIVER_RES_DISABLE,
334 .name = DEV_NAME " PnP",
335 .id_table = snd_es968_pnpids,
336 .probe = snd_es968_pnp_detect,
Bill Pemberton1bff2922012-12-06 12:35:21 -0500337 .remove = snd_es968_pnp_remove,
Krzysztof Helta20971b2010-05-10 09:47:32 +0200338#ifdef CONFIG_PM
339 .suspend = snd_es968_pnp_suspend,
340 .resume = snd_es968_pnp_resume,
341#endif
342};
343#endif
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int __init alsa_card_es1688_init(void)
346{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200347#ifdef CONFIG_PNP
348 pnp_register_card_driver(&es968_pnpc_driver);
349 if (snd_es968_pnp_is_probed)
350 return 0;
351 pnp_unregister_card_driver(&es968_pnpc_driver);
352#endif
Rene Hermanae596182007-02-14 13:26:17 +0100353 return isa_register_driver(&snd_es1688_driver, SNDRV_CARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356static void __exit alsa_card_es1688_exit(void)
357{
Krzysztof Helta20971b2010-05-10 09:47:32 +0200358 if (!snd_es968_pnp_is_probed) {
359 isa_unregister_driver(&snd_es1688_driver);
360 return;
361 }
362#ifdef CONFIG_PNP
363 pnp_unregister_card_driver(&es968_pnpc_driver);
364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Rene Hermanae596182007-02-14 13:26:17 +0100367module_init(alsa_card_es1688_init);
368module_exit(alsa_card_es1688_exit);