xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision 0f107573da417c7f5c6d3a0160ebacc3adb019c4)
178a56aabSPhil Blundell /*
278a56aabSPhil Blundell  * Driver for keys on GPIO lines capable of generating interrupts.
378a56aabSPhil Blundell  *
478a56aabSPhil Blundell  * Copyright 2005 Phil Blundell
5fd05d089SDavid Jander  * Copyright 2010, 2011 David Jander <david@protonic.nl>
678a56aabSPhil Blundell  *
778a56aabSPhil Blundell  * This program is free software; you can redistribute it and/or modify
878a56aabSPhil Blundell  * it under the terms of the GNU General Public License version 2 as
978a56aabSPhil Blundell  * published by the Free Software Foundation.
1078a56aabSPhil Blundell  */
1178a56aabSPhil Blundell 
1278a56aabSPhil Blundell #include <linux/module.h>
1378a56aabSPhil Blundell 
1478a56aabSPhil Blundell #include <linux/init.h>
1578a56aabSPhil Blundell #include <linux/fs.h>
1678a56aabSPhil Blundell #include <linux/interrupt.h>
1778a56aabSPhil Blundell #include <linux/irq.h>
1878a56aabSPhil Blundell #include <linux/sched.h>
1978a56aabSPhil Blundell #include <linux/pm.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2178a56aabSPhil Blundell #include <linux/sysctl.h>
2278a56aabSPhil Blundell #include <linux/proc_fs.h>
2378a56aabSPhil Blundell #include <linux/delay.h>
2478a56aabSPhil Blundell #include <linux/platform_device.h>
2578a56aabSPhil Blundell #include <linux/input.h>
2649015beeSDavid Brownell #include <linux/gpio_keys.h>
27da0d03feSJani Nikula #include <linux/workqueue.h>
28111bc59cSBen Dooks #include <linux/gpio.h>
295feeca3cSGeert Uytterhoeven #include <linux/gpio/consumer.h>
30415a4caaSSachin Kamat #include <linux/of.h>
31f2d347ffSAlexander Stein #include <linux/of_irq.h>
32d8ee4a1cSLaxman Dewangan #include <linux/spinlock.h>
3378a56aabSPhil Blundell 
34a33466e3SDmitry Baryshkov struct gpio_button_data {
35d9080921SDmitry Torokhov 	const struct gpio_keys_button *button;
36a33466e3SDmitry Baryshkov 	struct input_dev *input;
375feeca3cSGeert Uytterhoeven 	struct gpio_desc *gpiod;
388ed92556SDmitry Torokhov 
3983e4947aSHans de Goede 	unsigned short *code;
4083e4947aSHans de Goede 
418ed92556SDmitry Torokhov 	struct timer_list release_timer;
428ed92556SDmitry Torokhov 	unsigned int release_delay;	/* in msecs, for IRQ-only buttons */
438ed92556SDmitry Torokhov 
448ed92556SDmitry Torokhov 	struct delayed_work work;
458ed92556SDmitry Torokhov 	unsigned int software_debounce;	/* in msecs, for GPIO-driven buttons */
468ed92556SDmitry Torokhov 
47d8ee4a1cSLaxman Dewangan 	unsigned int irq;
48d8ee4a1cSLaxman Dewangan 	spinlock_t lock;
499e3af04fSMika Westerberg 	bool disabled;
50d8ee4a1cSLaxman Dewangan 	bool key_pressed;
51*0f107573SJoseph Lo 	bool suspended;
52a33466e3SDmitry Baryshkov };
53a33466e3SDmitry Baryshkov 
54a33466e3SDmitry Baryshkov struct gpio_keys_drvdata {
55219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata;
56a33466e3SDmitry Baryshkov 	struct input_dev *input;
579e3af04fSMika Westerberg 	struct mutex disable_lock;
5883e4947aSHans de Goede 	unsigned short *keymap;
59a33466e3SDmitry Baryshkov 	struct gpio_button_data data[0];
60a33466e3SDmitry Baryshkov };
61a33466e3SDmitry Baryshkov 
629e3af04fSMika Westerberg /*
639e3af04fSMika Westerberg  * SYSFS interface for enabling/disabling keys and switches:
649e3af04fSMika Westerberg  *
659e3af04fSMika Westerberg  * There are 4 attributes under /sys/devices/platform/gpio-keys/
669e3af04fSMika Westerberg  *	keys [ro]              - bitmap of keys (EV_KEY) which can be
679e3af04fSMika Westerberg  *	                         disabled
689e3af04fSMika Westerberg  *	switches [ro]          - bitmap of switches (EV_SW) which can be
699e3af04fSMika Westerberg  *	                         disabled
709e3af04fSMika Westerberg  *	disabled_keys [rw]     - bitmap of keys currently disabled
719e3af04fSMika Westerberg  *	disabled_switches [rw] - bitmap of switches currently disabled
729e3af04fSMika Westerberg  *
739e3af04fSMika Westerberg  * Userland can change these values and hence disable event generation
749e3af04fSMika Westerberg  * for each key (or switch). Disabling a key means its interrupt line
759e3af04fSMika Westerberg  * is disabled.
769e3af04fSMika Westerberg  *
779e3af04fSMika Westerberg  * For example, if we have following switches set up as gpio-keys:
789e3af04fSMika Westerberg  *	SW_DOCK = 5
799e3af04fSMika Westerberg  *	SW_CAMERA_LENS_COVER = 9
809e3af04fSMika Westerberg  *	SW_KEYPAD_SLIDE = 10
819e3af04fSMika Westerberg  *	SW_FRONT_PROXIMITY = 11
829e3af04fSMika Westerberg  * This is read from switches:
839e3af04fSMika Westerberg  *	11-9,5
849e3af04fSMika Westerberg  * Next we want to disable proximity (11) and dock (5), we write:
859e3af04fSMika Westerberg  *	11,5
869e3af04fSMika Westerberg  * to file disabled_switches. Now proximity and dock IRQs are disabled.
879e3af04fSMika Westerberg  * This can be verified by reading the file disabled_switches:
889e3af04fSMika Westerberg  *	11,5
899e3af04fSMika Westerberg  * If we now want to enable proximity (11) switch we write:
909e3af04fSMika Westerberg  *	5
919e3af04fSMika Westerberg  * to disabled_switches.
929e3af04fSMika Westerberg  *
939e3af04fSMika Westerberg  * We can disable only those keys which don't allow sharing the irq.
949e3af04fSMika Westerberg  */
959e3af04fSMika Westerberg 
969e3af04fSMika Westerberg /**
979e3af04fSMika Westerberg  * get_n_events_by_type() - returns maximum number of events per @type
989e3af04fSMika Westerberg  * @type: type of button (%EV_KEY, %EV_SW)
999e3af04fSMika Westerberg  *
1009e3af04fSMika Westerberg  * Return value of this function can be used to allocate bitmap
1019e3af04fSMika Westerberg  * large enough to hold all bits for given type.
1029e3af04fSMika Westerberg  */
1038679ee42SDmitry Torokhov static int get_n_events_by_type(int type)
1049e3af04fSMika Westerberg {
1059e3af04fSMika Westerberg 	BUG_ON(type != EV_SW && type != EV_KEY);
1069e3af04fSMika Westerberg 
1079e3af04fSMika Westerberg 	return (type == EV_KEY) ? KEY_CNT : SW_CNT;
1089e3af04fSMika Westerberg }
1099e3af04fSMika Westerberg 
1109e3af04fSMika Westerberg /**
1118679ee42SDmitry Torokhov  * get_bm_events_by_type() - returns bitmap of supported events per @type
1128679ee42SDmitry Torokhov  * @input: input device from which bitmap is retrieved
1138679ee42SDmitry Torokhov  * @type: type of button (%EV_KEY, %EV_SW)
1148679ee42SDmitry Torokhov  *
1158679ee42SDmitry Torokhov  * Return value of this function can be used to allocate bitmap
1168679ee42SDmitry Torokhov  * large enough to hold all bits for given type.
1178679ee42SDmitry Torokhov  */
1188679ee42SDmitry Torokhov static const unsigned long *get_bm_events_by_type(struct input_dev *dev,
1198679ee42SDmitry Torokhov 						  int type)
1208679ee42SDmitry Torokhov {
1218679ee42SDmitry Torokhov 	BUG_ON(type != EV_SW && type != EV_KEY);
1228679ee42SDmitry Torokhov 
1238679ee42SDmitry Torokhov 	return (type == EV_KEY) ? dev->keybit : dev->swbit;
1248679ee42SDmitry Torokhov }
1258679ee42SDmitry Torokhov 
1268679ee42SDmitry Torokhov /**
1279e3af04fSMika Westerberg  * gpio_keys_disable_button() - disables given GPIO button
1289e3af04fSMika Westerberg  * @bdata: button data for button to be disabled
1299e3af04fSMika Westerberg  *
1309e3af04fSMika Westerberg  * Disables button pointed by @bdata. This is done by masking
1319e3af04fSMika Westerberg  * IRQ line. After this function is called, button won't generate
1329e3af04fSMika Westerberg  * input events anymore. Note that one can only disable buttons
1339e3af04fSMika Westerberg  * that don't share IRQs.
1349e3af04fSMika Westerberg  *
1359e3af04fSMika Westerberg  * Make sure that @bdata->disable_lock is locked when entering
1369e3af04fSMika Westerberg  * this function to avoid races when concurrent threads are
1379e3af04fSMika Westerberg  * disabling buttons at the same time.
1389e3af04fSMika Westerberg  */
1399e3af04fSMika Westerberg static void gpio_keys_disable_button(struct gpio_button_data *bdata)
1409e3af04fSMika Westerberg {
1419e3af04fSMika Westerberg 	if (!bdata->disabled) {
1429e3af04fSMika Westerberg 		/*
1438ed92556SDmitry Torokhov 		 * Disable IRQ and associated timer/work structure.
1449e3af04fSMika Westerberg 		 */
145d8ee4a1cSLaxman Dewangan 		disable_irq(bdata->irq);
1468ed92556SDmitry Torokhov 
1475feeca3cSGeert Uytterhoeven 		if (bdata->gpiod)
1488ed92556SDmitry Torokhov 			cancel_delayed_work_sync(&bdata->work);
1498ed92556SDmitry Torokhov 		else
1508ed92556SDmitry Torokhov 			del_timer_sync(&bdata->release_timer);
1519e3af04fSMika Westerberg 
1529e3af04fSMika Westerberg 		bdata->disabled = true;
1539e3af04fSMika Westerberg 	}
1549e3af04fSMika Westerberg }
1559e3af04fSMika Westerberg 
1569e3af04fSMika Westerberg /**
1579e3af04fSMika Westerberg  * gpio_keys_enable_button() - enables given GPIO button
1589e3af04fSMika Westerberg  * @bdata: button data for button to be disabled
1599e3af04fSMika Westerberg  *
1609e3af04fSMika Westerberg  * Enables given button pointed by @bdata.
1619e3af04fSMika Westerberg  *
1629e3af04fSMika Westerberg  * Make sure that @bdata->disable_lock is locked when entering
1639e3af04fSMika Westerberg  * this function to avoid races with concurrent threads trying
1649e3af04fSMika Westerberg  * to enable the same button at the same time.
1659e3af04fSMika Westerberg  */
1669e3af04fSMika Westerberg static void gpio_keys_enable_button(struct gpio_button_data *bdata)
1679e3af04fSMika Westerberg {
1689e3af04fSMika Westerberg 	if (bdata->disabled) {
169d8ee4a1cSLaxman Dewangan 		enable_irq(bdata->irq);
1709e3af04fSMika Westerberg 		bdata->disabled = false;
1719e3af04fSMika Westerberg 	}
1729e3af04fSMika Westerberg }
1739e3af04fSMika Westerberg 
1749e3af04fSMika Westerberg /**
1759e3af04fSMika Westerberg  * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
1769e3af04fSMika Westerberg  * @ddata: pointer to drvdata
1779e3af04fSMika Westerberg  * @buf: buffer where stringified bitmap is written
1789e3af04fSMika Westerberg  * @type: button type (%EV_KEY, %EV_SW)
1799e3af04fSMika Westerberg  * @only_disabled: does caller want only those buttons that are
1809e3af04fSMika Westerberg  *                 currently disabled or all buttons that can be
1819e3af04fSMika Westerberg  *                 disabled
1829e3af04fSMika Westerberg  *
1839e3af04fSMika Westerberg  * This function writes buttons that can be disabled to @buf. If
1849e3af04fSMika Westerberg  * @only_disabled is true, then @buf contains only those buttons
1859e3af04fSMika Westerberg  * that are currently disabled. Returns 0 on success or negative
1869e3af04fSMika Westerberg  * errno on failure.
1879e3af04fSMika Westerberg  */
1889e3af04fSMika Westerberg static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
1899e3af04fSMika Westerberg 					  char *buf, unsigned int type,
1909e3af04fSMika Westerberg 					  bool only_disabled)
1919e3af04fSMika Westerberg {
1929e3af04fSMika Westerberg 	int n_events = get_n_events_by_type(type);
1939e3af04fSMika Westerberg 	unsigned long *bits;
1949e3af04fSMika Westerberg 	ssize_t ret;
1959e3af04fSMika Westerberg 	int i;
1969e3af04fSMika Westerberg 
1979e3af04fSMika Westerberg 	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
1989e3af04fSMika Westerberg 	if (!bits)
1999e3af04fSMika Westerberg 		return -ENOMEM;
2009e3af04fSMika Westerberg 
201219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
2029e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
2039e3af04fSMika Westerberg 
2049e3af04fSMika Westerberg 		if (bdata->button->type != type)
2059e3af04fSMika Westerberg 			continue;
2069e3af04fSMika Westerberg 
2079e3af04fSMika Westerberg 		if (only_disabled && !bdata->disabled)
2089e3af04fSMika Westerberg 			continue;
2099e3af04fSMika Westerberg 
21083e4947aSHans de Goede 		__set_bit(*bdata->code, bits);
2119e3af04fSMika Westerberg 	}
2129e3af04fSMika Westerberg 
2130b480037STejun Heo 	ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
2149e3af04fSMika Westerberg 	buf[ret++] = '\n';
2159e3af04fSMika Westerberg 	buf[ret] = '\0';
2169e3af04fSMika Westerberg 
2179e3af04fSMika Westerberg 	kfree(bits);
2189e3af04fSMika Westerberg 
2199e3af04fSMika Westerberg 	return ret;
2209e3af04fSMika Westerberg }
2219e3af04fSMika Westerberg 
2229e3af04fSMika Westerberg /**
2239e3af04fSMika Westerberg  * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
2249e3af04fSMika Westerberg  * @ddata: pointer to drvdata
2259e3af04fSMika Westerberg  * @buf: buffer from userspace that contains stringified bitmap
2269e3af04fSMika Westerberg  * @type: button type (%EV_KEY, %EV_SW)
2279e3af04fSMika Westerberg  *
2289e3af04fSMika Westerberg  * This function parses stringified bitmap from @buf and disables/enables
229a16ca239SDmitry Torokhov  * GPIO buttons accordingly. Returns 0 on success and negative error
2309e3af04fSMika Westerberg  * on failure.
2319e3af04fSMika Westerberg  */
2329e3af04fSMika Westerberg static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
2339e3af04fSMika Westerberg 					   const char *buf, unsigned int type)
2349e3af04fSMika Westerberg {
2359e3af04fSMika Westerberg 	int n_events = get_n_events_by_type(type);
2368679ee42SDmitry Torokhov 	const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
2379e3af04fSMika Westerberg 	unsigned long *bits;
2389e3af04fSMika Westerberg 	ssize_t error;
2399e3af04fSMika Westerberg 	int i;
2409e3af04fSMika Westerberg 
2419e3af04fSMika Westerberg 	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
2429e3af04fSMika Westerberg 	if (!bits)
2439e3af04fSMika Westerberg 		return -ENOMEM;
2449e3af04fSMika Westerberg 
2459e3af04fSMika Westerberg 	error = bitmap_parselist(buf, bits, n_events);
2469e3af04fSMika Westerberg 	if (error)
2479e3af04fSMika Westerberg 		goto out;
2489e3af04fSMika Westerberg 
2499e3af04fSMika Westerberg 	/* First validate */
2508679ee42SDmitry Torokhov 	if (!bitmap_subset(bits, bitmap, n_events)) {
2518679ee42SDmitry Torokhov 		error = -EINVAL;
2528679ee42SDmitry Torokhov 		goto out;
2538679ee42SDmitry Torokhov 	}
2548679ee42SDmitry Torokhov 
255219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
2569e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
2579e3af04fSMika Westerberg 
2589e3af04fSMika Westerberg 		if (bdata->button->type != type)
2599e3af04fSMika Westerberg 			continue;
2609e3af04fSMika Westerberg 
26183e4947aSHans de Goede 		if (test_bit(*bdata->code, bits) &&
2629e3af04fSMika Westerberg 		    !bdata->button->can_disable) {
2639e3af04fSMika Westerberg 			error = -EINVAL;
2649e3af04fSMika Westerberg 			goto out;
2659e3af04fSMika Westerberg 		}
2669e3af04fSMika Westerberg 	}
2679e3af04fSMika Westerberg 
2689e3af04fSMika Westerberg 	mutex_lock(&ddata->disable_lock);
2699e3af04fSMika Westerberg 
270219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
2719e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
2729e3af04fSMika Westerberg 
2739e3af04fSMika Westerberg 		if (bdata->button->type != type)
2749e3af04fSMika Westerberg 			continue;
2759e3af04fSMika Westerberg 
27683e4947aSHans de Goede 		if (test_bit(*bdata->code, bits))
2779e3af04fSMika Westerberg 			gpio_keys_disable_button(bdata);
2789e3af04fSMika Westerberg 		else
2799e3af04fSMika Westerberg 			gpio_keys_enable_button(bdata);
2809e3af04fSMika Westerberg 	}
2819e3af04fSMika Westerberg 
2829e3af04fSMika Westerberg 	mutex_unlock(&ddata->disable_lock);
2839e3af04fSMika Westerberg 
2849e3af04fSMika Westerberg out:
2859e3af04fSMika Westerberg 	kfree(bits);
2869e3af04fSMika Westerberg 	return error;
2879e3af04fSMika Westerberg }
2889e3af04fSMika Westerberg 
2899e3af04fSMika Westerberg #define ATTR_SHOW_FN(name, type, only_disabled)				\
2909e3af04fSMika Westerberg static ssize_t gpio_keys_show_##name(struct device *dev,		\
2919e3af04fSMika Westerberg 				     struct device_attribute *attr,	\
2929e3af04fSMika Westerberg 				     char *buf)				\
2939e3af04fSMika Westerberg {									\
2949e3af04fSMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);		\
2959e3af04fSMika Westerberg 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);	\
2969e3af04fSMika Westerberg 									\
2979e3af04fSMika Westerberg 	return gpio_keys_attr_show_helper(ddata, buf,			\
2989e3af04fSMika Westerberg 					  type, only_disabled);		\
2999e3af04fSMika Westerberg }
3009e3af04fSMika Westerberg 
3019e3af04fSMika Westerberg ATTR_SHOW_FN(keys, EV_KEY, false);
3029e3af04fSMika Westerberg ATTR_SHOW_FN(switches, EV_SW, false);
3039e3af04fSMika Westerberg ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
3049e3af04fSMika Westerberg ATTR_SHOW_FN(disabled_switches, EV_SW, true);
3059e3af04fSMika Westerberg 
3069e3af04fSMika Westerberg /*
3079e3af04fSMika Westerberg  * ATTRIBUTES:
3089e3af04fSMika Westerberg  *
3099e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/keys [ro]
3109e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/switches [ro]
3119e3af04fSMika Westerberg  */
3129e3af04fSMika Westerberg static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
3139e3af04fSMika Westerberg static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
3149e3af04fSMika Westerberg 
3159e3af04fSMika Westerberg #define ATTR_STORE_FN(name, type)					\
3169e3af04fSMika Westerberg static ssize_t gpio_keys_store_##name(struct device *dev,		\
3179e3af04fSMika Westerberg 				      struct device_attribute *attr,	\
3189e3af04fSMika Westerberg 				      const char *buf,			\
3199e3af04fSMika Westerberg 				      size_t count)			\
3209e3af04fSMika Westerberg {									\
3219e3af04fSMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);		\
3229e3af04fSMika Westerberg 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);	\
3239e3af04fSMika Westerberg 	ssize_t error;							\
3249e3af04fSMika Westerberg 									\
3259e3af04fSMika Westerberg 	error = gpio_keys_attr_store_helper(ddata, buf, type);		\
3269e3af04fSMika Westerberg 	if (error)							\
3279e3af04fSMika Westerberg 		return error;						\
3289e3af04fSMika Westerberg 									\
3299e3af04fSMika Westerberg 	return count;							\
3309e3af04fSMika Westerberg }
3319e3af04fSMika Westerberg 
3329e3af04fSMika Westerberg ATTR_STORE_FN(disabled_keys, EV_KEY);
3339e3af04fSMika Westerberg ATTR_STORE_FN(disabled_switches, EV_SW);
3349e3af04fSMika Westerberg 
3359e3af04fSMika Westerberg /*
3369e3af04fSMika Westerberg  * ATTRIBUTES:
3379e3af04fSMika Westerberg  *
3389e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/disabled_keys [rw]
3399e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/disables_switches [rw]
3409e3af04fSMika Westerberg  */
3419e3af04fSMika Westerberg static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
3429e3af04fSMika Westerberg 		   gpio_keys_show_disabled_keys,
3439e3af04fSMika Westerberg 		   gpio_keys_store_disabled_keys);
3449e3af04fSMika Westerberg static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
3459e3af04fSMika Westerberg 		   gpio_keys_show_disabled_switches,
3469e3af04fSMika Westerberg 		   gpio_keys_store_disabled_switches);
3479e3af04fSMika Westerberg 
3489e3af04fSMika Westerberg static struct attribute *gpio_keys_attrs[] = {
3499e3af04fSMika Westerberg 	&dev_attr_keys.attr,
3509e3af04fSMika Westerberg 	&dev_attr_switches.attr,
3519e3af04fSMika Westerberg 	&dev_attr_disabled_keys.attr,
3529e3af04fSMika Westerberg 	&dev_attr_disabled_switches.attr,
3539e3af04fSMika Westerberg 	NULL,
3549e3af04fSMika Westerberg };
3559e3af04fSMika Westerberg 
3569e3af04fSMika Westerberg static struct attribute_group gpio_keys_attr_group = {
3579e3af04fSMika Westerberg 	.attrs = gpio_keys_attrs,
3589e3af04fSMika Westerberg };
3599e3af04fSMika Westerberg 
360d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
36178a56aabSPhil Blundell {
362d9080921SDmitry Torokhov 	const struct gpio_keys_button *button = bdata->button;
363ce25d7e9SUwe Kleine-König 	struct input_dev *input = bdata->input;
36484767d00SRoman Moravcik 	unsigned int type = button->type ?: EV_KEY;
3655feeca3cSGeert Uytterhoeven 	int state;
36684767d00SRoman Moravcik 
3675feeca3cSGeert Uytterhoeven 	state = gpiod_get_value_cansleep(bdata->gpiod);
36877fa0554SBjorn Andersson 	if (state < 0) {
3695feeca3cSGeert Uytterhoeven 		dev_err(input->dev.parent,
3705feeca3cSGeert Uytterhoeven 			"failed to get gpio state: %d\n", state);
37177fa0554SBjorn Andersson 		return;
37277fa0554SBjorn Andersson 	}
37377fa0554SBjorn Andersson 
37492a47674SAlexander Stein 	if (type == EV_ABS) {
37592a47674SAlexander Stein 		if (state)
37692a47674SAlexander Stein 			input_event(input, type, button->code, button->value);
37792a47674SAlexander Stein 	} else {
37883e4947aSHans de Goede 		input_event(input, type, *bdata->code, state);
37992a47674SAlexander Stein 	}
38078a56aabSPhil Blundell 	input_sync(input);
381a33466e3SDmitry Baryshkov }
382a33466e3SDmitry Baryshkov 
383d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_work_func(struct work_struct *work)
3846ee88d71SDaniel Mack {
3856ee88d71SDaniel Mack 	struct gpio_button_data *bdata =
3868ed92556SDmitry Torokhov 		container_of(work, struct gpio_button_data, work.work);
3876ee88d71SDaniel Mack 
388d8ee4a1cSLaxman Dewangan 	gpio_keys_gpio_report_event(bdata);
3892fba26c6SNeilBrown 
3902fba26c6SNeilBrown 	if (bdata->button->wakeup)
3912fba26c6SNeilBrown 		pm_relax(bdata->input->dev.parent);
3926ee88d71SDaniel Mack }
3936ee88d71SDaniel Mack 
394d8ee4a1cSLaxman Dewangan static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
395a33466e3SDmitry Baryshkov {
39657ffe9d5SUwe Kleine-König 	struct gpio_button_data *bdata = dev_id;
397a33466e3SDmitry Baryshkov 
398d8ee4a1cSLaxman Dewangan 	BUG_ON(irq != bdata->irq);
399a33466e3SDmitry Baryshkov 
400*0f107573SJoseph Lo 	if (bdata->button->wakeup) {
401*0f107573SJoseph Lo 		const struct gpio_keys_button *button = bdata->button;
402*0f107573SJoseph Lo 
4032fba26c6SNeilBrown 		pm_stay_awake(bdata->input->dev.parent);
404*0f107573SJoseph Lo 		if (bdata->suspended  &&
405*0f107573SJoseph Lo 		    (button->type == 0 || button->type == EV_KEY)) {
406*0f107573SJoseph Lo 			/*
407*0f107573SJoseph Lo 			 * Simulate wakeup key press in case the key has
408*0f107573SJoseph Lo 			 * already released by the time we got interrupt
409*0f107573SJoseph Lo 			 * handler to run.
410*0f107573SJoseph Lo 			 */
411*0f107573SJoseph Lo 			input_report_key(bdata->input, button->code, 1);
412*0f107573SJoseph Lo 		}
413*0f107573SJoseph Lo 	}
4148ed92556SDmitry Torokhov 
4158ed92556SDmitry Torokhov 	mod_delayed_work(system_wq,
4168ed92556SDmitry Torokhov 			 &bdata->work,
4178ed92556SDmitry Torokhov 			 msecs_to_jiffies(bdata->software_debounce));
418a33466e3SDmitry Baryshkov 
4191164ec1aSDavid Brownell 	return IRQ_HANDLED;
42078a56aabSPhil Blundell }
42178a56aabSPhil Blundell 
422d8ee4a1cSLaxman Dewangan static void gpio_keys_irq_timer(unsigned long _data)
423d8ee4a1cSLaxman Dewangan {
424d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
425d8ee4a1cSLaxman Dewangan 	struct input_dev *input = bdata->input;
426d8ee4a1cSLaxman Dewangan 	unsigned long flags;
427d8ee4a1cSLaxman Dewangan 
428d8ee4a1cSLaxman Dewangan 	spin_lock_irqsave(&bdata->lock, flags);
429d8ee4a1cSLaxman Dewangan 	if (bdata->key_pressed) {
43083e4947aSHans de Goede 		input_event(input, EV_KEY, *bdata->code, 0);
431d8ee4a1cSLaxman Dewangan 		input_sync(input);
432d8ee4a1cSLaxman Dewangan 		bdata->key_pressed = false;
433d8ee4a1cSLaxman Dewangan 	}
434d8ee4a1cSLaxman Dewangan 	spin_unlock_irqrestore(&bdata->lock, flags);
435d8ee4a1cSLaxman Dewangan }
436d8ee4a1cSLaxman Dewangan 
437d8ee4a1cSLaxman Dewangan static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
438d8ee4a1cSLaxman Dewangan {
439d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = dev_id;
440d8ee4a1cSLaxman Dewangan 	struct input_dev *input = bdata->input;
441d8ee4a1cSLaxman Dewangan 	unsigned long flags;
442d8ee4a1cSLaxman Dewangan 
443d8ee4a1cSLaxman Dewangan 	BUG_ON(irq != bdata->irq);
444d8ee4a1cSLaxman Dewangan 
445d8ee4a1cSLaxman Dewangan 	spin_lock_irqsave(&bdata->lock, flags);
446d8ee4a1cSLaxman Dewangan 
447d8ee4a1cSLaxman Dewangan 	if (!bdata->key_pressed) {
4482fba26c6SNeilBrown 		if (bdata->button->wakeup)
4492fba26c6SNeilBrown 			pm_wakeup_event(bdata->input->dev.parent, 0);
4502fba26c6SNeilBrown 
45183e4947aSHans de Goede 		input_event(input, EV_KEY, *bdata->code, 1);
452d8ee4a1cSLaxman Dewangan 		input_sync(input);
453d8ee4a1cSLaxman Dewangan 
4548ed92556SDmitry Torokhov 		if (!bdata->release_delay) {
45583e4947aSHans de Goede 			input_event(input, EV_KEY, *bdata->code, 0);
456d8ee4a1cSLaxman Dewangan 			input_sync(input);
457d8ee4a1cSLaxman Dewangan 			goto out;
458d8ee4a1cSLaxman Dewangan 		}
459d8ee4a1cSLaxman Dewangan 
460d8ee4a1cSLaxman Dewangan 		bdata->key_pressed = true;
461d8ee4a1cSLaxman Dewangan 	}
462d8ee4a1cSLaxman Dewangan 
4638ed92556SDmitry Torokhov 	if (bdata->release_delay)
4648ed92556SDmitry Torokhov 		mod_timer(&bdata->release_timer,
4658ed92556SDmitry Torokhov 			jiffies + msecs_to_jiffies(bdata->release_delay));
466d8ee4a1cSLaxman Dewangan out:
467d8ee4a1cSLaxman Dewangan 	spin_unlock_irqrestore(&bdata->lock, flags);
468d8ee4a1cSLaxman Dewangan 	return IRQ_HANDLED;
469d8ee4a1cSLaxman Dewangan }
470d8ee4a1cSLaxman Dewangan 
47127245519SAlexander Shiyan static void gpio_keys_quiesce_key(void *data)
47227245519SAlexander Shiyan {
47327245519SAlexander Shiyan 	struct gpio_button_data *bdata = data;
47427245519SAlexander Shiyan 
4755feeca3cSGeert Uytterhoeven 	if (bdata->gpiod)
4768ed92556SDmitry Torokhov 		cancel_delayed_work_sync(&bdata->work);
4778ed92556SDmitry Torokhov 	else
4788ed92556SDmitry Torokhov 		del_timer_sync(&bdata->release_timer);
47927245519SAlexander Shiyan }
48027245519SAlexander Shiyan 
4815298cc4cSBill Pemberton static int gpio_keys_setup_key(struct platform_device *pdev,
482d9080921SDmitry Torokhov 				struct input_dev *input,
48383e4947aSHans de Goede 				struct gpio_keys_drvdata *ddata,
484700a38b2SDmitry Torokhov 				const struct gpio_keys_button *button,
48583e4947aSHans de Goede 				int idx,
486700a38b2SDmitry Torokhov 				struct fwnode_handle *child)
487bc8f1eafSBen Dooks {
48892a47674SAlexander Stein 	const char *desc = button->desc ? button->desc : "gpio_keys";
4899e3af04fSMika Westerberg 	struct device *dev = &pdev->dev;
49083e4947aSHans de Goede 	struct gpio_button_data *bdata = &ddata->data[idx];
491d8ee4a1cSLaxman Dewangan 	irq_handler_t isr;
4929e3af04fSMika Westerberg 	unsigned long irqflags;
49327245519SAlexander Shiyan 	int irq;
49427245519SAlexander Shiyan 	int error;
495bc8f1eafSBen Dooks 
496d9080921SDmitry Torokhov 	bdata->input = input;
497d9080921SDmitry Torokhov 	bdata->button = button;
498d8ee4a1cSLaxman Dewangan 	spin_lock_init(&bdata->lock);
499d8ee4a1cSLaxman Dewangan 
500700a38b2SDmitry Torokhov 	if (child) {
5014b094797SBoris Brezillon 		bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL,
5024b094797SBoris Brezillon 								child,
5034b094797SBoris Brezillon 								GPIOD_IN,
5044b094797SBoris Brezillon 								desc);
505700a38b2SDmitry Torokhov 		if (IS_ERR(bdata->gpiod)) {
506700a38b2SDmitry Torokhov 			error = PTR_ERR(bdata->gpiod);
507700a38b2SDmitry Torokhov 			if (error == -ENOENT) {
508700a38b2SDmitry Torokhov 				/*
509700a38b2SDmitry Torokhov 				 * GPIO is optional, we may be dealing with
510700a38b2SDmitry Torokhov 				 * purely interrupt-driven setup.
511700a38b2SDmitry Torokhov 				 */
512700a38b2SDmitry Torokhov 				bdata->gpiod = NULL;
513700a38b2SDmitry Torokhov 			} else {
514700a38b2SDmitry Torokhov 				if (error != -EPROBE_DEFER)
515700a38b2SDmitry Torokhov 					dev_err(dev, "failed to get gpio: %d\n",
516700a38b2SDmitry Torokhov 						error);
517700a38b2SDmitry Torokhov 				return error;
518700a38b2SDmitry Torokhov 			}
519700a38b2SDmitry Torokhov 		}
520700a38b2SDmitry Torokhov 	} else if (gpio_is_valid(button->gpio)) {
5215feeca3cSGeert Uytterhoeven 		/*
5225feeca3cSGeert Uytterhoeven 		 * Legacy GPIO number, so request the GPIO here and
5235feeca3cSGeert Uytterhoeven 		 * convert it to descriptor.
5245feeca3cSGeert Uytterhoeven 		 */
5255feeca3cSGeert Uytterhoeven 		unsigned flags = GPIOF_IN;
526bc8f1eafSBen Dooks 
5275feeca3cSGeert Uytterhoeven 		if (button->active_low)
5285feeca3cSGeert Uytterhoeven 			flags |= GPIOF_ACTIVE_LOW;
5295feeca3cSGeert Uytterhoeven 
530b4e66e7dSGuenter Roeck 		error = devm_gpio_request_one(dev, button->gpio, flags, desc);
531bc8f1eafSBen Dooks 		if (error < 0) {
532d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Failed to request GPIO %d, error %d\n",
533bc8f1eafSBen Dooks 				button->gpio, error);
534d8ee4a1cSLaxman Dewangan 			return error;
535bc8f1eafSBen Dooks 		}
536bc8f1eafSBen Dooks 
5375feeca3cSGeert Uytterhoeven 		bdata->gpiod = gpio_to_desc(button->gpio);
5385feeca3cSGeert Uytterhoeven 		if (!bdata->gpiod)
5395feeca3cSGeert Uytterhoeven 			return -EINVAL;
540700a38b2SDmitry Torokhov 	}
5415feeca3cSGeert Uytterhoeven 
542700a38b2SDmitry Torokhov 	if (bdata->gpiod) {
54328ed684fSGrazvydas Ignotas 		if (button->debounce_interval) {
5445feeca3cSGeert Uytterhoeven 			error = gpiod_set_debounce(bdata->gpiod,
54528ed684fSGrazvydas Ignotas 					button->debounce_interval * 1000);
54628ed684fSGrazvydas Ignotas 			/* use timer if gpiolib doesn't provide debounce */
54728ed684fSGrazvydas Ignotas 			if (error < 0)
5488ed92556SDmitry Torokhov 				bdata->software_debounce =
549d8ee4a1cSLaxman Dewangan 						button->debounce_interval;
55028ed684fSGrazvydas Ignotas 		}
55128ed684fSGrazvydas Ignotas 
55297d86e07SDmitry Torokhov 		if (button->irq) {
55397d86e07SDmitry Torokhov 			bdata->irq = button->irq;
55497d86e07SDmitry Torokhov 		} else {
5555feeca3cSGeert Uytterhoeven 			irq = gpiod_to_irq(bdata->gpiod);
556bc8f1eafSBen Dooks 			if (irq < 0) {
557bc8f1eafSBen Dooks 				error = irq;
558d8ee4a1cSLaxman Dewangan 				dev_err(dev,
559d8ee4a1cSLaxman Dewangan 					"Unable to get irq number for GPIO %d, error %d\n",
560bc8f1eafSBen Dooks 					button->gpio, error);
56127245519SAlexander Shiyan 				return error;
562d8ee4a1cSLaxman Dewangan 			}
563d8ee4a1cSLaxman Dewangan 			bdata->irq = irq;
56497d86e07SDmitry Torokhov 		}
565d8ee4a1cSLaxman Dewangan 
5668ed92556SDmitry Torokhov 		INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
567d8ee4a1cSLaxman Dewangan 
568d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_gpio_isr;
569d8ee4a1cSLaxman Dewangan 		irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
570d8ee4a1cSLaxman Dewangan 
571d8ee4a1cSLaxman Dewangan 	} else {
572d8ee4a1cSLaxman Dewangan 		if (!button->irq) {
573700a38b2SDmitry Torokhov 			dev_err(dev, "Found button without gpio or irq\n");
574d8ee4a1cSLaxman Dewangan 			return -EINVAL;
575d8ee4a1cSLaxman Dewangan 		}
576700a38b2SDmitry Torokhov 
577d8ee4a1cSLaxman Dewangan 		bdata->irq = button->irq;
578d8ee4a1cSLaxman Dewangan 
579d8ee4a1cSLaxman Dewangan 		if (button->type && button->type != EV_KEY) {
580d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
581d8ee4a1cSLaxman Dewangan 			return -EINVAL;
582bc8f1eafSBen Dooks 		}
583bc8f1eafSBen Dooks 
5848ed92556SDmitry Torokhov 		bdata->release_delay = button->debounce_interval;
5858ed92556SDmitry Torokhov 		setup_timer(&bdata->release_timer,
586d8ee4a1cSLaxman Dewangan 			    gpio_keys_irq_timer, (unsigned long)bdata);
587d8ee4a1cSLaxman Dewangan 
588d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_irq_isr;
589d8ee4a1cSLaxman Dewangan 		irqflags = 0;
590d8ee4a1cSLaxman Dewangan 	}
591d8ee4a1cSLaxman Dewangan 
59283e4947aSHans de Goede 	bdata->code = &ddata->keymap[idx];
59383e4947aSHans de Goede 	*bdata->code = button->code;
59483e4947aSHans de Goede 	input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
595d8ee4a1cSLaxman Dewangan 
5969e3af04fSMika Westerberg 	/*
5978ed92556SDmitry Torokhov 	 * Install custom action to cancel release timer and
59827245519SAlexander Shiyan 	 * workqueue item.
59927245519SAlexander Shiyan 	 */
600b4e66e7dSGuenter Roeck 	error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
60127245519SAlexander Shiyan 	if (error) {
602b4e66e7dSGuenter Roeck 		dev_err(dev, "failed to register quiesce action, error: %d\n",
60327245519SAlexander Shiyan 			error);
60427245519SAlexander Shiyan 		return error;
60527245519SAlexander Shiyan 	}
60627245519SAlexander Shiyan 
60727245519SAlexander Shiyan 	/*
6089e3af04fSMika Westerberg 	 * If platform has specified that the button can be disabled,
6099e3af04fSMika Westerberg 	 * we don't want it to share the interrupt line.
6109e3af04fSMika Westerberg 	 */
6119e3af04fSMika Westerberg 	if (!button->can_disable)
6129e3af04fSMika Westerberg 		irqflags |= IRQF_SHARED;
6139e3af04fSMika Westerberg 
614b4e66e7dSGuenter Roeck 	error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
615b4e66e7dSGuenter Roeck 					     desc, bdata);
61694a8cab8SPhilippe Langlais 	if (error < 0) {
617bc8f1eafSBen Dooks 		dev_err(dev, "Unable to claim irq %d; error %d\n",
618d8ee4a1cSLaxman Dewangan 			bdata->irq, error);
61927245519SAlexander Shiyan 		return error;
620bc8f1eafSBen Dooks 	}
621bc8f1eafSBen Dooks 
622bc8f1eafSBen Dooks 	return 0;
623bc8f1eafSBen Dooks }
624bc8f1eafSBen Dooks 
6255b76d7b4SDmitry Torokhov static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
6265b76d7b4SDmitry Torokhov {
6275b76d7b4SDmitry Torokhov 	struct input_dev *input = ddata->input;
6285b76d7b4SDmitry Torokhov 	int i;
6295b76d7b4SDmitry Torokhov 
6305b76d7b4SDmitry Torokhov 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
6315b76d7b4SDmitry Torokhov 		struct gpio_button_data *bdata = &ddata->data[i];
6325feeca3cSGeert Uytterhoeven 		if (bdata->gpiod)
6335b76d7b4SDmitry Torokhov 			gpio_keys_gpio_report_event(bdata);
6345b76d7b4SDmitry Torokhov 	}
6355b76d7b4SDmitry Torokhov 	input_sync(input);
6365b76d7b4SDmitry Torokhov }
6375b76d7b4SDmitry Torokhov 
638173bdd74SShubhrajyoti D static int gpio_keys_open(struct input_dev *input)
639173bdd74SShubhrajyoti D {
640173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
641219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
6425b76d7b4SDmitry Torokhov 	int error;
643173bdd74SShubhrajyoti D 
6445b76d7b4SDmitry Torokhov 	if (pdata->enable) {
6455b76d7b4SDmitry Torokhov 		error = pdata->enable(input->dev.parent);
6465b76d7b4SDmitry Torokhov 		if (error)
6475b76d7b4SDmitry Torokhov 			return error;
6485b76d7b4SDmitry Torokhov 	}
6495b76d7b4SDmitry Torokhov 
6505b76d7b4SDmitry Torokhov 	/* Report current state of buttons that are connected to GPIOs */
6515b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
6525b76d7b4SDmitry Torokhov 
6535b76d7b4SDmitry Torokhov 	return 0;
654173bdd74SShubhrajyoti D }
655173bdd74SShubhrajyoti D 
656173bdd74SShubhrajyoti D static void gpio_keys_close(struct input_dev *input)
657173bdd74SShubhrajyoti D {
658173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
659219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
660173bdd74SShubhrajyoti D 
661219edc71SAlexandre Pereira da Silva 	if (pdata->disable)
662219edc71SAlexandre Pereira da Silva 		pdata->disable(input->dev.parent);
663173bdd74SShubhrajyoti D }
664173bdd74SShubhrajyoti D 
665fd05d089SDavid Jander /*
666fd05d089SDavid Jander  * Handlers for alternative sources of platform_data
667fd05d089SDavid Jander  */
668219edc71SAlexandre Pereira da Silva 
669fd05d089SDavid Jander /*
670700a38b2SDmitry Torokhov  * Translate properties into platform_data
671fd05d089SDavid Jander  */
6725298cc4cSBill Pemberton static struct gpio_keys_platform_data *
673219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
674fd05d089SDavid Jander {
675219edc71SAlexandre Pereira da Silva 	struct gpio_keys_platform_data *pdata;
676219edc71SAlexandre Pereira da Silva 	struct gpio_keys_button *button;
677700a38b2SDmitry Torokhov 	struct fwnode_handle *child;
678219edc71SAlexandre Pereira da Silva 	int nbuttons;
679fd05d089SDavid Jander 
680700a38b2SDmitry Torokhov 	nbuttons = device_get_child_node_count(dev);
6815d422f2eSAndy Shevchenko 	if (nbuttons == 0)
6825d422f2eSAndy Shevchenko 		return ERR_PTR(-ENODEV);
683219edc71SAlexandre Pereira da Silva 
6845d422f2eSAndy Shevchenko 	pdata = devm_kzalloc(dev,
6855d422f2eSAndy Shevchenko 			     sizeof(*pdata) + nbuttons * sizeof(*button),
686219edc71SAlexandre Pereira da Silva 			     GFP_KERNEL);
6875d422f2eSAndy Shevchenko 	if (!pdata)
6885d422f2eSAndy Shevchenko 		return ERR_PTR(-ENOMEM);
689219edc71SAlexandre Pereira da Silva 
6900f78ba96SDmitry Torokhov 	button = (struct gpio_keys_button *)(pdata + 1);
6910f78ba96SDmitry Torokhov 
6920f78ba96SDmitry Torokhov 	pdata->buttons = button;
693219edc71SAlexandre Pereira da Silva 	pdata->nbuttons = nbuttons;
694fd05d089SDavid Jander 
695700a38b2SDmitry Torokhov 	pdata->rep = device_property_read_bool(dev, "autorepeat");
696fd05d089SDavid Jander 
697700a38b2SDmitry Torokhov 	device_property_read_string(dev, "label", &pdata->name);
698c4dc5f8cSLaxman Dewangan 
699700a38b2SDmitry Torokhov 	device_for_each_child_node(dev, child) {
700700a38b2SDmitry Torokhov 		if (is_of_node(child))
701700a38b2SDmitry Torokhov 			button->irq =
702700a38b2SDmitry Torokhov 				irq_of_parse_and_map(to_of_node(child), 0);
703fd05d089SDavid Jander 
704700a38b2SDmitry Torokhov 		if (fwnode_property_read_u32(child, "linux,code",
705700a38b2SDmitry Torokhov 					     &button->code)) {
706700a38b2SDmitry Torokhov 			dev_err(dev, "Button without keycode\n");
707700a38b2SDmitry Torokhov 			fwnode_handle_put(child);
70897d86e07SDmitry Torokhov 			return ERR_PTR(-EINVAL);
70997d86e07SDmitry Torokhov 		}
710fd05d089SDavid Jander 
711700a38b2SDmitry Torokhov 		fwnode_property_read_string(child, "label", &button->desc);
712fd05d089SDavid Jander 
713700a38b2SDmitry Torokhov 		if (fwnode_property_read_u32(child, "linux,input-type",
714700a38b2SDmitry Torokhov 					     &button->type))
715219edc71SAlexandre Pereira da Silva 			button->type = EV_KEY;
716fd05d089SDavid Jander 
717700a38b2SDmitry Torokhov 		button->wakeup =
718700a38b2SDmitry Torokhov 			fwnode_property_read_bool(child, "wakeup-source") ||
71999b4ffbdSDmitry Torokhov 			/* legacy name */
720700a38b2SDmitry Torokhov 			fwnode_property_read_bool(child, "gpio-key,wakeup");
721219edc71SAlexandre Pereira da Silva 
722700a38b2SDmitry Torokhov 		button->can_disable =
723700a38b2SDmitry Torokhov 			fwnode_property_read_bool(child, "linux,can-disable");
72497d86e07SDmitry Torokhov 
725700a38b2SDmitry Torokhov 		if (fwnode_property_read_u32(child, "debounce-interval",
726219edc71SAlexandre Pereira da Silva 					 &button->debounce_interval))
727219edc71SAlexandre Pereira da Silva 			button->debounce_interval = 5;
7280f78ba96SDmitry Torokhov 
7290f78ba96SDmitry Torokhov 		button++;
730219edc71SAlexandre Pereira da Silva 	}
731219edc71SAlexandre Pereira da Silva 
732219edc71SAlexandre Pereira da Silva 	return pdata;
733fd05d089SDavid Jander }
734fd05d089SDavid Jander 
73522daae31SJingoo Han static const struct of_device_id gpio_keys_of_match[] = {
736fd05d089SDavid Jander 	{ .compatible = "gpio-keys", },
737fd05d089SDavid Jander 	{ },
738fd05d089SDavid Jander };
739fd05d089SDavid Jander MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
740fd05d089SDavid Jander 
7415298cc4cSBill Pemberton static int gpio_keys_probe(struct platform_device *pdev)
74278a56aabSPhil Blundell {
743db19fd8bSBen Dooks 	struct device *dev = &pdev->dev;
744219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
745700a38b2SDmitry Torokhov 	struct fwnode_handle *child = NULL;
746219edc71SAlexandre Pereira da Silva 	struct gpio_keys_drvdata *ddata;
74778a56aabSPhil Blundell 	struct input_dev *input;
7485d422f2eSAndy Shevchenko 	size_t size;
74978a56aabSPhil Blundell 	int i, error;
750e15b0213SAnti Sullin 	int wakeup = 0;
75178a56aabSPhil Blundell 
752fd05d089SDavid Jander 	if (!pdata) {
753219edc71SAlexandre Pereira da Silva 		pdata = gpio_keys_get_devtree_pdata(dev);
754219edc71SAlexandre Pereira da Silva 		if (IS_ERR(pdata))
755219edc71SAlexandre Pereira da Silva 			return PTR_ERR(pdata);
756fd05d089SDavid Jander 	}
757fd05d089SDavid Jander 
7585d422f2eSAndy Shevchenko 	size = sizeof(struct gpio_keys_drvdata) +
7595d422f2eSAndy Shevchenko 			pdata->nbuttons * sizeof(struct gpio_button_data);
7605d422f2eSAndy Shevchenko 	ddata = devm_kzalloc(dev, size, GFP_KERNEL);
7615d422f2eSAndy Shevchenko 	if (!ddata) {
762db19fd8bSBen Dooks 		dev_err(dev, "failed to allocate state\n");
7635d422f2eSAndy Shevchenko 		return -ENOMEM;
7645d422f2eSAndy Shevchenko 	}
7655d422f2eSAndy Shevchenko 
76683e4947aSHans de Goede 	ddata->keymap = devm_kcalloc(dev,
76783e4947aSHans de Goede 				     pdata->nbuttons, sizeof(ddata->keymap[0]),
76883e4947aSHans de Goede 				     GFP_KERNEL);
76983e4947aSHans de Goede 	if (!ddata->keymap)
77083e4947aSHans de Goede 		return -ENOMEM;
77183e4947aSHans de Goede 
7725d422f2eSAndy Shevchenko 	input = devm_input_allocate_device(dev);
7735d422f2eSAndy Shevchenko 	if (!input) {
7745d422f2eSAndy Shevchenko 		dev_err(dev, "failed to allocate input device\n");
7755d422f2eSAndy Shevchenko 		return -ENOMEM;
776a33466e3SDmitry Baryshkov 	}
77778a56aabSPhil Blundell 
778219edc71SAlexandre Pereira da Silva 	ddata->pdata = pdata;
7799e3af04fSMika Westerberg 	ddata->input = input;
7809e3af04fSMika Westerberg 	mutex_init(&ddata->disable_lock);
7819e3af04fSMika Westerberg 
782a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
783173bdd74SShubhrajyoti D 	input_set_drvdata(input, ddata);
78478a56aabSPhil Blundell 
78546711277SAlexander Stein 	input->name = pdata->name ? : pdev->name;
78678a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
787b4e66e7dSGuenter Roeck 	input->dev.parent = dev;
788173bdd74SShubhrajyoti D 	input->open = gpio_keys_open;
789173bdd74SShubhrajyoti D 	input->close = gpio_keys_close;
79078a56aabSPhil Blundell 
79178a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
79278a56aabSPhil Blundell 	input->id.vendor = 0x0001;
79378a56aabSPhil Blundell 	input->id.product = 0x0001;
79478a56aabSPhil Blundell 	input->id.version = 0x0100;
79578a56aabSPhil Blundell 
79683e4947aSHans de Goede 	input->keycode = ddata->keymap;
79783e4947aSHans de Goede 	input->keycodesize = sizeof(ddata->keymap[0]);
79883e4947aSHans de Goede 	input->keycodemax = pdata->nbuttons;
79983e4947aSHans de Goede 
800b67b4b11SDominic Curran 	/* Enable auto repeat feature of Linux input subsystem */
801b67b4b11SDominic Curran 	if (pdata->rep)
802b67b4b11SDominic Curran 		__set_bit(EV_REP, input->evbit);
803b67b4b11SDominic Curran 
80478a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
805d9080921SDmitry Torokhov 		const struct gpio_keys_button *button = &pdata->buttons[i];
80678a56aabSPhil Blundell 
807700a38b2SDmitry Torokhov 		if (!dev_get_platdata(dev)) {
808b4e66e7dSGuenter Roeck 			child = device_get_next_child_node(dev, child);
809700a38b2SDmitry Torokhov 			if (!child) {
810b4e66e7dSGuenter Roeck 				dev_err(dev,
811700a38b2SDmitry Torokhov 					"missing child device node for entry %d\n",
812700a38b2SDmitry Torokhov 					i);
813700a38b2SDmitry Torokhov 				return -EINVAL;
814700a38b2SDmitry Torokhov 			}
815700a38b2SDmitry Torokhov 		}
816700a38b2SDmitry Torokhov 
81783e4947aSHans de Goede 		error = gpio_keys_setup_key(pdev, input, ddata,
81883e4947aSHans de Goede 					    button, i, child);
819700a38b2SDmitry Torokhov 		if (error) {
820700a38b2SDmitry Torokhov 			fwnode_handle_put(child);
82127245519SAlexander Shiyan 			return error;
822700a38b2SDmitry Torokhov 		}
82384767d00SRoman Moravcik 
824e15b0213SAnti Sullin 		if (button->wakeup)
825e15b0213SAnti Sullin 			wakeup = 1;
82678a56aabSPhil Blundell 	}
82778a56aabSPhil Blundell 
828700a38b2SDmitry Torokhov 	fwnode_handle_put(child);
829700a38b2SDmitry Torokhov 
830b4e66e7dSGuenter Roeck 	error = sysfs_create_group(&dev->kobj, &gpio_keys_attr_group);
8319e3af04fSMika Westerberg 	if (error) {
8329e3af04fSMika Westerberg 		dev_err(dev, "Unable to export keys/switches, error: %d\n",
8339e3af04fSMika Westerberg 			error);
83427245519SAlexander Shiyan 		return error;
8359e3af04fSMika Westerberg 	}
8369e3af04fSMika Westerberg 
83778a56aabSPhil Blundell 	error = input_register_device(input);
83878a56aabSPhil Blundell 	if (error) {
8399e3af04fSMika Westerberg 		dev_err(dev, "Unable to register input device, error: %d\n",
8409e3af04fSMika Westerberg 			error);
84127245519SAlexander Shiyan 		goto err_remove_group;
84278a56aabSPhil Blundell 	}
84378a56aabSPhil Blundell 
844b4e66e7dSGuenter Roeck 	device_init_wakeup(dev, wakeup);
845e15b0213SAnti Sullin 
84678a56aabSPhil Blundell 	return 0;
84778a56aabSPhil Blundell 
84827245519SAlexander Shiyan err_remove_group:
849b4e66e7dSGuenter Roeck 	sysfs_remove_group(&dev->kobj, &gpio_keys_attr_group);
85078a56aabSPhil Blundell 	return error;
85178a56aabSPhil Blundell }
85278a56aabSPhil Blundell 
853e2619cf7SBill Pemberton static int gpio_keys_remove(struct platform_device *pdev)
85478a56aabSPhil Blundell {
8559e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
8569e3af04fSMika Westerberg 
85778a56aabSPhil Blundell 	return 0;
85878a56aabSPhil Blundell }
85978a56aabSPhil Blundell 
860f9645f22SDmitry Torokhov static int __maybe_unused gpio_keys_suspend(struct device *dev)
861e15b0213SAnti Sullin {
862fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
863dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
864e15b0213SAnti Sullin 	int i;
865e15b0213SAnti Sullin 
866fd05d089SDavid Jander 	if (device_may_wakeup(dev)) {
867219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
868d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
869d8ee4a1cSLaxman Dewangan 			if (bdata->button->wakeup)
870d8ee4a1cSLaxman Dewangan 				enable_irq_wake(bdata->irq);
871*0f107573SJoseph Lo 			bdata->suspended = true;
872e15b0213SAnti Sullin 		}
873dda19a92SJonas Aaberg 	} else {
874dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
875dda19a92SJonas Aaberg 		if (input->users)
876dda19a92SJonas Aaberg 			gpio_keys_close(input);
877dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
878e15b0213SAnti Sullin 	}
879e15b0213SAnti Sullin 
880e15b0213SAnti Sullin 	return 0;
881e15b0213SAnti Sullin }
882e15b0213SAnti Sullin 
883f9645f22SDmitry Torokhov static int __maybe_unused gpio_keys_resume(struct device *dev)
884e15b0213SAnti Sullin {
885fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
886dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
887dda19a92SJonas Aaberg 	int error = 0;
888e15b0213SAnti Sullin 	int i;
889e15b0213SAnti Sullin 
890dda19a92SJonas Aaberg 	if (device_may_wakeup(dev)) {
891219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
892d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
893dda19a92SJonas Aaberg 			if (bdata->button->wakeup)
894d8ee4a1cSLaxman Dewangan 				disable_irq_wake(bdata->irq);
895*0f107573SJoseph Lo 			bdata->suspended = false;
896e15b0213SAnti Sullin 		}
897dda19a92SJonas Aaberg 	} else {
898dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
899dda19a92SJonas Aaberg 		if (input->users)
900dda19a92SJonas Aaberg 			error = gpio_keys_open(input);
901dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
902dda19a92SJonas Aaberg 	}
903dda19a92SJonas Aaberg 
904dda19a92SJonas Aaberg 	if (error)
905dda19a92SJonas Aaberg 		return error;
9065b76d7b4SDmitry Torokhov 
9075b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
908e15b0213SAnti Sullin 	return 0;
909e15b0213SAnti Sullin }
910e15b0213SAnti Sullin 
911bdda8216SDmitry Torokhov static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
912bdda8216SDmitry Torokhov 
9139b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = {
91478a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
9151cb0aa88SBill Pemberton 	.remove		= gpio_keys_remove,
91678a56aabSPhil Blundell 	.driver		= {
91778a56aabSPhil Blundell 		.name	= "gpio-keys",
918ae78e0e0SMike Rapoport 		.pm	= &gpio_keys_pm_ops,
919700a38b2SDmitry Torokhov 		.of_match_table = gpio_keys_of_match,
92078a56aabSPhil Blundell 	}
92178a56aabSPhil Blundell };
92278a56aabSPhil Blundell 
92378a56aabSPhil Blundell static int __init gpio_keys_init(void)
92478a56aabSPhil Blundell {
92578a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
92678a56aabSPhil Blundell }
92778a56aabSPhil Blundell 
92878a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
92978a56aabSPhil Blundell {
93078a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
93178a56aabSPhil Blundell }
93278a56aabSPhil Blundell 
933b2330205SDavid Jander late_initcall(gpio_keys_init);
93478a56aabSPhil Blundell module_exit(gpio_keys_exit);
93578a56aabSPhil Blundell 
93678a56aabSPhil Blundell MODULE_LICENSE("GPL");
93778a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
9387e2ecdf4SDavid Jander MODULE_DESCRIPTION("Keyboard driver for GPIOs");
939d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
940