1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_GT9XX_H
9 #define HPM_GT9XX_H
10 #include "board.h"
11 #include "hpm_common.h"
12 #include "hpm_i2c_drv.h"
13 
14 #ifdef BOARD_GT9XX_ADDR
15 /* if i2c addres is specified by board, use it */
16 #define GT9XX_I2C_ADDR BOARD_GT9XX_ADDR
17 /* no auto probe in this case */
18 #define GT9XX_NO_AUTO_PROBE 1
19 #else
20 #undef GT9XX_I2C_ADDR
21 
22 /* enable auto probe */
23 #ifndef GT9XX_NO_AUTO_PROBE
24 #define GT9XX_NO_AUTO_PROBE 0
25 #endif
26 
27 /* i2c device address candidates */
28 #define GT9XX_I2C_ADDR0 (0x14U)
29 #define GT9XX_I2C_ADDR1 (0x5DU)
30 #endif
31 
32 #define GT9XX_PRODUCT_ID (0x313139U)
33 /*
34  * GT9XX registers at operation mode
35  */
36 
37 #define GT9XX_CMD                    (0x8040U)
38 #define   GT9XX_CMD_READ_COORD_STAT  (0U)
39 #define   GT9XX_CMD_READ_RAW_DATA    (1U)
40 #define   GT9XX_CMD_SOFT_RESET       (2U)
41 #define   GT9XX_CMD_READ_SCREEN_OFF  (5U)
42 
43 #define GT9XX_CONFIG                    (0x8047U)
44 
45 #define GT9XX_ID_B0                     (0x8140U)
46 #define GT9XX_ID_B1                     (0x8141U)
47 #define GT9XX_ID_B2                     (0x8142U)
48 #define GT9XX_ID_B4                     (0x8143U)
49 #define GT9XX_FW_VERSION_L              (0x8144U)
50 #define GT9XX_FW_VERSION_H              (0x8145U)
51 #define GT9XX_TOUCH_XL                  (0x8146U)
52 #define GT9XX_TOUCH_XH                  (0x8147U)
53 #define GT9XX_TOUCH_YL                  (0x8148U)
54 #define GT9XX_TOUCH_YH                  (0x8149U)
55 #define GT9XX_VENDOR_ID                 (0x814AU)
56 #define GT9XX_STATUS                    (0x814EU)
57 #define GT9XX_GET_STATUS_NUM_OF_POINTS(x)  ((x) & 0xFU)
58 #define GT9XX_GET_STATUS_LARGE_DETECT(x)   (((x) & 0x40U) >> 6)
59 #define GT9XX_GET_STATUS_BUFFER_STAT(x)    (((x) & 0x80U) >> 7)
60 #define GT9XX_FIRST_POINT               (0x814FU)
61 
62 #define GT9XX_MAX_TOUCH_POINTS             (5U)
63 #define GT9XX_CONFIG_DATA_SIZE             (186U)
64 #define GT9XX_CONFIG_DATA_CONFIG_VERSION   (0U)
65 #define GT9XX_CONFIG_DATA_RESOLUTION_XL    (1U)
66 #define GT9XX_CONFIG_DATA_RESOLUTION_XH    (2U)
67 #define GT9XX_CONFIG_DATA_RESOLUTION_YL    (3U)
68 #define GT9XX_CONFIG_DATA_RESOLUTION_YH    (4U)
69 #define GT9XX_CONFIG_DATA_TOUCH_NUMBER     (5U)
70 #define GT9XX_CONFIG_DATA_MODULE_SWITCH1   (6U)
71 
72 typedef struct {
73     uint8_t track_id;
74     uint8_t x_l;
75     uint8_t x_h;
76     uint8_t y_l;
77     uint8_t y_h;
78     uint8_t size_l;
79     uint8_t size_h;
80     uint8_t reserved;
81 } gt9xx_touch_point_t;
82 
83 typedef struct {
84     uint8_t status;
85     gt9xx_touch_point_t points[GT9XX_MAX_TOUCH_POINTS];
86 } gt9xx_touch_data_t;
87 
88 typedef struct {
89     I2C_Type *ptr;
90     uint16_t abs_x_max;
91     uint16_t abs_y_max;
92     bool exchange_xy;
93     bool reverse_x;
94     bool reverse_y;
95 } gt9xx_context_t;
96 
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100 
101 /*
102  * gt9xx initialization routine
103  */
104 hpm_stat_t gt9xx_init(gt9xx_context_t *context, uint16_t width, uint16_t height);
105 
106 /*
107  * gt9xx read touch data
108  */
109 hpm_stat_t gt9xx_read_touch_data(gt9xx_context_t *context,
110                                   gt9xx_touch_data_t *touch_data);
111 
112 /*
113  * gt9xx read data
114  */
115 hpm_stat_t gt9xx_read_data(gt9xx_context_t *context, uint16_t addr,
116                             uint8_t *buf, uint32_t size);
117 
118 /*
119  * gt9xx write value to given register
120  */
121 hpm_stat_t gt9xx_write_register(gt9xx_context_t *context,
122                                  uint16_t reg, uint8_t val);
123 
124 /*
125  * gt9xx read value of given register
126  */
127 hpm_stat_t gt9xx_read_register(gt9xx_context_t *context, uint16_t reg, uint8_t *buf);
128 
129 /*
130  * gt9xx read config data
131  */
132 hpm_stat_t gt9xx_read_config(gt9xx_context_t *context, uint8_t *buf, uint8_t size);
133 #ifdef __cplusplus
134 }
135 #endif
136 #endif /* HPM_GT9XX_H */
137