xref: /openbmc/linux/drivers/input/keyboard/gpio_keys.c (revision e2619cf78e19476bfd7ceaefa9eff0847529346e)
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 
443bc8f1eafSBen Dooks 		error = gpio_request(button->gpio, 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 
450bc8f1eafSBen Dooks 		error = gpio_direction_input(button->gpio);
451bc8f1eafSBen Dooks 		if (error < 0) {
452d8ee4a1cSLaxman Dewangan 			dev_err(dev,
453d8ee4a1cSLaxman Dewangan 				"Failed to configure direction for GPIO %d, error %d\n",
454bc8f1eafSBen Dooks 				button->gpio, error);
455d8ee4a1cSLaxman Dewangan 			goto fail;
456bc8f1eafSBen Dooks 		}
457bc8f1eafSBen Dooks 
45828ed684fSGrazvydas Ignotas 		if (button->debounce_interval) {
45928ed684fSGrazvydas Ignotas 			error = gpio_set_debounce(button->gpio,
46028ed684fSGrazvydas Ignotas 					button->debounce_interval * 1000);
46128ed684fSGrazvydas Ignotas 			/* use timer if gpiolib doesn't provide debounce */
46228ed684fSGrazvydas Ignotas 			if (error < 0)
463d8ee4a1cSLaxman Dewangan 				bdata->timer_debounce =
464d8ee4a1cSLaxman Dewangan 						button->debounce_interval;
46528ed684fSGrazvydas Ignotas 		}
46628ed684fSGrazvydas Ignotas 
467bc8f1eafSBen Dooks 		irq = gpio_to_irq(button->gpio);
468bc8f1eafSBen Dooks 		if (irq < 0) {
469bc8f1eafSBen Dooks 			error = irq;
470d8ee4a1cSLaxman Dewangan 			dev_err(dev,
471d8ee4a1cSLaxman Dewangan 				"Unable to get irq number for GPIO %d, error %d\n",
472bc8f1eafSBen Dooks 				button->gpio, error);
473d8ee4a1cSLaxman Dewangan 			goto fail;
474d8ee4a1cSLaxman Dewangan 		}
475d8ee4a1cSLaxman Dewangan 		bdata->irq = irq;
476d8ee4a1cSLaxman Dewangan 
477d8ee4a1cSLaxman Dewangan 		INIT_WORK(&bdata->work, gpio_keys_gpio_work_func);
478d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
479d8ee4a1cSLaxman Dewangan 			    gpio_keys_gpio_timer, (unsigned long)bdata);
480d8ee4a1cSLaxman Dewangan 
481d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_gpio_isr;
482d8ee4a1cSLaxman Dewangan 		irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
483d8ee4a1cSLaxman Dewangan 
484d8ee4a1cSLaxman Dewangan 	} else {
485d8ee4a1cSLaxman Dewangan 		if (!button->irq) {
486d8ee4a1cSLaxman Dewangan 			dev_err(dev, "No IRQ specified\n");
487d8ee4a1cSLaxman Dewangan 			return -EINVAL;
488d8ee4a1cSLaxman Dewangan 		}
489d8ee4a1cSLaxman Dewangan 		bdata->irq = button->irq;
490d8ee4a1cSLaxman Dewangan 
491d8ee4a1cSLaxman Dewangan 		if (button->type && button->type != EV_KEY) {
492d8ee4a1cSLaxman Dewangan 			dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
493d8ee4a1cSLaxman Dewangan 			return -EINVAL;
494bc8f1eafSBen Dooks 		}
495bc8f1eafSBen Dooks 
496d8ee4a1cSLaxman Dewangan 		bdata->timer_debounce = button->debounce_interval;
497d8ee4a1cSLaxman Dewangan 		setup_timer(&bdata->timer,
498d8ee4a1cSLaxman Dewangan 			    gpio_keys_irq_timer, (unsigned long)bdata);
499d8ee4a1cSLaxman Dewangan 
500d8ee4a1cSLaxman Dewangan 		isr = gpio_keys_irq_isr;
501d8ee4a1cSLaxman Dewangan 		irqflags = 0;
502d8ee4a1cSLaxman Dewangan 	}
503d8ee4a1cSLaxman Dewangan 
504d8ee4a1cSLaxman Dewangan 	input_set_capability(input, button->type ?: EV_KEY, button->code);
505d8ee4a1cSLaxman Dewangan 
5069e3af04fSMika Westerberg 	/*
5079e3af04fSMika Westerberg 	 * If platform has specified that the button can be disabled,
5089e3af04fSMika Westerberg 	 * we don't want it to share the interrupt line.
5099e3af04fSMika Westerberg 	 */
5109e3af04fSMika Westerberg 	if (!button->can_disable)
5119e3af04fSMika Westerberg 		irqflags |= IRQF_SHARED;
5129e3af04fSMika Westerberg 
513d8ee4a1cSLaxman Dewangan 	error = request_any_context_irq(bdata->irq, isr, irqflags, desc, bdata);
51494a8cab8SPhilippe Langlais 	if (error < 0) {
515bc8f1eafSBen Dooks 		dev_err(dev, "Unable to claim irq %d; error %d\n",
516d8ee4a1cSLaxman Dewangan 			bdata->irq, error);
517d8ee4a1cSLaxman Dewangan 		goto fail;
518bc8f1eafSBen Dooks 	}
519bc8f1eafSBen Dooks 
520bc8f1eafSBen Dooks 	return 0;
521bc8f1eafSBen Dooks 
522d8ee4a1cSLaxman Dewangan fail:
523d8ee4a1cSLaxman Dewangan 	if (gpio_is_valid(button->gpio))
524bc8f1eafSBen Dooks 		gpio_free(button->gpio);
525d8ee4a1cSLaxman Dewangan 
526bc8f1eafSBen Dooks 	return error;
527bc8f1eafSBen Dooks }
528bc8f1eafSBen Dooks 
529173bdd74SShubhrajyoti D static int gpio_keys_open(struct input_dev *input)
530173bdd74SShubhrajyoti D {
531173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
532219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
533173bdd74SShubhrajyoti D 
534219edc71SAlexandre Pereira da Silva 	return pdata->enable ? pdata->enable(input->dev.parent) : 0;
535173bdd74SShubhrajyoti D }
536173bdd74SShubhrajyoti D 
537173bdd74SShubhrajyoti D static void gpio_keys_close(struct input_dev *input)
538173bdd74SShubhrajyoti D {
539173bdd74SShubhrajyoti D 	struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
540219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = ddata->pdata;
541173bdd74SShubhrajyoti D 
542219edc71SAlexandre Pereira da Silva 	if (pdata->disable)
543219edc71SAlexandre Pereira da Silva 		pdata->disable(input->dev.parent);
544173bdd74SShubhrajyoti D }
545173bdd74SShubhrajyoti D 
546fd05d089SDavid Jander /*
547fd05d089SDavid Jander  * Handlers for alternative sources of platform_data
548fd05d089SDavid Jander  */
549219edc71SAlexandre Pereira da Silva 
550fd05d089SDavid Jander #ifdef CONFIG_OF
551fd05d089SDavid Jander /*
552fd05d089SDavid Jander  * Translate OpenFirmware node properties into platform_data
553fd05d089SDavid Jander  */
5545298cc4cSBill Pemberton static struct gpio_keys_platform_data *
555219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
556fd05d089SDavid Jander {
557fd05d089SDavid Jander 	struct device_node *node, *pp;
558219edc71SAlexandre Pereira da Silva 	struct gpio_keys_platform_data *pdata;
559219edc71SAlexandre Pereira da Silva 	struct gpio_keys_button *button;
560219edc71SAlexandre Pereira da Silva 	int error;
561219edc71SAlexandre Pereira da Silva 	int nbuttons;
562fd05d089SDavid Jander 	int i;
563fd05d089SDavid Jander 
564fd05d089SDavid Jander 	node = dev->of_node;
565219edc71SAlexandre Pereira da Silva 	if (!node) {
566219edc71SAlexandre Pereira da Silva 		error = -ENODEV;
567219edc71SAlexandre Pereira da Silva 		goto err_out;
568219edc71SAlexandre Pereira da Silva 	}
569fd05d089SDavid Jander 
570219edc71SAlexandre Pereira da Silva 	nbuttons = of_get_child_count(node);
571219edc71SAlexandre Pereira da Silva 	if (nbuttons == 0) {
572219edc71SAlexandre Pereira da Silva 		error = -ENODEV;
573219edc71SAlexandre Pereira da Silva 		goto err_out;
574219edc71SAlexandre Pereira da Silva 	}
575219edc71SAlexandre Pereira da Silva 
576219edc71SAlexandre Pereira da Silva 	pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
577219edc71SAlexandre Pereira da Silva 			GFP_KERNEL);
578219edc71SAlexandre Pereira da Silva 	if (!pdata) {
579219edc71SAlexandre Pereira da Silva 		error = -ENOMEM;
580219edc71SAlexandre Pereira da Silva 		goto err_out;
581219edc71SAlexandre Pereira da Silva 	}
582219edc71SAlexandre Pereira da Silva 
583219edc71SAlexandre Pereira da Silva 	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
584219edc71SAlexandre Pereira da Silva 	pdata->nbuttons = nbuttons;
585fd05d089SDavid Jander 
586cca84699STobias Klauser 	pdata->rep = !!of_get_property(node, "autorepeat", NULL);
587fd05d089SDavid Jander 
588fd05d089SDavid Jander 	i = 0;
589219edc71SAlexandre Pereira da Silva 	for_each_child_of_node(node, pp) {
590fd05d089SDavid Jander 		enum of_gpio_flags flags;
591fd05d089SDavid Jander 
592fd05d089SDavid Jander 		if (!of_find_property(pp, "gpios", NULL)) {
593fd05d089SDavid Jander 			pdata->nbuttons--;
594fd05d089SDavid Jander 			dev_warn(dev, "Found button without gpios\n");
595fd05d089SDavid Jander 			continue;
596fd05d089SDavid Jander 		}
597fd05d089SDavid Jander 
598219edc71SAlexandre Pereira da Silva 		button = &pdata->buttons[i++];
599fd05d089SDavid Jander 
600219edc71SAlexandre Pereira da Silva 		button->gpio = of_get_gpio_flags(pp, 0, &flags);
601219edc71SAlexandre Pereira da Silva 		button->active_low = flags & OF_GPIO_ACTIVE_LOW;
602fd05d089SDavid Jander 
603219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "linux,code", &button->code)) {
604219edc71SAlexandre Pereira da Silva 			dev_err(dev, "Button without keycode: 0x%x\n",
605219edc71SAlexandre Pereira da Silva 				button->gpio);
606219edc71SAlexandre Pereira da Silva 			error = -EINVAL;
607219edc71SAlexandre Pereira da Silva 			goto err_free_pdata;
608fd05d089SDavid Jander 		}
609fd05d089SDavid Jander 
610219edc71SAlexandre Pereira da Silva 		button->desc = of_get_property(pp, "label", NULL);
611fd05d089SDavid Jander 
612219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "linux,input-type", &button->type))
613219edc71SAlexandre Pereira da Silva 			button->type = EV_KEY;
614fd05d089SDavid Jander 
615219edc71SAlexandre Pereira da Silva 		button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
616219edc71SAlexandre Pereira da Silva 
617219edc71SAlexandre Pereira da Silva 		if (of_property_read_u32(pp, "debounce-interval",
618219edc71SAlexandre Pereira da Silva 					 &button->debounce_interval))
619219edc71SAlexandre Pereira da Silva 			button->debounce_interval = 5;
620219edc71SAlexandre Pereira da Silva 	}
621219edc71SAlexandre Pereira da Silva 
622219edc71SAlexandre Pereira da Silva 	if (pdata->nbuttons == 0) {
623219edc71SAlexandre Pereira da Silva 		error = -EINVAL;
624219edc71SAlexandre Pereira da Silva 		goto err_free_pdata;
625219edc71SAlexandre Pereira da Silva 	}
626219edc71SAlexandre Pereira da Silva 
627219edc71SAlexandre Pereira da Silva 	return pdata;
628219edc71SAlexandre Pereira da Silva 
629219edc71SAlexandre Pereira da Silva err_free_pdata:
630219edc71SAlexandre Pereira da Silva 	kfree(pdata);
631219edc71SAlexandre Pereira da Silva err_out:
632219edc71SAlexandre Pereira da Silva 	return ERR_PTR(error);
633fd05d089SDavid Jander }
634fd05d089SDavid Jander 
635fd05d089SDavid Jander static struct of_device_id gpio_keys_of_match[] = {
636fd05d089SDavid Jander 	{ .compatible = "gpio-keys", },
637fd05d089SDavid Jander 	{ },
638fd05d089SDavid Jander };
639fd05d089SDavid Jander MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
640fd05d089SDavid Jander 
641fd05d089SDavid Jander #else
642fd05d089SDavid Jander 
643219edc71SAlexandre Pereira da Silva static inline struct gpio_keys_platform_data *
644219edc71SAlexandre Pereira da Silva gpio_keys_get_devtree_pdata(struct device *dev)
645fd05d089SDavid Jander {
646219edc71SAlexandre Pereira da Silva 	return ERR_PTR(-ENODEV);
647fd05d089SDavid Jander }
648fd05d089SDavid Jander 
649fd05d089SDavid Jander #endif
650fd05d089SDavid Jander 
651a16ca239SDmitry Torokhov static void gpio_remove_key(struct gpio_button_data *bdata)
652a16ca239SDmitry Torokhov {
653d8ee4a1cSLaxman Dewangan 	free_irq(bdata->irq, bdata);
654a16ca239SDmitry Torokhov 	if (bdata->timer_debounce)
655a16ca239SDmitry Torokhov 		del_timer_sync(&bdata->timer);
656a16ca239SDmitry Torokhov 	cancel_work_sync(&bdata->work);
657d8ee4a1cSLaxman Dewangan 	if (gpio_is_valid(bdata->button->gpio))
658a16ca239SDmitry Torokhov 		gpio_free(bdata->button->gpio);
659a16ca239SDmitry Torokhov }
660a16ca239SDmitry Torokhov 
6615298cc4cSBill Pemberton static int gpio_keys_probe(struct platform_device *pdev)
66278a56aabSPhil Blundell {
663db19fd8bSBen Dooks 	struct device *dev = &pdev->dev;
664219edc71SAlexandre Pereira da Silva 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
665219edc71SAlexandre Pereira da Silva 	struct gpio_keys_drvdata *ddata;
66678a56aabSPhil Blundell 	struct input_dev *input;
66778a56aabSPhil Blundell 	int i, error;
668e15b0213SAnti Sullin 	int wakeup = 0;
66978a56aabSPhil Blundell 
670fd05d089SDavid Jander 	if (!pdata) {
671219edc71SAlexandre Pereira da Silva 		pdata = gpio_keys_get_devtree_pdata(dev);
672219edc71SAlexandre Pereira da Silva 		if (IS_ERR(pdata))
673219edc71SAlexandre Pereira da Silva 			return PTR_ERR(pdata);
674fd05d089SDavid Jander 	}
675fd05d089SDavid Jander 
676a33466e3SDmitry Baryshkov 	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
677a33466e3SDmitry Baryshkov 			pdata->nbuttons * sizeof(struct gpio_button_data),
678a33466e3SDmitry Baryshkov 			GFP_KERNEL);
67978a56aabSPhil Blundell 	input = input_allocate_device();
680a33466e3SDmitry Baryshkov 	if (!ddata || !input) {
681db19fd8bSBen Dooks 		dev_err(dev, "failed to allocate state\n");
682a33466e3SDmitry Baryshkov 		error = -ENOMEM;
683a33466e3SDmitry Baryshkov 		goto fail1;
684a33466e3SDmitry Baryshkov 	}
68578a56aabSPhil Blundell 
686219edc71SAlexandre Pereira da Silva 	ddata->pdata = pdata;
6879e3af04fSMika Westerberg 	ddata->input = input;
6889e3af04fSMika Westerberg 	mutex_init(&ddata->disable_lock);
6899e3af04fSMika Westerberg 
690a33466e3SDmitry Baryshkov 	platform_set_drvdata(pdev, ddata);
691173bdd74SShubhrajyoti D 	input_set_drvdata(input, ddata);
69278a56aabSPhil Blundell 
69346711277SAlexander Stein 	input->name = pdata->name ? : pdev->name;
69478a56aabSPhil Blundell 	input->phys = "gpio-keys/input0";
695469ba4dfSDmitry Torokhov 	input->dev.parent = &pdev->dev;
696173bdd74SShubhrajyoti D 	input->open = gpio_keys_open;
697173bdd74SShubhrajyoti D 	input->close = gpio_keys_close;
69878a56aabSPhil Blundell 
69978a56aabSPhil Blundell 	input->id.bustype = BUS_HOST;
70078a56aabSPhil Blundell 	input->id.vendor = 0x0001;
70178a56aabSPhil Blundell 	input->id.product = 0x0001;
70278a56aabSPhil Blundell 	input->id.version = 0x0100;
70378a56aabSPhil Blundell 
704b67b4b11SDominic Curran 	/* Enable auto repeat feature of Linux input subsystem */
705b67b4b11SDominic Curran 	if (pdata->rep)
706b67b4b11SDominic Curran 		__set_bit(EV_REP, input->evbit);
707b67b4b11SDominic Curran 
70878a56aabSPhil Blundell 	for (i = 0; i < pdata->nbuttons; i++) {
709d9080921SDmitry Torokhov 		const struct gpio_keys_button *button = &pdata->buttons[i];
710a33466e3SDmitry Baryshkov 		struct gpio_button_data *bdata = &ddata->data[i];
71178a56aabSPhil Blundell 
712d9080921SDmitry Torokhov 		error = gpio_keys_setup_key(pdev, input, bdata, button);
713bc8f1eafSBen Dooks 		if (error)
714a33466e3SDmitry Baryshkov 			goto fail2;
71584767d00SRoman Moravcik 
716e15b0213SAnti Sullin 		if (button->wakeup)
717e15b0213SAnti Sullin 			wakeup = 1;
71878a56aabSPhil Blundell 	}
71978a56aabSPhil Blundell 
7209e3af04fSMika Westerberg 	error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7219e3af04fSMika Westerberg 	if (error) {
7229e3af04fSMika Westerberg 		dev_err(dev, "Unable to export keys/switches, error: %d\n",
7239e3af04fSMika Westerberg 			error);
7249e3af04fSMika Westerberg 		goto fail2;
7259e3af04fSMika Westerberg 	}
7269e3af04fSMika Westerberg 
72778a56aabSPhil Blundell 	error = input_register_device(input);
72878a56aabSPhil Blundell 	if (error) {
7299e3af04fSMika Westerberg 		dev_err(dev, "Unable to register input device, error: %d\n",
7309e3af04fSMika Westerberg 			error);
7319e3af04fSMika Westerberg 		goto fail3;
73278a56aabSPhil Blundell 	}
73378a56aabSPhil Blundell 
734d8ee4a1cSLaxman Dewangan 	/* get current state of buttons that are connected to GPIOs */
735d8ee4a1cSLaxman Dewangan 	for (i = 0; i < pdata->nbuttons; i++) {
736d8ee4a1cSLaxman Dewangan 		struct gpio_button_data *bdata = &ddata->data[i];
737d8ee4a1cSLaxman Dewangan 		if (gpio_is_valid(bdata->button->gpio))
738d8ee4a1cSLaxman Dewangan 			gpio_keys_gpio_report_event(bdata);
739d8ee4a1cSLaxman Dewangan 	}
7406ee88d71SDaniel Mack 	input_sync(input);
7416ee88d71SDaniel Mack 
742e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, wakeup);
743e15b0213SAnti Sullin 
74478a56aabSPhil Blundell 	return 0;
74578a56aabSPhil Blundell 
7469e3af04fSMika Westerberg  fail3:
7479e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
748a33466e3SDmitry Baryshkov  fail2:
749a16ca239SDmitry Torokhov 	while (--i >= 0)
750a16ca239SDmitry Torokhov 		gpio_remove_key(&ddata->data[i]);
75178a56aabSPhil Blundell 
752006df302SAnti Sullin 	platform_set_drvdata(pdev, NULL);
753a33466e3SDmitry Baryshkov  fail1:
75478a56aabSPhil Blundell 	input_free_device(input);
755a33466e3SDmitry Baryshkov 	kfree(ddata);
756219edc71SAlexandre Pereira da Silva 	/* If we have no platform data, we allocated pdata dynamically. */
757219edc71SAlexandre Pereira da Silva 	if (!dev_get_platdata(&pdev->dev))
758219edc71SAlexandre Pereira da Silva 		kfree(pdata);
75978a56aabSPhil Blundell 
76078a56aabSPhil Blundell 	return error;
76178a56aabSPhil Blundell }
76278a56aabSPhil Blundell 
763*e2619cf7SBill Pemberton static int gpio_keys_remove(struct platform_device *pdev)
76478a56aabSPhil Blundell {
765a33466e3SDmitry Baryshkov 	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
766a33466e3SDmitry Baryshkov 	struct input_dev *input = ddata->input;
76778a56aabSPhil Blundell 	int i;
76878a56aabSPhil Blundell 
7699e3af04fSMika Westerberg 	sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
7709e3af04fSMika Westerberg 
771e15b0213SAnti Sullin 	device_init_wakeup(&pdev->dev, 0);
772e15b0213SAnti Sullin 
773219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++)
774a16ca239SDmitry Torokhov 		gpio_remove_key(&ddata->data[i]);
77578a56aabSPhil Blundell 
77678a56aabSPhil Blundell 	input_unregister_device(input);
777fd05d089SDavid Jander 
778219edc71SAlexandre Pereira da Silva 	/* If we have no platform data, we allocated pdata dynamically. */
779219edc71SAlexandre Pereira da Silva 	if (!dev_get_platdata(&pdev->dev))
780219edc71SAlexandre Pereira da Silva 		kfree(ddata->pdata);
781fd05d089SDavid Jander 
78216382079SAxel Lin 	kfree(ddata);
78378a56aabSPhil Blundell 
78478a56aabSPhil Blundell 	return 0;
78578a56aabSPhil Blundell }
78678a56aabSPhil Blundell 
787bdda8216SDmitry Torokhov #ifdef CONFIG_PM_SLEEP
788ae78e0e0SMike Rapoport static int gpio_keys_suspend(struct device *dev)
789e15b0213SAnti Sullin {
790fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
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 		}
799e15b0213SAnti Sullin 	}
800e15b0213SAnti Sullin 
801e15b0213SAnti Sullin 	return 0;
802e15b0213SAnti Sullin }
803e15b0213SAnti Sullin 
804ae78e0e0SMike Rapoport static int gpio_keys_resume(struct device *dev)
805e15b0213SAnti Sullin {
806fd05d089SDavid Jander 	struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
807e15b0213SAnti Sullin 	int i;
808e15b0213SAnti Sullin 
809219edc71SAlexandre Pereira da Silva 	for (i = 0; i < ddata->pdata->nbuttons; i++) {
810d8ee4a1cSLaxman Dewangan 		struct gpio_button_data *bdata = &ddata->data[i];
811d8ee4a1cSLaxman Dewangan 		if (bdata->button->wakeup && device_may_wakeup(dev))
812d8ee4a1cSLaxman Dewangan 			disable_irq_wake(bdata->irq);
8136ee88d71SDaniel Mack 
814d8ee4a1cSLaxman Dewangan 		if (gpio_is_valid(bdata->button->gpio))
815d8ee4a1cSLaxman Dewangan 			gpio_keys_gpio_report_event(bdata);
816e15b0213SAnti Sullin 	}
8176ee88d71SDaniel Mack 	input_sync(ddata->input);
818e15b0213SAnti Sullin 
819e15b0213SAnti Sullin 	return 0;
820e15b0213SAnti Sullin }
821e15b0213SAnti Sullin #endif
822e15b0213SAnti Sullin 
823bdda8216SDmitry Torokhov static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
824bdda8216SDmitry Torokhov 
8259b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = {
82678a56aabSPhil Blundell 	.probe		= gpio_keys_probe,
8271cb0aa88SBill Pemberton 	.remove		= gpio_keys_remove,
82878a56aabSPhil Blundell 	.driver		= {
82978a56aabSPhil Blundell 		.name	= "gpio-keys",
830d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
831ae78e0e0SMike Rapoport 		.pm	= &gpio_keys_pm_ops,
832219edc71SAlexandre Pereira da Silva 		.of_match_table = of_match_ptr(gpio_keys_of_match),
83378a56aabSPhil Blundell 	}
83478a56aabSPhil Blundell };
83578a56aabSPhil Blundell 
83678a56aabSPhil Blundell static int __init gpio_keys_init(void)
83778a56aabSPhil Blundell {
83878a56aabSPhil Blundell 	return platform_driver_register(&gpio_keys_device_driver);
83978a56aabSPhil Blundell }
84078a56aabSPhil Blundell 
84178a56aabSPhil Blundell static void __exit gpio_keys_exit(void)
84278a56aabSPhil Blundell {
84378a56aabSPhil Blundell 	platform_driver_unregister(&gpio_keys_device_driver);
84478a56aabSPhil Blundell }
84578a56aabSPhil Blundell 
846b2330205SDavid Jander late_initcall(gpio_keys_init);
84778a56aabSPhil Blundell module_exit(gpio_keys_exit);
84878a56aabSPhil Blundell 
84978a56aabSPhil Blundell MODULE_LICENSE("GPL");
85078a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
8517e2ecdf4SDavid Jander MODULE_DESCRIPTION("Keyboard driver for GPIOs");
852d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys");
853