|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Analog Devices MAX16150/MAX16169 Pushbutton Driver |
| 4 | + * |
| 5 | + * Copyright 2024 Analog Devices Inc. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/module.h> |
| 9 | +#include <linux/gpio.h> |
| 10 | +#include <linux/interrupt.h> |
| 11 | +#include <linux/input.h> |
| 12 | +#include <linux/of.h> |
| 13 | +#include <linux/platform_device.h> |
| 14 | + |
| 15 | +#define GPIO_INT 17 |
| 16 | +#define GPIO_CLR 27 |
| 17 | + |
| 18 | +static struct input_dev *max16150_input_dev; |
| 19 | +static int irq_number; |
| 20 | +static ktime_t last_time; |
| 21 | +static const ktime_t short_pulse = 32 * NSEC_PER_MSEC; |
| 22 | +static const ktime_t long_pulse = 128 * NSEC_PER_MSEC; |
| 23 | + |
| 24 | +static irqreturn_t max16150_isr(int irq, void *dev_id) |
| 25 | +{ |
| 26 | + ktime_t now = ktime_get(); |
| 27 | + ktime_t duration = ktime_sub(now, last_time); |
| 28 | + |
| 29 | + if (duration >= long_pulse) |
| 30 | + gpio_set_value(GPIO_CLR, 0); |
| 31 | + |
| 32 | + last_time = now; |
| 33 | + return IRQ_HANDLED; |
| 34 | +} |
| 35 | + |
| 36 | +static int max16150_probe(struct platform_device *pdev) |
| 37 | +{ |
| 38 | + int ret; |
| 39 | + |
| 40 | + max16150_input_dev = devm_input_allocate_device(&pdev->dev); |
| 41 | + if (!max16150_input_dev) |
| 42 | + return -ENOMEM; |
| 43 | + |
| 44 | + max16150_input_dev->name = "max16150"; |
| 45 | + max16150_input_dev->id.bustype = BUS_HOST; |
| 46 | + |
| 47 | + ret = input_register_device(max16150_input_dev); |
| 48 | + if (ret) |
| 49 | + return ret; |
| 50 | + |
| 51 | + ret = devm_gpio_request_one(&pdev->dev, GPIO_CLR, GPIOF_OUT_INIT_HIGH, |
| 52 | + "max16150_clr"); |
| 53 | + if (ret) |
| 54 | + return ret; |
| 55 | + |
| 56 | + irq_number = gpio_to_irq(GPIO_INT); |
| 57 | + ret = devm_request_irq(&pdev->dev, irq_number, max16150_isr, |
| 58 | + IRQF_TRIGGER_RISING, "max16150_irq", NULL); |
| 59 | + if (ret) |
| 60 | + return ret; |
| 61 | + |
| 62 | + last_time = ktime_get(); |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static int max16150_remove(struct platform_device *pdev) |
| 67 | +{ |
| 68 | + input_unregister_device(max16150_input_dev); |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +static const struct of_device_id max16150_of_match[] = { |
| 73 | + { .compatible = "maxim,max16150", }, |
| 74 | + { } |
| 75 | +}; |
| 76 | +MODULE_DEVICE_TABLE(of, max16150_of_match); |
| 77 | + |
| 78 | +static struct platform_driver max16150_driver = { |
| 79 | + .probe = max16150_probe, |
| 80 | + .remove = max16150_remove, |
| 81 | + .driver = { |
| 82 | + .name = "max16150", |
| 83 | + .of_match_table = max16150_of_match, |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +module_platform_driver(max16150_driver); |
| 88 | + |
| 89 | +MODULE_AUTHOR( "Marc Paolo Sosa <[email protected]>"); |
| 90 | +MODULE_DESCRIPTION("MAX16150/MAX16169 Pushbutton Driver"); |
| 91 | +MODULE_LICENSE("GPL"); |
0 commit comments