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