aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/keyboard/gpio_keys.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/keyboard/gpio_keys.c')
-rw-r--r--drivers/input/keyboard/gpio_keys.c134
1 files changed, 81 insertions, 53 deletions
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index 77d130914259..1aff3b76effd 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -23,8 +23,7 @@
23#include <linux/input.h> 23#include <linux/input.h>
24#include <linux/gpio_keys.h> 24#include <linux/gpio_keys.h>
25#include <linux/workqueue.h> 25#include <linux/workqueue.h>
26 26#include <linux/gpio.h>
27#include <asm/gpio.h>
28 27
29struct gpio_button_data { 28struct gpio_button_data {
30 struct gpio_keys_button *button; 29 struct gpio_keys_button *button;
@@ -38,10 +37,8 @@ struct gpio_keys_drvdata {
38 struct gpio_button_data data[0]; 37 struct gpio_button_data data[0];
39}; 38};
40 39
41static void gpio_keys_report_event(struct work_struct *work) 40static void gpio_keys_report_event(struct gpio_button_data *bdata)
42{ 41{
43 struct gpio_button_data *bdata =
44 container_of(work, struct gpio_button_data, work);
45 struct gpio_keys_button *button = bdata->button; 42 struct gpio_keys_button *button = bdata->button;
46 struct input_dev *input = bdata->input; 43 struct input_dev *input = bdata->input;
47 unsigned int type = button->type ?: EV_KEY; 44 unsigned int type = button->type ?: EV_KEY;
@@ -51,6 +48,14 @@ static void gpio_keys_report_event(struct work_struct *work)
51 input_sync(input); 48 input_sync(input);
52} 49}
53 50
51static void gpio_keys_work_func(struct work_struct *work)
52{
53 struct gpio_button_data *bdata =
54 container_of(work, struct gpio_button_data, work);
55
56 gpio_keys_report_event(bdata);
57}
58
54static void gpio_keys_timer(unsigned long _data) 59static void gpio_keys_timer(unsigned long _data)
55{ 60{
56 struct gpio_button_data *data = (struct gpio_button_data *)_data; 61 struct gpio_button_data *data = (struct gpio_button_data *)_data;
@@ -74,10 +79,62 @@ static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
74 return IRQ_HANDLED; 79 return IRQ_HANDLED;
75} 80}
76 81
82static int __devinit gpio_keys_setup_key(struct device *dev,
83 struct gpio_button_data *bdata,
84 struct gpio_keys_button *button)
85{
86 char *desc = button->desc ? button->desc : "gpio_keys";
87 int irq, error;
88
89 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
90 INIT_WORK(&bdata->work, gpio_keys_work_func);
91
92 error = gpio_request(button->gpio, desc);
93 if (error < 0) {
94 dev_err(dev, "failed to request GPIO %d, error %d\n",
95 button->gpio, error);
96 goto fail2;
97 }
98
99 error = gpio_direction_input(button->gpio);
100 if (error < 0) {
101 dev_err(dev, "failed to configure"
102 " direction for GPIO %d, error %d\n",
103 button->gpio, error);
104 goto fail3;
105 }
106
107 irq = gpio_to_irq(button->gpio);
108 if (irq < 0) {
109 error = irq;
110 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
111 button->gpio, error);
112 goto fail3;
113 }
114
115 error = request_irq(irq, gpio_keys_isr,
116 IRQF_SHARED |
117 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
118 desc, bdata);
119 if (error) {
120 dev_err(dev, "Unable to claim irq %d; error %d\n",
121 irq, error);
122 goto fail3;
123 }
124
125 return 0;
126
127fail3:
128 gpio_free(button->gpio);
129fail2:
130 return error;
131}
132
77static int __devinit gpio_keys_probe(struct platform_device *pdev) 133static int __devinit gpio_keys_probe(struct platform_device *pdev)
78{ 134{
79 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 135 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
80 struct gpio_keys_drvdata *ddata; 136 struct gpio_keys_drvdata *ddata;
137 struct device *dev = &pdev->dev;
81 struct input_dev *input; 138 struct input_dev *input;
82 int i, error; 139 int i, error;
83 int wakeup = 0; 140 int wakeup = 0;
@@ -87,6 +144,7 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
87 GFP_KERNEL); 144 GFP_KERNEL);
88 input = input_allocate_device(); 145 input = input_allocate_device();
89 if (!ddata || !input) { 146 if (!ddata || !input) {
147 dev_err(dev, "failed to allocate state\n");
90 error = -ENOMEM; 148 error = -ENOMEM;
91 goto fail1; 149 goto fail1;
92 } 150 }
@@ -111,52 +169,14 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
111 for (i = 0; i < pdata->nbuttons; i++) { 169 for (i = 0; i < pdata->nbuttons; i++) {
112 struct gpio_keys_button *button = &pdata->buttons[i]; 170 struct gpio_keys_button *button = &pdata->buttons[i];
113 struct gpio_button_data *bdata = &ddata->data[i]; 171 struct gpio_button_data *bdata = &ddata->data[i];
114 int irq;
115 unsigned int type = button->type ?: EV_KEY; 172 unsigned int type = button->type ?: EV_KEY;
116 173
117 bdata->input = input; 174 bdata->input = input;
118 bdata->button = button; 175 bdata->button = button;
119 setup_timer(&bdata->timer,
120 gpio_keys_timer, (unsigned long)bdata);
121 INIT_WORK(&bdata->work, gpio_keys_report_event);
122
123 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
124 if (error < 0) {
125 pr_err("gpio-keys: failed to request GPIO %d,"
126 " error %d\n", button->gpio, error);
127 goto fail2;
128 }
129
130 error = gpio_direction_input(button->gpio);
131 if (error < 0) {
132 pr_err("gpio-keys: failed to configure input"
133 " direction for GPIO %d, error %d\n",
134 button->gpio, error);
135 gpio_free(button->gpio);
136 goto fail2;
137 }
138
139 irq = gpio_to_irq(button->gpio);
140 if (irq < 0) {
141 error = irq;
142 pr_err("gpio-keys: Unable to get irq number"
143 " for GPIO %d, error %d\n",
144 button->gpio, error);
145 gpio_free(button->gpio);
146 goto fail2;
147 }
148 176
149 error = request_irq(irq, gpio_keys_isr, 177 error = gpio_keys_setup_key(dev, bdata, button);
150 IRQF_SHARED | 178 if (error)
151 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
152 button->desc ? button->desc : "gpio_keys",
153 bdata);
154 if (error) {
155 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
156 irq, error);
157 gpio_free(button->gpio);
158 goto fail2; 179 goto fail2;
159 }
160 180
161 if (button->wakeup) 181 if (button->wakeup)
162 wakeup = 1; 182 wakeup = 1;
@@ -166,11 +186,16 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
166 186
167 error = input_register_device(input); 187 error = input_register_device(input);
168 if (error) { 188 if (error) {
169 pr_err("gpio-keys: Unable to register input device, " 189 dev_err(dev, "Unable to register input device, "
170 "error: %d\n", error); 190 "error: %d\n", error);
171 goto fail2; 191 goto fail2;
172 } 192 }
173 193
194 /* get current state of buttons */
195 for (i = 0; i < pdata->nbuttons; i++)
196 gpio_keys_report_event(&ddata->data[i]);
197 input_sync(input);
198
174 device_init_wakeup(&pdev->dev, wakeup); 199 device_init_wakeup(&pdev->dev, wakeup);
175 200
176 return 0; 201 return 0;
@@ -239,18 +264,21 @@ static int gpio_keys_suspend(struct device *dev)
239static int gpio_keys_resume(struct device *dev) 264static int gpio_keys_resume(struct device *dev)
240{ 265{
241 struct platform_device *pdev = to_platform_device(dev); 266 struct platform_device *pdev = to_platform_device(dev);
267 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
242 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 268 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
243 int i; 269 int i;
244 270
245 if (device_may_wakeup(&pdev->dev)) { 271 for (i = 0; i < pdata->nbuttons; i++) {
246 for (i = 0; i < pdata->nbuttons; i++) { 272
247 struct gpio_keys_button *button = &pdata->buttons[i]; 273 struct gpio_keys_button *button = &pdata->buttons[i];
248 if (button->wakeup) { 274 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
249 int irq = gpio_to_irq(button->gpio); 275 int irq = gpio_to_irq(button->gpio);
250 disable_irq_wake(irq); 276 disable_irq_wake(irq);
251 }
252 } 277 }
278
279 gpio_keys_report_event(&ddata->data[i]);
253 } 280 }
281 input_sync(ddata->input);
254 282
255 return 0; 283 return 0;
256} 284}