Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / input / misc / sparcspkr.c
blob8d7303fc13bce3cbc67b9ae4c3078f5b722dd4af
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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)
7 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/input.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
16 #include <asm/io.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 {
28 u32 clock_freq;
29 void __iomem *regs;
32 struct sparcspkr_state {
33 const char *name;
34 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
35 spinlock_t lock;
36 struct input_dev *input_dev;
37 union {
38 struct grover_beep_info grover;
39 struct bbc_beep_info bbc;
40 } u;
43 static u32 bbc_count_to_reg(struct bbc_beep_info *info, unsigned int count)
45 u32 val, clock_freq = info->clock_freq;
46 int i;
48 if (!count)
49 return 0;
51 if (count <= clock_freq >> 20)
52 return 1 << 18;
54 if (count >= clock_freq >> 12)
55 return 1 << 10;
57 val = 1 << 18;
58 for (i = 19; i >= 11; i--) {
59 val >>= 1;
60 if (count <= clock_freq >> i)
61 break;
64 return val;
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;
73 if (type != EV_SND)
74 return -1;
76 switch (code) {
77 case SND_BELL: if (value) value = 1000;
78 case SND_TONE: break;
79 default: return -1;
82 if (value > 20 && value < 32767)
83 count = 1193182 / value;
85 count = bbc_count_to_reg(info, count);
87 guard(spinlock_irqsave)(&state->lock);
89 if (count) {
90 sbus_writeb(0x01, info->regs + 0);
91 sbus_writeb(0x00, info->regs + 2);
92 sbus_writeb((count >> 16) & 0xff, info->regs + 3);
93 sbus_writeb((count >> 8) & 0xff, info->regs + 4);
94 sbus_writeb(0x00, info->regs + 5);
95 } else {
96 sbus_writeb(0x00, info->regs + 0);
99 return 0;
102 static int grover_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
104 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
105 struct grover_beep_info *info = &state->u.grover;
106 unsigned int count = 0;
108 if (type != EV_SND)
109 return -1;
111 switch (code) {
112 case SND_BELL: if (value) value = 1000;
113 case SND_TONE: break;
114 default: return -1;
117 if (value > 20 && value < 32767)
118 count = 1193182 / value;
120 guard(spinlock_irqsave)(&state->lock);
122 if (count) {
123 /* enable counter 2 */
124 sbus_writeb(sbus_readb(info->enable_reg) | 3, info->enable_reg);
125 /* set command for counter 2, 2 byte write */
126 sbus_writeb(0xB6, info->freq_regs + 1);
127 /* select desired HZ */
128 sbus_writeb(count & 0xff, info->freq_regs + 0);
129 sbus_writeb((count >> 8) & 0xff, info->freq_regs + 0);
130 } else {
131 /* disable counter 2 */
132 sbus_writeb(sbus_readb(info->enable_reg) & 0xFC, info->enable_reg);
135 return 0;
138 static int sparcspkr_probe(struct device *dev)
140 struct sparcspkr_state *state = dev_get_drvdata(dev);
141 struct input_dev *input_dev;
142 int error;
144 input_dev = input_allocate_device();
145 if (!input_dev)
146 return -ENOMEM;
148 input_dev->name = state->name;
149 input_dev->phys = "sparc/input0";
150 input_dev->id.bustype = BUS_ISA;
151 input_dev->id.vendor = 0x001f;
152 input_dev->id.product = 0x0001;
153 input_dev->id.version = 0x0100;
154 input_dev->dev.parent = dev;
156 input_dev->evbit[0] = BIT_MASK(EV_SND);
157 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
159 input_dev->event = state->event;
161 error = input_register_device(input_dev);
162 if (error) {
163 input_free_device(input_dev);
164 return error;
167 state->input_dev = input_dev;
169 return 0;
172 static void sparcspkr_shutdown(struct platform_device *dev)
174 struct sparcspkr_state *state = platform_get_drvdata(dev);
175 struct input_dev *input_dev = state->input_dev;
177 /* turn off the speaker */
178 state->event(input_dev, EV_SND, SND_BELL, 0);
181 static int bbc_beep_probe(struct platform_device *op)
183 struct sparcspkr_state *state;
184 struct bbc_beep_info *info;
185 int err;
187 state = devm_kzalloc(&op->dev, sizeof(*state), GFP_KERNEL);
188 if (!state)
189 return -ENOMEM;
191 state->name = "Sparc BBC Speaker";
192 state->event = bbc_spkr_event;
193 spin_lock_init(&state->lock);
195 struct device_node *dp __free(device_node) = of_find_node_by_path("/");
196 if (!dp)
197 return -ENODEV;
199 info = &state->u.bbc;
200 info->clock_freq = of_getintprop_default(dp, "clock-frequency", 0);
201 if (!info->clock_freq)
202 return -ENODEV;
204 info->regs = of_ioremap(&op->resource[0], 0, 6, "bbc beep");
205 if (!info->regs)
206 return -ENODEV;
208 platform_set_drvdata(op, state);
210 err = sparcspkr_probe(&op->dev);
211 if (err) {
212 of_iounmap(&op->resource[0], info->regs, 6);
213 return err;
216 return 0;
219 static void bbc_remove(struct platform_device *op)
221 struct sparcspkr_state *state = platform_get_drvdata(op);
222 struct input_dev *input_dev = state->input_dev;
223 struct bbc_beep_info *info = &state->u.bbc;
225 /* turn off the speaker */
226 state->event(input_dev, EV_SND, SND_BELL, 0);
228 input_unregister_device(input_dev);
230 of_iounmap(&op->resource[0], info->regs, 6);
233 static const struct of_device_id bbc_beep_match[] = {
235 .name = "beep",
236 .compatible = "SUNW,bbc-beep",
240 MODULE_DEVICE_TABLE(of, bbc_beep_match);
242 static struct platform_driver bbc_beep_driver = {
243 .driver = {
244 .name = "bbcbeep",
245 .of_match_table = bbc_beep_match,
247 .probe = bbc_beep_probe,
248 .remove = bbc_remove,
249 .shutdown = sparcspkr_shutdown,
252 static int grover_beep_probe(struct platform_device *op)
254 struct sparcspkr_state *state;
255 struct grover_beep_info *info;
256 int err = -ENOMEM;
258 state = devm_kzalloc(&op->dev, sizeof(*state), GFP_KERNEL);
259 if (!state)
260 return err;
262 state->name = "Sparc Grover Speaker";
263 state->event = grover_spkr_event;
264 spin_lock_init(&state->lock);
266 info = &state->u.grover;
267 info->freq_regs = of_ioremap(&op->resource[2], 0, 2, "grover beep freq");
268 if (!info->freq_regs)
269 return err;
271 info->enable_reg = of_ioremap(&op->resource[3], 0, 1, "grover beep enable");
272 if (!info->enable_reg)
273 goto out_unmap_freq_regs;
275 platform_set_drvdata(op, state);
277 err = sparcspkr_probe(&op->dev);
278 if (err)
279 goto out_clear_drvdata;
281 return 0;
283 out_clear_drvdata:
284 of_iounmap(&op->resource[3], info->enable_reg, 1);
286 out_unmap_freq_regs:
287 of_iounmap(&op->resource[2], info->freq_regs, 2);
289 return err;
292 static void grover_remove(struct platform_device *op)
294 struct sparcspkr_state *state = platform_get_drvdata(op);
295 struct grover_beep_info *info = &state->u.grover;
296 struct input_dev *input_dev = state->input_dev;
298 /* turn off the speaker */
299 state->event(input_dev, EV_SND, SND_BELL, 0);
301 input_unregister_device(input_dev);
303 of_iounmap(&op->resource[3], info->enable_reg, 1);
304 of_iounmap(&op->resource[2], info->freq_regs, 2);
307 static const struct of_device_id grover_beep_match[] = {
309 .name = "beep",
310 .compatible = "SUNW,smbus-beep",
314 MODULE_DEVICE_TABLE(of, grover_beep_match);
316 static struct platform_driver grover_beep_driver = {
317 .driver = {
318 .name = "groverbeep",
319 .of_match_table = grover_beep_match,
321 .probe = grover_beep_probe,
322 .remove = grover_remove,
323 .shutdown = sparcspkr_shutdown,
326 static struct platform_driver * const drivers[] = {
327 &bbc_beep_driver,
328 &grover_beep_driver,
331 static int __init sparcspkr_init(void)
333 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
336 static void __exit sparcspkr_exit(void)
338 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
341 module_init(sparcspkr_init);
342 module_exit(sparcspkr_exit);