Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 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> |
| 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> |
David Brownell | 49015be | 2007-03-05 00:30:22 -0800 | [diff] [blame] | 25 | #include <linux/gpio_keys.h> |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 26 | |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 27 | #include <asm/gpio.h> |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 28 | |
| 29 | static irqreturn_t gpio_keys_isr(int irq, void *dev_id) |
| 30 | { |
| 31 | int i; |
| 32 | struct platform_device *pdev = dev_id; |
| 33 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 34 | struct input_dev *input = platform_get_drvdata(pdev); |
| 35 | |
| 36 | for (i = 0; i < pdata->nbuttons; i++) { |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 37 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 38 | int gpio = button->gpio; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 39 | |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 40 | if (irq == gpio_to_irq(gpio)) { |
| 41 | unsigned int type = button->type ?: EV_KEY; |
| 42 | int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low; |
| 43 | |
| 44 | input_event(input, type, button->code, !!state); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 45 | input_sync(input); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return IRQ_HANDLED; |
| 50 | } |
| 51 | |
| 52 | static 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; |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 57 | int wakeup = 0; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 58 | |
| 59 | input = input_allocate_device(); |
| 60 | if (!input) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | platform_set_drvdata(pdev, input); |
| 64 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 65 | input->evbit[0] = BIT_MASK(EV_KEY); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 66 | |
| 67 | input->name = pdev->name; |
| 68 | input->phys = "gpio-keys/input0"; |
Dmitry Torokhov | 469ba4d | 2007-04-12 01:34:58 -0400 | [diff] [blame] | 69 | input->dev.parent = &pdev->dev; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 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++) { |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 77 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 78 | int irq = gpio_to_irq(button->gpio); |
| 79 | unsigned int type = button->type ?: EV_KEY; |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 80 | |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 81 | if (irq < 0) { |
| 82 | error = irq; |
| 83 | printk(KERN_ERR |
| 84 | "gpio-keys: " |
| 85 | "Unable to get irq number for GPIO %d," |
| 86 | "error %d\n", |
| 87 | button->gpio, error); |
| 88 | goto fail; |
| 89 | } |
| 90 | |
| 91 | error = request_irq(irq, gpio_keys_isr, |
| 92 | IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING | |
| 93 | IRQF_TRIGGER_FALLING, |
| 94 | button->desc ? button->desc : "gpio_keys", |
| 95 | pdev); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 96 | if (error) { |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 97 | printk(KERN_ERR |
| 98 | "gpio-keys: Unable to claim irq %d; error %d\n", |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 99 | irq, error); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 100 | goto fail; |
| 101 | } |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 102 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 103 | if (button->wakeup) |
| 104 | wakeup = 1; |
| 105 | |
Roman Moravcik | 84767d0 | 2007-05-01 00:39:13 -0400 | [diff] [blame] | 106 | input_set_capability(input, type, button->code); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | error = input_register_device(input); |
| 110 | if (error) { |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 111 | printk(KERN_ERR |
| 112 | "gpio-keys: Unable to register input device, " |
| 113 | "error: %d\n", error); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 114 | goto fail; |
| 115 | } |
| 116 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 117 | device_init_wakeup(&pdev->dev, wakeup); |
| 118 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 119 | return 0; |
| 120 | |
| 121 | fail: |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 122 | while (--i >= 0) |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 123 | free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 124 | |
Anti Sullin | 006df30 | 2007-09-26 00:01:03 -0400 | [diff] [blame] | 125 | platform_set_drvdata(pdev, NULL); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 126 | input_free_device(input); |
| 127 | |
| 128 | return error; |
| 129 | } |
| 130 | |
| 131 | static int __devexit gpio_keys_remove(struct platform_device *pdev) |
| 132 | { |
| 133 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 134 | struct input_dev *input = platform_get_drvdata(pdev); |
| 135 | int i; |
| 136 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 137 | device_init_wakeup(&pdev->dev, 0); |
| 138 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 139 | for (i = 0; i < pdata->nbuttons; i++) { |
Philipp Zabel | 0d98f6b | 2007-02-18 01:40:46 -0500 | [diff] [blame] | 140 | int irq = gpio_to_irq(pdata->buttons[i].gpio); |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 141 | free_irq(irq, pdev); |
| 142 | } |
| 143 | |
| 144 | input_unregister_device(input); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 149 | |
| 150 | #ifdef CONFIG_PM |
| 151 | static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state) |
| 152 | { |
| 153 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 154 | int i; |
| 155 | |
| 156 | if (device_may_wakeup(&pdev->dev)) { |
| 157 | for (i = 0; i < pdata->nbuttons; i++) { |
| 158 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 159 | if (button->wakeup) { |
| 160 | int irq = gpio_to_irq(button->gpio); |
| 161 | enable_irq_wake(irq); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int gpio_keys_resume(struct platform_device *pdev) |
| 170 | { |
| 171 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 172 | int i; |
| 173 | |
| 174 | if (device_may_wakeup(&pdev->dev)) { |
| 175 | for (i = 0; i < pdata->nbuttons; i++) { |
| 176 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 177 | if (button->wakeup) { |
| 178 | int irq = gpio_to_irq(button->gpio); |
| 179 | disable_irq_wake(irq); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | #else |
| 187 | #define gpio_keys_suspend NULL |
| 188 | #define gpio_keys_resume NULL |
| 189 | #endif |
| 190 | |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 191 | struct platform_driver gpio_keys_device_driver = { |
| 192 | .probe = gpio_keys_probe, |
| 193 | .remove = __devexit_p(gpio_keys_remove), |
Anti Sullin | e15b021 | 2007-09-26 00:01:17 -0400 | [diff] [blame] | 194 | .suspend = gpio_keys_suspend, |
| 195 | .resume = gpio_keys_resume, |
Phil Blundell | 78a56aa | 2007-01-18 00:44:09 -0500 | [diff] [blame] | 196 | .driver = { |
| 197 | .name = "gpio-keys", |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | static int __init gpio_keys_init(void) |
| 202 | { |
| 203 | return platform_driver_register(&gpio_keys_device_driver); |
| 204 | } |
| 205 | |
| 206 | static void __exit gpio_keys_exit(void) |
| 207 | { |
| 208 | platform_driver_unregister(&gpio_keys_device_driver); |
| 209 | } |
| 210 | |
| 211 | module_init(gpio_keys_init); |
| 212 | module_exit(gpio_keys_exit); |
| 213 | |
| 214 | MODULE_LICENSE("GPL"); |
| 215 | MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>"); |
| 216 | MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs"); |