Yoichi Yuasa | bebb8a2 | 2007-02-18 01:50:18 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Cobalt button interface driver. |
| 3 | * |
| 4 | * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 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> |
| 27 | |
| 28 | #define BUTTONS_POLL_INTERVAL 30 /* msec */ |
| 29 | #define BUTTONS_COUNT_THRESHOLD 3 |
| 30 | #define BUTTONS_STATUS_MASK 0xfe000000 |
| 31 | |
| 32 | struct buttons_dev { |
| 33 | struct input_dev *input; |
| 34 | void __iomem *reg; |
| 35 | }; |
| 36 | |
| 37 | struct buttons_map { |
| 38 | uint32_t mask; |
| 39 | int keycode; |
| 40 | int count; |
| 41 | }; |
| 42 | |
| 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, }, |
| 51 | }; |
| 52 | |
Yoichi Yuasa | bebb8a2 | 2007-02-18 01:50:18 -0500 | [diff] [blame] | 53 | static struct timer_list buttons_timer; |
| 54 | |
| 55 | static void handle_buttons(unsigned long data) |
| 56 | { |
| 57 | struct buttons_map *button = buttons_map; |
| 58 | struct buttons_dev *bdev; |
| 59 | uint32_t status; |
| 60 | int i; |
| 61 | |
| 62 | bdev = (struct buttons_dev *)data; |
| 63 | status = readl(bdev->reg); |
| 64 | status = ~status & BUTTONS_STATUS_MASK; |
| 65 | |
| 66 | for (i = 0; i < ARRAY_SIZE(buttons_map); i++) { |
| 67 | if (status & button->mask) { |
| 68 | button->count++; |
| 69 | } else { |
| 70 | if (button->count >= BUTTONS_COUNT_THRESHOLD) { |
| 71 | input_report_key(bdev->input, button->keycode, 0); |
| 72 | input_sync(bdev->input); |
| 73 | } |
| 74 | button->count = 0; |
| 75 | } |
| 76 | |
| 77 | if (button->count == BUTTONS_COUNT_THRESHOLD) { |
| 78 | input_report_key(bdev->input, button->keycode, 1); |
| 79 | input_sync(bdev->input); |
| 80 | } |
| 81 | |
| 82 | button++; |
| 83 | } |
| 84 | |
| 85 | mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL)); |
| 86 | } |
| 87 | |
| 88 | static int cobalt_buttons_open(struct input_dev *dev) |
| 89 | { |
| 90 | mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL)); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static void cobalt_buttons_close(struct input_dev *dev) |
| 96 | { |
| 97 | del_timer_sync(&buttons_timer); |
| 98 | } |
| 99 | |
| 100 | static int __devinit cobalt_buttons_probe(struct platform_device *pdev) |
| 101 | { |
| 102 | struct buttons_dev *bdev; |
| 103 | struct input_dev *input; |
| 104 | struct resource *res; |
| 105 | int error, i; |
| 106 | |
| 107 | bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL); |
| 108 | input = input_allocate_device(); |
| 109 | if (!bdev || !input) { |
| 110 | error = -ENOMEM; |
| 111 | goto err_free_mem; |
| 112 | } |
| 113 | |
| 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; |
| 120 | |
| 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; |
| 125 | } |
| 126 | |
| 127 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 128 | if (!res) { |
| 129 | error = -EBUSY; |
| 130 | goto err_free_mem; |
| 131 | } |
| 132 | |
| 133 | bdev->input = input; |
| 134 | bdev->reg = ioremap(res->start, res->end - res->start + 1); |
| 135 | dev_set_drvdata(&pdev->dev, bdev); |
| 136 | |
| 137 | setup_timer(&buttons_timer, handle_buttons, (unsigned long)bdev); |
| 138 | |
| 139 | error = input_register_device(input); |
| 140 | if (error) |
| 141 | goto err_iounmap; |
| 142 | |
| 143 | return 0; |
| 144 | |
| 145 | err_iounmap: |
| 146 | iounmap(bdev->reg); |
| 147 | err_free_mem: |
| 148 | input_free_device(input); |
| 149 | kfree(bdev); |
| 150 | dev_set_drvdata(&pdev->dev, NULL); |
| 151 | return error; |
| 152 | } |
| 153 | |
| 154 | static int __devexit cobalt_buttons_remove(struct platform_device *pdev) |
| 155 | { |
| 156 | struct device *dev = &pdev->dev; |
| 157 | struct buttons_dev *bdev = dev_get_drvdata(dev); |
| 158 | |
| 159 | input_unregister_device(bdev->input); |
| 160 | iounmap(bdev->reg); |
| 161 | kfree(bdev); |
| 162 | dev_set_drvdata(dev, NULL); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static struct platform_driver cobalt_buttons_driver = { |
| 168 | .probe = cobalt_buttons_probe, |
| 169 | .remove = __devexit_p(cobalt_buttons_remove), |
| 170 | .driver = { |
| 171 | .name = "Cobalt buttons", |
| 172 | .owner = THIS_MODULE, |
| 173 | }, |
| 174 | }; |
| 175 | |
| 176 | static int __init cobalt_buttons_init(void) |
| 177 | { |
Dmitry Torokhov | d0a0515 | 2007-04-12 01:36:12 -0400 | [diff] [blame^] | 178 | return platform_driver_register(&cobalt_buttons_driver); |
Yoichi Yuasa | bebb8a2 | 2007-02-18 01:50:18 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void __exit cobalt_buttons_exit(void) |
| 182 | { |
| 183 | platform_driver_unregister(&cobalt_buttons_driver); |
Yoichi Yuasa | bebb8a2 | 2007-02-18 01:50:18 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | module_init(cobalt_buttons_init); |
| 187 | module_exit(cobalt_buttons_exit); |