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
73 if (type != EV_SND)
74 return -1;
75
76 switch (code) {
77 case SND_BELL: if (value) value = 1000;
78 case SND_TONE: break;
79 default: return -1;
80 }
81
82 if (value > 20 && value < 32767)
83 count = 1193182 / value;
84
85 count = bbc_count_to_reg(info, count);
86
87 guard(spinlock_irqsave)(&state->lock);
88
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);
97 }
98
99 return 0;
100 }
101
grover_spkr_event(struct input_dev * dev,unsigned int type,unsigned int code,int value)102 static int grover_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
103 {
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;
107
108 if (type != EV_SND)
109 return -1;
110
111 switch (code) {
112 case SND_BELL: if (value) value = 1000;
113 case SND_TONE: break;
114 default: return -1;
115 }
116
117 if (value > 20 && value < 32767)
118 count = 1193182 / value;
119
120 guard(spinlock_irqsave)(&state->lock);
121
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);
133 }
134
135 return 0;
136 }
137
sparcspkr_probe(struct device * dev)138 static int sparcspkr_probe(struct device *dev)
139 {
140 struct sparcspkr_state *state = dev_get_drvdata(dev);
141 struct input_dev *input_dev;
142 int error;
143
144 input_dev = input_allocate_device();
145 if (!input_dev)
146 return -ENOMEM;
147
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;
155
156 input_dev->evbit[0] = BIT_MASK(EV_SND);
157 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
158
159 input_dev->event = state->event;
160
161 error = input_register_device(input_dev);
162 if (error) {
163 input_free_device(input_dev);
164 return error;
165 }
166
167 state->input_dev = input_dev;
168
169 return 0;
170 }
171
sparcspkr_shutdown(struct platform_device * dev)172 static void sparcspkr_shutdown(struct platform_device *dev)
173 {
174 struct sparcspkr_state *state = platform_get_drvdata(dev);
175 struct input_dev *input_dev = state->input_dev;
176
177 /* turn off the speaker */
178 state->event(input_dev, EV_SND, SND_BELL, 0);
179 }
180
bbc_beep_probe(struct platform_device * op)181 static int bbc_beep_probe(struct platform_device *op)
182 {
183 struct sparcspkr_state *state;
184 struct bbc_beep_info *info;
185 int err;
186
187 state = devm_kzalloc(&op->dev, sizeof(*state), GFP_KERNEL);
188 if (!state)
189 return -ENOMEM;
190
191 state->name = "Sparc BBC Speaker";
192 state->event = bbc_spkr_event;
193 spin_lock_init(&state->lock);
194
195 struct device_node *dp __free(device_node) = of_find_node_by_path("/");
196 if (!dp)
197 return -ENODEV;
198
199 info = &state->u.bbc;
200 info->clock_freq = of_getintprop_default(dp, "clock-frequency", 0);
201 if (!info->clock_freq)
202 return -ENODEV;
203
204 info->regs = of_ioremap(&op->resource[0], 0, 6, "bbc beep");
205 if (!info->regs)
206 return -ENODEV;
207
208 platform_set_drvdata(op, state);
209
210 err = sparcspkr_probe(&op->dev);
211 if (err) {
212 of_iounmap(&op->resource[0], info->regs, 6);
213 return err;
214 }
215
216 return 0;
217 }
218
bbc_remove(struct platform_device * op)219 static void bbc_remove(struct platform_device *op)
220 {
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;
224
225 /* turn off the speaker */
226 state->event(input_dev, EV_SND, SND_BELL, 0);
227
228 input_unregister_device(input_dev);
229
230 of_iounmap(&op->resource[0], info->regs, 6);
231 }
232
233 static const struct of_device_id bbc_beep_match[] = {
234 {
235 .name = "beep",
236 .compatible = "SUNW,bbc-beep",
237 },
238 {},
239 };
240 MODULE_DEVICE_TABLE(of, bbc_beep_match);
241
242 static struct platform_driver bbc_beep_driver = {
243 .driver = {
244 .name = "bbcbeep",
245 .of_match_table = bbc_beep_match,
246 },
247 .probe = bbc_beep_probe,
248 .remove = bbc_remove,
249 .shutdown = sparcspkr_shutdown,
250 };
251
grover_beep_probe(struct platform_device * op)252 static int grover_beep_probe(struct platform_device *op)
253 {
254 struct sparcspkr_state *state;
255 struct grover_beep_info *info;
256 int err = -ENOMEM;
257
258 state = devm_kzalloc(&op->dev, sizeof(*state), GFP_KERNEL);
259 if (!state)
260 return err;
261
262 state->name = "Sparc Grover Speaker";
263 state->event = grover_spkr_event;
264 spin_lock_init(&state->lock);
265
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;
270
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;
274
275 platform_set_drvdata(op, state);
276
277 err = sparcspkr_probe(&op->dev);
278 if (err)
279 goto out_clear_drvdata;
280
281 return 0;
282
283 out_clear_drvdata:
284 of_iounmap(&op->resource[3], info->enable_reg, 1);
285
286 out_unmap_freq_regs:
287 of_iounmap(&op->resource[2], info->freq_regs, 2);
288
289 return err;
290 }
291
grover_remove(struct platform_device * op)292 static void grover_remove(struct platform_device *op)
293 {
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;
297
298 /* turn off the speaker */
299 state->event(input_dev, EV_SND, SND_BELL, 0);
300
301 input_unregister_device(input_dev);
302
303 of_iounmap(&op->resource[3], info->enable_reg, 1);
304 of_iounmap(&op->resource[2], info->freq_regs, 2);
305 }
306
307 static const struct of_device_id grover_beep_match[] = {
308 {
309 .name = "beep",
310 .compatible = "SUNW,smbus-beep",
311 },
312 {},
313 };
314 MODULE_DEVICE_TABLE(of, grover_beep_match);
315
316 static struct platform_driver grover_beep_driver = {
317 .driver = {
318 .name = "groverbeep",
319 .of_match_table = grover_beep_match,
320 },
321 .probe = grover_beep_probe,
322 .remove = grover_remove,
323 .shutdown = sparcspkr_shutdown,
324 };
325
326 static struct platform_driver * const drivers[] = {
327 &bbc_beep_driver,
328 &grover_beep_driver,
329 };
330
sparcspkr_init(void)331 static int __init sparcspkr_init(void)
332 {
333 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
334 }
335
sparcspkr_exit(void)336 static void __exit sparcspkr_exit(void)
337 {
338 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
339 }
340
341 module_init(sparcspkr_init);
342 module_exit(sparcspkr_exit);
343