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