2 * Driver for PC-speaker like devices found on various Sparc systems.
4 * Copyright (c) 2002 Vojtech Pavlik
5 * Copyright (c) 2002, 2006 David S. Miller (davem@davemloft.net)
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/input.h>
12 #include <linux/platform_device.h>
18 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
19 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
20 MODULE_LICENSE("GPL");
22 struct sparcspkr_state {
25 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
27 struct input_dev *input_dev;
30 static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
32 struct sparcspkr_state *state = dev_get_drvdata(dev->cdev.dev);
33 unsigned int count = 0;
40 case SND_BELL: if (value) value = 1000;
45 if (value > 20 && value < 32767)
46 count = 1193182 / value;
48 spin_lock_irqsave(&state->lock, flags);
50 /* EBUS speaker only has on/off state, the frequency does not
51 * appear to be programmable.
53 if (state->iobase & 0x2UL)
54 outb(!!count, state->iobase);
56 outl(!!count, state->iobase);
58 spin_unlock_irqrestore(&state->lock, flags);
63 static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
65 struct sparcspkr_state *state = dev_get_drvdata(dev->cdev.dev);
66 unsigned int count = 0;
73 case SND_BELL: if (value) value = 1000;
78 if (value > 20 && value < 32767)
79 count = 1193182 / value;
81 spin_lock_irqsave(&state->lock, flags);
84 /* enable counter 2 */
85 outb(inb(state->iobase + 0x61) | 3, state->iobase + 0x61);
86 /* set command for counter 2, 2 byte write */
87 outb(0xB6, state->iobase + 0x43);
88 /* select desired HZ */
89 outb(count & 0xff, state->iobase + 0x42);
90 outb((count >> 8) & 0xff, state->iobase + 0x42);
92 /* disable counter 2 */
93 outb(inb_p(state->iobase + 0x61) & 0xFC, state->iobase + 0x61);
96 spin_unlock_irqrestore(&state->lock, flags);
101 static int __devinit sparcspkr_probe(struct device *dev)
103 struct sparcspkr_state *state = dev_get_drvdata(dev);
104 struct input_dev *input_dev;
107 input_dev = input_allocate_device();
111 input_dev->name = state->name;
112 input_dev->phys = "sparc/input0";
113 input_dev->id.bustype = BUS_ISA;
114 input_dev->id.vendor = 0x001f;
115 input_dev->id.product = 0x0001;
116 input_dev->id.version = 0x0100;
117 input_dev->cdev.dev = dev;
119 input_dev->evbit[0] = BIT(EV_SND);
120 input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
122 input_dev->event = state->event;
124 error = input_register_device(input_dev);
126 input_free_device(input_dev);
130 state->input_dev = input_dev;
135 static int __devexit sparcspkr_remove(struct of_device *dev)
137 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
138 struct input_dev *input_dev = state->input_dev;
140 /* turn off the speaker */
141 state->event(input_dev, EV_SND, SND_BELL, 0);
143 input_unregister_device(input_dev);
145 dev_set_drvdata(&dev->dev, NULL);
151 static int sparcspkr_shutdown(struct of_device *dev)
153 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
154 struct input_dev *input_dev = state->input_dev;
156 /* turn off the speaker */
157 state->event(input_dev, EV_SND, SND_BELL, 0);
162 static int __devinit ebus_beep_probe(struct of_device *dev, const struct of_device_id *match)
164 struct linux_ebus_device *edev = to_ebus_device(&dev->dev);
165 struct sparcspkr_state *state;
168 state = kzalloc(sizeof(*state), GFP_KERNEL);
172 state->name = "Sparc EBUS Speaker";
173 state->iobase = edev->resource[0].start;
174 state->event = ebus_spkr_event;
175 spin_lock_init(&state->lock);
177 dev_set_drvdata(&dev->dev, state);
179 err = sparcspkr_probe(&dev->dev);
181 dev_set_drvdata(&dev->dev, NULL);
188 static struct of_device_id ebus_beep_match[] = {
195 static struct of_platform_driver ebus_beep_driver = {
197 .match_table = ebus_beep_match,
198 .probe = ebus_beep_probe,
199 .remove = sparcspkr_remove,
200 .shutdown = sparcspkr_shutdown,
203 static int __devinit isa_beep_probe(struct of_device *dev, const struct of_device_id *match)
205 struct sparc_isa_device *idev = to_isa_device(&dev->dev);
206 struct sparcspkr_state *state;
209 state = kzalloc(sizeof(*state), GFP_KERNEL);
213 state->name = "Sparc ISA Speaker";
214 state->iobase = idev->resource.start;
215 state->event = isa_spkr_event;
216 spin_lock_init(&state->lock);
218 dev_set_drvdata(&dev->dev, state);
220 err = sparcspkr_probe(&dev->dev);
222 dev_set_drvdata(&dev->dev, NULL);
229 static struct of_device_id isa_beep_match[] = {
236 static struct of_platform_driver isa_beep_driver = {
238 .match_table = isa_beep_match,
239 .probe = isa_beep_probe,
240 .remove = sparcspkr_remove,
241 .shutdown = sparcspkr_shutdown,
244 static int __init sparcspkr_init(void)
246 int err = of_register_driver(&ebus_beep_driver, &ebus_bus_type);
249 err = of_register_driver(&isa_beep_driver, &isa_bus_type);
251 of_unregister_driver(&ebus_beep_driver);
257 static void __exit sparcspkr_exit(void)
259 of_unregister_driver(&ebus_beep_driver);
260 of_unregister_driver(&isa_beep_driver);
263 module_init(sparcspkr_init);
264 module_exit(sparcspkr_exit);