2 * SGI Volume Button interface driver
4 * Copyright (C) 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <linux/init.h>
21 #include <linux/input-polldev.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
26 #ifdef CONFIG_SGI_IP22
27 #include <asm/sgi/ioc.h>
29 static inline u8 button_status(void)
33 status = readb(&sgioc->panel) ^ 0xa0;
34 return ((status & 0x80) >> 6) | ((status & 0x20) >> 5);
38 #ifdef CONFIG_SGI_IP32
39 #include <asm/ip32/mace.h>
41 static inline u8 button_status(void)
45 status = readq(&mace->perif.audio.control);
46 writeq(status & ~(3U << 23), &mace->perif.audio.control);
48 return (status >> 23) & 3;
52 #define BUTTONS_POLL_INTERVAL 30 /* msec */
53 #define BUTTONS_COUNT_THRESHOLD 3
55 static const unsigned short sgi_map[] = {
61 struct input_polled_dev *poll_dev;
62 unsigned short keymap[ARRAY_SIZE(sgi_map)];
63 int count[ARRAY_SIZE(sgi_map)];
66 static void handle_buttons(struct input_polled_dev *dev)
68 struct buttons_dev *bdev = dev->private;
69 struct input_dev *input = dev->input;
73 status = button_status();
75 for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
76 if (status & (1U << i)) {
77 if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
78 input_event(input, EV_MSC, MSC_SCAN, i);
79 input_report_key(input, bdev->keymap[i], 1);
83 if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
84 input_event(input, EV_MSC, MSC_SCAN, i);
85 input_report_key(input, bdev->keymap[i], 0);
93 static int __devinit sgi_buttons_probe(struct platform_device *pdev)
95 struct buttons_dev *bdev;
96 struct input_polled_dev *poll_dev;
97 struct input_dev *input;
100 bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
101 poll_dev = input_allocate_polled_device();
102 if (!bdev || !poll_dev) {
107 memcpy(bdev->keymap, sgi_map, sizeof(bdev->keymap));
109 poll_dev->private = bdev;
110 poll_dev->poll = handle_buttons;
111 poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
113 input = poll_dev->input;
114 input->name = "SGI buttons";
115 input->phys = "sgi/input0";
116 input->id.bustype = BUS_HOST;
117 input->dev.parent = &pdev->dev;
119 input->keycode = bdev->keymap;
120 input->keycodemax = ARRAY_SIZE(bdev->keymap);
121 input->keycodesize = sizeof(unsigned short);
123 input_set_capability(input, EV_MSC, MSC_SCAN);
124 __set_bit(EV_KEY, input->evbit);
125 for (i = 0; i < ARRAY_SIZE(sgi_map); i++)
126 __set_bit(bdev->keymap[i], input->keybit);
127 __clear_bit(KEY_RESERVED, input->keybit);
129 bdev->poll_dev = poll_dev;
130 dev_set_drvdata(&pdev->dev, bdev);
132 error = input_register_polled_device(poll_dev);
139 input_free_polled_device(poll_dev);
141 dev_set_drvdata(&pdev->dev, NULL);
145 static int __devexit sgi_buttons_remove(struct platform_device *pdev)
147 struct device *dev = &pdev->dev;
148 struct buttons_dev *bdev = dev_get_drvdata(dev);
150 input_unregister_polled_device(bdev->poll_dev);
151 input_free_polled_device(bdev->poll_dev);
153 dev_set_drvdata(dev, NULL);
158 static struct platform_driver sgi_buttons_driver = {
159 .probe = sgi_buttons_probe,
160 .remove = __devexit_p(sgi_buttons_remove),
163 .owner = THIS_MODULE,
167 static int __init sgi_buttons_init(void)
169 return platform_driver_register(&sgi_buttons_driver);
172 static void __exit sgi_buttons_exit(void)
174 platform_driver_unregister(&sgi_buttons_driver);
177 MODULE_LICENSE("GPL");
178 module_init(sgi_buttons_init);
179 module_exit(sgi_buttons_exit);