xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision f2d347ff70be453e861304448cb2f32ff94d40e9)
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>
32*f2d347ffSAlexander 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 
473bc8f1eafSBen Dooks 		irq = gpio_to_irq(button->gpio);
474bc8f1eafSBen Dooks 		if (irq < 0) {
475bc8f1eafSBen Dooks 			error = irq;
476d8ee4a1cSLaxman Dewangan 			dev_err(dev,
477d8ee4a1cSLaxman Dewangan 				"Unable to get irq number for GPIO %d, error %d\n",
478bc8f1eafSBen Dooks 				button->gpio, error);
47927245519SAlexander Shiyan 			return error;
480d8ee4a1cSLaxman Dewangan 		}
481d8ee4a1cSLaxman Dewangan 		bdata->irq = irq;
482d8ee4a1cSLaxman Dewangan 
483d8ee4a1cSLaxman Dewangan 		INIT_WORK(&bdata->work, gpio_keys_gpio_work_func);
484d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
485d8ee4a1cSLaxman Dewangan 			    gpio_keys_gpio_timer, (unsigned long)bdata);
486d8ee4a1cSLaxman Dewangan 
487d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_gpio_isr;
488d8ee4a1cSLaxman Dewangan 		irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
489d8ee4a1cSLaxman Dewangan 
490d8ee4a1cSLaxman Dewangan 	} else {
491d8ee4a1cSLaxman Dewangan 		if (!button->irq) {
492d8ee4a1cSLaxman Dewangan 			dev_err(dev, "No IRQ specified\n");
493d8ee4a1cSLaxman Dewangan 			return -EINVAL;
494d8ee4a1cSLaxman Dewangan 		}
495d8ee4a1cSLaxman Dewangan 		bdata->irq = button->irq;
496d8ee4a1cSLaxman Dewangan 
497d8ee4a1cSLaxman Dewangan 		if (button->type && button->type != EV_KEY) {
498d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
499d8ee4a1cSLaxman Dewangan 			return -EINVAL;
500bc8f1eafSBen Dooks 		}
501bc8f1eafSBen Dooks 
502d8ee4a1cSLaxman Dewangan 		bdata->timer_debounce = button->debounce_interval;
503d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
504d8ee4a1cSLaxman Dewangan 			    gpio_keys_irq_timer, (unsigned long)bdata);
505d8ee4a1cSLaxman Dewangan 
506d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_irq_isr;
507d8ee4a1cSLaxman Dewangan 		irqflags = 0;
508d8ee4a1cSLaxman Dewangan 	}
509d8ee4a1cSLaxman Dewangan 
510d8ee4a1cSLaxman Dewangan 	input_set_capability(input, button->type ?: EV_KEY, button->code);
511d8ee4a1cSLaxman Dewangan 
5129e3af04fSMika Westerberg 	/*
51327245519SAlexander Shiyan 	 * Install custom action to cancel debounce timer and
51427245519SAlexander Shiyan 	 * workqueue item.
51527245519SAlexander Shiyan 	 */
51627245519SAlexander Shiyan 	error = devm_add_action(&pdev->dev, gpio_keys_quiesce_key, bdata);
51727245519SAlexander Shiyan 	if (error) {
51827245519SAlexander Shiyan 		dev_err(&pdev->dev,
51927245519SAlexander Shiyan 			"failed to register quiesce action, error: %d\n",
52027245519SAlexander Shiyan 			error);
52127245519SAlexander Shiyan 		return error;
52227245519SAlexander Shiyan 	}
52327245519SAlexander Shiyan 
52427245519SAlexander Shiyan 	/*
5259e3af04fSMika Westerberg 	 * If platform has specified that the button can be disabled,
5269e3af04fSMika Westerberg 	 * we don't want it to share the interrupt line.
5279e3af04fSMika Westerberg 	 */
5289e3af04fSMika Westerberg 	if (!button->can_disable)
5299e3af04fSMika Westerberg 		irqflags |= IRQF_SHARED;
5309e3af04fSMika Westerberg 
53127245519SAlexander Shiyan 	error = devm_request_any_context_irq(&pdev->dev, bdata->irq,
53227245519SAlexander Shiyan 					     isr, irqflags, desc, bdata);
53394a8cab8SPhilippe Langlais 	if (error < 0) {
534bc8f1eafSBen Dooks 		dev_err(dev, "Unable to claim irq %d; error %d\n",
535d8ee4a1cSLaxman Dewangan 			bdata->irq, error);
53627245519SAlexander Shiyan 		return error;
537bc8f1eafSBen Dooks 	}
538bc8f1eafSBen Dooks 
539bc8f1eafSBen Dooks 	return 0;
540bc8f1eafSBen Dooks }
541bc8f1eafSBen Dooks 
5425b76d7b4SDmitry Torokhov static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
5435b76d7b4SDmitry Torokhov {
5445b76d7b4SDmitry Torokhov 	struct input_dev *input = ddata->input;
5455b76d7b4SDmitry Torokhov 	int i;
5465b76d7b4SDmitry Torokhov 
5475b76d7b4SDmitry Torokhov 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
5485b76d7b4SDmitry Torokhov 		struct gpio_button_data *bdata = &ddata->data[i];
5495b76d7b4SDmitry Torokhov 		if (gpio_is_valid(bdata->button->gpio))
5505b76d7b4SDmitry Torokhov 			gpio_keys_gpio_report_event(bdata);
5515b76d7b4SDmitry Torokhov 	}
5525b76d7b4SDmitry Torokhov 	input_sync(input);
5535b76d7b4SDmitry Torokhov }
5545b76d7b4SDmitry Torokhov 
555173bdd74SShubhrajyoti D static int gpio_keys_open(struct input_dev *input)
556173bdd74SShubhrajyoti D {
557173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
558219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
5595b76d7b4SDmitry Torokhov 	int error;
560173bdd74SShubhrajyoti D 
5615b76d7b4SDmitry Torokhov 	if (pdata->enable) {
5625b76d7b4SDmitry Torokhov 		error = pdata->enable(input->dev.parent);
5635b76d7b4SDmitry Torokhov 		if (error)
5645b76d7b4SDmitry Torokhov 			return error;
5655b76d7b4SDmitry Torokhov 	}
5665b76d7b4SDmitry Torokhov 
5675b76d7b4SDmitry Torokhov 	/* Report current state of buttons that are connected to GPIOs */
5685b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
5695b76d7b4SDmitry Torokhov 
5705b76d7b4SDmitry Torokhov 	return 0;
571173bdd74SShubhrajyoti D }
572173bdd74SShubhrajyoti D 
573173bdd74SShubhrajyoti D static void gpio_keys_close(struct input_dev *input)
574173bdd74SShubhrajyoti D {
575173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
576219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
577173bdd74SShubhrajyoti D 
578219edc71SAlexandre Pereira da Silva 	if (pdata->disable)
579219edc71SAlexandre Pereira da Silva 		pdata->disable(input->dev.parent);
580173bdd74SShubhrajyoti D }
581173bdd74SShubhrajyoti D 
582fd05d089SDavid Jander /*
583fd05d089SDavid Jander  * Handlers for alternative sources of platform_data
584fd05d089SDavid Jander  */
585219edc71SAlexandre Pereira da Silva 
586fd05d089SDavid Jander #ifdef CONFIG_OF
587fd05d089SDavid Jander /*
588fd05d089SDavid Jander  * Translate OpenFirmware node properties into platform_data
589fd05d089SDavid Jander  */
5905298cc4cSBill Pemberton static struct gpio_keys_platform_data *
591219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
592fd05d089SDavid Jander {
593fd05d089SDavid Jander 	struct device_node *node, *pp;
594219edc71SAlexandre Pereira da Silva 	struct gpio_keys_platform_data *pdata;
595219edc71SAlexandre Pereira da Silva 	struct gpio_keys_button *button;
596219edc71SAlexandre Pereira da Silva 	int error;
597219edc71SAlexandre Pereira da Silva 	int nbuttons;
598fd05d089SDavid Jander 	int i;
599fd05d089SDavid Jander 
600fd05d089SDavid Jander 	node = dev->of_node;
6015d422f2eSAndy Shevchenko 	if (!node)
6025d422f2eSAndy Shevchenko 		return ERR_PTR(-ENODEV);
603fd05d089SDavid Jander 
604219edc71SAlexandre Pereira da Silva 	nbuttons = of_get_child_count(node);
6055d422f2eSAndy Shevchenko 	if (nbuttons == 0)
6065d422f2eSAndy Shevchenko 		return ERR_PTR(-ENODEV);
607219edc71SAlexandre Pereira da Silva 
6085d422f2eSAndy Shevchenko 	pdata = devm_kzalloc(dev,
6095d422f2eSAndy Shevchenko 			     sizeof(*pdata) + nbuttons * sizeof(*button),
610219edc71SAlexandre Pereira da Silva 			     GFP_KERNEL);
6115d422f2eSAndy Shevchenko 	if (!pdata)
6125d422f2eSAndy Shevchenko 		return ERR_PTR(-ENOMEM);
613219edc71SAlexandre Pereira da Silva 
614219edc71SAlexandre Pereira da Silva 	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
615219edc71SAlexandre Pereira da Silva 	pdata->nbuttons = nbuttons;
616fd05d089SDavid Jander 
617cca84699STobias Klauser 	pdata->rep = !!of_get_property(node, "autorepeat", NULL);
618fd05d089SDavid Jander 
619fd05d089SDavid Jander 	i = 0;
620219edc71SAlexandre Pereira da Silva 	for_each_child_of_node(node, pp) {
621*f2d347ffSAlexander Stein 		int gpio = -1;
622*f2d347ffSAlexander Stein 		int irq;
623fd05d089SDavid Jander 		enum of_gpio_flags flags;
624fd05d089SDavid Jander 
625fd05d089SDavid Jander 		if (!of_find_property(pp, "gpios", NULL)) {
626*f2d347ffSAlexander Stein 			irq = irq_of_parse_and_map(pp, 0);
627*f2d347ffSAlexander Stein 			if (irq == 0) {
628fd05d089SDavid Jander 				pdata->nbuttons--;
629*f2d347ffSAlexander Stein 				dev_warn(dev, "Found button without gpios or irqs\n");
630fd05d089SDavid Jander 				continue;
631fd05d089SDavid Jander 			}
632*f2d347ffSAlexander Stein 		} else {
633e324ce61SDmitry Torokhov 			gpio = of_get_gpio_flags(pp, 0, &flags);
634e324ce61SDmitry Torokhov 			if (gpio < 0) {
635e324ce61SDmitry Torokhov 				error = gpio;
636e324ce61SDmitry Torokhov 				if (error != -EPROBE_DEFER)
637e324ce61SDmitry Torokhov 					dev_err(dev,
638e324ce61SDmitry Torokhov 						"Failed to get gpio flags, error: %d\n",
639e324ce61SDmitry Torokhov 						error);
6405d422f2eSAndy Shevchenko 				return ERR_PTR(error);
641e324ce61SDmitry Torokhov 			}
642*f2d347ffSAlexander Stein 		}
643e324ce61SDmitry Torokhov 
644219edc71SAlexandre Pereira da Silva 		button = &pdata->buttons[i++];
645fd05d089SDavid Jander 
646e324ce61SDmitry Torokhov 		button->gpio = gpio;
647*f2d347ffSAlexander Stein 		button->irq = irq;
648219edc71SAlexandre Pereira da Silva 		button->active_low = flags & OF_GPIO_ACTIVE_LOW;
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 
663219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "debounce-interval",
664219edc71SAlexandre Pereira da Silva 					 &button->debounce_interval))
665219edc71SAlexandre Pereira da Silva 			button->debounce_interval = 5;
666219edc71SAlexandre Pereira da Silva 	}
667219edc71SAlexandre Pereira da Silva 
6685d422f2eSAndy Shevchenko 	if (pdata->nbuttons == 0)
6695d422f2eSAndy Shevchenko 		return ERR_PTR(-EINVAL);
670219edc71SAlexandre Pereira da Silva 
671219edc71SAlexandre Pereira da Silva 	return pdata;
672fd05d089SDavid Jander }
673fd05d089SDavid Jander 
67422daae31SJingoo Han static const struct of_device_id gpio_keys_of_match[] = {
675fd05d089SDavid Jander 	{ .compatible = "gpio-keys", },
676fd05d089SDavid Jander 	{ },
677fd05d089SDavid Jander };
678fd05d089SDavid Jander MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
679fd05d089SDavid Jander 
680fd05d089SDavid Jander #else
681fd05d089SDavid Jander 
682219edc71SAlexandre Pereira da Silva static inline struct gpio_keys_platform_data *
683219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
684fd05d089SDavid Jander {
685219edc71SAlexandre Pereira da Silva 	return ERR_PTR(-ENODEV);
686fd05d089SDavid Jander }
687fd05d089SDavid Jander 
688fd05d089SDavid Jander #endif
689fd05d089SDavid Jander 
6905298cc4cSBill Pemberton static int gpio_keys_probe(struct platform_device *pdev)
69178a56aabSPhil Blundell {
692db19fd8bSBen Dooks 	struct device *dev = &pdev->dev;
693219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
694219edc71SAlexandre Pereira da Silva 	struct gpio_keys_drvdata *ddata;
69578a56aabSPhil Blundell 	struct input_dev *input;
6965d422f2eSAndy Shevchenko 	size_t size;
69778a56aabSPhil Blundell 	int i, error;
698e15b0213SAnti Sullin 	int wakeup = 0;
69978a56aabSPhil Blundell 
700fd05d089SDavid Jander 	if (!pdata) {
701219edc71SAlexandre Pereira da Silva 		pdata = gpio_keys_get_devtree_pdata(dev);
702219edc71SAlexandre Pereira da Silva 		if (IS_ERR(pdata))
703219edc71SAlexandre Pereira da Silva 			return PTR_ERR(pdata);
704fd05d089SDavid Jander 	}
705fd05d089SDavid Jander 
7065d422f2eSAndy Shevchenko 	size = sizeof(struct gpio_keys_drvdata) +
7075d422f2eSAndy Shevchenko 			pdata->nbuttons * sizeof(struct gpio_button_data);
7085d422f2eSAndy Shevchenko 	ddata = devm_kzalloc(dev, size, GFP_KERNEL);
7095d422f2eSAndy Shevchenko 	if (!ddata) {
710db19fd8bSBen Dooks 		dev_err(dev, "failed to allocate state\n");
7115d422f2eSAndy Shevchenko 		return -ENOMEM;
7125d422f2eSAndy Shevchenko 	}
7135d422f2eSAndy Shevchenko 
7145d422f2eSAndy Shevchenko 	input = devm_input_allocate_device(dev);
7155d422f2eSAndy Shevchenko 	if (!input) {
7165d422f2eSAndy Shevchenko 		dev_err(dev, "failed to allocate input device\n");
7175d422f2eSAndy Shevchenko 		return -ENOMEM;
718a33466e3SDmitry Baryshkov 	}
71978a56aabSPhil Blundell 
720219edc71SAlexandre Pereira da Silva 	ddata->pdata = pdata;
7219e3af04fSMika Westerberg 	ddata->input = input;
7229e3af04fSMika Westerberg 	mutex_init(&ddata->disable_lock);
7239e3af04fSMika Westerberg 
724a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
725173bdd74SShubhrajyoti D 	input_set_drvdata(input, ddata);
72678a56aabSPhil Blundell 
72746711277SAlexander Stein 	input->name = pdata->name ? : pdev->name;
72878a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
729469ba4dfSDmitry Torokhov 	input->dev.parent = &pdev->dev;
730173bdd74SShubhrajyoti D 	input->open = gpio_keys_open;
731173bdd74SShubhrajyoti D 	input->close = gpio_keys_close;
73278a56aabSPhil Blundell 
73378a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
73478a56aabSPhil Blundell 	input->id.vendor = 0x0001;
73578a56aabSPhil Blundell 	input->id.product = 0x0001;
73678a56aabSPhil Blundell 	input->id.version = 0x0100;
73778a56aabSPhil Blundell 
738b67b4b11SDominic Curran 	/* Enable auto repeat feature of Linux input subsystem */
739b67b4b11SDominic Curran 	if (pdata->rep)
740b67b4b11SDominic Curran 		__set_bit(EV_REP, input->evbit);
741b67b4b11SDominic Curran 
74278a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
743d9080921SDmitry Torokhov 		const struct gpio_keys_button *button = &pdata->buttons[i];
744a33466e3SDmitry Baryshkov 		struct gpio_button_data *bdata = &ddata->data[i];
74578a56aabSPhil Blundell 
746d9080921SDmitry Torokhov 		error = gpio_keys_setup_key(pdev, input, bdata, button);
747bc8f1eafSBen Dooks 		if (error)
74827245519SAlexander Shiyan 			return error;
74984767d00SRoman Moravcik 
750e15b0213SAnti Sullin 		if (button->wakeup)
751e15b0213SAnti Sullin 			wakeup = 1;
75278a56aabSPhil Blundell 	}
75378a56aabSPhil Blundell 
7549e3af04fSMika Westerberg 	error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7559e3af04fSMika Westerberg 	if (error) {
7569e3af04fSMika Westerberg 		dev_err(dev, "Unable to export keys/switches, error: %d\n",
7579e3af04fSMika Westerberg 			error);
75827245519SAlexander Shiyan 		return error;
7599e3af04fSMika Westerberg 	}
7609e3af04fSMika Westerberg 
76178a56aabSPhil Blundell 	error = input_register_device(input);
76278a56aabSPhil Blundell 	if (error) {
7639e3af04fSMika Westerberg 		dev_err(dev, "Unable to register input device, error: %d\n",
7649e3af04fSMika Westerberg 			error);
76527245519SAlexander Shiyan 		goto err_remove_group;
76678a56aabSPhil Blundell 	}
76778a56aabSPhil Blundell 
768e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, wakeup);
769e15b0213SAnti Sullin 
77078a56aabSPhil Blundell 	return 0;
77178a56aabSPhil Blundell 
77227245519SAlexander Shiyan err_remove_group:
7739e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
77478a56aabSPhil Blundell 	return error;
77578a56aabSPhil Blundell }
77678a56aabSPhil Blundell 
777e2619cf7SBill Pemberton static int gpio_keys_remove(struct platform_device *pdev)
77878a56aabSPhil Blundell {
7799e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7809e3af04fSMika Westerberg 
781e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, 0);
782e15b0213SAnti Sullin 
78378a56aabSPhil Blundell 	return 0;
78478a56aabSPhil Blundell }
78578a56aabSPhil Blundell 
786bdda8216SDmitry Torokhov #ifdef CONFIG_PM_SLEEP
787ae78e0e0SMike Rapoport static int gpio_keys_suspend(struct device *dev)
788e15b0213SAnti Sullin {
789fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
790dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
791e15b0213SAnti Sullin 	int i;
792e15b0213SAnti Sullin 
793fd05d089SDavid Jander 	if (device_may_wakeup(dev)) {
794219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
795d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
796d8ee4a1cSLaxman Dewangan 			if (bdata->button->wakeup)
797d8ee4a1cSLaxman Dewangan 				enable_irq_wake(bdata->irq);
798e15b0213SAnti Sullin 		}
799dda19a92SJonas Aaberg 	} else {
800dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
801dda19a92SJonas Aaberg 		if (input->users)
802dda19a92SJonas Aaberg 			gpio_keys_close(input);
803dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
804e15b0213SAnti Sullin 	}
805e15b0213SAnti Sullin 
806e15b0213SAnti Sullin 	return 0;
807e15b0213SAnti Sullin }
808e15b0213SAnti Sullin 
809ae78e0e0SMike Rapoport static int gpio_keys_resume(struct device *dev)
810e15b0213SAnti Sullin {
811fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
812dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
813dda19a92SJonas Aaberg 	int error = 0;
814e15b0213SAnti Sullin 	int i;
815e15b0213SAnti Sullin 
816dda19a92SJonas Aaberg 	if (device_may_wakeup(dev)) {
817219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
818d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
819dda19a92SJonas Aaberg 			if (bdata->button->wakeup)
820d8ee4a1cSLaxman Dewangan 				disable_irq_wake(bdata->irq);
821e15b0213SAnti Sullin 		}
822dda19a92SJonas Aaberg 	} else {
823dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
824dda19a92SJonas Aaberg 		if (input->users)
825dda19a92SJonas Aaberg 			error = gpio_keys_open(input);
826dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
827dda19a92SJonas Aaberg 	}
828dda19a92SJonas Aaberg 
829dda19a92SJonas Aaberg 	if (error)
830dda19a92SJonas Aaberg 		return error;
8315b76d7b4SDmitry Torokhov 
8325b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
833e15b0213SAnti Sullin 	return 0;
834e15b0213SAnti Sullin }
835e15b0213SAnti Sullin #endif
836e15b0213SAnti Sullin 
837bdda8216SDmitry Torokhov static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
838bdda8216SDmitry Torokhov 
8399b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = {
84078a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
8411cb0aa88SBill Pemberton 	.remove		= gpio_keys_remove,
84278a56aabSPhil Blundell 	.driver		= {
84378a56aabSPhil Blundell 		.name	= "gpio-keys",
844d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
845ae78e0e0SMike Rapoport 		.pm	= &gpio_keys_pm_ops,
846219edc71SAlexandre Pereira da Silva 		.of_match_table = of_match_ptr(gpio_keys_of_match),
84778a56aabSPhil Blundell 	}
84878a56aabSPhil Blundell };
84978a56aabSPhil Blundell 
85078a56aabSPhil Blundell static int __init gpio_keys_init(void)
85178a56aabSPhil Blundell {
85278a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
85378a56aabSPhil Blundell }
85478a56aabSPhil Blundell 
85578a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
85678a56aabSPhil Blundell {
85778a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
85878a56aabSPhil Blundell }
85978a56aabSPhil Blundell 
860b2330205SDavid Jander late_initcall(gpio_keys_init);
86178a56aabSPhil Blundell module_exit(gpio_keys_exit);
86278a56aabSPhil Blundell 
86378a56aabSPhil Blundell MODULE_LICENSE("GPL");
86478a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
8657e2ecdf4SDavid Jander MODULE_DESCRIPTION("Keyboard driver for GPIOs");
866d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
867