upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / input / keyboard / gpio_keys.c
1 /*
2  * Driver for keys on GPIO lines capable of generating interrupts.
3  *
4  * Copyright 2005 Phil Blundell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12
13 #include <linux/init.h>
14 #include <linux/fs.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.h>
18 #include <linux/pm.h>
19 #include <linux/slab.h>
20 #include <linux/sysctl.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/input.h>
25 #include <linux/gpio_keys.h>
26 #include <linux/workqueue.h>
27 #include <linux/gpio.h>
28
29 struct gpio_button_data {
30         struct gpio_keys_button *button;
31         struct input_dev *input;
32         struct timer_list timer;
33         struct work_struct work;
34         int timer_debounce;     /* in msecs */
35         bool disabled;
36         bool key_state;
37 };
38
39 struct gpio_keys_drvdata {
40         struct input_dev *input;
41         struct mutex disable_lock;
42         unsigned int n_buttons;
43         int (*enable)(struct device *dev);
44         void (*disable)(struct device *dev);
45         struct gpio_button_data data[0];
46 };
47
48 static struct input_dev *input_temp;
49 static int gpiokey_debug_flag;
50
51 void earjack_key_report(unsigned int keycode, int value)
52 {
53         input_event(input_temp, EV_KEY, keycode, value);
54         input_sync(input_temp);
55         if (gpiokey_debug_flag)
56                 printk(KERN_INFO"[EARKEY] keypad : 0x%x, press : %d\n",
57                                 keycode, value);
58 }
59 EXPORT_SYMBOL(earjack_key_report);
60
61 /*
62  * SYSFS interface for enabling/disabling keys and switches:
63  *
64  * There are 4 attributes under /sys/devices/platform/gpio-keys/
65  *      keys [ro]              - bitmap of keys (EV_KEY) which can be
66  *                               disabled
67  *      switches [ro]          - bitmap of switches (EV_SW) which can be
68  *                               disabled
69  *      disabled_keys [rw]     - bitmap of keys currently disabled
70  *      disabled_switches [rw] - bitmap of switches currently disabled
71  *
72  * Userland can change these values and hence disable event generation
73  * for each key (or switch). Disabling a key means its interrupt line
74  * is disabled.
75  *
76  * For example, if we have following switches set up as gpio-keys:
77  *      SW_DOCK = 5
78  *      SW_CAMERA_LENS_COVER = 9
79  *      SW_KEYPAD_SLIDE = 10
80  *      SW_FRONT_PROXIMITY = 11
81  * This is read from switches:
82  *      11-9,5
83  * Next we want to disable proximity (11) and dock (5), we write:
84  *      11,5
85  * to file disabled_switches. Now proximity and dock IRQs are disabled.
86  * This can be verified by reading the file disabled_switches:
87  *      11,5
88  * If we now want to enable proximity (11) switch we write:
89  *      5
90  * to disabled_switches.
91  *
92  * We can disable only those keys which don't allow sharing the irq.
93  */
94
95 /**
96  * get_n_events_by_type() - returns maximum number of events per @type
97  * @type: type of button (%EV_KEY, %EV_SW)
98  *
99  * Return value of this function can be used to allocate bitmap
100  * large enough to hold all bits for given type.
101  */
102 static inline int get_n_events_by_type(int type)
103 {
104         BUG_ON(type != EV_SW && type != EV_KEY);
105
106         return (type == EV_KEY) ? KEY_CNT : SW_CNT;
107 }
108
109 /**
110  * gpio_keys_disable_button() - disables given GPIO button
111  * @bdata: button data for button to be disabled
112  *
113  * Disables button pointed by @bdata. This is done by masking
114  * IRQ line. After this function is called, button won't generate
115  * input events anymore. Note that one can only disable buttons
116  * that don't share IRQs.
117  *
118  * Make sure that @bdata->disable_lock is locked when entering
119  * this function to avoid races when concurrent threads are
120  * disabling buttons at the same time.
121  */
122 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
123 {
124         if (!bdata->disabled) {
125                 /*
126                  * Disable IRQ and possible debouncing timer.
127                  */
128                 disable_irq(gpio_to_irq(bdata->button->gpio));
129                 if (bdata->timer_debounce)
130                         del_timer_sync(&bdata->timer);
131
132                 bdata->disabled = true;
133         }
134 }
135
136 /**
137  * gpio_keys_enable_button() - enables given GPIO button
138  * @bdata: button data for button to be disabled
139  *
140  * Enables given button pointed by @bdata.
141  *
142  * Make sure that @bdata->disable_lock is locked when entering
143  * this function to avoid races with concurrent threads trying
144  * to enable the same button at the same time.
145  */
146 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
147 {
148         if (bdata->disabled) {
149                 enable_irq(gpio_to_irq(bdata->button->gpio));
150                 bdata->disabled = false;
151         }
152 }
153
154 /**
155  * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
156  * @ddata: pointer to drvdata
157  * @buf: buffer where stringified bitmap is written
158  * @type: button type (%EV_KEY, %EV_SW)
159  * @only_disabled: does caller want only those buttons that are
160  *                 currently disabled or all buttons that can be
161  *                 disabled
162  *
163  * This function writes buttons that can be disabled to @buf. If
164  * @only_disabled is true, then @buf contains only those buttons
165  * that are currently disabled. Returns 0 on success or negative
166  * errno on failure.
167  */
168 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
169                                           char *buf, unsigned int type,
170                                           bool only_disabled)
171 {
172         int n_events = get_n_events_by_type(type);
173         unsigned long *bits;
174         ssize_t ret;
175         int i;
176
177         bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
178         if (!bits)
179                 return -ENOMEM;
180
181         for (i = 0; i < ddata->n_buttons; i++) {
182                 struct gpio_button_data *bdata = &ddata->data[i];
183
184                 if (bdata->button->type != type)
185                         continue;
186
187                 if (only_disabled && !bdata->disabled)
188                         continue;
189
190                 __set_bit(bdata->button->code, bits);
191         }
192
193         ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
194         buf[ret++] = '\n';
195         buf[ret] = '\0';
196
197         kfree(bits);
198
199         return ret;
200 }
201
202 /**
203  * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
204  * @ddata: pointer to drvdata
205  * @buf: buffer from userspace that contains stringified bitmap
206  * @type: button type (%EV_KEY, %EV_SW)
207  *
208  * This function parses stringified bitmap from @buf and disables/enables
209  * GPIO buttons accordinly. Returns 0 on success and negative error
210  * on failure.
211  */
212 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
213                                            const char *buf, unsigned int type)
214 {
215         int n_events = get_n_events_by_type(type);
216         unsigned long *bits;
217         ssize_t error;
218         int i;
219
220         bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
221         if (!bits)
222                 return -ENOMEM;
223
224         error = bitmap_parselist(buf, bits, n_events);
225         if (error)
226                 goto out;
227
228         /* First validate */
229         for (i = 0; i < ddata->n_buttons; i++) {
230                 struct gpio_button_data *bdata = &ddata->data[i];
231
232                 if (bdata->button->type != type)
233                         continue;
234
235                 if (test_bit(bdata->button->code, bits) &&
236                     !bdata->button->can_disable) {
237                         error = -EINVAL;
238                         goto out;
239                 }
240         }
241
242         mutex_lock(&ddata->disable_lock);
243
244         for (i = 0; i < ddata->n_buttons; i++) {
245                 struct gpio_button_data *bdata = &ddata->data[i];
246
247                 if (bdata->button->type != type)
248                         continue;
249
250                 if (test_bit(bdata->button->code, bits))
251                         gpio_keys_disable_button(bdata);
252                 else
253                         gpio_keys_enable_button(bdata);
254         }
255
256         mutex_unlock(&ddata->disable_lock);
257
258 out:
259         kfree(bits);
260         return error;
261 }
262
263 #define ATTR_SHOW_FN(name, type, only_disabled)                         \
264 static ssize_t gpio_keys_show_##name(struct device *dev,                \
265                                      struct device_attribute *attr,     \
266                                      char *buf)                         \
267 {                                                                       \
268         struct platform_device *pdev = to_platform_device(dev);         \
269         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);   \
270                                                                         \
271         return gpio_keys_attr_show_helper(ddata, buf,                   \
272                                           type, only_disabled);         \
273 }
274
275 ATTR_SHOW_FN(keys, EV_KEY, false);
276 ATTR_SHOW_FN(switches, EV_SW, false);
277 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
278 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
279
280 /*
281  * ATTRIBUTES:
282  *
283  * /sys/devices/platform/gpio-keys.0/keys [ro]
284  * /sys/devices/platform/gpio-keys.0/switches [ro]
285  * /sys/devices/platform/gpio-keys.0/key_pressed [ro]
286  */
287 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
288 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
289
290 #define ATTR_STORE_FN(name, type)                                       \
291 static ssize_t gpio_keys_store_##name(struct device *dev,               \
292                                       struct device_attribute *attr,    \
293                                       const char *buf,                  \
294                                       size_t count)                     \
295 {                                                                       \
296         struct platform_device *pdev = to_platform_device(dev);         \
297         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);   \
298         ssize_t error;                                                  \
299                                                                         \
300         error = gpio_keys_attr_store_helper(ddata, buf, type);          \
301         if (error)                                                      \
302                 return error;                                           \
303                                                                         \
304         return count;                                                   \
305 }
306
307 ATTR_STORE_FN(disabled_keys, EV_KEY);
308 ATTR_STORE_FN(disabled_switches, EV_SW);
309
310 /*
311  * ATTRIBUTES:
312  *
313  * /sys/devices/platform/gpio-keys/disabled_keys [rw]
314  * /sys/devices/platform/gpio-keys/disables_switches [rw]
315  */
316 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
317                    gpio_keys_show_disabled_keys,
318                    gpio_keys_store_disabled_keys);
319 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
320                    gpio_keys_show_disabled_switches,
321                    gpio_keys_store_disabled_switches);
322
323 static ssize_t key_pressed_show(struct device *dev,
324                                 struct device_attribute *attr, char *buf)
325 {
326         struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
327         int i;
328         int keystate = 0;
329
330         for (i = 0; i < ddata->n_buttons; i++) {
331                 struct gpio_button_data *bdata = &ddata->data[i];
332                 keystate |= bdata->key_state;
333         }
334
335         if (keystate)
336                 sprintf(buf, "PRESS");
337         else
338                 sprintf(buf, "RELEASE");
339
340         return strlen(buf);
341 }
342
343 static DEVICE_ATTR(key_pressed, 0664, key_pressed_show, NULL);
344
345 static ssize_t sysfs_debug_store(struct device *dev,
346                 struct device_attribute *attr, const char *buf, size_t count)
347 {
348         unsigned int type;
349
350         sscanf(buf, "%d", &type);
351
352         if (type == 0) {
353                 printk(KERN_INFO"gpio-keys : disable gpio key log\n");
354                 gpiokey_debug_flag = 0;
355         } else if (type == 1) {
356                 printk(KERN_INFO"gpio-keys : enable gpio key log\n");
357                 gpiokey_debug_flag = 1;
358         }
359         return count;
360 }
361
362 static ssize_t sysfs_debug_show(struct device *dev,
363                 struct device_attribute *attr, char *buf)
364 {
365         printk(KERN_INFO"gpio-keys : gpiokey_debug_flag = %d\n",
366                         gpiokey_debug_flag);
367
368         return 0;
369 }
370
371 static DEVICE_ATTR(debug, 0664, sysfs_debug_show, sysfs_debug_store);
372
373 static struct attribute *gpio_keys_attrs[] = {
374         &dev_attr_keys.attr,
375         &dev_attr_switches.attr,
376         &dev_attr_key_pressed.attr,
377         &dev_attr_disabled_keys.attr,
378         &dev_attr_disabled_switches.attr,
379         &dev_attr_debug.attr,
380         NULL,
381 };
382
383 static struct attribute_group gpio_keys_attr_group = {
384         .attrs = gpio_keys_attrs,
385 };
386
387 static void gpio_keys_report_event(struct gpio_button_data *bdata)
388 {
389         struct gpio_keys_button *button = bdata->button;
390         struct input_dev *input = bdata->input;
391         unsigned int type = button->type ?: EV_KEY;
392         struct irq_desc *desc = irq_to_desc(gpio_to_irq(button->gpio));
393         int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
394
395         bdata->key_state = !!state;
396         input_event(input, type, button->code,
397                 (desc->status & IRQ_WAKEUP) ? 1 : !!state);
398         input_sync(input);
399 }
400
401 static void gpio_keys_work_func(struct work_struct *work)
402 {
403         struct gpio_button_data *bdata =
404                 container_of(work, struct gpio_button_data, work);
405
406         gpio_keys_report_event(bdata);
407 }
408
409 static void gpio_keys_timer(unsigned long _data)
410 {
411         struct gpio_button_data *data = (struct gpio_button_data *)_data;
412
413         schedule_work(&data->work);
414 }
415
416  /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump ++ */
417 #ifdef CONFIG_INPUT_FORCE_PANIC_KEY
418 int     get_panic_reset_state(void);
419 void sec_debug_check_crash_key(unsigned int code, int value)
420 {
421         static enum { NONE, HOME_DOWN } state = NONE;
422         static unsigned long home_down_jiffies;
423
424 #ifdef  CONFIG_SAMSUNG_WATCHDOG_RESET_BOOT
425  /* YUBAEK 20110906 RAMDUMP_DEBUG : forceable panic on/off  ++ */
426         if (get_panic_reset_state() == 0)
427         {
428                 return ;
429         }
430  /* YUBAEK 20110906 RAMDUMP_DEBUG : forceable panic on/off ++ */
431 #endif
432         pr_info("%s: %d %d\n", __func__, code, value);
433
434         if (code == KEY_MENU) {
435                 if (value) {
436                         state = HOME_DOWN;
437                         home_down_jiffies = jiffies;
438                 } else
439                         state = NONE;
440         } else if (code == KEY_VOLUMEUP) {
441                 if (value) {
442                         if (state == HOME_DOWN) {
443                                 pr_err("%s: %u msec after home down\n",
444                                        __func__,
445                                        jiffies_to_msecs(jiffies -
446                                                         home_down_jiffies));
447                                         panic(FORCIBLE_CRASH_KEY_STING);
448                         }
449                         /* else do nothing */
450                 } else
451                         state = NONE;
452         } else
453                 state = NONE;
454 }
455 #endif /* CONFIG_INPUT_FORCE_PANIC_KEY */
456  /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump -- */
457
458
459 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
460 {
461         struct gpio_button_data *bdata = dev_id;
462         struct gpio_keys_button *button = bdata->button;
463
464         BUG_ON(irq != gpio_to_irq(button->gpio));
465
466         if (bdata->timer_debounce)
467                 mod_timer(&bdata->timer,
468                         jiffies + msecs_to_jiffies(bdata->timer_debounce));
469         else
470                 schedule_work(&bdata->work);
471
472 /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump ++ */
473 #ifdef CONFIG_INPUT_FORCE_PANIC_KEY
474         if (button->isr_hook) {
475                 int state =
476                     (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
477                 button->isr_hook(button->code, state);
478         }
479 #endif /* CONFIG_INPUT_FORCE_PANIC_KEY */
480 /* YUBAEK 20110905 RAMDUMP_DEBUG : Android forceable ramdump ++ */
481
482
483         return IRQ_HANDLED;
484 }
485
486 static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
487                                          struct gpio_button_data *bdata,
488                                          struct gpio_keys_button *button)
489 {
490         char *desc = button->desc ? button->desc : "gpio_keys";
491         struct device *dev = &pdev->dev;
492         unsigned long irqflags;
493         int irq, error;
494
495         setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
496         INIT_WORK(&bdata->work, gpio_keys_work_func);
497
498         error = gpio_request(button->gpio, desc);
499         if (error < 0) {
500                 dev_err(dev, "failed to request GPIO %d, error %d\n",
501                         button->gpio, error);
502                 goto fail2;
503         }
504
505         error = gpio_direction_input(button->gpio);
506         if (error < 0) {
507                 dev_err(dev, "failed to configure"
508                         " direction for GPIO %d, error %d\n",
509                         button->gpio, error);
510                 goto fail3;
511         }
512
513         if (button->debounce_interval) {
514                 error = gpio_set_debounce(button->gpio,
515                                           button->debounce_interval * 1000);
516                 /* use timer if gpiolib doesn't provide debounce */
517                 if (error < 0)
518                         bdata->timer_debounce = button->debounce_interval;
519         }
520
521         irq = gpio_to_irq(button->gpio);
522         if (irq < 0) {
523                 error = irq;
524                 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
525                         button->gpio, error);
526                 goto fail3;
527         }
528
529         irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
530
531         /*
532          * If platform has specified that the button wakes up the system,
533          * we want the interrupt handler to be invoked in the
534          * suspend context as well.
535          */
536         if (button->wakeup)
537                 irqflags |= IRQF_NO_SUSPEND;
538
539         /*
540          * If platform has specified that the button can be disabled,
541          * we don't want it to share the interrupt line.
542          */
543         if (!button->can_disable)
544                 irqflags |= IRQF_SHARED;
545
546         error = request_irq(irq, gpio_keys_isr, irqflags, desc, bdata);
547         if (error) {
548                 dev_err(dev, "Unable to claim irq %d; error %d\n",
549                         irq, error);
550                 goto fail3;
551         }
552
553         return 0;
554
555 fail3:
556         gpio_free(button->gpio);
557 fail2:
558         return error;
559 }
560
561 static int gpio_keys_open(struct input_dev *input)
562 {
563         struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
564
565         return ddata->enable ? ddata->enable(input->dev.parent) : 0;
566 }
567
568 static void gpio_keys_close(struct input_dev *input)
569 {
570         struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
571
572         if (ddata->disable)
573                 ddata->disable(input->dev.parent);
574 }
575
576 static int __devinit gpio_keys_probe(struct platform_device *pdev)
577 {
578         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
579         struct gpio_keys_drvdata *ddata;
580         struct device *dev = &pdev->dev;
581         struct input_dev *input;
582         int i, error;
583         int wakeup = 0;
584
585         ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
586                         pdata->nbuttons * sizeof(struct gpio_button_data),
587                         GFP_KERNEL);
588         input = input_allocate_device();
589         if (!ddata || !input) {
590                 dev_err(dev, "failed to allocate state\n");
591                 error = -ENOMEM;
592                 goto fail1;
593         }
594
595         ddata->input = input;
596         ddata->n_buttons = pdata->nbuttons;
597         ddata->enable = pdata->enable;
598         ddata->disable = pdata->disable;
599         mutex_init(&ddata->disable_lock);
600
601         platform_set_drvdata(pdev, ddata);
602         input_set_drvdata(input, ddata);
603
604         input->name = pdev->name;
605         input->phys = "gpio-keys/input0";
606         input->dev.parent = &pdev->dev;
607         input->open = gpio_keys_open;
608         input->close = gpio_keys_close;
609
610         input->id.bustype = BUS_HOST;
611         input->id.vendor = 0x0001;
612         input->id.product = 0x0001;
613         input->id.version = 0x0100;
614
615         /* Enable auto repeat feature of Linux input subsystem */
616         if (pdata->rep)
617                 __set_bit(EV_REP, input->evbit);
618
619         for (i = 0; i < pdata->nbuttons; i++) {
620                 struct gpio_keys_button *button = &pdata->buttons[i];
621                 struct gpio_button_data *bdata = &ddata->data[i];
622                 unsigned int type = button->type ?: EV_KEY;
623
624                 bdata->input = input;
625                 bdata->button = button;
626
627                 error = gpio_keys_setup_key(pdev, bdata, button);
628                 if (error)
629                         goto fail2;
630
631                 if (button->wakeup)
632                         wakeup = 1;
633
634                 input_set_capability(input, type, button->code);
635         }
636
637         /* For earkey working */
638         input_set_capability(input, EV_KEY, KEY_SEND);
639
640         error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
641         if (error) {
642                 dev_err(dev, "Unable to export keys/switches, error: %d\n",
643                         error);
644                 goto fail2;
645         }
646
647         error = input_register_device(input);
648         if (error) {
649                 dev_err(dev, "Unable to register input device, error: %d\n",
650                         error);
651                 goto fail3;
652         }
653
654         /* Just for misc key such as earkey */
655         input_temp = input;
656
657         /* get current state of buttons */
658         for (i = 0; i < pdata->nbuttons; i++)
659                 gpio_keys_report_event(&ddata->data[i]);
660         input_sync(input);
661
662         device_init_wakeup(&pdev->dev, wakeup);
663
664         return 0;
665
666 fail3:
667         sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
668 fail2:
669         while (--i >= 0) {
670                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
671                 if (ddata->data[i].timer_debounce)
672                         del_timer_sync(&ddata->data[i].timer);
673                 cancel_work_sync(&ddata->data[i].work);
674                 gpio_free(pdata->buttons[i].gpio);
675         }
676
677         platform_set_drvdata(pdev, NULL);
678 fail1:
679         input_free_device(input);
680         kfree(ddata);
681
682         return error;
683 }
684
685 static int __devexit gpio_keys_remove(struct platform_device *pdev)
686 {
687         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
688         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
689         struct input_dev *input = ddata->input;
690         int i;
691
692         sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
693
694         device_init_wakeup(&pdev->dev, 0);
695
696         for (i = 0; i < pdata->nbuttons; i++) {
697                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
698                 free_irq(irq, &ddata->data[i]);
699                 if (ddata->data[i].timer_debounce)
700                         del_timer_sync(&ddata->data[i].timer);
701                 cancel_work_sync(&ddata->data[i].work);
702                 gpio_free(pdata->buttons[i].gpio);
703         }
704
705         input_unregister_device(input);
706
707         return 0;
708 }
709
710
711 #ifdef CONFIG_PM
712 static int gpio_keys_suspend(struct device *dev)
713 {
714         struct platform_device *pdev = to_platform_device(dev);
715         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
716         int i;
717
718         if (device_may_wakeup(&pdev->dev)) {
719                 for (i = 0; i < pdata->nbuttons; i++) {
720                         struct gpio_keys_button *button = &pdata->buttons[i];
721                         if (button->wakeup) {
722                                 int irq = gpio_to_irq(button->gpio);
723                                 enable_irq_wake(irq);
724                         }
725                 }
726         }
727
728         return 0;
729 }
730
731 static int gpio_keys_resume(struct device *dev)
732 {
733         struct platform_device *pdev = to_platform_device(dev);
734         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
735         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
736         int i;
737
738         for (i = 0; i < pdata->nbuttons; i++) {
739
740                 struct gpio_keys_button *button = &pdata->buttons[i];
741                 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
742                         int irq = gpio_to_irq(button->gpio);
743                         disable_irq_wake(irq);
744                 }
745
746                 gpio_keys_report_event(&ddata->data[i]);
747         }
748         input_sync(ddata->input);
749
750         return 0;
751 }
752
753 static const struct dev_pm_ops gpio_keys_pm_ops = {
754         .suspend        = gpio_keys_suspend,
755         .resume         = gpio_keys_resume,
756 };
757 #endif
758
759 static struct platform_driver gpio_keys_device_driver = {
760         .probe          = gpio_keys_probe,
761         .remove         = __devexit_p(gpio_keys_remove),
762         .driver         = {
763                 .name   = "gpio-keys",
764                 .owner  = THIS_MODULE,
765 #ifdef CONFIG_PM
766                 .pm     = &gpio_keys_pm_ops,
767 #endif
768         }
769 };
770
771 static int __init gpio_keys_init(void)
772 {
773         return platform_driver_register(&gpio_keys_device_driver);
774 }
775
776 static void __exit gpio_keys_exit(void)
777 {
778         platform_driver_unregister(&gpio_keys_device_driver);
779 }
780
781 module_init(gpio_keys_init);
782 module_exit(gpio_keys_exit);
783
784 MODULE_LICENSE("GPL");
785 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
786 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
787 MODULE_ALIAS("platform:gpio-keys");