xref: /openbmc/linux/sound/drivers/mpu401/mpu401.c (revision c2cd9d04)
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 	if ((err = snd_card_register(card)) < 0) {
108 		snd_card_free(card);
109 		return err;
110 	}
111 	platform_set_drvdata(devptr, card);
112 	return 0;
113 }
114 
115 static int snd_mpu401_remove(struct platform_device *devptr)
116 {
117 	snd_card_free(platform_get_drvdata(devptr));
118 	return 0;
119 }
120 
121 #define SND_MPU401_DRIVER	"snd_mpu401"
122 
123 static struct platform_driver snd_mpu401_driver = {
124 	.probe		= snd_mpu401_probe,
125 	.remove		= snd_mpu401_remove,
126 	.driver		= {
127 		.name	= SND_MPU401_DRIVER,
128 	},
129 };
130 
131 
132 #ifdef CONFIG_PNP
133 
134 #define IO_EXTENT 2
135 
136 static const struct pnp_device_id snd_mpu401_pnpids[] = {
137 	{ .id = "PNPb006" },
138 	{ .id = "" }
139 };
140 
141 MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
142 
143 static int snd_mpu401_pnp(int dev, struct pnp_dev *device,
144 			  const struct pnp_device_id *id)
145 {
146 	if (!pnp_port_valid(device, 0) ||
147 	    pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
148 		snd_printk(KERN_ERR "no PnP port\n");
149 		return -ENODEV;
150 	}
151 	if (pnp_port_len(device, 0) < IO_EXTENT) {
152 		snd_printk(KERN_ERR "PnP port length is %llu, expected %d\n",
153 			   (unsigned long long)pnp_port_len(device, 0),
154 			   IO_EXTENT);
155 		return -ENODEV;
156 	}
157 	port[dev] = pnp_port_start(device, 0);
158 
159 	if (!pnp_irq_valid(device, 0) ||
160 	    pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
161 		snd_printk(KERN_WARNING "no PnP irq, using polling\n");
162 		irq[dev] = -1;
163 	} else {
164 		irq[dev] = pnp_irq(device, 0);
165 	}
166 	return 0;
167 }
168 
169 static int snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
170 				const struct pnp_device_id *id)
171 {
172 	static int dev;
173 	struct snd_card *card;
174 	int err;
175 
176 	for ( ; dev < SNDRV_CARDS; ++dev) {
177 		if (!enable[dev] || !pnp[dev])
178 			continue;
179 		err = snd_mpu401_pnp(dev, pnp_dev, id);
180 		if (err < 0)
181 			return err;
182 		err = snd_mpu401_create(&pnp_dev->dev, dev, &card);
183 		if (err < 0)
184 			return err;
185 		if ((err = snd_card_register(card)) < 0) {
186 			snd_card_free(card);
187 			return err;
188 		}
189 		pnp_set_drvdata(pnp_dev, card);
190 		snd_mpu401_devices++;
191 		++dev;
192 		return 0;
193 	}
194 	return -ENODEV;
195 }
196 
197 static void snd_mpu401_pnp_remove(struct pnp_dev *dev)
198 {
199 	struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
200 
201 	snd_card_disconnect(card);
202 	snd_card_free_when_closed(card);
203 }
204 
205 static struct pnp_driver snd_mpu401_pnp_driver = {
206 	.name = "mpu401",
207 	.id_table = snd_mpu401_pnpids,
208 	.probe = snd_mpu401_pnp_probe,
209 	.remove = snd_mpu401_pnp_remove,
210 };
211 #else
212 static struct pnp_driver snd_mpu401_pnp_driver;
213 #endif
214 
215 static void snd_mpu401_unregister_all(void)
216 {
217 	int i;
218 
219 	if (pnp_registered)
220 		pnp_unregister_driver(&snd_mpu401_pnp_driver);
221 	for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
222 		platform_device_unregister(platform_devices[i]);
223 	platform_driver_unregister(&snd_mpu401_driver);
224 }
225 
226 static int __init alsa_card_mpu401_init(void)
227 {
228 	int i, err;
229 
230 	if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
231 		return err;
232 
233 	for (i = 0; i < SNDRV_CARDS; i++) {
234 		struct platform_device *device;
235 		if (! enable[i])
236 			continue;
237 #ifdef CONFIG_PNP
238 		if (pnp[i])
239 			continue;
240 #endif
241 		device = platform_device_register_simple(SND_MPU401_DRIVER,
242 							 i, NULL, 0);
243 		if (IS_ERR(device))
244 			continue;
245 		if (!platform_get_drvdata(device)) {
246 			platform_device_unregister(device);
247 			continue;
248 		}
249 		platform_devices[i] = device;
250 		snd_mpu401_devices++;
251 	}
252 	err = pnp_register_driver(&snd_mpu401_pnp_driver);
253 	if (!err)
254 		pnp_registered = 1;
255 
256 	if (!snd_mpu401_devices) {
257 #ifdef MODULE
258 		printk(KERN_ERR "MPU-401 device not found or device busy\n");
259 #endif
260 		snd_mpu401_unregister_all();
261 		return -ENODEV;
262 	}
263 	return 0;
264 }
265 
266 static void __exit alsa_card_mpu401_exit(void)
267 {
268 	snd_mpu401_unregister_all();
269 }
270 
271 module_init(alsa_card_mpu401_init)
272 module_exit(alsa_card_mpu401_exit)
273