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