blob: 42c11fbf3c79ff12d405f9f2c13d1ebed9da7901 [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. Millera2bd4fd2006-06-23 01:44:10 -07005 * Copyright (c) 2002, 2006 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include <linux/config.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/input.h>
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050012#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <asm/io.h>
15#include <asm/ebus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
David S. Millera2bd4fd2006-06-23 01:44:10 -070018MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -050019MODULE_DESCRIPTION("Sparc Speaker beeper driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070020MODULE_LICENSE("GPL");
21
David S. Millera2bd4fd2006-06-23 01:44:10 -070022struct sparcspkr_state {
23 const char *name;
24 unsigned long iobase;
25 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
26 spinlock_t lock;
27 struct input_dev *input_dev;
28};
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
31{
David S. Millera2bd4fd2006-06-23 01:44:10 -070032 struct sparcspkr_state *state = dev_get_drvdata(dev->cdev.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 unsigned int count = 0;
34 unsigned long flags;
35
36 if (type != EV_SND)
37 return -1;
38
39 switch (code) {
40 case SND_BELL: if (value) value = 1000;
41 case SND_TONE: break;
42 default: return -1;
43 }
44
45 if (value > 20 && value < 32767)
46 count = 1193182 / value;
47
David S. Millera2bd4fd2006-06-23 01:44:10 -070048 spin_lock_irqsave(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 /* EBUS speaker only has on/off state, the frequency does not
51 * appear to be programmable.
52 */
David S. Millera2bd4fd2006-06-23 01:44:10 -070053 if (state->iobase & 0x2UL)
54 outb(!!count, state->iobase);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050055 else
David S. Millera2bd4fd2006-06-23 01:44:10 -070056 outl(!!count, state->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David S. Millera2bd4fd2006-06-23 01:44:10 -070058 spin_unlock_irqrestore(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 return 0;
61}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
64{
David S. Millera2bd4fd2006-06-23 01:44:10 -070065 struct sparcspkr_state *state = dev_get_drvdata(dev->cdev.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 unsigned int count = 0;
67 unsigned long flags;
68
69 if (type != EV_SND)
70 return -1;
71
72 switch (code) {
73 case SND_BELL: if (value) value = 1000;
74 case SND_TONE: break;
75 default: return -1;
76 }
77
78 if (value > 20 && value < 32767)
79 count = 1193182 / value;
80
David S. Millera2bd4fd2006-06-23 01:44:10 -070081 spin_lock_irqsave(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (count) {
84 /* enable counter 2 */
David S. Millera2bd4fd2006-06-23 01:44:10 -070085 outb(inb(state->iobase + 0x61) | 3, state->iobase + 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 /* set command for counter 2, 2 byte write */
David S. Millera2bd4fd2006-06-23 01:44:10 -070087 outb(0xB6, state->iobase + 0x43);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /* select desired HZ */
David S. Millera2bd4fd2006-06-23 01:44:10 -070089 outb(count & 0xff, state->iobase + 0x42);
90 outb((count >> 8) & 0xff, state->iobase + 0x42);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 } else {
92 /* disable counter 2 */
David S. Millera2bd4fd2006-06-23 01:44:10 -070093 outb(inb_p(state->iobase + 0x61) & 0xFC, state->iobase + 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
David S. Millera2bd4fd2006-06-23 01:44:10 -070096 spin_unlock_irqrestore(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 return 0;
99}
100
David S. Millera2bd4fd2006-06-23 01:44:10 -0700101static int __devinit sparcspkr_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700103 struct sparcspkr_state *state = dev_get_drvdata(dev);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500104 struct input_dev *input_dev;
105 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500107 input_dev = input_allocate_device();
108 if (!input_dev)
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -0500109 return -ENOMEM;
110
David S. Millera2bd4fd2006-06-23 01:44:10 -0700111 input_dev->name = state->name;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500112 input_dev->phys = "sparc/input0";
113 input_dev->id.bustype = BUS_ISA;
114 input_dev->id.vendor = 0x001f;
115 input_dev->id.product = 0x0001;
116 input_dev->id.version = 0x0100;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700117 input_dev->cdev.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500119 input_dev->evbit[0] = BIT(EV_SND);
120 input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
David S. Millera2bd4fd2006-06-23 01:44:10 -0700122 input_dev->event = state->event;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500123
124 error = input_register_device(input_dev);
125 if (error) {
126 input_free_device(input_dev);
127 return error;
128 }
129
David S. Millera2bd4fd2006-06-23 01:44:10 -0700130 state->input_dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 0;
133}
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500134
David S. Millera2bd4fd2006-06-23 01:44:10 -0700135static int __devexit sparcspkr_remove(struct of_device *dev)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500136{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700137 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
138 struct input_dev *input_dev = state->input_dev;
139
140 /* turn off the speaker */
141 state->event(input_dev, EV_SND, SND_BELL, 0);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500142
143 input_unregister_device(input_dev);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700144
145 dev_set_drvdata(&dev->dev, NULL);
146 kfree(state);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500147
148 return 0;
149}
150
David S. Millera2bd4fd2006-06-23 01:44:10 -0700151static int sparcspkr_shutdown(struct of_device *dev)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500152{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700153 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
154 struct input_dev *input_dev = state->input_dev;
155
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500156 /* turn off the speaker */
David S. Millera2bd4fd2006-06-23 01:44:10 -0700157 state->event(input_dev, EV_SND, SND_BELL, 0);
158
159 return 0;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500160}
161
David S. Millera2bd4fd2006-06-23 01:44:10 -0700162static int __devinit ebus_beep_probe(struct of_device *dev, const struct of_device_id *match)
163{
164 struct linux_ebus_device *edev = to_ebus_device(&dev->dev);
165 struct sparcspkr_state *state;
166 int err;
167
168 state = kzalloc(sizeof(*state), GFP_KERNEL);
169 if (!state)
170 return -ENOMEM;
171
172 state->name = "Sparc EBUS Speaker";
173 state->iobase = edev->resource[0].start;
174 state->event = ebus_spkr_event;
175 spin_lock_init(&state->lock);
176
177 dev_set_drvdata(&dev->dev, state);
178
179 err = sparcspkr_probe(&dev->dev);
180 if (err) {
181 dev_set_drvdata(&dev->dev, NULL);
182 kfree(state);
183 }
184
185 return 0;
186}
187
188static struct of_device_id ebus_beep_match[] = {
189 {
190 .name = "beep",
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500191 },
David S. Millera2bd4fd2006-06-23 01:44:10 -0700192 {},
193};
194
195static struct of_platform_driver ebus_beep_driver = {
196 .name = "beep",
197 .match_table = ebus_beep_match,
198 .probe = ebus_beep_probe,
199 .remove = sparcspkr_remove,
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500200 .shutdown = sparcspkr_shutdown,
201};
202
David S. Millera2bd4fd2006-06-23 01:44:10 -0700203static int __devinit isa_beep_probe(struct of_device *dev, const struct of_device_id *match)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500204{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700205 struct sparc_isa_device *idev = to_isa_device(&dev->dev);
206 struct sparcspkr_state *state;
207 int err;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500208
David S. Millera2bd4fd2006-06-23 01:44:10 -0700209 state = kzalloc(sizeof(*state), GFP_KERNEL);
210 if (!state)
211 return -ENOMEM;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500212
David S. Millera2bd4fd2006-06-23 01:44:10 -0700213 state->name = "Sparc ISA Speaker";
214 state->iobase = idev->resource.start;
215 state->event = isa_spkr_event;
216 spin_lock_init(&state->lock);
217
218 dev_set_drvdata(&dev->dev, state);
219
220 err = sparcspkr_probe(&dev->dev);
221 if (err) {
222 dev_set_drvdata(&dev->dev, NULL);
223 kfree(state);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500224 }
225
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500226 return 0;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
David S. Millera2bd4fd2006-06-23 01:44:10 -0700229static struct of_device_id isa_beep_match[] = {
230 {
231 .name = "dma",
232 },
233 {},
234};
235
236static struct of_platform_driver isa_beep_driver = {
237 .name = "beep",
238 .match_table = isa_beep_match,
239 .probe = isa_beep_probe,
240 .remove = sparcspkr_remove,
241 .shutdown = sparcspkr_shutdown,
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int __init sparcspkr_init(void)
245{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700246 int err = of_register_driver(&ebus_beep_driver, &ebus_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David S. Millera2bd4fd2006-06-23 01:44:10 -0700248 if (!err) {
249 err = of_register_driver(&isa_beep_driver, &isa_bus_type);
250 if (err)
251 of_unregister_driver(&ebus_beep_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
David S. Millera2bd4fd2006-06-23 01:44:10 -0700254 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static void __exit sparcspkr_exit(void)
258{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700259 of_unregister_driver(&ebus_beep_driver);
260 of_unregister_driver(&isa_beep_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263module_init(sparcspkr_init);
264module_exit(sparcspkr_exit);