1 /*
2  * Copyright (c) 2020 NXP
3  * Copyright (c) 2020 Mark Olsson <mark@markolsson.se>
4  * Copyright (c) 2020 Teslabs Engineering S.L.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define DT_DRV_COMPAT goodix_gt911
10 
11 #include <zephyr/drivers/kscan.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/drivers/gpio.h>
14 #include <zephyr/sys/byteorder.h>
15 
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(gt911, CONFIG_KSCAN_LOG_LEVEL);
18 
19 /* GT911 used registers */
20 #define DEVICE_ID           __bswap_16(0x8140U)
21 #define REG_STATUS		    __bswap_16(0x814EU)
22 #define REG_FIRST_POINT		__bswap_16(0x814FU)
23 
24 /* REG_TD_STATUS: Touch points. */
25 #define TOUCH_POINTS_MSK	0x0FU
26 
27 /* REG_TD_STATUS: Pressed. */
28 #define TOUCH_STATUS_MSK    (1 << 7U)
29 
30 /* The GT911's config */
31 #define GT911_CONFIG_REG         __bswap_16(0x8047U)
32 #define REG_CONFIG_VERSION GT911_CONFIG_REG
33 #define REG_CONFIG_SIZE (186U)
34 
35 /** GT911 configuration (DT). */
36 struct gt911_config {
37 	/** I2C bus. */
38 	struct i2c_dt_spec bus;
39 	struct gpio_dt_spec rst_gpio;
40 	/** Interrupt GPIO information. */
41 	struct gpio_dt_spec int_gpio;
42 };
43 
44 /** GT911 data. */
45 struct gt911_data {
46 	/** Device pointer. */
47 	const struct device *dev;
48 	/** KSCAN Callback. */
49 	kscan_callback_t callback;
50 	/** Work queue (for deferred read). */
51 	struct k_work work;
52 #ifdef CONFIG_KSCAN_GT911_INTERRUPT
53 	/** Interrupt GPIO callback. */
54 	struct gpio_callback int_gpio_cb;
55 #else
56 	/** Timer (polling mode). */
57 	struct k_timer timer;
58 #endif
59 };
60 
61 /** gt911 point reg */
62 struct  gt911_point_reg_t {
63 	uint8_t id;       /*!< Track ID. */
64 	uint8_t lowX;     /*!< Low byte of x coordinate. */
65 	uint8_t highX;    /*!< High byte of x coordinate. */
66 	uint8_t lowY;     /*!< Low byte of y coordinate. */
67 	uint8_t highY;    /*!< High byte of x coordinate. */
68 	uint8_t lowSize;  /*!< Low byte of point size. */
69 	uint8_t highSize; /*!< High byte of point size. */
70 	uint8_t reserved; /*!< Reserved. */
71 };
72 
gt911_process(const struct device * dev)73 static int gt911_process(const struct device *dev)
74 {
75 	const struct gt911_config *config = dev->config;
76 	struct gt911_data *data = dev->data;
77 
78 	int r;
79 	uint16_t reg_addr;
80 	uint8_t status;
81 	uint8_t points;
82 	struct gt911_point_reg_t pointRegs;
83 	uint16_t row, col;
84 	bool pressed;
85 
86 	/* obtain number of touch points (NOTE: multi-touch ignored) */
87 	reg_addr = REG_STATUS;
88 	r = i2c_write_read_dt(&config->bus, &reg_addr, sizeof(reg_addr),
89 							&status, sizeof(status));
90 	if (r < 0) {
91 		return r;
92 	}
93 
94 	points = status & TOUCH_POINTS_MSK;
95 	if (points != 0U && points != 1U && (0 != (status & TOUCH_STATUS_MSK))) {
96 		return 0;
97 	}
98 
99 	if (!(status & TOUCH_STATUS_MSK)) {
100 		/* Status bit not set, ignore this event */
101 		return 0;
102 	}
103 	/* need to clear the status */
104 	uint8_t clear_buffer[3] = {(uint8_t)REG_STATUS, (uint8_t)(REG_STATUS >> 8), 0};
105 
106 	r = i2c_write_dt(&config->bus, clear_buffer, sizeof(clear_buffer));
107 	if (r < 0) {
108 		return r;
109 	}
110 
111 	/* obtain first point X, Y coordinates and event from:
112 	 * REG_P1_XH, REG_P1_XL, REG_P1_YH, REG_P1_YL.
113 	 */
114 	reg_addr = REG_FIRST_POINT;
115 	r = i2c_write_read_dt(&config->bus, &reg_addr, sizeof(reg_addr),
116 							&pointRegs, sizeof(pointRegs));
117 	if (r < 0) {
118 		return r;
119 	}
120 
121 	pressed = (points == 1);
122 	row = ((pointRegs.highY) << 8U) | pointRegs.lowY;
123 	col = ((pointRegs.highX) << 8U) | pointRegs.lowX;
124 
125 	LOG_DBG("pressed: %d, row: %d, col: %d", pressed, row, col);
126 
127 	data->callback(dev, row, col, pressed);
128 
129 	return 0;
130 }
131 
gt911_work_handler(struct k_work * work)132 static void gt911_work_handler(struct k_work *work)
133 {
134 	struct gt911_data *data = CONTAINER_OF(work, struct gt911_data, work);
135 
136 	gt911_process(data->dev);
137 }
138 
139 #ifdef CONFIG_KSCAN_GT911_INTERRUPT
gt911_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t pins)140 static void gt911_isr_handler(const struct device *dev,
141 			       struct gpio_callback *cb, uint32_t pins)
142 {
143 	struct gt911_data *data = CONTAINER_OF(cb, struct gt911_data, int_gpio_cb);
144 
145 	k_work_submit(&data->work);
146 }
147 #else
gt911_timer_handler(struct k_timer * timer)148 static void gt911_timer_handler(struct k_timer *timer)
149 {
150 	struct gt911_data *data = CONTAINER_OF(timer, struct gt911_data, timer);
151 
152 	k_work_submit(&data->work);
153 }
154 #endif
155 
gt911_configure(const struct device * dev,kscan_callback_t callback)156 static int gt911_configure(const struct device *dev,
157 			    kscan_callback_t callback)
158 {
159 	struct gt911_data *data = dev->data;
160 
161 	if (!callback) {
162 		LOG_ERR("Invalid callback (NULL)");
163 		return -EINVAL;
164 	}
165 
166 	data->callback = callback;
167 
168 	return 0;
169 }
170 
gt911_enable_callback(const struct device * dev)171 static int gt911_enable_callback(const struct device *dev)
172 {
173 	struct gt911_data *data = dev->data;
174 
175 #ifdef CONFIG_KSCAN_GT911_INTERRUPT
176 	const struct gt911_config *config = dev->config;
177 
178 	gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
179 #else
180 	k_timer_start(&data->timer, K_MSEC(CONFIG_KSCAN_GT911_PERIOD),
181 		      K_MSEC(CONFIG_KSCAN_GT911_PERIOD));
182 #endif
183 
184 	return 0;
185 }
186 
gt911_disable_callback(const struct device * dev)187 static int gt911_disable_callback(const struct device *dev)
188 {
189 	struct gt911_data *data = dev->data;
190 
191 #ifdef CONFIG_KSCAN_GT911_INTERRUPT
192 	const struct gt911_config *config = dev->config;
193 
194 	gpio_remove_callback(config->int_gpio.port, &data->int_gpio_cb);
195 #else
196 	k_timer_stop(&data->timer);
197 #endif
198 
199 	return 0;
200 }
201 
gt911_get_firmware_checksum(const uint8_t * firmware)202 static uint8_t gt911_get_firmware_checksum(const uint8_t *firmware)
203 {
204 	uint8_t sum = 0;
205 	uint16_t i  = 0;
206 
207 	for (i = 0; i < REG_CONFIG_SIZE - 2U; i++) {
208 		sum += (*firmware);
209 		firmware++;
210 	}
211 
212 	return (~sum + 1U);
213 }
214 
gt911_verify_firmware(const uint8_t * firmware)215 static bool gt911_verify_firmware(const uint8_t *firmware)
216 {
217 	return ((firmware[REG_CONFIG_VERSION - GT911_CONFIG_REG] != 0U) &&
218 		(gt911_get_firmware_checksum(firmware) == firmware[REG_CONFIG_SIZE - 2U]));
219 }
220 
gt911_init(const struct device * dev)221 static int gt911_init(const struct device *dev)
222 {
223 	const struct gt911_config *config = dev->config;
224 	struct gt911_data *data = dev->data;
225 
226 	if (!device_is_ready(config->bus.bus)) {
227 		LOG_ERR("I2C controller device not ready");
228 		return -ENODEV;
229 	}
230 
231 	data->dev = dev;
232 
233 	k_work_init(&data->work, gt911_work_handler);
234 
235 	int r;
236 
237 	r = gpio_pin_configure_dt(&config->rst_gpio, GPIO_OUTPUT_INACTIVE);
238 	if (r < 0) {
239 		LOG_ERR("Could not configure reset GPIO pin");
240 		return r;
241 	}
242 
243 	/* we need to configure the int-pin to 0, in order toenter the AddressModel0 */
244 	r = gpio_pin_configure_dt(&config->int_gpio, GPIO_OUTPUT_INACTIVE);
245 	if (r < 0) {
246 		LOG_ERR("Could not configure int GPIO pin");
247 		return r;
248 	}
249 	/* Delay at least 10 ms after power on before we configure gt911 */
250 	k_sleep(K_MSEC(20));
251 	/* reset the device and confgiure the addr mode0 */
252 	gpio_pin_set_dt(&config->rst_gpio, 0);
253 	/* hold down at least 1us, 1ms here */
254 	k_sleep(K_MSEC(1));
255 	gpio_pin_set_dt(&config->rst_gpio, 1);
256 	/* hold down at least 5ms, before set the int pin low */
257 	k_sleep(K_MSEC(5));
258 	gpio_pin_set_dt(&config->int_gpio, 0);
259 	/* hold down 50ms to make sure the address available */
260 	k_sleep(K_MSEC(50));
261 	if (!device_is_ready(config->int_gpio.port)) {
262 		LOG_ERR("Interrupt GPIO controller device not ready");
263 		return -ENODEV;
264 	}
265 
266 	r = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
267 	if (r < 0) {
268 		LOG_ERR("Could not configure interrupt GPIO pin");
269 		return r;
270 	}
271 
272 #ifdef CONFIG_KSCAN_GT911_INTERRUPT
273 	r = gpio_pin_interrupt_configure_dt(&config->int_gpio,
274 					    GPIO_INT_EDGE_TO_ACTIVE);
275 	if (r < 0) {
276 		LOG_ERR("Could not configure interrupt GPIO interrupt.");
277 		return r;
278 	}
279 
280 	gpio_init_callback(&data->int_gpio_cb, gt911_isr_handler,
281 			   BIT(config->int_gpio.pin));
282 #else
283 	k_timer_init(&data->timer, gt911_timer_handler, NULL);
284 #endif
285 
286 	/* check the Device ID first: '911' */
287 	uint32_t reg_id = 0;
288 	uint16_t reg_addr = DEVICE_ID;
289 
290 	r = i2c_write_read_dt(&config->bus, &reg_addr, sizeof(reg_addr),
291 							&reg_id, sizeof(reg_id));
292 	if (r < 0) {
293 		return r;
294 	}
295 	if (reg_id != 0x00313139U) {
296 		LOG_ERR("The Devide ID is not correct");
297 		return -ENODEV;
298 	}
299 
300 	/* need to setup the firmware first: read and write */
301 	uint8_t gt911Config[REG_CONFIG_SIZE + 2] = {
302 		(uint8_t)GT911_CONFIG_REG, (uint8_t)(GT911_CONFIG_REG >> 8)
303 	};
304 
305 	reg_addr = GT911_CONFIG_REG;
306 	r = i2c_write_read_dt(&config->bus, &reg_addr, sizeof(reg_addr),
307 							gt911Config + 2, REG_CONFIG_SIZE);
308 	if (r < 0) {
309 		return r;
310 	}
311 	if (!gt911_verify_firmware(gt911Config + 2)) {
312 		return -ENODEV;
313 	}
314 
315 	gt911Config[REG_CONFIG_SIZE] = gt911_get_firmware_checksum(gt911Config + 2);
316 	gt911Config[REG_CONFIG_SIZE + 1] = 1;
317 
318 	r = i2c_write_dt(&config->bus, gt911Config, sizeof(gt911Config));
319 	if (r < 0) {
320 		return r;
321 	}
322 
323 	return 0;
324 }
325 
326 static const struct kscan_driver_api gt911_driver_api = {
327 	.config = gt911_configure,
328 	.enable_callback = gt911_enable_callback,
329 	.disable_callback = gt911_disable_callback,
330 };
331 
332 #define GT911_INIT(index)                                                     \
333 	static const struct gt911_config gt911_config_##index = {	       \
334 		.bus = I2C_DT_SPEC_INST_GET(index),			       \
335 		.rst_gpio = GPIO_DT_SPEC_INST_GET(index, reset_gpios), \
336 		.int_gpio = GPIO_DT_SPEC_INST_GET(index, irq_gpios)	       \
337 	};								       \
338 	static struct gt911_data gt911_data_##index;			       \
339 	DEVICE_DT_INST_DEFINE(index, gt911_init, NULL,			       \
340 			    &gt911_data_##index, &gt911_config_##index,      \
341 			    POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY,	       \
342 			    &gt911_driver_api);
343 
344 DT_INST_FOREACH_STATUS_OKAY(GT911_INIT)
345