Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Driver for keys on GPIO lines capable of generating interrupts. |
| 3 | * |
| 4 | * Copyright 2005 Phil Blundell |
| 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 version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/version.h> |
| 13 | |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/pm.h> |
| 20 | #include <linux/sysctl.h> |
| 21 | #include <linux/proc_fs.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/input.h> |
David Brownell | 49015be | 2007-03-05 00:30:22 -0800 | [diff] [blame] | 25 | #include <linux/gpio_keys.h> |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 26 | |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 27 | #include <asm/gpio.h> |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 28 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 29 | struct gpio_button_data { |
| 30 | struct gpio_keys_button *button; |
| 31 | struct input_dev *input; |
| 32 | struct timer_list timer; |
| 33 | }; |
| 34 | |
| 35 | struct gpio_keys_drvdata { |
| 36 | struct input_dev *input; |
| 37 | struct gpio_button_data data[0]; |
| 38 | }; |
| 39 | |
| 40 | static void gpio_keys_report_event(struct gpio_keys_button *button, |
| 41 | struct input_dev *input) |
| 42 | { |
| 43 | unsigned int type = button->type ?: EV_KEY; |
| 44 | int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low; |
| 45 | |
| 46 | input_event(input, type, button->code, !!state); |
| 47 | input_sync(input); |
| 48 | } |
| 49 | |
| 50 | static void gpio_check_button(unsigned long _data) |
| 51 | { |
| 52 | struct gpio_button_data *data = (struct gpio_button_data *)_data; |
| 53 | |
| 54 | gpio_keys_report_event(data->button, data->input); |
| 55 | } |
| 56 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 57 | static irqreturn_t gpio_keys_isr(int irq, void *dev_id) |
| 58 | { |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 59 | struct platform_device *pdev = dev_id; |
| 60 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 61 | struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); |
| 62 | int i; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 63 | |
| 64 | for (i = 0; i < pdata->nbuttons; i++) { |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 65 | struct gpio_keys_button *button = &pdata->buttons[i]; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 66 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 67 | if (irq == gpio_to_irq(button->gpio)) { |
| 68 | struct gpio_button_data *bdata = &ddata->data[i]; |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 69 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 70 | if (button->debounce_interval) |
| 71 | mod_timer(&bdata->timer, |
| 72 | jiffies + |
| 73 | msecs_to_jiffies(button->debounce_interval)); |
| 74 | else |
| 75 | gpio_keys_report_event(button, bdata->input); |
| 76 | |
David Brownell | 1164ec1 | 2008-04-15 01:31:13 -0400 | [diff] [blame] | 77 | return IRQ_HANDLED; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
David Brownell | 1164ec1 | 2008-04-15 01:31:13 -0400 | [diff] [blame] | 81 | return IRQ_NONE; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int __devinit gpio_keys_probe(struct platform_device *pdev) |
| 85 | { |
| 86 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 87 | struct gpio_keys_drvdata *ddata; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 88 | struct input_dev *input; |
| 89 | int i, error; |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 90 | int wakeup = 0; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 91 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 92 | ddata = kzalloc(sizeof(struct gpio_keys_drvdata) + |
| 93 | pdata->nbuttons * sizeof(struct gpio_button_data), |
| 94 | GFP_KERNEL); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 95 | input = input_allocate_device(); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 96 | if (!ddata || !input) { |
| 97 | error = -ENOMEM; |
| 98 | goto fail1; |
| 99 | } |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 100 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 101 | platform_set_drvdata(pdev, ddata); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 102 | |
| 103 | input->name = pdev->name; |
| 104 | input->phys = "gpio-keys/input0"; |
Dmitry Torokhov | 469ba4d | 2007-04-12 01:34:58 -0400 | [diff] [blame] | 105 | input->dev.parent = &pdev->dev; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 106 | |
| 107 | input->id.bustype = BUS_HOST; |
| 108 | input->id.vendor = 0x0001; |
| 109 | input->id.product = 0x0001; |
| 110 | input->id.version = 0x0100; |
| 111 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 112 | ddata->input = input; |
| 113 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 114 | for (i = 0; i < pdata->nbuttons; i++) { |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 115 | struct gpio_keys_button *button = &pdata->buttons[i]; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 116 | struct gpio_button_data *bdata = &ddata->data[i]; |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 117 | int irq; |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 118 | unsigned int type = button->type ?: EV_KEY; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 119 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 120 | bdata->input = input; |
Uwe Kleine-König | 74dd439 | 2008-07-30 10:33:43 -0400 | [diff] [blame^] | 121 | bdata->button = button; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 122 | setup_timer(&bdata->timer, |
| 123 | gpio_check_button, (unsigned long)bdata); |
| 124 | |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 125 | error = gpio_request(button->gpio, button->desc ?: "gpio_keys"); |
| 126 | if (error < 0) { |
| 127 | pr_err("gpio-keys: failed to request GPIO %d," |
| 128 | " error %d\n", button->gpio, error); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 129 | goto fail2; |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | error = gpio_direction_input(button->gpio); |
| 133 | if (error < 0) { |
| 134 | pr_err("gpio-keys: failed to configure input" |
| 135 | " direction for GPIO %d, error %d\n", |
| 136 | button->gpio, error); |
| 137 | gpio_free(button->gpio); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 138 | goto fail2; |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | irq = gpio_to_irq(button->gpio); |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 142 | if (irq < 0) { |
| 143 | error = irq; |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 144 | pr_err("gpio-keys: Unable to get irq number" |
| 145 | " for GPIO %d, error %d\n", |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 146 | button->gpio, error); |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 147 | gpio_free(button->gpio); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 148 | goto fail2; |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | error = request_irq(irq, gpio_keys_isr, |
| 152 | IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING | |
| 153 | IRQF_TRIGGER_FALLING, |
| 154 | button->desc ? button->desc : "gpio_keys", |
| 155 | pdev); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 156 | if (error) { |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 157 | pr_err("gpio-keys: Unable to claim irq %d; error %d\n", |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 158 | irq, error); |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 159 | gpio_free(button->gpio); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 160 | goto fail2; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 161 | } |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 162 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 163 | if (button->wakeup) |
| 164 | wakeup = 1; |
| 165 | |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 166 | input_set_capability(input, type, button->code); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | error = input_register_device(input); |
| 170 | if (error) { |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 171 | pr_err("gpio-keys: Unable to register input device, " |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 172 | "error: %d\n", error); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 173 | goto fail2; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 176 | device_init_wakeup(&pdev->dev, wakeup); |
| 177 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 178 | return 0; |
| 179 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 180 | fail2: |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 181 | while (--i >= 0) { |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 182 | free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 183 | if (pdata->buttons[i].debounce_interval) |
| 184 | del_timer_sync(&ddata->data[i].timer); |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 185 | gpio_free(pdata->buttons[i].gpio); |
| 186 | } |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 187 | |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 188 | platform_set_drvdata(pdev, NULL); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 189 | fail1: |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 190 | input_free_device(input); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 191 | kfree(ddata); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 192 | |
| 193 | return error; |
| 194 | } |
| 195 | |
| 196 | static int __devexit gpio_keys_remove(struct platform_device *pdev) |
| 197 | { |
| 198 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 199 | struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); |
| 200 | struct input_dev *input = ddata->input; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 201 | int i; |
| 202 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 203 | device_init_wakeup(&pdev->dev, 0); |
| 204 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 205 | for (i = 0; i < pdata->nbuttons; i++) { |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 206 | int irq = gpio_to_irq(pdata->buttons[i].gpio); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 207 | free_irq(irq, pdev); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 208 | if (pdata->buttons[i].debounce_interval) |
| 209 | del_timer_sync(&ddata->data[i].timer); |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 210 | gpio_free(pdata->buttons[i].gpio); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | input_unregister_device(input); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 218 | |
| 219 | #ifdef CONFIG_PM |
| 220 | static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state) |
| 221 | { |
| 222 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 223 | int i; |
| 224 | |
| 225 | if (device_may_wakeup(&pdev->dev)) { |
| 226 | for (i = 0; i < pdata->nbuttons; i++) { |
| 227 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 228 | if (button->wakeup) { |
| 229 | int irq = gpio_to_irq(button->gpio); |
| 230 | enable_irq_wake(irq); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int gpio_keys_resume(struct platform_device *pdev) |
| 239 | { |
| 240 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 241 | int i; |
| 242 | |
| 243 | if (device_may_wakeup(&pdev->dev)) { |
| 244 | for (i = 0; i < pdata->nbuttons; i++) { |
| 245 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 246 | if (button->wakeup) { |
| 247 | int irq = gpio_to_irq(button->gpio); |
| 248 | disable_irq_wake(irq); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | #else |
| 256 | #define gpio_keys_suspend NULL |
| 257 | #define gpio_keys_resume NULL |
| 258 | #endif |
| 259 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 260 | struct platform_driver gpio_keys_device_driver = { |
| 261 | .probe = gpio_keys_probe, |
| 262 | .remove = __devexit_p(gpio_keys_remove), |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 263 | .suspend = gpio_keys_suspend, |
| 264 | .resume = gpio_keys_resume, |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 265 | .driver = { |
| 266 | .name = "gpio-keys", |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 267 | .owner = THIS_MODULE, |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 268 | } |
| 269 | }; |
| 270 | |
| 271 | static int __init gpio_keys_init(void) |
| 272 | { |
| 273 | return platform_driver_register(&gpio_keys_device_driver); |
| 274 | } |
| 275 | |
| 276 | static void __exit gpio_keys_exit(void) |
| 277 | { |
| 278 | platform_driver_unregister(&gpio_keys_device_driver); |
| 279 | } |
| 280 | |
| 281 | module_init(gpio_keys_init); |
| 282 | module_exit(gpio_keys_exit); |
| 283 | |
| 284 | MODULE_LICENSE("GPL"); |
| 285 | MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>"); |
| 286 | MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs"); |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 287 | MODULE_ALIAS("platform:gpio-keys"); |