blob: 97bada4b680d210fe618412ba2e692c463a22a12 [file] [log] [blame]
Phil Blundell78a56aa2007-01-18 00:44:09 -05001/*
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 Blundell78a56aa2007-01-18 00:44:09 -050012
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 Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050020#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 Brownell49015be2007-03-05 00:30:22 -080025#include <linux/gpio_keys.h>
Jani Nikulada0d03f2009-06-28 22:38:56 -070026#include <linux/workqueue.h>
Ben Dooks111bc592009-11-10 21:01:31 -080027#include <linux/gpio.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050028
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040029struct gpio_button_data {
30 struct gpio_keys_button *button;
31 struct input_dev *input;
Jani Nikulaca865a72009-06-28 22:38:44 -070032 struct timer_list timer;
Jani Nikulada0d03f2009-06-28 22:38:56 -070033 struct work_struct work;
Grazvydas Ignotas28ed6842010-06-28 10:59:32 -070034 int timer_debounce; /* in msecs */
Mika Westerberg9e3af042010-02-04 00:48:00 -080035 bool disabled;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040036};
37
38struct gpio_keys_drvdata {
39 struct input_dev *input;
Mika Westerberg9e3af042010-02-04 00:48:00 -080040 struct mutex disable_lock;
41 unsigned int n_buttons;
Shubhrajyoti D173bdd72010-08-03 19:44:40 -070042 int (*enable)(struct device *dev);
43 void (*disable)(struct device *dev);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -040044 struct gpio_button_data data[0];
45};
46
Mika Westerberg9e3af042010-02-04 00:48:00 -080047/*
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 */
88static 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 */
108static 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 Ignotas28ed6842010-06-28 10:59:32 -0700115 if (bdata->timer_debounce)
Mika Westerberg9e3af042010-02-04 00:48:00 -0800116 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 */
132static 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 */
154static 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 */
198static 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
244out:
245 kfree(bits);
246 return error;
247}
248
249#define ATTR_SHOW_FN(name, type, only_disabled) \
250static 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
261ATTR_SHOW_FN(keys, EV_KEY, false);
262ATTR_SHOW_FN(switches, EV_SW, false);
263ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
264ATTR_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 */
272static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
273static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
274
275#define ATTR_STORE_FN(name, type) \
276static 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
292ATTR_STORE_FN(disabled_keys, EV_KEY);
293ATTR_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 */
301static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
302 gpio_keys_show_disabled_keys,
303 gpio_keys_store_disabled_keys);
304static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
305 gpio_keys_show_disabled_switches,
306 gpio_keys_store_disabled_switches);
307
308static 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
316static struct attribute_group gpio_keys_attr_group = {
317 .attrs = gpio_keys_attrs,
318};
319
Daniel Mack6ee88d72009-11-30 00:04:02 -0800320static void gpio_keys_report_event(struct gpio_button_data *bdata)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400321{
Uwe Kleine-Königce25d7e2008-08-08 12:14:36 -0400322 struct gpio_keys_button *button = bdata->button;
323 struct input_dev *input = bdata->input;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400324 unsigned int type = button->type ?: EV_KEY;
Philippe Langlais94a8cab2011-01-20 23:09:30 -0800325 int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400326
Alexander Stein92a47672011-04-11 23:34:37 -0700327 if (type == EV_ABS) {
328 if (state)
329 input_event(input, type, button->code, button->value);
330 } else {
331 input_event(input, type, button->code, !!state);
332 }
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400333 input_sync(input);
334}
335
Daniel Mack6ee88d72009-11-30 00:04:02 -0800336static void gpio_keys_work_func(struct work_struct *work)
337{
338 struct gpio_button_data *bdata =
339 container_of(work, struct gpio_button_data, work);
340
341 gpio_keys_report_event(bdata);
342}
343
Jani Nikulada0d03f2009-06-28 22:38:56 -0700344static void gpio_keys_timer(unsigned long _data)
Jani Nikulaca865a72009-06-28 22:38:44 -0700345{
346 struct gpio_button_data *data = (struct gpio_button_data *)_data;
347
Jani Nikulada0d03f2009-06-28 22:38:56 -0700348 schedule_work(&data->work);
Jani Nikulaca865a72009-06-28 22:38:44 -0700349}
350
Phil Blundell78a56aa2007-01-18 00:44:09 -0500351static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
352{
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400353 struct gpio_button_data *bdata = dev_id;
354 struct gpio_keys_button *button = bdata->button;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500355
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400356 BUG_ON(irq != gpio_to_irq(button->gpio));
Phil Blundell78a56aa2007-01-18 00:44:09 -0500357
Grazvydas Ignotas28ed6842010-06-28 10:59:32 -0700358 if (bdata->timer_debounce)
Jani Nikulaca865a72009-06-28 22:38:44 -0700359 mod_timer(&bdata->timer,
Grazvydas Ignotas28ed6842010-06-28 10:59:32 -0700360 jiffies + msecs_to_jiffies(bdata->timer_debounce));
Jani Nikulaca865a72009-06-28 22:38:44 -0700361 else
Jani Nikulada0d03f2009-06-28 22:38:56 -0700362 schedule_work(&bdata->work);
Roman Moravcik84767d02007-05-01 00:39:13 -0400363
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400364 return IRQ_HANDLED;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500365}
366
Mika Westerberg9e3af042010-02-04 00:48:00 -0800367static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800368 struct gpio_button_data *bdata,
369 struct gpio_keys_button *button)
370{
Alexander Stein92a47672011-04-11 23:34:37 -0700371 const char *desc = button->desc ? button->desc : "gpio_keys";
Mika Westerberg9e3af042010-02-04 00:48:00 -0800372 struct device *dev = &pdev->dev;
373 unsigned long irqflags;
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800374 int irq, error;
375
376 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
Daniel Mack6ee88d72009-11-30 00:04:02 -0800377 INIT_WORK(&bdata->work, gpio_keys_work_func);
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800378
379 error = gpio_request(button->gpio, desc);
380 if (error < 0) {
381 dev_err(dev, "failed to request GPIO %d, error %d\n",
382 button->gpio, error);
383 goto fail2;
384 }
385
386 error = gpio_direction_input(button->gpio);
387 if (error < 0) {
388 dev_err(dev, "failed to configure"
389 " direction for GPIO %d, error %d\n",
390 button->gpio, error);
391 goto fail3;
392 }
393
Grazvydas Ignotas28ed6842010-06-28 10:59:32 -0700394 if (button->debounce_interval) {
395 error = gpio_set_debounce(button->gpio,
396 button->debounce_interval * 1000);
397 /* use timer if gpiolib doesn't provide debounce */
398 if (error < 0)
399 bdata->timer_debounce = button->debounce_interval;
400 }
401
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800402 irq = gpio_to_irq(button->gpio);
403 if (irq < 0) {
404 error = irq;
405 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
406 button->gpio, error);
407 goto fail3;
408 }
409
Mika Westerberg9e3af042010-02-04 00:48:00 -0800410 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
411 /*
412 * If platform has specified that the button can be disabled,
413 * we don't want it to share the interrupt line.
414 */
415 if (!button->can_disable)
416 irqflags |= IRQF_SHARED;
417
David Jander7e2ecdf2011-06-21 14:26:18 -0700418 error = request_threaded_irq(irq, NULL, gpio_keys_isr, irqflags, desc, bdata);
Philippe Langlais94a8cab2011-01-20 23:09:30 -0800419 if (error < 0) {
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800420 dev_err(dev, "Unable to claim irq %d; error %d\n",
421 irq, error);
422 goto fail3;
423 }
424
425 return 0;
426
427fail3:
428 gpio_free(button->gpio);
429fail2:
430 return error;
431}
432
Shubhrajyoti D173bdd72010-08-03 19:44:40 -0700433static int gpio_keys_open(struct input_dev *input)
434{
435 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
436
437 return ddata->enable ? ddata->enable(input->dev.parent) : 0;
438}
439
440static void gpio_keys_close(struct input_dev *input)
441{
442 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
443
444 if (ddata->disable)
445 ddata->disable(input->dev.parent);
446}
447
Phil Blundell78a56aa2007-01-18 00:44:09 -0500448static int __devinit gpio_keys_probe(struct platform_device *pdev)
449{
450 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400451 struct gpio_keys_drvdata *ddata;
Ben Dooksdb19fd82009-11-10 21:00:35 -0800452 struct device *dev = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500453 struct input_dev *input;
454 int i, error;
Anti Sulline15b0212007-09-26 00:01:17 -0400455 int wakeup = 0;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500456
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400457 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
458 pdata->nbuttons * sizeof(struct gpio_button_data),
459 GFP_KERNEL);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500460 input = input_allocate_device();
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400461 if (!ddata || !input) {
Ben Dooksdb19fd82009-11-10 21:00:35 -0800462 dev_err(dev, "failed to allocate state\n");
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400463 error = -ENOMEM;
464 goto fail1;
465 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500466
Mika Westerberg9e3af042010-02-04 00:48:00 -0800467 ddata->input = input;
468 ddata->n_buttons = pdata->nbuttons;
Shubhrajyoti D173bdd72010-08-03 19:44:40 -0700469 ddata->enable = pdata->enable;
470 ddata->disable = pdata->disable;
Mika Westerberg9e3af042010-02-04 00:48:00 -0800471 mutex_init(&ddata->disable_lock);
472
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400473 platform_set_drvdata(pdev, ddata);
Shubhrajyoti D173bdd72010-08-03 19:44:40 -0700474 input_set_drvdata(input, ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500475
Alexander Stein46711272011-04-11 23:34:48 -0700476 input->name = pdata->name ? : pdev->name;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500477 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400478 input->dev.parent = &pdev->dev;
Shubhrajyoti D173bdd72010-08-03 19:44:40 -0700479 input->open = gpio_keys_open;
480 input->close = gpio_keys_close;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500481
482 input->id.bustype = BUS_HOST;
483 input->id.vendor = 0x0001;
484 input->id.product = 0x0001;
485 input->id.version = 0x0100;
486
Dominic Curranb67b4b12008-10-27 22:30:53 -0400487 /* Enable auto repeat feature of Linux input subsystem */
488 if (pdata->rep)
489 __set_bit(EV_REP, input->evbit);
490
Phil Blundell78a56aa2007-01-18 00:44:09 -0500491 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -0400492 struct gpio_keys_button *button = &pdata->buttons[i];
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400493 struct gpio_button_data *bdata = &ddata->data[i];
Roman Moravcik84767d02007-05-01 00:39:13 -0400494 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500495
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400496 bdata->input = input;
Uwe Kleine-König74dd4392008-07-30 10:33:43 -0400497 bdata->button = button;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400498
Mika Westerberg9e3af042010-02-04 00:48:00 -0800499 error = gpio_keys_setup_key(pdev, bdata, button);
Ben Dooksbc8f1ea2009-11-10 21:01:31 -0800500 if (error)
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400501 goto fail2;
Roman Moravcik84767d02007-05-01 00:39:13 -0400502
Anti Sulline15b0212007-09-26 00:01:17 -0400503 if (button->wakeup)
504 wakeup = 1;
505
Roman Moravcik84767d02007-05-01 00:39:13 -0400506 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500507 }
508
Mika Westerberg9e3af042010-02-04 00:48:00 -0800509 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
510 if (error) {
511 dev_err(dev, "Unable to export keys/switches, error: %d\n",
512 error);
513 goto fail2;
514 }
515
Phil Blundell78a56aa2007-01-18 00:44:09 -0500516 error = input_register_device(input);
517 if (error) {
Mika Westerberg9e3af042010-02-04 00:48:00 -0800518 dev_err(dev, "Unable to register input device, error: %d\n",
519 error);
520 goto fail3;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500521 }
522
Daniel Mack6ee88d72009-11-30 00:04:02 -0800523 /* get current state of buttons */
524 for (i = 0; i < pdata->nbuttons; i++)
525 gpio_keys_report_event(&ddata->data[i]);
526 input_sync(input);
527
Anti Sulline15b0212007-09-26 00:01:17 -0400528 device_init_wakeup(&pdev->dev, wakeup);
529
Phil Blundell78a56aa2007-01-18 00:44:09 -0500530 return 0;
531
Mika Westerberg9e3af042010-02-04 00:48:00 -0800532 fail3:
533 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400534 fail2:
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500535 while (--i >= 0) {
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400536 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
Grazvydas Ignotas28ed6842010-06-28 10:59:32 -0700537 if (ddata->data[i].timer_debounce)
Jani Nikulaca865a72009-06-28 22:38:44 -0700538 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700539 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500540 gpio_free(pdata->buttons[i].gpio);
541 }
Phil Blundell78a56aa2007-01-18 00:44:09 -0500542
Anti Sullin006df302007-09-26 00:01:03 -0400543 platform_set_drvdata(pdev, NULL);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400544 fail1:
Phil Blundell78a56aa2007-01-18 00:44:09 -0500545 input_free_device(input);
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400546 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500547
548 return error;
549}
550
551static int __devexit gpio_keys_remove(struct platform_device *pdev)
552{
553 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
Dmitry Baryshkova33466e2008-05-07 16:30:15 -0400554 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
555 struct input_dev *input = ddata->input;
Phil Blundell78a56aa2007-01-18 00:44:09 -0500556 int i;
557
Mika Westerberg9e3af042010-02-04 00:48:00 -0800558 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
559
Anti Sulline15b0212007-09-26 00:01:17 -0400560 device_init_wakeup(&pdev->dev, 0);
561
Phil Blundell78a56aa2007-01-18 00:44:09 -0500562 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500563 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Uwe Kleine-König57ffe9d2008-08-08 12:14:34 -0400564 free_irq(irq, &ddata->data[i]);
Grazvydas Ignotas28ed6842010-06-28 10:59:32 -0700565 if (ddata->data[i].timer_debounce)
Jani Nikulaca865a72009-06-28 22:38:44 -0700566 del_timer_sync(&ddata->data[i].timer);
Jani Nikulada0d03f2009-06-28 22:38:56 -0700567 cancel_work_sync(&ddata->data[i].work);
Herbert Valerio Riedel6a2e3912007-11-21 14:42:33 -0500568 gpio_free(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500569 }
570
571 input_unregister_device(input);
Axel Lin16382072011-06-28 14:23:30 -0700572 kfree(ddata);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500573
574 return 0;
575}
576
Anti Sulline15b0212007-09-26 00:01:17 -0400577
578#ifdef CONFIG_PM
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700579static int gpio_keys_suspend(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400580{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700581 struct platform_device *pdev = to_platform_device(dev);
Anti Sulline15b0212007-09-26 00:01:17 -0400582 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
583 int i;
584
585 if (device_may_wakeup(&pdev->dev)) {
586 for (i = 0; i < pdata->nbuttons; i++) {
587 struct gpio_keys_button *button = &pdata->buttons[i];
588 if (button->wakeup) {
589 int irq = gpio_to_irq(button->gpio);
590 enable_irq_wake(irq);
591 }
592 }
593 }
594
595 return 0;
596}
597
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700598static int gpio_keys_resume(struct device *dev)
Anti Sulline15b0212007-09-26 00:01:17 -0400599{
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700600 struct platform_device *pdev = to_platform_device(dev);
Daniel Mack6ee88d72009-11-30 00:04:02 -0800601 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
Anti Sulline15b0212007-09-26 00:01:17 -0400602 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
603 int i;
604
Daniel Mack6ee88d72009-11-30 00:04:02 -0800605 for (i = 0; i < pdata->nbuttons; i++) {
606
607 struct gpio_keys_button *button = &pdata->buttons[i];
608 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
609 int irq = gpio_to_irq(button->gpio);
610 disable_irq_wake(irq);
Anti Sulline15b0212007-09-26 00:01:17 -0400611 }
Daniel Mack6ee88d72009-11-30 00:04:02 -0800612
613 gpio_keys_report_event(&ddata->data[i]);
Anti Sulline15b0212007-09-26 00:01:17 -0400614 }
Daniel Mack6ee88d72009-11-30 00:04:02 -0800615 input_sync(ddata->input);
Anti Sulline15b0212007-09-26 00:01:17 -0400616
617 return 0;
618}
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700619
620static const struct dev_pm_ops gpio_keys_pm_ops = {
621 .suspend = gpio_keys_suspend,
622 .resume = gpio_keys_resume,
623};
Anti Sulline15b0212007-09-26 00:01:17 -0400624#endif
625
Uwe Kleine-König9b070442008-07-30 10:34:02 -0400626static struct platform_driver gpio_keys_device_driver = {
Phil Blundell78a56aa2007-01-18 00:44:09 -0500627 .probe = gpio_keys_probe,
628 .remove = __devexit_p(gpio_keys_remove),
Phil Blundell78a56aa2007-01-18 00:44:09 -0500629 .driver = {
630 .name = "gpio-keys",
Kay Sieversd7b52472008-04-18 00:24:42 -0400631 .owner = THIS_MODULE,
Mike Rapoportae78e0e2009-07-22 23:02:54 -0700632#ifdef CONFIG_PM
633 .pm = &gpio_keys_pm_ops,
634#endif
Phil Blundell78a56aa2007-01-18 00:44:09 -0500635 }
636};
637
638static int __init gpio_keys_init(void)
639{
640 return platform_driver_register(&gpio_keys_device_driver);
641}
642
643static void __exit gpio_keys_exit(void)
644{
645 platform_driver_unregister(&gpio_keys_device_driver);
646}
647
David Janderb2330202011-06-23 01:30:09 -0700648late_initcall(gpio_keys_init);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500649module_exit(gpio_keys_exit);
650
651MODULE_LICENSE("GPL");
652MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
David Jander7e2ecdf2011-06-21 14:26:18 -0700653MODULE_DESCRIPTION("Keyboard driver for GPIOs");
Kay Sieversd7b52472008-04-18 00:24:42 -0400654MODULE_ALIAS("platform:gpio-keys");