xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision 97d86e07b71643086a6d22a60efae2fb095fa82a)
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>
29415a4caaSSachin Kamat #include <linux/of.h>
30fd05d089SDavid Jander #include <linux/of_platform.h>
31fd05d089SDavid Jander #include <linux/of_gpio.h>
32f2d347ffSAlexander Stein #include <linux/of_irq.h>
33d8ee4a1cSLaxman Dewangan #include <linux/spinlock.h>
3478a56aabSPhil Blundell 
35a33466e3SDmitry Baryshkov struct gpio_button_data {
36d9080921SDmitry Torokhov 	const struct gpio_keys_button *button;
37a33466e3SDmitry Baryshkov 	struct input_dev *input;
38ca865a77SJani Nikula 	struct timer_list timer;
39da0d03feSJani Nikula 	struct work_struct work;
40d8ee4a1cSLaxman Dewangan 	unsigned int timer_debounce;	/* in msecs */
41d8ee4a1cSLaxman Dewangan 	unsigned int irq;
42d8ee4a1cSLaxman Dewangan 	spinlock_t lock;
439e3af04fSMika Westerberg 	bool disabled;
44d8ee4a1cSLaxman Dewangan 	bool key_pressed;
45a33466e3SDmitry Baryshkov };
46a33466e3SDmitry Baryshkov 
47a33466e3SDmitry Baryshkov struct gpio_keys_drvdata {
48219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata;
49a33466e3SDmitry Baryshkov 	struct input_dev *input;
509e3af04fSMika Westerberg 	struct mutex disable_lock;
51a33466e3SDmitry Baryshkov 	struct gpio_button_data data[0];
52a33466e3SDmitry Baryshkov };
53a33466e3SDmitry Baryshkov 
549e3af04fSMika Westerberg /*
559e3af04fSMika Westerberg  * SYSFS interface for enabling/disabling keys and switches:
569e3af04fSMika Westerberg  *
579e3af04fSMika Westerberg  * There are 4 attributes under /sys/devices/platform/gpio-keys/
589e3af04fSMika Westerberg  *	keys [ro]              - bitmap of keys (EV_KEY) which can be
599e3af04fSMika Westerberg  *	                         disabled
609e3af04fSMika Westerberg  *	switches [ro]          - bitmap of switches (EV_SW) which can be
619e3af04fSMika Westerberg  *	                         disabled
629e3af04fSMika Westerberg  *	disabled_keys [rw]     - bitmap of keys currently disabled
639e3af04fSMika Westerberg  *	disabled_switches [rw] - bitmap of switches currently disabled
649e3af04fSMika Westerberg  *
659e3af04fSMika Westerberg  * Userland can change these values and hence disable event generation
669e3af04fSMika Westerberg  * for each key (or switch). Disabling a key means its interrupt line
679e3af04fSMika Westerberg  * is disabled.
689e3af04fSMika Westerberg  *
699e3af04fSMika Westerberg  * For example, if we have following switches set up as gpio-keys:
709e3af04fSMika Westerberg  *	SW_DOCK = 5
719e3af04fSMika Westerberg  *	SW_CAMERA_LENS_COVER = 9
729e3af04fSMika Westerberg  *	SW_KEYPAD_SLIDE = 10
739e3af04fSMika Westerberg  *	SW_FRONT_PROXIMITY = 11
749e3af04fSMika Westerberg  * This is read from switches:
759e3af04fSMika Westerberg  *	11-9,5
769e3af04fSMika Westerberg  * Next we want to disable proximity (11) and dock (5), we write:
779e3af04fSMika Westerberg  *	11,5
789e3af04fSMika Westerberg  * to file disabled_switches. Now proximity and dock IRQs are disabled.
799e3af04fSMika Westerberg  * This can be verified by reading the file disabled_switches:
809e3af04fSMika Westerberg  *	11,5
819e3af04fSMika Westerberg  * If we now want to enable proximity (11) switch we write:
829e3af04fSMika Westerberg  *	5
839e3af04fSMika Westerberg  * to disabled_switches.
849e3af04fSMika Westerberg  *
859e3af04fSMika Westerberg  * We can disable only those keys which don't allow sharing the irq.
869e3af04fSMika Westerberg  */
879e3af04fSMika Westerberg 
889e3af04fSMika Westerberg /**
899e3af04fSMika Westerberg  * get_n_events_by_type() - returns maximum number of events per @type
909e3af04fSMika Westerberg  * @type: type of button (%EV_KEY, %EV_SW)
919e3af04fSMika Westerberg  *
929e3af04fSMika Westerberg  * Return value of this function can be used to allocate bitmap
939e3af04fSMika Westerberg  * large enough to hold all bits for given type.
949e3af04fSMika Westerberg  */
959e3af04fSMika Westerberg static inline int get_n_events_by_type(int type)
969e3af04fSMika Westerberg {
979e3af04fSMika Westerberg 	BUG_ON(type != EV_SW && type != EV_KEY);
989e3af04fSMika Westerberg 
999e3af04fSMika Westerberg 	return (type == EV_KEY) ? KEY_CNT : SW_CNT;
1009e3af04fSMika Westerberg }
1019e3af04fSMika Westerberg 
1029e3af04fSMika Westerberg /**
1039e3af04fSMika Westerberg  * gpio_keys_disable_button() - disables given GPIO button
1049e3af04fSMika Westerberg  * @bdata: button data for button to be disabled
1059e3af04fSMika Westerberg  *
1069e3af04fSMika Westerberg  * Disables button pointed by @bdata. This is done by masking
1079e3af04fSMika Westerberg  * IRQ line. After this function is called, button won't generate
1089e3af04fSMika Westerberg  * input events anymore. Note that one can only disable buttons
1099e3af04fSMika Westerberg  * that don't share IRQs.
1109e3af04fSMika Westerberg  *
1119e3af04fSMika Westerberg  * Make sure that @bdata->disable_lock is locked when entering
1129e3af04fSMika Westerberg  * this function to avoid races when concurrent threads are
1139e3af04fSMika Westerberg  * disabling buttons at the same time.
1149e3af04fSMika Westerberg  */
1159e3af04fSMika Westerberg static void gpio_keys_disable_button(struct gpio_button_data *bdata)
1169e3af04fSMika Westerberg {
1179e3af04fSMika Westerberg 	if (!bdata->disabled) {
1189e3af04fSMika Westerberg 		/*
1199e3af04fSMika Westerberg 		 * Disable IRQ and possible debouncing timer.
1209e3af04fSMika Westerberg 		 */
121d8ee4a1cSLaxman Dewangan 		disable_irq(bdata->irq);
12228ed684fSGrazvydas Ignotas 		if (bdata->timer_debounce)
1239e3af04fSMika Westerberg 			del_timer_sync(&bdata->timer);
1249e3af04fSMika Westerberg 
1259e3af04fSMika Westerberg 		bdata->disabled = true;
1269e3af04fSMika Westerberg 	}
1279e3af04fSMika Westerberg }
1289e3af04fSMika Westerberg 
1299e3af04fSMika Westerberg /**
1309e3af04fSMika Westerberg  * gpio_keys_enable_button() - enables given GPIO button
1319e3af04fSMika Westerberg  * @bdata: button data for button to be disabled
1329e3af04fSMika Westerberg  *
1339e3af04fSMika Westerberg  * Enables given button pointed by @bdata.
1349e3af04fSMika Westerberg  *
1359e3af04fSMika Westerberg  * Make sure that @bdata->disable_lock is locked when entering
1369e3af04fSMika Westerberg  * this function to avoid races with concurrent threads trying
1379e3af04fSMika Westerberg  * to enable the same button at the same time.
1389e3af04fSMika Westerberg  */
1399e3af04fSMika Westerberg static void gpio_keys_enable_button(struct gpio_button_data *bdata)
1409e3af04fSMika Westerberg {
1419e3af04fSMika Westerberg 	if (bdata->disabled) {
142d8ee4a1cSLaxman Dewangan 		enable_irq(bdata->irq);
1439e3af04fSMika Westerberg 		bdata->disabled = false;
1449e3af04fSMika Westerberg 	}
1459e3af04fSMika Westerberg }
1469e3af04fSMika Westerberg 
1479e3af04fSMika Westerberg /**
1489e3af04fSMika Westerberg  * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
1499e3af04fSMika Westerberg  * @ddata: pointer to drvdata
1509e3af04fSMika Westerberg  * @buf: buffer where stringified bitmap is written
1519e3af04fSMika Westerberg  * @type: button type (%EV_KEY, %EV_SW)
1529e3af04fSMika Westerberg  * @only_disabled: does caller want only those buttons that are
1539e3af04fSMika Westerberg  *                 currently disabled or all buttons that can be
1549e3af04fSMika Westerberg  *                 disabled
1559e3af04fSMika Westerberg  *
1569e3af04fSMika Westerberg  * This function writes buttons that can be disabled to @buf. If
1579e3af04fSMika Westerberg  * @only_disabled is true, then @buf contains only those buttons
1589e3af04fSMika Westerberg  * that are currently disabled. Returns 0 on success or negative
1599e3af04fSMika Westerberg  * errno on failure.
1609e3af04fSMika Westerberg  */
1619e3af04fSMika Westerberg static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
1629e3af04fSMika Westerberg 					  char *buf, unsigned int type,
1639e3af04fSMika Westerberg 					  bool only_disabled)
1649e3af04fSMika Westerberg {
1659e3af04fSMika Westerberg 	int n_events = get_n_events_by_type(type);
1669e3af04fSMika Westerberg 	unsigned long *bits;
1679e3af04fSMika Westerberg 	ssize_t ret;
1689e3af04fSMika Westerberg 	int i;
1699e3af04fSMika Westerberg 
1709e3af04fSMika Westerberg 	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
1719e3af04fSMika Westerberg 	if (!bits)
1729e3af04fSMika Westerberg 		return -ENOMEM;
1739e3af04fSMika Westerberg 
174219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
1759e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
1769e3af04fSMika Westerberg 
1779e3af04fSMika Westerberg 		if (bdata->button->type != type)
1789e3af04fSMika Westerberg 			continue;
1799e3af04fSMika Westerberg 
1809e3af04fSMika Westerberg 		if (only_disabled && !bdata->disabled)
1819e3af04fSMika Westerberg 			continue;
1829e3af04fSMika Westerberg 
1839e3af04fSMika Westerberg 		__set_bit(bdata->button->code, bits);
1849e3af04fSMika Westerberg 	}
1859e3af04fSMika Westerberg 
1869e3af04fSMika Westerberg 	ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
1879e3af04fSMika Westerberg 	buf[ret++] = '\n';
1889e3af04fSMika Westerberg 	buf[ret] = '\0';
1899e3af04fSMika Westerberg 
1909e3af04fSMika Westerberg 	kfree(bits);
1919e3af04fSMika Westerberg 
1929e3af04fSMika Westerberg 	return ret;
1939e3af04fSMika Westerberg }
1949e3af04fSMika Westerberg 
1959e3af04fSMika Westerberg /**
1969e3af04fSMika Westerberg  * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
1979e3af04fSMika Westerberg  * @ddata: pointer to drvdata
1989e3af04fSMika Westerberg  * @buf: buffer from userspace that contains stringified bitmap
1999e3af04fSMika Westerberg  * @type: button type (%EV_KEY, %EV_SW)
2009e3af04fSMika Westerberg  *
2019e3af04fSMika Westerberg  * This function parses stringified bitmap from @buf and disables/enables
202a16ca239SDmitry Torokhov  * GPIO buttons accordingly. Returns 0 on success and negative error
2039e3af04fSMika Westerberg  * on failure.
2049e3af04fSMika Westerberg  */
2059e3af04fSMika Westerberg static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
2069e3af04fSMika Westerberg 					   const char *buf, unsigned int type)
2079e3af04fSMika Westerberg {
2089e3af04fSMika Westerberg 	int n_events = get_n_events_by_type(type);
2099e3af04fSMika Westerberg 	unsigned long *bits;
2109e3af04fSMika Westerberg 	ssize_t error;
2119e3af04fSMika Westerberg 	int i;
2129e3af04fSMika Westerberg 
2139e3af04fSMika Westerberg 	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
2149e3af04fSMika Westerberg 	if (!bits)
2159e3af04fSMika Westerberg 		return -ENOMEM;
2169e3af04fSMika Westerberg 
2179e3af04fSMika Westerberg 	error = bitmap_parselist(buf, bits, n_events);
2189e3af04fSMika Westerberg 	if (error)
2199e3af04fSMika Westerberg 		goto out;
2209e3af04fSMika Westerberg 
2219e3af04fSMika Westerberg 	/* First validate */
222219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
2239e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
2249e3af04fSMika Westerberg 
2259e3af04fSMika Westerberg 		if (bdata->button->type != type)
2269e3af04fSMika Westerberg 			continue;
2279e3af04fSMika Westerberg 
2289e3af04fSMika Westerberg 		if (test_bit(bdata->button->code, bits) &&
2299e3af04fSMika Westerberg 		    !bdata->button->can_disable) {
2309e3af04fSMika Westerberg 			error = -EINVAL;
2319e3af04fSMika Westerberg 			goto out;
2329e3af04fSMika Westerberg 		}
2339e3af04fSMika Westerberg 	}
2349e3af04fSMika Westerberg 
2359e3af04fSMika Westerberg 	mutex_lock(&ddata->disable_lock);
2369e3af04fSMika Westerberg 
237219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
2389e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
2399e3af04fSMika Westerberg 
2409e3af04fSMika Westerberg 		if (bdata->button->type != type)
2419e3af04fSMika Westerberg 			continue;
2429e3af04fSMika Westerberg 
2439e3af04fSMika Westerberg 		if (test_bit(bdata->button->code, bits))
2449e3af04fSMika Westerberg 			gpio_keys_disable_button(bdata);
2459e3af04fSMika Westerberg 		else
2469e3af04fSMika Westerberg 			gpio_keys_enable_button(bdata);
2479e3af04fSMika Westerberg 	}
2489e3af04fSMika Westerberg 
2499e3af04fSMika Westerberg 	mutex_unlock(&ddata->disable_lock);
2509e3af04fSMika Westerberg 
2519e3af04fSMika Westerberg out:
2529e3af04fSMika Westerberg 	kfree(bits);
2539e3af04fSMika Westerberg 	return error;
2549e3af04fSMika Westerberg }
2559e3af04fSMika Westerberg 
2569e3af04fSMika Westerberg #define ATTR_SHOW_FN(name, type, only_disabled)				\
2579e3af04fSMika Westerberg static ssize_t gpio_keys_show_##name(struct device *dev,		\
2589e3af04fSMika Westerberg 				     struct device_attribute *attr,	\
2599e3af04fSMika Westerberg 				     char *buf)				\
2609e3af04fSMika Westerberg {									\
2619e3af04fSMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);		\
2629e3af04fSMika Westerberg 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);	\
2639e3af04fSMika Westerberg 									\
2649e3af04fSMika Westerberg 	return gpio_keys_attr_show_helper(ddata, buf,			\
2659e3af04fSMika Westerberg 					  type, only_disabled);		\
2669e3af04fSMika Westerberg }
2679e3af04fSMika Westerberg 
2689e3af04fSMika Westerberg ATTR_SHOW_FN(keys, EV_KEY, false);
2699e3af04fSMika Westerberg ATTR_SHOW_FN(switches, EV_SW, false);
2709e3af04fSMika Westerberg ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
2719e3af04fSMika Westerberg ATTR_SHOW_FN(disabled_switches, EV_SW, true);
2729e3af04fSMika Westerberg 
2739e3af04fSMika Westerberg /*
2749e3af04fSMika Westerberg  * ATTRIBUTES:
2759e3af04fSMika Westerberg  *
2769e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/keys [ro]
2779e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/switches [ro]
2789e3af04fSMika Westerberg  */
2799e3af04fSMika Westerberg static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
2809e3af04fSMika Westerberg static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
2819e3af04fSMika Westerberg 
2829e3af04fSMika Westerberg #define ATTR_STORE_FN(name, type)					\
2839e3af04fSMika Westerberg static ssize_t gpio_keys_store_##name(struct device *dev,		\
2849e3af04fSMika Westerberg 				      struct device_attribute *attr,	\
2859e3af04fSMika Westerberg 				      const char *buf,			\
2869e3af04fSMika Westerberg 				      size_t count)			\
2879e3af04fSMika Westerberg {									\
2889e3af04fSMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);		\
2899e3af04fSMika Westerberg 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);	\
2909e3af04fSMika Westerberg 	ssize_t error;							\
2919e3af04fSMika Westerberg 									\
2929e3af04fSMika Westerberg 	error = gpio_keys_attr_store_helper(ddata, buf, type);		\
2939e3af04fSMika Westerberg 	if (error)							\
2949e3af04fSMika Westerberg 		return error;						\
2959e3af04fSMika Westerberg 									\
2969e3af04fSMika Westerberg 	return count;							\
2979e3af04fSMika Westerberg }
2989e3af04fSMika Westerberg 
2999e3af04fSMika Westerberg ATTR_STORE_FN(disabled_keys, EV_KEY);
3009e3af04fSMika Westerberg ATTR_STORE_FN(disabled_switches, EV_SW);
3019e3af04fSMika Westerberg 
3029e3af04fSMika Westerberg /*
3039e3af04fSMika Westerberg  * ATTRIBUTES:
3049e3af04fSMika Westerberg  *
3059e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/disabled_keys [rw]
3069e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/disables_switches [rw]
3079e3af04fSMika Westerberg  */
3089e3af04fSMika Westerberg static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
3099e3af04fSMika Westerberg 		   gpio_keys_show_disabled_keys,
3109e3af04fSMika Westerberg 		   gpio_keys_store_disabled_keys);
3119e3af04fSMika Westerberg static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
3129e3af04fSMika Westerberg 		   gpio_keys_show_disabled_switches,
3139e3af04fSMika Westerberg 		   gpio_keys_store_disabled_switches);
3149e3af04fSMika Westerberg 
3159e3af04fSMika Westerberg static struct attribute *gpio_keys_attrs[] = {
3169e3af04fSMika Westerberg 	&dev_attr_keys.attr,
3179e3af04fSMika Westerberg 	&dev_attr_switches.attr,
3189e3af04fSMika Westerberg 	&dev_attr_disabled_keys.attr,
3199e3af04fSMika Westerberg 	&dev_attr_disabled_switches.attr,
3209e3af04fSMika Westerberg 	NULL,
3219e3af04fSMika Westerberg };
3229e3af04fSMika Westerberg 
3239e3af04fSMika Westerberg static struct attribute_group gpio_keys_attr_group = {
3249e3af04fSMika Westerberg 	.attrs = gpio_keys_attrs,
3259e3af04fSMika Westerberg };
3269e3af04fSMika Westerberg 
327d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
32878a56aabSPhil Blundell {
329d9080921SDmitry Torokhov 	const struct gpio_keys_button *button = bdata->button;
330ce25d7e9SUwe Kleine-König 	struct input_dev *input = bdata->input;
33184767d00SRoman Moravcik 	unsigned int type = button->type ?: EV_KEY;
33294a8cab8SPhilippe Langlais 	int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
33384767d00SRoman Moravcik 
33492a47674SAlexander Stein 	if (type == EV_ABS) {
33592a47674SAlexander Stein 		if (state)
33692a47674SAlexander Stein 			input_event(input, type, button->code, button->value);
33792a47674SAlexander Stein 	} else {
33884767d00SRoman Moravcik 		input_event(input, type, button->code, !!state);
33992a47674SAlexander Stein 	}
34078a56aabSPhil Blundell 	input_sync(input);
341a33466e3SDmitry Baryshkov }
342a33466e3SDmitry Baryshkov 
343d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_work_func(struct work_struct *work)
3446ee88d71SDaniel Mack {
3456ee88d71SDaniel Mack 	struct gpio_button_data *bdata =
3466ee88d71SDaniel Mack 		container_of(work, struct gpio_button_data, work);
3476ee88d71SDaniel Mack 
348d8ee4a1cSLaxman Dewangan 	gpio_keys_gpio_report_event(bdata);
3492fba26c6SNeilBrown 
3502fba26c6SNeilBrown 	if (bdata->button->wakeup)
3512fba26c6SNeilBrown 		pm_relax(bdata->input->dev.parent);
3526ee88d71SDaniel Mack }
3536ee88d71SDaniel Mack 
354d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_timer(unsigned long _data)
355ca865a77SJani Nikula {
356d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
357ca865a77SJani Nikula 
358d8ee4a1cSLaxman Dewangan 	schedule_work(&bdata->work);
359ca865a77SJani Nikula }
360ca865a77SJani Nikula 
361d8ee4a1cSLaxman Dewangan static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
362a33466e3SDmitry Baryshkov {
36357ffe9d5SUwe Kleine-König 	struct gpio_button_data *bdata = dev_id;
364a33466e3SDmitry Baryshkov 
365d8ee4a1cSLaxman Dewangan 	BUG_ON(irq != bdata->irq);
366a33466e3SDmitry Baryshkov 
3672fba26c6SNeilBrown 	if (bdata->button->wakeup)
3682fba26c6SNeilBrown 		pm_stay_awake(bdata->input->dev.parent);
36928ed684fSGrazvydas Ignotas 	if (bdata->timer_debounce)
370ca865a77SJani Nikula 		mod_timer(&bdata->timer,
37128ed684fSGrazvydas Ignotas 			jiffies + msecs_to_jiffies(bdata->timer_debounce));
372ca865a77SJani Nikula 	else
373da0d03feSJani Nikula 		schedule_work(&bdata->work);
374a33466e3SDmitry Baryshkov 
3751164ec1aSDavid Brownell 	return IRQ_HANDLED;
37678a56aabSPhil Blundell }
37778a56aabSPhil Blundell 
378d8ee4a1cSLaxman Dewangan static void gpio_keys_irq_timer(unsigned long _data)
379d8ee4a1cSLaxman Dewangan {
380d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
381d8ee4a1cSLaxman Dewangan 	struct input_dev *input = bdata->input;
382d8ee4a1cSLaxman Dewangan 	unsigned long flags;
383d8ee4a1cSLaxman Dewangan 
384d8ee4a1cSLaxman Dewangan 	spin_lock_irqsave(&bdata->lock, flags);
385d8ee4a1cSLaxman Dewangan 	if (bdata->key_pressed) {
386d8ee4a1cSLaxman Dewangan 		input_event(input, EV_KEY, bdata->button->code, 0);
387d8ee4a1cSLaxman Dewangan 		input_sync(input);
388d8ee4a1cSLaxman Dewangan 		bdata->key_pressed = false;
389d8ee4a1cSLaxman Dewangan 	}
390d8ee4a1cSLaxman Dewangan 	spin_unlock_irqrestore(&bdata->lock, flags);
391d8ee4a1cSLaxman Dewangan }
392d8ee4a1cSLaxman Dewangan 
393d8ee4a1cSLaxman Dewangan static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
394d8ee4a1cSLaxman Dewangan {
395d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = dev_id;
396d8ee4a1cSLaxman Dewangan 	const struct gpio_keys_button *button = bdata->button;
397d8ee4a1cSLaxman Dewangan 	struct input_dev *input = bdata->input;
398d8ee4a1cSLaxman Dewangan 	unsigned long flags;
399d8ee4a1cSLaxman Dewangan 
400d8ee4a1cSLaxman Dewangan 	BUG_ON(irq != bdata->irq);
401d8ee4a1cSLaxman Dewangan 
402d8ee4a1cSLaxman Dewangan 	spin_lock_irqsave(&bdata->lock, flags);
403d8ee4a1cSLaxman Dewangan 
404d8ee4a1cSLaxman Dewangan 	if (!bdata->key_pressed) {
4052fba26c6SNeilBrown 		if (bdata->button->wakeup)
4062fba26c6SNeilBrown 			pm_wakeup_event(bdata->input->dev.parent, 0);
4072fba26c6SNeilBrown 
408d8ee4a1cSLaxman Dewangan 		input_event(input, EV_KEY, button->code, 1);
409d8ee4a1cSLaxman Dewangan 		input_sync(input);
410d8ee4a1cSLaxman Dewangan 
411d8ee4a1cSLaxman Dewangan 		if (!bdata->timer_debounce) {
412d8ee4a1cSLaxman Dewangan 			input_event(input, EV_KEY, button->code, 0);
413d8ee4a1cSLaxman Dewangan 			input_sync(input);
414d8ee4a1cSLaxman Dewangan 			goto out;
415d8ee4a1cSLaxman Dewangan 		}
416d8ee4a1cSLaxman Dewangan 
417d8ee4a1cSLaxman Dewangan 		bdata->key_pressed = true;
418d8ee4a1cSLaxman Dewangan 	}
419d8ee4a1cSLaxman Dewangan 
420d8ee4a1cSLaxman Dewangan 	if (bdata->timer_debounce)
421d8ee4a1cSLaxman Dewangan 		mod_timer(&bdata->timer,
422d8ee4a1cSLaxman Dewangan 			jiffies + msecs_to_jiffies(bdata->timer_debounce));
423d8ee4a1cSLaxman Dewangan out:
424d8ee4a1cSLaxman Dewangan 	spin_unlock_irqrestore(&bdata->lock, flags);
425d8ee4a1cSLaxman Dewangan 	return IRQ_HANDLED;
426d8ee4a1cSLaxman Dewangan }
427d8ee4a1cSLaxman Dewangan 
42827245519SAlexander Shiyan static void gpio_keys_quiesce_key(void *data)
42927245519SAlexander Shiyan {
43027245519SAlexander Shiyan 	struct gpio_button_data *bdata = data;
43127245519SAlexander Shiyan 
43227245519SAlexander Shiyan 	if (bdata->timer_debounce)
43327245519SAlexander Shiyan 		del_timer_sync(&bdata->timer);
43427245519SAlexander Shiyan 
43527245519SAlexander Shiyan 	cancel_work_sync(&bdata->work);
43627245519SAlexander Shiyan }
43727245519SAlexander Shiyan 
4385298cc4cSBill Pemberton static int gpio_keys_setup_key(struct platform_device *pdev,
439d9080921SDmitry Torokhov 				struct input_dev *input,
440bc8f1eafSBen Dooks 				struct gpio_button_data *bdata,
441d9080921SDmitry Torokhov 				const struct gpio_keys_button *button)
442bc8f1eafSBen Dooks {
44392a47674SAlexander Stein 	const char *desc = button->desc ? button->desc : "gpio_keys";
4449e3af04fSMika Westerberg 	struct device *dev = &pdev->dev;
445d8ee4a1cSLaxman Dewangan 	irq_handler_t isr;
4469e3af04fSMika Westerberg 	unsigned long irqflags;
44727245519SAlexander Shiyan 	int irq;
44827245519SAlexander Shiyan 	int error;
449bc8f1eafSBen Dooks 
450d9080921SDmitry Torokhov 	bdata->input = input;
451d9080921SDmitry Torokhov 	bdata->button = button;
452d8ee4a1cSLaxman Dewangan 	spin_lock_init(&bdata->lock);
453d8ee4a1cSLaxman Dewangan 
454d8ee4a1cSLaxman Dewangan 	if (gpio_is_valid(button->gpio)) {
455bc8f1eafSBen Dooks 
45627245519SAlexander Shiyan 		error = devm_gpio_request_one(&pdev->dev, button->gpio,
45727245519SAlexander Shiyan 					      GPIOF_IN, desc);
458bc8f1eafSBen Dooks 		if (error < 0) {
459d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Failed to request GPIO %d, error %d\n",
460bc8f1eafSBen Dooks 				button->gpio, error);
461d8ee4a1cSLaxman Dewangan 			return error;
462bc8f1eafSBen Dooks 		}
463bc8f1eafSBen Dooks 
46428ed684fSGrazvydas Ignotas 		if (button->debounce_interval) {
46528ed684fSGrazvydas Ignotas 			error = gpio_set_debounce(button->gpio,
46628ed684fSGrazvydas Ignotas 					button->debounce_interval * 1000);
46728ed684fSGrazvydas Ignotas 			/* use timer if gpiolib doesn't provide debounce */
46828ed684fSGrazvydas Ignotas 			if (error < 0)
469d8ee4a1cSLaxman Dewangan 				bdata->timer_debounce =
470d8ee4a1cSLaxman Dewangan 						button->debounce_interval;
47128ed684fSGrazvydas Ignotas 		}
47228ed684fSGrazvydas Ignotas 
473*97d86e07SDmitry Torokhov 		if (button->irq) {
474*97d86e07SDmitry Torokhov 			bdata->irq = button->irq;
475*97d86e07SDmitry Torokhov 		} else {
476bc8f1eafSBen Dooks 			irq = gpio_to_irq(button->gpio);
477bc8f1eafSBen Dooks 			if (irq < 0) {
478bc8f1eafSBen Dooks 				error = irq;
479d8ee4a1cSLaxman Dewangan 				dev_err(dev,
480d8ee4a1cSLaxman Dewangan 					"Unable to get irq number for GPIO %d, error %d\n",
481bc8f1eafSBen Dooks 					button->gpio, error);
48227245519SAlexander Shiyan 				return error;
483d8ee4a1cSLaxman Dewangan 			}
484d8ee4a1cSLaxman Dewangan 			bdata->irq = irq;
485*97d86e07SDmitry Torokhov 		}
486d8ee4a1cSLaxman Dewangan 
487d8ee4a1cSLaxman Dewangan 		INIT_WORK(&bdata->work, gpio_keys_gpio_work_func);
488d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
489d8ee4a1cSLaxman Dewangan 			    gpio_keys_gpio_timer, (unsigned long)bdata);
490d8ee4a1cSLaxman Dewangan 
491d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_gpio_isr;
492d8ee4a1cSLaxman Dewangan 		irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
493d8ee4a1cSLaxman Dewangan 
494d8ee4a1cSLaxman Dewangan 	} else {
495d8ee4a1cSLaxman Dewangan 		if (!button->irq) {
496d8ee4a1cSLaxman Dewangan 			dev_err(dev, "No IRQ specified\n");
497d8ee4a1cSLaxman Dewangan 			return -EINVAL;
498d8ee4a1cSLaxman Dewangan 		}
499d8ee4a1cSLaxman Dewangan 		bdata->irq = button->irq;
500d8ee4a1cSLaxman Dewangan 
501d8ee4a1cSLaxman Dewangan 		if (button->type && button->type != EV_KEY) {
502d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
503d8ee4a1cSLaxman Dewangan 			return -EINVAL;
504bc8f1eafSBen Dooks 		}
505bc8f1eafSBen Dooks 
506d8ee4a1cSLaxman Dewangan 		bdata->timer_debounce = button->debounce_interval;
507d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
508d8ee4a1cSLaxman Dewangan 			    gpio_keys_irq_timer, (unsigned long)bdata);
509d8ee4a1cSLaxman Dewangan 
510d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_irq_isr;
511d8ee4a1cSLaxman Dewangan 		irqflags = 0;
512d8ee4a1cSLaxman Dewangan 	}
513d8ee4a1cSLaxman Dewangan 
514d8ee4a1cSLaxman Dewangan 	input_set_capability(input, button->type ?: EV_KEY, button->code);
515d8ee4a1cSLaxman Dewangan 
5169e3af04fSMika Westerberg 	/*
51727245519SAlexander Shiyan 	 * Install custom action to cancel debounce timer and
51827245519SAlexander Shiyan 	 * workqueue item.
51927245519SAlexander Shiyan 	 */
52027245519SAlexander Shiyan 	error = devm_add_action(&pdev->dev, gpio_keys_quiesce_key, bdata);
52127245519SAlexander Shiyan 	if (error) {
52227245519SAlexander Shiyan 		dev_err(&pdev->dev,
52327245519SAlexander Shiyan 			"failed to register quiesce action, error: %d\n",
52427245519SAlexander Shiyan 			error);
52527245519SAlexander Shiyan 		return error;
52627245519SAlexander Shiyan 	}
52727245519SAlexander Shiyan 
52827245519SAlexander Shiyan 	/*
5299e3af04fSMika Westerberg 	 * If platform has specified that the button can be disabled,
5309e3af04fSMika Westerberg 	 * we don't want it to share the interrupt line.
5319e3af04fSMika Westerberg 	 */
5329e3af04fSMika Westerberg 	if (!button->can_disable)
5339e3af04fSMika Westerberg 		irqflags |= IRQF_SHARED;
5349e3af04fSMika Westerberg 
53527245519SAlexander Shiyan 	error = devm_request_any_context_irq(&pdev->dev, bdata->irq,
53627245519SAlexander Shiyan 					     isr, irqflags, desc, bdata);
53794a8cab8SPhilippe Langlais 	if (error < 0) {
538bc8f1eafSBen Dooks 		dev_err(dev, "Unable to claim irq %d; error %d\n",
539d8ee4a1cSLaxman Dewangan 			bdata->irq, error);
54027245519SAlexander Shiyan 		return error;
541bc8f1eafSBen Dooks 	}
542bc8f1eafSBen Dooks 
543bc8f1eafSBen Dooks 	return 0;
544bc8f1eafSBen Dooks }
545bc8f1eafSBen Dooks 
5465b76d7b4SDmitry Torokhov static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
5475b76d7b4SDmitry Torokhov {
5485b76d7b4SDmitry Torokhov 	struct input_dev *input = ddata->input;
5495b76d7b4SDmitry Torokhov 	int i;
5505b76d7b4SDmitry Torokhov 
5515b76d7b4SDmitry Torokhov 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
5525b76d7b4SDmitry Torokhov 		struct gpio_button_data *bdata = &ddata->data[i];
5535b76d7b4SDmitry Torokhov 		if (gpio_is_valid(bdata->button->gpio))
5545b76d7b4SDmitry Torokhov 			gpio_keys_gpio_report_event(bdata);
5555b76d7b4SDmitry Torokhov 	}
5565b76d7b4SDmitry Torokhov 	input_sync(input);
5575b76d7b4SDmitry Torokhov }
5585b76d7b4SDmitry Torokhov 
559173bdd74SShubhrajyoti D static int gpio_keys_open(struct input_dev *input)
560173bdd74SShubhrajyoti D {
561173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
562219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
5635b76d7b4SDmitry Torokhov 	int error;
564173bdd74SShubhrajyoti D 
5655b76d7b4SDmitry Torokhov 	if (pdata->enable) {
5665b76d7b4SDmitry Torokhov 		error = pdata->enable(input->dev.parent);
5675b76d7b4SDmitry Torokhov 		if (error)
5685b76d7b4SDmitry Torokhov 			return error;
5695b76d7b4SDmitry Torokhov 	}
5705b76d7b4SDmitry Torokhov 
5715b76d7b4SDmitry Torokhov 	/* Report current state of buttons that are connected to GPIOs */
5725b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
5735b76d7b4SDmitry Torokhov 
5745b76d7b4SDmitry Torokhov 	return 0;
575173bdd74SShubhrajyoti D }
576173bdd74SShubhrajyoti D 
577173bdd74SShubhrajyoti D static void gpio_keys_close(struct input_dev *input)
578173bdd74SShubhrajyoti D {
579173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
580219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
581173bdd74SShubhrajyoti D 
582219edc71SAlexandre Pereira da Silva 	if (pdata->disable)
583219edc71SAlexandre Pereira da Silva 		pdata->disable(input->dev.parent);
584173bdd74SShubhrajyoti D }
585173bdd74SShubhrajyoti D 
586fd05d089SDavid Jander /*
587fd05d089SDavid Jander  * Handlers for alternative sources of platform_data
588fd05d089SDavid Jander  */
589219edc71SAlexandre Pereira da Silva 
590fd05d089SDavid Jander #ifdef CONFIG_OF
591fd05d089SDavid Jander /*
592fd05d089SDavid Jander  * Translate OpenFirmware node properties into platform_data
593fd05d089SDavid Jander  */
5945298cc4cSBill Pemberton static struct gpio_keys_platform_data *
595219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
596fd05d089SDavid Jander {
597fd05d089SDavid Jander 	struct device_node *node, *pp;
598219edc71SAlexandre Pereira da Silva 	struct gpio_keys_platform_data *pdata;
599219edc71SAlexandre Pereira da Silva 	struct gpio_keys_button *button;
600219edc71SAlexandre Pereira da Silva 	int error;
601219edc71SAlexandre Pereira da Silva 	int nbuttons;
602fd05d089SDavid Jander 	int i;
603fd05d089SDavid Jander 
604fd05d089SDavid Jander 	node = dev->of_node;
6055d422f2eSAndy Shevchenko 	if (!node)
6065d422f2eSAndy Shevchenko 		return ERR_PTR(-ENODEV);
607fd05d089SDavid Jander 
608219edc71SAlexandre Pereira da Silva 	nbuttons = of_get_child_count(node);
6095d422f2eSAndy Shevchenko 	if (nbuttons == 0)
6105d422f2eSAndy Shevchenko 		return ERR_PTR(-ENODEV);
611219edc71SAlexandre Pereira da Silva 
6125d422f2eSAndy Shevchenko 	pdata = devm_kzalloc(dev,
6135d422f2eSAndy Shevchenko 			     sizeof(*pdata) + nbuttons * sizeof(*button),
614219edc71SAlexandre Pereira da Silva 			     GFP_KERNEL);
6155d422f2eSAndy Shevchenko 	if (!pdata)
6165d422f2eSAndy Shevchenko 		return ERR_PTR(-ENOMEM);
617219edc71SAlexandre Pereira da Silva 
618219edc71SAlexandre Pereira da Silva 	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
619219edc71SAlexandre Pereira da Silva 	pdata->nbuttons = nbuttons;
620fd05d089SDavid Jander 
621cca84699STobias Klauser 	pdata->rep = !!of_get_property(node, "autorepeat", NULL);
622fd05d089SDavid Jander 
623fd05d089SDavid Jander 	i = 0;
624219edc71SAlexandre Pereira da Silva 	for_each_child_of_node(node, pp) {
625fd05d089SDavid Jander 		enum of_gpio_flags flags;
626fd05d089SDavid Jander 
6271d6a0136SFabio Estevam 		button = &pdata->buttons[i++];
6281d6a0136SFabio Estevam 
629*97d86e07SDmitry Torokhov 		button->gpio = of_get_gpio_flags(pp, 0, &flags);
630*97d86e07SDmitry Torokhov 		if (button->gpio < 0) {
631*97d86e07SDmitry Torokhov 			error = button->gpio;
632*97d86e07SDmitry Torokhov 			if (error != -ENOENT) {
633e324ce61SDmitry Torokhov 				if (error != -EPROBE_DEFER)
634e324ce61SDmitry Torokhov 					dev_err(dev,
635e324ce61SDmitry Torokhov 						"Failed to get gpio flags, error: %d\n",
636e324ce61SDmitry Torokhov 						error);
6375d422f2eSAndy Shevchenko 				return ERR_PTR(error);
638e324ce61SDmitry Torokhov 			}
639*97d86e07SDmitry Torokhov 		} else {
640*97d86e07SDmitry Torokhov 			button->active_low = flags & OF_GPIO_ACTIVE_LOW;
641f2d347ffSAlexander Stein 		}
642e324ce61SDmitry Torokhov 
643*97d86e07SDmitry Torokhov 		button->irq = irq_of_parse_and_map(pp, 0);
644*97d86e07SDmitry Torokhov 
645*97d86e07SDmitry Torokhov 		if (!gpio_is_valid(button->gpio) && !button->irq) {
646*97d86e07SDmitry Torokhov 			dev_err(dev, "Found button without gpios or irqs\n");
647*97d86e07SDmitry Torokhov 			return ERR_PTR(-EINVAL);
648*97d86e07SDmitry Torokhov 		}
649fd05d089SDavid Jander 
650219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "linux,code", &button->code)) {
651219edc71SAlexandre Pereira da Silva 			dev_err(dev, "Button without keycode: 0x%x\n",
652219edc71SAlexandre Pereira da Silva 				button->gpio);
6535d422f2eSAndy Shevchenko 			return ERR_PTR(-EINVAL);
654fd05d089SDavid Jander 		}
655fd05d089SDavid Jander 
656219edc71SAlexandre Pereira da Silva 		button->desc = of_get_property(pp, "label", NULL);
657fd05d089SDavid Jander 
658219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "linux,input-type", &button->type))
659219edc71SAlexandre Pereira da Silva 			button->type = EV_KEY;
660fd05d089SDavid Jander 
661219edc71SAlexandre Pereira da Silva 		button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
662219edc71SAlexandre Pereira da Silva 
663*97d86e07SDmitry Torokhov 		button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL);
664*97d86e07SDmitry Torokhov 
665219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "debounce-interval",
666219edc71SAlexandre Pereira da Silva 					 &button->debounce_interval))
667219edc71SAlexandre Pereira da Silva 			button->debounce_interval = 5;
668219edc71SAlexandre Pereira da Silva 	}
669219edc71SAlexandre Pereira da Silva 
6705d422f2eSAndy Shevchenko 	if (pdata->nbuttons == 0)
6715d422f2eSAndy Shevchenko 		return ERR_PTR(-EINVAL);
672219edc71SAlexandre Pereira da Silva 
673219edc71SAlexandre Pereira da Silva 	return pdata;
674fd05d089SDavid Jander }
675fd05d089SDavid Jander 
67622daae31SJingoo Han static const struct of_device_id gpio_keys_of_match[] = {
677fd05d089SDavid Jander 	{ .compatible = "gpio-keys", },
678fd05d089SDavid Jander 	{ },
679fd05d089SDavid Jander };
680fd05d089SDavid Jander MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
681fd05d089SDavid Jander 
682fd05d089SDavid Jander #else
683fd05d089SDavid Jander 
684219edc71SAlexandre Pereira da Silva static inline struct gpio_keys_platform_data *
685219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
686fd05d089SDavid Jander {
687219edc71SAlexandre Pereira da Silva 	return ERR_PTR(-ENODEV);
688fd05d089SDavid Jander }
689fd05d089SDavid Jander 
690fd05d089SDavid Jander #endif
691fd05d089SDavid Jander 
6925298cc4cSBill Pemberton static int gpio_keys_probe(struct platform_device *pdev)
69378a56aabSPhil Blundell {
694db19fd8bSBen Dooks 	struct device *dev = &pdev->dev;
695219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
696219edc71SAlexandre Pereira da Silva 	struct gpio_keys_drvdata *ddata;
69778a56aabSPhil Blundell 	struct input_dev *input;
6985d422f2eSAndy Shevchenko 	size_t size;
69978a56aabSPhil Blundell 	int i, error;
700e15b0213SAnti Sullin 	int wakeup = 0;
70178a56aabSPhil Blundell 
702fd05d089SDavid Jander 	if (!pdata) {
703219edc71SAlexandre Pereira da Silva 		pdata = gpio_keys_get_devtree_pdata(dev);
704219edc71SAlexandre Pereira da Silva 		if (IS_ERR(pdata))
705219edc71SAlexandre Pereira da Silva 			return PTR_ERR(pdata);
706fd05d089SDavid Jander 	}
707fd05d089SDavid Jander 
7085d422f2eSAndy Shevchenko 	size = sizeof(struct gpio_keys_drvdata) +
7095d422f2eSAndy Shevchenko 			pdata->nbuttons * sizeof(struct gpio_button_data);
7105d422f2eSAndy Shevchenko 	ddata = devm_kzalloc(dev, size, GFP_KERNEL);
7115d422f2eSAndy Shevchenko 	if (!ddata) {
712db19fd8bSBen Dooks 		dev_err(dev, "failed to allocate state\n");
7135d422f2eSAndy Shevchenko 		return -ENOMEM;
7145d422f2eSAndy Shevchenko 	}
7155d422f2eSAndy Shevchenko 
7165d422f2eSAndy Shevchenko 	input = devm_input_allocate_device(dev);
7175d422f2eSAndy Shevchenko 	if (!input) {
7185d422f2eSAndy Shevchenko 		dev_err(dev, "failed to allocate input device\n");
7195d422f2eSAndy Shevchenko 		return -ENOMEM;
720a33466e3SDmitry Baryshkov 	}
72178a56aabSPhil Blundell 
722219edc71SAlexandre Pereira da Silva 	ddata->pdata = pdata;
7239e3af04fSMika Westerberg 	ddata->input = input;
7249e3af04fSMika Westerberg 	mutex_init(&ddata->disable_lock);
7259e3af04fSMika Westerberg 
726a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
727173bdd74SShubhrajyoti D 	input_set_drvdata(input, ddata);
72878a56aabSPhil Blundell 
72946711277SAlexander Stein 	input->name = pdata->name ? : pdev->name;
73078a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
731469ba4dfSDmitry Torokhov 	input->dev.parent = &pdev->dev;
732173bdd74SShubhrajyoti D 	input->open = gpio_keys_open;
733173bdd74SShubhrajyoti D 	input->close = gpio_keys_close;
73478a56aabSPhil Blundell 
73578a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
73678a56aabSPhil Blundell 	input->id.vendor = 0x0001;
73778a56aabSPhil Blundell 	input->id.product = 0x0001;
73878a56aabSPhil Blundell 	input->id.version = 0x0100;
73978a56aabSPhil Blundell 
740b67b4b11SDominic Curran 	/* Enable auto repeat feature of Linux input subsystem */
741b67b4b11SDominic Curran 	if (pdata->rep)
742b67b4b11SDominic Curran 		__set_bit(EV_REP, input->evbit);
743b67b4b11SDominic Curran 
74478a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
745d9080921SDmitry Torokhov 		const struct gpio_keys_button *button = &pdata->buttons[i];
746a33466e3SDmitry Baryshkov 		struct gpio_button_data *bdata = &ddata->data[i];
74778a56aabSPhil Blundell 
748d9080921SDmitry Torokhov 		error = gpio_keys_setup_key(pdev, input, bdata, button);
749bc8f1eafSBen Dooks 		if (error)
75027245519SAlexander Shiyan 			return error;
75184767d00SRoman Moravcik 
752e15b0213SAnti Sullin 		if (button->wakeup)
753e15b0213SAnti Sullin 			wakeup = 1;
75478a56aabSPhil Blundell 	}
75578a56aabSPhil Blundell 
7569e3af04fSMika Westerberg 	error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7579e3af04fSMika Westerberg 	if (error) {
7589e3af04fSMika Westerberg 		dev_err(dev, "Unable to export keys/switches, error: %d\n",
7599e3af04fSMika Westerberg 			error);
76027245519SAlexander Shiyan 		return error;
7619e3af04fSMika Westerberg 	}
7629e3af04fSMika Westerberg 
76378a56aabSPhil Blundell 	error = input_register_device(input);
76478a56aabSPhil Blundell 	if (error) {
7659e3af04fSMika Westerberg 		dev_err(dev, "Unable to register input device, error: %d\n",
7669e3af04fSMika Westerberg 			error);
76727245519SAlexander Shiyan 		goto err_remove_group;
76878a56aabSPhil Blundell 	}
76978a56aabSPhil Blundell 
770e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, wakeup);
771e15b0213SAnti Sullin 
77278a56aabSPhil Blundell 	return 0;
77378a56aabSPhil Blundell 
77427245519SAlexander Shiyan err_remove_group:
7759e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
77678a56aabSPhil Blundell 	return error;
77778a56aabSPhil Blundell }
77878a56aabSPhil Blundell 
779e2619cf7SBill Pemberton static int gpio_keys_remove(struct platform_device *pdev)
78078a56aabSPhil Blundell {
7819e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7829e3af04fSMika Westerberg 
783e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, 0);
784e15b0213SAnti Sullin 
78578a56aabSPhil Blundell 	return 0;
78678a56aabSPhil Blundell }
78778a56aabSPhil Blundell 
788bdda8216SDmitry Torokhov #ifdef CONFIG_PM_SLEEP
789ae78e0e0SMike Rapoport static int gpio_keys_suspend(struct device *dev)
790e15b0213SAnti Sullin {
791fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
792dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
793e15b0213SAnti Sullin 	int i;
794e15b0213SAnti Sullin 
795fd05d089SDavid Jander 	if (device_may_wakeup(dev)) {
796219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
797d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
798d8ee4a1cSLaxman Dewangan 			if (bdata->button->wakeup)
799d8ee4a1cSLaxman Dewangan 				enable_irq_wake(bdata->irq);
800e15b0213SAnti Sullin 		}
801dda19a92SJonas Aaberg 	} else {
802dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
803dda19a92SJonas Aaberg 		if (input->users)
804dda19a92SJonas Aaberg 			gpio_keys_close(input);
805dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
806e15b0213SAnti Sullin 	}
807e15b0213SAnti Sullin 
808e15b0213SAnti Sullin 	return 0;
809e15b0213SAnti Sullin }
810e15b0213SAnti Sullin 
811ae78e0e0SMike Rapoport static int gpio_keys_resume(struct device *dev)
812e15b0213SAnti Sullin {
813fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
814dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
815dda19a92SJonas Aaberg 	int error = 0;
816e15b0213SAnti Sullin 	int i;
817e15b0213SAnti Sullin 
818dda19a92SJonas Aaberg 	if (device_may_wakeup(dev)) {
819219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
820d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
821dda19a92SJonas Aaberg 			if (bdata->button->wakeup)
822d8ee4a1cSLaxman Dewangan 				disable_irq_wake(bdata->irq);
823e15b0213SAnti Sullin 		}
824dda19a92SJonas Aaberg 	} else {
825dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
826dda19a92SJonas Aaberg 		if (input->users)
827dda19a92SJonas Aaberg 			error = gpio_keys_open(input);
828dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
829dda19a92SJonas Aaberg 	}
830dda19a92SJonas Aaberg 
831dda19a92SJonas Aaberg 	if (error)
832dda19a92SJonas Aaberg 		return error;
8335b76d7b4SDmitry Torokhov 
8345b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
835e15b0213SAnti Sullin 	return 0;
836e15b0213SAnti Sullin }
837e15b0213SAnti Sullin #endif
838e15b0213SAnti Sullin 
839bdda8216SDmitry Torokhov static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
840bdda8216SDmitry Torokhov 
8419b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = {
84278a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
8431cb0aa88SBill Pemberton 	.remove		= gpio_keys_remove,
84478a56aabSPhil Blundell 	.driver		= {
84578a56aabSPhil Blundell 		.name	= "gpio-keys",
846d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
847ae78e0e0SMike Rapoport 		.pm	= &gpio_keys_pm_ops,
848219edc71SAlexandre Pereira da Silva 		.of_match_table = of_match_ptr(gpio_keys_of_match),
84978a56aabSPhil Blundell 	}
85078a56aabSPhil Blundell };
85178a56aabSPhil Blundell 
85278a56aabSPhil Blundell static int __init gpio_keys_init(void)
85378a56aabSPhil Blundell {
85478a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
85578a56aabSPhil Blundell }
85678a56aabSPhil Blundell 
85778a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
85878a56aabSPhil Blundell {
85978a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
86078a56aabSPhil Blundell }
86178a56aabSPhil Blundell 
862b2330205SDavid Jander late_initcall(gpio_keys_init);
86378a56aabSPhil Blundell module_exit(gpio_keys_exit);
86478a56aabSPhil Blundell 
86578a56aabSPhil Blundell MODULE_LICENSE("GPL");
86678a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
8677e2ecdf4SDavid Jander MODULE_DESCRIPTION("Keyboard driver for GPIOs");
868d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
869