blob: a3637d870880a22a8d92befb7e75aa9272096c1d [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 */
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>
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050011#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include <asm/io.h>
14#include <asm/ebus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
David S. Millera2bd4fd2006-06-23 01:44:10 -070017MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -050018MODULE_DESCRIPTION("Sparc Speaker beeper driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070019MODULE_LICENSE("GPL");
20
David S. Millera2bd4fd2006-06-23 01:44:10 -070021struct sparcspkr_state {
22 const char *name;
23 unsigned long iobase;
24 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
25 spinlock_t lock;
26 struct input_dev *input_dev;
27};
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
30{
Dmitry Torokhov293e6392007-04-12 01:35:32 -040031 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 unsigned int count = 0;
33 unsigned long flags;
34
35 if (type != EV_SND)
36 return -1;
37
38 switch (code) {
39 case SND_BELL: if (value) value = 1000;
40 case SND_TONE: break;
41 default: return -1;
42 }
43
44 if (value > 20 && value < 32767)
45 count = 1193182 / value;
46
David S. Millera2bd4fd2006-06-23 01:44:10 -070047 spin_lock_irqsave(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 /* EBUS speaker only has on/off state, the frequency does not
50 * appear to be programmable.
51 */
David S. Millera2bd4fd2006-06-23 01:44:10 -070052 if (state->iobase & 0x2UL)
53 outb(!!count, state->iobase);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050054 else
David S. Millera2bd4fd2006-06-23 01:44:10 -070055 outl(!!count, state->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David S. Millera2bd4fd2006-06-23 01:44:10 -070057 spin_unlock_irqrestore(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 return 0;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
63{
Dmitry Torokhov293e6392007-04-12 01:35:32 -040064 struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned int count = 0;
66 unsigned long flags;
67
68 if (type != EV_SND)
69 return -1;
70
71 switch (code) {
72 case SND_BELL: if (value) value = 1000;
73 case SND_TONE: break;
74 default: return -1;
75 }
76
77 if (value > 20 && value < 32767)
78 count = 1193182 / value;
79
David S. Millera2bd4fd2006-06-23 01:44:10 -070080 spin_lock_irqsave(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (count) {
83 /* enable counter 2 */
David S. Millera2bd4fd2006-06-23 01:44:10 -070084 outb(inb(state->iobase + 0x61) | 3, state->iobase + 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* set command for counter 2, 2 byte write */
David S. Millera2bd4fd2006-06-23 01:44:10 -070086 outb(0xB6, state->iobase + 0x43);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /* select desired HZ */
David S. Millera2bd4fd2006-06-23 01:44:10 -070088 outb(count & 0xff, state->iobase + 0x42);
89 outb((count >> 8) & 0xff, state->iobase + 0x42);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 } else {
91 /* disable counter 2 */
David S. Millera2bd4fd2006-06-23 01:44:10 -070092 outb(inb_p(state->iobase + 0x61) & 0xFC, state->iobase + 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
David S. Millera2bd4fd2006-06-23 01:44:10 -070095 spin_unlock_irqrestore(&state->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 return 0;
98}
99
David S. Millera2bd4fd2006-06-23 01:44:10 -0700100static int __devinit sparcspkr_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700102 struct sparcspkr_state *state = dev_get_drvdata(dev);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500103 struct input_dev *input_dev;
104 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500106 input_dev = input_allocate_device();
107 if (!input_dev)
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -0500108 return -ENOMEM;
109
David S. Millera2bd4fd2006-06-23 01:44:10 -0700110 input_dev->name = state->name;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500111 input_dev->phys = "sparc/input0";
112 input_dev->id.bustype = BUS_ISA;
113 input_dev->id.vendor = 0x001f;
114 input_dev->id.product = 0x0001;
115 input_dev->id.version = 0x0100;
Dmitry Torokhov293e6392007-04-12 01:35:32 -0400116 input_dev->dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700118 input_dev->evbit[0] = BIT_MASK(EV_SND);
119 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
David S. Millera2bd4fd2006-06-23 01:44:10 -0700121 input_dev->event = state->event;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500122
123 error = input_register_device(input_dev);
124 if (error) {
125 input_free_device(input_dev);
126 return error;
127 }
128
David S. Millera2bd4fd2006-06-23 01:44:10 -0700129 state->input_dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132}
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500133
David S. Millera2bd4fd2006-06-23 01:44:10 -0700134static int __devexit sparcspkr_remove(struct of_device *dev)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500135{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700136 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
137 struct input_dev *input_dev = state->input_dev;
138
139 /* turn off the speaker */
140 state->event(input_dev, EV_SND, SND_BELL, 0);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500141
142 input_unregister_device(input_dev);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700143
144 dev_set_drvdata(&dev->dev, NULL);
145 kfree(state);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500146
147 return 0;
148}
149
David S. Millera2bd4fd2006-06-23 01:44:10 -0700150static int sparcspkr_shutdown(struct of_device *dev)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500151{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700152 struct sparcspkr_state *state = dev_get_drvdata(&dev->dev);
153 struct input_dev *input_dev = state->input_dev;
154
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500155 /* turn off the speaker */
David S. Millera2bd4fd2006-06-23 01:44:10 -0700156 state->event(input_dev, EV_SND, SND_BELL, 0);
157
158 return 0;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500159}
160
David S. Millera2bd4fd2006-06-23 01:44:10 -0700161static int __devinit ebus_beep_probe(struct of_device *dev, const struct of_device_id *match)
162{
163 struct linux_ebus_device *edev = to_ebus_device(&dev->dev);
164 struct sparcspkr_state *state;
165 int err;
166
167 state = kzalloc(sizeof(*state), GFP_KERNEL);
168 if (!state)
169 return -ENOMEM;
170
171 state->name = "Sparc EBUS Speaker";
172 state->iobase = edev->resource[0].start;
173 state->event = ebus_spkr_event;
174 spin_lock_init(&state->lock);
175
176 dev_set_drvdata(&dev->dev, state);
177
178 err = sparcspkr_probe(&dev->dev);
179 if (err) {
180 dev_set_drvdata(&dev->dev, NULL);
181 kfree(state);
182 }
183
184 return 0;
185}
186
187static struct of_device_id ebus_beep_match[] = {
188 {
189 .name = "beep",
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500190 },
David S. Millera2bd4fd2006-06-23 01:44:10 -0700191 {},
192};
193
194static struct of_platform_driver ebus_beep_driver = {
195 .name = "beep",
196 .match_table = ebus_beep_match,
197 .probe = ebus_beep_probe,
198 .remove = sparcspkr_remove,
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500199 .shutdown = sparcspkr_shutdown,
200};
201
David S. Millera2bd4fd2006-06-23 01:44:10 -0700202static int __devinit isa_beep_probe(struct of_device *dev, const struct of_device_id *match)
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500203{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700204 struct sparc_isa_device *idev = to_isa_device(&dev->dev);
205 struct sparcspkr_state *state;
206 int err;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500207
David S. Millera2bd4fd2006-06-23 01:44:10 -0700208 state = kzalloc(sizeof(*state), GFP_KERNEL);
209 if (!state)
210 return -ENOMEM;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500211
David S. Millera2bd4fd2006-06-23 01:44:10 -0700212 state->name = "Sparc ISA Speaker";
213 state->iobase = idev->resource.start;
214 state->event = isa_spkr_event;
215 spin_lock_init(&state->lock);
216
217 dev_set_drvdata(&dev->dev, state);
218
219 err = sparcspkr_probe(&dev->dev);
220 if (err) {
221 dev_set_drvdata(&dev->dev, NULL);
222 kfree(state);
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500223 }
224
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500225 return 0;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
David S. Millera2bd4fd2006-06-23 01:44:10 -0700228static struct of_device_id isa_beep_match[] = {
229 {
230 .name = "dma",
231 },
232 {},
233};
234
235static struct of_platform_driver isa_beep_driver = {
236 .name = "beep",
237 .match_table = isa_beep_match,
238 .probe = isa_beep_probe,
239 .remove = sparcspkr_remove,
240 .shutdown = sparcspkr_shutdown,
241};
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int __init sparcspkr_init(void)
244{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700245 int err = of_register_driver(&ebus_beep_driver, &ebus_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David S. Millera2bd4fd2006-06-23 01:44:10 -0700247 if (!err) {
248 err = of_register_driver(&isa_beep_driver, &isa_bus_type);
249 if (err)
250 of_unregister_driver(&ebus_beep_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David S. Millera2bd4fd2006-06-23 01:44:10 -0700253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256static void __exit sparcspkr_exit(void)
257{
David S. Millera2bd4fd2006-06-23 01:44:10 -0700258 of_unregister_driver(&ebus_beep_driver);
259 of_unregister_driver(&isa_beep_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262module_init(sparcspkr_init);
263module_exit(sparcspkr_exit);