1 /*
2  * Copyright (c) 2024, Kickmaker
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /* Address by default is 0x55 */
8 
9 #define DT_DRV_COMPAT sitronix_cf1133
10 
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/input/input.h>
14 #include <zephyr/logging/log.h>
15 #include <stdbool.h>
16 
17 LOG_MODULE_REGISTER(cf1133, CONFIG_INPUT_LOG_LEVEL);
18 
19 /* cf1133 used registers */
20 #define FIRMWARE_VERSION    0x0
21 #define STATUS_REG          0x1
22 #define DEVICE_CONTROL_REG  0x2
23 #define TIMEOUT_TO_IDLE_REG 0x3
24 #define XY_RESOLUTION_HIGH  0x4
25 #define X_RESOLUTION_LOW    0x5
26 #define Y_RESOLUTION_LOW    0x6
27 #define DEVICE_CONTROL_REG2 0x09
28 #define FIRMWARE_REVISION_3 0x0C
29 #define FIRMWARE_REVISION_2 0x0D
30 #define FIRMWARE_REVISION_1 0x0E
31 #define FIRMWARE_REVISION_0 0x0F
32 #define FINGERS             0x10
33 #define KEYS_REG            0x11
34 #define XY0_COORD_H         0x12
35 #define X0_COORD_L          0x13
36 #define Y0_COORD_L          0x14
37 #define I2C_PROTOCOL        0x3E
38 #define MAX_NUM_TOUCHES     0x3F
39 #define DATA_0_HIGH         0x40
40 #define DATA_0_LOW          0x41
41 #define MISC_CONTROL        0xF1
42 #define SMART_WAKE_UP_REG   0xF2
43 #define CHIP_ID             0xF4
44 #define PAGE_REG            0xFF
45 
46 /* Constants */
47 #define SUPPORTED_POINT			 0x1
48 #define PIXEL_DATA_LENGTH_B      0x3
49 #define PIXEL_DATA_LENGTH_A      0x4
50 #define SITRONIX_RESERVED_TYPE_0 0x0
51 #define SITRONIX_A_TYPE          0x1
52 #define SITRONIX_B_TYPE          0x2
53 
54 /* Mask */
55 #define ONE_D_SENSING_CONTROL_SHFT GENMASK(1, 1)
56 #define ONE_D_SENSING_CONTROL_BMSK GENMASK(1, 0)
57 #define I2C_PROTOCOL_BMSK          GENMASK(1, 0)
58 #define TOUCH_POINT_VALID_MSK	   GENMASK(7, 7)
59 
60 /* Offset for coordinates registers */
61 #define XY_COORD_H               0x0
62 #define X_COORD_L                0x1
63 #define Y_COORD_L                0x2
64 
65 /* CF1133 configuration (DT) */
66 struct cf1133_config {
67 	/** I2C bus. */
68 	struct i2c_dt_spec bus;
69 #ifdef CONFIG_INPUT_CF1133_INTERRUPT
70 	/** Interrupt GPIO information. */
71 	struct gpio_dt_spec int_gpio;
72 #endif
73 };
74 
75 /* CF1133 data */
76 struct cf1133_data {
77 	/** Device pointer. */
78 	const struct device *dev;
79 	/** Work queue (for deferred read). */
80 	struct k_work work;
81 #ifdef CONFIG_INPUT_CF1133_INTERRUPT
82 	/** Interrupt GPIO callback. */
83 	struct gpio_callback int_gpio_cb;
84 #else
85 	/* Timer (polling mode) */
86 	struct k_timer timer;
87 #endif
88 	/* Last pressed state */
89 	uint8_t pressed_old : 1;
90 	uint8_t pressed : 1;
91 
92 	int resolution_x;
93 	int resolution_y;
94 	uint8_t touch_protocol_type;
95 	uint8_t pixel_length;
96 	uint8_t chip_id;
97 };
98 
cf1133_get_chip_id(const struct device * dev)99 static int cf1133_get_chip_id(const struct device *dev)
100 {
101 	int ret;
102 	uint8_t buffer[3];
103 	uint8_t num_x;
104 	uint8_t num_y;
105 	const struct cf1133_config *config = dev->config;
106 	struct cf1133_data *data = dev->data;
107 
108 	ret = i2c_burst_read_dt(&config->bus, CHIP_ID, buffer, sizeof(buffer));
109 	if (ret < 0) {
110 		LOG_ERR("Read burst failed: %d", ret);
111 		return ret;
112 	}
113 	if (buffer[0] == 0) {
114 		if (buffer[1] + buffer[2] > 32) {
115 			data->chip_id = 2;
116 		} else {
117 			data->chip_id = 0;
118 		}
119 	} else {
120 		data->chip_id = buffer[0];
121 	}
122 	num_x = buffer[1];
123 	num_y = buffer[2];
124 	LOG_DBG("Chip ID = %d, num_x = %d, num_y = %d", data->chip_id, num_x, num_y);
125 
126 	return 0;
127 }
128 
cf1133_get_protocol_type(const struct device * dev)129 static int cf1133_get_protocol_type(const struct device *dev)
130 {
131 	int ret;
132 	uint8_t buffer;
133 	uint8_t sensing_mode;
134 	const struct cf1133_config *config = dev->config;
135 	struct cf1133_data *data = dev->data;
136 
137 	if (data->chip_id <= 3) {
138 		ret = i2c_reg_read_byte_dt(&config->bus, I2C_PROTOCOL, &buffer);
139 		if (ret < 0) {
140 			LOG_ERR("read i2c protocol failed: %d", ret);
141 			return ret;
142 		}
143 		data->touch_protocol_type = FIELD_GET(I2C_PROTOCOL_BMSK, buffer);
144 		LOG_DBG("i2c protocol = %d", data->touch_protocol_type);
145 		sensing_mode = FIELD_GET(ONE_D_SENSING_CONTROL_BMSK << ONE_D_SENSING_CONTROL_SHFT,
146 					 buffer);
147 		LOG_DBG("sensing mode = %d", sensing_mode);
148 	} else {
149 		data->touch_protocol_type = SITRONIX_A_TYPE;
150 		LOG_DBG("i2c protocol = %d", data->touch_protocol_type);
151 
152 		ret = i2c_reg_read_byte_dt(&config->bus, 0xf0, &buffer);
153 		if (ret < 0) {
154 			LOG_ERR("read sensing mode failed: (%d)", ret);
155 			return ret;
156 		}
157 		sensing_mode = FIELD_GET(ONE_D_SENSING_CONTROL_BMSK, buffer);
158 		LOG_DBG("sensing mode = %d", sensing_mode);
159 	}
160 	return 0;
161 }
162 
cf1133_ts_init(const struct device * dev)163 static int cf1133_ts_init(const struct device *dev)
164 {
165 	struct cf1133_data *data = dev->data;
166 	int ret;
167 
168 	/* Get device status before use to do at least once */
169 	ret = cf1133_get_chip_id(dev);
170 	if (ret < 0) {
171 		LOG_ERR("Read chip id failed: %d", ret);
172 		return ret;
173 	}
174 
175 	ret = cf1133_get_protocol_type(dev);
176 	if (ret < 0) {
177 		LOG_ERR("Read protocol failed: %d", ret);
178 		return ret;
179 	}
180 
181 	if (data->touch_protocol_type == SITRONIX_A_TYPE) {
182 		data->pixel_length = PIXEL_DATA_LENGTH_A;
183 	} else {
184 		data->pixel_length = PIXEL_DATA_LENGTH_B;
185 	}
186 	LOG_DBG("Pixel length: %d", data->pixel_length);
187 	return ret;
188 }
189 
cf1133_process(const struct device * dev)190 static int cf1133_process(const struct device *dev)
191 {
192 	const struct cf1133_config *config = dev->config;
193 	struct cf1133_data *data = dev->data;
194 	uint16_t y;
195 	uint16_t x;
196 	int ret;
197 	uint8_t buffer[1 + SUPPORTED_POINT * PIXEL_DATA_LENGTH_A];
198 
199 	/* Coordinates are retrieved for one valid touch point detected*/
200 	ret = i2c_burst_read_dt(&config->bus, KEYS_REG, buffer,
201 			      1 + SUPPORTED_POINT * data->pixel_length);
202 	if (ret < 0) {
203 		LOG_ERR("Read coordinates failed: %d", ret);
204 		return ret;
205 	}
206 
207 	/* Coordinates for one valid touch point */
208 	if (buffer[1 + XY_COORD_H] & TOUCH_POINT_VALID_MSK) {
209 		x = (uint16_t)(buffer[1 + XY_COORD_H] & 0x70) << 4 | buffer[1 + X_COORD_L];
210 		y = (uint16_t)(buffer[1 + XY_COORD_H] & 0x07) << 8 | buffer[1 + Y_COORD_L];
211 		data->pressed = true;
212 
213 		if (!data->pressed_old) {
214 			/* Finger pressed */
215 			input_report_abs(dev, INPUT_ABS_X, x, false, K_FOREVER);
216 			input_report_abs(dev, INPUT_ABS_Y, y, false, K_FOREVER);
217 			input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
218 			LOG_DBG("Finger is touching x = %i y = %i", x, y);
219 		} else if (data->pressed_old) {
220 			/* Continuous pressed */
221 			input_report_abs(dev, INPUT_ABS_X, x, false, K_FOREVER);
222 			input_report_abs(dev, INPUT_ABS_Y, y, false, K_FOREVER);
223 			LOG_DBG("Finger keeps touching x = %i y = %i", x, y);
224 		}
225 	} else {
226 		data->pressed = false;
227 
228 		if (data->pressed_old) {
229 			/* Finger removed */
230 			input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER);
231 			LOG_DBG("Finger is removed");
232 		}
233 	}
234 
235 	data->pressed_old = data->pressed;
236 
237 	return 0;
238 }
239 
cf1133_work_handler(struct k_work * work)240 static void cf1133_work_handler(struct k_work *work)
241 {
242 	struct cf1133_data *data = CONTAINER_OF(work, struct cf1133_data, work);
243 
244 	cf1133_process(data->dev);
245 }
246 
247 #ifdef CONFIG_INPUT_CF1133_INTERRUPT
248 
cf1133_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t pins)249 static void cf1133_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
250 {
251 	struct cf1133_data *data = CONTAINER_OF(cb, struct cf1133_data, int_gpio_cb);
252 
253 	k_work_submit(&data->work);
254 }
255 #else
cf1133_timer_handler(struct k_timer * timer)256 static void cf1133_timer_handler(struct k_timer *timer)
257 {
258 	struct cf1133_data *data = CONTAINER_OF(timer, struct cf1133_data, timer);
259 
260 	k_work_submit(&data->work);
261 }
262 #endif
263 
cf1133_init(const struct device * dev)264 static int cf1133_init(const struct device *dev)
265 {
266 	const struct cf1133_config *config = dev->config;
267 	struct cf1133_data *data = dev->data;
268 	int ret;
269 
270 	if (!i2c_is_ready_dt(&config->bus)) {
271 		LOG_ERR("I2C controller device not ready");
272 		return -ENODEV;
273 	}
274 
275 	data->dev = dev;
276 	k_work_init(&data->work, cf1133_work_handler);
277 
278 #ifdef CONFIG_INPUT_CF1133_INTERRUPT
279 
280 	LOG_DBG("Int conf for TS gpio: %d", &config->int_gpio);
281 
282 	if (!gpio_is_ready_dt(&config->int_gpio)) {
283 		LOG_ERR("Interrupt GPIO controller device not ready");
284 		return -ENODEV;
285 	}
286 
287 	ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
288 	if (ret < 0) {
289 		LOG_ERR("Could not configure interrupt GPIO pin");
290 		return ret;
291 	}
292 
293 	gpio_init_callback(&data->int_gpio_cb, cf1133_isr_handler, BIT(config->int_gpio.pin));
294 
295 	ret = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
296 	if (ret < 0) {
297 		LOG_ERR("Could not set gpio callback");
298 		return ret;
299 	}
300 
301 	ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
302 	if (ret < 0) {
303 		LOG_ERR("Could not configure interrupt GPIO interrupt.");
304 		return ret;
305 	}
306 #else
307 	LOG_DBG("Timer Mode");
308 	k_timer_init(&data->timer, cf1133_timer_handler, NULL);
309 	k_timer_start(&data->timer, K_MSEC(CONFIG_INPUT_CF1133_PERIOD_MS),
310 		      K_MSEC(CONFIG_INPUT_CF1133_PERIOD_MS));
311 #endif
312 
313 	ret = cf1133_ts_init(dev);
314 	if (ret < 0) {
315 		LOG_ERR("Init information of sensor failed: %d", ret);
316 		return ret;
317 	}
318 	return 0;
319 }
320 
321 #define CF1133_INIT(index)								\
322 	static const struct cf1133_config cf1133_config_##index = {			\
323 		.bus = I2C_DT_SPEC_INST_GET(index),					\
324 		IF_ENABLED(CONFIG_INPUT_CF1133_INTERRUPT,				\
325 		(.int_gpio = GPIO_DT_SPEC_INST_GET(index, int_gpios),			\
326 		))									\
327 		};									\
328 	static struct cf1133_data cf1133_data_##index;					\
329 											\
330 	DEVICE_DT_INST_DEFINE(index, cf1133_init, NULL, &cf1133_data_##index,		\
331 			      &cf1133_config_##index, POST_KERNEL,			\
332 			      CONFIG_INPUT_INIT_PRIORITY, NULL);
333 
334 DT_INST_FOREACH_STATUS_OKAY(CF1133_INIT);
335