xref: /openbmc/linux/sound/isa/es1688/es1688.c (revision 84d517f3)
1 /*
2  *  Driver for generic ESS AudioDrive ESx688 soundcards
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
23 #include <linux/err.h>
24 #include <linux/isa.h>
25 #include <linux/isapnp.h>
26 #include <linux/time.h>
27 #include <linux/wait.h>
28 #include <linux/module.h>
29 #include <asm/dma.h>
30 #include <sound/core.h>
31 #include <sound/es1688.h>
32 #include <sound/mpu401.h>
33 #include <sound/opl3.h>
34 #define SNDRV_LEGACY_FIND_FREE_IRQ
35 #define SNDRV_LEGACY_FIND_FREE_DMA
36 #include <sound/initval.h>
37 
38 #define CRD_NAME "Generic ESS ES1688/ES688 AudioDrive"
39 #define DEV_NAME "es1688"
40 
41 MODULE_DESCRIPTION(CRD_NAME);
42 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
43 MODULE_LICENSE("GPL");
44 MODULE_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 
49 MODULE_ALIAS("snd_es968");
50 
51 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
52 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
53 #ifdef CONFIG_PNP
54 static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
55 #endif
56 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;	/* Enable this card */
57 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x240,0x260 */
58 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* Usually 0x388 */
59 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
60 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,10 */
61 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,10 */
62 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
63 
64 module_param_array(index, int, NULL, 0444);
65 MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
66 module_param_array(id, charp, NULL, 0444);
67 MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
68 module_param_array(enable, bool, NULL, 0444);
69 #ifdef CONFIG_PNP
70 module_param_array(isapnp, bool, NULL, 0444);
71 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
72 #endif
73 MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
74 module_param_array(port, long, NULL, 0444);
75 MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
76 module_param_array(mpu_port, long, NULL, 0444);
77 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
78 module_param_array(irq, int, NULL, 0444);
79 module_param_array(fm_port, long, NULL, 0444);
80 MODULE_PARM_DESC(fm_port, "FM port # for ES1688 driver.");
81 MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
82 module_param_array(mpu_irq, int, NULL, 0444);
83 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
84 module_param_array(dma8, int, NULL, 0444);
85 MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver.");
86 
87 #ifdef CONFIG_PNP
88 #define is_isapnp_selected(dev)		isapnp[dev]
89 #else
90 #define is_isapnp_selected(dev)		0
91 #endif
92 
93 static int snd_es1688_match(struct device *dev, unsigned int n)
94 {
95 	return enable[n] && !is_isapnp_selected(n);
96 }
97 
98 static int snd_es1688_legacy_create(struct snd_card *card,
99 				    struct device *dev, unsigned int n)
100 {
101 	struct snd_es1688 *chip = card->private_data;
102 	static long possible_ports[] = {0x220, 0x240, 0x260};
103 	static int possible_irqs[] = {5, 9, 10, 7, -1};
104 	static int possible_dmas[] = {1, 3, 0, -1};
105 
106 	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) {
111 			dev_err(dev, "unable to find a free IRQ\n");
112 			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) {
118 			dev_err(dev, "unable to find a free DMA\n");
119 			return -EBUSY;
120 		}
121 	}
122 
123 	if (port[n] != SNDRV_AUTO_PORT)
124 		return snd_es1688_create(card, chip, port[n], mpu_port[n],
125 				irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
126 
127 	i = 0;
128 	do {
129 		port[n] = possible_ports[i];
130 		error = snd_es1688_create(card, chip, port[n], mpu_port[n],
131 				irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
132 	} while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
133 
134 	return error;
135 }
136 
137 static int snd_es1688_probe(struct snd_card *card, unsigned int n)
138 {
139 	struct snd_es1688 *chip = card->private_data;
140 	struct snd_opl3 *opl3;
141 	struct snd_pcm *pcm;
142 	int error;
143 
144 	error = snd_es1688_pcm(card, chip, 0, &pcm);
145 	if (error < 0)
146 		return error;
147 
148 	error = snd_es1688_mixer(card, chip);
149 	if (error < 0)
150 		return error;
151 
152 	strlcpy(card->driver, "ES1688", sizeof(card->driver));
153 	strlcpy(card->shortname, pcm->name, sizeof(card->shortname));
154 	snprintf(card->longname, sizeof(card->longname),
155 		"%s at 0x%lx, irq %i, dma %i", pcm->name, chip->port,
156 		 chip->irq, chip->dma8);
157 
158 	if (fm_port[n] == SNDRV_AUTO_PORT)
159 		fm_port[n] = port[n];	/* share the same port */
160 
161 	if (fm_port[n] > 0) {
162 		if (snd_opl3_create(card, fm_port[n], fm_port[n] + 2,
163 				OPL3_HW_OPL3, 0, &opl3) < 0)
164 			dev_warn(card->dev,
165 				 "opl3 not detected at 0x%lx\n", fm_port[n]);
166 		else {
167 			error =	snd_opl3_hwdep_new(opl3, 0, 1, NULL);
168 			if (error < 0)
169 				return error;
170 		}
171 	}
172 
173 	if (mpu_irq[n] >= 0 && mpu_irq[n] != SNDRV_AUTO_IRQ &&
174 			chip->mpu_port > 0) {
175 		error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
176 				chip->mpu_port, 0,
177 				mpu_irq[n], NULL);
178 		if (error < 0)
179 			return error;
180 	}
181 
182 	return snd_card_register(card);
183 }
184 
185 static int snd_es1688_isa_probe(struct device *dev, unsigned int n)
186 {
187 	struct snd_card *card;
188 	int error;
189 
190 	error = snd_card_new(dev, index[n], id[n], THIS_MODULE,
191 			     sizeof(struct snd_es1688), &card);
192 	if (error < 0)
193 		return error;
194 
195 	error = snd_es1688_legacy_create(card, dev, n);
196 	if (error < 0)
197 		goto out;
198 
199 	error = snd_es1688_probe(card, n);
200 	if (error < 0)
201 		goto out;
202 
203 	dev_set_drvdata(dev, card);
204 
205 	return 0;
206 out:
207 	snd_card_free(card);
208 	return error;
209 }
210 
211 static int snd_es1688_isa_remove(struct device *dev, unsigned int n)
212 {
213 	snd_card_free(dev_get_drvdata(dev));
214 	return 0;
215 }
216 
217 static struct isa_driver snd_es1688_driver = {
218 	.match		= snd_es1688_match,
219 	.probe		= snd_es1688_isa_probe,
220 	.remove		= snd_es1688_isa_remove,
221 #if 0	/* FIXME */
222 	.suspend	= snd_es1688_suspend,
223 	.resume		= snd_es1688_resume,
224 #endif
225 	.driver		= {
226 		.name	= DEV_NAME
227 	}
228 };
229 
230 static int snd_es968_pnp_is_probed;
231 
232 #ifdef CONFIG_PNP
233 static int snd_card_es968_pnp(struct snd_card *card, unsigned int n,
234 			      struct pnp_card_link *pcard,
235 			      const struct pnp_card_device_id *pid)
236 {
237 	struct snd_es1688 *chip = card->private_data;
238 	struct pnp_dev *pdev;
239 	int error;
240 
241 	pdev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
242 	if (pdev == NULL)
243 		return -ENODEV;
244 
245 	error = pnp_activate_dev(pdev);
246 	if (error < 0) {
247 		snd_printk(KERN_ERR "ES968 pnp configure failure\n");
248 		return error;
249 	}
250 	port[n] = pnp_port_start(pdev, 0);
251 	dma8[n] = pnp_dma(pdev, 0);
252 	irq[n] = pnp_irq(pdev, 0);
253 
254 	return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n],
255 				 mpu_irq[n], dma8[n], ES1688_HW_AUTO);
256 }
257 
258 static int snd_es968_pnp_detect(struct pnp_card_link *pcard,
259 				const struct pnp_card_device_id *pid)
260 {
261 	struct snd_card *card;
262 	static unsigned int dev;
263 	int error;
264 	struct snd_es1688 *chip;
265 
266 	if (snd_es968_pnp_is_probed)
267 		return -EBUSY;
268 	for ( ; dev < SNDRV_CARDS; dev++) {
269 		if (enable[dev] && isapnp[dev])
270 			break;
271 	}
272 	if (dev == SNDRV_CARDS)
273 		return -ENODEV;
274 
275 	error = snd_card_new(&pcard->card->dev,
276 			     index[dev], id[dev], THIS_MODULE,
277 			     sizeof(struct snd_es1688), &card);
278 	if (error < 0)
279 		return error;
280 	chip = card->private_data;
281 
282 	error = snd_card_es968_pnp(card, dev, pcard, pid);
283 	if (error < 0) {
284 		snd_card_free(card);
285 		return error;
286 	}
287 	error = snd_es1688_probe(card, dev);
288 	if (error < 0)
289 		return error;
290 	pnp_set_card_drvdata(pcard, card);
291 	snd_es968_pnp_is_probed = 1;
292 	return 0;
293 }
294 
295 static void snd_es968_pnp_remove(struct pnp_card_link *pcard)
296 {
297 	snd_card_free(pnp_get_card_drvdata(pcard));
298 	pnp_set_card_drvdata(pcard, NULL);
299 	snd_es968_pnp_is_probed = 0;
300 }
301 
302 #ifdef CONFIG_PM
303 static int snd_es968_pnp_suspend(struct pnp_card_link *pcard,
304 				 pm_message_t state)
305 {
306 	struct snd_card *card = pnp_get_card_drvdata(pcard);
307 	struct snd_es1688 *chip = card->private_data;
308 
309 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
310 	snd_pcm_suspend_all(chip->pcm);
311 	return 0;
312 }
313 
314 static int snd_es968_pnp_resume(struct pnp_card_link *pcard)
315 {
316 	struct snd_card *card = pnp_get_card_drvdata(pcard);
317 	struct snd_es1688 *chip = card->private_data;
318 
319 	snd_es1688_reset(chip);
320 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
321 	return 0;
322 }
323 #endif
324 
325 static struct pnp_card_device_id snd_es968_pnpids[] = {
326 	{ .id = "ESS0968", .devs = { { "@@@0968" }, } },
327 	{ .id = "ESS0968", .devs = { { "ESS0968" }, } },
328 	{ .id = "", } /* end */
329 };
330 
331 MODULE_DEVICE_TABLE(pnp_card, snd_es968_pnpids);
332 
333 static struct pnp_card_driver es968_pnpc_driver = {
334 	.flags		= PNP_DRIVER_RES_DISABLE,
335 	.name		= DEV_NAME " PnP",
336 	.id_table	= snd_es968_pnpids,
337 	.probe		= snd_es968_pnp_detect,
338 	.remove		= snd_es968_pnp_remove,
339 #ifdef CONFIG_PM
340 	.suspend	= snd_es968_pnp_suspend,
341 	.resume		= snd_es968_pnp_resume,
342 #endif
343 };
344 #endif
345 
346 static int __init alsa_card_es1688_init(void)
347 {
348 #ifdef CONFIG_PNP
349 	pnp_register_card_driver(&es968_pnpc_driver);
350 	if (snd_es968_pnp_is_probed)
351 		return 0;
352 	pnp_unregister_card_driver(&es968_pnpc_driver);
353 #endif
354 	return isa_register_driver(&snd_es1688_driver, SNDRV_CARDS);
355 }
356 
357 static void __exit alsa_card_es1688_exit(void)
358 {
359 	if (!snd_es968_pnp_is_probed) {
360 		isa_unregister_driver(&snd_es1688_driver);
361 		return;
362 	}
363 #ifdef CONFIG_PNP
364 	pnp_unregister_card_driver(&es968_pnpc_driver);
365 #endif
366 }
367 
368 module_init(alsa_card_es1688_init);
369 module_exit(alsa_card_es1688_exit);
370