xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision e324ce61ef483dd26d03502d35666ad48a2e1b33)
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>
29fd05d089SDavid Jander #include <linux/of_platform.h>
30fd05d089SDavid Jander #include <linux/of_gpio.h>
31d8ee4a1cSLaxman Dewangan #include <linux/spinlock.h>
3278a56aabSPhil Blundell 
33a33466e3SDmitry Baryshkov struct gpio_button_data {
34d9080921SDmitry Torokhov 	const struct gpio_keys_button *button;
35a33466e3SDmitry Baryshkov 	struct input_dev *input;
36ca865a77SJani Nikula 	struct timer_list timer;
37da0d03feSJani Nikula 	struct work_struct work;
38d8ee4a1cSLaxman Dewangan 	unsigned int timer_debounce;	/* in msecs */
39d8ee4a1cSLaxman Dewangan 	unsigned int irq;
40d8ee4a1cSLaxman Dewangan 	spinlock_t lock;
419e3af04fSMika Westerberg 	bool disabled;
42d8ee4a1cSLaxman Dewangan 	bool key_pressed;
43a33466e3SDmitry Baryshkov };
44a33466e3SDmitry Baryshkov 
45a33466e3SDmitry Baryshkov struct gpio_keys_drvdata {
46219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata;
47a33466e3SDmitry Baryshkov 	struct input_dev *input;
489e3af04fSMika Westerberg 	struct mutex disable_lock;
49a33466e3SDmitry Baryshkov 	struct gpio_button_data data[0];
50a33466e3SDmitry Baryshkov };
51a33466e3SDmitry Baryshkov 
529e3af04fSMika Westerberg /*
539e3af04fSMika Westerberg  * SYSFS interface for enabling/disabling keys and switches:
549e3af04fSMika Westerberg  *
559e3af04fSMika Westerberg  * There are 4 attributes under /sys/devices/platform/gpio-keys/
569e3af04fSMika Westerberg  *	keys [ro]              - bitmap of keys (EV_KEY) which can be
579e3af04fSMika Westerberg  *	                         disabled
589e3af04fSMika Westerberg  *	switches [ro]          - bitmap of switches (EV_SW) which can be
599e3af04fSMika Westerberg  *	                         disabled
609e3af04fSMika Westerberg  *	disabled_keys [rw]     - bitmap of keys currently disabled
619e3af04fSMika Westerberg  *	disabled_switches [rw] - bitmap of switches currently disabled
629e3af04fSMika Westerberg  *
639e3af04fSMika Westerberg  * Userland can change these values and hence disable event generation
649e3af04fSMika Westerberg  * for each key (or switch). Disabling a key means its interrupt line
659e3af04fSMika Westerberg  * is disabled.
669e3af04fSMika Westerberg  *
679e3af04fSMika Westerberg  * For example, if we have following switches set up as gpio-keys:
689e3af04fSMika Westerberg  *	SW_DOCK = 5
699e3af04fSMika Westerberg  *	SW_CAMERA_LENS_COVER = 9
709e3af04fSMika Westerberg  *	SW_KEYPAD_SLIDE = 10
719e3af04fSMika Westerberg  *	SW_FRONT_PROXIMITY = 11
729e3af04fSMika Westerberg  * This is read from switches:
739e3af04fSMika Westerberg  *	11-9,5
749e3af04fSMika Westerberg  * Next we want to disable proximity (11) and dock (5), we write:
759e3af04fSMika Westerberg  *	11,5
769e3af04fSMika Westerberg  * to file disabled_switches. Now proximity and dock IRQs are disabled.
779e3af04fSMika Westerberg  * This can be verified by reading the file disabled_switches:
789e3af04fSMika Westerberg  *	11,5
799e3af04fSMika Westerberg  * If we now want to enable proximity (11) switch we write:
809e3af04fSMika Westerberg  *	5
819e3af04fSMika Westerberg  * to disabled_switches.
829e3af04fSMika Westerberg  *
839e3af04fSMika Westerberg  * We can disable only those keys which don't allow sharing the irq.
849e3af04fSMika Westerberg  */
859e3af04fSMika Westerberg 
869e3af04fSMika Westerberg /**
879e3af04fSMika Westerberg  * get_n_events_by_type() - returns maximum number of events per @type
889e3af04fSMika Westerberg  * @type: type of button (%EV_KEY, %EV_SW)
899e3af04fSMika Westerberg  *
909e3af04fSMika Westerberg  * Return value of this function can be used to allocate bitmap
919e3af04fSMika Westerberg  * large enough to hold all bits for given type.
929e3af04fSMika Westerberg  */
939e3af04fSMika Westerberg static inline int get_n_events_by_type(int type)
949e3af04fSMika Westerberg {
959e3af04fSMika Westerberg 	BUG_ON(type != EV_SW && type != EV_KEY);
969e3af04fSMika Westerberg 
979e3af04fSMika Westerberg 	return (type == EV_KEY) ? KEY_CNT : SW_CNT;
989e3af04fSMika Westerberg }
999e3af04fSMika Westerberg 
1009e3af04fSMika Westerberg /**
1019e3af04fSMika Westerberg  * gpio_keys_disable_button() - disables given GPIO button
1029e3af04fSMika Westerberg  * @bdata: button data for button to be disabled
1039e3af04fSMika Westerberg  *
1049e3af04fSMika Westerberg  * Disables button pointed by @bdata. This is done by masking
1059e3af04fSMika Westerberg  * IRQ line. After this function is called, button won't generate
1069e3af04fSMika Westerberg  * input events anymore. Note that one can only disable buttons
1079e3af04fSMika Westerberg  * that don't share IRQs.
1089e3af04fSMika Westerberg  *
1099e3af04fSMika Westerberg  * Make sure that @bdata->disable_lock is locked when entering
1109e3af04fSMika Westerberg  * this function to avoid races when concurrent threads are
1119e3af04fSMika Westerberg  * disabling buttons at the same time.
1129e3af04fSMika Westerberg  */
1139e3af04fSMika Westerberg static void gpio_keys_disable_button(struct gpio_button_data *bdata)
1149e3af04fSMika Westerberg {
1159e3af04fSMika Westerberg 	if (!bdata->disabled) {
1169e3af04fSMika Westerberg 		/*
1179e3af04fSMika Westerberg 		 * Disable IRQ and possible debouncing timer.
1189e3af04fSMika Westerberg 		 */
119d8ee4a1cSLaxman Dewangan 		disable_irq(bdata->irq);
12028ed684fSGrazvydas Ignotas 		if (bdata->timer_debounce)
1219e3af04fSMika Westerberg 			del_timer_sync(&bdata->timer);
1229e3af04fSMika Westerberg 
1239e3af04fSMika Westerberg 		bdata->disabled = true;
1249e3af04fSMika Westerberg 	}
1259e3af04fSMika Westerberg }
1269e3af04fSMika Westerberg 
1279e3af04fSMika Westerberg /**
1289e3af04fSMika Westerberg  * gpio_keys_enable_button() - enables given GPIO button
1299e3af04fSMika Westerberg  * @bdata: button data for button to be disabled
1309e3af04fSMika Westerberg  *
1319e3af04fSMika Westerberg  * Enables given button pointed by @bdata.
1329e3af04fSMika Westerberg  *
1339e3af04fSMika Westerberg  * Make sure that @bdata->disable_lock is locked when entering
1349e3af04fSMika Westerberg  * this function to avoid races with concurrent threads trying
1359e3af04fSMika Westerberg  * to enable the same button at the same time.
1369e3af04fSMika Westerberg  */
1379e3af04fSMika Westerberg static void gpio_keys_enable_button(struct gpio_button_data *bdata)
1389e3af04fSMika Westerberg {
1399e3af04fSMika Westerberg 	if (bdata->disabled) {
140d8ee4a1cSLaxman Dewangan 		enable_irq(bdata->irq);
1419e3af04fSMika Westerberg 		bdata->disabled = false;
1429e3af04fSMika Westerberg 	}
1439e3af04fSMika Westerberg }
1449e3af04fSMika Westerberg 
1459e3af04fSMika Westerberg /**
1469e3af04fSMika Westerberg  * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
1479e3af04fSMika Westerberg  * @ddata: pointer to drvdata
1489e3af04fSMika Westerberg  * @buf: buffer where stringified bitmap is written
1499e3af04fSMika Westerberg  * @type: button type (%EV_KEY, %EV_SW)
1509e3af04fSMika Westerberg  * @only_disabled: does caller want only those buttons that are
1519e3af04fSMika Westerberg  *                 currently disabled or all buttons that can be
1529e3af04fSMika Westerberg  *                 disabled
1539e3af04fSMika Westerberg  *
1549e3af04fSMika Westerberg  * This function writes buttons that can be disabled to @buf. If
1559e3af04fSMika Westerberg  * @only_disabled is true, then @buf contains only those buttons
1569e3af04fSMika Westerberg  * that are currently disabled. Returns 0 on success or negative
1579e3af04fSMika Westerberg  * errno on failure.
1589e3af04fSMika Westerberg  */
1599e3af04fSMika Westerberg static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
1609e3af04fSMika Westerberg 					  char *buf, unsigned int type,
1619e3af04fSMika Westerberg 					  bool only_disabled)
1629e3af04fSMika Westerberg {
1639e3af04fSMika Westerberg 	int n_events = get_n_events_by_type(type);
1649e3af04fSMika Westerberg 	unsigned long *bits;
1659e3af04fSMika Westerberg 	ssize_t ret;
1669e3af04fSMika Westerberg 	int i;
1679e3af04fSMika Westerberg 
1689e3af04fSMika Westerberg 	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
1699e3af04fSMika Westerberg 	if (!bits)
1709e3af04fSMika Westerberg 		return -ENOMEM;
1719e3af04fSMika Westerberg 
172219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
1739e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
1749e3af04fSMika Westerberg 
1759e3af04fSMika Westerberg 		if (bdata->button->type != type)
1769e3af04fSMika Westerberg 			continue;
1779e3af04fSMika Westerberg 
1789e3af04fSMika Westerberg 		if (only_disabled && !bdata->disabled)
1799e3af04fSMika Westerberg 			continue;
1809e3af04fSMika Westerberg 
1819e3af04fSMika Westerberg 		__set_bit(bdata->button->code, bits);
1829e3af04fSMika Westerberg 	}
1839e3af04fSMika Westerberg 
1849e3af04fSMika Westerberg 	ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
1859e3af04fSMika Westerberg 	buf[ret++] = '\n';
1869e3af04fSMika Westerberg 	buf[ret] = '\0';
1879e3af04fSMika Westerberg 
1889e3af04fSMika Westerberg 	kfree(bits);
1899e3af04fSMika Westerberg 
1909e3af04fSMika Westerberg 	return ret;
1919e3af04fSMika Westerberg }
1929e3af04fSMika Westerberg 
1939e3af04fSMika Westerberg /**
1949e3af04fSMika Westerberg  * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
1959e3af04fSMika Westerberg  * @ddata: pointer to drvdata
1969e3af04fSMika Westerberg  * @buf: buffer from userspace that contains stringified bitmap
1979e3af04fSMika Westerberg  * @type: button type (%EV_KEY, %EV_SW)
1989e3af04fSMika Westerberg  *
1999e3af04fSMika Westerberg  * This function parses stringified bitmap from @buf and disables/enables
200a16ca239SDmitry Torokhov  * GPIO buttons accordingly. Returns 0 on success and negative error
2019e3af04fSMika Westerberg  * on failure.
2029e3af04fSMika Westerberg  */
2039e3af04fSMika Westerberg static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
2049e3af04fSMika Westerberg 					   const char *buf, unsigned int type)
2059e3af04fSMika Westerberg {
2069e3af04fSMika Westerberg 	int n_events = get_n_events_by_type(type);
2079e3af04fSMika Westerberg 	unsigned long *bits;
2089e3af04fSMika Westerberg 	ssize_t error;
2099e3af04fSMika Westerberg 	int i;
2109e3af04fSMika Westerberg 
2119e3af04fSMika Westerberg 	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
2129e3af04fSMika Westerberg 	if (!bits)
2139e3af04fSMika Westerberg 		return -ENOMEM;
2149e3af04fSMika Westerberg 
2159e3af04fSMika Westerberg 	error = bitmap_parselist(buf, bits, n_events);
2169e3af04fSMika Westerberg 	if (error)
2179e3af04fSMika Westerberg 		goto out;
2189e3af04fSMika Westerberg 
2199e3af04fSMika Westerberg 	/* First validate */
220219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
2219e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
2229e3af04fSMika Westerberg 
2239e3af04fSMika Westerberg 		if (bdata->button->type != type)
2249e3af04fSMika Westerberg 			continue;
2259e3af04fSMika Westerberg 
2269e3af04fSMika Westerberg 		if (test_bit(bdata->button->code, bits) &&
2279e3af04fSMika Westerberg 		    !bdata->button->can_disable) {
2289e3af04fSMika Westerberg 			error = -EINVAL;
2299e3af04fSMika Westerberg 			goto out;
2309e3af04fSMika Westerberg 		}
2319e3af04fSMika Westerberg 	}
2329e3af04fSMika Westerberg 
2339e3af04fSMika Westerberg 	mutex_lock(&ddata->disable_lock);
2349e3af04fSMika Westerberg 
235219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
2369e3af04fSMika Westerberg 		struct gpio_button_data *bdata = &ddata->data[i];
2379e3af04fSMika Westerberg 
2389e3af04fSMika Westerberg 		if (bdata->button->type != type)
2399e3af04fSMika Westerberg 			continue;
2409e3af04fSMika Westerberg 
2419e3af04fSMika Westerberg 		if (test_bit(bdata->button->code, bits))
2429e3af04fSMika Westerberg 			gpio_keys_disable_button(bdata);
2439e3af04fSMika Westerberg 		else
2449e3af04fSMika Westerberg 			gpio_keys_enable_button(bdata);
2459e3af04fSMika Westerberg 	}
2469e3af04fSMika Westerberg 
2479e3af04fSMika Westerberg 	mutex_unlock(&ddata->disable_lock);
2489e3af04fSMika Westerberg 
2499e3af04fSMika Westerberg out:
2509e3af04fSMika Westerberg 	kfree(bits);
2519e3af04fSMika Westerberg 	return error;
2529e3af04fSMika Westerberg }
2539e3af04fSMika Westerberg 
2549e3af04fSMika Westerberg #define ATTR_SHOW_FN(name, type, only_disabled)				\
2559e3af04fSMika Westerberg static ssize_t gpio_keys_show_##name(struct device *dev,		\
2569e3af04fSMika Westerberg 				     struct device_attribute *attr,	\
2579e3af04fSMika Westerberg 				     char *buf)				\
2589e3af04fSMika Westerberg {									\
2599e3af04fSMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);		\
2609e3af04fSMika Westerberg 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);	\
2619e3af04fSMika Westerberg 									\
2629e3af04fSMika Westerberg 	return gpio_keys_attr_show_helper(ddata, buf,			\
2639e3af04fSMika Westerberg 					  type, only_disabled);		\
2649e3af04fSMika Westerberg }
2659e3af04fSMika Westerberg 
2669e3af04fSMika Westerberg ATTR_SHOW_FN(keys, EV_KEY, false);
2679e3af04fSMika Westerberg ATTR_SHOW_FN(switches, EV_SW, false);
2689e3af04fSMika Westerberg ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
2699e3af04fSMika Westerberg ATTR_SHOW_FN(disabled_switches, EV_SW, true);
2709e3af04fSMika Westerberg 
2719e3af04fSMika Westerberg /*
2729e3af04fSMika Westerberg  * ATTRIBUTES:
2739e3af04fSMika Westerberg  *
2749e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/keys [ro]
2759e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/switches [ro]
2769e3af04fSMika Westerberg  */
2779e3af04fSMika Westerberg static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
2789e3af04fSMika Westerberg static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
2799e3af04fSMika Westerberg 
2809e3af04fSMika Westerberg #define ATTR_STORE_FN(name, type)					\
2819e3af04fSMika Westerberg static ssize_t gpio_keys_store_##name(struct device *dev,		\
2829e3af04fSMika Westerberg 				      struct device_attribute *attr,	\
2839e3af04fSMika Westerberg 				      const char *buf,			\
2849e3af04fSMika Westerberg 				      size_t count)			\
2859e3af04fSMika Westerberg {									\
2869e3af04fSMika Westerberg 	struct platform_device *pdev = to_platform_device(dev);		\
2879e3af04fSMika Westerberg 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);	\
2889e3af04fSMika Westerberg 	ssize_t error;							\
2899e3af04fSMika Westerberg 									\
2909e3af04fSMika Westerberg 	error = gpio_keys_attr_store_helper(ddata, buf, type);		\
2919e3af04fSMika Westerberg 	if (error)							\
2929e3af04fSMika Westerberg 		return error;						\
2939e3af04fSMika Westerberg 									\
2949e3af04fSMika Westerberg 	return count;							\
2959e3af04fSMika Westerberg }
2969e3af04fSMika Westerberg 
2979e3af04fSMika Westerberg ATTR_STORE_FN(disabled_keys, EV_KEY);
2989e3af04fSMika Westerberg ATTR_STORE_FN(disabled_switches, EV_SW);
2999e3af04fSMika Westerberg 
3009e3af04fSMika Westerberg /*
3019e3af04fSMika Westerberg  * ATTRIBUTES:
3029e3af04fSMika Westerberg  *
3039e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/disabled_keys [rw]
3049e3af04fSMika Westerberg  * /sys/devices/platform/gpio-keys/disables_switches [rw]
3059e3af04fSMika Westerberg  */
3069e3af04fSMika Westerberg static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
3079e3af04fSMika Westerberg 		   gpio_keys_show_disabled_keys,
3089e3af04fSMika Westerberg 		   gpio_keys_store_disabled_keys);
3099e3af04fSMika Westerberg static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
3109e3af04fSMika Westerberg 		   gpio_keys_show_disabled_switches,
3119e3af04fSMika Westerberg 		   gpio_keys_store_disabled_switches);
3129e3af04fSMika Westerberg 
3139e3af04fSMika Westerberg static struct attribute *gpio_keys_attrs[] = {
3149e3af04fSMika Westerberg 	&dev_attr_keys.attr,
3159e3af04fSMika Westerberg 	&dev_attr_switches.attr,
3169e3af04fSMika Westerberg 	&dev_attr_disabled_keys.attr,
3179e3af04fSMika Westerberg 	&dev_attr_disabled_switches.attr,
3189e3af04fSMika Westerberg 	NULL,
3199e3af04fSMika Westerberg };
3209e3af04fSMika Westerberg 
3219e3af04fSMika Westerberg static struct attribute_group gpio_keys_attr_group = {
3229e3af04fSMika Westerberg 	.attrs = gpio_keys_attrs,
3239e3af04fSMika Westerberg };
3249e3af04fSMika Westerberg 
325d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
32678a56aabSPhil Blundell {
327d9080921SDmitry Torokhov 	const struct gpio_keys_button *button = bdata->button;
328ce25d7e9SUwe Kleine-König 	struct input_dev *input = bdata->input;
32984767d00SRoman Moravcik 	unsigned int type = button->type ?: EV_KEY;
33094a8cab8SPhilippe Langlais 	int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
33184767d00SRoman Moravcik 
33292a47674SAlexander Stein 	if (type == EV_ABS) {
33392a47674SAlexander Stein 		if (state)
33492a47674SAlexander Stein 			input_event(input, type, button->code, button->value);
33592a47674SAlexander Stein 	} else {
33684767d00SRoman Moravcik 		input_event(input, type, button->code, !!state);
33792a47674SAlexander Stein 	}
33878a56aabSPhil Blundell 	input_sync(input);
339a33466e3SDmitry Baryshkov }
340a33466e3SDmitry Baryshkov 
341d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_work_func(struct work_struct *work)
3426ee88d71SDaniel Mack {
3436ee88d71SDaniel Mack 	struct gpio_button_data *bdata =
3446ee88d71SDaniel Mack 		container_of(work, struct gpio_button_data, work);
3456ee88d71SDaniel Mack 
346d8ee4a1cSLaxman Dewangan 	gpio_keys_gpio_report_event(bdata);
3472fba26c6SNeilBrown 
3482fba26c6SNeilBrown 	if (bdata->button->wakeup)
3492fba26c6SNeilBrown 		pm_relax(bdata->input->dev.parent);
3506ee88d71SDaniel Mack }
3516ee88d71SDaniel Mack 
352d8ee4a1cSLaxman Dewangan static void gpio_keys_gpio_timer(unsigned long _data)
353ca865a77SJani Nikula {
354d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
355ca865a77SJani Nikula 
356d8ee4a1cSLaxman Dewangan 	schedule_work(&bdata->work);
357ca865a77SJani Nikula }
358ca865a77SJani Nikula 
359d8ee4a1cSLaxman Dewangan static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
360a33466e3SDmitry Baryshkov {
36157ffe9d5SUwe Kleine-König 	struct gpio_button_data *bdata = dev_id;
362a33466e3SDmitry Baryshkov 
363d8ee4a1cSLaxman Dewangan 	BUG_ON(irq != bdata->irq);
364a33466e3SDmitry Baryshkov 
3652fba26c6SNeilBrown 	if (bdata->button->wakeup)
3662fba26c6SNeilBrown 		pm_stay_awake(bdata->input->dev.parent);
36728ed684fSGrazvydas Ignotas 	if (bdata->timer_debounce)
368ca865a77SJani Nikula 		mod_timer(&bdata->timer,
36928ed684fSGrazvydas Ignotas 			jiffies + msecs_to_jiffies(bdata->timer_debounce));
370ca865a77SJani Nikula 	else
371da0d03feSJani Nikula 		schedule_work(&bdata->work);
372a33466e3SDmitry Baryshkov 
3731164ec1aSDavid Brownell 	return IRQ_HANDLED;
37478a56aabSPhil Blundell }
37578a56aabSPhil Blundell 
376d8ee4a1cSLaxman Dewangan static void gpio_keys_irq_timer(unsigned long _data)
377d8ee4a1cSLaxman Dewangan {
378d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
379d8ee4a1cSLaxman Dewangan 	struct input_dev *input = bdata->input;
380d8ee4a1cSLaxman Dewangan 	unsigned long flags;
381d8ee4a1cSLaxman Dewangan 
382d8ee4a1cSLaxman Dewangan 	spin_lock_irqsave(&bdata->lock, flags);
383d8ee4a1cSLaxman Dewangan 	if (bdata->key_pressed) {
384d8ee4a1cSLaxman Dewangan 		input_event(input, EV_KEY, bdata->button->code, 0);
385d8ee4a1cSLaxman Dewangan 		input_sync(input);
386d8ee4a1cSLaxman Dewangan 		bdata->key_pressed = false;
387d8ee4a1cSLaxman Dewangan 	}
388d8ee4a1cSLaxman Dewangan 	spin_unlock_irqrestore(&bdata->lock, flags);
389d8ee4a1cSLaxman Dewangan }
390d8ee4a1cSLaxman Dewangan 
391d8ee4a1cSLaxman Dewangan static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
392d8ee4a1cSLaxman Dewangan {
393d8ee4a1cSLaxman Dewangan 	struct gpio_button_data *bdata = dev_id;
394d8ee4a1cSLaxman Dewangan 	const struct gpio_keys_button *button = bdata->button;
395d8ee4a1cSLaxman Dewangan 	struct input_dev *input = bdata->input;
396d8ee4a1cSLaxman Dewangan 	unsigned long flags;
397d8ee4a1cSLaxman Dewangan 
398d8ee4a1cSLaxman Dewangan 	BUG_ON(irq != bdata->irq);
399d8ee4a1cSLaxman Dewangan 
400d8ee4a1cSLaxman Dewangan 	spin_lock_irqsave(&bdata->lock, flags);
401d8ee4a1cSLaxman Dewangan 
402d8ee4a1cSLaxman Dewangan 	if (!bdata->key_pressed) {
4032fba26c6SNeilBrown 		if (bdata->button->wakeup)
4042fba26c6SNeilBrown 			pm_wakeup_event(bdata->input->dev.parent, 0);
4052fba26c6SNeilBrown 
406d8ee4a1cSLaxman Dewangan 		input_event(input, EV_KEY, button->code, 1);
407d8ee4a1cSLaxman Dewangan 		input_sync(input);
408d8ee4a1cSLaxman Dewangan 
409d8ee4a1cSLaxman Dewangan 		if (!bdata->timer_debounce) {
410d8ee4a1cSLaxman Dewangan 			input_event(input, EV_KEY, button->code, 0);
411d8ee4a1cSLaxman Dewangan 			input_sync(input);
412d8ee4a1cSLaxman Dewangan 			goto out;
413d8ee4a1cSLaxman Dewangan 		}
414d8ee4a1cSLaxman Dewangan 
415d8ee4a1cSLaxman Dewangan 		bdata->key_pressed = true;
416d8ee4a1cSLaxman Dewangan 	}
417d8ee4a1cSLaxman Dewangan 
418d8ee4a1cSLaxman Dewangan 	if (bdata->timer_debounce)
419d8ee4a1cSLaxman Dewangan 		mod_timer(&bdata->timer,
420d8ee4a1cSLaxman Dewangan 			jiffies + msecs_to_jiffies(bdata->timer_debounce));
421d8ee4a1cSLaxman Dewangan out:
422d8ee4a1cSLaxman Dewangan 	spin_unlock_irqrestore(&bdata->lock, flags);
423d8ee4a1cSLaxman Dewangan 	return IRQ_HANDLED;
424d8ee4a1cSLaxman Dewangan }
425d8ee4a1cSLaxman Dewangan 
4265298cc4cSBill Pemberton static int gpio_keys_setup_key(struct platform_device *pdev,
427d9080921SDmitry Torokhov 				struct input_dev *input,
428bc8f1eafSBen Dooks 				struct gpio_button_data *bdata,
429d9080921SDmitry Torokhov 				const struct gpio_keys_button *button)
430bc8f1eafSBen Dooks {
43192a47674SAlexander Stein 	const char *desc = button->desc ? button->desc : "gpio_keys";
4329e3af04fSMika Westerberg 	struct device *dev = &pdev->dev;
433d8ee4a1cSLaxman Dewangan 	irq_handler_t isr;
4349e3af04fSMika Westerberg 	unsigned long irqflags;
435bc8f1eafSBen Dooks 	int irq, error;
436bc8f1eafSBen Dooks 
437d9080921SDmitry Torokhov 	bdata->input = input;
438d9080921SDmitry Torokhov 	bdata->button = button;
439d8ee4a1cSLaxman Dewangan 	spin_lock_init(&bdata->lock);
440d8ee4a1cSLaxman Dewangan 
441d8ee4a1cSLaxman Dewangan 	if (gpio_is_valid(button->gpio)) {
442bc8f1eafSBen Dooks 
443333e34bfSDmitry Torokhov 		error = gpio_request_one(button->gpio, GPIOF_IN, desc);
444bc8f1eafSBen Dooks 		if (error < 0) {
445d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Failed to request GPIO %d, error %d\n",
446bc8f1eafSBen Dooks 				button->gpio, error);
447d8ee4a1cSLaxman Dewangan 			return error;
448bc8f1eafSBen Dooks 		}
449bc8f1eafSBen Dooks 
45028ed684fSGrazvydas Ignotas 		if (button->debounce_interval) {
45128ed684fSGrazvydas Ignotas 			error = gpio_set_debounce(button->gpio,
45228ed684fSGrazvydas Ignotas 					button->debounce_interval * 1000);
45328ed684fSGrazvydas Ignotas 			/* use timer if gpiolib doesn't provide debounce */
45428ed684fSGrazvydas Ignotas 			if (error < 0)
455d8ee4a1cSLaxman Dewangan 				bdata->timer_debounce =
456d8ee4a1cSLaxman Dewangan 						button->debounce_interval;
45728ed684fSGrazvydas Ignotas 		}
45828ed684fSGrazvydas Ignotas 
459bc8f1eafSBen Dooks 		irq = gpio_to_irq(button->gpio);
460bc8f1eafSBen Dooks 		if (irq < 0) {
461bc8f1eafSBen Dooks 			error = irq;
462d8ee4a1cSLaxman Dewangan 			dev_err(dev,
463d8ee4a1cSLaxman Dewangan 				"Unable to get irq number for GPIO %d, error %d\n",
464bc8f1eafSBen Dooks 				button->gpio, error);
465d8ee4a1cSLaxman Dewangan 			goto fail;
466d8ee4a1cSLaxman Dewangan 		}
467d8ee4a1cSLaxman Dewangan 		bdata->irq = irq;
468d8ee4a1cSLaxman Dewangan 
469d8ee4a1cSLaxman Dewangan 		INIT_WORK(&bdata->work, gpio_keys_gpio_work_func);
470d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
471d8ee4a1cSLaxman Dewangan 			    gpio_keys_gpio_timer, (unsigned long)bdata);
472d8ee4a1cSLaxman Dewangan 
473d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_gpio_isr;
474d8ee4a1cSLaxman Dewangan 		irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
475d8ee4a1cSLaxman Dewangan 
476d8ee4a1cSLaxman Dewangan 	} else {
477d8ee4a1cSLaxman Dewangan 		if (!button->irq) {
478d8ee4a1cSLaxman Dewangan 			dev_err(dev, "No IRQ specified\n");
479d8ee4a1cSLaxman Dewangan 			return -EINVAL;
480d8ee4a1cSLaxman Dewangan 		}
481d8ee4a1cSLaxman Dewangan 		bdata->irq = button->irq;
482d8ee4a1cSLaxman Dewangan 
483d8ee4a1cSLaxman Dewangan 		if (button->type && button->type != EV_KEY) {
484d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
485d8ee4a1cSLaxman Dewangan 			return -EINVAL;
486bc8f1eafSBen Dooks 		}
487bc8f1eafSBen Dooks 
488d8ee4a1cSLaxman Dewangan 		bdata->timer_debounce = button->debounce_interval;
489d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
490d8ee4a1cSLaxman Dewangan 			    gpio_keys_irq_timer, (unsigned long)bdata);
491d8ee4a1cSLaxman Dewangan 
492d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_irq_isr;
493d8ee4a1cSLaxman Dewangan 		irqflags = 0;
494d8ee4a1cSLaxman Dewangan 	}
495d8ee4a1cSLaxman Dewangan 
496d8ee4a1cSLaxman Dewangan 	input_set_capability(input, button->type ?: EV_KEY, button->code);
497d8ee4a1cSLaxman Dewangan 
4989e3af04fSMika Westerberg 	/*
4999e3af04fSMika Westerberg 	 * If platform has specified that the button can be disabled,
5009e3af04fSMika Westerberg 	 * we don't want it to share the interrupt line.
5019e3af04fSMika Westerberg 	 */
5029e3af04fSMika Westerberg 	if (!button->can_disable)
5039e3af04fSMika Westerberg 		irqflags |= IRQF_SHARED;
5049e3af04fSMika Westerberg 
505d8ee4a1cSLaxman Dewangan 	error = request_any_context_irq(bdata->irq, isr, irqflags, desc, bdata);
50694a8cab8SPhilippe Langlais 	if (error < 0) {
507bc8f1eafSBen Dooks 		dev_err(dev, "Unable to claim irq %d; error %d\n",
508d8ee4a1cSLaxman Dewangan 			bdata->irq, error);
509d8ee4a1cSLaxman Dewangan 		goto fail;
510bc8f1eafSBen Dooks 	}
511bc8f1eafSBen Dooks 
512bc8f1eafSBen Dooks 	return 0;
513bc8f1eafSBen Dooks 
514d8ee4a1cSLaxman Dewangan fail:
515d8ee4a1cSLaxman Dewangan 	if (gpio_is_valid(button->gpio))
516bc8f1eafSBen Dooks 		gpio_free(button->gpio);
517d8ee4a1cSLaxman Dewangan 
518bc8f1eafSBen Dooks 	return error;
519bc8f1eafSBen Dooks }
520bc8f1eafSBen Dooks 
5215b76d7b4SDmitry Torokhov static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
5225b76d7b4SDmitry Torokhov {
5235b76d7b4SDmitry Torokhov 	struct input_dev *input = ddata->input;
5245b76d7b4SDmitry Torokhov 	int i;
5255b76d7b4SDmitry Torokhov 
5265b76d7b4SDmitry Torokhov 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
5275b76d7b4SDmitry Torokhov 		struct gpio_button_data *bdata = &ddata->data[i];
5285b76d7b4SDmitry Torokhov 		if (gpio_is_valid(bdata->button->gpio))
5295b76d7b4SDmitry Torokhov 			gpio_keys_gpio_report_event(bdata);
5305b76d7b4SDmitry Torokhov 	}
5315b76d7b4SDmitry Torokhov 	input_sync(input);
5325b76d7b4SDmitry Torokhov }
5335b76d7b4SDmitry Torokhov 
534173bdd74SShubhrajyoti D static int gpio_keys_open(struct input_dev *input)
535173bdd74SShubhrajyoti D {
536173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
537219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
5385b76d7b4SDmitry Torokhov 	int error;
539173bdd74SShubhrajyoti D 
5405b76d7b4SDmitry Torokhov 	if (pdata->enable) {
5415b76d7b4SDmitry Torokhov 		error = pdata->enable(input->dev.parent);
5425b76d7b4SDmitry Torokhov 		if (error)
5435b76d7b4SDmitry Torokhov 			return error;
5445b76d7b4SDmitry Torokhov 	}
5455b76d7b4SDmitry Torokhov 
5465b76d7b4SDmitry Torokhov 	/* Report current state of buttons that are connected to GPIOs */
5475b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
5485b76d7b4SDmitry Torokhov 
5495b76d7b4SDmitry Torokhov 	return 0;
550173bdd74SShubhrajyoti D }
551173bdd74SShubhrajyoti D 
552173bdd74SShubhrajyoti D static void gpio_keys_close(struct input_dev *input)
553173bdd74SShubhrajyoti D {
554173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
555219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
556173bdd74SShubhrajyoti D 
557219edc71SAlexandre Pereira da Silva 	if (pdata->disable)
558219edc71SAlexandre Pereira da Silva 		pdata->disable(input->dev.parent);
559173bdd74SShubhrajyoti D }
560173bdd74SShubhrajyoti D 
561fd05d089SDavid Jander /*
562fd05d089SDavid Jander  * Handlers for alternative sources of platform_data
563fd05d089SDavid Jander  */
564219edc71SAlexandre Pereira da Silva 
565fd05d089SDavid Jander #ifdef CONFIG_OF
566fd05d089SDavid Jander /*
567fd05d089SDavid Jander  * Translate OpenFirmware node properties into platform_data
568fd05d089SDavid Jander  */
5695298cc4cSBill Pemberton static struct gpio_keys_platform_data *
570219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
571fd05d089SDavid Jander {
572fd05d089SDavid Jander 	struct device_node *node, *pp;
573219edc71SAlexandre Pereira da Silva 	struct gpio_keys_platform_data *pdata;
574219edc71SAlexandre Pereira da Silva 	struct gpio_keys_button *button;
575219edc71SAlexandre Pereira da Silva 	int error;
576219edc71SAlexandre Pereira da Silva 	int nbuttons;
577fd05d089SDavid Jander 	int i;
578fd05d089SDavid Jander 
579fd05d089SDavid Jander 	node = dev->of_node;
580219edc71SAlexandre Pereira da Silva 	if (!node) {
581219edc71SAlexandre Pereira da Silva 		error = -ENODEV;
582219edc71SAlexandre Pereira da Silva 		goto err_out;
583219edc71SAlexandre Pereira da Silva 	}
584fd05d089SDavid Jander 
585219edc71SAlexandre Pereira da Silva 	nbuttons = of_get_child_count(node);
586219edc71SAlexandre Pereira da Silva 	if (nbuttons == 0) {
587219edc71SAlexandre Pereira da Silva 		error = -ENODEV;
588219edc71SAlexandre Pereira da Silva 		goto err_out;
589219edc71SAlexandre Pereira da Silva 	}
590219edc71SAlexandre Pereira da Silva 
591219edc71SAlexandre Pereira da Silva 	pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
592219edc71SAlexandre Pereira da Silva 			GFP_KERNEL);
593219edc71SAlexandre Pereira da Silva 	if (!pdata) {
594219edc71SAlexandre Pereira da Silva 		error = -ENOMEM;
595219edc71SAlexandre Pereira da Silva 		goto err_out;
596219edc71SAlexandre Pereira da Silva 	}
597219edc71SAlexandre Pereira da Silva 
598219edc71SAlexandre Pereira da Silva 	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
599219edc71SAlexandre Pereira da Silva 	pdata->nbuttons = nbuttons;
600fd05d089SDavid Jander 
601cca84699STobias Klauser 	pdata->rep = !!of_get_property(node, "autorepeat", NULL);
602fd05d089SDavid Jander 
603fd05d089SDavid Jander 	i = 0;
604219edc71SAlexandre Pereira da Silva 	for_each_child_of_node(node, pp) {
605*e324ce61SDmitry Torokhov 		int gpio;
606fd05d089SDavid Jander 		enum of_gpio_flags flags;
607fd05d089SDavid Jander 
608fd05d089SDavid Jander 		if (!of_find_property(pp, "gpios", NULL)) {
609fd05d089SDavid Jander 			pdata->nbuttons--;
610fd05d089SDavid Jander 			dev_warn(dev, "Found button without gpios\n");
611fd05d089SDavid Jander 			continue;
612fd05d089SDavid Jander 		}
613fd05d089SDavid Jander 
614*e324ce61SDmitry Torokhov 		gpio = of_get_gpio_flags(pp, 0, &flags);
615*e324ce61SDmitry Torokhov 		if (gpio < 0) {
616*e324ce61SDmitry Torokhov 			error = gpio;
617*e324ce61SDmitry Torokhov 			if (error != -EPROBE_DEFER)
618*e324ce61SDmitry Torokhov 				dev_err(dev,
619*e324ce61SDmitry Torokhov 					"Failed to get gpio flags, error: %d\n",
620*e324ce61SDmitry Torokhov 					error);
621*e324ce61SDmitry Torokhov 			goto err_free_pdata;
622*e324ce61SDmitry Torokhov 		}
623*e324ce61SDmitry Torokhov 
624219edc71SAlexandre Pereira da Silva 		button = &pdata->buttons[i++];
625fd05d089SDavid Jander 
626*e324ce61SDmitry Torokhov 		button->gpio = gpio;
627219edc71SAlexandre Pereira da Silva 		button->active_low = flags & OF_GPIO_ACTIVE_LOW;
628fd05d089SDavid Jander 
629219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "linux,code", &button->code)) {
630219edc71SAlexandre Pereira da Silva 			dev_err(dev, "Button without keycode: 0x%x\n",
631219edc71SAlexandre Pereira da Silva 				button->gpio);
632219edc71SAlexandre Pereira da Silva 			error = -EINVAL;
633219edc71SAlexandre Pereira da Silva 			goto err_free_pdata;
634fd05d089SDavid Jander 		}
635fd05d089SDavid Jander 
636219edc71SAlexandre Pereira da Silva 		button->desc = of_get_property(pp, "label", NULL);
637fd05d089SDavid Jander 
638219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "linux,input-type", &button->type))
639219edc71SAlexandre Pereira da Silva 			button->type = EV_KEY;
640fd05d089SDavid Jander 
641219edc71SAlexandre Pereira da Silva 		button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
642219edc71SAlexandre Pereira da Silva 
643219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "debounce-interval",
644219edc71SAlexandre Pereira da Silva 					 &button->debounce_interval))
645219edc71SAlexandre Pereira da Silva 			button->debounce_interval = 5;
646219edc71SAlexandre Pereira da Silva 	}
647219edc71SAlexandre Pereira da Silva 
648219edc71SAlexandre Pereira da Silva 	if (pdata->nbuttons == 0) {
649219edc71SAlexandre Pereira da Silva 		error = -EINVAL;
650219edc71SAlexandre Pereira da Silva 		goto err_free_pdata;
651219edc71SAlexandre Pereira da Silva 	}
652219edc71SAlexandre Pereira da Silva 
653219edc71SAlexandre Pereira da Silva 	return pdata;
654219edc71SAlexandre Pereira da Silva 
655219edc71SAlexandre Pereira da Silva err_free_pdata:
656219edc71SAlexandre Pereira da Silva 	kfree(pdata);
657219edc71SAlexandre Pereira da Silva err_out:
658219edc71SAlexandre Pereira da Silva 	return ERR_PTR(error);
659fd05d089SDavid Jander }
660fd05d089SDavid Jander 
661fd05d089SDavid Jander static struct of_device_id gpio_keys_of_match[] = {
662fd05d089SDavid Jander 	{ .compatible = "gpio-keys", },
663fd05d089SDavid Jander 	{ },
664fd05d089SDavid Jander };
665fd05d089SDavid Jander MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
666fd05d089SDavid Jander 
667fd05d089SDavid Jander #else
668fd05d089SDavid Jander 
669219edc71SAlexandre Pereira da Silva static inline struct gpio_keys_platform_data *
670219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
671fd05d089SDavid Jander {
672219edc71SAlexandre Pereira da Silva 	return ERR_PTR(-ENODEV);
673fd05d089SDavid Jander }
674fd05d089SDavid Jander 
675fd05d089SDavid Jander #endif
676fd05d089SDavid Jander 
677a16ca239SDmitry Torokhov static void gpio_remove_key(struct gpio_button_data *bdata)
678a16ca239SDmitry Torokhov {
679d8ee4a1cSLaxman Dewangan 	free_irq(bdata->irq, bdata);
680a16ca239SDmitry Torokhov 	if (bdata->timer_debounce)
681a16ca239SDmitry Torokhov 		del_timer_sync(&bdata->timer);
682a16ca239SDmitry Torokhov 	cancel_work_sync(&bdata->work);
683d8ee4a1cSLaxman Dewangan 	if (gpio_is_valid(bdata->button->gpio))
684a16ca239SDmitry Torokhov 		gpio_free(bdata->button->gpio);
685a16ca239SDmitry Torokhov }
686a16ca239SDmitry Torokhov 
6875298cc4cSBill Pemberton static int gpio_keys_probe(struct platform_device *pdev)
68878a56aabSPhil Blundell {
689db19fd8bSBen Dooks 	struct device *dev = &pdev->dev;
690219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
691219edc71SAlexandre Pereira da Silva 	struct gpio_keys_drvdata *ddata;
69278a56aabSPhil Blundell 	struct input_dev *input;
69378a56aabSPhil Blundell 	int i, error;
694e15b0213SAnti Sullin 	int wakeup = 0;
69578a56aabSPhil Blundell 
696fd05d089SDavid Jander 	if (!pdata) {
697219edc71SAlexandre Pereira da Silva 		pdata = gpio_keys_get_devtree_pdata(dev);
698219edc71SAlexandre Pereira da Silva 		if (IS_ERR(pdata))
699219edc71SAlexandre Pereira da Silva 			return PTR_ERR(pdata);
700fd05d089SDavid Jander 	}
701fd05d089SDavid Jander 
702a33466e3SDmitry Baryshkov 	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
703a33466e3SDmitry Baryshkov 			pdata->nbuttons * sizeof(struct gpio_button_data),
704a33466e3SDmitry Baryshkov 			GFP_KERNEL);
70578a56aabSPhil Blundell 	input = input_allocate_device();
706a33466e3SDmitry Baryshkov 	if (!ddata || !input) {
707db19fd8bSBen Dooks 		dev_err(dev, "failed to allocate state\n");
708a33466e3SDmitry Baryshkov 		error = -ENOMEM;
709a33466e3SDmitry Baryshkov 		goto fail1;
710a33466e3SDmitry Baryshkov 	}
71178a56aabSPhil Blundell 
712219edc71SAlexandre Pereira da Silva 	ddata->pdata = pdata;
7139e3af04fSMika Westerberg 	ddata->input = input;
7149e3af04fSMika Westerberg 	mutex_init(&ddata->disable_lock);
7159e3af04fSMika Westerberg 
716a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
717173bdd74SShubhrajyoti D 	input_set_drvdata(input, ddata);
71878a56aabSPhil Blundell 
71946711277SAlexander Stein 	input->name = pdata->name ? : pdev->name;
72078a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
721469ba4dfSDmitry Torokhov 	input->dev.parent = &pdev->dev;
722173bdd74SShubhrajyoti D 	input->open = gpio_keys_open;
723173bdd74SShubhrajyoti D 	input->close = gpio_keys_close;
72478a56aabSPhil Blundell 
72578a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
72678a56aabSPhil Blundell 	input->id.vendor = 0x0001;
72778a56aabSPhil Blundell 	input->id.product = 0x0001;
72878a56aabSPhil Blundell 	input->id.version = 0x0100;
72978a56aabSPhil Blundell 
730b67b4b11SDominic Curran 	/* Enable auto repeat feature of Linux input subsystem */
731b67b4b11SDominic Curran 	if (pdata->rep)
732b67b4b11SDominic Curran 		__set_bit(EV_REP, input->evbit);
733b67b4b11SDominic Curran 
73478a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
735d9080921SDmitry Torokhov 		const struct gpio_keys_button *button = &pdata->buttons[i];
736a33466e3SDmitry Baryshkov 		struct gpio_button_data *bdata = &ddata->data[i];
73778a56aabSPhil Blundell 
738d9080921SDmitry Torokhov 		error = gpio_keys_setup_key(pdev, input, bdata, button);
739bc8f1eafSBen Dooks 		if (error)
740a33466e3SDmitry Baryshkov 			goto fail2;
74184767d00SRoman Moravcik 
742e15b0213SAnti Sullin 		if (button->wakeup)
743e15b0213SAnti Sullin 			wakeup = 1;
74478a56aabSPhil Blundell 	}
74578a56aabSPhil Blundell 
7469e3af04fSMika Westerberg 	error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7479e3af04fSMika Westerberg 	if (error) {
7489e3af04fSMika Westerberg 		dev_err(dev, "Unable to export keys/switches, error: %d\n",
7499e3af04fSMika Westerberg 			error);
7509e3af04fSMika Westerberg 		goto fail2;
7519e3af04fSMika Westerberg 	}
7529e3af04fSMika Westerberg 
75378a56aabSPhil Blundell 	error = input_register_device(input);
75478a56aabSPhil Blundell 	if (error) {
7559e3af04fSMika Westerberg 		dev_err(dev, "Unable to register input device, error: %d\n",
7569e3af04fSMika Westerberg 			error);
7579e3af04fSMika Westerberg 		goto fail3;
75878a56aabSPhil Blundell 	}
75978a56aabSPhil Blundell 
760e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, wakeup);
761e15b0213SAnti Sullin 
76278a56aabSPhil Blundell 	return 0;
76378a56aabSPhil Blundell 
7649e3af04fSMika Westerberg  fail3:
7659e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
766a33466e3SDmitry Baryshkov  fail2:
767a16ca239SDmitry Torokhov 	while (--i >= 0)
768a16ca239SDmitry Torokhov 		gpio_remove_key(&ddata->data[i]);
76978a56aabSPhil Blundell 
770006df302SAnti Sullin 	platform_set_drvdata(pdev, NULL);
771a33466e3SDmitry Baryshkov  fail1:
77278a56aabSPhil Blundell 	input_free_device(input);
773a33466e3SDmitry Baryshkov 	kfree(ddata);
774219edc71SAlexandre Pereira da Silva 	/* If we have no platform data, we allocated pdata dynamically. */
775219edc71SAlexandre Pereira da Silva 	if (!dev_get_platdata(&pdev->dev))
776219edc71SAlexandre Pereira da Silva 		kfree(pdata);
77778a56aabSPhil Blundell 
77878a56aabSPhil Blundell 	return error;
77978a56aabSPhil Blundell }
78078a56aabSPhil Blundell 
781e2619cf7SBill Pemberton static int gpio_keys_remove(struct platform_device *pdev)
78278a56aabSPhil Blundell {
783a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
784a33466e3SDmitry Baryshkov 	struct input_dev *input = ddata->input;
78578a56aabSPhil Blundell 	int i;
78678a56aabSPhil Blundell 
7879e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7889e3af04fSMika Westerberg 
789e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, 0);
790e15b0213SAnti Sullin 
791219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++)
792a16ca239SDmitry Torokhov 		gpio_remove_key(&ddata->data[i]);
79378a56aabSPhil Blundell 
79478a56aabSPhil Blundell 	input_unregister_device(input);
795fd05d089SDavid Jander 
796219edc71SAlexandre Pereira da Silva 	/* If we have no platform data, we allocated pdata dynamically. */
797219edc71SAlexandre Pereira da Silva 	if (!dev_get_platdata(&pdev->dev))
798219edc71SAlexandre Pereira da Silva 		kfree(ddata->pdata);
799fd05d089SDavid Jander 
80016382079SAxel Lin 	kfree(ddata);
80178a56aabSPhil Blundell 
80278a56aabSPhil Blundell 	return 0;
80378a56aabSPhil Blundell }
80478a56aabSPhil Blundell 
805bdda8216SDmitry Torokhov #ifdef CONFIG_PM_SLEEP
806ae78e0e0SMike Rapoport static int gpio_keys_suspend(struct device *dev)
807e15b0213SAnti Sullin {
808fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
809dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
810e15b0213SAnti Sullin 	int i;
811e15b0213SAnti Sullin 
812fd05d089SDavid Jander 	if (device_may_wakeup(dev)) {
813219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
814d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
815d8ee4a1cSLaxman Dewangan 			if (bdata->button->wakeup)
816d8ee4a1cSLaxman Dewangan 				enable_irq_wake(bdata->irq);
817e15b0213SAnti Sullin 		}
818dda19a92SJonas Aaberg 	} else {
819dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
820dda19a92SJonas Aaberg 		if (input->users)
821dda19a92SJonas Aaberg 			gpio_keys_close(input);
822dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
823e15b0213SAnti Sullin 	}
824e15b0213SAnti Sullin 
825e15b0213SAnti Sullin 	return 0;
826e15b0213SAnti Sullin }
827e15b0213SAnti Sullin 
828ae78e0e0SMike Rapoport static int gpio_keys_resume(struct device *dev)
829e15b0213SAnti Sullin {
830fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
831dda19a92SJonas Aaberg 	struct input_dev *input = ddata->input;
832dda19a92SJonas Aaberg 	int error = 0;
833e15b0213SAnti Sullin 	int i;
834e15b0213SAnti Sullin 
835dda19a92SJonas Aaberg 	if (device_may_wakeup(dev)) {
836219edc71SAlexandre Pereira da Silva 		for (i = 0; i < ddata->pdata->nbuttons; i++) {
837d8ee4a1cSLaxman Dewangan 			struct gpio_button_data *bdata = &ddata->data[i];
838dda19a92SJonas Aaberg 			if (bdata->button->wakeup)
839d8ee4a1cSLaxman Dewangan 				disable_irq_wake(bdata->irq);
840e15b0213SAnti Sullin 		}
841dda19a92SJonas Aaberg 	} else {
842dda19a92SJonas Aaberg 		mutex_lock(&input->mutex);
843dda19a92SJonas Aaberg 		if (input->users)
844dda19a92SJonas Aaberg 			error = gpio_keys_open(input);
845dda19a92SJonas Aaberg 		mutex_unlock(&input->mutex);
846dda19a92SJonas Aaberg 	}
847dda19a92SJonas Aaberg 
848dda19a92SJonas Aaberg 	if (error)
849dda19a92SJonas Aaberg 		return error;
8505b76d7b4SDmitry Torokhov 
8515b76d7b4SDmitry Torokhov 	gpio_keys_report_state(ddata);
852e15b0213SAnti Sullin 	return 0;
853e15b0213SAnti Sullin }
854e15b0213SAnti Sullin #endif
855e15b0213SAnti Sullin 
856bdda8216SDmitry Torokhov static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
857bdda8216SDmitry Torokhov 
8589b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = {
85978a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
8601cb0aa88SBill Pemberton 	.remove		= gpio_keys_remove,
86178a56aabSPhil Blundell 	.driver		= {
86278a56aabSPhil Blundell 		.name	= "gpio-keys",
863d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
864ae78e0e0SMike Rapoport 		.pm	= &gpio_keys_pm_ops,
865219edc71SAlexandre Pereira da Silva 		.of_match_table = of_match_ptr(gpio_keys_of_match),
86678a56aabSPhil Blundell 	}
86778a56aabSPhil Blundell };
86878a56aabSPhil Blundell 
86978a56aabSPhil Blundell static int __init gpio_keys_init(void)
87078a56aabSPhil Blundell {
87178a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
87278a56aabSPhil Blundell }
87378a56aabSPhil Blundell 
87478a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
87578a56aabSPhil Blundell {
87678a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
87778a56aabSPhil Blundell }
87878a56aabSPhil Blundell 
879b2330205SDavid Jander late_initcall(gpio_keys_init);
88078a56aabSPhil Blundell module_exit(gpio_keys_exit);
88178a56aabSPhil Blundell 
88278a56aabSPhil Blundell MODULE_LICENSE("GPL");
88378a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
8847e2ecdf4SDavid Jander MODULE_DESCRIPTION("Keyboard driver for GPIOs");
885d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
886