Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Driver for PC-speaker like devices found on various Sparc systems. | |
3 | * | |
4 | * Copyright (c) 2002 Vojtech Pavlik | |
9c1a5077 | 5 | * Copyright (c) 2002, 2006, 2008 David S. Miller (davem@davemloft.net) |
1da177e4 | 6 | */ |
1da177e4 LT |
7 | #include <linux/kernel.h> |
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/input.h> | |
9c1a5077 | 11 | #include <linux/of_device.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
1da177e4 LT |
13 | |
14 | #include <asm/io.h> | |
1da177e4 | 15 | |
a2bd4fd1 | 16 | MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); |
76b7cddf | 17 | MODULE_DESCRIPTION("Sparc Speaker beeper driver"); |
1da177e4 LT |
18 | MODULE_LICENSE("GPL"); |
19 | ||
9c1a5077 DM |
20 | struct grover_beep_info { |
21 | void __iomem *freq_regs; | |
22 | void __iomem *enable_reg; | |
23 | }; | |
24 | ||
25 | struct bbc_beep_info { | |
26 | u32 clock_freq; | |
27 | void __iomem *regs; | |
28 | }; | |
29 | ||
a2bd4fd1 DM |
30 | struct sparcspkr_state { |
31 | const char *name; | |
a2bd4fd1 DM |
32 | int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value); |
33 | spinlock_t lock; | |
34 | struct input_dev *input_dev; | |
9c1a5077 DM |
35 | union { |
36 | struct grover_beep_info grover; | |
37 | struct bbc_beep_info bbc; | |
38 | } u; | |
a2bd4fd1 | 39 | }; |
1da177e4 | 40 | |
9c1a5077 DM |
41 | static u32 bbc_count_to_reg(struct bbc_beep_info *info, unsigned int count) |
42 | { | |
43 | u32 val, clock_freq = info->clock_freq; | |
44 | int i; | |
45 | ||
46 | if (!count) | |
47 | return 0; | |
48 | ||
49 | if (count <= clock_freq >> 20) | |
50 | return 1 << 18; | |
51 | ||
52 | if (count >= clock_freq >> 12) | |
53 | return 1 << 10; | |
54 | ||
55 | val = 1 << 18; | |
56 | for (i = 19; i >= 11; i--) { | |
57 | val >>= 1; | |
58 | if (count <= clock_freq >> i) | |
59 | break; | |
60 | } | |
61 | ||
62 | return val; | |
63 | } | |
64 | ||
65 | static int bbc_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) | |
1da177e4 | 66 | { |
293e6392 | 67 | struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent); |
9c1a5077 | 68 | struct bbc_beep_info *info = &state->u.bbc; |
1da177e4 LT |
69 | unsigned int count = 0; |
70 | unsigned long flags; | |
71 | ||
72 | if (type != EV_SND) | |
73 | return -1; | |
74 | ||
75 | switch (code) { | |
76 | case SND_BELL: if (value) value = 1000; | |
77 | case SND_TONE: break; | |
78 | default: return -1; | |
79 | } | |
80 | ||
81 | if (value > 20 && value < 32767) | |
82 | count = 1193182 / value; | |
83 | ||
9c1a5077 DM |
84 | count = bbc_count_to_reg(info, count); |
85 | ||
a2bd4fd1 | 86 | spin_lock_irqsave(&state->lock, flags); |
1da177e4 | 87 | |
9c1a5077 DM |
88 | if (count) { |
89 | outb(0x01, info->regs + 0); | |
90 | outb(0x00, info->regs + 2); | |
91 | outb((count >> 16) & 0xff, info->regs + 3); | |
92 | outb((count >> 8) & 0xff, info->regs + 4); | |
93 | outb(0x00, info->regs + 5); | |
94 | } else { | |
95 | outb(0x00, info->regs + 0); | |
96 | } | |
1da177e4 | 97 | |
a2bd4fd1 | 98 | spin_unlock_irqrestore(&state->lock, flags); |
1da177e4 LT |
99 | |
100 | return 0; | |
101 | } | |
102 | ||
9c1a5077 | 103 | static int grover_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
1da177e4 | 104 | { |
293e6392 | 105 | struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent); |
9c1a5077 | 106 | struct grover_beep_info *info = &state->u.grover; |
1da177e4 LT |
107 | unsigned int count = 0; |
108 | unsigned long flags; | |
109 | ||
110 | if (type != EV_SND) | |
111 | return -1; | |
112 | ||
113 | switch (code) { | |
114 | case SND_BELL: if (value) value = 1000; | |
115 | case SND_TONE: break; | |
116 | default: return -1; | |
117 | } | |
118 | ||
119 | if (value > 20 && value < 32767) | |
120 | count = 1193182 / value; | |
121 | ||
a2bd4fd1 | 122 | spin_lock_irqsave(&state->lock, flags); |
1da177e4 LT |
123 | |
124 | if (count) { | |
125 | /* enable counter 2 */ | |
9c1a5077 | 126 | outb(inb(info->enable_reg) | 3, info->enable_reg); |
1da177e4 | 127 | /* set command for counter 2, 2 byte write */ |
9c1a5077 | 128 | outb(0xB6, info->freq_regs + 1); |
1da177e4 | 129 | /* select desired HZ */ |
9c1a5077 DM |
130 | outb(count & 0xff, info->freq_regs + 0); |
131 | outb((count >> 8) & 0xff, info->freq_regs + 0); | |
1da177e4 LT |
132 | } else { |
133 | /* disable counter 2 */ | |
9c1a5077 | 134 | outb(inb_p(info->enable_reg) & 0xFC, info->enable_reg); |
1da177e4 LT |
135 | } |
136 | ||
a2bd4fd1 | 137 | spin_unlock_irqrestore(&state->lock, flags); |
1da177e4 LT |
138 | |
139 | return 0; | |
140 | } | |
141 | ||
a2bd4fd1 | 142 | static int __devinit sparcspkr_probe(struct device *dev) |
1da177e4 | 143 | { |
a2bd4fd1 | 144 | struct sparcspkr_state *state = dev_get_drvdata(dev); |
f5b64078 DT |
145 | struct input_dev *input_dev; |
146 | int error; | |
1da177e4 | 147 | |
f5b64078 DT |
148 | input_dev = input_allocate_device(); |
149 | if (!input_dev) | |
76b7cddf DT |
150 | return -ENOMEM; |
151 | ||
a2bd4fd1 | 152 | input_dev->name = state->name; |
f5b64078 DT |
153 | input_dev->phys = "sparc/input0"; |
154 | input_dev->id.bustype = BUS_ISA; | |
155 | input_dev->id.vendor = 0x001f; | |
156 | input_dev->id.product = 0x0001; | |
157 | input_dev->id.version = 0x0100; | |
293e6392 | 158 | input_dev->dev.parent = dev; |
1da177e4 | 159 | |
7b19ada2 JS |
160 | input_dev->evbit[0] = BIT_MASK(EV_SND); |
161 | input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE); | |
1da177e4 | 162 | |
a2bd4fd1 | 163 | input_dev->event = state->event; |
f5b64078 DT |
164 | |
165 | error = input_register_device(input_dev); | |
166 | if (error) { | |
167 | input_free_device(input_dev); | |
168 | return error; | |
169 | } | |
170 | ||
a2bd4fd1 | 171 | state->input_dev = input_dev; |
1da177e4 | 172 | |
1da177e4 LT |
173 | return 0; |
174 | } | |
f5b64078 | 175 | |
4ebb24f7 | 176 | static void sparcspkr_shutdown(struct platform_device *dev) |
f5b64078 | 177 | { |
a2bd4fd1 DM |
178 | struct sparcspkr_state *state = dev_get_drvdata(&dev->dev); |
179 | struct input_dev *input_dev = state->input_dev; | |
f5b64078 | 180 | |
f5b64078 | 181 | /* turn off the speaker */ |
a2bd4fd1 | 182 | state->event(input_dev, EV_SND, SND_BELL, 0); |
f5b64078 DT |
183 | } |
184 | ||
4ebb24f7 | 185 | static int __devinit bbc_beep_probe(struct platform_device *op) |
f5b64078 | 186 | { |
9c1a5077 DM |
187 | struct sparcspkr_state *state; |
188 | struct bbc_beep_info *info; | |
189 | struct device_node *dp; | |
190 | int err = -ENOMEM; | |
a2bd4fd1 | 191 | |
9c1a5077 DM |
192 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
193 | if (!state) | |
194 | goto out_err; | |
195 | ||
196 | state->name = "Sparc BBC Speaker"; | |
197 | state->event = bbc_spkr_event; | |
198 | spin_lock_init(&state->lock); | |
199 | ||
200 | dp = of_find_node_by_path("/"); | |
201 | err = -ENODEV; | |
202 | if (!dp) | |
203 | goto out_free; | |
204 | ||
205 | info = &state->u.bbc; | |
206 | info->clock_freq = of_getintprop_default(dp, "clock-frequency", 0); | |
207 | if (!info->clock_freq) | |
208 | goto out_free; | |
209 | ||
210 | info->regs = of_ioremap(&op->resource[0], 0, 6, "bbc beep"); | |
211 | if (!info->regs) | |
212 | goto out_free; | |
213 | ||
214 | dev_set_drvdata(&op->dev, state); | |
215 | ||
216 | err = sparcspkr_probe(&op->dev); | |
217 | if (err) | |
218 | goto out_clear_drvdata; | |
a2bd4fd1 DM |
219 | |
220 | return 0; | |
9c1a5077 DM |
221 | |
222 | out_clear_drvdata: | |
223 | dev_set_drvdata(&op->dev, NULL); | |
224 | of_iounmap(&op->resource[0], info->regs, 6); | |
225 | ||
226 | out_free: | |
227 | kfree(state); | |
228 | out_err: | |
229 | return err; | |
a2bd4fd1 DM |
230 | } |
231 | ||
2dc11581 | 232 | static int __devexit bbc_remove(struct platform_device *op) |
a2bd4fd1 | 233 | { |
9c1a5077 DM |
234 | struct sparcspkr_state *state = dev_get_drvdata(&op->dev); |
235 | struct input_dev *input_dev = state->input_dev; | |
236 | struct bbc_beep_info *info = &state->u.bbc; | |
a2bd4fd1 | 237 | |
9c1a5077 DM |
238 | /* turn off the speaker */ |
239 | state->event(input_dev, EV_SND, SND_BELL, 0); | |
a2bd4fd1 | 240 | |
9c1a5077 | 241 | input_unregister_device(input_dev); |
a2bd4fd1 | 242 | |
9c1a5077 | 243 | of_iounmap(&op->resource[0], info->regs, 6); |
a2bd4fd1 | 244 | |
9c1a5077 DM |
245 | dev_set_drvdata(&op->dev, NULL); |
246 | kfree(state); | |
a2bd4fd1 DM |
247 | |
248 | return 0; | |
f5b64078 DT |
249 | } |
250 | ||
fd098316 | 251 | static const struct of_device_id bbc_beep_match[] = { |
a2bd4fd1 DM |
252 | { |
253 | .name = "beep", | |
9c1a5077 | 254 | .compatible = "SUNW,bbc-beep", |
f5b64078 | 255 | }, |
a2bd4fd1 | 256 | {}, |
f5b64078 DT |
257 | }; |
258 | ||
4ebb24f7 | 259 | static struct platform_driver bbc_beep_driver = { |
4018294b GL |
260 | .driver = { |
261 | .name = "bbcbeep", | |
262 | .owner = THIS_MODULE, | |
263 | .of_match_table = bbc_beep_match, | |
264 | }, | |
9c1a5077 | 265 | .probe = bbc_beep_probe, |
1cb0aa88 | 266 | .remove = bbc_remove, |
a2bd4fd1 DM |
267 | .shutdown = sparcspkr_shutdown, |
268 | }; | |
f5b64078 | 269 | |
4ebb24f7 | 270 | static int __devinit grover_beep_probe(struct platform_device *op) |
f5b64078 | 271 | { |
a2bd4fd1 | 272 | struct sparcspkr_state *state; |
9c1a5077 DM |
273 | struct grover_beep_info *info; |
274 | int err = -ENOMEM; | |
f5b64078 | 275 | |
a2bd4fd1 DM |
276 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
277 | if (!state) | |
9c1a5077 | 278 | goto out_err; |
f5b64078 | 279 | |
9c1a5077 DM |
280 | state->name = "Sparc Grover Speaker"; |
281 | state->event = grover_spkr_event; | |
a2bd4fd1 DM |
282 | spin_lock_init(&state->lock); |
283 | ||
9c1a5077 DM |
284 | info = &state->u.grover; |
285 | info->freq_regs = of_ioremap(&op->resource[2], 0, 2, "grover beep freq"); | |
286 | if (!info->freq_regs) | |
287 | goto out_free; | |
f5b64078 | 288 | |
9c1a5077 DM |
289 | info->enable_reg = of_ioremap(&op->resource[3], 0, 1, "grover beep enable"); |
290 | if (!info->enable_reg) | |
291 | goto out_unmap_freq_regs; | |
292 | ||
293 | dev_set_drvdata(&op->dev, state); | |
294 | ||
295 | err = sparcspkr_probe(&op->dev); | |
296 | if (err) | |
297 | goto out_clear_drvdata; | |
298 | ||
299 | return 0; | |
300 | ||
301 | out_clear_drvdata: | |
302 | dev_set_drvdata(&op->dev, NULL); | |
303 | of_iounmap(&op->resource[3], info->enable_reg, 1); | |
304 | ||
305 | out_unmap_freq_regs: | |
306 | of_iounmap(&op->resource[2], info->freq_regs, 2); | |
307 | out_free: | |
308 | kfree(state); | |
309 | out_err: | |
310 | return err; | |
311 | } | |
312 | ||
2dc11581 | 313 | static int __devexit grover_remove(struct platform_device *op) |
9c1a5077 DM |
314 | { |
315 | struct sparcspkr_state *state = dev_get_drvdata(&op->dev); | |
316 | struct grover_beep_info *info = &state->u.grover; | |
317 | struct input_dev *input_dev = state->input_dev; | |
318 | ||
319 | /* turn off the speaker */ | |
320 | state->event(input_dev, EV_SND, SND_BELL, 0); | |
321 | ||
322 | input_unregister_device(input_dev); | |
323 | ||
324 | of_iounmap(&op->resource[3], info->enable_reg, 1); | |
325 | of_iounmap(&op->resource[2], info->freq_regs, 2); | |
326 | ||
327 | dev_set_drvdata(&op->dev, NULL); | |
328 | kfree(state); | |
f5b64078 DT |
329 | |
330 | return 0; | |
a2bd4fd1 | 331 | } |
f5b64078 | 332 | |
fd098316 | 333 | static const struct of_device_id grover_beep_match[] = { |
a2bd4fd1 | 334 | { |
9c1a5077 DM |
335 | .name = "beep", |
336 | .compatible = "SUNW,smbus-beep", | |
a2bd4fd1 DM |
337 | }, |
338 | {}, | |
339 | }; | |
f5b64078 | 340 | |
4ebb24f7 | 341 | static struct platform_driver grover_beep_driver = { |
4018294b GL |
342 | .driver = { |
343 | .name = "groverbeep", | |
344 | .owner = THIS_MODULE, | |
345 | .of_match_table = grover_beep_match, | |
346 | }, | |
9c1a5077 | 347 | .probe = grover_beep_probe, |
1cb0aa88 | 348 | .remove = grover_remove, |
a2bd4fd1 DM |
349 | .shutdown = sparcspkr_shutdown, |
350 | }; | |
1da177e4 LT |
351 | |
352 | static int __init sparcspkr_init(void) | |
353 | { | |
4ebb24f7 | 354 | int err = platform_driver_register(&bbc_beep_driver); |
a2bd4fd1 DM |
355 | |
356 | if (!err) { | |
4ebb24f7 | 357 | err = platform_driver_register(&grover_beep_driver); |
a2bd4fd1 | 358 | if (err) |
4ebb24f7 | 359 | platform_driver_unregister(&bbc_beep_driver); |
1da177e4 | 360 | } |
1da177e4 | 361 | |
a2bd4fd1 | 362 | return err; |
1da177e4 LT |
363 | } |
364 | ||
365 | static void __exit sparcspkr_exit(void) | |
366 | { | |
4ebb24f7 GL |
367 | platform_driver_unregister(&bbc_beep_driver); |
368 | platform_driver_unregister(&grover_beep_driver); | |
1da177e4 LT |
369 | } |
370 | ||
371 | module_init(sparcspkr_init); | |
372 | module_exit(sparcspkr_exit); |