xref: /openbmc/linux/sound/isa/es1688/es1688.c (revision 249592bf)
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_ALIAS("snd_es968");
30 
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 */
33 #ifdef CONFIG_PNP
34 static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
35 #endif
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 */
43 
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);
49 #ifdef CONFIG_PNP
50 module_param_array(isapnp, bool, NULL, 0444);
51 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
52 #endif
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.");
66 
67 #ifdef CONFIG_PNP
68 #define is_isapnp_selected(dev)		isapnp[dev]
69 #else
70 #define is_isapnp_selected(dev)		0
71 #endif
72 
73 static int snd_es1688_match(struct device *dev, unsigned int n)
74 {
75 	return enable[n] && !is_isapnp_selected(n);
76 }
77 
78 static int snd_es1688_legacy_create(struct snd_card *card,
79 				    struct device *dev, unsigned int n)
80 {
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};
85 
86 	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) {
91 			dev_err(dev, "unable to find a free IRQ\n");
92 			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) {
98 			dev_err(dev, "unable to find a free DMA\n");
99 			return -EBUSY;
100 		}
101 	}
102 
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);
106 
107 	i = 0;
108 	do {
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));
113 
114 	return error;
115 }
116 
117 static int snd_es1688_probe(struct snd_card *card, unsigned int n)
118 {
119 	struct snd_es1688 *chip = card->private_data;
120 	struct snd_opl3 *opl3;
121 	int error;
122 
123 	error = snd_es1688_pcm(card, chip, 0);
124 	if (error < 0)
125 		return error;
126 
127 	error = snd_es1688_mixer(card, chip);
128 	if (error < 0)
129 		return error;
130 
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);
136 
137 	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)
143 			dev_warn(card->dev,
144 				 "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)
148 				return error;
149 		}
150 	}
151 
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,
155 				chip->mpu_port, 0,
156 				mpu_irq[n], NULL);
157 		if (error < 0)
158 			return error;
159 	}
160 
161 	return snd_card_register(card);
162 }
163 
164 static int snd_es1688_isa_probe(struct device *dev, unsigned int n)
165 {
166 	struct snd_card *card;
167 	int error;
168 
169 	error = snd_card_new(dev, index[n], id[n], THIS_MODULE,
170 			     sizeof(struct snd_es1688), &card);
171 	if (error < 0)
172 		return error;
173 
174 	error = snd_es1688_legacy_create(card, dev, n);
175 	if (error < 0)
176 		goto out;
177 
178 	error = snd_es1688_probe(card, n);
179 	if (error < 0)
180 		goto out;
181 
182 	dev_set_drvdata(dev, card);
183 
184 	return 0;
185 out:
186 	snd_card_free(card);
187 	return error;
188 }
189 
190 static void snd_es1688_isa_remove(struct device *dev, unsigned int n)
191 {
192 	snd_card_free(dev_get_drvdata(dev));
193 }
194 
195 static struct isa_driver snd_es1688_driver = {
196 	.match		= snd_es1688_match,
197 	.probe		= snd_es1688_isa_probe,
198 	.remove		= snd_es1688_isa_remove,
199 #if 0	/* FIXME */
200 	.suspend	= snd_es1688_suspend,
201 	.resume		= snd_es1688_resume,
202 #endif
203 	.driver		= {
204 		.name	= DEV_NAME
205 	}
206 };
207 
208 static int snd_es968_pnp_is_probed;
209 
210 #ifdef CONFIG_PNP
211 static int snd_card_es968_pnp(struct snd_card *card, unsigned int n,
212 			      struct pnp_card_link *pcard,
213 			      const struct pnp_card_device_id *pid)
214 {
215 	struct snd_es1688 *chip = card->private_data;
216 	struct pnp_dev *pdev;
217 	int error;
218 
219 	pdev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
220 	if (pdev == NULL)
221 		return -ENODEV;
222 
223 	error = pnp_activate_dev(pdev);
224 	if (error < 0) {
225 		snd_printk(KERN_ERR "ES968 pnp configure failure\n");
226 		return error;
227 	}
228 	port[n] = pnp_port_start(pdev, 0);
229 	dma8[n] = pnp_dma(pdev, 0);
230 	irq[n] = pnp_irq(pdev, 0);
231 
232 	return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n],
233 				 mpu_irq[n], dma8[n], ES1688_HW_AUTO);
234 }
235 
236 static int snd_es968_pnp_detect(struct pnp_card_link *pcard,
237 				const struct pnp_card_device_id *pid)
238 {
239 	struct snd_card *card;
240 	static unsigned int dev;
241 	int error;
242 
243 	if (snd_es968_pnp_is_probed)
244 		return -EBUSY;
245 	for ( ; dev < SNDRV_CARDS; dev++) {
246 		if (enable[dev] && isapnp[dev])
247 			break;
248 	}
249 	if (dev == SNDRV_CARDS)
250 		return -ENODEV;
251 
252 	error = snd_card_new(&pcard->card->dev,
253 			     index[dev], id[dev], THIS_MODULE,
254 			     sizeof(struct snd_es1688), &card);
255 	if (error < 0)
256 		return error;
257 
258 	error = snd_card_es968_pnp(card, dev, pcard, pid);
259 	if (error < 0) {
260 		snd_card_free(card);
261 		return error;
262 	}
263 	error = snd_es1688_probe(card, dev);
264 	if (error < 0) {
265 		snd_card_free(card);
266 		return error;
267 	}
268 	pnp_set_card_drvdata(pcard, card);
269 	snd_es968_pnp_is_probed = 1;
270 	return 0;
271 }
272 
273 static void snd_es968_pnp_remove(struct pnp_card_link *pcard)
274 {
275 	snd_card_free(pnp_get_card_drvdata(pcard));
276 	pnp_set_card_drvdata(pcard, NULL);
277 	snd_es968_pnp_is_probed = 0;
278 }
279 
280 #ifdef CONFIG_PM
281 static int snd_es968_pnp_suspend(struct pnp_card_link *pcard,
282 				 pm_message_t state)
283 {
284 	struct snd_card *card = pnp_get_card_drvdata(pcard);
285 
286 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
287 	return 0;
288 }
289 
290 static int snd_es968_pnp_resume(struct pnp_card_link *pcard)
291 {
292 	struct snd_card *card = pnp_get_card_drvdata(pcard);
293 	struct snd_es1688 *chip = card->private_data;
294 
295 	snd_es1688_reset(chip);
296 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
297 	return 0;
298 }
299 #endif
300 
301 static const struct pnp_card_device_id snd_es968_pnpids[] = {
302 	{ .id = "ESS0968", .devs = { { "@@@0968" }, } },
303 	{ .id = "ESS0968", .devs = { { "ESS0968" }, } },
304 	{ .id = "", } /* end */
305 };
306 
307 MODULE_DEVICE_TABLE(pnp_card, snd_es968_pnpids);
308 
309 static struct pnp_card_driver es968_pnpc_driver = {
310 	.flags		= PNP_DRIVER_RES_DISABLE,
311 	.name		= DEV_NAME " PnP",
312 	.id_table	= snd_es968_pnpids,
313 	.probe		= snd_es968_pnp_detect,
314 	.remove		= snd_es968_pnp_remove,
315 #ifdef CONFIG_PM
316 	.suspend	= snd_es968_pnp_suspend,
317 	.resume		= snd_es968_pnp_resume,
318 #endif
319 };
320 #endif
321 
322 static int __init alsa_card_es1688_init(void)
323 {
324 #ifdef CONFIG_PNP
325 	pnp_register_card_driver(&es968_pnpc_driver);
326 	if (snd_es968_pnp_is_probed)
327 		return 0;
328 	pnp_unregister_card_driver(&es968_pnpc_driver);
329 #endif
330 	return isa_register_driver(&snd_es1688_driver, SNDRV_CARDS);
331 }
332 
333 static void __exit alsa_card_es1688_exit(void)
334 {
335 	if (!snd_es968_pnp_is_probed) {
336 		isa_unregister_driver(&snd_es1688_driver);
337 		return;
338 	}
339 #ifdef CONFIG_PNP
340 	pnp_unregister_card_driver(&es968_pnpc_driver);
341 #endif
342 }
343 
344 module_init(alsa_card_es1688_init);
345 module_exit(alsa_card_es1688_exit);
346