1 /*
2  * Copyright (c) 2022 Keiya Nobuta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT microchip_cap1203
8 
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/input/input.h>
12 
13 #include <zephyr/logging/log.h>
14 LOG_MODULE_REGISTER(cap1203, CONFIG_INPUT_LOG_LEVEL);
15 
16 #define REG_MAIN_CONTROL 0x0
17 #define CONTROL_INT 0x1
18 
19 #define REG_INPUT_STATUS 0x03
20 
21 #define REG_INTERRUPT_ENABLE 0x27
22 #define INTERRUPT_ENABLE     0x7
23 #define INTERRUPT_DISABLE    0x0
24 
25 #define TOUCH_INPUT_COUNT 3
26 
27 struct cap1203_config {
28 	struct i2c_dt_spec i2c;
29 	struct gpio_dt_spec int_gpio;
30 	const uint16_t *input_codes;
31 };
32 
33 struct cap1203_data {
34 	const struct device *dev;
35 	struct k_work work;
36 	/* Interrupt GPIO callback. */
37 	struct gpio_callback int_gpio_cb;
38 	uint8_t prev_input_state;
39 #ifdef CONFIG_INPUT_CAP1203_POLL
40 	/* Timer (polling mode). */
41 	struct k_timer timer;
42 #endif
43 };
44 
cap1203_clear_interrupt(const struct i2c_dt_spec * i2c)45 static int cap1203_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 
cap1203_enable_interrupt(const struct i2c_dt_spec * i2c,bool enable)59 static int cap1203_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 
cap1203_process(const struct device * dev)66 static int cap1203_process(const struct device *dev)
67 {
68 	const struct cap1203_config *config = dev->config;
69 	struct cap1203_data *data = dev->data;
70 	int r;
71 	uint8_t input;
72 	uint8_t single_input_state;
73 
74 	r = i2c_reg_read_byte_dt(&config->i2c, REG_INPUT_STATUS, &input);
75 	if (r < 0) {
76 		return r;
77 	}
78 
79 	for (uint8_t i = 0; i < TOUCH_INPUT_COUNT; i++) {
80 		single_input_state = input & BIT(i);
81 		if (single_input_state != (data->prev_input_state & BIT(i))) {
82 			input_report_key(dev, config->input_codes[i], single_input_state, true,
83 					 K_FOREVER);
84 		}
85 	}
86 	data->prev_input_state = input;
87 
88 	LOG_DBG("event: input: %d\n", input);
89 
90 	/*
91 	 * Clear INT bit to clear SENSOR INPUT STATUS bits.
92 	 * Note that this is also required in polling mode.
93 	 */
94 	r = cap1203_clear_interrupt(&config->i2c);
95 	if (r < 0) {
96 		return r;
97 	}
98 
99 	return 0;
100 }
101 
cap1203_work_handler(struct k_work * work)102 static void cap1203_work_handler(struct k_work *work)
103 {
104 	struct cap1203_data *data = CONTAINER_OF(work, struct cap1203_data, work);
105 
106 	cap1203_process(data->dev);
107 }
108 
cap1203_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t pins)109 static void cap1203_isr_handler(const struct device *dev,
110 				struct gpio_callback *cb, uint32_t pins)
111 {
112 	struct cap1203_data *data = CONTAINER_OF(cb, struct cap1203_data, int_gpio_cb);
113 
114 	k_work_submit(&data->work);
115 }
116 
117 #ifdef CONFIG_INPUT_CAP1203_POLL
cap1203_timer_handler(struct k_timer * timer)118 static void cap1203_timer_handler(struct k_timer *timer)
119 {
120 	struct cap1203_data *data = CONTAINER_OF(timer, struct cap1203_data, timer);
121 
122 	k_work_submit(&data->work);
123 }
124 #endif
125 
cap1203_init(const struct device * dev)126 static int cap1203_init(const struct device *dev)
127 {
128 	const struct cap1203_config *config = dev->config;
129 	struct cap1203_data *data = dev->data;
130 	int r;
131 
132 	if (!device_is_ready(config->i2c.bus)) {
133 		LOG_ERR("I2C controller device not ready");
134 		return -ENODEV;
135 	}
136 
137 	data->dev = dev;
138 
139 	k_work_init(&data->work, cap1203_work_handler);
140 
141 	if (config->int_gpio.port != NULL) {
142 		if (!gpio_is_ready_dt(&config->int_gpio)) {
143 			LOG_ERR("Interrupt GPIO controller device not ready");
144 			return -ENODEV;
145 		}
146 
147 		r = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
148 		if (r < 0) {
149 			LOG_ERR("Could not confighure interrupt GPIO pin");
150 			return r;
151 		}
152 
153 		r = gpio_pin_interrupt_configure_dt(&config->int_gpio,
154 						   GPIO_INT_EDGE_TO_ACTIVE);
155 		if (r < 0) {
156 			LOG_ERR("Could not configure interrupt GPIO interrupt");
157 			return r;
158 		}
159 
160 		gpio_init_callback(&data->int_gpio_cb, cap1203_isr_handler,
161 				   BIT(config->int_gpio.pin));
162 
163 		r = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
164 		if (r < 0) {
165 			LOG_ERR("Could not set gpio callback");
166 			return r;
167 		}
168 
169 		r = cap1203_clear_interrupt(&config->i2c);
170 		if (r < 0) {
171 			LOG_ERR("Could not clear interrupt");
172 			return r;
173 		}
174 
175 		r = cap1203_enable_interrupt(&config->i2c, true);
176 		if (r < 0) {
177 			LOG_ERR("Could not configure interrupt");
178 			return r;
179 		}
180 	}
181 #ifdef CONFIG_INPUT_CAP1203_POLL
182 	else {
183 		k_timer_init(&data->timer, cap1203_timer_handler, NULL);
184 
185 		r = cap1203_enable_interrupt(&config->i2c, false);
186 		if (r < 0) {
187 			LOG_ERR("Could not configure interrupt");
188 			return r;
189 		}
190 
191 		k_timer_start(&data->timer, K_MSEC(CONFIG_INPUT_CAP1203_PERIOD),
192 			      K_MSEC(CONFIG_INPUT_CAP1203_PERIOD));
193 	}
194 #endif
195 
196 	return 0;
197 }
198 
199 #define CAP1203_INIT(index)                                                                        \
200 	static const uint16_t cap1203_input_codes_##inst[] = DT_INST_PROP(index, input_codes);     \
201 	BUILD_ASSERT(DT_INST_PROP_LEN(index, input_codes) == TOUCH_INPUT_COUNT);                   \
202 	static const struct cap1203_config cap1203_config_##index = {                              \
203 		.i2c = I2C_DT_SPEC_INST_GET(index),                                                \
204 		.int_gpio = GPIO_DT_SPEC_INST_GET_OR(index, int_gpios, {0}),                       \
205 		.input_codes = cap1203_input_codes_##inst,                                         \
206 	};                                                                                         \
207 	static struct cap1203_data cap1203_data_##index;                                           \
208 	DEVICE_DT_INST_DEFINE(index, cap1203_init, NULL, &cap1203_data_##index,                    \
209 			      &cap1203_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY,    \
210 			      NULL);
211 
212 DT_INST_FOREACH_STATUS_OKAY(CAP1203_INIT)
213