GNU Linux-libre 4.9.309-gnu1
[releases.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  * Copyright 2010, 2011 David Jander <david@protonic.nl>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/sched.h>
19 #include <linux/pm.h>
20 #include <linux/slab.h>
21 #include <linux/sysctl.h>
22 #include <linux/proc_fs.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/input.h>
26 #include <linux/gpio_keys.h>
27 #include <linux/workqueue.h>
28 #include <linux/gpio.h>
29 #include <linux/gpio/consumer.h>
30 #include <linux/of.h>
31 #include <linux/of_platform.h>
32 #include <linux/of_gpio.h>
33 #include <linux/of_irq.h>
34 #include <linux/spinlock.h>
35
36 struct gpio_button_data {
37         const struct gpio_keys_button *button;
38         struct input_dev *input;
39         struct gpio_desc *gpiod;
40
41         struct timer_list release_timer;
42         unsigned int release_delay;     /* in msecs, for IRQ-only buttons */
43
44         struct delayed_work work;
45         unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */
46
47         unsigned int irq;
48         spinlock_t lock;
49         bool disabled;
50         bool key_pressed;
51 };
52
53 struct gpio_keys_drvdata {
54         const struct gpio_keys_platform_data *pdata;
55         struct input_dev *input;
56         struct mutex disable_lock;
57         struct gpio_button_data data[0];
58 };
59
60 /*
61  * SYSFS interface for enabling/disabling keys and switches:
62  *
63  * There are 4 attributes under /sys/devices/platform/gpio-keys/
64  *      keys [ro]              - bitmap of keys (EV_KEY) which can be
65  *                               disabled
66  *      switches [ro]          - bitmap of switches (EV_SW) which can be
67  *                               disabled
68  *      disabled_keys [rw]     - bitmap of keys currently disabled
69  *      disabled_switches [rw] - bitmap of switches currently disabled
70  *
71  * Userland can change these values and hence disable event generation
72  * for each key (or switch). Disabling a key means its interrupt line
73  * is disabled.
74  *
75  * For example, if we have following switches set up as gpio-keys:
76  *      SW_DOCK = 5
77  *      SW_CAMERA_LENS_COVER = 9
78  *      SW_KEYPAD_SLIDE = 10
79  *      SW_FRONT_PROXIMITY = 11
80  * This is read from switches:
81  *      11-9,5
82  * Next we want to disable proximity (11) and dock (5), we write:
83  *      11,5
84  * to file disabled_switches. Now proximity and dock IRQs are disabled.
85  * This can be verified by reading the file disabled_switches:
86  *      11,5
87  * If we now want to enable proximity (11) switch we write:
88  *      5
89  * to disabled_switches.
90  *
91  * We can disable only those keys which don't allow sharing the irq.
92  */
93
94 /**
95  * get_n_events_by_type() - returns maximum number of events per @type
96  * @type: type of button (%EV_KEY, %EV_SW)
97  *
98  * Return value of this function can be used to allocate bitmap
99  * large enough to hold all bits for given type.
100  */
101 static int get_n_events_by_type(int type)
102 {
103         BUG_ON(type != EV_SW && type != EV_KEY);
104
105         return (type == EV_KEY) ? KEY_CNT : SW_CNT;
106 }
107
108 /**
109  * get_bm_events_by_type() - returns bitmap of supported events per @type
110  * @input: input device from which bitmap is retrieved
111  * @type: type of button (%EV_KEY, %EV_SW)
112  *
113  * Return value of this function can be used to allocate bitmap
114  * large enough to hold all bits for given type.
115  */
116 static const unsigned long *get_bm_events_by_type(struct input_dev *dev,
117                                                   int type)
118 {
119         BUG_ON(type != EV_SW && type != EV_KEY);
120
121         return (type == EV_KEY) ? dev->keybit : dev->swbit;
122 }
123
124 /**
125  * gpio_keys_disable_button() - disables given GPIO button
126  * @bdata: button data for button to be disabled
127  *
128  * Disables button pointed by @bdata. This is done by masking
129  * IRQ line. After this function is called, button won't generate
130  * input events anymore. Note that one can only disable buttons
131  * that don't share IRQs.
132  *
133  * Make sure that @bdata->disable_lock is locked when entering
134  * this function to avoid races when concurrent threads are
135  * disabling buttons at the same time.
136  */
137 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
138 {
139         if (!bdata->disabled) {
140                 /*
141                  * Disable IRQ and associated timer/work structure.
142                  */
143                 disable_irq(bdata->irq);
144
145                 if (bdata->gpiod)
146                         cancel_delayed_work_sync(&bdata->work);
147                 else
148                         del_timer_sync(&bdata->release_timer);
149
150                 bdata->disabled = true;
151         }
152 }
153
154 /**
155  * gpio_keys_enable_button() - enables given GPIO button
156  * @bdata: button data for button to be disabled
157  *
158  * Enables given button pointed by @bdata.
159  *
160  * Make sure that @bdata->disable_lock is locked when entering
161  * this function to avoid races with concurrent threads trying
162  * to enable the same button at the same time.
163  */
164 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
165 {
166         if (bdata->disabled) {
167                 enable_irq(bdata->irq);
168                 bdata->disabled = false;
169         }
170 }
171
172 /**
173  * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
174  * @ddata: pointer to drvdata
175  * @buf: buffer where stringified bitmap is written
176  * @type: button type (%EV_KEY, %EV_SW)
177  * @only_disabled: does caller want only those buttons that are
178  *                 currently disabled or all buttons that can be
179  *                 disabled
180  *
181  * This function writes buttons that can be disabled to @buf. If
182  * @only_disabled is true, then @buf contains only those buttons
183  * that are currently disabled. Returns 0 on success or negative
184  * errno on failure.
185  */
186 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
187                                           char *buf, unsigned int type,
188                                           bool only_disabled)
189 {
190         int n_events = get_n_events_by_type(type);
191         unsigned long *bits;
192         ssize_t ret;
193         int i;
194
195         bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
196         if (!bits)
197                 return -ENOMEM;
198
199         for (i = 0; i < ddata->pdata->nbuttons; i++) {
200                 struct gpio_button_data *bdata = &ddata->data[i];
201
202                 if (bdata->button->type != type)
203                         continue;
204
205                 if (only_disabled && !bdata->disabled)
206                         continue;
207
208                 __set_bit(bdata->button->code, bits);
209         }
210
211         ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
212         buf[ret++] = '\n';
213         buf[ret] = '\0';
214
215         kfree(bits);
216
217         return ret;
218 }
219
220 /**
221  * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
222  * @ddata: pointer to drvdata
223  * @buf: buffer from userspace that contains stringified bitmap
224  * @type: button type (%EV_KEY, %EV_SW)
225  *
226  * This function parses stringified bitmap from @buf and disables/enables
227  * GPIO buttons accordingly. Returns 0 on success and negative error
228  * on failure.
229  */
230 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
231                                            const char *buf, unsigned int type)
232 {
233         int n_events = get_n_events_by_type(type);
234         const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
235         unsigned long *bits;
236         ssize_t error;
237         int i;
238
239         bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
240         if (!bits)
241                 return -ENOMEM;
242
243         error = bitmap_parselist(buf, bits, n_events);
244         if (error)
245                 goto out;
246
247         /* First validate */
248         if (!bitmap_subset(bits, bitmap, n_events)) {
249                 error = -EINVAL;
250                 goto out;
251         }
252
253         for (i = 0; i < ddata->pdata->nbuttons; i++) {
254                 struct gpio_button_data *bdata = &ddata->data[i];
255
256                 if (bdata->button->type != type)
257                         continue;
258
259                 if (test_bit(bdata->button->code, bits) &&
260                     !bdata->button->can_disable) {
261                         error = -EINVAL;
262                         goto out;
263                 }
264         }
265
266         mutex_lock(&ddata->disable_lock);
267
268         for (i = 0; i < ddata->pdata->nbuttons; i++) {
269                 struct gpio_button_data *bdata = &ddata->data[i];
270
271                 if (bdata->button->type != type)
272                         continue;
273
274                 if (test_bit(bdata->button->code, bits))
275                         gpio_keys_disable_button(bdata);
276                 else
277                         gpio_keys_enable_button(bdata);
278         }
279
280         mutex_unlock(&ddata->disable_lock);
281
282 out:
283         kfree(bits);
284         return error;
285 }
286
287 #define ATTR_SHOW_FN(name, type, only_disabled)                         \
288 static ssize_t gpio_keys_show_##name(struct device *dev,                \
289                                      struct device_attribute *attr,     \
290                                      char *buf)                         \
291 {                                                                       \
292         struct platform_device *pdev = to_platform_device(dev);         \
293         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);   \
294                                                                         \
295         return gpio_keys_attr_show_helper(ddata, buf,                   \
296                                           type, only_disabled);         \
297 }
298
299 ATTR_SHOW_FN(keys, EV_KEY, false);
300 ATTR_SHOW_FN(switches, EV_SW, false);
301 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
302 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
303
304 /*
305  * ATTRIBUTES:
306  *
307  * /sys/devices/platform/gpio-keys/keys [ro]
308  * /sys/devices/platform/gpio-keys/switches [ro]
309  */
310 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
311 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
312
313 #define ATTR_STORE_FN(name, type)                                       \
314 static ssize_t gpio_keys_store_##name(struct device *dev,               \
315                                       struct device_attribute *attr,    \
316                                       const char *buf,                  \
317                                       size_t count)                     \
318 {                                                                       \
319         struct platform_device *pdev = to_platform_device(dev);         \
320         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);   \
321         ssize_t error;                                                  \
322                                                                         \
323         error = gpio_keys_attr_store_helper(ddata, buf, type);          \
324         if (error)                                                      \
325                 return error;                                           \
326                                                                         \
327         return count;                                                   \
328 }
329
330 ATTR_STORE_FN(disabled_keys, EV_KEY);
331 ATTR_STORE_FN(disabled_switches, EV_SW);
332
333 /*
334  * ATTRIBUTES:
335  *
336  * /sys/devices/platform/gpio-keys/disabled_keys [rw]
337  * /sys/devices/platform/gpio-keys/disables_switches [rw]
338  */
339 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
340                    gpio_keys_show_disabled_keys,
341                    gpio_keys_store_disabled_keys);
342 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
343                    gpio_keys_show_disabled_switches,
344                    gpio_keys_store_disabled_switches);
345
346 static struct attribute *gpio_keys_attrs[] = {
347         &dev_attr_keys.attr,
348         &dev_attr_switches.attr,
349         &dev_attr_disabled_keys.attr,
350         &dev_attr_disabled_switches.attr,
351         NULL,
352 };
353
354 static struct attribute_group gpio_keys_attr_group = {
355         .attrs = gpio_keys_attrs,
356 };
357
358 static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
359 {
360         const struct gpio_keys_button *button = bdata->button;
361         struct input_dev *input = bdata->input;
362         unsigned int type = button->type ?: EV_KEY;
363         int state;
364
365         state = gpiod_get_value_cansleep(bdata->gpiod);
366         if (state < 0) {
367                 dev_err(input->dev.parent,
368                         "failed to get gpio state: %d\n", state);
369                 return;
370         }
371
372         if (type == EV_ABS) {
373                 if (state)
374                         input_event(input, type, button->code, button->value);
375         } else {
376                 input_event(input, type, button->code, state);
377         }
378         input_sync(input);
379 }
380
381 static void gpio_keys_gpio_work_func(struct work_struct *work)
382 {
383         struct gpio_button_data *bdata =
384                 container_of(work, struct gpio_button_data, work.work);
385
386         gpio_keys_gpio_report_event(bdata);
387
388         if (bdata->button->wakeup)
389                 pm_relax(bdata->input->dev.parent);
390 }
391
392 static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
393 {
394         struct gpio_button_data *bdata = dev_id;
395
396         BUG_ON(irq != bdata->irq);
397
398         if (bdata->button->wakeup)
399                 pm_stay_awake(bdata->input->dev.parent);
400
401         mod_delayed_work(system_wq,
402                          &bdata->work,
403                          msecs_to_jiffies(bdata->software_debounce));
404
405         return IRQ_HANDLED;
406 }
407
408 static void gpio_keys_irq_timer(unsigned long _data)
409 {
410         struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
411         struct input_dev *input = bdata->input;
412         unsigned long flags;
413
414         spin_lock_irqsave(&bdata->lock, flags);
415         if (bdata->key_pressed) {
416                 input_event(input, EV_KEY, bdata->button->code, 0);
417                 input_sync(input);
418                 bdata->key_pressed = false;
419         }
420         spin_unlock_irqrestore(&bdata->lock, flags);
421 }
422
423 static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
424 {
425         struct gpio_button_data *bdata = dev_id;
426         const struct gpio_keys_button *button = bdata->button;
427         struct input_dev *input = bdata->input;
428         unsigned long flags;
429
430         BUG_ON(irq != bdata->irq);
431
432         spin_lock_irqsave(&bdata->lock, flags);
433
434         if (!bdata->key_pressed) {
435                 if (bdata->button->wakeup)
436                         pm_wakeup_event(bdata->input->dev.parent, 0);
437
438                 input_event(input, EV_KEY, button->code, 1);
439                 input_sync(input);
440
441                 if (!bdata->release_delay) {
442                         input_event(input, EV_KEY, button->code, 0);
443                         input_sync(input);
444                         goto out;
445                 }
446
447                 bdata->key_pressed = true;
448         }
449
450         if (bdata->release_delay)
451                 mod_timer(&bdata->release_timer,
452                         jiffies + msecs_to_jiffies(bdata->release_delay));
453 out:
454         spin_unlock_irqrestore(&bdata->lock, flags);
455         return IRQ_HANDLED;
456 }
457
458 static void gpio_keys_quiesce_key(void *data)
459 {
460         struct gpio_button_data *bdata = data;
461
462         if (bdata->gpiod)
463                 cancel_delayed_work_sync(&bdata->work);
464         else
465                 del_timer_sync(&bdata->release_timer);
466 }
467
468 static int gpio_keys_setup_key(struct platform_device *pdev,
469                                 struct input_dev *input,
470                                 struct gpio_button_data *bdata,
471                                 const struct gpio_keys_button *button)
472 {
473         const char *desc = button->desc ? button->desc : "gpio_keys";
474         struct device *dev = &pdev->dev;
475         irq_handler_t isr;
476         unsigned long irqflags;
477         int irq;
478         int error;
479
480         bdata->input = input;
481         bdata->button = button;
482         spin_lock_init(&bdata->lock);
483
484         /*
485          * Legacy GPIO number, so request the GPIO here and
486          * convert it to descriptor.
487          */
488         if (gpio_is_valid(button->gpio)) {
489                 unsigned flags = GPIOF_IN;
490
491                 if (button->active_low)
492                         flags |= GPIOF_ACTIVE_LOW;
493
494                 error = devm_gpio_request_one(&pdev->dev, button->gpio, flags,
495                                               desc);
496                 if (error < 0) {
497                         dev_err(dev, "Failed to request GPIO %d, error %d\n",
498                                 button->gpio, error);
499                         return error;
500                 }
501
502                 bdata->gpiod = gpio_to_desc(button->gpio);
503                 if (!bdata->gpiod)
504                         return -EINVAL;
505
506                 if (button->debounce_interval) {
507                         error = gpiod_set_debounce(bdata->gpiod,
508                                         button->debounce_interval * 1000);
509                         /* use timer if gpiolib doesn't provide debounce */
510                         if (error < 0)
511                                 bdata->software_debounce =
512                                                 button->debounce_interval;
513                 }
514
515                 if (button->irq) {
516                         bdata->irq = button->irq;
517                 } else {
518                         irq = gpiod_to_irq(bdata->gpiod);
519                         if (irq < 0) {
520                                 error = irq;
521                                 dev_err(dev,
522                                         "Unable to get irq number for GPIO %d, error %d\n",
523                                         button->gpio, error);
524                                 return error;
525                         }
526                         bdata->irq = irq;
527                 }
528
529                 INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
530
531                 isr = gpio_keys_gpio_isr;
532                 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
533
534         } else {
535                 if (!button->irq) {
536                         dev_err(dev, "No IRQ specified\n");
537                         return -EINVAL;
538                 }
539                 bdata->irq = button->irq;
540
541                 if (button->type && button->type != EV_KEY) {
542                         dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
543                         return -EINVAL;
544                 }
545
546                 bdata->release_delay = button->debounce_interval;
547                 setup_timer(&bdata->release_timer,
548                             gpio_keys_irq_timer, (unsigned long)bdata);
549
550                 isr = gpio_keys_irq_isr;
551                 irqflags = 0;
552         }
553
554         input_set_capability(input, button->type ?: EV_KEY, button->code);
555
556         /*
557          * Install custom action to cancel release timer and
558          * workqueue item.
559          */
560         error = devm_add_action(&pdev->dev, gpio_keys_quiesce_key, bdata);
561         if (error) {
562                 dev_err(&pdev->dev,
563                         "failed to register quiesce action, error: %d\n",
564                         error);
565                 return error;
566         }
567
568         /*
569          * If platform has specified that the button can be disabled,
570          * we don't want it to share the interrupt line.
571          */
572         if (!button->can_disable)
573                 irqflags |= IRQF_SHARED;
574
575         error = devm_request_any_context_irq(&pdev->dev, bdata->irq,
576                                              isr, irqflags, desc, bdata);
577         if (error < 0) {
578                 dev_err(dev, "Unable to claim irq %d; error %d\n",
579                         bdata->irq, error);
580                 return error;
581         }
582
583         return 0;
584 }
585
586 static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
587 {
588         struct input_dev *input = ddata->input;
589         int i;
590
591         for (i = 0; i < ddata->pdata->nbuttons; i++) {
592                 struct gpio_button_data *bdata = &ddata->data[i];
593                 if (bdata->gpiod)
594                         gpio_keys_gpio_report_event(bdata);
595         }
596         input_sync(input);
597 }
598
599 static int gpio_keys_open(struct input_dev *input)
600 {
601         struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
602         const struct gpio_keys_platform_data *pdata = ddata->pdata;
603         int error;
604
605         if (pdata->enable) {
606                 error = pdata->enable(input->dev.parent);
607                 if (error)
608                         return error;
609         }
610
611         /* Report current state of buttons that are connected to GPIOs */
612         gpio_keys_report_state(ddata);
613
614         return 0;
615 }
616
617 static void gpio_keys_close(struct input_dev *input)
618 {
619         struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
620         const struct gpio_keys_platform_data *pdata = ddata->pdata;
621
622         if (pdata->disable)
623                 pdata->disable(input->dev.parent);
624 }
625
626 /*
627  * Handlers for alternative sources of platform_data
628  */
629
630 #ifdef CONFIG_OF
631 /*
632  * Translate OpenFirmware node properties into platform_data
633  */
634 static struct gpio_keys_platform_data *
635 gpio_keys_get_devtree_pdata(struct device *dev)
636 {
637         struct device_node *node, *pp;
638         struct gpio_keys_platform_data *pdata;
639         struct gpio_keys_button *button;
640         int error;
641         int nbuttons;
642         int i;
643
644         node = dev->of_node;
645         if (!node)
646                 return ERR_PTR(-ENODEV);
647
648         nbuttons = of_get_available_child_count(node);
649         if (nbuttons == 0)
650                 return ERR_PTR(-ENODEV);
651
652         pdata = devm_kzalloc(dev,
653                              sizeof(*pdata) + nbuttons * sizeof(*button),
654                              GFP_KERNEL);
655         if (!pdata)
656                 return ERR_PTR(-ENOMEM);
657
658         pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
659         pdata->nbuttons = nbuttons;
660
661         pdata->rep = !!of_get_property(node, "autorepeat", NULL);
662
663         of_property_read_string(node, "label", &pdata->name);
664
665         i = 0;
666         for_each_available_child_of_node(node, pp) {
667                 enum of_gpio_flags flags;
668
669                 button = &pdata->buttons[i++];
670
671                 button->gpio = of_get_gpio_flags(pp, 0, &flags);
672                 if (button->gpio < 0) {
673                         error = button->gpio;
674                         if (error != -ENOENT) {
675                                 if (error != -EPROBE_DEFER)
676                                         dev_err(dev,
677                                                 "Failed to get gpio flags, error: %d\n",
678                                                 error);
679                                 return ERR_PTR(error);
680                         }
681                 } else {
682                         button->active_low = flags & OF_GPIO_ACTIVE_LOW;
683                 }
684
685                 button->irq = irq_of_parse_and_map(pp, 0);
686
687                 if (!gpio_is_valid(button->gpio) && !button->irq) {
688                         dev_err(dev, "Found button without gpios or irqs\n");
689                         return ERR_PTR(-EINVAL);
690                 }
691
692                 if (of_property_read_u32(pp, "linux,code", &button->code)) {
693                         dev_err(dev, "Button without keycode: 0x%x\n",
694                                 button->gpio);
695                         return ERR_PTR(-EINVAL);
696                 }
697
698                 button->desc = of_get_property(pp, "label", NULL);
699
700                 if (of_property_read_u32(pp, "linux,input-type", &button->type))
701                         button->type = EV_KEY;
702
703                 button->wakeup = of_property_read_bool(pp, "wakeup-source") ||
704                                  /* legacy name */
705                                  of_property_read_bool(pp, "gpio-key,wakeup");
706
707                 button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL);
708
709                 if (of_property_read_u32(pp, "debounce-interval",
710                                          &button->debounce_interval))
711                         button->debounce_interval = 5;
712         }
713
714         if (pdata->nbuttons == 0)
715                 return ERR_PTR(-EINVAL);
716
717         return pdata;
718 }
719
720 static const struct of_device_id gpio_keys_of_match[] = {
721         { .compatible = "gpio-keys", },
722         { },
723 };
724 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
725
726 #else
727
728 static inline struct gpio_keys_platform_data *
729 gpio_keys_get_devtree_pdata(struct device *dev)
730 {
731         return ERR_PTR(-ENODEV);
732 }
733
734 #endif
735
736 static int gpio_keys_probe(struct platform_device *pdev)
737 {
738         struct device *dev = &pdev->dev;
739         const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
740         struct gpio_keys_drvdata *ddata;
741         struct input_dev *input;
742         size_t size;
743         int i, error;
744         int wakeup = 0;
745
746         if (!pdata) {
747                 pdata = gpio_keys_get_devtree_pdata(dev);
748                 if (IS_ERR(pdata))
749                         return PTR_ERR(pdata);
750         }
751
752         size = sizeof(struct gpio_keys_drvdata) +
753                         pdata->nbuttons * sizeof(struct gpio_button_data);
754         ddata = devm_kzalloc(dev, size, GFP_KERNEL);
755         if (!ddata) {
756                 dev_err(dev, "failed to allocate state\n");
757                 return -ENOMEM;
758         }
759
760         input = devm_input_allocate_device(dev);
761         if (!input) {
762                 dev_err(dev, "failed to allocate input device\n");
763                 return -ENOMEM;
764         }
765
766         ddata->pdata = pdata;
767         ddata->input = input;
768         mutex_init(&ddata->disable_lock);
769
770         platform_set_drvdata(pdev, ddata);
771         input_set_drvdata(input, ddata);
772
773         input->name = pdata->name ? : pdev->name;
774         input->phys = "gpio-keys/input0";
775         input->dev.parent = &pdev->dev;
776         input->open = gpio_keys_open;
777         input->close = gpio_keys_close;
778
779         input->id.bustype = BUS_HOST;
780         input->id.vendor = 0x0001;
781         input->id.product = 0x0001;
782         input->id.version = 0x0100;
783
784         /* Enable auto repeat feature of Linux input subsystem */
785         if (pdata->rep)
786                 __set_bit(EV_REP, input->evbit);
787
788         for (i = 0; i < pdata->nbuttons; i++) {
789                 const struct gpio_keys_button *button = &pdata->buttons[i];
790                 struct gpio_button_data *bdata = &ddata->data[i];
791
792                 error = gpio_keys_setup_key(pdev, input, bdata, button);
793                 if (error)
794                         return error;
795
796                 if (button->wakeup)
797                         wakeup = 1;
798         }
799
800         error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
801         if (error) {
802                 dev_err(dev, "Unable to export keys/switches, error: %d\n",
803                         error);
804                 return error;
805         }
806
807         error = input_register_device(input);
808         if (error) {
809                 dev_err(dev, "Unable to register input device, error: %d\n",
810                         error);
811                 goto err_remove_group;
812         }
813
814         device_init_wakeup(&pdev->dev, wakeup);
815
816         return 0;
817
818 err_remove_group:
819         sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
820         return error;
821 }
822
823 static int gpio_keys_remove(struct platform_device *pdev)
824 {
825         sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
826
827         device_init_wakeup(&pdev->dev, 0);
828
829         return 0;
830 }
831
832 #ifdef CONFIG_PM_SLEEP
833 static int gpio_keys_suspend(struct device *dev)
834 {
835         struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
836         struct input_dev *input = ddata->input;
837         int i;
838
839         if (device_may_wakeup(dev)) {
840                 for (i = 0; i < ddata->pdata->nbuttons; i++) {
841                         struct gpio_button_data *bdata = &ddata->data[i];
842                         if (bdata->button->wakeup)
843                                 enable_irq_wake(bdata->irq);
844                 }
845         } else {
846                 mutex_lock(&input->mutex);
847                 if (input->users)
848                         gpio_keys_close(input);
849                 mutex_unlock(&input->mutex);
850         }
851
852         return 0;
853 }
854
855 static int gpio_keys_resume(struct device *dev)
856 {
857         struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
858         struct input_dev *input = ddata->input;
859         int error = 0;
860         int i;
861
862         if (device_may_wakeup(dev)) {
863                 for (i = 0; i < ddata->pdata->nbuttons; i++) {
864                         struct gpio_button_data *bdata = &ddata->data[i];
865                         if (bdata->button->wakeup)
866                                 disable_irq_wake(bdata->irq);
867                 }
868         } else {
869                 mutex_lock(&input->mutex);
870                 if (input->users)
871                         error = gpio_keys_open(input);
872                 mutex_unlock(&input->mutex);
873         }
874
875         if (error)
876                 return error;
877
878         gpio_keys_report_state(ddata);
879         return 0;
880 }
881 #endif
882
883 static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
884
885 static struct platform_driver gpio_keys_device_driver = {
886         .probe          = gpio_keys_probe,
887         .remove         = gpio_keys_remove,
888         .driver         = {
889                 .name   = "gpio-keys",
890                 .pm     = &gpio_keys_pm_ops,
891                 .of_match_table = of_match_ptr(gpio_keys_of_match),
892         }
893 };
894
895 static int __init gpio_keys_init(void)
896 {
897         return platform_driver_register(&gpio_keys_device_driver);
898 }
899
900 static void __exit gpio_keys_exit(void)
901 {
902         platform_driver_unregister(&gpio_keys_device_driver);
903 }
904
905 late_initcall(gpio_keys_init);
906 module_exit(gpio_keys_exit);
907
908 MODULE_LICENSE("GPL");
909 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
910 MODULE_DESCRIPTION("Keyboard driver for GPIOs");
911 MODULE_ALIAS("platform:gpio-keys");