include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / keyboard / gpio_keys.c
CommitLineData
78a56aab
PB
1/*
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>
78a56aab
PB
12
13#include <linux/init.h>
14#include <linux/fs.h>
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/sched.h>
18#include <linux/pm.h>
5a0e3ad6 19#include <linux/slab.h>
78a56aab
PB
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>
49015bee 25#include <linux/gpio_keys.h>
da0d03fe 26#include <linux/workqueue.h>
111bc59c 27#include <linux/gpio.h>
78a56aab 28
a33466e3
DES
29struct gpio_button_data {
30 struct gpio_keys_button *button;
31 struct input_dev *input;
ca865a77 32 struct timer_list timer;
da0d03fe 33 struct work_struct work;
9e3af04f 34 bool disabled;
a33466e3
DES
35};
36
37struct gpio_keys_drvdata {
38 struct input_dev *input;
9e3af04f
MW
39 struct mutex disable_lock;
40 unsigned int n_buttons;
a33466e3
DES
41 struct gpio_button_data data[0];
42};
43
9e3af04f
MW
44/*
45 * SYSFS interface for enabling/disabling keys and switches:
46 *
47 * There are 4 attributes under /sys/devices/platform/gpio-keys/
48 * keys [ro] - bitmap of keys (EV_KEY) which can be
49 * disabled
50 * switches [ro] - bitmap of switches (EV_SW) which can be
51 * disabled
52 * disabled_keys [rw] - bitmap of keys currently disabled
53 * disabled_switches [rw] - bitmap of switches currently disabled
54 *
55 * Userland can change these values and hence disable event generation
56 * for each key (or switch). Disabling a key means its interrupt line
57 * is disabled.
58 *
59 * For example, if we have following switches set up as gpio-keys:
60 * SW_DOCK = 5
61 * SW_CAMERA_LENS_COVER = 9
62 * SW_KEYPAD_SLIDE = 10
63 * SW_FRONT_PROXIMITY = 11
64 * This is read from switches:
65 * 11-9,5
66 * Next we want to disable proximity (11) and dock (5), we write:
67 * 11,5
68 * to file disabled_switches. Now proximity and dock IRQs are disabled.
69 * This can be verified by reading the file disabled_switches:
70 * 11,5
71 * If we now want to enable proximity (11) switch we write:
72 * 5
73 * to disabled_switches.
74 *
75 * We can disable only those keys which don't allow sharing the irq.
76 */
77
78/**
79 * get_n_events_by_type() - returns maximum number of events per @type
80 * @type: type of button (%EV_KEY, %EV_SW)
81 *
82 * Return value of this function can be used to allocate bitmap
83 * large enough to hold all bits for given type.
84 */
85static inline int get_n_events_by_type(int type)
86{
87 BUG_ON(type != EV_SW && type != EV_KEY);
88
89 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
90}
91
92/**
93 * gpio_keys_disable_button() - disables given GPIO button
94 * @bdata: button data for button to be disabled
95 *
96 * Disables button pointed by @bdata. This is done by masking
97 * IRQ line. After this function is called, button won't generate
98 * input events anymore. Note that one can only disable buttons
99 * that don't share IRQs.
100 *
101 * Make sure that @bdata->disable_lock is locked when entering
102 * this function to avoid races when concurrent threads are
103 * disabling buttons at the same time.
104 */
105static void gpio_keys_disable_button(struct gpio_button_data *bdata)
106{
107 if (!bdata->disabled) {
108 /*
109 * Disable IRQ and possible debouncing timer.
110 */
111 disable_irq(gpio_to_irq(bdata->button->gpio));
112 if (bdata->button->debounce_interval)
113 del_timer_sync(&bdata->timer);
114
115 bdata->disabled = true;
116 }
117}
118
119/**
120 * gpio_keys_enable_button() - enables given GPIO button
121 * @bdata: button data for button to be disabled
122 *
123 * Enables given button pointed by @bdata.
124 *
125 * Make sure that @bdata->disable_lock is locked when entering
126 * this function to avoid races with concurrent threads trying
127 * to enable the same button at the same time.
128 */
129static void gpio_keys_enable_button(struct gpio_button_data *bdata)
130{
131 if (bdata->disabled) {
132 enable_irq(gpio_to_irq(bdata->button->gpio));
133 bdata->disabled = false;
134 }
135}
136
137/**
138 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
139 * @ddata: pointer to drvdata
140 * @buf: buffer where stringified bitmap is written
141 * @type: button type (%EV_KEY, %EV_SW)
142 * @only_disabled: does caller want only those buttons that are
143 * currently disabled or all buttons that can be
144 * disabled
145 *
146 * This function writes buttons that can be disabled to @buf. If
147 * @only_disabled is true, then @buf contains only those buttons
148 * that are currently disabled. Returns 0 on success or negative
149 * errno on failure.
150 */
151static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
152 char *buf, unsigned int type,
153 bool only_disabled)
154{
155 int n_events = get_n_events_by_type(type);
156 unsigned long *bits;
157 ssize_t ret;
158 int i;
159
160 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
161 if (!bits)
162 return -ENOMEM;
163
164 for (i = 0; i < ddata->n_buttons; i++) {
165 struct gpio_button_data *bdata = &ddata->data[i];
166
167 if (bdata->button->type != type)
168 continue;
169
170 if (only_disabled && !bdata->disabled)
171 continue;
172
173 __set_bit(bdata->button->code, bits);
174 }
175
176 ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
177 buf[ret++] = '\n';
178 buf[ret] = '\0';
179
180 kfree(bits);
181
182 return ret;
183}
184
185/**
186 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
187 * @ddata: pointer to drvdata
188 * @buf: buffer from userspace that contains stringified bitmap
189 * @type: button type (%EV_KEY, %EV_SW)
190 *
191 * This function parses stringified bitmap from @buf and disables/enables
192 * GPIO buttons accordinly. Returns 0 on success and negative error
193 * on failure.
194 */
195static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
196 const char *buf, unsigned int type)
197{
198 int n_events = get_n_events_by_type(type);
199 unsigned long *bits;
200 ssize_t error;
201 int i;
202
203 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
204 if (!bits)
205 return -ENOMEM;
206
207 error = bitmap_parselist(buf, bits, n_events);
208 if (error)
209 goto out;
210
211 /* First validate */
212 for (i = 0; i < ddata->n_buttons; i++) {
213 struct gpio_button_data *bdata = &ddata->data[i];
214
215 if (bdata->button->type != type)
216 continue;
217
218 if (test_bit(bdata->button->code, bits) &&
219 !bdata->button->can_disable) {
220 error = -EINVAL;
221 goto out;
222 }
223 }
224
225 mutex_lock(&ddata->disable_lock);
226
227 for (i = 0; i < ddata->n_buttons; i++) {
228 struct gpio_button_data *bdata = &ddata->data[i];
229
230 if (bdata->button->type != type)
231 continue;
232
233 if (test_bit(bdata->button->code, bits))
234 gpio_keys_disable_button(bdata);
235 else
236 gpio_keys_enable_button(bdata);
237 }
238
239 mutex_unlock(&ddata->disable_lock);
240
241out:
242 kfree(bits);
243 return error;
244}
245
246#define ATTR_SHOW_FN(name, type, only_disabled) \
247static ssize_t gpio_keys_show_##name(struct device *dev, \
248 struct device_attribute *attr, \
249 char *buf) \
250{ \
251 struct platform_device *pdev = to_platform_device(dev); \
252 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
253 \
254 return gpio_keys_attr_show_helper(ddata, buf, \
255 type, only_disabled); \
256}
257
258ATTR_SHOW_FN(keys, EV_KEY, false);
259ATTR_SHOW_FN(switches, EV_SW, false);
260ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
261ATTR_SHOW_FN(disabled_switches, EV_SW, true);
262
263/*
264 * ATTRIBUTES:
265 *
266 * /sys/devices/platform/gpio-keys/keys [ro]
267 * /sys/devices/platform/gpio-keys/switches [ro]
268 */
269static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
270static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
271
272#define ATTR_STORE_FN(name, type) \
273static ssize_t gpio_keys_store_##name(struct device *dev, \
274 struct device_attribute *attr, \
275 const char *buf, \
276 size_t count) \
277{ \
278 struct platform_device *pdev = to_platform_device(dev); \
279 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
280 ssize_t error; \
281 \
282 error = gpio_keys_attr_store_helper(ddata, buf, type); \
283 if (error) \
284 return error; \
285 \
286 return count; \
287}
288
289ATTR_STORE_FN(disabled_keys, EV_KEY);
290ATTR_STORE_FN(disabled_switches, EV_SW);
291
292/*
293 * ATTRIBUTES:
294 *
295 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
296 * /sys/devices/platform/gpio-keys/disables_switches [rw]
297 */
298static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
299 gpio_keys_show_disabled_keys,
300 gpio_keys_store_disabled_keys);
301static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
302 gpio_keys_show_disabled_switches,
303 gpio_keys_store_disabled_switches);
304
305static struct attribute *gpio_keys_attrs[] = {
306 &dev_attr_keys.attr,
307 &dev_attr_switches.attr,
308 &dev_attr_disabled_keys.attr,
309 &dev_attr_disabled_switches.attr,
310 NULL,
311};
312
313static struct attribute_group gpio_keys_attr_group = {
314 .attrs = gpio_keys_attrs,
315};
316
6ee88d71 317static void gpio_keys_report_event(struct gpio_button_data *bdata)
a33466e3 318{
ce25d7e9
UKK
319 struct gpio_keys_button *button = bdata->button;
320 struct input_dev *input = bdata->input;
a33466e3
DES
321 unsigned int type = button->type ?: EV_KEY;
322 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
323
324 input_event(input, type, button->code, !!state);
325 input_sync(input);
326}
327
6ee88d71
DM
328static void gpio_keys_work_func(struct work_struct *work)
329{
330 struct gpio_button_data *bdata =
331 container_of(work, struct gpio_button_data, work);
332
333 gpio_keys_report_event(bdata);
334}
335
da0d03fe 336static void gpio_keys_timer(unsigned long _data)
ca865a77
JN
337{
338 struct gpio_button_data *data = (struct gpio_button_data *)_data;
339
da0d03fe 340 schedule_work(&data->work);
ca865a77
JN
341}
342
78a56aab
PB
343static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
344{
57ffe9d5
UKK
345 struct gpio_button_data *bdata = dev_id;
346 struct gpio_keys_button *button = bdata->button;
78a56aab 347
57ffe9d5 348 BUG_ON(irq != gpio_to_irq(button->gpio));
84767d00 349
ca865a77
JN
350 if (button->debounce_interval)
351 mod_timer(&bdata->timer,
352 jiffies + msecs_to_jiffies(button->debounce_interval));
353 else
da0d03fe 354 schedule_work(&bdata->work);
78a56aab 355
57ffe9d5 356 return IRQ_HANDLED;
78a56aab
PB
357}
358
9e3af04f 359static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
bc8f1eaf
BD
360 struct gpio_button_data *bdata,
361 struct gpio_keys_button *button)
362{
363 char *desc = button->desc ? button->desc : "gpio_keys";
9e3af04f
MW
364 struct device *dev = &pdev->dev;
365 unsigned long irqflags;
bc8f1eaf
BD
366 int irq, error;
367
368 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
6ee88d71 369 INIT_WORK(&bdata->work, gpio_keys_work_func);
bc8f1eaf
BD
370
371 error = gpio_request(button->gpio, desc);
372 if (error < 0) {
373 dev_err(dev, "failed to request GPIO %d, error %d\n",
374 button->gpio, error);
375 goto fail2;
376 }
377
378 error = gpio_direction_input(button->gpio);
379 if (error < 0) {
380 dev_err(dev, "failed to configure"
381 " direction for GPIO %d, error %d\n",
382 button->gpio, error);
383 goto fail3;
384 }
385
386 irq = gpio_to_irq(button->gpio);
387 if (irq < 0) {
388 error = irq;
389 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
390 button->gpio, error);
391 goto fail3;
392 }
393
9e3af04f
MW
394 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
395 /*
396 * If platform has specified that the button can be disabled,
397 * we don't want it to share the interrupt line.
398 */
399 if (!button->can_disable)
400 irqflags |= IRQF_SHARED;
401
402 error = request_irq(irq, gpio_keys_isr, irqflags, desc, bdata);
bc8f1eaf
BD
403 if (error) {
404 dev_err(dev, "Unable to claim irq %d; error %d\n",
405 irq, error);
406 goto fail3;
407 }
408
409 return 0;
410
411fail3:
412 gpio_free(button->gpio);
413fail2:
414 return error;
415}
416
78a56aab
PB
417static int __devinit gpio_keys_probe(struct platform_device *pdev)
418{
419 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
a33466e3 420 struct gpio_keys_drvdata *ddata;
db19fd8b 421 struct device *dev = &pdev->dev;
78a56aab
PB
422 struct input_dev *input;
423 int i, error;
e15b0213 424 int wakeup = 0;
78a56aab 425
a33466e3
DES
426 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
427 pdata->nbuttons * sizeof(struct gpio_button_data),
428 GFP_KERNEL);
78a56aab 429 input = input_allocate_device();
a33466e3 430 if (!ddata || !input) {
db19fd8b 431 dev_err(dev, "failed to allocate state\n");
a33466e3
DES
432 error = -ENOMEM;
433 goto fail1;
434 }
78a56aab 435
9e3af04f
MW
436 ddata->input = input;
437 ddata->n_buttons = pdata->nbuttons;
438 mutex_init(&ddata->disable_lock);
439
a33466e3 440 platform_set_drvdata(pdev, ddata);
78a56aab
PB
441
442 input->name = pdev->name;
443 input->phys = "gpio-keys/input0";
469ba4df 444 input->dev.parent = &pdev->dev;
78a56aab
PB
445
446 input->id.bustype = BUS_HOST;
447 input->id.vendor = 0x0001;
448 input->id.product = 0x0001;
449 input->id.version = 0x0100;
450
b67b4b11
DC
451 /* Enable auto repeat feature of Linux input subsystem */
452 if (pdata->rep)
453 __set_bit(EV_REP, input->evbit);
454
78a56aab 455 for (i = 0; i < pdata->nbuttons; i++) {
84767d00 456 struct gpio_keys_button *button = &pdata->buttons[i];
a33466e3 457 struct gpio_button_data *bdata = &ddata->data[i];
84767d00 458 unsigned int type = button->type ?: EV_KEY;
78a56aab 459
a33466e3 460 bdata->input = input;
74dd4393 461 bdata->button = button;
bc8f1eaf 462
9e3af04f 463 error = gpio_keys_setup_key(pdev, bdata, button);
bc8f1eaf 464 if (error)
a33466e3 465 goto fail2;
84767d00 466
e15b0213
AS
467 if (button->wakeup)
468 wakeup = 1;
469
84767d00 470 input_set_capability(input, type, button->code);
78a56aab
PB
471 }
472
9e3af04f 473 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
78a56aab 474 if (error) {
9e3af04f
MW
475 dev_err(dev, "Unable to export keys/switches, error: %d\n",
476 error);
a33466e3 477 goto fail2;
78a56aab
PB
478 }
479
9e3af04f
MW
480 error = input_register_device(input);
481 if (error) {
482 dev_err(dev, "Unable to register input device, error: %d\n",
483 error);
484 goto fail3;
485 }
486
6ee88d71
DM
487 /* get current state of buttons */
488 for (i = 0; i < pdata->nbuttons; i++)
489 gpio_keys_report_event(&ddata->data[i]);
490 input_sync(input);
491
e15b0213
AS
492 device_init_wakeup(&pdev->dev, wakeup);
493
78a56aab
PB
494 return 0;
495
9e3af04f
MW
496 fail3:
497 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
a33466e3 498 fail2:
6a2e3911 499 while (--i >= 0) {
57ffe9d5 500 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
ca865a77
JN
501 if (pdata->buttons[i].debounce_interval)
502 del_timer_sync(&ddata->data[i].timer);
da0d03fe 503 cancel_work_sync(&ddata->data[i].work);
6a2e3911
HVR
504 gpio_free(pdata->buttons[i].gpio);
505 }
78a56aab 506
006df302 507 platform_set_drvdata(pdev, NULL);
a33466e3 508 fail1:
78a56aab 509 input_free_device(input);
a33466e3 510 kfree(ddata);
78a56aab
PB
511
512 return error;
513}
514
515static int __devexit gpio_keys_remove(struct platform_device *pdev)
516{
517 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
a33466e3
DES
518 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
519 struct input_dev *input = ddata->input;
78a56aab
PB
520 int i;
521
9e3af04f
MW
522 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
523
e15b0213
AS
524 device_init_wakeup(&pdev->dev, 0);
525
78a56aab 526 for (i = 0; i < pdata->nbuttons; i++) {
0d98f6bb 527 int irq = gpio_to_irq(pdata->buttons[i].gpio);
57ffe9d5 528 free_irq(irq, &ddata->data[i]);
ca865a77
JN
529 if (pdata->buttons[i].debounce_interval)
530 del_timer_sync(&ddata->data[i].timer);
da0d03fe 531 cancel_work_sync(&ddata->data[i].work);
6a2e3911 532 gpio_free(pdata->buttons[i].gpio);
78a56aab
PB
533 }
534
535 input_unregister_device(input);
536
537 return 0;
538}
539
e15b0213
AS
540
541#ifdef CONFIG_PM
ae78e0e0 542static int gpio_keys_suspend(struct device *dev)
e15b0213 543{
ae78e0e0 544 struct platform_device *pdev = to_platform_device(dev);
e15b0213
AS
545 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
546 int i;
547
548 if (device_may_wakeup(&pdev->dev)) {
549 for (i = 0; i < pdata->nbuttons; i++) {
550 struct gpio_keys_button *button = &pdata->buttons[i];
551 if (button->wakeup) {
552 int irq = gpio_to_irq(button->gpio);
553 enable_irq_wake(irq);
554 }
555 }
556 }
557
558 return 0;
559}
560
ae78e0e0 561static int gpio_keys_resume(struct device *dev)
e15b0213 562{
ae78e0e0 563 struct platform_device *pdev = to_platform_device(dev);
6ee88d71 564 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
e15b0213
AS
565 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
566 int i;
567
6ee88d71
DM
568 for (i = 0; i < pdata->nbuttons; i++) {
569
570 struct gpio_keys_button *button = &pdata->buttons[i];
571 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
572 int irq = gpio_to_irq(button->gpio);
573 disable_irq_wake(irq);
e15b0213 574 }
6ee88d71
DM
575
576 gpio_keys_report_event(&ddata->data[i]);
e15b0213 577 }
6ee88d71 578 input_sync(ddata->input);
e15b0213
AS
579
580 return 0;
581}
ae78e0e0
MR
582
583static const struct dev_pm_ops gpio_keys_pm_ops = {
584 .suspend = gpio_keys_suspend,
585 .resume = gpio_keys_resume,
586};
e15b0213
AS
587#endif
588
9b07044c 589static struct platform_driver gpio_keys_device_driver = {
78a56aab
PB
590 .probe = gpio_keys_probe,
591 .remove = __devexit_p(gpio_keys_remove),
592 .driver = {
593 .name = "gpio-keys",
d7b5247b 594 .owner = THIS_MODULE,
ae78e0e0
MR
595#ifdef CONFIG_PM
596 .pm = &gpio_keys_pm_ops,
597#endif
78a56aab
PB
598 }
599};
600
601static int __init gpio_keys_init(void)
602{
603 return platform_driver_register(&gpio_keys_device_driver);
604}
605
606static void __exit gpio_keys_exit(void)
607{
608 platform_driver_unregister(&gpio_keys_device_driver);
609}
610
611module_init(gpio_keys_init);
612module_exit(gpio_keys_exit);
613
614MODULE_LICENSE("GPL");
615MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
616MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
d7b5247b 617MODULE_ALIAS("platform:gpio-keys");