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