blob: fa03a00b4c6df8948e8d6eba0033cf58bf360ba8 [file] [log] [blame]
Phil Blundell78a56aa2007-01-18 00:44:09 -05001/*
2 * Driver for keys on GPIO lines capable of generating interrupts.
3 *
4 * Copyright 2005 Phil Blundell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/version.h>
13
14#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/sched.h>
19#include <linux/pm.h>
20#include <linux/sysctl.h>
21#include <linux/proc_fs.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/input.h>
25#include <linux/irq.h>
26
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050027#include <asm/gpio.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050028#include <asm/arch/hardware.h>
29
30#include <asm/hardware/gpio_keys.h>
31
32static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
33{
34 int i;
35 struct platform_device *pdev = dev_id;
36 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
37 struct input_dev *input = platform_get_drvdata(pdev);
38
39 for (i = 0; i < pdata->nbuttons; i++) {
40 int gpio = pdata->buttons[i].gpio;
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050041 if (irq == gpio_to_irq(gpio)) {
42 int state = (gpio_get_value(gpio) ? 1 : 0) ^ (pdata->buttons[i].active_low);
Phil Blundell78a56aa2007-01-18 00:44:09 -050043
44 input_report_key(input, pdata->buttons[i].keycode, state);
45 input_sync(input);
46 }
47 }
48
49 return IRQ_HANDLED;
50}
51
52static int __devinit gpio_keys_probe(struct platform_device *pdev)
53{
54 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
55 struct input_dev *input;
56 int i, error;
57
58 input = input_allocate_device();
59 if (!input)
60 return -ENOMEM;
61
62 platform_set_drvdata(pdev, input);
63
64 input->evbit[0] = BIT(EV_KEY);
65
66 input->name = pdev->name;
67 input->phys = "gpio-keys/input0";
68 input->cdev.dev = &pdev->dev;
69 input->private = pdata;
70
71 input->id.bustype = BUS_HOST;
72 input->id.vendor = 0x0001;
73 input->id.product = 0x0001;
74 input->id.version = 0x0100;
75
76 for (i = 0; i < pdata->nbuttons; i++) {
77 int code = pdata->buttons[i].keycode;
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050078 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -050079
80 set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
Thomas Gleixner2db63462007-02-14 00:33:20 -080081 error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM,
Phil Blundell78a56aa2007-01-18 00:44:09 -050082 pdata->buttons[i].desc ? pdata->buttons[i].desc : "gpio_keys",
83 pdev);
84 if (error) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050085 printk(KERN_ERR "gpio-keys: unable to claim irq %d; error %d\n",
86 irq, error);
Phil Blundell78a56aa2007-01-18 00:44:09 -050087 goto fail;
88 }
89 set_bit(code, input->keybit);
90 }
91
92 error = input_register_device(input);
93 if (error) {
94 printk(KERN_ERR "Unable to register gpio-keys input device\n");
95 goto fail;
96 }
97
98 return 0;
99
100 fail:
101 for (i = i - 1; i >= 0; i--)
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500102 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500103
104 input_free_device(input);
105
106 return error;
107}
108
109static int __devexit gpio_keys_remove(struct platform_device *pdev)
110{
111 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
112 struct input_dev *input = platform_get_drvdata(pdev);
113 int i;
114
115 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500116 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500117 free_irq(irq, pdev);
118 }
119
120 input_unregister_device(input);
121
122 return 0;
123}
124
125struct platform_driver gpio_keys_device_driver = {
126 .probe = gpio_keys_probe,
127 .remove = __devexit_p(gpio_keys_remove),
128 .driver = {
129 .name = "gpio-keys",
130 }
131};
132
133static int __init gpio_keys_init(void)
134{
135 return platform_driver_register(&gpio_keys_device_driver);
136}
137
138static void __exit gpio_keys_exit(void)
139{
140 platform_driver_unregister(&gpio_keys_device_driver);
141}
142
143module_init(gpio_keys_init);
144module_exit(gpio_keys_exit);
145
146MODULE_LICENSE("GPL");
147MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
148MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");