fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / drivers / input / misc / sparcspkr.c
blobe36ec1d92be895a4279e12f88d7654d2eddc466d
1 /*
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)
6 */
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/input.h>
11 #include <linux/platform_device.h>
13 #include <asm/io.h>
14 #include <asm/ebus.h>
15 #include <asm/isa.h>
17 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
18 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
19 MODULE_LICENSE("GPL");
21 struct sparcspkr_state {
22 const char *name;
23 unsigned long iobase;
24 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
25 spinlock_t lock;
26 struct input_dev *input_dev;
29 static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
31 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
32 unsigned int count = 0;
33 unsigned long flags;
35 if (type != EV_SND)
36 return -1;
38 switch (code) {
39 case SND_BELL: if (value) value = 1000;
40 case SND_TONE: break;
41 default: return -1;
44 if (value > 20 && value < 32767)
45 count = 1193182 / value;
47 spin_lock_irqsave(&state->lock, flags);
49 /* EBUS speaker only has on/off state, the frequency does not
50 * appear to be programmable.
52 if (state->iobase & 0x2UL)
53 outb(!!count, state->iobase);
54 else
55 outl(!!count, state->iobase);
57 spin_unlock_irqrestore(&state->lock, flags);
59 return 0;
62 static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
64 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
65 unsigned int count = 0;
66 unsigned long flags;
68 if (type != EV_SND)
69 return -1;
71 switch (code) {
72 case SND_BELL: if (value) value = 1000;
73 case SND_TONE: break;
74 default: return -1;
77 if (value > 20 && value < 32767)
78 count = 1193182 / value;
80 spin_lock_irqsave(&state->lock, flags);
82 if (count) {
83 /* enable counter 2 */
84 outb(inb(state->iobase + 0x61) | 3, state->iobase + 0x61);
85 /* set command for counter 2, 2 byte write */
86 outb(0xB6, state->iobase + 0x43);
87 /* select desired HZ */
88 outb(count & 0xff, state->iobase + 0x42);
89 outb((count >> 8) & 0xff, state->iobase + 0x42);
90 } else {
91 /* disable counter 2 */
92 outb(inb_p(state->iobase + 0x61) & 0xFC, state->iobase + 0x61);
95 spin_unlock_irqrestore(&state->lock, flags);
97 return 0;
100 static int __devinit sparcspkr_probe(struct device *dev)
102 struct sparcspkr_state *state = dev_get_drvdata(dev);
103 struct input_dev *input_dev;
104 int error;
106 input_dev = input_allocate_device();
107 if (!input_dev)
108 return -ENOMEM;
110 input_dev->name = state->name;
111 input_dev->phys = "sparc/input0";
112 input_dev->id.bustype = BUS_ISA;
113 input_dev->id.vendor = 0x001f;
114 input_dev->id.product = 0x0001;
115 input_dev->id.version = 0x0100;
116 input_dev->dev.parent = dev;
118 input_dev->evbit[0] = BIT(EV_SND);
119 input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
121 input_dev->event = state->event;
123 error = input_register_device(input_dev);
124 if (error) {
125 input_free_device(input_dev);
126 return error;
129 state->input_dev = input_dev;
131 return 0;
134 static int __devexit sparcspkr_remove(struct of_device *dev)
136 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
137 struct input_dev *input_dev = state->input_dev;
139 /* turn off the speaker */
140 state->event(input_dev, EV_SND, SND_BELL, 0);
142 input_unregister_device(input_dev);
144 dev_set_drvdata(&dev->dev, NULL);
145 kfree(state);
147 return 0;
150 static int sparcspkr_shutdown(struct of_device *dev)
152 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
153 struct input_dev *input_dev = state->input_dev;
155 /* turn off the speaker */
156 state->event(input_dev, EV_SND, SND_BELL, 0);
158 return 0;
161 static int __devinit ebus_beep_probe(struct of_device *dev, const struct of_device_id *match)
163 struct linux_ebus_device *edev = to_ebus_device(&dev->dev);
164 struct sparcspkr_state *state;
165 int err;
167 state = kzalloc(sizeof(*state), GFP_KERNEL);
168 if (!state)
169 return -ENOMEM;
171 state->name = "Sparc EBUS Speaker";
172 state->iobase = edev->resource[0].start;
173 state->event = ebus_spkr_event;
174 spin_lock_init(&state->lock);
176 dev_set_drvdata(&dev->dev, state);
178 err = sparcspkr_probe(&dev->dev);
179 if (err) {
180 dev_set_drvdata(&dev->dev, NULL);
181 kfree(state);
184 return 0;
187 static struct of_device_id ebus_beep_match[] = {
189 .name = "beep",
194 static struct of_platform_driver ebus_beep_driver = {
195 .name = "beep",
196 .match_table = ebus_beep_match,
197 .probe = ebus_beep_probe,
198 .remove = sparcspkr_remove,
199 .shutdown = sparcspkr_shutdown,
202 static int __devinit isa_beep_probe(struct of_device *dev, const struct of_device_id *match)
204 struct sparc_isa_device *idev = to_isa_device(&dev->dev);
205 struct sparcspkr_state *state;
206 int err;
208 state = kzalloc(sizeof(*state), GFP_KERNEL);
209 if (!state)
210 return -ENOMEM;
212 state->name = "Sparc ISA Speaker";
213 state->iobase = idev->resource.start;
214 state->event = isa_spkr_event;
215 spin_lock_init(&state->lock);
217 dev_set_drvdata(&dev->dev, state);
219 err = sparcspkr_probe(&dev->dev);
220 if (err) {
221 dev_set_drvdata(&dev->dev, NULL);
222 kfree(state);
225 return 0;
228 static struct of_device_id isa_beep_match[] = {
230 .name = "dma",
235 static struct of_platform_driver isa_beep_driver = {
236 .name = "beep",
237 .match_table = isa_beep_match,
238 .probe = isa_beep_probe,
239 .remove = sparcspkr_remove,
240 .shutdown = sparcspkr_shutdown,
243 static int __init sparcspkr_init(void)
245 int err = of_register_driver(&ebus_beep_driver, &ebus_bus_type);
247 if (!err) {
248 err = of_register_driver(&isa_beep_driver, &isa_bus_type);
249 if (err)
250 of_unregister_driver(&ebus_beep_driver);
253 return err;
256 static void __exit sparcspkr_exit(void)
258 of_unregister_driver(&ebus_beep_driver);
259 of_unregister_driver(&isa_beep_driver);
262 module_init(sparcspkr_init);
263 module_exit(sparcspkr_exit);