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(struct timer_list *t)
423 {
424 	struct gpio_button_data *bdata = from_timer(bdata, t, release_timer);
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 		timer_setup(&bdata->release_timer, gpio_keys_irq_timer, 0);
586 
587 		isr = gpio_keys_irq_isr;
588 		irqflags = 0;
589 	}
590 
591 	bdata->code = &ddata->keymap[idx];
592 	*bdata->code = button->code;
593 	input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
594 
595 	/*
596 	 * Install custom action to cancel release timer and
597 	 * workqueue item.
598 	 */
599 	error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
600 	if (error) {
601 		dev_err(dev, "failed to register quiesce action, error: %d\n",
602 			error);
603 		return error;
604 	}
605 
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 	error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
614 					     desc, bdata);
615 	if (error < 0) {
616 		dev_err(dev, "Unable to claim irq %d; error %d\n",
617 			bdata->irq, error);
618 		return error;
619 	}
620 
621 	return 0;
622 }
623 
624 static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
625 {
626 	struct input_dev *input = ddata->input;
627 	int i;
628 
629 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
630 		struct gpio_button_data *bdata = &ddata->data[i];
631 		if (bdata->gpiod)
632 			gpio_keys_gpio_report_event(bdata);
633 	}
634 	input_sync(input);
635 }
636 
637 static int gpio_keys_open(struct input_dev *input)
638 {
639 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
640 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
641 	int error;
642 
643 	if (pdata->enable) {
644 		error = pdata->enable(input->dev.parent);
645 		if (error)
646 			return error;
647 	}
648 
649 	/* Report current state of buttons that are connected to GPIOs */
650 	gpio_keys_report_state(ddata);
651 
652 	return 0;
653 }
654 
655 static void gpio_keys_close(struct input_dev *input)
656 {
657 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
658 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
659 
660 	if (pdata->disable)
661 		pdata->disable(input->dev.parent);
662 }
663 
664 /*
665  * Handlers for alternative sources of platform_data
666  */
667 
668 /*
669  * Translate properties into platform_data
670  */
671 static struct gpio_keys_platform_data *
672 gpio_keys_get_devtree_pdata(struct device *dev)
673 {
674 	struct gpio_keys_platform_data *pdata;
675 	struct gpio_keys_button *button;
676 	struct fwnode_handle *child;
677 	int nbuttons;
678 
679 	nbuttons = device_get_child_node_count(dev);
680 	if (nbuttons == 0)
681 		return ERR_PTR(-ENODEV);
682 
683 	pdata = devm_kzalloc(dev,
684 			     sizeof(*pdata) + nbuttons * sizeof(*button),
685 			     GFP_KERNEL);
686 	if (!pdata)
687 		return ERR_PTR(-ENOMEM);
688 
689 	button = (struct gpio_keys_button *)(pdata + 1);
690 
691 	pdata->buttons = button;
692 	pdata->nbuttons = nbuttons;
693 
694 	pdata->rep = device_property_read_bool(dev, "autorepeat");
695 
696 	device_property_read_string(dev, "label", &pdata->name);
697 
698 	device_for_each_child_node(dev, child) {
699 		if (is_of_node(child))
700 			button->irq =
701 				irq_of_parse_and_map(to_of_node(child), 0);
702 
703 		if (fwnode_property_read_u32(child, "linux,code",
704 					     &button->code)) {
705 			dev_err(dev, "Button without keycode\n");
706 			fwnode_handle_put(child);
707 			return ERR_PTR(-EINVAL);
708 		}
709 
710 		fwnode_property_read_string(child, "label", &button->desc);
711 
712 		if (fwnode_property_read_u32(child, "linux,input-type",
713 					     &button->type))
714 			button->type = EV_KEY;
715 
716 		button->wakeup =
717 			fwnode_property_read_bool(child, "wakeup-source") ||
718 			/* legacy name */
719 			fwnode_property_read_bool(child, "gpio-key,wakeup");
720 
721 		button->can_disable =
722 			fwnode_property_read_bool(child, "linux,can-disable");
723 
724 		if (fwnode_property_read_u32(child, "debounce-interval",
725 					 &button->debounce_interval))
726 			button->debounce_interval = 5;
727 
728 		button++;
729 	}
730 
731 	return pdata;
732 }
733 
734 static const struct of_device_id gpio_keys_of_match[] = {
735 	{ .compatible = "gpio-keys", },
736 	{ },
737 };
738 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
739 
740 static int gpio_keys_probe(struct platform_device *pdev)
741 {
742 	struct device *dev = &pdev->dev;
743 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
744 	struct fwnode_handle *child = NULL;
745 	struct gpio_keys_drvdata *ddata;
746 	struct input_dev *input;
747 	size_t size;
748 	int i, error;
749 	int wakeup = 0;
750 
751 	if (!pdata) {
752 		pdata = gpio_keys_get_devtree_pdata(dev);
753 		if (IS_ERR(pdata))
754 			return PTR_ERR(pdata);
755 	}
756 
757 	size = sizeof(struct gpio_keys_drvdata) +
758 			pdata->nbuttons * sizeof(struct gpio_button_data);
759 	ddata = devm_kzalloc(dev, size, GFP_KERNEL);
760 	if (!ddata) {
761 		dev_err(dev, "failed to allocate state\n");
762 		return -ENOMEM;
763 	}
764 
765 	ddata->keymap = devm_kcalloc(dev,
766 				     pdata->nbuttons, sizeof(ddata->keymap[0]),
767 				     GFP_KERNEL);
768 	if (!ddata->keymap)
769 		return -ENOMEM;
770 
771 	input = devm_input_allocate_device(dev);
772 	if (!input) {
773 		dev_err(dev, "failed to allocate input device\n");
774 		return -ENOMEM;
775 	}
776 
777 	ddata->pdata = pdata;
778 	ddata->input = input;
779 	mutex_init(&ddata->disable_lock);
780 
781 	platform_set_drvdata(pdev, ddata);
782 	input_set_drvdata(input, ddata);
783 
784 	input->name = pdata->name ? : pdev->name;
785 	input->phys = "gpio-keys/input0";
786 	input->dev.parent = dev;
787 	input->open = gpio_keys_open;
788 	input->close = gpio_keys_close;
789 
790 	input->id.bustype = BUS_HOST;
791 	input->id.vendor = 0x0001;
792 	input->id.product = 0x0001;
793 	input->id.version = 0x0100;
794 
795 	input->keycode = ddata->keymap;
796 	input->keycodesize = sizeof(ddata->keymap[0]);
797 	input->keycodemax = pdata->nbuttons;
798 
799 	/* Enable auto repeat feature of Linux input subsystem */
800 	if (pdata->rep)
801 		__set_bit(EV_REP, input->evbit);
802 
803 	for (i = 0; i < pdata->nbuttons; i++) {
804 		const struct gpio_keys_button *button = &pdata->buttons[i];
805 
806 		if (!dev_get_platdata(dev)) {
807 			child = device_get_next_child_node(dev, child);
808 			if (!child) {
809 				dev_err(dev,
810 					"missing child device node for entry %d\n",
811 					i);
812 				return -EINVAL;
813 			}
814 		}
815 
816 		error = gpio_keys_setup_key(pdev, input, ddata,
817 					    button, i, child);
818 		if (error) {
819 			fwnode_handle_put(child);
820 			return error;
821 		}
822 
823 		if (button->wakeup)
824 			wakeup = 1;
825 	}
826 
827 	fwnode_handle_put(child);
828 
829 	error = devm_device_add_group(dev, &gpio_keys_attr_group);
830 	if (error) {
831 		dev_err(dev, "Unable to export keys/switches, error: %d\n",
832 			error);
833 		return error;
834 	}
835 
836 	error = input_register_device(input);
837 	if (error) {
838 		dev_err(dev, "Unable to register input device, error: %d\n",
839 			error);
840 		return error;
841 	}
842 
843 	device_init_wakeup(dev, wakeup);
844 
845 	return 0;
846 }
847 
848 static int __maybe_unused gpio_keys_suspend(struct device *dev)
849 {
850 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
851 	struct input_dev *input = ddata->input;
852 	int i;
853 
854 	if (device_may_wakeup(dev)) {
855 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
856 			struct gpio_button_data *bdata = &ddata->data[i];
857 			if (bdata->button->wakeup)
858 				enable_irq_wake(bdata->irq);
859 			bdata->suspended = true;
860 		}
861 	} else {
862 		mutex_lock(&input->mutex);
863 		if (input->users)
864 			gpio_keys_close(input);
865 		mutex_unlock(&input->mutex);
866 	}
867 
868 	return 0;
869 }
870 
871 static int __maybe_unused gpio_keys_resume(struct device *dev)
872 {
873 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
874 	struct input_dev *input = ddata->input;
875 	int error = 0;
876 	int i;
877 
878 	if (device_may_wakeup(dev)) {
879 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
880 			struct gpio_button_data *bdata = &ddata->data[i];
881 			if (bdata->button->wakeup)
882 				disable_irq_wake(bdata->irq);
883 			bdata->suspended = false;
884 		}
885 	} else {
886 		mutex_lock(&input->mutex);
887 		if (input->users)
888 			error = gpio_keys_open(input);
889 		mutex_unlock(&input->mutex);
890 	}
891 
892 	if (error)
893 		return error;
894 
895 	gpio_keys_report_state(ddata);
896 	return 0;
897 }
898 
899 static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
900 
901 static struct platform_driver gpio_keys_device_driver = {
902 	.probe		= gpio_keys_probe,
903 	.driver		= {
904 		.name	= "gpio-keys",
905 		.pm	= &gpio_keys_pm_ops,
906 		.of_match_table = gpio_keys_of_match,
907 	}
908 };
909 
910 static int __init gpio_keys_init(void)
911 {
912 	return platform_driver_register(&gpio_keys_device_driver);
913 }
914 
915 static void __exit gpio_keys_exit(void)
916 {
917 	platform_driver_unregister(&gpio_keys_device_driver);
918 }
919 
920 late_initcall(gpio_keys_init);
921 module_exit(gpio_keys_exit);
922 
923 MODULE_LICENSE("GPL");
924 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
925 MODULE_DESCRIPTION("Keyboard driver for GPIOs");
926 MODULE_ALIAS("platform:gpio-keys");
927