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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 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> |
Jani Nikula | da0d03f | 2009-06-28 22:38:56 -0700 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
Ben Dooks | 111bc59 | 2009-11-10 21:01:31 -0800 | [diff] [blame] | 27 | #include <linux/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; |
Jani Nikula | ca865a7 | 2009-06-28 22:38:44 -0700 | [diff] [blame] | 32 | struct timer_list timer; |
Jani Nikula | da0d03f | 2009-06-28 22:38:56 -0700 | [diff] [blame] | 33 | struct work_struct work; |
Grazvydas Ignotas | 28ed684 | 2010-06-28 10:59:32 -0700 | [diff] [blame] | 34 | int timer_debounce; /* in msecs */ |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 35 | bool disabled; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct gpio_keys_drvdata { |
| 39 | struct input_dev *input; |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 40 | struct mutex disable_lock; |
| 41 | unsigned int n_buttons; |
Shubhrajyoti D | 173bdd7 | 2010-08-03 19:44:40 -0700 | [diff] [blame] | 42 | int (*enable)(struct device *dev); |
| 43 | void (*disable)(struct device *dev); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 44 | struct gpio_button_data data[0]; |
| 45 | }; |
| 46 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 47 | /* |
| 48 | * SYSFS interface for enabling/disabling keys and switches: |
| 49 | * |
| 50 | * There are 4 attributes under /sys/devices/platform/gpio-keys/ |
| 51 | * keys [ro] - bitmap of keys (EV_KEY) which can be |
| 52 | * disabled |
| 53 | * switches [ro] - bitmap of switches (EV_SW) which can be |
| 54 | * disabled |
| 55 | * disabled_keys [rw] - bitmap of keys currently disabled |
| 56 | * disabled_switches [rw] - bitmap of switches currently disabled |
| 57 | * |
| 58 | * Userland can change these values and hence disable event generation |
| 59 | * for each key (or switch). Disabling a key means its interrupt line |
| 60 | * is disabled. |
| 61 | * |
| 62 | * For example, if we have following switches set up as gpio-keys: |
| 63 | * SW_DOCK = 5 |
| 64 | * SW_CAMERA_LENS_COVER = 9 |
| 65 | * SW_KEYPAD_SLIDE = 10 |
| 66 | * SW_FRONT_PROXIMITY = 11 |
| 67 | * This is read from switches: |
| 68 | * 11-9,5 |
| 69 | * Next we want to disable proximity (11) and dock (5), we write: |
| 70 | * 11,5 |
| 71 | * to file disabled_switches. Now proximity and dock IRQs are disabled. |
| 72 | * This can be verified by reading the file disabled_switches: |
| 73 | * 11,5 |
| 74 | * If we now want to enable proximity (11) switch we write: |
| 75 | * 5 |
| 76 | * to disabled_switches. |
| 77 | * |
| 78 | * We can disable only those keys which don't allow sharing the irq. |
| 79 | */ |
| 80 | |
| 81 | /** |
| 82 | * get_n_events_by_type() - returns maximum number of events per @type |
| 83 | * @type: type of button (%EV_KEY, %EV_SW) |
| 84 | * |
| 85 | * Return value of this function can be used to allocate bitmap |
| 86 | * large enough to hold all bits for given type. |
| 87 | */ |
| 88 | static inline int get_n_events_by_type(int type) |
| 89 | { |
| 90 | BUG_ON(type != EV_SW && type != EV_KEY); |
| 91 | |
| 92 | return (type == EV_KEY) ? KEY_CNT : SW_CNT; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * gpio_keys_disable_button() - disables given GPIO button |
| 97 | * @bdata: button data for button to be disabled |
| 98 | * |
| 99 | * Disables button pointed by @bdata. This is done by masking |
| 100 | * IRQ line. After this function is called, button won't generate |
| 101 | * input events anymore. Note that one can only disable buttons |
| 102 | * that don't share IRQs. |
| 103 | * |
| 104 | * Make sure that @bdata->disable_lock is locked when entering |
| 105 | * this function to avoid races when concurrent threads are |
| 106 | * disabling buttons at the same time. |
| 107 | */ |
| 108 | static void gpio_keys_disable_button(struct gpio_button_data *bdata) |
| 109 | { |
| 110 | if (!bdata->disabled) { |
| 111 | /* |
| 112 | * Disable IRQ and possible debouncing timer. |
| 113 | */ |
| 114 | disable_irq(gpio_to_irq(bdata->button->gpio)); |
Grazvydas Ignotas | 28ed684 | 2010-06-28 10:59:32 -0700 | [diff] [blame] | 115 | if (bdata->timer_debounce) |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 116 | del_timer_sync(&bdata->timer); |
| 117 | |
| 118 | bdata->disabled = true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * gpio_keys_enable_button() - enables given GPIO button |
| 124 | * @bdata: button data for button to be disabled |
| 125 | * |
| 126 | * Enables given button pointed by @bdata. |
| 127 | * |
| 128 | * Make sure that @bdata->disable_lock is locked when entering |
| 129 | * this function to avoid races with concurrent threads trying |
| 130 | * to enable the same button at the same time. |
| 131 | */ |
| 132 | static void gpio_keys_enable_button(struct gpio_button_data *bdata) |
| 133 | { |
| 134 | if (bdata->disabled) { |
| 135 | enable_irq(gpio_to_irq(bdata->button->gpio)); |
| 136 | bdata->disabled = false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons |
| 142 | * @ddata: pointer to drvdata |
| 143 | * @buf: buffer where stringified bitmap is written |
| 144 | * @type: button type (%EV_KEY, %EV_SW) |
| 145 | * @only_disabled: does caller want only those buttons that are |
| 146 | * currently disabled or all buttons that can be |
| 147 | * disabled |
| 148 | * |
| 149 | * This function writes buttons that can be disabled to @buf. If |
| 150 | * @only_disabled is true, then @buf contains only those buttons |
| 151 | * that are currently disabled. Returns 0 on success or negative |
| 152 | * errno on failure. |
| 153 | */ |
| 154 | static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata, |
| 155 | char *buf, unsigned int type, |
| 156 | bool only_disabled) |
| 157 | { |
| 158 | int n_events = get_n_events_by_type(type); |
| 159 | unsigned long *bits; |
| 160 | ssize_t ret; |
| 161 | int i; |
| 162 | |
| 163 | bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL); |
| 164 | if (!bits) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | for (i = 0; i < ddata->n_buttons; i++) { |
| 168 | struct gpio_button_data *bdata = &ddata->data[i]; |
| 169 | |
| 170 | if (bdata->button->type != type) |
| 171 | continue; |
| 172 | |
| 173 | if (only_disabled && !bdata->disabled) |
| 174 | continue; |
| 175 | |
| 176 | __set_bit(bdata->button->code, bits); |
| 177 | } |
| 178 | |
| 179 | ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events); |
| 180 | buf[ret++] = '\n'; |
| 181 | buf[ret] = '\0'; |
| 182 | |
| 183 | kfree(bits); |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap |
| 190 | * @ddata: pointer to drvdata |
| 191 | * @buf: buffer from userspace that contains stringified bitmap |
| 192 | * @type: button type (%EV_KEY, %EV_SW) |
| 193 | * |
| 194 | * This function parses stringified bitmap from @buf and disables/enables |
| 195 | * GPIO buttons accordinly. Returns 0 on success and negative error |
| 196 | * on failure. |
| 197 | */ |
| 198 | static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, |
| 199 | const char *buf, unsigned int type) |
| 200 | { |
| 201 | int n_events = get_n_events_by_type(type); |
| 202 | unsigned long *bits; |
| 203 | ssize_t error; |
| 204 | int i; |
| 205 | |
| 206 | bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL); |
| 207 | if (!bits) |
| 208 | return -ENOMEM; |
| 209 | |
| 210 | error = bitmap_parselist(buf, bits, n_events); |
| 211 | if (error) |
| 212 | goto out; |
| 213 | |
| 214 | /* First validate */ |
| 215 | for (i = 0; i < ddata->n_buttons; i++) { |
| 216 | struct gpio_button_data *bdata = &ddata->data[i]; |
| 217 | |
| 218 | if (bdata->button->type != type) |
| 219 | continue; |
| 220 | |
| 221 | if (test_bit(bdata->button->code, bits) && |
| 222 | !bdata->button->can_disable) { |
| 223 | error = -EINVAL; |
| 224 | goto out; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | mutex_lock(&ddata->disable_lock); |
| 229 | |
| 230 | for (i = 0; i < ddata->n_buttons; i++) { |
| 231 | struct gpio_button_data *bdata = &ddata->data[i]; |
| 232 | |
| 233 | if (bdata->button->type != type) |
| 234 | continue; |
| 235 | |
| 236 | if (test_bit(bdata->button->code, bits)) |
| 237 | gpio_keys_disable_button(bdata); |
| 238 | else |
| 239 | gpio_keys_enable_button(bdata); |
| 240 | } |
| 241 | |
| 242 | mutex_unlock(&ddata->disable_lock); |
| 243 | |
| 244 | out: |
| 245 | kfree(bits); |
| 246 | return error; |
| 247 | } |
| 248 | |
| 249 | #define ATTR_SHOW_FN(name, type, only_disabled) \ |
| 250 | static ssize_t gpio_keys_show_##name(struct device *dev, \ |
| 251 | struct device_attribute *attr, \ |
| 252 | char *buf) \ |
| 253 | { \ |
| 254 | struct platform_device *pdev = to_platform_device(dev); \ |
| 255 | struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \ |
| 256 | \ |
| 257 | return gpio_keys_attr_show_helper(ddata, buf, \ |
| 258 | type, only_disabled); \ |
| 259 | } |
| 260 | |
| 261 | ATTR_SHOW_FN(keys, EV_KEY, false); |
| 262 | ATTR_SHOW_FN(switches, EV_SW, false); |
| 263 | ATTR_SHOW_FN(disabled_keys, EV_KEY, true); |
| 264 | ATTR_SHOW_FN(disabled_switches, EV_SW, true); |
| 265 | |
| 266 | /* |
| 267 | * ATTRIBUTES: |
| 268 | * |
| 269 | * /sys/devices/platform/gpio-keys/keys [ro] |
| 270 | * /sys/devices/platform/gpio-keys/switches [ro] |
| 271 | */ |
| 272 | static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL); |
| 273 | static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL); |
| 274 | |
| 275 | #define ATTR_STORE_FN(name, type) \ |
| 276 | static ssize_t gpio_keys_store_##name(struct device *dev, \ |
| 277 | struct device_attribute *attr, \ |
| 278 | const char *buf, \ |
| 279 | size_t count) \ |
| 280 | { \ |
| 281 | struct platform_device *pdev = to_platform_device(dev); \ |
| 282 | struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \ |
| 283 | ssize_t error; \ |
| 284 | \ |
| 285 | error = gpio_keys_attr_store_helper(ddata, buf, type); \ |
| 286 | if (error) \ |
| 287 | return error; \ |
| 288 | \ |
| 289 | return count; \ |
| 290 | } |
| 291 | |
| 292 | ATTR_STORE_FN(disabled_keys, EV_KEY); |
| 293 | ATTR_STORE_FN(disabled_switches, EV_SW); |
| 294 | |
| 295 | /* |
| 296 | * ATTRIBUTES: |
| 297 | * |
| 298 | * /sys/devices/platform/gpio-keys/disabled_keys [rw] |
| 299 | * /sys/devices/platform/gpio-keys/disables_switches [rw] |
| 300 | */ |
| 301 | static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO, |
| 302 | gpio_keys_show_disabled_keys, |
| 303 | gpio_keys_store_disabled_keys); |
| 304 | static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO, |
| 305 | gpio_keys_show_disabled_switches, |
| 306 | gpio_keys_store_disabled_switches); |
| 307 | |
| 308 | static struct attribute *gpio_keys_attrs[] = { |
| 309 | &dev_attr_keys.attr, |
| 310 | &dev_attr_switches.attr, |
| 311 | &dev_attr_disabled_keys.attr, |
| 312 | &dev_attr_disabled_switches.attr, |
| 313 | NULL, |
| 314 | }; |
| 315 | |
| 316 | static struct attribute_group gpio_keys_attr_group = { |
| 317 | .attrs = gpio_keys_attrs, |
| 318 | }; |
| 319 | |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 320 | static void gpio_keys_report_event(struct gpio_button_data *bdata) |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 321 | { |
Uwe Kleine-König | ce25d7e | 2008-08-08 12:14:36 -0400 | [diff] [blame] | 322 | struct gpio_keys_button *button = bdata->button; |
| 323 | struct input_dev *input = bdata->input; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 324 | unsigned int type = button->type ?: EV_KEY; |
| 325 | int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low; |
| 326 | |
| 327 | input_event(input, type, button->code, !!state); |
| 328 | input_sync(input); |
| 329 | } |
| 330 | |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 331 | static void gpio_keys_work_func(struct work_struct *work) |
| 332 | { |
| 333 | struct gpio_button_data *bdata = |
| 334 | container_of(work, struct gpio_button_data, work); |
| 335 | |
| 336 | gpio_keys_report_event(bdata); |
| 337 | } |
| 338 | |
Jani Nikula | da0d03f | 2009-06-28 22:38:56 -0700 | [diff] [blame] | 339 | static void gpio_keys_timer(unsigned long _data) |
Jani Nikula | ca865a7 | 2009-06-28 22:38:44 -0700 | [diff] [blame] | 340 | { |
| 341 | struct gpio_button_data *data = (struct gpio_button_data *)_data; |
| 342 | |
Jani Nikula | da0d03f | 2009-06-28 22:38:56 -0700 | [diff] [blame] | 343 | schedule_work(&data->work); |
Jani Nikula | ca865a7 | 2009-06-28 22:38:44 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 346 | static irqreturn_t gpio_keys_isr(int irq, void *dev_id) |
| 347 | { |
Uwe Kleine-König | 57ffe9d | 2008-08-08 12:14:34 -0400 | [diff] [blame] | 348 | struct gpio_button_data *bdata = dev_id; |
| 349 | struct gpio_keys_button *button = bdata->button; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 350 | |
Uwe Kleine-König | 57ffe9d | 2008-08-08 12:14:34 -0400 | [diff] [blame] | 351 | BUG_ON(irq != gpio_to_irq(button->gpio)); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 352 | |
Grazvydas Ignotas | 28ed684 | 2010-06-28 10:59:32 -0700 | [diff] [blame] | 353 | if (bdata->timer_debounce) |
Jani Nikula | ca865a7 | 2009-06-28 22:38:44 -0700 | [diff] [blame] | 354 | mod_timer(&bdata->timer, |
Grazvydas Ignotas | 28ed684 | 2010-06-28 10:59:32 -0700 | [diff] [blame] | 355 | jiffies + msecs_to_jiffies(bdata->timer_debounce)); |
Jani Nikula | ca865a7 | 2009-06-28 22:38:44 -0700 | [diff] [blame] | 356 | else |
Jani Nikula | da0d03f | 2009-06-28 22:38:56 -0700 | [diff] [blame] | 357 | schedule_work(&bdata->work); |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 358 | |
Uwe Kleine-König | 57ffe9d | 2008-08-08 12:14:34 -0400 | [diff] [blame] | 359 | return IRQ_HANDLED; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 360 | } |
| 361 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 362 | static int __devinit gpio_keys_setup_key(struct platform_device *pdev, |
Ben Dooks | bc8f1ea | 2009-11-10 21:01:31 -0800 | [diff] [blame] | 363 | struct gpio_button_data *bdata, |
| 364 | struct gpio_keys_button *button) |
| 365 | { |
| 366 | char *desc = button->desc ? button->desc : "gpio_keys"; |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 367 | struct device *dev = &pdev->dev; |
| 368 | unsigned long irqflags; |
Ben Dooks | bc8f1ea | 2009-11-10 21:01:31 -0800 | [diff] [blame] | 369 | int irq, error; |
| 370 | |
| 371 | setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata); |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 372 | INIT_WORK(&bdata->work, gpio_keys_work_func); |
Ben Dooks | bc8f1ea | 2009-11-10 21:01:31 -0800 | [diff] [blame] | 373 | |
| 374 | error = gpio_request(button->gpio, desc); |
| 375 | if (error < 0) { |
| 376 | dev_err(dev, "failed to request GPIO %d, error %d\n", |
| 377 | button->gpio, error); |
| 378 | goto fail2; |
| 379 | } |
| 380 | |
| 381 | error = gpio_direction_input(button->gpio); |
| 382 | if (error < 0) { |
| 383 | dev_err(dev, "failed to configure" |
| 384 | " direction for GPIO %d, error %d\n", |
| 385 | button->gpio, error); |
| 386 | goto fail3; |
| 387 | } |
| 388 | |
Grazvydas Ignotas | 28ed684 | 2010-06-28 10:59:32 -0700 | [diff] [blame] | 389 | if (button->debounce_interval) { |
| 390 | error = gpio_set_debounce(button->gpio, |
| 391 | button->debounce_interval * 1000); |
| 392 | /* use timer if gpiolib doesn't provide debounce */ |
| 393 | if (error < 0) |
| 394 | bdata->timer_debounce = button->debounce_interval; |
| 395 | } |
| 396 | |
Ben Dooks | bc8f1ea | 2009-11-10 21:01:31 -0800 | [diff] [blame] | 397 | irq = gpio_to_irq(button->gpio); |
| 398 | if (irq < 0) { |
| 399 | error = irq; |
| 400 | dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n", |
| 401 | button->gpio, error); |
| 402 | goto fail3; |
| 403 | } |
| 404 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 405 | irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING; |
| 406 | /* |
| 407 | * If platform has specified that the button can be disabled, |
| 408 | * we don't want it to share the interrupt line. |
| 409 | */ |
| 410 | if (!button->can_disable) |
| 411 | irqflags |= IRQF_SHARED; |
| 412 | |
| 413 | error = request_irq(irq, gpio_keys_isr, irqflags, desc, bdata); |
Ben Dooks | bc8f1ea | 2009-11-10 21:01:31 -0800 | [diff] [blame] | 414 | if (error) { |
| 415 | dev_err(dev, "Unable to claim irq %d; error %d\n", |
| 416 | irq, error); |
| 417 | goto fail3; |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | |
| 422 | fail3: |
| 423 | gpio_free(button->gpio); |
| 424 | fail2: |
| 425 | return error; |
| 426 | } |
| 427 | |
Shubhrajyoti D | 173bdd7 | 2010-08-03 19:44:40 -0700 | [diff] [blame] | 428 | static int gpio_keys_open(struct input_dev *input) |
| 429 | { |
| 430 | struct gpio_keys_drvdata *ddata = input_get_drvdata(input); |
| 431 | |
| 432 | return ddata->enable ? ddata->enable(input->dev.parent) : 0; |
| 433 | } |
| 434 | |
| 435 | static void gpio_keys_close(struct input_dev *input) |
| 436 | { |
| 437 | struct gpio_keys_drvdata *ddata = input_get_drvdata(input); |
| 438 | |
| 439 | if (ddata->disable) |
| 440 | ddata->disable(input->dev.parent); |
| 441 | } |
| 442 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 443 | static int __devinit gpio_keys_probe(struct platform_device *pdev) |
| 444 | { |
| 445 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 446 | struct gpio_keys_drvdata *ddata; |
Ben Dooks | db19fd8 | 2009-11-10 21:00:35 -0800 | [diff] [blame] | 447 | struct device *dev = &pdev->dev; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 448 | struct input_dev *input; |
| 449 | int i, error; |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 450 | int wakeup = 0; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 451 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 452 | ddata = kzalloc(sizeof(struct gpio_keys_drvdata) + |
| 453 | pdata->nbuttons * sizeof(struct gpio_button_data), |
| 454 | GFP_KERNEL); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 455 | input = input_allocate_device(); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 456 | if (!ddata || !input) { |
Ben Dooks | db19fd8 | 2009-11-10 21:00:35 -0800 | [diff] [blame] | 457 | dev_err(dev, "failed to allocate state\n"); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 458 | error = -ENOMEM; |
| 459 | goto fail1; |
| 460 | } |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 461 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 462 | ddata->input = input; |
| 463 | ddata->n_buttons = pdata->nbuttons; |
Shubhrajyoti D | 173bdd7 | 2010-08-03 19:44:40 -0700 | [diff] [blame] | 464 | ddata->enable = pdata->enable; |
| 465 | ddata->disable = pdata->disable; |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 466 | mutex_init(&ddata->disable_lock); |
| 467 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 468 | platform_set_drvdata(pdev, ddata); |
Shubhrajyoti D | 173bdd7 | 2010-08-03 19:44:40 -0700 | [diff] [blame] | 469 | input_set_drvdata(input, ddata); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 470 | |
| 471 | input->name = pdev->name; |
| 472 | input->phys = "gpio-keys/input0"; |
Dmitry Torokhov | 469ba4d | 2007-04-12 01:34:58 -0400 | [diff] [blame] | 473 | input->dev.parent = &pdev->dev; |
Shubhrajyoti D | 173bdd7 | 2010-08-03 19:44:40 -0700 | [diff] [blame] | 474 | input->open = gpio_keys_open; |
| 475 | input->close = gpio_keys_close; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 476 | |
| 477 | input->id.bustype = BUS_HOST; |
| 478 | input->id.vendor = 0x0001; |
| 479 | input->id.product = 0x0001; |
| 480 | input->id.version = 0x0100; |
| 481 | |
Dominic Curran | b67b4b1 | 2008-10-27 22:30:53 -0400 | [diff] [blame] | 482 | /* Enable auto repeat feature of Linux input subsystem */ |
| 483 | if (pdata->rep) |
| 484 | __set_bit(EV_REP, input->evbit); |
| 485 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 486 | for (i = 0; i < pdata->nbuttons; i++) { |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 487 | struct gpio_keys_button *button = &pdata->buttons[i]; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 488 | struct gpio_button_data *bdata = &ddata->data[i]; |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 489 | unsigned int type = button->type ?: EV_KEY; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 490 | |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 491 | bdata->input = input; |
Uwe Kleine-König | 74dd439 | 2008-07-30 10:33:43 -0400 | [diff] [blame] | 492 | bdata->button = button; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 493 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 494 | error = gpio_keys_setup_key(pdev, bdata, button); |
Ben Dooks | bc8f1ea | 2009-11-10 21:01:31 -0800 | [diff] [blame] | 495 | if (error) |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 496 | goto fail2; |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 497 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 498 | if (button->wakeup) |
| 499 | wakeup = 1; |
| 500 | |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 501 | input_set_capability(input, type, button->code); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 502 | } |
| 503 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 504 | error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group); |
| 505 | if (error) { |
| 506 | dev_err(dev, "Unable to export keys/switches, error: %d\n", |
| 507 | error); |
| 508 | goto fail2; |
| 509 | } |
| 510 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 511 | error = input_register_device(input); |
| 512 | if (error) { |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 513 | dev_err(dev, "Unable to register input device, error: %d\n", |
| 514 | error); |
| 515 | goto fail3; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 516 | } |
| 517 | |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 518 | /* get current state of buttons */ |
| 519 | for (i = 0; i < pdata->nbuttons; i++) |
| 520 | gpio_keys_report_event(&ddata->data[i]); |
| 521 | input_sync(input); |
| 522 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 523 | device_init_wakeup(&pdev->dev, wakeup); |
| 524 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 525 | return 0; |
| 526 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 527 | fail3: |
| 528 | sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 529 | fail2: |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 530 | while (--i >= 0) { |
Uwe Kleine-König | 57ffe9d | 2008-08-08 12:14:34 -0400 | [diff] [blame] | 531 | free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]); |
Grazvydas Ignotas | 28ed684 | 2010-06-28 10:59:32 -0700 | [diff] [blame] | 532 | if (ddata->data[i].timer_debounce) |
Jani Nikula | ca865a7 | 2009-06-28 22:38:44 -0700 | [diff] [blame] | 533 | del_timer_sync(&ddata->data[i].timer); |
Jani Nikula | da0d03f | 2009-06-28 22:38:56 -0700 | [diff] [blame] | 534 | cancel_work_sync(&ddata->data[i].work); |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 535 | gpio_free(pdata->buttons[i].gpio); |
| 536 | } |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 537 | |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 538 | platform_set_drvdata(pdev, NULL); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 539 | fail1: |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 540 | input_free_device(input); |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 541 | kfree(ddata); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 542 | |
| 543 | return error; |
| 544 | } |
| 545 | |
| 546 | static int __devexit gpio_keys_remove(struct platform_device *pdev) |
| 547 | { |
| 548 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
Dmitry Baryshkov | a33466e | 2008-05-07 16:30:15 -0400 | [diff] [blame] | 549 | struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); |
| 550 | struct input_dev *input = ddata->input; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 551 | int i; |
| 552 | |
Mika Westerberg | 9e3af04 | 2010-02-04 00:48:00 -0800 | [diff] [blame] | 553 | sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group); |
| 554 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 555 | device_init_wakeup(&pdev->dev, 0); |
| 556 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 557 | for (i = 0; i < pdata->nbuttons; i++) { |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 558 | int irq = gpio_to_irq(pdata->buttons[i].gpio); |
Uwe Kleine-König | 57ffe9d | 2008-08-08 12:14:34 -0400 | [diff] [blame] | 559 | free_irq(irq, &ddata->data[i]); |
Grazvydas Ignotas | 28ed684 | 2010-06-28 10:59:32 -0700 | [diff] [blame] | 560 | if (ddata->data[i].timer_debounce) |
Jani Nikula | ca865a7 | 2009-06-28 22:38:44 -0700 | [diff] [blame] | 561 | del_timer_sync(&ddata->data[i].timer); |
Jani Nikula | da0d03f | 2009-06-28 22:38:56 -0700 | [diff] [blame] | 562 | cancel_work_sync(&ddata->data[i].work); |
Herbert Valerio Riedel | 6a2e391 | 2007-11-21 14:42:33 -0500 | [diff] [blame] | 563 | gpio_free(pdata->buttons[i].gpio); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | input_unregister_device(input); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 571 | |
| 572 | #ifdef CONFIG_PM |
Mike Rapoport | ae78e0e | 2009-07-22 23:02:54 -0700 | [diff] [blame] | 573 | static int gpio_keys_suspend(struct device *dev) |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 574 | { |
Mike Rapoport | ae78e0e | 2009-07-22 23:02:54 -0700 | [diff] [blame] | 575 | struct platform_device *pdev = to_platform_device(dev); |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 576 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 577 | int i; |
| 578 | |
| 579 | if (device_may_wakeup(&pdev->dev)) { |
| 580 | for (i = 0; i < pdata->nbuttons; i++) { |
| 581 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 582 | if (button->wakeup) { |
| 583 | int irq = gpio_to_irq(button->gpio); |
| 584 | enable_irq_wake(irq); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
Mike Rapoport | ae78e0e | 2009-07-22 23:02:54 -0700 | [diff] [blame] | 592 | static int gpio_keys_resume(struct device *dev) |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 593 | { |
Mike Rapoport | ae78e0e | 2009-07-22 23:02:54 -0700 | [diff] [blame] | 594 | struct platform_device *pdev = to_platform_device(dev); |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 595 | struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 596 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 597 | int i; |
| 598 | |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 599 | for (i = 0; i < pdata->nbuttons; i++) { |
| 600 | |
| 601 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 602 | if (button->wakeup && device_may_wakeup(&pdev->dev)) { |
| 603 | int irq = gpio_to_irq(button->gpio); |
| 604 | disable_irq_wake(irq); |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 605 | } |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 606 | |
| 607 | gpio_keys_report_event(&ddata->data[i]); |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 608 | } |
Daniel Mack | 6ee88d7 | 2009-11-30 00:04:02 -0800 | [diff] [blame] | 609 | input_sync(ddata->input); |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 610 | |
| 611 | return 0; |
| 612 | } |
Mike Rapoport | ae78e0e | 2009-07-22 23:02:54 -0700 | [diff] [blame] | 613 | |
| 614 | static const struct dev_pm_ops gpio_keys_pm_ops = { |
| 615 | .suspend = gpio_keys_suspend, |
| 616 | .resume = gpio_keys_resume, |
| 617 | }; |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 618 | #endif |
| 619 | |
Uwe Kleine-König | 9b07044 | 2008-07-30 10:34:02 -0400 | [diff] [blame] | 620 | static struct platform_driver gpio_keys_device_driver = { |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 621 | .probe = gpio_keys_probe, |
| 622 | .remove = __devexit_p(gpio_keys_remove), |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 623 | .driver = { |
| 624 | .name = "gpio-keys", |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 625 | .owner = THIS_MODULE, |
Mike Rapoport | ae78e0e | 2009-07-22 23:02:54 -0700 | [diff] [blame] | 626 | #ifdef CONFIG_PM |
| 627 | .pm = &gpio_keys_pm_ops, |
| 628 | #endif |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 629 | } |
| 630 | }; |
| 631 | |
| 632 | static int __init gpio_keys_init(void) |
| 633 | { |
| 634 | return platform_driver_register(&gpio_keys_device_driver); |
| 635 | } |
| 636 | |
| 637 | static void __exit gpio_keys_exit(void) |
| 638 | { |
| 639 | platform_driver_unregister(&gpio_keys_device_driver); |
| 640 | } |
| 641 | |
| 642 | module_init(gpio_keys_init); |
| 643 | module_exit(gpio_keys_exit); |
| 644 | |
| 645 | MODULE_LICENSE("GPL"); |
| 646 | MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>"); |
| 647 | MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs"); |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 648 | MODULE_ALIAS("platform:gpio-keys"); |