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