Skip to content

Commit 56ccb2c

Browse files
committed
input: misc: add driver for max16150
MAX16150/MAX16169 nanoPower Pushbutton On/Off Controller Signed-off-by: Marc Paolo Sosa <marcpaolo.sosa@analog.com>
1 parent 0287e34 commit 56ccb2c

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

drivers/input/misc/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ config INPUT_E3X0_BUTTON
150150
To compile this driver as a module, choose M here: the
151151
module will be called e3x0_button.
152152

153+
config INPUT_MAX16150_PWRBUTTON
154+
tristate "MAX16150/MAX16169 Pushbutton driver"
155+
help
156+
Say Y here if you want to enable power key reporting via
157+
MAX16150/MAX16169 nanoPower Pushbutton On/Off Controller.
158+
159+
To compile this driver as a module, choose M here. The module will
160+
be called max16150.
161+
153162
config INPUT_PCSPKR
154163
tristate "PC Speaker support"
155164
depends on PCSPKR_PLATFORM

drivers/input/misc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ obj-$(CONFIG_INPUT_IQS7222) += iqs7222.o
4949
obj-$(CONFIG_INPUT_KEYSPAN_REMOTE) += keyspan_remote.o
5050
obj-$(CONFIG_INPUT_KXTJ9) += kxtj9.o
5151
obj-$(CONFIG_INPUT_M68K_BEEP) += m68kspkr.o
52+
obj-$(CONFIG_INPUT_MAX16150_PWRBUTTON) += max16150.o
5253
obj-$(CONFIG_INPUT_MAX77650_ONKEY) += max77650-onkey.o
5354
obj-$(CONFIG_INPUT_MAX77693_HAPTIC) += max77693-haptic.o
5455
obj-$(CONFIG_INPUT_MAX8925_ONKEY) += max8925_onkey.o

drivers/input/misc/max16150.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
51+
gpiod_set_value(max16150->clr_gpiod, 1);
52+
53+
input_report_key(max16150->input, max16150->keycode, 1);
54+
input_sync(max16150->input);
55+
56+
input_report_key(max16150->input, max16150->keycode, 0);
57+
input_sync(max16150->input);
58+
}
59+
60+
max16150->low = 0;
61+
}
62+
63+
return IRQ_HANDLED;
64+
}
65+
66+
static const struct max16150_chip_info max16150_variant_a = {
67+
.has_clr_gpio = true,
68+
};
69+
70+
static const struct max16150_chip_info max16150_variant_b = {
71+
.has_clr_gpio = false,
72+
};
73+
74+
static int max16150_probe(struct platform_device *pdev)
75+
{
76+
const struct max16150_chip_info *chip_info;
77+
struct max16150_device *max16150;
78+
struct device *dev = &pdev->dev;
79+
int err, irq, ret;
80+
u32 keycode;
81+
82+
chip_info = device_get_match_data(dev);
83+
if (!chip_info)
84+
return -EINVAL;
85+
86+
max16150 = devm_kzalloc(dev, sizeof(*max16150), GFP_KERNEL);
87+
if (!max16150)
88+
return -ENOMEM;
89+
90+
max16150->chip_info = chip_info;
91+
92+
max16150->input = devm_input_allocate_device(dev);
93+
if (!max16150->input)
94+
return -ENOMEM;
95+
96+
max16150->input->name = "MAX16150 Pushbutton";
97+
max16150->input->phys = "max16150/input0";
98+
max16150->input->id.bustype = BUS_HOST;
99+
100+
keycode = KEY_POWER;
101+
ret = device_property_read_u32(dev, "linux,code", &keycode);
102+
if (ret)
103+
return dev_err_probe(dev, ret, "Failed to get keycode\n");
104+
105+
max16150->keycode = keycode;
106+
107+
input_set_capability(max16150->input, EV_KEY, max16150->keycode);
108+
109+
max16150->gpiod = devm_gpiod_get(dev, "interrupt", GPIOD_IN);
110+
if (IS_ERR(max16150->gpiod))
111+
return dev_err_probe(dev, PTR_ERR(max16150->gpiod),
112+
"Failed to get interrupt GPIO\n");
113+
114+
if (chip_info->has_clr_gpio) {
115+
max16150->clr_gpiod = devm_gpiod_get(dev, "clr", GPIOD_OUT_HIGH);
116+
if (IS_ERR(max16150->clr_gpiod))
117+
return dev_err_probe(dev, PTR_ERR(max16150->clr_gpiod),
118+
"Failed to get clr GPIO\n");
119+
120+
if (!max16150->clr_gpiod)
121+
return dev_err_probe(dev, -ENODEV,
122+
"clr GPIO is mandatory\n");
123+
124+
if (max16150->clr_gpiod) {
125+
fsleep(1000);
126+
gpiod_set_value(max16150->clr_gpiod, 0);
127+
}
128+
}
129+
130+
irq = gpiod_to_irq(max16150->gpiod);
131+
if (irq < 0)
132+
return dev_err_probe(dev, irq,
133+
"MAX16150: Failed to map GPIO to IRQ");
134+
135+
err = devm_request_irq(dev, irq, max16150_irq_handler,
136+
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
137+
"max16150_irq", max16150);
138+
if (err)
139+
return err;
140+
141+
return input_register_device(max16150->input);
142+
}
143+
144+
static const struct of_device_id max16150_of_match[] = {
145+
{ .compatible = "adi,max16150a", .data = &max16150_variant_a },
146+
{ .compatible = "adi,max16150b", .data = &max16150_variant_b },
147+
{ .compatible = "adi,max16169a", .data = &max16150_variant_a },
148+
{ .compatible = "adi,max16169b", .data = &max16150_variant_b },
149+
{ }
150+
};
151+
MODULE_DEVICE_TABLE(of, max16150_of_match);
152+
153+
static struct platform_driver max16150_driver = {
154+
.probe = max16150_probe,
155+
.driver = {
156+
.name = "max16150",
157+
.of_match_table = max16150_of_match,
158+
},
159+
};
160+
module_platform_driver(max16150_driver);
161+
162+
MODULE_AUTHOR("Marc Paolo Sosa <marcpaolo.sosa@analog.com>");
163+
MODULE_DESCRIPTION("MAX16150/MAX16169 Pushbutton Driver");
164+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)