| /* |
| * Nest driver for keys on GPIO lines |
| * |
| * Copyright 2017 Dmitry Yatsushkevich <dmitryya@google.com> |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 as |
| * published by the Free Software Foundation. |
| */ |
| |
| #include <linux/module.h> |
| |
| #include <linux/init.h> |
| #include <linux/fs.h> |
| #include <linux/interrupt.h> |
| #include <linux/irq.h> |
| #include <linux/sched.h> |
| #include <linux/slab.h> |
| #include <linux/sysctl.h> |
| #include <linux/proc_fs.h> |
| #include <linux/delay.h> |
| #include <linux/platform_device.h> |
| #include <linux/input.h> |
| #include <linux/gpio_keys.h> |
| #include <linux/gpio.h> |
| #include <linux/of_platform.h> |
| #include <linux/of_gpio.h> |
| #include <linux/spinlock.h> |
| #include <linux/hrtimer.h> |
| #include <linux/delay.h> |
| #include <linux/ktime.h> |
| #include <linux/nest_gpio_keys.h> |
| |
| struct gpio_button_data { |
| const struct gpio_keys_button *button; |
| struct input_dev *input; |
| unsigned int irq; |
| struct hrtimer debounce_timer; |
| atomic_t debounce_ongoing; |
| }; |
| |
| struct gpio_keys_drvdata { |
| const struct gpio_keys_platform_data *pdata; |
| struct input_dev *input; |
| struct gpio_button_data data[0]; |
| }; |
| |
| static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata) |
| { |
| const struct gpio_keys_button *button = bdata->button; |
| struct input_dev *input = bdata->input; |
| int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low; |
| |
| input_event(input, button->type, button->code, !!state); |
| input_sync(input); |
| } |
| |
| #define KEY_DEBOUNCE_TIMEOUT_MS 40 |
| static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id) |
| { |
| struct gpio_button_data *bdata = dev_id; |
| |
| if (!atomic_xchg(&bdata->debounce_ongoing, 1)) { |
| ktime_t kt; |
| |
| BUG_ON(irq != bdata->irq); |
| |
| kt = ktime_set(KEY_DEBOUNCE_TIMEOUT_MS / 1000 /* sec */ , |
| (KEY_DEBOUNCE_TIMEOUT_MS % 1000) * 1000000 /* nsec */ ); |
| hrtimer_start(&bdata->debounce_timer, kt, HRTIMER_MODE_REL); |
| } |
| |
| return IRQ_HANDLED; |
| } |
| |
| static enum hrtimer_restart debounce_timer_handler(struct hrtimer *handle) |
| { |
| struct gpio_button_data *pdata = |
| container_of(handle, struct gpio_button_data, debounce_timer); |
| const struct gpio_keys_button *button = pdata->button; |
| struct input_dev *input = pdata->input; |
| int state; |
| |
| state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low; |
| |
| input_event(input, button->type, button->code, !!state); |
| input_sync(input); |
| |
| atomic_set(&pdata->debounce_ongoing, 0); |
| return HRTIMER_NORESTART; |
| } |
| |
| static int gpio_keys_setup_key(struct platform_device *pdev, |
| struct input_dev *input, |
| struct gpio_button_data *bdata, |
| const struct gpio_keys_button *button) |
| { |
| const char *desc = button->desc ? button->desc : "nest_gpio_keys"; |
| struct device *dev = &pdev->dev; |
| unsigned long irqflags; |
| int irq, error; |
| |
| bdata->input = input; |
| bdata->button = button; |
| |
| if (!gpio_is_valid(button->gpio)) { |
| dev_err(dev, "No valid GPIO specified\n"); |
| return -EINVAL; |
| } |
| |
| error = gpio_request_one(button->gpio, GPIOF_IN, desc); |
| if (error < 0) { |
| dev_err(dev, "Failed to request GPIO %d, error %d\n", |
| button->gpio, error); |
| return error; |
| } |
| |
| irq = gpio_to_irq(button->gpio); |
| if (irq < 0) { |
| error = irq; |
| dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n", |
| button->gpio, error); |
| goto fail; |
| } |
| bdata->irq = irq; |
| hrtimer_init(&bdata->debounce_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| bdata->debounce_timer.function = debounce_timer_handler; |
| atomic_set(&bdata->debounce_ongoing, 0); |
| |
| irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING; |
| |
| input_set_capability(input, button->type, button->code); |
| |
| /* |
| * If platform has specified that the button can be disabled, |
| * we don't want it to share the interrupt line. |
| */ |
| if (!button->can_disable) |
| irqflags |= IRQF_SHARED; |
| |
| error = request_any_context_irq(bdata->irq, gpio_keys_gpio_isr, irqflags, desc, bdata); |
| if (error < 0) { |
| dev_err(dev, "Unable to claim irq %d; error %d\n", bdata->irq, error); |
| goto fail; |
| } |
| |
| return 0; |
| |
| fail: |
| if (gpio_is_valid(button->gpio)) |
| gpio_free(button->gpio); |
| |
| return error; |
| } |
| |
| static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata) |
| { |
| struct input_dev *input = ddata->input; |
| int i; |
| |
| for (i = 0; i < ddata->pdata->nbuttons; i++) { |
| struct gpio_button_data *bdata = &ddata->data[i]; |
| if (gpio_is_valid(bdata->button->gpio)) |
| gpio_keys_gpio_report_event(bdata); |
| } |
| input_sync(input); |
| } |
| |
| static int gpio_keys_open(struct input_dev *input) |
| { |
| struct gpio_keys_drvdata *ddata = input_get_drvdata(input); |
| const struct gpio_keys_platform_data *pdata = ddata->pdata; |
| int error; |
| |
| if (pdata->enable) { |
| error = pdata->enable(input->dev.parent); |
| if (error) |
| return error; |
| } |
| |
| /* Report current state of buttons that are connected to GPIOs */ |
| gpio_keys_report_state(ddata); |
| |
| return 0; |
| } |
| |
| static void gpio_keys_close(struct input_dev *input) |
| { |
| struct gpio_keys_drvdata *ddata = input_get_drvdata(input); |
| const struct gpio_keys_platform_data *pdata = ddata->pdata; |
| |
| if (pdata->disable) |
| pdata->disable(input->dev.parent); |
| } |
| |
| /* |
| * Handlers for alternative sources of platform_data |
| */ |
| |
| #ifdef CONFIG_OF |
| /* |
| * Translate OpenFirmware node properties into platform_data |
| */ |
| static struct gpio_keys_platform_data *gpio_keys_get_devtree_pdata(struct device *dev) |
| { |
| struct device_node *node, *pp; |
| struct gpio_keys_platform_data *pdata; |
| struct gpio_keys_button *button; |
| int error; |
| int nbuttons; |
| int i; |
| |
| node = dev->of_node; |
| if (!node) { |
| error = -ENODEV; |
| goto err_out; |
| } |
| |
| nbuttons = of_get_child_count(node); |
| if (nbuttons == 0) { |
| error = -ENODEV; |
| goto err_out; |
| } |
| |
| pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button), |
| GFP_KERNEL); |
| if (!pdata) { |
| error = -ENOMEM; |
| goto err_out; |
| } |
| |
| pdata->buttons = (struct gpio_keys_button *)(pdata + 1); |
| pdata->nbuttons = nbuttons; |
| |
| pdata->rep = ! !of_get_property(node, "autorepeat", NULL); |
| |
| i = 0; |
| for_each_child_of_node(node, pp) { |
| int gpio; |
| enum of_gpio_flags flags; |
| |
| if (!of_find_property(pp, "gpios", NULL)) { |
| pdata->nbuttons--; |
| dev_warn(dev, "Found button without gpios\n"); |
| continue; |
| } |
| |
| gpio = of_get_gpio_flags(pp, 0, &flags); |
| if (gpio < 0) { |
| error = gpio; |
| if (error != -EPROBE_DEFER) |
| dev_err(dev, "Failed to get gpio flags, error: %d\n", |
| error); |
| goto err_free_pdata; |
| } |
| |
| button = &pdata->buttons[i++]; |
| |
| button->gpio = gpio; |
| button->active_low = flags & OF_GPIO_ACTIVE_LOW; |
| |
| if (of_property_read_u32(pp, "linux,code", &button->code)) { |
| dev_err(dev, "Button without keycode: 0x%x\n", |
| button->gpio); |
| error = -EINVAL; |
| goto err_free_pdata; |
| } |
| |
| button->desc = of_get_property(pp, "label", NULL); |
| button->type = EV_KEY; |
| } |
| |
| if (pdata->nbuttons == 0) { |
| error = -EINVAL; |
| goto err_free_pdata; |
| } |
| |
| return pdata; |
| |
| err_free_pdata: |
| kfree(pdata); |
| err_out: |
| return ERR_PTR(error); |
| } |
| |
| static struct of_device_id gpio_keys_of_match[] = { |
| {.compatible = "nest-gpio-keys",}, |
| {}, |
| }; |
| |
| MODULE_DEVICE_TABLE(of, gpio_keys_of_match); |
| |
| #else |
| |
| static inline struct gpio_keys_platform_data *gpio_keys_get_devtree_pdata(struct device *dev) |
| { |
| return ERR_PTR(-ENODEV); |
| } |
| |
| #endif |
| |
| static void gpio_remove_key(struct gpio_button_data *bdata) |
| { |
| hrtimer_cancel(&bdata->debounce_timer); |
| atomic_set(&bdata->debounce_ongoing, 0); |
| free_irq(bdata->irq, bdata); |
| if (gpio_is_valid(bdata->button->gpio)) |
| gpio_free(bdata->button->gpio); |
| } |
| |
| static int gpio_keys_probe(struct platform_device *pdev) |
| { |
| struct device *dev = &pdev->dev; |
| const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev); |
| struct gpio_keys_drvdata *ddata; |
| struct input_dev *input; |
| int i, error; |
| |
| if (!pdata) { |
| pdata = gpio_keys_get_devtree_pdata(dev); |
| if (IS_ERR(pdata)) |
| return PTR_ERR(pdata); |
| } |
| |
| ddata = kzalloc(sizeof(struct gpio_keys_drvdata) + |
| pdata->nbuttons * sizeof(struct gpio_button_data), |
| GFP_KERNEL); |
| input = input_allocate_device(); |
| if (!ddata || !input) { |
| dev_err(dev, "failed to allocate state\n"); |
| error = -ENOMEM; |
| goto fail1; |
| } |
| |
| ddata->pdata = pdata; |
| ddata->input = input; |
| |
| platform_set_drvdata(pdev, ddata); |
| input_set_drvdata(input, ddata); |
| |
| input->name = pdata->name ? : pdev->name; |
| input->phys = "gpio-keys/input0"; |
| input->dev.parent = &pdev->dev; |
| input->open = gpio_keys_open; |
| input->close = gpio_keys_close; |
| |
| input->id.bustype = BUS_HOST; |
| input->id.vendor = NEST_ID; |
| input->id.product = 0x0001; |
| input->id.version = 0x0100; |
| |
| /* Enable auto repeat feature of Linux input subsystem */ |
| if (pdata->rep) |
| __set_bit(EV_REP, input->evbit); |
| |
| for (i = 0; i < pdata->nbuttons; i++) { |
| const struct gpio_keys_button *button = &pdata->buttons[i]; |
| struct gpio_button_data *bdata = &ddata->data[i]; |
| |
| error = gpio_keys_setup_key(pdev, input, bdata, button); |
| if (error) |
| goto fail2; |
| } |
| |
| error = input_register_device(input); |
| if (error) { |
| dev_err(dev, "Unable to register input device, error: %d\n", |
| error); |
| goto fail2; |
| } |
| |
| return 0; |
| |
| fail2: |
| while (--i >= 0) |
| gpio_remove_key(&ddata->data[i]); |
| |
| platform_set_drvdata(pdev, NULL); |
| fail1: |
| input_free_device(input); |
| kfree(ddata); |
| /* If we have no platform data, we allocated pdata dynamically. */ |
| if (!dev_get_platdata(&pdev->dev)) |
| kfree(pdata); |
| |
| return error; |
| } |
| |
| static int gpio_keys_remove(struct platform_device *pdev) |
| { |
| struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); |
| struct input_dev *input = ddata->input; |
| int i; |
| |
| for (i = 0; i < ddata->pdata->nbuttons; i++) |
| gpio_remove_key(&ddata->data[i]); |
| |
| input_unregister_device(input); |
| |
| /* If we have no platform data, we allocated pdata dynamically. */ |
| if (!dev_get_platdata(&pdev->dev)) |
| kfree(ddata->pdata); |
| |
| kfree(ddata); |
| |
| return 0; |
| } |
| |
| static struct platform_driver gpio_keys_device_driver = { |
| .probe = gpio_keys_probe, |
| .remove = gpio_keys_remove, |
| .driver = { |
| .name = "nest-gpio-keys", |
| .owner = THIS_MODULE, |
| .of_match_table = of_match_ptr(gpio_keys_of_match), |
| } |
| }; |
| |
| static int __init gpio_keys_init(void) |
| { |
| return platform_driver_register(&gpio_keys_device_driver); |
| } |
| |
| static void __exit gpio_keys_exit(void) |
| { |
| platform_driver_unregister(&gpio_keys_device_driver); |
| } |
| |
| late_initcall(gpio_keys_init); |
| module_exit(gpio_keys_exit); |
| |
| MODULE_LICENSE("GPL"); |
| MODULE_AUTHOR("Dmitry Yatsushkevich <dmitryya@google.com>"); |
| MODULE_DESCRIPTION("Nest GPIOs keyboard driver. Based on gpio-keys driver."); |
| MODULE_ALIAS("platform:nest-gpio-keys"); |