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 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> 19 #include <linux/sysctl.h> 20 #include <linux/proc_fs.h> 21 #include <linux/delay.h> 22 #include <linux/platform_device.h> 23 #include <linux/input.h> 24 #include <linux/gpio_keys.h> 25 26 #include <asm/gpio.h> 27 28 struct gpio_button_data { 29 struct gpio_keys_button *button; 30 struct input_dev *input; 31 struct timer_list timer; 32 }; 33 34 struct gpio_keys_drvdata { 35 struct input_dev *input; 36 struct gpio_button_data data[0]; 37 }; 38 39 static void gpio_keys_report_event(struct gpio_button_data *bdata) 40 { 41 struct gpio_keys_button *button = bdata->button; 42 struct input_dev *input = bdata->input; 43 unsigned int type = button->type ?: EV_KEY; 44 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low; 45 46 input_event(input, type, button->code, !!state); 47 input_sync(input); 48 } 49 50 static void gpio_check_button(unsigned long _data) 51 { 52 struct gpio_button_data *data = (struct gpio_button_data *)_data; 53 54 gpio_keys_report_event(data); 55 } 56 57 static irqreturn_t gpio_keys_isr(int irq, void *dev_id) 58 { 59 struct gpio_button_data *bdata = dev_id; 60 struct gpio_keys_button *button = bdata->button; 61 62 BUG_ON(irq != gpio_to_irq(button->gpio)); 63 64 if (button->debounce_interval) 65 mod_timer(&bdata->timer, 66 jiffies + msecs_to_jiffies(button->debounce_interval)); 67 else 68 gpio_keys_report_event(bdata); 69 70 return IRQ_HANDLED; 71 } 72 73 static int __devinit gpio_keys_probe(struct platform_device *pdev) 74 { 75 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 76 struct gpio_keys_drvdata *ddata; 77 struct input_dev *input; 78 int i, error; 79 int wakeup = 0; 80 81 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) + 82 pdata->nbuttons * sizeof(struct gpio_button_data), 83 GFP_KERNEL); 84 input = input_allocate_device(); 85 if (!ddata || !input) { 86 error = -ENOMEM; 87 goto fail1; 88 } 89 90 platform_set_drvdata(pdev, ddata); 91 92 input->name = pdev->name; 93 input->phys = "gpio-keys/input0"; 94 input->dev.parent = &pdev->dev; 95 96 input->id.bustype = BUS_HOST; 97 input->id.vendor = 0x0001; 98 input->id.product = 0x0001; 99 input->id.version = 0x0100; 100 101 /* Enable auto repeat feature of Linux input subsystem */ 102 if (pdata->rep) 103 __set_bit(EV_REP, input->evbit); 104 105 ddata->input = input; 106 107 for (i = 0; i < pdata->nbuttons; i++) { 108 struct gpio_keys_button *button = &pdata->buttons[i]; 109 struct gpio_button_data *bdata = &ddata->data[i]; 110 int irq; 111 unsigned int type = button->type ?: EV_KEY; 112 113 bdata->input = input; 114 bdata->button = button; 115 setup_timer(&bdata->timer, 116 gpio_check_button, (unsigned long)bdata); 117 118 error = gpio_request(button->gpio, button->desc ?: "gpio_keys"); 119 if (error < 0) { 120 pr_err("gpio-keys: failed to request GPIO %d," 121 " error %d\n", button->gpio, error); 122 goto fail2; 123 } 124 125 error = gpio_direction_input(button->gpio); 126 if (error < 0) { 127 pr_err("gpio-keys: failed to configure input" 128 " direction for GPIO %d, error %d\n", 129 button->gpio, error); 130 gpio_free(button->gpio); 131 goto fail2; 132 } 133 134 irq = gpio_to_irq(button->gpio); 135 if (irq < 0) { 136 error = irq; 137 pr_err("gpio-keys: Unable to get irq number" 138 " for GPIO %d, error %d\n", 139 button->gpio, error); 140 gpio_free(button->gpio); 141 goto fail2; 142 } 143 144 error = request_irq(irq, gpio_keys_isr, 145 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING | 146 IRQF_TRIGGER_FALLING, 147 button->desc ? button->desc : "gpio_keys", 148 bdata); 149 if (error) { 150 pr_err("gpio-keys: Unable to claim irq %d; error %d\n", 151 irq, error); 152 gpio_free(button->gpio); 153 goto fail2; 154 } 155 156 if (button->wakeup) 157 wakeup = 1; 158 159 input_set_capability(input, type, button->code); 160 } 161 162 error = input_register_device(input); 163 if (error) { 164 pr_err("gpio-keys: Unable to register input device, " 165 "error: %d\n", error); 166 goto fail2; 167 } 168 169 device_init_wakeup(&pdev->dev, wakeup); 170 171 return 0; 172 173 fail2: 174 while (--i >= 0) { 175 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]); 176 if (pdata->buttons[i].debounce_interval) 177 del_timer_sync(&ddata->data[i].timer); 178 gpio_free(pdata->buttons[i].gpio); 179 } 180 181 platform_set_drvdata(pdev, NULL); 182 fail1: 183 input_free_device(input); 184 kfree(ddata); 185 186 return error; 187 } 188 189 static int __devexit gpio_keys_remove(struct platform_device *pdev) 190 { 191 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 192 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); 193 struct input_dev *input = ddata->input; 194 int i; 195 196 device_init_wakeup(&pdev->dev, 0); 197 198 for (i = 0; i < pdata->nbuttons; i++) { 199 int irq = gpio_to_irq(pdata->buttons[i].gpio); 200 free_irq(irq, &ddata->data[i]); 201 if (pdata->buttons[i].debounce_interval) 202 del_timer_sync(&ddata->data[i].timer); 203 gpio_free(pdata->buttons[i].gpio); 204 } 205 206 input_unregister_device(input); 207 208 return 0; 209 } 210 211 212 #ifdef CONFIG_PM 213 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state) 214 { 215 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 216 int i; 217 218 if (device_may_wakeup(&pdev->dev)) { 219 for (i = 0; i < pdata->nbuttons; i++) { 220 struct gpio_keys_button *button = &pdata->buttons[i]; 221 if (button->wakeup) { 222 int irq = gpio_to_irq(button->gpio); 223 enable_irq_wake(irq); 224 } 225 } 226 } 227 228 return 0; 229 } 230 231 static int gpio_keys_resume(struct platform_device *pdev) 232 { 233 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 234 int i; 235 236 if (device_may_wakeup(&pdev->dev)) { 237 for (i = 0; i < pdata->nbuttons; i++) { 238 struct gpio_keys_button *button = &pdata->buttons[i]; 239 if (button->wakeup) { 240 int irq = gpio_to_irq(button->gpio); 241 disable_irq_wake(irq); 242 } 243 } 244 } 245 246 return 0; 247 } 248 #else 249 #define gpio_keys_suspend NULL 250 #define gpio_keys_resume NULL 251 #endif 252 253 static struct platform_driver gpio_keys_device_driver = { 254 .probe = gpio_keys_probe, 255 .remove = __devexit_p(gpio_keys_remove), 256 .suspend = gpio_keys_suspend, 257 .resume = gpio_keys_resume, 258 .driver = { 259 .name = "gpio-keys", 260 .owner = THIS_MODULE, 261 } 262 }; 263 264 static int __init gpio_keys_init(void) 265 { 266 return platform_driver_register(&gpio_keys_device_driver); 267 } 268 269 static void __exit gpio_keys_exit(void) 270 { 271 platform_driver_unregister(&gpio_keys_device_driver); 272 } 273 274 module_init(gpio_keys_init); 275 module_exit(gpio_keys_exit); 276 277 MODULE_LICENSE("GPL"); 278 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>"); 279 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs"); 280 MODULE_ALIAS("platform:gpio-keys"); 281