1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for PC-speaker like devices found on various Sparc systems.
4 *
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>
15
16 #include <asm/io.h>
17
18 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
19 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
20 MODULE_LICENSE("GPL");
21
22 struct grover_beep_info {
23 void __iomem *freq_regs;
24 void __iomem *enable_reg;
25 };
26
27 struct bbc_beep_info {
28 u32 clock_freq;
29 void __iomem *regs;
30 };
31
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;
41 };
42
bbc_count_to_reg(struct bbc_beep_info * info,unsigned int count)43 static u32 bbc_count_to_reg(struct bbc_beep_info *info, unsigned int count)
44 {
45 u32 val, clock_freq = info->clock_freq;
46 int i;
47
48 if (!count)
49 return 0;
50
51 if (count <= clock_freq >> 20)
52 return 1 << 18;
53
54 if (count >= clock_freq >> 12)
55 return 1 << 10;
56
57 val = 1 << 18;
58 for (i = 19; i >= 11; i--) {
59 val >>= 1;
60 if (count <= clock_freq >> i)
61 break;
62 }
63
64 return val;
65 }
66
bbc_spkr_event(struct input_dev * dev,unsigned int type,unsigned int code,int value)67 static int bbc_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
68 {
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;
72 unsigned long flags;
73
74 if (type != EV_SND)
75 return -1;
76
77 switch (code) {
78 case SND_BELL: if (value) value = 1000;
79 case SND_TONE: break;
80 default: return -1;
81 }
82
83 if (value > 20 && value < 32767)
84 count = 1193182 / value;
85
86 count = bbc_count_to_reg(info, count);
87
88 spin_lock_irqsave(&state->lock, flags);
89
90 if (count) {
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);
96 } else {
97 sbus_writeb(0x00, info->regs + 0);
98 }
99
100 spin_unlock_irqrestore(&state->lock, flags);
101
102 return 0;
103 }
104
grover_spkr_event(struct input_dev * dev,unsigned int type,unsigned int code,int value)105 static int grover_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
106 {
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;
110 unsigned long flags;
111
112 if (type != EV_SND)
113 return -1;
114
115 switch (code) {
116 case SND_BELL: if (value) value = 1000;
117 case SND_TONE: break;
118 default: return -1;
119 }
120
121 if (value > 20 && value < 32767)
122 count = 1193182 / value;
123
124 spin_lock_irqsave(&state->lock, flags);
125
126 if (count) {
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);
134 } else {
135 /* disable counter 2 */
136 sbus_writeb(sbus_readb(info->enable_reg) & 0xFC, info->enable_reg);
137 }
138
139 spin_unlock_irqrestore(&state->lock, flags);
140
141 return 0;
142 }
143
sparcspkr_probe(struct device * dev)144 static int sparcspkr_probe(struct device *dev)
145 {
146 struct sparcspkr_state *state = dev_get_drvdata(dev);
147 struct input_dev *input_dev;
148 int error;
149
150 input_dev = input_allocate_device();
151 if (!input_dev)
152 return -ENOMEM;
153
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;
161
162 input_dev->evbit[0] = BIT_MASK(EV_SND);
163 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
164
165 input_dev->event = state->event;
166
167 error = input_register_device(input_dev);
168 if (error) {
169 input_free_device(input_dev);
170 return error;
171 }
172
173 state->input_dev = input_dev;
174
175 return 0;
176 }
177
sparcspkr_shutdown(struct platform_device * dev)178 static void sparcspkr_shutdown(struct platform_device *dev)
179 {
180 struct sparcspkr_state *state = platform_get_drvdata(dev);
181 struct input_dev *input_dev = state->input_dev;
182
183 /* turn off the speaker */
184 state->event(input_dev, EV_SND, SND_BELL, 0);
185 }
186
bbc_beep_probe(struct platform_device * op)187 static int bbc_beep_probe(struct platform_device *op)
188 {
189 struct sparcspkr_state *state;
190 struct bbc_beep_info *info;
191 struct device_node *dp;
192 int err = -ENOMEM;
193
194 state = kzalloc(sizeof(*state), GFP_KERNEL);
195 if (!state)
196 goto out_err;
197
198 state->name = "Sparc BBC Speaker";
199 state->event = bbc_spkr_event;
200 spin_lock_init(&state->lock);
201
202 dp = of_find_node_by_path("/");
203 err = -ENODEV;
204 if (!dp)
205 goto out_free;
206
207 info = &state->u.bbc;
208 info->clock_freq = of_getintprop_default(dp, "clock-frequency", 0);
209 of_node_put(dp);
210 if (!info->clock_freq)
211 goto out_free;
212
213 info->regs = of_ioremap(&op->resource[0], 0, 6, "bbc beep");
214 if (!info->regs)
215 goto out_free;
216
217 platform_set_drvdata(op, state);
218
219 err = sparcspkr_probe(&op->dev);
220 if (err)
221 goto out_clear_drvdata;
222
223 return 0;
224
225 out_clear_drvdata:
226 of_iounmap(&op->resource[0], info->regs, 6);
227
228 out_free:
229 kfree(state);
230 out_err:
231 return err;
232 }
233
bbc_remove(struct platform_device * op)234 static void bbc_remove(struct platform_device *op)
235 {
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;
239
240 /* turn off the speaker */
241 state->event(input_dev, EV_SND, SND_BELL, 0);
242
243 input_unregister_device(input_dev);
244
245 of_iounmap(&op->resource[0], info->regs, 6);
246
247 kfree(state);
248 }
249
250 static const struct of_device_id bbc_beep_match[] = {
251 {
252 .name = "beep",
253 .compatible = "SUNW,bbc-beep",
254 },
255 {},
256 };
257 MODULE_DEVICE_TABLE(of, bbc_beep_match);
258
259 static struct platform_driver bbc_beep_driver = {
260 .driver = {
261 .name = "bbcbeep",
262 .of_match_table = bbc_beep_match,
263 },
264 .probe = bbc_beep_probe,
265 .remove_new = bbc_remove,
266 .shutdown = sparcspkr_shutdown,
267 };
268
grover_beep_probe(struct platform_device * op)269 static int grover_beep_probe(struct platform_device *op)
270 {
271 struct sparcspkr_state *state;
272 struct grover_beep_info *info;
273 int err = -ENOMEM;
274
275 state = kzalloc(sizeof(*state), GFP_KERNEL);
276 if (!state)
277 goto out_err;
278
279 state->name = "Sparc Grover Speaker";
280 state->event = grover_spkr_event;
281 spin_lock_init(&state->lock);
282
283 info = &state->u.grover;
284 info->freq_regs = of_ioremap(&op->resource[2], 0, 2, "grover beep freq");
285 if (!info->freq_regs)
286 goto out_free;
287
288 info->enable_reg = of_ioremap(&op->resource[3], 0, 1, "grover beep enable");
289 if (!info->enable_reg)
290 goto out_unmap_freq_regs;
291
292 platform_set_drvdata(op, state);
293
294 err = sparcspkr_probe(&op->dev);
295 if (err)
296 goto out_clear_drvdata;
297
298 return 0;
299
300 out_clear_drvdata:
301 of_iounmap(&op->resource[3], info->enable_reg, 1);
302
303 out_unmap_freq_regs:
304 of_iounmap(&op->resource[2], info->freq_regs, 2);
305 out_free:
306 kfree(state);
307 out_err:
308 return err;
309 }
310
grover_remove(struct platform_device * op)311 static void grover_remove(struct platform_device *op)
312 {
313 struct sparcspkr_state *state = platform_get_drvdata(op);
314 struct grover_beep_info *info = &state->u.grover;
315 struct input_dev *input_dev = state->input_dev;
316
317 /* turn off the speaker */
318 state->event(input_dev, EV_SND, SND_BELL, 0);
319
320 input_unregister_device(input_dev);
321
322 of_iounmap(&op->resource[3], info->enable_reg, 1);
323 of_iounmap(&op->resource[2], info->freq_regs, 2);
324
325 kfree(state);
326 }
327
328 static const struct of_device_id grover_beep_match[] = {
329 {
330 .name = "beep",
331 .compatible = "SUNW,smbus-beep",
332 },
333 {},
334 };
335 MODULE_DEVICE_TABLE(of, grover_beep_match);
336
337 static struct platform_driver grover_beep_driver = {
338 .driver = {
339 .name = "groverbeep",
340 .of_match_table = grover_beep_match,
341 },
342 .probe = grover_beep_probe,
343 .remove_new = grover_remove,
344 .shutdown = sparcspkr_shutdown,
345 };
346
347 static struct platform_driver * const drivers[] = {
348 &bbc_beep_driver,
349 &grover_beep_driver,
350 };
351
sparcspkr_init(void)352 static int __init sparcspkr_init(void)
353 {
354 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
355 }
356
sparcspkr_exit(void)357 static void __exit sparcspkr_exit(void)
358 {
359 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
360 }
361
362 module_init(sparcspkr_init);
363 module_exit(sparcspkr_exit);
364