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/gpio.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/input/input.h>
14 #include <zephyr/sys/byteorder.h>
15
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(gt911, CONFIG_INPUT_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 #define GT911_PRODUCT_ID (0x00313139U)
35
36 /** GT911 configuration (DT). */
37 struct gt911_config {
38 /** I2C bus. */
39 struct i2c_dt_spec bus;
40 struct gpio_dt_spec rst_gpio;
41 /** Interrupt GPIO information. */
42 struct gpio_dt_spec int_gpio;
43 /* Alternate fallback I2C address */
44 uint8_t alt_addr;
45 };
46
47 /** GT911 data. */
48 struct gt911_data {
49 /** Device pointer. */
50 const struct device *dev;
51 /** Work queue (for deferred read). */
52 struct k_work work;
53 /** Actual device I2C address */
54 uint8_t actual_address;
55 #ifdef CONFIG_INPUT_GT911_INTERRUPT
56 /** Interrupt GPIO callback. */
57 struct gpio_callback int_gpio_cb;
58 #else
59 /** Timer (polling mode). */
60 struct k_timer timer;
61 #endif
62 };
63
64 /** gt911 point reg */
65 struct gt911_point_reg_t {
66 uint8_t id; /*!< Track ID. */
67 uint8_t lowX; /*!< Low byte of x coordinate. */
68 uint8_t highX; /*!< High byte of x coordinate. */
69 uint8_t lowY; /*!< Low byte of y coordinate. */
70 uint8_t highY; /*!< High byte of x coordinate. */
71 uint8_t lowSize; /*!< Low byte of point size. */
72 uint8_t highSize; /*!< High byte of point size. */
73 uint8_t reserved; /*!< Reserved. */
74 };
75
76 /*
77 * Device-specific wrappers around i2c_write_dt and i2c_write_read_dt.
78 * These wrappers handle the case where the GT911 did not accept the requested
79 * I2C address, and the alternate I2C address is used.
80 */
gt911_i2c_write(const struct device * dev,const uint8_t * buf,uint32_t num_bytes)81 static int gt911_i2c_write(const struct device *dev,
82 const uint8_t *buf, uint32_t num_bytes)
83 {
84 const struct gt911_config *config = dev->config;
85 struct gt911_data *data = dev->data;
86
87 return i2c_write(config->bus.bus, buf, num_bytes, data->actual_address);
88 }
89
gt911_i2c_write_read(const struct device * dev,const void * write_buf,size_t num_write,void * read_buf,size_t num_read)90 static int gt911_i2c_write_read(const struct device *dev,
91 const void *write_buf, size_t num_write,
92 void *read_buf, size_t num_read)
93 {
94 const struct gt911_config *config = dev->config;
95 struct gt911_data *data = dev->data;
96
97 return i2c_write_read(config->bus.bus, data->actual_address, write_buf,
98 num_write, read_buf, num_read);
99 }
100
gt911_process(const struct device * dev)101 static int gt911_process(const struct device *dev)
102 {
103 int r;
104 uint16_t reg_addr;
105 uint8_t status;
106 uint8_t points;
107 struct gt911_point_reg_t pointRegs;
108 uint16_t row, col;
109 bool pressed;
110
111 /* obtain number of touch points (NOTE: multi-touch ignored) */
112 reg_addr = REG_STATUS;
113 r = gt911_i2c_write_read(dev, ®_addr, sizeof(reg_addr),
114 &status, sizeof(status));
115 if (r < 0) {
116 return r;
117 }
118
119 points = status & TOUCH_POINTS_MSK;
120 if (points != 0U && points != 1U && (0 != (status & TOUCH_STATUS_MSK))) {
121 points = 1;
122 }
123
124 if (!(status & TOUCH_STATUS_MSK)) {
125 /* Status bit not set, ignore this event */
126 return 0;
127 }
128 /* need to clear the status */
129 uint8_t clear_buffer[3] = {(uint8_t)REG_STATUS, (uint8_t)(REG_STATUS >> 8), 0};
130
131 r = gt911_i2c_write(dev, clear_buffer, sizeof(clear_buffer));
132 if (r < 0) {
133 return r;
134 }
135
136 /* obtain first point X, Y coordinates and event from:
137 * REG_P1_XH, REG_P1_XL, REG_P1_YH, REG_P1_YL.
138 */
139 reg_addr = REG_FIRST_POINT;
140 r = gt911_i2c_write_read(dev, ®_addr, sizeof(reg_addr),
141 &pointRegs, sizeof(pointRegs));
142 if (r < 0) {
143 return r;
144 }
145
146 pressed = (points == 1);
147 row = ((pointRegs.highY) << 8U) | pointRegs.lowY;
148 col = ((pointRegs.highX) << 8U) | pointRegs.lowX;
149
150 LOG_DBG("pressed: %d, row: %d, col: %d", pressed, row, col);
151
152 if (pressed) {
153 input_report_abs(dev, INPUT_ABS_X, col, false, K_FOREVER);
154 input_report_abs(dev, INPUT_ABS_Y, row, false, K_FOREVER);
155 input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
156 } else {
157 input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER);
158 }
159
160 return 0;
161 }
162
gt911_work_handler(struct k_work * work)163 static void gt911_work_handler(struct k_work *work)
164 {
165 struct gt911_data *data = CONTAINER_OF(work, struct gt911_data, work);
166
167 gt911_process(data->dev);
168 }
169
170 #ifdef CONFIG_INPUT_GT911_INTERRUPT
gt911_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t pins)171 static void gt911_isr_handler(const struct device *dev,
172 struct gpio_callback *cb, uint32_t pins)
173 {
174 struct gt911_data *data = CONTAINER_OF(cb, struct gt911_data, int_gpio_cb);
175
176 k_work_submit(&data->work);
177 }
178 #else
gt911_timer_handler(struct k_timer * timer)179 static void gt911_timer_handler(struct k_timer *timer)
180 {
181 struct gt911_data *data = CONTAINER_OF(timer, struct gt911_data, timer);
182
183 k_work_submit(&data->work);
184 }
185 #endif
186
gt911_get_firmware_checksum(const uint8_t * firmware)187 static uint8_t gt911_get_firmware_checksum(const uint8_t *firmware)
188 {
189 uint8_t sum = 0;
190 uint16_t i = 0;
191
192 for (i = 0; i < REG_CONFIG_SIZE - 2U; i++) {
193 sum += (*firmware);
194 firmware++;
195 }
196
197 return (~sum + 1U);
198 }
199
gt911_verify_firmware(const uint8_t * firmware)200 static bool gt911_verify_firmware(const uint8_t *firmware)
201 {
202 return ((firmware[REG_CONFIG_VERSION - GT911_CONFIG_REG] != 0U) &&
203 (gt911_get_firmware_checksum(firmware) == firmware[REG_CONFIG_SIZE - 2U]));
204 }
205
gt911_init(const struct device * dev)206 static int gt911_init(const struct device *dev)
207 {
208 const struct gt911_config *config = dev->config;
209 struct gt911_data *data = dev->data;
210
211 if (!i2c_is_ready_dt(&config->bus)) {
212 LOG_ERR("I2C controller device not ready");
213 return -ENODEV;
214 }
215
216 data->dev = dev;
217 data->actual_address = config->bus.addr;
218
219 k_work_init(&data->work, gt911_work_handler);
220
221 int r;
222
223 if (!gpio_is_ready_dt(&config->int_gpio)) {
224 LOG_ERR("Interrupt GPIO controller device not ready");
225 return -ENODEV;
226 }
227
228 if (config->rst_gpio.port != NULL) {
229 if (!gpio_is_ready_dt(&config->rst_gpio)) {
230 LOG_ERR("Reset GPIO controller device not ready");
231 return -ENODEV;
232 }
233
234 r = gpio_pin_configure_dt(&config->rst_gpio, GPIO_OUTPUT_ACTIVE);
235 if (r < 0) {
236 LOG_ERR("Could not configure reset GPIO pin");
237 return r;
238 }
239 }
240
241 if (config->alt_addr == 0x0) {
242 /*
243 * We need to configure the int-pin to 0, in order to enter the
244 * AddressMode0. Keeping the INT pin low during the reset sequence
245 * should result in the device selecting an I2C address of 0x5D.
246 * Note we skip this step if an alternate I2C address is set,
247 * and fall through to probing for the actual address.
248 */
249 r = gpio_pin_configure_dt(&config->int_gpio, GPIO_OUTPUT_INACTIVE);
250 if (r < 0) {
251 LOG_ERR("Could not configure int GPIO pin");
252 return r;
253 }
254 }
255 /* Delay at least 10 ms after power on before we configure gt911 */
256 k_sleep(K_MSEC(20));
257 if (config->rst_gpio.port != NULL) {
258 /* reset the device and confgiure the addr mode0 */
259 gpio_pin_set_dt(&config->rst_gpio, 1);
260 /* hold down at least 1us, 1ms here */
261 k_sleep(K_MSEC(1));
262 gpio_pin_set_dt(&config->rst_gpio, 0);
263 /* hold down at least 5ms. This is the point the INT pin must be low. */
264 k_sleep(K_MSEC(5));
265 }
266 /* hold down 50ms to make sure the address available */
267 k_sleep(K_MSEC(50));
268
269 r = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
270 if (r < 0) {
271 LOG_ERR("Could not configure interrupt GPIO pin");
272 return r;
273 }
274
275 #ifdef CONFIG_INPUT_GT911_INTERRUPT
276 r = gpio_pin_interrupt_configure_dt(&config->int_gpio,
277 GPIO_INT_EDGE_TO_ACTIVE);
278 if (r < 0) {
279 LOG_ERR("Could not configure interrupt GPIO interrupt.");
280 return r;
281 }
282
283 gpio_init_callback(&data->int_gpio_cb, gt911_isr_handler,
284 BIT(config->int_gpio.pin));
285 #else
286 k_timer_init(&data->timer, gt911_timer_handler, NULL);
287 #endif
288
289 /* check the Device ID first: '911' */
290 uint32_t reg_id = 0;
291 uint16_t reg_addr = DEVICE_ID;
292
293 if (config->alt_addr != 0x0) {
294 /*
295 * The level of the INT pin during reset is used by the GT911
296 * to select the I2C address mode. If an alternate I2C address
297 * is set, we should probe the GT911 to determine which address
298 * it actually selected. This is useful for boards that do not
299 * route the INT pin, or can only read it as an input (IE when
300 * using a level shifter).
301 */
302 r = gt911_i2c_write_read(dev, ®_addr, sizeof(reg_addr),
303 ®_id, sizeof(reg_id));
304 if (r < 0) {
305 /* Try alternate address */
306 data->actual_address = config->alt_addr;
307 r = gt911_i2c_write_read(dev, ®_addr,
308 sizeof(reg_addr),
309 ®_id, sizeof(reg_id));
310 LOG_INF("Device did not accept I2C address, "
311 "updated to 0x%02X", data->actual_address);
312 }
313 } else {
314 r = gt911_i2c_write_read(dev, ®_addr, sizeof(reg_addr),
315 ®_id, sizeof(reg_id));
316 }
317 if (r < 0) {
318 LOG_ERR("Device did not respond to I2C request");
319 return r;
320 }
321 if (reg_id != GT911_PRODUCT_ID) {
322 LOG_ERR("The Device ID is not correct");
323 return -ENODEV;
324 }
325
326 /* need to setup the firmware first: read and write */
327 uint8_t gt911Config[REG_CONFIG_SIZE + 2] = {
328 (uint8_t)GT911_CONFIG_REG, (uint8_t)(GT911_CONFIG_REG >> 8)
329 };
330
331 reg_addr = GT911_CONFIG_REG;
332 r = gt911_i2c_write_read(dev, ®_addr, sizeof(reg_addr),
333 gt911Config + 2, REG_CONFIG_SIZE);
334 if (r < 0) {
335 return r;
336 }
337 if (!gt911_verify_firmware(gt911Config + 2)) {
338 return -ENODEV;
339 }
340
341 gt911Config[REG_CONFIG_SIZE] = gt911_get_firmware_checksum(gt911Config + 2);
342 gt911Config[REG_CONFIG_SIZE + 1] = 1;
343
344 r = gt911_i2c_write(dev, gt911Config, sizeof(gt911Config));
345 if (r < 0) {
346 return r;
347 }
348
349 #ifdef CONFIG_INPUT_GT911_INTERRUPT
350 r = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
351 if (r < 0) {
352 LOG_ERR("Could not set gpio callback");
353 return r;
354 }
355 #else
356 k_timer_start(&data->timer, K_MSEC(CONFIG_INPUT_GT911_PERIOD_MS),
357 K_MSEC(CONFIG_INPUT_GT911_PERIOD_MS));
358 #endif
359
360 return 0;
361 }
362
363 #define GT911_INIT(index) \
364 static const struct gt911_config gt911_config_##index = { \
365 .bus = I2C_DT_SPEC_INST_GET(index), \
366 .rst_gpio = GPIO_DT_SPEC_INST_GET_OR(index, reset_gpios, {0}), \
367 .int_gpio = GPIO_DT_SPEC_INST_GET(index, irq_gpios), \
368 .alt_addr = DT_INST_PROP_OR(index, alt_addr, 0), \
369 }; \
370 static struct gt911_data gt911_data_##index; \
371 DEVICE_DT_INST_DEFINE(index, gt911_init, NULL, \
372 >911_data_##index, >911_config_##index, \
373 POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \
374 NULL);
375
376 DT_INST_FOREACH_STATUS_OKAY(GT911_INIT)
377