xref: /openbmc/linux/sound/drivers/mpu401/mpu401.c (revision 35267cea)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for generic MPU-401 boards (UART mode only)
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  *  Copyright (c) 2004 by Castet Matthieu <castet.matthieu@free.fr>
6  */
7 
8 #include <linux/init.h>
9 #include <linux/pnp.h>
10 #include <linux/err.h>
11 #include <linux/platform_device.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include <sound/mpu401.h>
15 #include <sound/initval.h>
16 
17 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
18 MODULE_DESCRIPTION("MPU-401 UART");
19 MODULE_LICENSE("GPL");
20 
21 static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
22 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
23 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;	/* Enable this card */
24 #ifdef CONFIG_PNP
25 static bool pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
26 #endif
27 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* MPU-401 port number */
28 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* MPU-401 IRQ */
29 static bool uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
30 
31 module_param_array(index, int, NULL, 0444);
32 MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
33 module_param_array(id, charp, NULL, 0444);
34 MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
35 module_param_array(enable, bool, NULL, 0444);
36 MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
37 #ifdef CONFIG_PNP
38 module_param_array(pnp, bool, NULL, 0444);
39 MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
40 #endif
41 module_param_hw_array(port, long, ioport, NULL, 0444);
42 MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
43 module_param_hw_array(irq, int, irq, NULL, 0444);
44 MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
45 module_param_array(uart_enter, bool, NULL, 0444);
46 MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
47 
48 static struct platform_device *platform_devices[SNDRV_CARDS];
49 static int pnp_registered;
50 static unsigned int snd_mpu401_devices;
51 
52 static int snd_mpu401_create(struct device *devptr, int dev,
53 			     struct snd_card **rcard)
54 {
55 	struct snd_card *card;
56 	int err;
57 
58 	if (!uart_enter[dev])
59 		snd_printk(KERN_ERR "the uart_enter option is obsolete; remove it\n");
60 
61 	*rcard = NULL;
62 	err = snd_card_new(devptr, index[dev], id[dev], THIS_MODULE,
63 			   0, &card);
64 	if (err < 0)
65 		return err;
66 	strcpy(card->driver, "MPU-401 UART");
67 	strcpy(card->shortname, card->driver);
68 	sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
69 	if (irq[dev] >= 0) {
70 		sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
71 	} else {
72 		strcat(card->longname, "polled");
73 	}
74 
75 	err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port[dev], 0,
76 				  irq[dev], NULL);
77 	if (err < 0) {
78 		printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
79 		goto _err;
80 	}
81 
82 	*rcard = card;
83 	return 0;
84 
85  _err:
86 	snd_card_free(card);
87 	return err;
88 }
89 
90 static int snd_mpu401_probe(struct platform_device *devptr)
91 {
92 	int dev = devptr->id;
93 	int err;
94 	struct snd_card *card;
95 
96 	if (port[dev] == SNDRV_AUTO_PORT) {
97 		snd_printk(KERN_ERR "specify port\n");
98 		return -EINVAL;
99 	}
100 	if (irq[dev] == SNDRV_AUTO_IRQ) {
101 		snd_printk(KERN_ERR "specify or disable IRQ\n");
102 		return -EINVAL;
103 	}
104 	err = snd_mpu401_create(&devptr->dev, dev, &card);
105 	if (err < 0)
106 		return err;
107 	err = snd_card_register(card);
108 	if (err < 0) {
109 		snd_card_free(card);
110 		return err;
111 	}
112 	platform_set_drvdata(devptr, card);
113 	return 0;
114 }
115 
116 static int snd_mpu401_remove(struct platform_device *devptr)
117 {
118 	snd_card_free(platform_get_drvdata(devptr));
119 	return 0;
120 }
121 
122 #define SND_MPU401_DRIVER	"snd_mpu401"
123 
124 static struct platform_driver snd_mpu401_driver = {
125 	.probe		= snd_mpu401_probe,
126 	.remove		= snd_mpu401_remove,
127 	.driver		= {
128 		.name	= SND_MPU401_DRIVER,
129 	},
130 };
131 
132 
133 #ifdef CONFIG_PNP
134 
135 #define IO_EXTENT 2
136 
137 static const struct pnp_device_id snd_mpu401_pnpids[] = {
138 	{ .id = "PNPb006" },
139 	{ .id = "" }
140 };
141 
142 MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
143 
144 static int snd_mpu401_pnp(int dev, struct pnp_dev *device,
145 			  const struct pnp_device_id *id)
146 {
147 	if (!pnp_port_valid(device, 0) ||
148 	    pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
149 		snd_printk(KERN_ERR "no PnP port\n");
150 		return -ENODEV;
151 	}
152 	if (pnp_port_len(device, 0) < IO_EXTENT) {
153 		snd_printk(KERN_ERR "PnP port length is %llu, expected %d\n",
154 			   (unsigned long long)pnp_port_len(device, 0),
155 			   IO_EXTENT);
156 		return -ENODEV;
157 	}
158 	port[dev] = pnp_port_start(device, 0);
159 
160 	if (!pnp_irq_valid(device, 0) ||
161 	    pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
162 		snd_printk(KERN_WARNING "no PnP irq, using polling\n");
163 		irq[dev] = -1;
164 	} else {
165 		irq[dev] = pnp_irq(device, 0);
166 	}
167 	return 0;
168 }
169 
170 static int snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
171 				const struct pnp_device_id *id)
172 {
173 	static int dev;
174 	struct snd_card *card;
175 	int err;
176 
177 	for ( ; dev < SNDRV_CARDS; ++dev) {
178 		if (!enable[dev] || !pnp[dev])
179 			continue;
180 		err = snd_mpu401_pnp(dev, pnp_dev, id);
181 		if (err < 0)
182 			return err;
183 		err = snd_mpu401_create(&pnp_dev->dev, dev, &card);
184 		if (err < 0)
185 			return err;
186 		err = snd_card_register(card);
187 		if (err < 0) {
188 			snd_card_free(card);
189 			return err;
190 		}
191 		pnp_set_drvdata(pnp_dev, card);
192 		snd_mpu401_devices++;
193 		++dev;
194 		return 0;
195 	}
196 	return -ENODEV;
197 }
198 
199 static void snd_mpu401_pnp_remove(struct pnp_dev *dev)
200 {
201 	struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
202 
203 	snd_card_disconnect(card);
204 	snd_card_free_when_closed(card);
205 }
206 
207 static struct pnp_driver snd_mpu401_pnp_driver = {
208 	.name = "mpu401",
209 	.id_table = snd_mpu401_pnpids,
210 	.probe = snd_mpu401_pnp_probe,
211 	.remove = snd_mpu401_pnp_remove,
212 };
213 #else
214 static struct pnp_driver snd_mpu401_pnp_driver;
215 #endif
216 
217 static void snd_mpu401_unregister_all(void)
218 {
219 	int i;
220 
221 	if (pnp_registered)
222 		pnp_unregister_driver(&snd_mpu401_pnp_driver);
223 	for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
224 		platform_device_unregister(platform_devices[i]);
225 	platform_driver_unregister(&snd_mpu401_driver);
226 }
227 
228 static int __init alsa_card_mpu401_init(void)
229 {
230 	int i, err;
231 
232 	err = platform_driver_register(&snd_mpu401_driver);
233 	if (err < 0)
234 		return err;
235 
236 	for (i = 0; i < SNDRV_CARDS; i++) {
237 		struct platform_device *device;
238 		if (! enable[i])
239 			continue;
240 #ifdef CONFIG_PNP
241 		if (pnp[i])
242 			continue;
243 #endif
244 		device = platform_device_register_simple(SND_MPU401_DRIVER,
245 							 i, NULL, 0);
246 		if (IS_ERR(device))
247 			continue;
248 		if (!platform_get_drvdata(device)) {
249 			platform_device_unregister(device);
250 			continue;
251 		}
252 		platform_devices[i] = device;
253 		snd_mpu401_devices++;
254 	}
255 	err = pnp_register_driver(&snd_mpu401_pnp_driver);
256 	if (!err)
257 		pnp_registered = 1;
258 
259 	if (!snd_mpu401_devices) {
260 #ifdef MODULE
261 		printk(KERN_ERR "MPU-401 device not found or device busy\n");
262 #endif
263 		snd_mpu401_unregister_all();
264 		return -ENODEV;
265 	}
266 	return 0;
267 }
268 
269 static void __exit alsa_card_mpu401_exit(void)
270 {
271 	snd_mpu401_unregister_all();
272 }
273 
274 module_init(alsa_card_mpu401_init)
275 module_exit(alsa_card_mpu401_exit)
276