2 * Cobalt button interface driver.
4 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
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.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/jiffies.h>
26 #include <linux/timer.h>
28 #define BUTTONS_POLL_INTERVAL 30 /* msec */
29 #define BUTTONS_COUNT_THRESHOLD 3
30 #define BUTTONS_STATUS_MASK 0xfe000000
33 struct input_dev *input;
43 static struct buttons_map buttons_map[] = {
44 { 0x02000000, KEY_RESTART, },
45 { 0x04000000, KEY_LEFT, },
46 { 0x08000000, KEY_UP, },
47 { 0x10000000, KEY_DOWN, },
48 { 0x20000000, KEY_RIGHT, },
49 { 0x40000000, KEY_ENTER, },
50 { 0x80000000, KEY_SELECT, },
53 static struct timer_list buttons_timer;
55 static void handle_buttons(unsigned long data)
57 struct buttons_map *button = buttons_map;
58 struct buttons_dev *bdev;
62 bdev = (struct buttons_dev *)data;
63 status = readl(bdev->reg);
64 status = ~status & BUTTONS_STATUS_MASK;
66 for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
67 if (status & button->mask) {
70 if (button->count >= BUTTONS_COUNT_THRESHOLD) {
71 input_report_key(bdev->input, button->keycode, 0);
72 input_sync(bdev->input);
77 if (button->count == BUTTONS_COUNT_THRESHOLD) {
78 input_report_key(bdev->input, button->keycode, 1);
79 input_sync(bdev->input);
85 mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL));
88 static int cobalt_buttons_open(struct input_dev *dev)
90 mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL));
95 static void cobalt_buttons_close(struct input_dev *dev)
97 del_timer_sync(&buttons_timer);
100 static int __devinit cobalt_buttons_probe(struct platform_device *pdev)
102 struct buttons_dev *bdev;
103 struct input_dev *input;
104 struct resource *res;
107 bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
108 input = input_allocate_device();
109 if (!bdev || !input) {
114 input->name = "Cobalt buttons";
115 input->phys = "cobalt/input0";
116 input->id.bustype = BUS_HOST;
117 input->cdev.dev = &pdev->dev;
118 input->open = cobalt_buttons_open;
119 input->close = cobalt_buttons_close;
121 input->evbit[0] = BIT(EV_KEY);
122 for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
123 set_bit(buttons_map[i].keycode, input->keybit);
124 buttons_map[i].count = 0;
127 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134 bdev->reg = ioremap(res->start, res->end - res->start + 1);
135 dev_set_drvdata(&pdev->dev, bdev);
137 setup_timer(&buttons_timer, handle_buttons, (unsigned long)bdev);
139 error = input_register_device(input);
148 input_free_device(input);
150 dev_set_drvdata(&pdev->dev, NULL);
154 static int __devexit cobalt_buttons_remove(struct platform_device *pdev)
156 struct device *dev = &pdev->dev;
157 struct buttons_dev *bdev = dev_get_drvdata(dev);
159 input_unregister_device(bdev->input);
162 dev_set_drvdata(dev, NULL);
167 static struct platform_driver cobalt_buttons_driver = {
168 .probe = cobalt_buttons_probe,
169 .remove = __devexit_p(cobalt_buttons_remove),
171 .name = "Cobalt buttons",
172 .owner = THIS_MODULE,
176 static int __init cobalt_buttons_init(void)
178 return platform_driver_register(&cobalt_buttons_driver);
181 static void __exit cobalt_buttons_exit(void)
183 platform_driver_unregister(&cobalt_buttons_driver);
186 module_init(cobalt_buttons_init);
187 module_exit(cobalt_buttons_exit);