xref: /openbmc/linux/sound/isa/azt2320.c (revision f7af616c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     card-azt2320.c - driver for Aztech Systems AZT2320 based soundcards.
4     Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
5 
6 */
7 
8 /*
9     This driver should provide support for most Aztech AZT2320 based cards.
10     Several AZT2316 chips are also supported/tested, but autoprobe doesn't
11     work: all module option have to be set.
12 
13     No docs available for us at Aztech headquarters !!!   Unbelievable ...
14     No other help obtained.
15 
16     Thanks to Rainer Wiesner <rainer.wiesner@01019freenet.de> for the WSS
17     activation method (full-duplex audio!).
18 */
19 
20 #include <linux/io.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/time.h>
24 #include <linux/wait.h>
25 #include <linux/pnp.h>
26 #include <linux/module.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
29 #include <sound/wss.h>
30 #include <sound/mpu401.h>
31 #include <sound/opl3.h>
32 
33 #define PFX "azt2320: "
34 
35 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
36 MODULE_DESCRIPTION("Aztech Systems AZT2320");
37 MODULE_LICENSE("GPL");
38 
39 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
40 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
41 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
42 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
43 static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
44 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
45 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
46 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* Pnp setup */
47 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* Pnp setup */
48 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* PnP setup */
49 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* PnP setup */
50 
51 module_param_array(index, int, NULL, 0444);
52 MODULE_PARM_DESC(index, "Index value for azt2320 based soundcard.");
53 module_param_array(id, charp, NULL, 0444);
54 MODULE_PARM_DESC(id, "ID string for azt2320 based soundcard.");
55 module_param_array(enable, bool, NULL, 0444);
56 MODULE_PARM_DESC(enable, "Enable azt2320 based soundcard.");
57 
58 struct snd_card_azt2320 {
59 	int dev_no;
60 	struct pnp_dev *dev;
61 	struct pnp_dev *devmpu;
62 	struct snd_wss *chip;
63 };
64 
65 static const struct pnp_card_device_id snd_azt2320_pnpids[] = {
66 	/* PRO16V */
67 	{ .id = "AZT1008", .devs = { { "AZT1008" }, { "AZT2001" }, } },
68 	/* Aztech Sound Galaxy 16 */
69 	{ .id = "AZT2320", .devs = { { "AZT0001" }, { "AZT0002" }, } },
70 	/* Packard Bell Sound III 336 AM/SP */
71 	{ .id = "AZT3000", .devs = { { "AZT1003" }, { "AZT2001" }, } },
72 	/* AT3300 */
73 	{ .id = "AZT3002", .devs = { { "AZT1004" }, { "AZT2001" }, } },
74 	/* --- */
75 	{ .id = "AZT3005", .devs = { { "AZT1003" }, { "AZT2001" }, } },
76 	/* --- */
77 	{ .id = "AZT3011", .devs = { { "AZT1003" }, { "AZT2001" }, } },
78 	{ .id = "" }	/* end */
79 };
80 
81 MODULE_DEVICE_TABLE(pnp_card, snd_azt2320_pnpids);
82 
83 #define	DRIVER_NAME	"snd-card-azt2320"
84 
85 static int snd_card_azt2320_pnp(int dev, struct snd_card_azt2320 *acard,
86 				struct pnp_card_link *card,
87 				const struct pnp_card_device_id *id)
88 {
89 	struct pnp_dev *pdev;
90 	int err;
91 
92 	acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
93 	if (acard->dev == NULL)
94 		return -ENODEV;
95 
96 	acard->devmpu = pnp_request_card_device(card, id->devs[1].id, NULL);
97 
98 	pdev = acard->dev;
99 
100 	err = pnp_activate_dev(pdev);
101 	if (err < 0) {
102 		snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n");
103 		return err;
104 	}
105 	port[dev] = pnp_port_start(pdev, 0);
106 	fm_port[dev] = pnp_port_start(pdev, 1);
107 	wss_port[dev] = pnp_port_start(pdev, 2);
108 	dma1[dev] = pnp_dma(pdev, 0);
109 	dma2[dev] = pnp_dma(pdev, 1);
110 	irq[dev] = pnp_irq(pdev, 0);
111 
112 	pdev = acard->devmpu;
113 	if (pdev != NULL) {
114 		err = pnp_activate_dev(pdev);
115 		if (err < 0)
116 			goto __mpu_error;
117 		mpu_port[dev] = pnp_port_start(pdev, 0);
118 		mpu_irq[dev] = pnp_irq(pdev, 0);
119 	} else {
120 	     __mpu_error:
121 	     	if (pdev) {
122 		     	pnp_release_card_device(pdev);
123 	     		snd_printk(KERN_ERR PFX "MPU401 pnp configure failure, skipping\n");
124 	     	}
125 	     	acard->devmpu = NULL;
126 	     	mpu_port[dev] = -1;
127 	}
128 
129 	return 0;
130 }
131 
132 /* same of snd_sbdsp_command by Jaroslav Kysela */
133 static int snd_card_azt2320_command(unsigned long port, unsigned char val)
134 {
135 	int i;
136 	unsigned long limit;
137 
138 	limit = jiffies + HZ / 10;
139 	for (i = 50000; i && time_after(limit, jiffies); i--)
140 		if (!(inb(port + 0x0c) & 0x80)) {
141 			outb(val, port + 0x0c);
142 			return 0;
143 		}
144 	return -EBUSY;
145 }
146 
147 static int snd_card_azt2320_enable_wss(unsigned long port)
148 {
149 	int error;
150 
151 	if ((error = snd_card_azt2320_command(port, 0x09)))
152 		return error;
153 	if ((error = snd_card_azt2320_command(port, 0x00)))
154 		return error;
155 
156 	mdelay(5);
157 	return 0;
158 }
159 
160 static int snd_card_azt2320_probe(int dev,
161 				  struct pnp_card_link *pcard,
162 				  const struct pnp_card_device_id *pid)
163 {
164 	int error;
165 	struct snd_card *card;
166 	struct snd_card_azt2320 *acard;
167 	struct snd_wss *chip;
168 	struct snd_opl3 *opl3;
169 
170 	error = snd_card_new(&pcard->card->dev,
171 			     index[dev], id[dev], THIS_MODULE,
172 			     sizeof(struct snd_card_azt2320), &card);
173 	if (error < 0)
174 		return error;
175 	acard = card->private_data;
176 
177 	if ((error = snd_card_azt2320_pnp(dev, acard, pcard, pid))) {
178 		snd_card_free(card);
179 		return error;
180 	}
181 
182 	if ((error = snd_card_azt2320_enable_wss(port[dev]))) {
183 		snd_card_free(card);
184 		return error;
185 	}
186 
187 	error = snd_wss_create(card, wss_port[dev], -1,
188 			       irq[dev],
189 			       dma1[dev], dma2[dev],
190 			       WSS_HW_DETECT, 0, &chip);
191 	if (error < 0) {
192 		snd_card_free(card);
193 		return error;
194 	}
195 
196 	strcpy(card->driver, "AZT2320");
197 	strcpy(card->shortname, "Aztech AZT2320");
198 	sprintf(card->longname, "%s, WSS at 0x%lx, irq %i, dma %i&%i",
199 		card->shortname, chip->port, irq[dev], dma1[dev], dma2[dev]);
200 
201 	error = snd_wss_pcm(chip, 0);
202 	if (error < 0) {
203 		snd_card_free(card);
204 		return error;
205 	}
206 	error = snd_wss_mixer(chip);
207 	if (error < 0) {
208 		snd_card_free(card);
209 		return error;
210 	}
211 	error = snd_wss_timer(chip, 0);
212 	if (error < 0) {
213 		snd_card_free(card);
214 		return error;
215 	}
216 
217 	if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
218 		if (snd_mpu401_uart_new(card, 0, MPU401_HW_AZT2320,
219 				mpu_port[dev], 0,
220 				mpu_irq[dev], NULL) < 0)
221 			snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx\n", mpu_port[dev]);
222 	}
223 
224 	if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
225 		if (snd_opl3_create(card,
226 				    fm_port[dev], fm_port[dev] + 2,
227 				    OPL3_HW_AUTO, 0, &opl3) < 0) {
228 			snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n",
229 				   fm_port[dev], fm_port[dev] + 2);
230 		} else {
231 			if ((error = snd_opl3_timer_new(opl3, 1, 2)) < 0) {
232 				snd_card_free(card);
233 				return error;
234 			}
235 			if ((error = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
236 				snd_card_free(card);
237 				return error;
238 			}
239 		}
240 	}
241 
242 	if ((error = snd_card_register(card)) < 0) {
243 		snd_card_free(card);
244 		return error;
245 	}
246 	pnp_set_card_drvdata(pcard, card);
247 	return 0;
248 }
249 
250 static unsigned int azt2320_devices;
251 
252 static int snd_azt2320_pnp_detect(struct pnp_card_link *card,
253 				  const struct pnp_card_device_id *id)
254 {
255 	static int dev;
256 	int res;
257 
258 	for ( ; dev < SNDRV_CARDS; dev++) {
259 		if (!enable[dev])
260 			continue;
261 		res = snd_card_azt2320_probe(dev, card, id);
262 		if (res < 0)
263 			return res;
264 		dev++;
265 		azt2320_devices++;
266 		return 0;
267 	}
268         return -ENODEV;
269 }
270 
271 static void snd_azt2320_pnp_remove(struct pnp_card_link *pcard)
272 {
273 	snd_card_free(pnp_get_card_drvdata(pcard));
274 	pnp_set_card_drvdata(pcard, NULL);
275 }
276 
277 #ifdef CONFIG_PM
278 static int snd_azt2320_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
279 {
280 	struct snd_card *card = pnp_get_card_drvdata(pcard);
281 	struct snd_card_azt2320 *acard = card->private_data;
282 	struct snd_wss *chip = acard->chip;
283 
284 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
285 	chip->suspend(chip);
286 	return 0;
287 }
288 
289 static int snd_azt2320_pnp_resume(struct pnp_card_link *pcard)
290 {
291 	struct snd_card *card = pnp_get_card_drvdata(pcard);
292 	struct snd_card_azt2320 *acard = card->private_data;
293 	struct snd_wss *chip = acard->chip;
294 
295 	chip->resume(chip);
296 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
297 	return 0;
298 }
299 #endif
300 
301 static struct pnp_card_driver azt2320_pnpc_driver = {
302 	.flags          = PNP_DRIVER_RES_DISABLE,
303 	.name           = "azt2320",
304 	.id_table       = snd_azt2320_pnpids,
305 	.probe          = snd_azt2320_pnp_detect,
306 	.remove         = snd_azt2320_pnp_remove,
307 #ifdef CONFIG_PM
308 	.suspend	= snd_azt2320_pnp_suspend,
309 	.resume		= snd_azt2320_pnp_resume,
310 #endif
311 };
312 
313 static int __init alsa_card_azt2320_init(void)
314 {
315 	int err;
316 
317 	err = pnp_register_card_driver(&azt2320_pnpc_driver);
318 	if (err)
319 		return err;
320 
321 	if (!azt2320_devices) {
322 		pnp_unregister_card_driver(&azt2320_pnpc_driver);
323 #ifdef MODULE
324 		snd_printk(KERN_ERR "no AZT2320 based soundcards found\n");
325 #endif
326 		return -ENODEV;
327 	}
328 	return 0;
329 }
330 
331 static void __exit alsa_card_azt2320_exit(void)
332 {
333 	pnp_unregister_card_driver(&azt2320_pnpc_driver);
334 }
335 
336 module_init(alsa_card_azt2320_init)
337 module_exit(alsa_card_azt2320_exit)
338