1 /*
2 * Copyright (c) 2022 Keiya Nobuta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT microchip_cap12xx
8
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/input/input.h>
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(cap12xx, CONFIG_INPUT_LOG_LEVEL);
14
15 #define REG_MAIN_CONTROL 0x00
16 #define CONTROL_INT 0x01
17
18 #define REG_INPUT_STATUS 0x03
19
20 #define REG_INTERRUPT_ENABLE 0x27
21 #define INTERRUPT_ENABLE 0xFF
22 #define INTERRUPT_DISABLE 0x00
23
24 #define REG_REPEAT_ENABLE 0x28
25 #define REPEAT_ENABLE 0xFF
26 #define REPEAT_DISABLE 0x00
27
28 struct cap12xx_config {
29 struct i2c_dt_spec i2c;
30 const uint8_t input_channels;
31 const uint16_t *input_codes;
32 struct gpio_dt_spec *int_gpio;
33 bool repeat;
34 const uint16_t poll_interval_ms;
35 };
36
37 struct cap12xx_data {
38 const struct device *dev;
39 struct k_work work;
40 uint8_t prev_input_state;
41 struct gpio_callback int_gpio_cb;
42 struct k_timer poll_timer;
43 };
44
cap12xx_clear_interrupt(const struct i2c_dt_spec * i2c)45 static int cap12xx_clear_interrupt(const struct i2c_dt_spec *i2c)
46 {
47 uint8_t ctrl;
48 int r;
49
50 r = i2c_reg_read_byte_dt(i2c, REG_MAIN_CONTROL, &ctrl);
51 if (r < 0) {
52 return r;
53 }
54
55 ctrl = ctrl & ~CONTROL_INT;
56 return i2c_reg_write_byte_dt(i2c, REG_MAIN_CONTROL, ctrl);
57 }
58
cap12xx_enable_interrupt(const struct i2c_dt_spec * i2c,bool enable)59 static int cap12xx_enable_interrupt(const struct i2c_dt_spec *i2c, bool enable)
60 {
61 uint8_t intr = enable ? INTERRUPT_ENABLE : INTERRUPT_DISABLE;
62
63 return i2c_reg_write_byte_dt(i2c, REG_INTERRUPT_ENABLE, intr);
64 }
65
cap12xx_process(const struct device * dev)66 static int cap12xx_process(const struct device *dev)
67 {
68 const struct cap12xx_config *config = dev->config;
69 struct cap12xx_data *data = dev->data;
70 int r;
71 uint8_t input_state;
72
73 /*
74 * Clear INT bit to clear SENSOR INPUT STATUS bits.
75 * Note that this is also required in polling mode.
76 */
77 r = cap12xx_clear_interrupt(&config->i2c);
78
79 if (r < 0) {
80 return r;
81 }
82 r = i2c_reg_read_byte_dt(&config->i2c, REG_INPUT_STATUS, &input_state);
83 if (r < 0) {
84 return r;
85 }
86
87 if (config->int_gpio == NULL) {
88 if (data->prev_input_state == input_state) {
89 return 0;
90 }
91 }
92
93 for (uint8_t i = 0; i < config->input_channels; i++) {
94 if (input_state & BIT(i)) {
95 input_report_key(dev, config->input_codes[i], input_state & BIT(i), true,
96 K_FOREVER);
97 } else if (data->prev_input_state & BIT(i)) {
98 input_report_key(dev, config->input_codes[i], input_state & BIT(i), true,
99 K_FOREVER);
100 }
101 }
102 data->prev_input_state = input_state;
103
104 return 0;
105 }
106
cap12xx_work_handler(struct k_work * work)107 static void cap12xx_work_handler(struct k_work *work)
108 {
109 struct cap12xx_data *data = CONTAINER_OF(work, struct cap12xx_data, work);
110
111 cap12xx_process(data->dev);
112 }
113
cap12xx_timer_handler(struct k_timer * poll_timer)114 static void cap12xx_timer_handler(struct k_timer *poll_timer)
115 {
116 struct cap12xx_data *data = CONTAINER_OF(poll_timer, struct cap12xx_data, poll_timer);
117
118 k_work_submit(&data->work);
119 }
120
cap12xx_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t pins)121 static void cap12xx_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
122 {
123 struct cap12xx_data *data = CONTAINER_OF(cb, struct cap12xx_data, int_gpio_cb);
124
125 k_work_submit(&data->work);
126 }
127
cap12xx_init(const struct device * dev)128 static int cap12xx_init(const struct device *dev)
129 {
130 const struct cap12xx_config *config = dev->config;
131 struct cap12xx_data *data = dev->data;
132 int r;
133
134 if (!device_is_ready(config->i2c.bus)) {
135 LOG_ERR("I2C controller device not ready");
136 return -ENODEV;
137 }
138
139 data->dev = dev;
140
141 k_work_init(&data->work, cap12xx_work_handler);
142
143 if (config->int_gpio == NULL) {
144 LOG_DBG("cap12xx driver in polling mode");
145 k_timer_init(&data->poll_timer, cap12xx_timer_handler, NULL);
146 r = cap12xx_enable_interrupt(&config->i2c, true);
147 if (r < 0) {
148 LOG_ERR("Could not configure interrupt");
149 return r;
150 }
151 k_timer_start(&data->poll_timer, K_MSEC(config->poll_interval_ms),
152 K_MSEC(config->poll_interval_ms));
153 } else {
154 LOG_DBG("cap12xx driver in interrupt mode");
155 if (!gpio_is_ready_dt(config->int_gpio)) {
156 LOG_ERR("Interrupt GPIO controller device not ready (missing device tree "
157 "node?)");
158 return -ENODEV;
159 }
160
161 r = gpio_pin_configure_dt(config->int_gpio, GPIO_INPUT);
162 if (r < 0) {
163 LOG_ERR("Could not configure interrupt GPIO pin");
164 return r;
165 }
166
167 r = gpio_pin_interrupt_configure_dt(config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
168 if (r < 0) {
169 LOG_ERR("Could not configure interrupt GPIO interrupt");
170 return r;
171 }
172
173 gpio_init_callback(&data->int_gpio_cb, cap12xx_isr_handler,
174 BIT(config->int_gpio->pin));
175
176 r = gpio_add_callback_dt(config->int_gpio, &data->int_gpio_cb);
177 if (r < 0) {
178 LOG_ERR("Could not set gpio callback");
179 return r;
180 }
181
182 r = cap12xx_clear_interrupt(&config->i2c);
183 if (r < 0) {
184 LOG_ERR("Could not clear interrupt");
185 return r;
186 }
187 r = cap12xx_enable_interrupt(&config->i2c, true);
188 if (r < 0) {
189 LOG_ERR("Could not configure interrupt");
190 return r;
191 }
192 if (config->repeat) {
193 r = i2c_reg_write_byte_dt(&config->i2c, REG_REPEAT_ENABLE, REPEAT_ENABLE);
194 if (r < 0) {
195 LOG_ERR("Could not disable repeated interrupts");
196 return r;
197 }
198 LOG_DBG("cap12xx enabled repeated interrupts");
199 } else {
200 r = i2c_reg_write_byte_dt(&config->i2c, REG_REPEAT_ENABLE, REPEAT_DISABLE);
201 if (r < 0) {
202 LOG_ERR("Could not enable repeated interrupts");
203 return r;
204 }
205 LOG_DBG("cap12xx disabled repeated interrupts");
206 }
207 }
208 LOG_DBG("%d channels configured", config->input_channels);
209 return 0;
210 }
211
212 #define CAP12XX_INIT(index) \
213 IF_ENABLED(DT_INST_NODE_HAS_PROP(index, int_gpios), ( \
214 static struct gpio_dt_spec cap12xx_int_gpio_##index = \
215 GPIO_DT_SPEC_INST_GET(index, int_gpios);)) \
216 static const uint16_t cap12xx_input_codes_##index[] = DT_INST_PROP(index, input_codes); \
217 static const struct cap12xx_config cap12xx_config_##index = { \
218 .i2c = I2C_DT_SPEC_INST_GET(index), \
219 .input_channels = DT_INST_PROP_LEN(index, input_codes), \
220 .input_codes = cap12xx_input_codes_##index, \
221 IF_ENABLED(DT_INST_NODE_HAS_PROP(index, int_gpios), ( \
222 .int_gpio = &cap12xx_int_gpio_##index,)) \
223 .repeat = DT_INST_PROP(index, repeat), \
224 .poll_interval_ms = DT_INST_PROP_OR(index, poll_interval_ms, 10)}; \
225 static struct cap12xx_data cap12xx_data_##index; \
226 DEVICE_DT_INST_DEFINE(index, cap12xx_init, NULL, &cap12xx_data_##index, \
227 &cap12xx_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \
228 NULL);
229
230 DT_INST_FOREACH_STATUS_OKAY(CAP12XX_INIT)
231