|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Analog Devices MAX16150/MAX16169 Pushbutton Driver |
| 4 | + * |
| 5 | + * Copyright 2025 Analog Devices Inc. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/delay.h> |
| 9 | +#include <linux/init.h> |
| 10 | +#include <linux/input.h> |
| 11 | +#include <linux/interrupt.h> |
| 12 | +#include <linux/gpio/consumer.h> |
| 13 | +#include <linux/kernel.h> |
| 14 | +#include <linux/mod_devicetable.h> |
| 15 | +#include <linux/platform_device.h> |
| 16 | +#include <linux/property.h> |
| 17 | + |
| 18 | +#define MAX16150_LONG_INTERRUPT 120000000 |
| 19 | + |
| 20 | +struct max16150_chip_info { |
| 21 | + bool has_clr_gpio; |
| 22 | +}; |
| 23 | + |
| 24 | +struct max16150_device { |
| 25 | + struct input_dev *input; |
| 26 | + struct gpio_desc *gpiod; |
| 27 | + struct gpio_desc *clr_gpiod; |
| 28 | + const struct max16150_chip_info *chip_info; |
| 29 | + u64 low, high, duration; |
| 30 | + unsigned int keycode; |
| 31 | +}; |
| 32 | + |
| 33 | +static irqreturn_t max16150_irq_handler(int irq, void *_max16150) |
| 34 | +{ |
| 35 | + struct max16150_device *max16150 = _max16150; |
| 36 | + int value; |
| 37 | + |
| 38 | + value = gpiod_get_value(max16150->gpiod); |
| 39 | + |
| 40 | + if (!value) { |
| 41 | + max16150->low = ktime_get_ns(); |
| 42 | + return IRQ_HANDLED; |
| 43 | + } |
| 44 | + |
| 45 | + max16150->high = ktime_get_ns(); |
| 46 | + if (max16150->low) { |
| 47 | + max16150->duration = max16150->high - max16150->low; |
| 48 | + |
| 49 | + if (max16150->duration > MAX16150_LONG_INTERRUPT) { |
| 50 | + gpiod_set_value(max16150->clr_gpiod, 1); |
| 51 | + input_report_key(max16150->input, max16150->keycode, 1); |
| 52 | + input_sync(max16150->input); |
| 53 | + input_report_key(max16150->input, max16150->keycode, 0); |
| 54 | + input_sync(max16150->input); |
| 55 | + } |
| 56 | + |
| 57 | + max16150->low = 0; |
| 58 | + } |
| 59 | + |
| 60 | + return IRQ_HANDLED; |
| 61 | +} |
| 62 | + |
| 63 | +static const struct max16150_chip_info max16150_variant_a = { |
| 64 | + .has_clr_gpio = true, |
| 65 | +}; |
| 66 | + |
| 67 | +static const struct max16150_chip_info max16150_variant_b = { |
| 68 | + .has_clr_gpio = false, |
| 69 | +}; |
| 70 | + |
| 71 | +static int max16150_probe(struct platform_device *pdev) |
| 72 | +{ |
| 73 | + const struct max16150_chip_info *chip_info; |
| 74 | + struct max16150_device *max16150; |
| 75 | + struct device *dev = &pdev->dev; |
| 76 | + int err, irq, ret; |
| 77 | + u32 keycode; |
| 78 | + |
| 79 | + chip_info = device_get_match_data(dev); |
| 80 | + if (!chip_info) |
| 81 | + return -EINVAL; |
| 82 | + |
| 83 | + max16150 = devm_kzalloc(dev, sizeof(*max16150), GFP_KERNEL); |
| 84 | + if (!max16150) |
| 85 | + return -ENOMEM; |
| 86 | + |
| 87 | + max16150->chip_info = chip_info; |
| 88 | + |
| 89 | + max16150->input = devm_input_allocate_device(dev); |
| 90 | + if (!max16150->input) |
| 91 | + return -ENOMEM; |
| 92 | + |
| 93 | + max16150->input->name = "MAX16150 Pushbutton"; |
| 94 | + max16150->input->phys = "max16150/input0"; |
| 95 | + max16150->input->id.bustype = BUS_HOST; |
| 96 | + |
| 97 | + keycode = KEY_POWER; |
| 98 | + ret = device_property_read_u32(dev, "linux,code", &keycode); |
| 99 | + if (ret) |
| 100 | + return dev_err_probe(dev, ret, "Failed to get keycode\n"); |
| 101 | + |
| 102 | + max16150->keycode = keycode; |
| 103 | + |
| 104 | + input_set_capability(max16150->input, EV_KEY, max16150->keycode); |
| 105 | + |
| 106 | + max16150->gpiod = devm_gpiod_get(dev, "interrupt", GPIOD_IN); |
| 107 | + if (IS_ERR(max16150->gpiod)) |
| 108 | + return dev_err_probe(dev, PTR_ERR(max16150->gpiod), |
| 109 | + "Failed to get interrupt GPIO\n"); |
| 110 | + |
| 111 | + if (chip_info->has_clr_gpio) { |
| 112 | + max16150->clr_gpiod = devm_gpiod_get(dev, "clr", GPIOD_OUT_HIGH); |
| 113 | + if (IS_ERR(max16150->clr_gpiod)) |
| 114 | + return dev_err_probe(dev, PTR_ERR(max16150->clr_gpiod), |
| 115 | + "Failed to get clr GPIO\n"); |
| 116 | + |
| 117 | + if (!max16150->clr_gpiod) |
| 118 | + return dev_err_probe(dev, -ENODEV, |
| 119 | + "clr GPIO is mandatory\n"); |
| 120 | + |
| 121 | + if (max16150->clr_gpiod) { |
| 122 | + fsleep(1000); |
| 123 | + gpiod_set_value(max16150->clr_gpiod, 0); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + irq = gpiod_to_irq(max16150->gpiod); |
| 128 | + if (irq < 0) |
| 129 | + return dev_err_probe(dev, irq, |
| 130 | + "MAX16150: Failed to map GPIO to IRQ"); |
| 131 | + |
| 132 | + err = devm_request_irq(dev, irq, max16150_irq_handler, |
| 133 | + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 134 | + "max16150_irq", max16150); |
| 135 | + if (err) |
| 136 | + return err; |
| 137 | + |
| 138 | + return input_register_device(max16150->input); |
| 139 | +} |
| 140 | + |
| 141 | +static const struct of_device_id max16150_of_match[] = { |
| 142 | + { .compatible = "adi,max16150a", .data = &max16150_variant_a }, |
| 143 | + { .compatible = "adi,max16150b", .data = &max16150_variant_b }, |
| 144 | + { .compatible = "adi,max16169a", .data = &max16150_variant_a }, |
| 145 | + { .compatible = "adi,max16169b", .data = &max16150_variant_b }, |
| 146 | + { } |
| 147 | +}; |
| 148 | +MODULE_DEVICE_TABLE(of, max16150_of_match); |
| 149 | + |
| 150 | +static struct platform_driver max16150_driver = { |
| 151 | + .probe = max16150_probe, |
| 152 | + .driver = { |
| 153 | + .name = "max16150", |
| 154 | + .of_match_table = max16150_of_match, |
| 155 | + }, |
| 156 | +}; |
| 157 | +module_platform_driver(max16150_driver); |
| 158 | + |
| 159 | +MODULE_AUTHOR( "Marc Paolo Sosa <[email protected]>"); |
| 160 | +MODULE_DESCRIPTION("MAX16150/MAX16169 Pushbutton Driver"); |
| 161 | +MODULE_LICENSE("GPL"); |
0 commit comments