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