OSDN Git Service

Input: gpio_input: disable_irq -> disable_irq_nosync
[android-x86/kernel.git] / drivers / input / misc / gpio_input.c
1 /* drivers/input/misc/gpio_input.c
2  *
3  * Copyright (C) 2007 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio_event.h>
19 #include <linux/hrtimer.h>
20 #include <linux/input.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/wakelock.h>
24
25 enum {
26         DEBOUNCE_UNSTABLE     = BIT(0), /* Got irq, while debouncing */
27         DEBOUNCE_PRESSED      = BIT(1),
28         DEBOUNCE_NOTPRESSED   = BIT(2),
29         DEBOUNCE_WAIT_IRQ     = BIT(3), /* Stable irq state */
30         DEBOUNCE_POLL         = BIT(4), /* Stable polling state */
31
32         DEBOUNCE_UNKNOWN =
33                 DEBOUNCE_PRESSED | DEBOUNCE_NOTPRESSED,
34 };
35
36 struct gpio_key_state {
37         struct gpio_input_state *ds;
38         uint8_t debounce;
39 };
40
41 struct gpio_input_state {
42         struct gpio_event_input_devs *input_devs;
43         const struct gpio_event_input_info *info;
44         struct hrtimer timer;
45         int use_irq;
46         int debounce_count;
47         spinlock_t irq_lock;
48         struct wake_lock wake_lock;
49         struct gpio_key_state key_state[0];
50 };
51
52 static enum hrtimer_restart gpio_event_input_timer_func(struct hrtimer *timer)
53 {
54         int i;
55         int pressed;
56         struct gpio_input_state *ds =
57                 container_of(timer, struct gpio_input_state, timer);
58         unsigned gpio_flags = ds->info->flags;
59         unsigned npolarity;
60         int nkeys = ds->info->keymap_size;
61         const struct gpio_event_direct_entry *key_entry;
62         struct gpio_key_state *key_state;
63         unsigned long irqflags;
64         uint8_t debounce;
65
66 #if 0
67         key_entry = kp->keys_info->keymap;
68         key_state = kp->key_state;
69         for (i = 0; i < nkeys; i++, key_entry++, key_state++)
70                 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
71                         gpio_read_detect_status(key_entry->gpio));
72 #endif
73         key_entry = ds->info->keymap;
74         key_state = ds->key_state;
75         spin_lock_irqsave(&ds->irq_lock, irqflags);
76         for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
77                 debounce = key_state->debounce;
78                 if (debounce & DEBOUNCE_WAIT_IRQ)
79                         continue;
80                 if (key_state->debounce & DEBOUNCE_UNSTABLE) {
81                         debounce = key_state->debounce = DEBOUNCE_UNKNOWN;
82                         enable_irq(gpio_to_irq(key_entry->gpio));
83                         pr_info("gpio_keys_scan_keys: key %x-%x, %d "
84                                 "(%d) continue debounce\n",
85                                 ds->info->type, key_entry->code,
86                                 i, key_entry->gpio);
87                 }
88                 npolarity = !(gpio_flags & GPIOEDF_ACTIVE_HIGH);
89                 pressed = gpio_get_value(key_entry->gpio) ^ npolarity;
90                 if (debounce & DEBOUNCE_POLL) {
91                         if (pressed == !(debounce & DEBOUNCE_PRESSED)) {
92                                 ds->debounce_count++;
93                                 key_state->debounce = DEBOUNCE_UNKNOWN;
94                                 if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
95                                         pr_info("gpio_keys_scan_keys: key %x-"
96                                                 "%x, %d (%d) start debounce\n",
97                                                 ds->info->type, key_entry->code,
98                                                 i, key_entry->gpio);
99                         }
100                         continue;
101                 }
102                 if (pressed && (debounce & DEBOUNCE_NOTPRESSED)) {
103                         if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
104                                 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
105                                         "(%d) debounce pressed 1\n",
106                                         ds->info->type, key_entry->code,
107                                         i, key_entry->gpio);
108                         key_state->debounce = DEBOUNCE_PRESSED;
109                         continue;
110                 }
111                 if (!pressed && (debounce & DEBOUNCE_PRESSED)) {
112                         if (gpio_flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
113                                 pr_info("gpio_keys_scan_keys: key %x-%x, %d "
114                                         "(%d) debounce pressed 0\n",
115                                         ds->info->type, key_entry->code,
116                                         i, key_entry->gpio);
117                         key_state->debounce = DEBOUNCE_NOTPRESSED;
118                         continue;
119                 }
120                 /* key is stable */
121                 ds->debounce_count--;
122                 if (ds->use_irq)
123                         key_state->debounce |= DEBOUNCE_WAIT_IRQ;
124                 else
125                         key_state->debounce |= DEBOUNCE_POLL;
126                 if (gpio_flags & GPIOEDF_PRINT_KEYS)
127                         pr_info("gpio_keys_scan_keys: key %x-%x, %d (%d) "
128                                 "changed to %d\n", ds->info->type,
129                                 key_entry->code, i, key_entry->gpio, pressed);
130                 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
131                             key_entry->code, pressed);
132         }
133
134 #if 0
135         key_entry = kp->keys_info->keymap;
136         key_state = kp->key_state;
137         for (i = 0; i < nkeys; i++, key_entry++, key_state++) {
138                 pr_info("gpio_read_detect_status %d %d\n", key_entry->gpio,
139                         gpio_read_detect_status(key_entry->gpio));
140         }
141 #endif
142
143         if (ds->debounce_count)
144                 hrtimer_start(timer, ds->info->debounce_time, HRTIMER_MODE_REL);
145         else if (!ds->use_irq)
146                 hrtimer_start(timer, ds->info->poll_time, HRTIMER_MODE_REL);
147         else
148                 wake_unlock(&ds->wake_lock);
149
150         spin_unlock_irqrestore(&ds->irq_lock, irqflags);
151
152         return HRTIMER_NORESTART;
153 }
154
155 static irqreturn_t gpio_event_input_irq_handler(int irq, void *dev_id)
156 {
157         struct gpio_key_state *ks = dev_id;
158         struct gpio_input_state *ds = ks->ds;
159         int keymap_index = ks - ds->key_state;
160         const struct gpio_event_direct_entry *key_entry;
161         unsigned long irqflags;
162         int pressed;
163
164         if (!ds->use_irq)
165                 return IRQ_HANDLED;
166
167         key_entry = &ds->info->keymap[keymap_index];
168
169         if (ds->info->debounce_time.tv64) {
170                 spin_lock_irqsave(&ds->irq_lock, irqflags);
171                 if (ks->debounce & DEBOUNCE_WAIT_IRQ) {
172                         ks->debounce = DEBOUNCE_UNKNOWN;
173                         if (ds->debounce_count++ == 0) {
174                                 wake_lock(&ds->wake_lock);
175                                 hrtimer_start(
176                                         &ds->timer, ds->info->debounce_time,
177                                         HRTIMER_MODE_REL);
178                         }
179                         if (ds->info->flags & GPIOEDF_PRINT_KEY_DEBOUNCE)
180                                 pr_info("gpio_event_input_irq_handler: "
181                                         "key %x-%x, %d (%d) start debounce\n",
182                                         ds->info->type, key_entry->code,
183                                         keymap_index, key_entry->gpio);
184                 } else {
185                         disable_irq_nosync(irq);
186                         ks->debounce = DEBOUNCE_UNSTABLE;
187                 }
188                 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
189         } else {
190                 pressed = gpio_get_value(key_entry->gpio) ^
191                         !(ds->info->flags & GPIOEDF_ACTIVE_HIGH);
192                 if (ds->info->flags & GPIOEDF_PRINT_KEYS)
193                         pr_info("gpio_event_input_irq_handler: key %x-%x, %d "
194                                 "(%d) changed to %d\n",
195                                 ds->info->type, key_entry->code, keymap_index,
196                                 key_entry->gpio, pressed);
197                 input_event(ds->input_devs->dev[key_entry->dev], ds->info->type,
198                             key_entry->code, pressed);
199         }
200         return IRQ_HANDLED;
201 }
202
203 static int gpio_event_input_request_irqs(struct gpio_input_state *ds)
204 {
205         int i;
206         int err;
207         unsigned int irq;
208         unsigned long req_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
209
210         for (i = 0; i < ds->info->keymap_size; i++) {
211                 err = irq = gpio_to_irq(ds->info->keymap[i].gpio);
212                 if (err < 0)
213                         goto err_gpio_get_irq_num_failed;
214                 err = request_irq(irq, gpio_event_input_irq_handler,
215                                   req_flags, "gpio_keys", &ds->key_state[i]);
216                 if (err) {
217                         pr_err("gpio_event_input_request_irqs: request_irq "
218                                 "failed for input %d, irq %d\n",
219                                 ds->info->keymap[i].gpio, irq);
220                         goto err_request_irq_failed;
221                 }
222                 enable_irq_wake(irq);
223         }
224         return 0;
225
226         for (i = ds->info->keymap_size - 1; i >= 0; i--) {
227                 free_irq(gpio_to_irq(ds->info->keymap[i].gpio),
228                          &ds->key_state[i]);
229 err_request_irq_failed:
230 err_gpio_get_irq_num_failed:
231                 ;
232         }
233         return err;
234 }
235
236 int gpio_event_input_func(struct gpio_event_input_devs *input_devs,
237                         struct gpio_event_info *info, void **data, int func)
238 {
239         int ret;
240         int i;
241         unsigned long irqflags;
242         struct gpio_event_input_info *di;
243         struct gpio_input_state *ds = *data;
244
245         di = container_of(info, struct gpio_event_input_info, info);
246
247         if (func == GPIO_EVENT_FUNC_SUSPEND) {
248                 if (ds->use_irq)
249                         for (i = 0; i < di->keymap_size; i++)
250                                 disable_irq(gpio_to_irq(di->keymap[i].gpio));
251                 hrtimer_cancel(&ds->timer);
252                 return 0;
253         }
254         if (func == GPIO_EVENT_FUNC_RESUME) {
255                 spin_lock_irqsave(&ds->irq_lock, irqflags);
256                 if (ds->use_irq)
257                         for (i = 0; i < di->keymap_size; i++)
258                                 enable_irq(gpio_to_irq(di->keymap[i].gpio));
259                 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
260                 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
261                 return 0;
262         }
263
264         if (func == GPIO_EVENT_FUNC_INIT) {
265                 if (ktime_to_ns(di->poll_time) <= 0)
266                         di->poll_time = ktime_set(0, 20 * NSEC_PER_MSEC);
267
268                 *data = ds = kzalloc(sizeof(*ds) + sizeof(ds->key_state[0]) *
269                                         di->keymap_size, GFP_KERNEL);
270                 if (ds == NULL) {
271                         ret = -ENOMEM;
272                         pr_err("gpio_event_input_func: "
273                                 "Failed to allocate private data\n");
274                         goto err_ds_alloc_failed;
275                 }
276                 ds->debounce_count = di->keymap_size;
277                 ds->input_devs = input_devs;
278                 ds->info = di;
279                 wake_lock_init(&ds->wake_lock, WAKE_LOCK_SUSPEND, "gpio_input");
280                 spin_lock_init(&ds->irq_lock);
281
282                 for (i = 0; i < di->keymap_size; i++) {
283                         int dev = di->keymap[i].dev;
284                         if (dev >= input_devs->count) {
285                                 pr_err("gpio_event_input_func: bad device "
286                                         "index %d >= %d for key code %d\n",
287                                         dev, input_devs->count,
288                                         di->keymap[i].code);
289                                 ret = -EINVAL;
290                                 goto err_bad_keymap;
291                         }
292                         input_set_capability(input_devs->dev[dev], di->type,
293                                              di->keymap[i].code);
294                         ds->key_state[i].ds = ds;
295                         ds->key_state[i].debounce = DEBOUNCE_UNKNOWN;
296                 }
297
298                 for (i = 0; i < di->keymap_size; i++) {
299                         ret = gpio_request(di->keymap[i].gpio, "gpio_kp_in");
300                         if (ret) {
301                                 pr_err("gpio_event_input_func: gpio_request "
302                                         "failed for %d\n", di->keymap[i].gpio);
303                                 goto err_gpio_request_failed;
304                         }
305                         ret = gpio_direction_input(di->keymap[i].gpio);
306                         if (ret) {
307                                 pr_err("gpio_event_input_func: "
308                                         "gpio_direction_input failed for %d\n",
309                                         di->keymap[i].gpio);
310                                 goto err_gpio_configure_failed;
311                         }
312                 }
313
314                 ret = gpio_event_input_request_irqs(ds);
315
316                 spin_lock_irqsave(&ds->irq_lock, irqflags);
317                 ds->use_irq = ret == 0;
318
319                 pr_info("GPIO Input Driver: Start gpio inputs for %s%s in %s "
320                         "mode\n", input_devs->dev[0]->name,
321                         (input_devs->count > 1) ? "..." : "",
322                         ret == 0 ? "interrupt" : "polling");
323
324                 hrtimer_init(&ds->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
325                 ds->timer.function = gpio_event_input_timer_func;
326                 hrtimer_start(&ds->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
327                 spin_unlock_irqrestore(&ds->irq_lock, irqflags);
328                 return 0;
329         }
330
331         ret = 0;
332         spin_lock_irqsave(&ds->irq_lock, irqflags);
333         hrtimer_cancel(&ds->timer);
334         if (ds->use_irq) {
335                 for (i = di->keymap_size - 1; i >= 0; i--) {
336                         free_irq(gpio_to_irq(di->keymap[i].gpio),
337                                  &ds->key_state[i]);
338                 }
339         }
340         spin_unlock_irqrestore(&ds->irq_lock, irqflags);
341
342         for (i = di->keymap_size - 1; i >= 0; i--) {
343 err_gpio_configure_failed:
344                 gpio_free(di->keymap[i].gpio);
345 err_gpio_request_failed:
346                 ;
347         }
348 err_bad_keymap:
349         wake_lock_destroy(&ds->wake_lock);
350         kfree(ds);
351 err_ds_alloc_failed:
352         return ret;
353 }