gpio_keys.c 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Driver for keys on GPIO lines capable of generating interrupts.
 *
 * Copyright 2005 Phil Blundell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
24
#include <linux/gpio_keys.h>
25
#include <linux/workqueue.h>
26

27
#include <asm/gpio.h>
28

29 30 31
struct gpio_button_data {
	struct gpio_keys_button *button;
	struct input_dev *input;
32
	struct timer_list timer;
33
	struct work_struct work;
34 35 36 37 38 39 40
};

struct gpio_keys_drvdata {
	struct input_dev *input;
	struct gpio_button_data data[0];
};

41
static void gpio_keys_report_event(struct work_struct *work)
42
{
43 44
	struct gpio_button_data *bdata =
		container_of(work, struct gpio_button_data, work);
45 46
	struct gpio_keys_button *button = bdata->button;
	struct input_dev *input = bdata->input;
47 48 49 50 51 52 53
	unsigned int type = button->type ?: EV_KEY;
	int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;

	input_event(input, type, button->code, !!state);
	input_sync(input);
}

54
static void gpio_keys_timer(unsigned long _data)
55 56 57
{
	struct gpio_button_data *data = (struct gpio_button_data *)_data;

58
	schedule_work(&data->work);
59 60
}

61 62
static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
{
63 64
	struct gpio_button_data *bdata = dev_id;
	struct gpio_keys_button *button = bdata->button;
65

66
	BUG_ON(irq != gpio_to_irq(button->gpio));
67

68 69 70 71
	if (button->debounce_interval)
		mod_timer(&bdata->timer,
			jiffies + msecs_to_jiffies(button->debounce_interval));
	else
72
		schedule_work(&bdata->work);
73

74
	return IRQ_HANDLED;
75 76 77 78 79
}

static int __devinit gpio_keys_probe(struct platform_device *pdev)
{
	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
80
	struct gpio_keys_drvdata *ddata;
81
	struct device *dev = &pdev->dev;
82 83
	struct input_dev *input;
	int i, error;
84
	int wakeup = 0;
85

86 87 88
	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
			pdata->nbuttons * sizeof(struct gpio_button_data),
			GFP_KERNEL);
89
	input = input_allocate_device();
90
	if (!ddata || !input) {
91
		dev_err(dev, "failed to allocate state\n");
92 93 94
		error = -ENOMEM;
		goto fail1;
	}
95

96
	platform_set_drvdata(pdev, ddata);
97 98 99

	input->name = pdev->name;
	input->phys = "gpio-keys/input0";
100
	input->dev.parent = &pdev->dev;
101 102 103 104 105 106

	input->id.bustype = BUS_HOST;
	input->id.vendor = 0x0001;
	input->id.product = 0x0001;
	input->id.version = 0x0100;

107 108 109 110
	/* Enable auto repeat feature of Linux input subsystem */
	if (pdata->rep)
		__set_bit(EV_REP, input->evbit);

111 112
	ddata->input = input;

113
	for (i = 0; i < pdata->nbuttons; i++) {
114
		struct gpio_keys_button *button = &pdata->buttons[i];
115
		struct gpio_button_data *bdata = &ddata->data[i];
116
		int irq;
117
		unsigned int type = button->type ?: EV_KEY;
118

119
		bdata->input = input;
120
		bdata->button = button;
121
		setup_timer(&bdata->timer,
122 123
			    gpio_keys_timer, (unsigned long)bdata);
		INIT_WORK(&bdata->work, gpio_keys_report_event);
124

125 126
		error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
		if (error < 0) {
127 128
			dev_err(dev, "failed to request GPIO %d, error %d\n",
				button->gpio, error);
129
			goto fail2;
130 131 132 133
		}

		error = gpio_direction_input(button->gpio);
		if (error < 0) {
134
			dev_err(dev, "failed to configure"
135 136 137
				" direction for GPIO %d, error %d\n",
				button->gpio, error);
			gpio_free(button->gpio);
138
			goto fail2;
139 140 141
		}

		irq = gpio_to_irq(button->gpio);
142 143
		if (irq < 0) {
			error = irq;
144 145
			dev_err(dev, "Unable to get irq number "
				"for GPIO %d, error %d\n",
146
				button->gpio, error);
147
			gpio_free(button->gpio);
148
			goto fail2;
149 150 151
		}

		error = request_irq(irq, gpio_keys_isr,
152
				    IRQF_SHARED |
153
				    IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
154
				    button->desc ? button->desc : "gpio_keys",
155
				    bdata);
156
		if (error) {
157
			dev_err(dev, "Unable to claim irq %d; error %d\n",
158
				irq, error);
159
			gpio_free(button->gpio);
160
			goto fail2;
161
		}
162

163 164 165
		if (button->wakeup)
			wakeup = 1;

166
		input_set_capability(input, type, button->code);
167 168 169 170
	}

	error = input_register_device(input);
	if (error) {
171
		dev_err(dev, "Unable to register input device, "
172
			"error: %d\n", error);
173
		goto fail2;
174 175
	}

176 177
	device_init_wakeup(&pdev->dev, wakeup);

178 179
	return 0;

180
 fail2:
181
	while (--i >= 0) {
182
		free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
183 184
		if (pdata->buttons[i].debounce_interval)
			del_timer_sync(&ddata->data[i].timer);
185
		cancel_work_sync(&ddata->data[i].work);
186 187
		gpio_free(pdata->buttons[i].gpio);
	}
188

189
	platform_set_drvdata(pdev, NULL);
190
 fail1:
191
	input_free_device(input);
192
	kfree(ddata);
193 194 195 196 197 198 199

	return error;
}

static int __devexit gpio_keys_remove(struct platform_device *pdev)
{
	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
200 201
	struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
	struct input_dev *input = ddata->input;
202 203
	int i;

204 205
	device_init_wakeup(&pdev->dev, 0);

206
	for (i = 0; i < pdata->nbuttons; i++) {
207
		int irq = gpio_to_irq(pdata->buttons[i].gpio);
208
		free_irq(irq, &ddata->data[i]);
209 210
		if (pdata->buttons[i].debounce_interval)
			del_timer_sync(&ddata->data[i].timer);
211
		cancel_work_sync(&ddata->data[i].work);
212
		gpio_free(pdata->buttons[i].gpio);
213 214 215 216 217 218 219
	}

	input_unregister_device(input);

	return 0;
}

220 221

#ifdef CONFIG_PM
222
static int gpio_keys_suspend(struct device *dev)
223
{
224
	struct platform_device *pdev = to_platform_device(dev);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
	int i;

	if (device_may_wakeup(&pdev->dev)) {
		for (i = 0; i < pdata->nbuttons; i++) {
			struct gpio_keys_button *button = &pdata->buttons[i];
			if (button->wakeup) {
				int irq = gpio_to_irq(button->gpio);
				enable_irq_wake(irq);
			}
		}
	}

	return 0;
}

241
static int gpio_keys_resume(struct device *dev)
242
{
243
	struct platform_device *pdev = to_platform_device(dev);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
	int i;

	if (device_may_wakeup(&pdev->dev)) {
		for (i = 0; i < pdata->nbuttons; i++) {
			struct gpio_keys_button *button = &pdata->buttons[i];
			if (button->wakeup) {
				int irq = gpio_to_irq(button->gpio);
				disable_irq_wake(irq);
			}
		}
	}

	return 0;
}
259 260 261 262 263

static const struct dev_pm_ops gpio_keys_pm_ops = {
	.suspend	= gpio_keys_suspend,
	.resume		= gpio_keys_resume,
};
264 265
#endif

266
static struct platform_driver gpio_keys_device_driver = {
267 268 269 270
	.probe		= gpio_keys_probe,
	.remove		= __devexit_p(gpio_keys_remove),
	.driver		= {
		.name	= "gpio-keys",
271
		.owner	= THIS_MODULE,
272 273 274
#ifdef CONFIG_PM
		.pm	= &gpio_keys_pm_ops,
#endif
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	}
};

static int __init gpio_keys_init(void)
{
	return platform_driver_register(&gpio_keys_device_driver);
}

static void __exit gpio_keys_exit(void)
{
	platform_driver_unregister(&gpio_keys_device_driver);
}

module_init(gpio_keys_init);
module_exit(gpio_keys_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
294
MODULE_ALIAS("platform:gpio-keys");