blob: 179ff1cd6f6b39a5437b5cbf81d45a131af4b438 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for PC-speaker like devices found on various Sparc systems.
3 *
4 * Copyright (c) 2002 Vojtech Pavlik
David S. Miller9c1a5072008-04-26 21:02:21 -07005 * Copyright (c) 2002, 2006, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/input.h>
David S. Miller9c1a5072008-04-26 21:02:21 -070011#include <linux/of_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
David S. Millera2bd4fd2006-06-23 01:44:10 -070016MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -050017MODULE_DESCRIPTION("Sparc Speaker beeper driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070018MODULE_LICENSE("GPL");
19
David S. Miller9c1a5072008-04-26 21:02:21 -070020struct grover_beep_info {
21 void __iomem *freq_regs;
22 void __iomem *enable_reg;
23};
24
25struct bbc_beep_info {
26 u32 clock_freq;
27 void __iomem *regs;
28};
29
David S. Millera2bd4fd2006-06-23 01:44:10 -070030struct sparcspkr_state {
31 const char *name;
David S. Millera2bd4fd2006-06-23 01:44:10 -070032 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
33 spinlock_t lock;
34 struct input_dev *input_dev;
David S. Miller9c1a5072008-04-26 21:02:21 -070035 union {
36 struct grover_beep_info grover;
37 struct bbc_beep_info bbc;
38 } u;
David S. Millera2bd4fd2006-06-23 01:44:10 -070039};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David S. Miller9c1a5072008-04-26 21:02:21 -070041static 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
65static int bbc_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Dmitry Torokhov293e6392007-04-12 01:35:32 -040067 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
David S. Miller9c1a5072008-04-26 21:02:21 -070068 struct bbc_beep_info *info = &state->u.bbc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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
David S. Miller9c1a5072008-04-26 21:02:21 -070084 count = bbc_count_to_reg(info, count);
85
David S. Millera2bd4fd2006-06-23 01:44:10 -070086 spin_lock_irqsave(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
David S. Miller9c1a5072008-04-26 21:02:21 -070088 if (count) {
Sam Ravnborgd6504712014-07-20 13:38:58 +020089 sbus_writeb(0x01, info->regs + 0);
90 sbus_writeb(0x00, info->regs + 2);
91 sbus_writeb((count >> 16) & 0xff, info->regs + 3);
92 sbus_writeb((count >> 8) & 0xff, info->regs + 4);
93 sbus_writeb(0x00, info->regs + 5);
David S. Miller9c1a5072008-04-26 21:02:21 -070094 } else {
Sam Ravnborgd6504712014-07-20 13:38:58 +020095 sbus_writeb(0x00, info->regs + 0);
David S. Miller9c1a5072008-04-26 21:02:21 -070096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David S. Millera2bd4fd2006-06-23 01:44:10 -070098 spin_unlock_irqrestore(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 return 0;
101}
102
David S. Miller9c1a5072008-04-26 21:02:21 -0700103static int grover_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Dmitry Torokhov293e6392007-04-12 01:35:32 -0400105 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
David S. Miller9c1a5072008-04-26 21:02:21 -0700106 struct grover_beep_info *info = &state->u.grover;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 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
David S. Millera2bd4fd2006-06-23 01:44:10 -0700122 spin_lock_irqsave(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (count) {
125 /* enable counter 2 */
Sam Ravnborgd6504712014-07-20 13:38:58 +0200126 sbus_writeb(sbus_readb(info->enable_reg) | 3, info->enable_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* set command for counter 2, 2 byte write */
Sam Ravnborgd6504712014-07-20 13:38:58 +0200128 sbus_writeb(0xB6, info->freq_regs + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* select desired HZ */
Sam Ravnborgd6504712014-07-20 13:38:58 +0200130 sbus_writeb(count & 0xff, info->freq_regs + 0);
131 sbus_writeb((count >> 8) & 0xff, info->freq_regs + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } else {
133 /* disable counter 2 */
Sam Ravnborgd6504712014-07-20 13:38:58 +0200134 sbus_writeb(sbus_readb(info->enable_reg) & 0xFC, info->enable_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
David S. Millera2bd4fd2006-06-23 01:44:10 -0700137 spin_unlock_irqrestore(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 return 0;
140}
141
Bill Pemberton5298cc42012-11-23 21:38:25 -0800142static int sparcspkr_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700144 struct sparcspkr_state *state = dev_get_drvdata(dev);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500145 struct input_dev *input_dev;
146 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500148 input_dev = input_allocate_device();
149 if (!input_dev)
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -0500150 return -ENOMEM;
151
David S. Millera2bd4fd2006-06-23 01:44:10 -0700152 input_dev->name = state->name;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500153 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;
Dmitry Torokhov293e6392007-04-12 01:35:32 -0400158 input_dev->dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700160 input_dev->evbit[0] = BIT_MASK(EV_SND);
161 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David S. Millera2bd4fd2006-06-23 01:44:10 -0700163 input_dev->event = state->event;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500164
165 error = input_register_device(input_dev);
166 if (error) {
167 input_free_device(input_dev);
168 return error;
169 }
170
David S. Millera2bd4fd2006-06-23 01:44:10 -0700171 state->input_dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500175
Grant Likely4ebb24f2011-02-22 20:01:33 -0700176static void sparcspkr_shutdown(struct platform_device *dev)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500177{
Jingoo Han35c4b122013-05-23 09:20:21 -0700178 struct sparcspkr_state *state = platform_get_drvdata(dev);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700179 struct input_dev *input_dev = state->input_dev;
180
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500181 /* turn off the speaker */
David S. Millera2bd4fd2006-06-23 01:44:10 -0700182 state->event(input_dev, EV_SND, SND_BELL, 0);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500183}
184
Bill Pemberton5298cc42012-11-23 21:38:25 -0800185static int bbc_beep_probe(struct platform_device *op)
David S. Millera2bd4fd2006-06-23 01:44:10 -0700186{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700187 struct sparcspkr_state *state;
David S. Miller9c1a5072008-04-26 21:02:21 -0700188 struct bbc_beep_info *info;
189 struct device_node *dp;
190 int err = -ENOMEM;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700191
192 state = kzalloc(sizeof(*state), GFP_KERNEL);
193 if (!state)
David S. Miller9c1a5072008-04-26 21:02:21 -0700194 goto out_err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700195
David S. Miller9c1a5072008-04-26 21:02:21 -0700196 state->name = "Sparc BBC Speaker";
197 state->event = bbc_spkr_event;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700198 spin_lock_init(&state->lock);
199
David S. Miller9c1a5072008-04-26 21:02:21 -0700200 dp = of_find_node_by_path("/");
201 err = -ENODEV;
202 if (!dp)
203 goto out_free;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700204
David S. Miller9c1a5072008-04-26 21:02:21 -0700205 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
Jingoo Han35c4b122013-05-23 09:20:21 -0700214 platform_set_drvdata(op, state);
David S. Miller9c1a5072008-04-26 21:02:21 -0700215
216 err = sparcspkr_probe(&op->dev);
217 if (err)
218 goto out_clear_drvdata;
219
220 return 0;
221
222out_clear_drvdata:
David S. Miller9c1a5072008-04-26 21:02:21 -0700223 of_iounmap(&op->resource[0], info->regs, 6);
224
225out_free:
226 kfree(state);
227out_err:
228 return err;
229}
230
Bill Pembertone2619cf2012-11-23 21:50:47 -0800231static int bbc_remove(struct platform_device *op)
David S. Miller9c1a5072008-04-26 21:02:21 -0700232{
Jingoo Han35c4b122013-05-23 09:20:21 -0700233 struct sparcspkr_state *state = platform_get_drvdata(op);
David S. Miller9c1a5072008-04-26 21:02:21 -0700234 struct input_dev *input_dev = state->input_dev;
235 struct bbc_beep_info *info = &state->u.bbc;
236
237 /* turn off the speaker */
238 state->event(input_dev, EV_SND, SND_BELL, 0);
239
240 input_unregister_device(input_dev);
241
242 of_iounmap(&op->resource[0], info->regs, 6);
243
David S. Miller9c1a5072008-04-26 21:02:21 -0700244 kfree(state);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700245
246 return 0;
247}
248
David S. Millerfd098312008-08-31 01:23:17 -0700249static const struct of_device_id bbc_beep_match[] = {
David S. Millera2bd4fd2006-06-23 01:44:10 -0700250 {
251 .name = "beep",
David S. Miller9c1a5072008-04-26 21:02:21 -0700252 .compatible = "SUNW,bbc-beep",
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500253 },
David S. Millera2bd4fd2006-06-23 01:44:10 -0700254 {},
255};
256
Grant Likely4ebb24f2011-02-22 20:01:33 -0700257static struct platform_driver bbc_beep_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700258 .driver = {
259 .name = "bbcbeep",
260 .owner = THIS_MODULE,
261 .of_match_table = bbc_beep_match,
262 },
David S. Miller9c1a5072008-04-26 21:02:21 -0700263 .probe = bbc_beep_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800264 .remove = bbc_remove,
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500265 .shutdown = sparcspkr_shutdown,
266};
267
Bill Pemberton5298cc42012-11-23 21:38:25 -0800268static int grover_beep_probe(struct platform_device *op)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500269{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700270 struct sparcspkr_state *state;
David S. Miller9c1a5072008-04-26 21:02:21 -0700271 struct grover_beep_info *info;
272 int err = -ENOMEM;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500273
David S. Millera2bd4fd2006-06-23 01:44:10 -0700274 state = kzalloc(sizeof(*state), GFP_KERNEL);
275 if (!state)
David S. Miller9c1a5072008-04-26 21:02:21 -0700276 goto out_err;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500277
David S. Miller9c1a5072008-04-26 21:02:21 -0700278 state->name = "Sparc Grover Speaker";
279 state->event = grover_spkr_event;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700280 spin_lock_init(&state->lock);
281
David S. Miller9c1a5072008-04-26 21:02:21 -0700282 info = &state->u.grover;
283 info->freq_regs = of_ioremap(&op->resource[2], 0, 2, "grover beep freq");
284 if (!info->freq_regs)
285 goto out_free;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700286
David S. Miller9c1a5072008-04-26 21:02:21 -0700287 info->enable_reg = of_ioremap(&op->resource[3], 0, 1, "grover beep enable");
288 if (!info->enable_reg)
289 goto out_unmap_freq_regs;
290
Jingoo Han35c4b122013-05-23 09:20:21 -0700291 platform_set_drvdata(op, state);
David S. Miller9c1a5072008-04-26 21:02:21 -0700292
293 err = sparcspkr_probe(&op->dev);
294 if (err)
295 goto out_clear_drvdata;
296
297 return 0;
298
299out_clear_drvdata:
David S. Miller9c1a5072008-04-26 21:02:21 -0700300 of_iounmap(&op->resource[3], info->enable_reg, 1);
301
302out_unmap_freq_regs:
303 of_iounmap(&op->resource[2], info->freq_regs, 2);
304out_free:
305 kfree(state);
306out_err:
307 return err;
308}
309
Bill Pembertone2619cf2012-11-23 21:50:47 -0800310static int grover_remove(struct platform_device *op)
David S. Miller9c1a5072008-04-26 21:02:21 -0700311{
Jingoo Han35c4b122013-05-23 09:20:21 -0700312 struct sparcspkr_state *state = platform_get_drvdata(op);
David S. Miller9c1a5072008-04-26 21:02:21 -0700313 struct grover_beep_info *info = &state->u.grover;
314 struct input_dev *input_dev = state->input_dev;
315
316 /* turn off the speaker */
317 state->event(input_dev, EV_SND, SND_BELL, 0);
318
319 input_unregister_device(input_dev);
320
321 of_iounmap(&op->resource[3], info->enable_reg, 1);
322 of_iounmap(&op->resource[2], info->freq_regs, 2);
323
David S. Miller9c1a5072008-04-26 21:02:21 -0700324 kfree(state);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500325
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500326 return 0;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David S. Millerfd098312008-08-31 01:23:17 -0700329static const struct of_device_id grover_beep_match[] = {
David S. Millera2bd4fd2006-06-23 01:44:10 -0700330 {
David S. Miller9c1a5072008-04-26 21:02:21 -0700331 .name = "beep",
332 .compatible = "SUNW,smbus-beep",
David S. Millera2bd4fd2006-06-23 01:44:10 -0700333 },
334 {},
335};
336
Grant Likely4ebb24f2011-02-22 20:01:33 -0700337static struct platform_driver grover_beep_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700338 .driver = {
339 .name = "groverbeep",
340 .owner = THIS_MODULE,
341 .of_match_table = grover_beep_match,
342 },
David S. Miller9c1a5072008-04-26 21:02:21 -0700343 .probe = grover_beep_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800344 .remove = grover_remove,
David S. Millera2bd4fd2006-06-23 01:44:10 -0700345 .shutdown = sparcspkr_shutdown,
346};
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static int __init sparcspkr_init(void)
349{
Grant Likely4ebb24f2011-02-22 20:01:33 -0700350 int err = platform_driver_register(&bbc_beep_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
David S. Millera2bd4fd2006-06-23 01:44:10 -0700352 if (!err) {
Grant Likely4ebb24f2011-02-22 20:01:33 -0700353 err = platform_driver_register(&grover_beep_driver);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700354 if (err)
Grant Likely4ebb24f2011-02-22 20:01:33 -0700355 platform_driver_unregister(&bbc_beep_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David S. Millera2bd4fd2006-06-23 01:44:10 -0700358 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361static void __exit sparcspkr_exit(void)
362{
Grant Likely4ebb24f2011-02-22 20:01:33 -0700363 platform_driver_unregister(&bbc_beep_driver);
364 platform_driver_unregister(&grover_beep_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367module_init(sparcspkr_init);
368module_exit(sparcspkr_exit);