178a56aabSPhil Blundell /* 278a56aabSPhil Blundell * Driver for keys on GPIO lines capable of generating interrupts. 378a56aabSPhil Blundell * 478a56aabSPhil Blundell * Copyright 2005 Phil Blundell 578a56aabSPhil Blundell * 678a56aabSPhil Blundell * This program is free software; you can redistribute it and/or modify 778a56aabSPhil Blundell * it under the terms of the GNU General Public License version 2 as 878a56aabSPhil Blundell * published by the Free Software Foundation. 978a56aabSPhil Blundell */ 1078a56aabSPhil Blundell 1178a56aabSPhil Blundell #include <linux/module.h> 1278a56aabSPhil Blundell 1378a56aabSPhil Blundell #include <linux/init.h> 1478a56aabSPhil Blundell #include <linux/fs.h> 1578a56aabSPhil Blundell #include <linux/interrupt.h> 1678a56aabSPhil Blundell #include <linux/irq.h> 1778a56aabSPhil Blundell #include <linux/sched.h> 1878a56aabSPhil Blundell #include <linux/pm.h> 1978a56aabSPhil Blundell #include <linux/sysctl.h> 2078a56aabSPhil Blundell #include <linux/proc_fs.h> 2178a56aabSPhil Blundell #include <linux/delay.h> 2278a56aabSPhil Blundell #include <linux/platform_device.h> 2378a56aabSPhil Blundell #include <linux/input.h> 2449015beeSDavid Brownell #include <linux/gpio_keys.h> 25da0d03feSJani Nikula #include <linux/workqueue.h> 2678a56aabSPhil Blundell 270d98f6bbSPhilipp Zabel #include <asm/gpio.h> 2878a56aabSPhil Blundell 29a33466e3SDmitry Baryshkov struct gpio_button_data { 30a33466e3SDmitry Baryshkov struct gpio_keys_button *button; 31a33466e3SDmitry Baryshkov struct input_dev *input; 32ca865a77SJani Nikula struct timer_list timer; 33da0d03feSJani Nikula struct work_struct work; 34a33466e3SDmitry Baryshkov }; 35a33466e3SDmitry Baryshkov 36a33466e3SDmitry Baryshkov struct gpio_keys_drvdata { 37a33466e3SDmitry Baryshkov struct input_dev *input; 38a33466e3SDmitry Baryshkov struct gpio_button_data data[0]; 39a33466e3SDmitry Baryshkov }; 40a33466e3SDmitry Baryshkov 41da0d03feSJani Nikula static void gpio_keys_report_event(struct work_struct *work) 4278a56aabSPhil Blundell { 43da0d03feSJani Nikula struct gpio_button_data *bdata = 44da0d03feSJani Nikula container_of(work, struct gpio_button_data, work); 45ce25d7e9SUwe Kleine-König struct gpio_keys_button *button = bdata->button; 46ce25d7e9SUwe Kleine-König struct input_dev *input = bdata->input; 4784767d00SRoman Moravcik unsigned int type = button->type ?: EV_KEY; 48a33466e3SDmitry Baryshkov int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low; 4984767d00SRoman Moravcik 5084767d00SRoman Moravcik input_event(input, type, button->code, !!state); 5178a56aabSPhil Blundell input_sync(input); 52a33466e3SDmitry Baryshkov } 53a33466e3SDmitry Baryshkov 54da0d03feSJani Nikula static void gpio_keys_timer(unsigned long _data) 55ca865a77SJani Nikula { 56ca865a77SJani Nikula struct gpio_button_data *data = (struct gpio_button_data *)_data; 57ca865a77SJani Nikula 58da0d03feSJani Nikula schedule_work(&data->work); 59ca865a77SJani Nikula } 60ca865a77SJani Nikula 61a33466e3SDmitry Baryshkov static irqreturn_t gpio_keys_isr(int irq, void *dev_id) 62a33466e3SDmitry Baryshkov { 6357ffe9d5SUwe Kleine-König struct gpio_button_data *bdata = dev_id; 6457ffe9d5SUwe Kleine-König struct gpio_keys_button *button = bdata->button; 65a33466e3SDmitry Baryshkov 6657ffe9d5SUwe Kleine-König BUG_ON(irq != gpio_to_irq(button->gpio)); 67a33466e3SDmitry Baryshkov 68ca865a77SJani Nikula if (button->debounce_interval) 69ca865a77SJani Nikula mod_timer(&bdata->timer, 70ca865a77SJani Nikula jiffies + msecs_to_jiffies(button->debounce_interval)); 71ca865a77SJani Nikula else 72da0d03feSJani Nikula schedule_work(&bdata->work); 73a33466e3SDmitry Baryshkov 741164ec1aSDavid Brownell return IRQ_HANDLED; 7578a56aabSPhil Blundell } 7678a56aabSPhil Blundell 7778a56aabSPhil Blundell static int __devinit gpio_keys_probe(struct platform_device *pdev) 7878a56aabSPhil Blundell { 7978a56aabSPhil Blundell struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 80a33466e3SDmitry Baryshkov struct gpio_keys_drvdata *ddata; 8178a56aabSPhil Blundell struct input_dev *input; 8278a56aabSPhil Blundell int i, error; 83e15b0213SAnti Sullin int wakeup = 0; 8478a56aabSPhil Blundell 85a33466e3SDmitry Baryshkov ddata = kzalloc(sizeof(struct gpio_keys_drvdata) + 86a33466e3SDmitry Baryshkov pdata->nbuttons * sizeof(struct gpio_button_data), 87a33466e3SDmitry Baryshkov GFP_KERNEL); 8878a56aabSPhil Blundell input = input_allocate_device(); 89a33466e3SDmitry Baryshkov if (!ddata || !input) { 90a33466e3SDmitry Baryshkov error = -ENOMEM; 91a33466e3SDmitry Baryshkov goto fail1; 92a33466e3SDmitry Baryshkov } 9378a56aabSPhil Blundell 94a33466e3SDmitry Baryshkov platform_set_drvdata(pdev, ddata); 9578a56aabSPhil Blundell 9678a56aabSPhil Blundell input->name = pdev->name; 9778a56aabSPhil Blundell input->phys = "gpio-keys/input0"; 98469ba4dfSDmitry Torokhov input->dev.parent = &pdev->dev; 9978a56aabSPhil Blundell 10078a56aabSPhil Blundell input->id.bustype = BUS_HOST; 10178a56aabSPhil Blundell input->id.vendor = 0x0001; 10278a56aabSPhil Blundell input->id.product = 0x0001; 10378a56aabSPhil Blundell input->id.version = 0x0100; 10478a56aabSPhil Blundell 105b67b4b11SDominic Curran /* Enable auto repeat feature of Linux input subsystem */ 106b67b4b11SDominic Curran if (pdata->rep) 107b67b4b11SDominic Curran __set_bit(EV_REP, input->evbit); 108b67b4b11SDominic Curran 109a33466e3SDmitry Baryshkov ddata->input = input; 110a33466e3SDmitry Baryshkov 11178a56aabSPhil Blundell for (i = 0; i < pdata->nbuttons; i++) { 11284767d00SRoman Moravcik struct gpio_keys_button *button = &pdata->buttons[i]; 113a33466e3SDmitry Baryshkov struct gpio_button_data *bdata = &ddata->data[i]; 1146a2e3911SHerbert Valerio Riedel int irq; 11584767d00SRoman Moravcik unsigned int type = button->type ?: EV_KEY; 11678a56aabSPhil Blundell 117a33466e3SDmitry Baryshkov bdata->input = input; 11874dd4393SUwe Kleine-König bdata->button = button; 119ca865a77SJani Nikula setup_timer(&bdata->timer, 120da0d03feSJani Nikula gpio_keys_timer, (unsigned long)bdata); 121da0d03feSJani Nikula INIT_WORK(&bdata->work, gpio_keys_report_event); 122a33466e3SDmitry Baryshkov 1236a2e3911SHerbert Valerio Riedel error = gpio_request(button->gpio, button->desc ?: "gpio_keys"); 1246a2e3911SHerbert Valerio Riedel if (error < 0) { 1256a2e3911SHerbert Valerio Riedel pr_err("gpio-keys: failed to request GPIO %d," 1266a2e3911SHerbert Valerio Riedel " error %d\n", button->gpio, error); 127a33466e3SDmitry Baryshkov goto fail2; 1286a2e3911SHerbert Valerio Riedel } 1296a2e3911SHerbert Valerio Riedel 1306a2e3911SHerbert Valerio Riedel error = gpio_direction_input(button->gpio); 1316a2e3911SHerbert Valerio Riedel if (error < 0) { 1326a2e3911SHerbert Valerio Riedel pr_err("gpio-keys: failed to configure input" 1336a2e3911SHerbert Valerio Riedel " direction for GPIO %d, error %d\n", 1346a2e3911SHerbert Valerio Riedel button->gpio, error); 1356a2e3911SHerbert Valerio Riedel gpio_free(button->gpio); 136a33466e3SDmitry Baryshkov goto fail2; 1376a2e3911SHerbert Valerio Riedel } 1386a2e3911SHerbert Valerio Riedel 1396a2e3911SHerbert Valerio Riedel irq = gpio_to_irq(button->gpio); 140006df302SAnti Sullin if (irq < 0) { 141006df302SAnti Sullin error = irq; 1426a2e3911SHerbert Valerio Riedel pr_err("gpio-keys: Unable to get irq number" 1436a2e3911SHerbert Valerio Riedel " for GPIO %d, error %d\n", 144006df302SAnti Sullin button->gpio, error); 1456a2e3911SHerbert Valerio Riedel gpio_free(button->gpio); 146a33466e3SDmitry Baryshkov goto fail2; 147006df302SAnti Sullin } 148006df302SAnti Sullin 149006df302SAnti Sullin error = request_irq(irq, gpio_keys_isr, 15064e8563cSDmitry Torokhov IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 15184767d00SRoman Moravcik button->desc ? button->desc : "gpio_keys", 15257ffe9d5SUwe Kleine-König bdata); 15378a56aabSPhil Blundell if (error) { 1546a2e3911SHerbert Valerio Riedel pr_err("gpio-keys: Unable to claim irq %d; error %d\n", 1550d98f6bbSPhilipp Zabel irq, error); 1566a2e3911SHerbert Valerio Riedel gpio_free(button->gpio); 157a33466e3SDmitry Baryshkov goto fail2; 15878a56aabSPhil Blundell } 15984767d00SRoman Moravcik 160e15b0213SAnti Sullin if (button->wakeup) 161e15b0213SAnti Sullin wakeup = 1; 162e15b0213SAnti Sullin 16384767d00SRoman Moravcik input_set_capability(input, type, button->code); 16478a56aabSPhil Blundell } 16578a56aabSPhil Blundell 16678a56aabSPhil Blundell error = input_register_device(input); 16778a56aabSPhil Blundell if (error) { 1686a2e3911SHerbert Valerio Riedel pr_err("gpio-keys: Unable to register input device, " 169006df302SAnti Sullin "error: %d\n", error); 170a33466e3SDmitry Baryshkov goto fail2; 17178a56aabSPhil Blundell } 17278a56aabSPhil Blundell 173e15b0213SAnti Sullin device_init_wakeup(&pdev->dev, wakeup); 174e15b0213SAnti Sullin 17578a56aabSPhil Blundell return 0; 17678a56aabSPhil Blundell 177a33466e3SDmitry Baryshkov fail2: 1786a2e3911SHerbert Valerio Riedel while (--i >= 0) { 17957ffe9d5SUwe Kleine-König free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]); 180ca865a77SJani Nikula if (pdata->buttons[i].debounce_interval) 181ca865a77SJani Nikula del_timer_sync(&ddata->data[i].timer); 182da0d03feSJani Nikula cancel_work_sync(&ddata->data[i].work); 1836a2e3911SHerbert Valerio Riedel gpio_free(pdata->buttons[i].gpio); 1846a2e3911SHerbert Valerio Riedel } 18578a56aabSPhil Blundell 186006df302SAnti Sullin platform_set_drvdata(pdev, NULL); 187a33466e3SDmitry Baryshkov fail1: 18878a56aabSPhil Blundell input_free_device(input); 189a33466e3SDmitry Baryshkov kfree(ddata); 19078a56aabSPhil Blundell 19178a56aabSPhil Blundell return error; 19278a56aabSPhil Blundell } 19378a56aabSPhil Blundell 19478a56aabSPhil Blundell static int __devexit gpio_keys_remove(struct platform_device *pdev) 19578a56aabSPhil Blundell { 19678a56aabSPhil Blundell struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 197a33466e3SDmitry Baryshkov struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); 198a33466e3SDmitry Baryshkov struct input_dev *input = ddata->input; 19978a56aabSPhil Blundell int i; 20078a56aabSPhil Blundell 201e15b0213SAnti Sullin device_init_wakeup(&pdev->dev, 0); 202e15b0213SAnti Sullin 20378a56aabSPhil Blundell for (i = 0; i < pdata->nbuttons; i++) { 2040d98f6bbSPhilipp Zabel int irq = gpio_to_irq(pdata->buttons[i].gpio); 20557ffe9d5SUwe Kleine-König free_irq(irq, &ddata->data[i]); 206ca865a77SJani Nikula if (pdata->buttons[i].debounce_interval) 207ca865a77SJani Nikula del_timer_sync(&ddata->data[i].timer); 208da0d03feSJani Nikula cancel_work_sync(&ddata->data[i].work); 2096a2e3911SHerbert Valerio Riedel gpio_free(pdata->buttons[i].gpio); 21078a56aabSPhil Blundell } 21178a56aabSPhil Blundell 21278a56aabSPhil Blundell input_unregister_device(input); 21378a56aabSPhil Blundell 21478a56aabSPhil Blundell return 0; 21578a56aabSPhil Blundell } 21678a56aabSPhil Blundell 217e15b0213SAnti Sullin 218e15b0213SAnti Sullin #ifdef CONFIG_PM 219*ae78e0e0SMike Rapoport static int gpio_keys_suspend(struct device *dev) 220e15b0213SAnti Sullin { 221*ae78e0e0SMike Rapoport struct platform_device *pdev = to_platform_device(dev); 222e15b0213SAnti Sullin struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 223e15b0213SAnti Sullin int i; 224e15b0213SAnti Sullin 225e15b0213SAnti Sullin if (device_may_wakeup(&pdev->dev)) { 226e15b0213SAnti Sullin for (i = 0; i < pdata->nbuttons; i++) { 227e15b0213SAnti Sullin struct gpio_keys_button *button = &pdata->buttons[i]; 228e15b0213SAnti Sullin if (button->wakeup) { 229e15b0213SAnti Sullin int irq = gpio_to_irq(button->gpio); 230e15b0213SAnti Sullin enable_irq_wake(irq); 231e15b0213SAnti Sullin } 232e15b0213SAnti Sullin } 233e15b0213SAnti Sullin } 234e15b0213SAnti Sullin 235e15b0213SAnti Sullin return 0; 236e15b0213SAnti Sullin } 237e15b0213SAnti Sullin 238*ae78e0e0SMike Rapoport static int gpio_keys_resume(struct device *dev) 239e15b0213SAnti Sullin { 240*ae78e0e0SMike Rapoport struct platform_device *pdev = to_platform_device(dev); 241e15b0213SAnti Sullin struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 242e15b0213SAnti Sullin int i; 243e15b0213SAnti Sullin 244e15b0213SAnti Sullin if (device_may_wakeup(&pdev->dev)) { 245e15b0213SAnti Sullin for (i = 0; i < pdata->nbuttons; i++) { 246e15b0213SAnti Sullin struct gpio_keys_button *button = &pdata->buttons[i]; 247e15b0213SAnti Sullin if (button->wakeup) { 248e15b0213SAnti Sullin int irq = gpio_to_irq(button->gpio); 249e15b0213SAnti Sullin disable_irq_wake(irq); 250e15b0213SAnti Sullin } 251e15b0213SAnti Sullin } 252e15b0213SAnti Sullin } 253e15b0213SAnti Sullin 254e15b0213SAnti Sullin return 0; 255e15b0213SAnti Sullin } 256*ae78e0e0SMike Rapoport 257*ae78e0e0SMike Rapoport static const struct dev_pm_ops gpio_keys_pm_ops = { 258*ae78e0e0SMike Rapoport .suspend = gpio_keys_suspend, 259*ae78e0e0SMike Rapoport .resume = gpio_keys_resume, 260*ae78e0e0SMike Rapoport }; 261e15b0213SAnti Sullin #endif 262e15b0213SAnti Sullin 2639b07044cSUwe Kleine-König static struct platform_driver gpio_keys_device_driver = { 26478a56aabSPhil Blundell .probe = gpio_keys_probe, 26578a56aabSPhil Blundell .remove = __devexit_p(gpio_keys_remove), 26678a56aabSPhil Blundell .driver = { 26778a56aabSPhil Blundell .name = "gpio-keys", 268d7b5247bSKay Sievers .owner = THIS_MODULE, 269*ae78e0e0SMike Rapoport #ifdef CONFIG_PM 270*ae78e0e0SMike Rapoport .pm = &gpio_keys_pm_ops, 271*ae78e0e0SMike Rapoport #endif 27278a56aabSPhil Blundell } 27378a56aabSPhil Blundell }; 27478a56aabSPhil Blundell 27578a56aabSPhil Blundell static int __init gpio_keys_init(void) 27678a56aabSPhil Blundell { 27778a56aabSPhil Blundell return platform_driver_register(&gpio_keys_device_driver); 27878a56aabSPhil Blundell } 27978a56aabSPhil Blundell 28078a56aabSPhil Blundell static void __exit gpio_keys_exit(void) 28178a56aabSPhil Blundell { 28278a56aabSPhil Blundell platform_driver_unregister(&gpio_keys_device_driver); 28378a56aabSPhil Blundell } 28478a56aabSPhil Blundell 28578a56aabSPhil Blundell module_init(gpio_keys_init); 28678a56aabSPhil Blundell module_exit(gpio_keys_exit); 28778a56aabSPhil Blundell 28878a56aabSPhil Blundell MODULE_LICENSE("GPL"); 28978a56aabSPhil Blundell MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>"); 29078a56aabSPhil Blundell MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs"); 291d7b5247bSKay Sievers MODULE_ALIAS("platform:gpio-keys"); 292