2 * Driver for PC-speaker like devices found on various Sparc systems.
4 * Copyright (c) 2002 Vojtech Pavlik
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
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>
12 #include <linux/platform_device.h>
20 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
21 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
22 MODULE_LICENSE("GPL");
24 const char *beep_name;
25 static unsigned long beep_iobase;
26 static int (*beep_event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
27 static DEFINE_SPINLOCK(beep_lock);
29 static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
31 unsigned int count = 0;
38 case SND_BELL: if (value) value = 1000;
43 if (value > 20 && value < 32767)
44 count = 1193182 / value;
46 spin_lock_irqsave(&beep_lock, flags);
48 /* EBUS speaker only has on/off state, the frequency does not
49 * appear to be programmable.
51 if (beep_iobase & 0x2UL)
52 outb(!!count, beep_iobase);
54 outl(!!count, beep_iobase);
56 spin_unlock_irqrestore(&beep_lock, flags);
62 static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
64 unsigned int count = 0;
71 case SND_BELL: if (value) value = 1000;
76 if (value > 20 && value < 32767)
77 count = 1193182 / value;
79 spin_lock_irqsave(&beep_lock, flags);
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);
90 /* disable counter 2 */
91 outb(inb_p(beep_iobase + 0x61) & 0xFC, beep_iobase + 0x61);
94 spin_unlock_irqrestore(&beep_lock, flags);
100 static int __devinit sparcspkr_probe(struct platform_device *dev)
102 struct input_dev *input_dev;
105 input_dev = input_allocate_device();
109 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;
117 input_dev->evbit[0] = BIT(EV_SND);
118 input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
120 input_dev->event = beep_event;
122 error = input_register_device(input_dev);
124 input_free_device(input_dev);
128 platform_set_drvdata(dev, input_dev);
133 static int __devexit sparcspkr_remove(struct platform_device *dev)
135 struct input_dev *input_dev = platform_get_drvdata(dev);
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);
145 static void sparcspkr_shutdown(struct platform_device *dev)
147 /* turn off the speaker */
148 beep_event(NULL, EV_SND, SND_BELL, 0);
151 static struct platform_driver sparcspkr_platform_driver = {
154 .owner = THIS_MODULE,
156 .probe = sparcspkr_probe,
157 .remove = __devexit_p(sparcspkr_remove),
158 .shutdown = sparcspkr_shutdown,
161 static struct platform_device *sparcspkr_platform_device;
163 static int __init sparcspkr_drv_init(void)
167 error = platform_driver_register(&sparcspkr_platform_driver);
171 sparcspkr_platform_device = platform_device_alloc("sparcspkr", -1);
172 if (!sparcspkr_platform_device) {
174 goto err_unregister_driver;
177 error = platform_device_add(sparcspkr_platform_device);
179 goto err_free_device;
184 platform_device_put(sparcspkr_platform_device);
185 err_unregister_driver:
186 platform_driver_unregister(&sparcspkr_platform_driver);
191 static int __init sparcspkr_init(void)
193 struct linux_ebus *ebus;
194 struct linux_ebus_device *edev;
195 #ifdef CONFIG_SPARC64
196 struct sparc_isa_bridge *isa_br;
197 struct sparc_isa_device *isa_dev;
200 for_each_ebus(ebus) {
201 for_each_ebusdev(edev, ebus) {
202 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();
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
216 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();
229 static void __exit sparcspkr_exit(void)
231 platform_device_unregister(sparcspkr_platform_device);
232 platform_driver_unregister(&sparcspkr_platform_driver);
235 module_init(sparcspkr_init);
236 module_exit(sparcspkr_exit);