1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include <securec.h>
10 #include "osal_mem.h"
11 #include "hdf_device_desc.h"
12 #include "hdf_log.h"
13 #include "hdf_touch.h"
14 #include "input_i2c_ops.h"
15 #include "touch_gt911.h"
16
17 #define MAX_POINT 5
18
ChipInit(ChipDevice * device)19 static int32_t ChipInit(ChipDevice *device)
20 {
21 return HDF_SUCCESS;
22 }
23
ChipResume(ChipDevice * device)24 static int32_t ChipResume(ChipDevice *device)
25 {
26 return HDF_SUCCESS;
27 }
28
ChipSuspend(ChipDevice * device)29 static int32_t ChipSuspend(ChipDevice *device)
30 {
31 return HDF_SUCCESS;
32 }
33
ChipDetect(ChipDevice * device)34 static int32_t ChipDetect(ChipDevice *device)
35 {
36 int32_t ret;
37 int32_t version;
38 int32_t xSolution;
39 int32_t ySolution;
40 InputI2cClient *i2cClient = &device->driver->i2cClient;
41 uint8_t buf[GT_CFG_INFO_LEN] = {0};
42 uint8_t reg[GT_ADDR_LEN] = {0};
43 reg[0] = (GT_CFG_INFO_ADDR >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
44 reg[1] = GT_CFG_INFO_ADDR & ONE_BYTE_MASK;
45
46 ret = InputI2cRead(i2cClient, reg, GT_ADDR_LEN, buf, GT_CFG_INFO_LEN);
47 if (ret < 0) {
48 HDF_LOGE("%s: read chip version failed", __func__);
49 return HDF_FAILURE;
50 }
51
52 version = (buf[GT_FW_VER_HIGH] << ONE_BYTE_OFFSET) | buf[GT_FW_VER_LOW];
53 xSolution = (buf[GT_SOLU_X_HIGH] << ONE_BYTE_OFFSET) | buf[GT_SOLU_X_LOW];
54 ySolution = (buf[GT_SOLU_Y_HIGH] << ONE_BYTE_OFFSET) | buf[GT_SOLU_Y_LOW];
55 #if defined(CONFIG_ARCH_ROCKCHIP)
56 if (buf[GT_PROD_ID_1ST] != '5' || buf[GT_PROD_ID_2ND] != '6' || \
57 buf[GT_PROD_ID_3RD] != '8' || buf[GT_PROD_ID_4TH] != '8') {
58 HDF_LOGE("%s: ID wrong,IC FW version is 0x%x", __func__, version);
59 return HDF_FAILURE;
60 }
61 #endif
62 HDF_LOGI("%s: IC FW version is 0x%x", __func__, version);
63 if (buf[GT_FW_VER_HIGH] == 0x0) {
64 HDF_LOGI("Product ID : %c%c%c_%02x%02x, xSol = %d, ySol = %d", buf[GT_PROD_ID_1ST], buf[GT_PROD_ID_2ND],
65 buf[GT_PROD_ID_3RD], buf[GT_FW_VER_HIGH], buf[GT_FW_VER_LOW], xSolution, ySolution);
66 } else {
67 HDF_LOGI("Product_ID: %c%c%c%c_%02x%02x, x_sol = %d, y_sol = %d", buf[GT_PROD_ID_1ST], buf[GT_PROD_ID_2ND],
68 buf[GT_PROD_ID_3RD], buf[GT_PROD_ID_4TH], buf[GT_FW_VER_HIGH], buf[GT_FW_VER_LOW], xSolution, ySolution);
69 }
70
71 (void)ChipInit(device);
72 (void)ChipResume(device);
73 (void)ChipSuspend(device);
74 return HDF_SUCCESS;
75 }
76
ChipCleanBuffer(InputI2cClient * i2cClient)77 static int ChipCleanBuffer(InputI2cClient *i2cClient)
78 {
79 int32_t ret;
80 uint8_t writeBuf[GT_CLEAN_DATA_LEN];
81 writeBuf[GT_REG_HIGH_POS] = (GT_BUF_STATE_ADDR >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
82 writeBuf[GT_REG_LOW_POS] = GT_BUF_STATE_ADDR & ONE_BYTE_MASK;
83 writeBuf[GT_CLEAN_POS] = GT_CLEAN_FLAG;
84 ret = InputI2cWrite(i2cClient, writeBuf, GT_CLEAN_DATA_LEN);
85 if (ret != HDF_SUCCESS) {
86 HDF_LOGE("%s: InputI2cWrite failed, ret = %d", __func__, ret);
87 }
88 return ret;
89 }
90
91 #define X_OFFSET 1
92
ParsePointData(ChipDevice * device,FrameData * frame,uint8_t * buf,uint8_t pointNum)93 static void ParsePointData(ChipDevice *device, FrameData *frame, uint8_t *buf, uint8_t pointNum)
94 {
95 int32_t chipVer = device->chipCfg->chipVersion;
96 int32_t resX = device->driver->boardCfg->attr.resolutionX;
97 int32_t resY = device->driver->boardCfg->attr.resolutionY;
98 int32_t i;
99
100 for (i = 0; i < pointNum; i++) {
101 if (chipVer == 0) { // chipversion A:gt911_zsj5p5
102 frame->fingers[i].trackId = buf[GT_POINT_SIZE * i + GT_TRACK_ID];
103 #if defined(CONFIG_ARCH_SPRD)
104 frame->fingers[i].y = (resX - 1 - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
105 ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) <<
106 ONE_BYTE_OFFSET))) * resY / resX;
107 frame->fingers[i].x = ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
108 ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) <<
109 ONE_BYTE_OFFSET)) * resX / resY;
110 #elif defined(CONFIG_ARCH_ROCKCHIP)
111 frame->fingers[i].x = resX - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
112 ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
113 frame->fingers[i].y = resY - ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
114 ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
115 #else
116 frame->fingers[i].y = (buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
117 ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET);
118 frame->fingers[i].x = (buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
119 ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET);
120 #endif
121 if (frame->fingers[i].x == 0) {
122 frame->fingers[i].x = X_OFFSET;
123 }
124 } else if (chipVer == 1) { // chipversion B:gt911_zsj4p0
125 frame->fingers[i].x = resX - 1 - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
126 ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
127 frame->fingers[i].y = resY - 1 - ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
128 ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
129 } else { // chipversion C:gt911_tg7p0
130 frame->fingers[i].x = resX - 1 - ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
131 ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
132 frame->fingers[i].y = resY - 1 - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
133 ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
134 }
135 frame->fingers[i].valid = true;
136 }
137 }
138
ChipDataHandle(ChipDevice * device)139 static int32_t ChipDataHandle(ChipDevice *device)
140 {
141 int32_t ret;
142 uint8_t touchStatus = 0;
143 uint8_t pointNum;
144 uint8_t buf[GT_POINT_SIZE * MAX_SUPPORT_POINT] = {0};
145 InputI2cClient *i2cClient = &device->driver->i2cClient;
146 uint8_t reg[GT_ADDR_LEN] = {0};
147 FrameData *frame = &device->driver->frameData;
148
149 reg[0] = (GT_BUF_STATE_ADDR >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
150 reg[1] = GT_BUF_STATE_ADDR & ONE_BYTE_MASK;
151 ret = InputI2cRead(i2cClient, reg, GT_ADDR_LEN, &touchStatus, 1);
152 if (ret < 0 || touchStatus == GT_EVENT_INVALID) {
153 HDF_LOGE("InputI2cRead fail || ouchStatus is GT_EVENT_INVALID %s", __func__);
154 return HDF_FAILURE;
155 }
156
157 OsalMutexLock(&device->driver->mutex);
158 (void)memset_s(frame, sizeof(FrameData), 0, sizeof(FrameData));
159 if (touchStatus == GT_EVENT_UP) {
160 frame->realPointNum = 0;
161 frame->definedEvent = TOUCH_UP;
162 goto EXIT;
163 }
164
165 reg[0] = (GT_X_LOW_BYTE_BASE >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
166 reg[1] = GT_X_LOW_BYTE_BASE & ONE_BYTE_MASK;
167 pointNum = touchStatus & GT_FINGER_NUM_MASK;
168 if (pointNum == 0 || pointNum > MAX_SUPPORT_POINT) {
169 HDF_LOGE("%s: pointNum is invalid, %u", __func__, pointNum);
170 (void)ChipCleanBuffer(i2cClient);
171 OsalMutexUnlock(&device->driver->mutex);
172 return HDF_FAILURE;
173 }
174 frame->realPointNum = pointNum;
175 frame->definedEvent = TOUCH_DOWN;
176 (void)InputI2cRead(i2cClient, reg, GT_ADDR_LEN, buf, GT_POINT_SIZE * pointNum);
177 ParsePointData(device, frame, buf, pointNum);
178
179 EXIT:
180 OsalMutexUnlock(&device->driver->mutex);
181 if (ChipCleanBuffer(i2cClient) != HDF_SUCCESS) {
182 HDF_LOGE("ChipCleanBuffer fail %s", __func__);
183 return HDF_FAILURE;
184 }
185 return HDF_SUCCESS;
186 }
187
UpdateFirmware(ChipDevice * device)188 static int32_t UpdateFirmware(ChipDevice *device)
189 {
190 int32_t ret;
191 InputI2cClient *i2cClient = &device->driver->i2cClient;
192 #if defined(CONFIG_ARCH_ROCKCHIP)
193 uint8_t buf[1] = {0};
194 uint8_t reg[GT_ADDR_LEN] = {0};
195
196 reg[0] = (GTP_REG_CONFIG_DATA >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
197 reg[1] = GTP_REG_CONFIG_DATA & ONE_BYTE_MASK;
198 ret = InputI2cRead(i2cClient, reg, GT_ADDR_LEN, buf, 1);
199 if (ret < 0) {
200 HDF_LOGE("%s: read fw version failed", __func__);
201 return HDF_FAILURE;
202 }
203
204 HDF_LOGI("%s: buf[0]=0x%x", __func__, buf[0]);
205 if (buf[0] == firmWareParm[FIRMWARE_3RD]) {
206 HDF_LOGI("%s: needn't update fw version", __func__);
207 return HDF_SUCCESS;
208 }
209 #endif
210 ret = InputI2cWrite(i2cClient, firmWareParm, FIRMWARE_LEN);
211 if (ret < 0) {
212 return HDF_FAILURE;
213 }
214 HDF_LOGI("%s: update firmware success\n", __func__);
215 return HDF_SUCCESS;
216 }
217
SetAbility(ChipDevice * device)218 static void SetAbility(ChipDevice *device)
219 {
220 device->driver->inputDev->abilitySet.devProp[0] = SET_BIT(INPUT_PROP_DIRECT);
221 device->driver->inputDev->abilitySet.eventType[0] = SET_BIT(EV_SYN) |
222 SET_BIT(EV_KEY) | SET_BIT(EV_ABS);
223 device->driver->inputDev->abilitySet.absCode[0] = SET_BIT(ABS_X) | SET_BIT(ABS_Y);
224 device->driver->inputDev->abilitySet.absCode[1] = SET_BIT(ABS_MT_POSITION_X) |
225 SET_BIT(ABS_MT_POSITION_Y) | SET_BIT(ABS_MT_TRACKING_ID);
226 device->driver->inputDev->abilitySet.keyCode[KEY_CODE_4TH] = SET_BIT(KEY_UP) | SET_BIT(KEY_DOWN);
227 device->driver->inputDev->attrSet.axisInfo[ABS_X].min = 0;
228 device->driver->inputDev->attrSet.axisInfo[ABS_X].max = device->boardCfg->attr.resolutionX - 1;
229 device->driver->inputDev->attrSet.axisInfo[ABS_X].range = 0;
230 device->driver->inputDev->attrSet.axisInfo[ABS_Y].min = 0;
231 device->driver->inputDev->attrSet.axisInfo[ABS_Y].max = device->boardCfg->attr.resolutionY - 1;
232 device->driver->inputDev->attrSet.axisInfo[ABS_Y].range = 0;
233 device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_X].min = 0;
234 device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_X].max = device->boardCfg->attr.resolutionX - 1;
235 device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_X].range = 0;
236 device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_Y].min = 0;
237 device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_Y].max = device->boardCfg->attr.resolutionY - 1;
238 device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_Y].range = 0;
239 device->driver->inputDev->attrSet.axisInfo[ABS_MT_TRACKING_ID].max = MAX_POINT;
240 }
241
242 static struct TouchChipOps g_gt911ChipOps = {
243 .Init = ChipInit,
244 .Detect = ChipDetect,
245 .Resume = ChipResume,
246 .Suspend = ChipSuspend,
247 .DataHandle = ChipDataHandle,
248 .UpdateFirmware = UpdateFirmware,
249 .SetAbility = SetAbility,
250 };
251
ChipConfigInstance(struct HdfDeviceObject * device)252 static TouchChipCfg *ChipConfigInstance(struct HdfDeviceObject *device)
253 {
254 TouchChipCfg *chipCfg = (TouchChipCfg *)OsalMemAlloc(sizeof(TouchChipCfg));
255 if (chipCfg == NULL) {
256 HDF_LOGE("%s: instance chip config failed", __func__);
257 return NULL;
258 }
259 (void)memset_s(chipCfg, sizeof(TouchChipCfg), 0, sizeof(TouchChipCfg));
260
261 if (ParseTouchChipConfig(device->property, chipCfg) != HDF_SUCCESS) {
262 HDF_LOGE("%s: parse chip config failed", __func__);
263 OsalMemFree(chipCfg);
264 chipCfg = NULL;
265 }
266 return chipCfg;
267 }
268
ChipDeviceInstance(void)269 static ChipDevice *ChipDeviceInstance(void)
270 {
271 ChipDevice *chipDev = (ChipDevice *)OsalMemAlloc(sizeof(ChipDevice));
272 if (chipDev == NULL) {
273 HDF_LOGE("%s: instance chip device failed", __func__);
274 return NULL;
275 }
276 (void)memset_s(chipDev, sizeof(ChipDevice), 0, sizeof(ChipDevice));
277 return chipDev;
278 }
279
FreeChipConfig(TouchChipCfg * config)280 static void FreeChipConfig(TouchChipCfg *config)
281 {
282 if (config == NULL) {
283 HDF_LOGE("%s: param is null", __func__);
284 return;
285 }
286 if (config->pwrSeq.pwrOn.buf != NULL) {
287 OsalMemFree(config->pwrSeq.pwrOn.buf);
288 }
289
290 if (config->pwrSeq.pwrOff.buf != NULL) {
291 OsalMemFree(config->pwrSeq.pwrOff.buf);
292 }
293
294 if (config->pwrSeq.resume.buf != NULL) {
295 OsalMemFree(config->pwrSeq.resume.buf);
296 }
297
298 if (config->pwrSeq.suspend.buf != NULL) {
299 OsalMemFree(config->pwrSeq.suspend.buf);
300 }
301
302 OsalMemFree(config);
303 }
304
HdfGoodixChipInit(struct HdfDeviceObject * device)305 static int32_t HdfGoodixChipInit(struct HdfDeviceObject *device)
306 {
307 TouchChipCfg *chipCfg = NULL;
308 ChipDevice *chipDev = NULL;
309
310 HDF_LOGI("%s: enter", __func__);
311 if (device == NULL) {
312 return HDF_ERR_INVALID_PARAM;
313 }
314
315 chipCfg = ChipConfigInstance(device);
316 if (chipCfg == NULL) {
317 return HDF_ERR_MALLOC_FAIL;
318 }
319
320 chipDev = ChipDeviceInstance();
321 if (chipDev == NULL) {
322 goto EXIT;
323 }
324
325 chipDev->chipCfg = chipCfg;
326 chipDev->ops = &g_gt911ChipOps;
327 chipDev->chipName = chipCfg->chipName;
328 chipDev->vendorName = chipCfg->vendorName;
329 device->priv = (void *)chipDev;
330
331 if (RegisterTouchChipDevice(chipDev) != HDF_SUCCESS) {
332 goto EXIT1;
333 }
334 HDF_LOGI("%s: exit succ, chipName = %s", __func__, chipCfg->chipName);
335 return HDF_SUCCESS;
336
337 EXIT1:
338 OsalMemFree(chipDev);
339 EXIT:
340 FreeChipConfig(chipCfg);
341 return HDF_FAILURE;
342 }
343
HdfGoodixChipRelease(struct HdfDeviceObject * device)344 static void HdfGoodixChipRelease(struct HdfDeviceObject *device)
345 {
346 if (device == NULL || device->priv == NULL) {
347 HDF_LOGE("%s: param is null", __func__);
348 return;
349 }
350 HDF_LOGI("%s: goodix chip is release", __func__);
351 }
352
353 struct HdfDriverEntry g_touchGoodixChipEntry = {
354 .moduleVersion = 1,
355 .moduleName = "HDF_TOUCH_GT911",
356 .Init = HdfGoodixChipInit,
357 .Release = HdfGoodixChipRelease,
358 };
359
360 HDF_INIT(g_touchGoodixChipEntry);
361