blob: f0fd2c4740f19611678a927a6302a0fc4e27cced [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
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 */
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>
16#ifdef CONFIG_SPARC64
17#include <asm/isa.h>
18#endif
19
20MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -050021MODULE_DESCRIPTION("Sparc Speaker beeper driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070022MODULE_LICENSE("GPL");
23
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050024const char *beep_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static unsigned long beep_iobase;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050026static int (*beep_event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
27static DEFINE_SPINLOCK(beep_lock);
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{
31 unsigned int count = 0;
32 unsigned long flags;
33
34 if (type != EV_SND)
35 return -1;
36
37 switch (code) {
38 case SND_BELL: if (value) value = 1000;
39 case SND_TONE: break;
40 default: return -1;
41 }
42
43 if (value > 20 && value < 32767)
44 count = 1193182 / value;
45
46 spin_lock_irqsave(&beep_lock, flags);
47
48 /* EBUS speaker only has on/off state, the frequency does not
49 * appear to be programmable.
50 */
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050051 if (beep_iobase & 0x2UL)
52 outb(!!count, beep_iobase);
53 else
54 outl(!!count, beep_iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 spin_unlock_irqrestore(&beep_lock, flags);
57
58 return 0;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_SPARC64
62static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
63{
64 unsigned int count = 0;
65 unsigned long flags;
66
67 if (type != EV_SND)
68 return -1;
69
70 switch (code) {
71 case SND_BELL: if (value) value = 1000;
72 case SND_TONE: break;
73 default: return -1;
74 }
75
76 if (value > 20 && value < 32767)
77 count = 1193182 / value;
78
79 spin_lock_irqsave(&beep_lock, flags);
80
81 if (count) {
82 /* enable counter 2 */
83 outb(inb(beep_iobase + 0x61) | 3, beep_iobase + 0x61);
84 /* set command for counter 2, 2 byte write */
85 outb(0xB6, beep_iobase + 0x43);
86 /* select desired HZ */
87 outb(count & 0xff, beep_iobase + 0x42);
88 outb((count >> 8) & 0xff, beep_iobase + 0x42);
89 } else {
90 /* disable counter 2 */
91 outb(inb_p(beep_iobase + 0x61) & 0xFC, beep_iobase + 0x61);
92 }
93
94 spin_unlock_irqrestore(&beep_lock, flags);
95
96 return 0;
97}
Dmitry Torokhovf5b64072005-12-21 00:52:35 -050098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500100static int __devinit sparcspkr_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500102 struct input_dev *input_dev;
103 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500105 input_dev = input_allocate_device();
106 if (!input_dev)
Dmitry Torokhov76b7cdd2005-09-15 02:01:51 -0500107 return -ENOMEM;
108
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500109 input_dev->name = beep_name;
110 input_dev->phys = "sparc/input0";
111 input_dev->id.bustype = BUS_ISA;
112 input_dev->id.vendor = 0x001f;
113 input_dev->id.product = 0x0001;
114 input_dev->id.version = 0x0100;
115 input_dev->cdev.dev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500117 input_dev->evbit[0] = BIT(EV_SND);
118 input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500120 input_dev->event = beep_event;
121
122 error = input_register_device(input_dev);
123 if (error) {
124 input_free_device(input_dev);
125 return error;
126 }
127
128 platform_set_drvdata(dev, input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
131}
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500132
133static int __devexit sparcspkr_remove(struct platform_device *dev)
134{
135 struct input_dev *input_dev = platform_get_drvdata(dev);
136
137 input_unregister_device(input_dev);
138 platform_set_drvdata(dev, NULL);
139 /* turn off the speaker */
140 beep_event(NULL, EV_SND, SND_BELL, 0);
141
142 return 0;
143}
144
145static void sparcspkr_shutdown(struct platform_device *dev)
146{
147 /* turn off the speaker */
148 beep_event(NULL, EV_SND, SND_BELL, 0);
149}
150
151static struct platform_driver sparcspkr_platform_driver = {
152 .driver = {
153 .name = "sparcspkr",
154 .owner = THIS_MODULE,
155 },
156 .probe = sparcspkr_probe,
157 .remove = __devexit_p(sparcspkr_remove),
158 .shutdown = sparcspkr_shutdown,
159};
160
161static struct platform_device *sparcspkr_platform_device;
162
163static int __init sparcspkr_drv_init(void)
164{
165 int error;
166
167 error = platform_driver_register(&sparcspkr_platform_driver);
168 if (error)
169 return error;
170
171 sparcspkr_platform_device = platform_device_alloc("sparcspkr", -1);
172 if (!sparcspkr_platform_device) {
173 error = -ENOMEM;
174 goto err_unregister_driver;
175 }
176
177 error = platform_device_add(sparcspkr_platform_device);
178 if (error)
179 goto err_free_device;
180
181 return 0;
182
183 err_free_device:
184 platform_device_put(sparcspkr_platform_device);
185 err_unregister_driver:
186 platform_driver_unregister(&sparcspkr_platform_driver);
187
188 return error;
189}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191static int __init sparcspkr_init(void)
192{
193 struct linux_ebus *ebus;
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500194 struct linux_ebus_device *edev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifdef CONFIG_SPARC64
196 struct sparc_isa_bridge *isa_br;
197 struct sparc_isa_device *isa_dev;
198#endif
199
200 for_each_ebus(ebus) {
201 for_each_ebusdev(edev, ebus) {
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500202 if (!strcmp(edev->prom_name, "beep")) {
203 beep_name = "Sparc EBUS Speaker";
204 beep_event = ebus_spkr_event;
205 beep_iobase = edev->resource[0].start;
206 return sparcspkr_drv_init();
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 }
210#ifdef CONFIG_SPARC64
211 for_each_isa(isa_br) {
212 for_each_isadev(isa_dev, isa_br) {
213 /* A hack, the beep device's base lives in
214 * the DMA isa node.
215 */
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500216 if (!strcmp(isa_dev->prom_name, "dma")) {
217 beep_name = "Sparc ISA Speaker";
218 beep_event = isa_spkr_event,
219 beep_iobase = isa_dev->resource.start;
220 return sparcspkr_drv_init();
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224#endif
225
226 return -ENODEV;
227}
228
229static void __exit sparcspkr_exit(void)
230{
Dmitry Torokhovf5b64072005-12-21 00:52:35 -0500231 platform_device_unregister(sparcspkr_platform_device);
232 platform_driver_unregister(&sparcspkr_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235module_init(sparcspkr_init);
236module_exit(sparcspkr_exit);