xref: /linux/sound/isa/ad1816a/ad1816a.c (revision a9cb09b7be84a7adc9e6f4aaf8d770933d65f953)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4     card-ad1816a.c - driver for ADI SoundPort AD1816A based soundcards.
5     Copyright (C) 2000 by Massimo Piccioni <dafastidio@libero.it>
6 
7 */
8 
9 #include <linux/init.h>
10 #include <linux/time.h>
11 #include <linux/wait.h>
12 #include <linux/pnp.h>
13 #include <linux/module.h>
14 #include <sound/core.h>
15 #include <sound/initval.h>
16 #include <sound/ad1816a.h>
17 #include <sound/mpu401.h>
18 #include <sound/opl3.h>
19 
20 #define PFX "ad1816a: "
21 
22 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
23 MODULE_DESCRIPTION("AD1816A, AD1815");
24 MODULE_LICENSE("GPL");
25 
26 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 1-MAX */
27 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
28 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;	/* Enable this card */
29 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
30 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
31 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
32 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* Pnp setup */
33 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* Pnp setup */
34 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* PnP setup */
35 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* PnP setup */
36 static int clockfreq[SNDRV_CARDS];
37 
38 module_param_array(index, int, NULL, 0444);
39 MODULE_PARM_DESC(index, "Index value for ad1816a based soundcard.");
40 module_param_array(id, charp, NULL, 0444);
41 MODULE_PARM_DESC(id, "ID string for ad1816a based soundcard.");
42 module_param_array(enable, bool, NULL, 0444);
43 MODULE_PARM_DESC(enable, "Enable ad1816a based soundcard.");
44 module_param_array(clockfreq, int, NULL, 0444);
45 MODULE_PARM_DESC(clockfreq, "Clock frequency for ad1816a driver (default = 0).");
46 
47 static const struct pnp_card_device_id snd_ad1816a_pnpids[] = {
48 	/* Analog Devices AD1815 */
49 	{ .id = "ADS7150", .devs = { { .id = "ADS7150" }, { .id = "ADS7151" } } },
50 	/* Analog Devices AD1816? */
51 	{ .id = "ADS7180", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
52 	/* Analog Devices AD1816A - added by Kenneth Platz <kxp@atl.hp.com> */
53 	{ .id = "ADS7181", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
54 	/* Analog Devices AD1816A - Aztech/Newcom SC-16 3D */
55 	{ .id = "AZT1022", .devs = { { .id = "AZT1018" }, { .id = "AZT2002" } } },
56 	/* Highscreen Sound-Boostar 16 3D - added by Stefan Behnel */
57 	{ .id = "LWC1061", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
58 	/* Highscreen Sound-Boostar 16 3D */
59 	{ .id = "MDK1605", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
60 	/* Shark Predator ISA - added by Ken Arromdee */
61 	{ .id = "SMM7180", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
62 	/* Analog Devices AD1816A - Terratec AudioSystem EWS64 S */
63 	{ .id = "TER1112", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
64 	/* Analog Devices AD1816A - Terratec AudioSystem EWS64 S */
65 	{ .id = "TER1112", .devs = { { .id = "TER1100" }, { .id = "TER1101" } } },
66 	/* Analog Devices AD1816A - Terratec Base 64 */
67 	{ .id = "TER1411", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
68 	/* end */
69 	{ .id = "" }
70 };
71 
72 MODULE_DEVICE_TABLE(pnp_card, snd_ad1816a_pnpids);
73 
74 
75 #define	DRIVER_NAME	"snd-card-ad1816a"
76 
77 
78 static int snd_card_ad1816a_pnp(int dev, struct pnp_card_link *card,
79 				const struct pnp_card_device_id *id)
80 {
81 	struct pnp_dev *pdev;
82 	int err;
83 
84 	pdev = pnp_request_card_device(card, id->devs[0].id, NULL);
85 	if (pdev == NULL)
86 		return -EBUSY;
87 
88 	err = pnp_activate_dev(pdev);
89 	if (err < 0) {
90 		printk(KERN_ERR PFX "AUDIO PnP configure failure\n");
91 		return -EBUSY;
92 	}
93 
94 	port[dev] = pnp_port_start(pdev, 2);
95 	fm_port[dev] = pnp_port_start(pdev, 1);
96 	dma1[dev] = pnp_dma(pdev, 0);
97 	dma2[dev] = pnp_dma(pdev, 1);
98 	irq[dev] = pnp_irq(pdev, 0);
99 
100 	pdev = pnp_request_card_device(card, id->devs[1].id, NULL);
101 	if (pdev == NULL) {
102 		mpu_port[dev] = -1;
103 		snd_printk(KERN_WARNING PFX "MPU401 device busy, skipping.\n");
104 		return 0;
105 	}
106 
107 	err = pnp_activate_dev(pdev);
108 	if (err < 0) {
109 		printk(KERN_ERR PFX "MPU401 PnP configure failure\n");
110 		mpu_port[dev] = -1;
111 	} else {
112 		mpu_port[dev] = pnp_port_start(pdev, 0);
113 		mpu_irq[dev] = pnp_irq(pdev, 0);
114 	}
115 
116 	return 0;
117 }
118 
119 static int snd_card_ad1816a_probe(int dev, struct pnp_card_link *pcard,
120 				  const struct pnp_card_device_id *pid)
121 {
122 	int error;
123 	struct snd_card *card;
124 	struct snd_ad1816a *chip;
125 	struct snd_opl3 *opl3;
126 
127 	error = snd_card_new(&pcard->card->dev,
128 			     index[dev], id[dev], THIS_MODULE,
129 			     sizeof(struct snd_ad1816a), &card);
130 	if (error < 0)
131 		return error;
132 	chip = card->private_data;
133 
134 	error = snd_card_ad1816a_pnp(dev, pcard, pid);
135 	if (error) {
136 		snd_card_free(card);
137 		return error;
138 	}
139 
140 	error = snd_ad1816a_create(card, port[dev],
141 				   irq[dev],
142 				   dma1[dev],
143 				   dma2[dev],
144 				   chip);
145 	if (error) {
146 		snd_card_free(card);
147 		return error;
148 	}
149 	if (clockfreq[dev] >= 5000 && clockfreq[dev] <= 100000)
150 		chip->clock_freq = clockfreq[dev];
151 
152 	strcpy(card->driver, "AD1816A");
153 	strcpy(card->shortname, "ADI SoundPort AD1816A");
154 	sprintf(card->longname, "%s, SS at 0x%lx, irq %d, dma %d&%d",
155 		card->shortname, chip->port, irq[dev], dma1[dev], dma2[dev]);
156 
157 	error = snd_ad1816a_pcm(chip, 0);
158 	if (error < 0) {
159 		snd_card_free(card);
160 		return error;
161 	}
162 
163 	error = snd_ad1816a_mixer(chip);
164 	if (error < 0) {
165 		snd_card_free(card);
166 		return error;
167 	}
168 
169 	error = snd_ad1816a_timer(chip, 0);
170 	if (error < 0) {
171 		snd_card_free(card);
172 		return error;
173 	}
174 
175 	if (mpu_port[dev] > 0) {
176 		if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
177 					mpu_port[dev], 0, mpu_irq[dev],
178 					NULL) < 0)
179 			printk(KERN_ERR PFX "no MPU-401 device at 0x%lx.\n", mpu_port[dev]);
180 	}
181 
182 	if (fm_port[dev] > 0) {
183 		if (snd_opl3_create(card,
184 				    fm_port[dev], fm_port[dev] + 2,
185 				    OPL3_HW_AUTO, 0, &opl3) < 0) {
186 			printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx.\n", fm_port[dev], fm_port[dev] + 2);
187 		} else {
188 			error = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
189 			if (error < 0) {
190 				snd_card_free(card);
191 				return error;
192 			}
193 		}
194 	}
195 
196 	error = snd_card_register(card);
197 	if (error < 0) {
198 		snd_card_free(card);
199 		return error;
200 	}
201 	pnp_set_card_drvdata(pcard, card);
202 	return 0;
203 }
204 
205 static unsigned int ad1816a_devices;
206 
207 static int snd_ad1816a_pnp_detect(struct pnp_card_link *card,
208 				  const struct pnp_card_device_id *id)
209 {
210 	static int dev;
211 	int res;
212 
213 	for ( ; dev < SNDRV_CARDS; dev++) {
214 		if (!enable[dev])
215 			continue;
216 		res = snd_card_ad1816a_probe(dev, card, id);
217 		if (res < 0)
218 			return res;
219 		dev++;
220 		ad1816a_devices++;
221 		return 0;
222 	}
223         return -ENODEV;
224 }
225 
226 static void snd_ad1816a_pnp_remove(struct pnp_card_link *pcard)
227 {
228 	snd_card_free(pnp_get_card_drvdata(pcard));
229 	pnp_set_card_drvdata(pcard, NULL);
230 }
231 
232 #ifdef CONFIG_PM
233 static int snd_ad1816a_pnp_suspend(struct pnp_card_link *pcard,
234 				   pm_message_t state)
235 {
236 	struct snd_card *card = pnp_get_card_drvdata(pcard);
237 
238 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
239 	snd_ad1816a_suspend(card->private_data);
240 	return 0;
241 }
242 
243 static int snd_ad1816a_pnp_resume(struct pnp_card_link *pcard)
244 {
245 	struct snd_card *card = pnp_get_card_drvdata(pcard);
246 
247 	snd_ad1816a_resume(card->private_data);
248 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
249 	return 0;
250 }
251 #endif
252 
253 static struct pnp_card_driver ad1816a_pnpc_driver = {
254 	.flags		= PNP_DRIVER_RES_DISABLE,
255 	.name		= "ad1816a",
256 	.id_table	= snd_ad1816a_pnpids,
257 	.probe		= snd_ad1816a_pnp_detect,
258 	.remove		= snd_ad1816a_pnp_remove,
259 #ifdef CONFIG_PM
260 	.suspend	= snd_ad1816a_pnp_suspend,
261 	.resume		= snd_ad1816a_pnp_resume,
262 #endif
263 };
264 
265 static int __init alsa_card_ad1816a_init(void)
266 {
267 	int err;
268 
269 	err = pnp_register_card_driver(&ad1816a_pnpc_driver);
270 	if (err)
271 		return err;
272 
273 	if (!ad1816a_devices) {
274 		pnp_unregister_card_driver(&ad1816a_pnpc_driver);
275 #ifdef MODULE
276 		printk(KERN_ERR "no AD1816A based soundcards found.\n");
277 #endif	/* MODULE */
278 		return -ENODEV;
279 	}
280 	return 0;
281 }
282 
283 static void __exit alsa_card_ad1816a_exit(void)
284 {
285 	pnp_unregister_card_driver(&ad1816a_pnpc_driver);
286 }
287 
288 module_init(alsa_card_ad1816a_init)
289 module_exit(alsa_card_ad1816a_exit)
290