1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for PC-speaker like devices found on various Sparc systems.
5 * Copyright (c) 2002 Vojtech Pavlik
6 * Copyright (c) 2002, 2006, 2008 David S. Miller (davem@davemloft.net)
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/input.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
18 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
19 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
20 MODULE_LICENSE("GPL");
22 struct grover_beep_info {
23 void __iomem *freq_regs;
24 void __iomem *enable_reg;
27 struct bbc_beep_info {
32 struct sparcspkr_state {
34 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
36 struct input_dev *input_dev;
38 struct grover_beep_info grover;
39 struct bbc_beep_info bbc;
43 static u32 bbc_count_to_reg(struct bbc_beep_info *info, unsigned int count)
45 u32 val, clock_freq = info->clock_freq;
51 if (count <= clock_freq >> 20)
54 if (count >= clock_freq >> 12)
58 for (i = 19; i >= 11; i--) {
60 if (count <= clock_freq >> i)
67 static int bbc_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
69 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
70 struct bbc_beep_info *info = &state->u.bbc;
71 unsigned int count = 0;
78 case SND_BELL: if (value) value = 1000;
83 if (value > 20 && value < 32767)
84 count = 1193182 / value;
86 count = bbc_count_to_reg(info, count);
88 spin_lock_irqsave(&state->lock, flags);
91 sbus_writeb(0x01, info->regs + 0);
92 sbus_writeb(0x00, info->regs + 2);
93 sbus_writeb((count >> 16) & 0xff, info->regs + 3);
94 sbus_writeb((count >> 8) & 0xff, info->regs + 4);
95 sbus_writeb(0x00, info->regs + 5);
97 sbus_writeb(0x00, info->regs + 0);
100 spin_unlock_irqrestore(&state->lock, flags);
105 static int grover_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
107 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
108 struct grover_beep_info *info = &state->u.grover;
109 unsigned int count = 0;
116 case SND_BELL: if (value) value = 1000;
117 case SND_TONE: break;
121 if (value > 20 && value < 32767)
122 count = 1193182 / value;
124 spin_lock_irqsave(&state->lock, flags);
127 /* enable counter 2 */
128 sbus_writeb(sbus_readb(info->enable_reg) | 3, info->enable_reg);
129 /* set command for counter 2, 2 byte write */
130 sbus_writeb(0xB6, info->freq_regs + 1);
131 /* select desired HZ */
132 sbus_writeb(count & 0xff, info->freq_regs + 0);
133 sbus_writeb((count >> 8) & 0xff, info->freq_regs + 0);
135 /* disable counter 2 */
136 sbus_writeb(sbus_readb(info->enable_reg) & 0xFC, info->enable_reg);
139 spin_unlock_irqrestore(&state->lock, flags);
144 static int sparcspkr_probe(struct device *dev)
146 struct sparcspkr_state *state = dev_get_drvdata(dev);
147 struct input_dev *input_dev;
150 input_dev = input_allocate_device();
154 input_dev->name = state->name;
155 input_dev->phys = "sparc/input0";
156 input_dev->id.bustype = BUS_ISA;
157 input_dev->id.vendor = 0x001f;
158 input_dev->id.product = 0x0001;
159 input_dev->id.version = 0x0100;
160 input_dev->dev.parent = dev;
162 input_dev->evbit[0] = BIT_MASK(EV_SND);
163 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
165 input_dev->event = state->event;
167 error = input_register_device(input_dev);
169 input_free_device(input_dev);
173 state->input_dev = input_dev;
178 static void sparcspkr_shutdown(struct platform_device *dev)
180 struct sparcspkr_state *state = platform_get_drvdata(dev);
181 struct input_dev *input_dev = state->input_dev;
183 /* turn off the speaker */
184 state->event(input_dev, EV_SND, SND_BELL, 0);
187 static int bbc_beep_probe(struct platform_device *op)
189 struct sparcspkr_state *state;
190 struct bbc_beep_info *info;
191 struct device_node *dp;
194 state = kzalloc(sizeof(*state), GFP_KERNEL);
198 state->name = "Sparc BBC Speaker";
199 state->event = bbc_spkr_event;
200 spin_lock_init(&state->lock);
202 dp = of_find_node_by_path("/");
207 info = &state->u.bbc;
208 info->clock_freq = of_getintprop_default(dp, "clock-frequency", 0);
210 if (!info->clock_freq)
213 info->regs = of_ioremap(&op->resource[0], 0, 6, "bbc beep");
217 platform_set_drvdata(op, state);
219 err = sparcspkr_probe(&op->dev);
221 goto out_clear_drvdata;
226 of_iounmap(&op->resource[0], info->regs, 6);
234 static int bbc_remove(struct platform_device *op)
236 struct sparcspkr_state *state = platform_get_drvdata(op);
237 struct input_dev *input_dev = state->input_dev;
238 struct bbc_beep_info *info = &state->u.bbc;
240 /* turn off the speaker */
241 state->event(input_dev, EV_SND, SND_BELL, 0);
243 input_unregister_device(input_dev);
245 of_iounmap(&op->resource[0], info->regs, 6);
252 static const struct of_device_id bbc_beep_match[] = {
255 .compatible = "SUNW,bbc-beep",
259 MODULE_DEVICE_TABLE(of, bbc_beep_match);
261 static struct platform_driver bbc_beep_driver = {
264 .of_match_table = bbc_beep_match,
266 .probe = bbc_beep_probe,
267 .remove = bbc_remove,
268 .shutdown = sparcspkr_shutdown,
271 static int grover_beep_probe(struct platform_device *op)
273 struct sparcspkr_state *state;
274 struct grover_beep_info *info;
277 state = kzalloc(sizeof(*state), GFP_KERNEL);
281 state->name = "Sparc Grover Speaker";
282 state->event = grover_spkr_event;
283 spin_lock_init(&state->lock);
285 info = &state->u.grover;
286 info->freq_regs = of_ioremap(&op->resource[2], 0, 2, "grover beep freq");
287 if (!info->freq_regs)
290 info->enable_reg = of_ioremap(&op->resource[3], 0, 1, "grover beep enable");
291 if (!info->enable_reg)
292 goto out_unmap_freq_regs;
294 platform_set_drvdata(op, state);
296 err = sparcspkr_probe(&op->dev);
298 goto out_clear_drvdata;
303 of_iounmap(&op->resource[3], info->enable_reg, 1);
306 of_iounmap(&op->resource[2], info->freq_regs, 2);
313 static int grover_remove(struct platform_device *op)
315 struct sparcspkr_state *state = platform_get_drvdata(op);
316 struct grover_beep_info *info = &state->u.grover;
317 struct input_dev *input_dev = state->input_dev;
319 /* turn off the speaker */
320 state->event(input_dev, EV_SND, SND_BELL, 0);
322 input_unregister_device(input_dev);
324 of_iounmap(&op->resource[3], info->enable_reg, 1);
325 of_iounmap(&op->resource[2], info->freq_regs, 2);
332 static const struct of_device_id grover_beep_match[] = {
335 .compatible = "SUNW,smbus-beep",
339 MODULE_DEVICE_TABLE(of, grover_beep_match);
341 static struct platform_driver grover_beep_driver = {
343 .name = "groverbeep",
344 .of_match_table = grover_beep_match,
346 .probe = grover_beep_probe,
347 .remove = grover_remove,
348 .shutdown = sparcspkr_shutdown,
351 static struct platform_driver * const drivers[] = {
356 static int __init sparcspkr_init(void)
358 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
361 static void __exit sparcspkr_exit(void)
363 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
366 module_init(sparcspkr_init);
367 module_exit(sparcspkr_exit);