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