Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / input / misc / sparcspkr.c
1 /*
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>
12
13 #include <asm/io.h>
14 #include <asm/ebus.h>
15 #ifdef CONFIG_SPARC64
16 #include <asm/isa.h>
17 #endif
18
19 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
20 MODULE_DESCRIPTION("PC Speaker beeper driver");
21 MODULE_LICENSE("GPL");
22
23 static unsigned long beep_iobase;
24
25 static char *sparcspkr_isa_name = "Sparc ISA Speaker";
26 static char *sparcspkr_ebus_name = "Sparc EBUS Speaker";
27 static char *sparcspkr_phys = "sparc/input0";
28 static struct input_dev sparcspkr_dev;
29
30 DEFINE_SPINLOCK(beep_lock);
31
32 static void __init init_sparcspkr_struct(void)
33 {
34         sparcspkr_dev.evbit[0] = BIT(EV_SND);
35         sparcspkr_dev.sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
36
37         sparcspkr_dev.phys = sparcspkr_phys;
38         sparcspkr_dev.id.bustype = BUS_ISA;
39         sparcspkr_dev.id.vendor = 0x001f;
40         sparcspkr_dev.id.product = 0x0001;
41         sparcspkr_dev.id.version = 0x0100;
42 }
43
44 static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
45 {
46         unsigned int count = 0;
47         unsigned long flags;
48
49         if (type != EV_SND)
50                 return -1;
51
52         switch (code) {
53                 case SND_BELL: if (value) value = 1000;
54                 case SND_TONE: break;
55                 default: return -1;
56         }
57
58         if (value > 20 && value < 32767)
59                 count = 1193182 / value;
60
61         spin_lock_irqsave(&beep_lock, flags);
62
63         /* EBUS speaker only has on/off state, the frequency does not
64          * appear to be programmable.
65          */
66         if (count) {
67                 if (beep_iobase & 0x2UL)
68                         outb(1, beep_iobase);
69                 else
70                         outl(1, beep_iobase);
71         } else {
72                 if (beep_iobase & 0x2UL)
73                         outb(0, beep_iobase);
74                 else
75                         outl(0, beep_iobase);
76         }
77
78         spin_unlock_irqrestore(&beep_lock, flags);
79
80         return 0;
81 }
82
83 static int __init init_ebus_beep(struct linux_ebus_device *edev)
84 {
85         beep_iobase = edev->resource[0].start;
86
87         init_sparcspkr_struct();
88
89         sparcspkr_dev.name = sparcspkr_ebus_name;
90         sparcspkr_dev.event = ebus_spkr_event;
91
92         input_register_device(&sparcspkr_dev);
93
94         printk(KERN_INFO "input: %s\n", sparcspkr_ebus_name);
95         return 0;
96 }
97
98 #ifdef CONFIG_SPARC64
99 static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
100 {
101         unsigned int count = 0;
102         unsigned long flags;
103
104         if (type != EV_SND)
105                 return -1;
106
107         switch (code) {
108                 case SND_BELL: if (value) value = 1000;
109                 case SND_TONE: break;
110                 default: return -1;
111         }
112
113         if (value > 20 && value < 32767)
114                 count = 1193182 / value;
115
116         spin_lock_irqsave(&beep_lock, flags);
117
118         if (count) {
119                 /* enable counter 2 */
120                 outb(inb(beep_iobase + 0x61) | 3, beep_iobase + 0x61);
121                 /* set command for counter 2, 2 byte write */
122                 outb(0xB6, beep_iobase + 0x43);
123                 /* select desired HZ */
124                 outb(count & 0xff, beep_iobase + 0x42);
125                 outb((count >> 8) & 0xff, beep_iobase + 0x42);
126         } else {
127                 /* disable counter 2 */
128                 outb(inb_p(beep_iobase + 0x61) & 0xFC, beep_iobase + 0x61);
129         }
130
131         spin_unlock_irqrestore(&beep_lock, flags);
132
133         return 0;
134 }
135
136 static int __init init_isa_beep(struct sparc_isa_device *isa_dev)
137 {
138         beep_iobase = isa_dev->resource.start;
139
140         init_sparcspkr_struct();
141
142         sparcspkr_dev.name = sparcspkr_isa_name;
143         sparcspkr_dev.event = isa_spkr_event;
144         sparcspkr_dev.id.bustype = BUS_ISA;
145
146         input_register_device(&sparcspkr_dev);
147
148         printk(KERN_INFO "input: %s\n", sparcspkr_isa_name);
149         return 0;
150 }
151 #endif
152
153 static int __init sparcspkr_init(void)
154 {
155         struct linux_ebus *ebus;
156         struct linux_ebus_device *edev = NULL;
157 #ifdef CONFIG_SPARC64
158         struct sparc_isa_bridge *isa_br;
159         struct sparc_isa_device *isa_dev;
160 #endif
161
162         for_each_ebus(ebus) {
163                 for_each_ebusdev(edev, ebus) {
164                         if (!strcmp(edev->prom_name, "beep"))
165                                 return init_ebus_beep(edev);
166                 }
167         }
168 #ifdef CONFIG_SPARC64
169         for_each_isa(isa_br) {
170                 for_each_isadev(isa_dev, isa_br) {
171                         /* A hack, the beep device's base lives in
172                          * the DMA isa node.
173                          */
174                         if (!strcmp(isa_dev->prom_name, "dma"))
175                                 return init_isa_beep(isa_dev);
176                 }
177         }
178 #endif
179
180         return -ENODEV;
181 }
182
183 static void __exit sparcspkr_exit(void)
184 {
185         input_unregister_device(&sparcspkr_dev);
186 }
187
188 module_init(sparcspkr_init);
189 module_exit(sparcspkr_exit);