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 #if defined(CONFIG_ARCH_ROCKCHIP)
19 #define GT5688_FIRMWARE_VERSION    256
20 #define GT911_FIRMWARE_VERSION     4192
21 #endif
22 
23 #define FOUR_BYTES 4
24 #define FOUR_BYTES_BIT 32
25 
ChipInit(ChipDevice * device)26 static int32_t ChipInit(ChipDevice *device)
27 {
28     return HDF_SUCCESS;
29 }
30 
ChipResume(ChipDevice * device)31 static int32_t ChipResume(ChipDevice *device)
32 {
33     return HDF_SUCCESS;
34 }
35 
ChipSuspend(ChipDevice * device)36 static int32_t ChipSuspend(ChipDevice *device)
37 {
38     return HDF_SUCCESS;
39 }
40 
ChipDetect(ChipDevice * device)41 static int32_t ChipDetect(ChipDevice *device)
42 {
43     int32_t ret;
44     int32_t version;
45     int32_t xSolution;
46     int32_t ySolution;
47     InputI2cClient *i2cClient = &device->driver->i2cClient;
48     uint8_t buf[GT_CFG_INFO_LEN] = {0};
49     uint8_t reg[GT_ADDR_LEN] = {0};
50     reg[0] = (GT_CFG_INFO_ADDR >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
51     reg[1] = GT_CFG_INFO_ADDR & ONE_BYTE_MASK;
52 
53     ret = InputI2cRead(i2cClient, reg, GT_ADDR_LEN, buf, GT_CFG_INFO_LEN);
54     if (ret < 0) {
55         HDF_LOGE("%s: read chip version failed", __func__);
56         return HDF_FAILURE;
57     }
58 
59     version = (buf[GT_FW_VER_HIGH] << ONE_BYTE_OFFSET) | buf[GT_FW_VER_LOW];
60     xSolution = (buf[GT_SOLU_X_HIGH] << ONE_BYTE_OFFSET) | buf[GT_SOLU_X_LOW];
61     ySolution = (buf[GT_SOLU_Y_HIGH] << ONE_BYTE_OFFSET) | buf[GT_SOLU_Y_LOW];
62 #if defined(CONFIG_ARCH_ROCKCHIP)
63     HDF_LOGI("%s:IC FW version is %d", __func__, version);
64     switch (version) {
65         case GT5688_FIRMWARE_VERSION:
66             HDF_LOGI("%s:TOUCH IC is GT5688", __func__);
67             break;
68         case GT911_FIRMWARE_VERSION:
69             HDF_LOGI("%s:TOUCH IC is GT911", __func__);
70             break;
71         default:
72             HDF_LOGE("%s: ID wrong,IC FW version is %d", __func__, version);
73             return HDF_FAILURE;
74     }
75 #endif
76     if (buf[GT_FW_VER_HIGH] == 0x0) {
77         HDF_LOGI("Product ID : %c%c%c_%02x%02x, xSol = %d, ySol = %d", buf[GT_PROD_ID_1ST], buf[GT_PROD_ID_2ND],
78             buf[GT_PROD_ID_3RD], buf[GT_FW_VER_HIGH], buf[GT_FW_VER_LOW], xSolution, ySolution);
79     } else {
80         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],
81             buf[GT_PROD_ID_3RD], buf[GT_PROD_ID_4TH], buf[GT_FW_VER_HIGH], buf[GT_FW_VER_LOW], xSolution, ySolution);
82     }
83 
84     (void)ChipInit(device);
85     (void)ChipResume(device);
86     (void)ChipSuspend(device);
87     return HDF_SUCCESS;
88 }
89 
ChipCleanBuffer(InputI2cClient * i2cClient)90 static int ChipCleanBuffer(InputI2cClient *i2cClient)
91 {
92     int32_t ret;
93     uint8_t writeBuf[GT_CLEAN_DATA_LEN];
94     writeBuf[GT_REG_HIGH_POS] = (GT_BUF_STATE_ADDR >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
95     writeBuf[GT_REG_LOW_POS] = GT_BUF_STATE_ADDR & ONE_BYTE_MASK;
96     writeBuf[GT_CLEAN_POS] = GT_CLEAN_FLAG;
97     ret = InputI2cWrite(i2cClient, writeBuf, GT_CLEAN_DATA_LEN);
98     if (ret != HDF_SUCCESS) {
99         HDF_LOGE("%s: InputI2cWrite failed, ret = %d", __func__, ret);
100     }
101     return ret;
102 }
103 
104 #define X_OFFSET    1
105 
ChipVersionDefault(ChipDevice * device,FrameData * frame,const uint8_t * buf,uint8_t pointNum)106 static void ChipVersionDefault(ChipDevice *device, FrameData *frame, const uint8_t *buf, uint8_t pointNum)
107 {
108     for (uint8_t i = 0; i < pointNum; i++) {         // chipversion  A:gt911_zsj5p5
109         frame->fingers[i].trackId = buf[GT_POINT_SIZE * i + GT_TRACK_ID];
110 #if defined(CONFIG_ARCH_SPRD)
111         int32_t resX = device->driver->boardCfg->attr.resolutionX;
112         int32_t resY = device->driver->boardCfg->attr.resolutionY;
113         frame->fingers[i].y = (resX - 1 - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
114                               ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) <<
115                               ONE_BYTE_OFFSET))) * resY / resX;
116         frame->fingers[i].x = ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
117                               ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) <<
118                               ONE_BYTE_OFFSET)) * resX / resY;
119 #elif defined(CONFIG_ARCH_ROCKCHIP)
120         int32_t resX = device->driver->boardCfg->attr.resolutionX;
121         int32_t resY = device->driver->boardCfg->attr.resolutionY;
122         frame->fingers[i].x = resX - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
123                               ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
124         frame->fingers[i].y = resY - ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
125                               ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
126         if (frame->fingers[i].y < GT_Y_OFFSET_A) {
127             frame->fingers[i].y += CORRECTION_VALUE_A;
128         } else if (frame->fingers[i].y < GT_Y_OFFSET_B) {
129             frame->fingers[i].y += CORRECTION_VALUE_B;
130         } else if (frame->fingers[i].y < GT_Y_OFFSET_C) {
131             frame->fingers[i].y += CORRECTION_VALUE_C;
132         }
133 #elif defined(LOSCFG_PLATFORM_STM32MP157)
134         frame->fingers[i].x = (buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
135                               ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET);
136         frame->fingers[i].y = (buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
137                               ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET);
138 #else
139         frame->fingers[i].y = (buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
140                               ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET);
141         frame->fingers[i].x = (buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
142                               ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET);
143 #endif
144         if (frame->fingers[i].x == 0) {
145             frame->fingers[i].x = X_OFFSET;
146         }
147         frame->fingers[i].valid = true;
148     }
149 }
150 
ChipVersionIsOne(ChipDevice * device,FrameData * frame,const uint8_t * buf,uint8_t pointNum)151 static void ChipVersionIsOne(ChipDevice *device, FrameData *frame, const uint8_t *buf, uint8_t pointNum)
152 {
153     int32_t resX = device->driver->boardCfg->attr.resolutionX;
154     int32_t resY = device->driver->boardCfg->attr.resolutionY;
155     for (uint8_t i = 0; i < pointNum; i++) {
156         frame->fingers[i].x = resX - 1 - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
157                               ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
158         frame->fingers[i].y = resY - 1 - ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
159                               ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
160         frame->fingers[i].valid = true;
161     }
162 }
163 
ChipVersionIsExt(ChipDevice * device,FrameData * frame,const uint8_t * buf,uint8_t pointNum)164 static void ChipVersionIsExt(ChipDevice *device, FrameData *frame, const uint8_t *buf, uint8_t pointNum)
165 {
166     int32_t resX = device->driver->boardCfg->attr.resolutionX;
167     int32_t resY = device->driver->boardCfg->attr.resolutionY;
168     for (uint8_t i = 0; i < pointNum; i++) {
169         frame->fingers[i].x = resX - 1 - ((buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) |
170                               ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
171         frame->fingers[i].y = resY - 1 - ((buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) |
172                               ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET));
173         frame->fingers[i].valid = true;
174     }
175 }
176 
ParsePointData(ChipDevice * device,FrameData * frame,uint8_t * buf,uint8_t pointNum)177 static void ParsePointData(ChipDevice *device, FrameData *frame, uint8_t *buf, uint8_t pointNum)
178 {
179     int32_t chipVer = device->chipCfg->chipVersion;
180     if (chipVer == 0) {         // chipversion  A:gt911_zsj5p5
181         ChipVersionDefault(device, frame, buf, pointNum);
182     } else if (chipVer == 1) {  // chipversion B:gt911_zsj4p0
183         ChipVersionIsOne(device, frame, buf, pointNum);
184     } else {                    // chipversion C:gt911_tg7p0
185         ChipVersionIsExt(device, frame, buf, pointNum);
186     }
187 }
188 
ChipDataHandle(ChipDevice * device)189 static int32_t ChipDataHandle(ChipDevice *device)
190 {
191     int32_t ret;
192     uint8_t touchStatus = 0;
193     uint8_t pointNum;
194     uint8_t buf[GT_POINT_SIZE * MAX_SUPPORT_POINT] = {0};
195     InputI2cClient *i2cClient = &device->driver->i2cClient;
196     uint8_t reg[GT_ADDR_LEN] = {0};
197     FrameData *frame = &device->driver->frameData;
198 
199     reg[0] = (GT_BUF_STATE_ADDR >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
200     reg[1] = GT_BUF_STATE_ADDR & ONE_BYTE_MASK;
201     ret = InputI2cRead(i2cClient, reg, GT_ADDR_LEN, &touchStatus, 1);
202     if (ret < 0 || touchStatus == GT_EVENT_INVALID) {
203         return HDF_FAILURE;
204     }
205 
206     OsalMutexLock(&device->driver->mutex);
207     (void)memset_s(frame, sizeof(FrameData), 0, sizeof(FrameData));
208     if (touchStatus == GT_EVENT_UP) {
209         frame->realPointNum = 0;
210         frame->definedEvent = TOUCH_UP;
211         goto EXIT;
212     }
213 
214     reg[0] = (GT_X_LOW_BYTE_BASE >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
215     reg[1] = GT_X_LOW_BYTE_BASE & ONE_BYTE_MASK;
216     pointNum = touchStatus & GT_FINGER_NUM_MASK;
217     if (pointNum == 0 || pointNum > MAX_SUPPORT_POINT) {
218         HDF_LOGE("%s: pointNum is invalid, %u", __func__, pointNum);
219         (void)ChipCleanBuffer(i2cClient);
220         OsalMutexUnlock(&device->driver->mutex);
221         return HDF_FAILURE;
222     }
223     frame->realPointNum = pointNum;
224     frame->definedEvent = TOUCH_DOWN;
225     (void)InputI2cRead(i2cClient, reg, GT_ADDR_LEN, buf, GT_POINT_SIZE * pointNum);
226     ParsePointData(device, frame, buf, pointNum);
227 
228 EXIT:
229     OsalMutexUnlock(&device->driver->mutex);
230     if (ChipCleanBuffer(i2cClient) != HDF_SUCCESS) {
231         return HDF_FAILURE;
232     }
233     return HDF_SUCCESS;
234 }
235 
UpdateFirmware(ChipDevice * device)236 static int32_t UpdateFirmware(ChipDevice *device)
237 {
238     int32_t ret;
239     InputI2cClient *i2cClient = &device->driver->i2cClient;
240 #if defined(CONFIG_ARCH_ROCKCHIP)
241     uint8_t buf[1] = {0};
242     uint8_t reg[GT_ADDR_LEN] = {0};
243 
244     reg[0] = (GTP_REG_CONFIG_DATA >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK;
245     reg[1] = GTP_REG_CONFIG_DATA & ONE_BYTE_MASK;
246     ret = InputI2cRead(i2cClient, reg, GT_ADDR_LEN, buf, 1);
247     if (ret < 0) {
248         HDF_LOGE("%s: read fw version failed", __func__);
249         return HDF_FAILURE;
250     }
251 
252     HDF_LOGI("%s: buf[0]=0x%x", __func__, buf[0]);
253     if (buf[0] == firmWareParm[FIRMWARE_3RD]) {
254         HDF_LOGI("%s: needn't update fw version", __func__);
255         return HDF_SUCCESS;
256     }
257 #endif
258     ret = InputI2cWrite(i2cClient, firmWareParm, FIRMWARE_LEN);
259     if (ret < 0) {
260         return HDF_FAILURE;
261     }
262     HDF_LOGI("%s: update firmware success\n", __func__);
263     return HDF_SUCCESS;
264 }
265 
SetAbility(ChipDevice * device)266 static void SetAbility(ChipDevice *device)
267 {
268     device->driver->inputDev->abilitySet.devProp[0] = SET_BIT(INPUT_PROP_DIRECT);
269     device->driver->inputDev->abilitySet.eventType[0] = SET_BIT(EV_SYN) |
270         SET_BIT(EV_KEY) | SET_BIT(EV_ABS);
271     if (sizeof(unsigned long) > FOUR_BYTES) {
272         unsigned long highBit = SET_BIT(ABS_MT_POSITION_X) | SET_BIT(ABS_MT_POSITION_Y) |
273             SET_BIT(ABS_MT_TRACKING_ID);
274         if (BITS_PER_LONG == FOUR_BYTES_BIT) {
275             highBit <<= FOUR_BYTES_BIT;
276         }
277         device->driver->inputDev->abilitySet.absCode[0] = SET_BIT(ABS_X) | SET_BIT(ABS_Y) | highBit;
278     } else {
279         device->driver->inputDev->abilitySet.absCode[0] = SET_BIT(ABS_X) | SET_BIT(ABS_Y);
280         device->driver->inputDev->abilitySet.absCode[1] = SET_BIT(ABS_MT_POSITION_X) |
281             SET_BIT(ABS_MT_POSITION_Y) | SET_BIT(ABS_MT_TRACKING_ID);
282     }
283     device->driver->inputDev->abilitySet.keyCode[KEY_CODE_4TH] = SET_BIT(KEY_UP) | SET_BIT(KEY_DOWN);
284     device->driver->inputDev->attrSet.axisInfo[ABS_X].min = 0;
285     device->driver->inputDev->attrSet.axisInfo[ABS_X].max = device->boardCfg->attr.resolutionX - 1;
286     device->driver->inputDev->attrSet.axisInfo[ABS_X].range = 0;
287     device->driver->inputDev->attrSet.axisInfo[ABS_Y].min = 0;
288     device->driver->inputDev->attrSet.axisInfo[ABS_Y].max = device->boardCfg->attr.resolutionY - 1;
289     device->driver->inputDev->attrSet.axisInfo[ABS_Y].range = 0;
290     device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_X].min = 0;
291     device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_X].max = device->boardCfg->attr.resolutionX - 1;
292     device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_X].range = 0;
293     device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_Y].min = 0;
294     device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_Y].max = device->boardCfg->attr.resolutionY - 1;
295     device->driver->inputDev->attrSet.axisInfo[ABS_MT_POSITION_Y].range = 0;
296     device->driver->inputDev->attrSet.axisInfo[ABS_MT_TRACKING_ID].max = MAX_POINT;
297 }
298 
299 static struct TouchChipOps g_gt911ChipOps = {
300     .Init = ChipInit,
301     .Detect = ChipDetect,
302     .Resume = ChipResume,
303     .Suspend = ChipSuspend,
304     .DataHandle = ChipDataHandle,
305     .UpdateFirmware = UpdateFirmware,
306     .SetAbility = SetAbility,
307 };
308 
ChipConfigInstance(struct HdfDeviceObject * device)309 static TouchChipCfg *ChipConfigInstance(struct HdfDeviceObject *device)
310 {
311     TouchChipCfg *chipCfg = (TouchChipCfg *)OsalMemAlloc(sizeof(TouchChipCfg));
312     if (chipCfg == NULL) {
313         HDF_LOGE("%s: instance chip config failed", __func__);
314         return NULL;
315     }
316     (void)memset_s(chipCfg, sizeof(TouchChipCfg), 0, sizeof(TouchChipCfg));
317 
318     if (ParseTouchChipConfig(device->property, chipCfg) != HDF_SUCCESS) {
319         HDF_LOGE("%s: parse chip config failed", __func__);
320         OsalMemFree(chipCfg);
321         chipCfg = NULL;
322     }
323     return chipCfg;
324 }
325 
ChipDeviceInstance(void)326 static ChipDevice *ChipDeviceInstance(void)
327 {
328     ChipDevice *chipDev = (ChipDevice *)OsalMemAlloc(sizeof(ChipDevice));
329     if (chipDev == NULL) {
330         HDF_LOGE("%s: instance chip device failed", __func__);
331         return NULL;
332     }
333     (void)memset_s(chipDev, sizeof(ChipDevice), 0, sizeof(ChipDevice));
334     return chipDev;
335 }
336 
FreeChipConfig(TouchChipCfg * config)337 static void FreeChipConfig(TouchChipCfg *config)
338 {
339     if (config == NULL) {
340         HDF_LOGE("%s: param is null", __func__);
341         return;
342     }
343     if (config->pwrSeq.pwrOn.buf != NULL) {
344         OsalMemFree(config->pwrSeq.pwrOn.buf);
345     }
346 
347     if (config->pwrSeq.pwrOff.buf != NULL) {
348         OsalMemFree(config->pwrSeq.pwrOff.buf);
349     }
350 
351     if (config->pwrSeq.resume.buf != NULL) {
352         OsalMemFree(config->pwrSeq.resume.buf);
353     }
354 
355     if (config->pwrSeq.suspend.buf != NULL) {
356         OsalMemFree(config->pwrSeq.suspend.buf);
357     }
358 
359     OsalMemFree(config);
360 }
361 
HdfGoodixChipInit(struct HdfDeviceObject * device)362 static int32_t HdfGoodixChipInit(struct HdfDeviceObject *device)
363 {
364     TouchChipCfg *chipCfg = NULL;
365     ChipDevice *chipDev = NULL;
366 
367     HDF_LOGI("%s: enter", __func__);
368     if (device == NULL) {
369         return HDF_ERR_INVALID_PARAM;
370     }
371 
372     chipCfg = ChipConfigInstance(device);
373     if (chipCfg == NULL) {
374         return HDF_ERR_MALLOC_FAIL;
375     }
376 
377     chipDev = ChipDeviceInstance();
378     if (chipDev == NULL) {
379         goto EXIT;
380     }
381 
382     chipDev->chipCfg = chipCfg;
383     chipDev->ops = &g_gt911ChipOps;
384     chipDev->chipName = chipCfg->chipName;
385     chipDev->vendorName = chipCfg->vendorName;
386     device->priv = (void *)chipDev;
387 
388     if (RegisterTouchChipDevice(chipDev) != HDF_SUCCESS) {
389         goto EXIT1;
390     }
391     HDF_LOGI("%s: exit succ, chipName = %s", __func__, chipCfg->chipName);
392     return HDF_SUCCESS;
393 
394 EXIT1:
395     OsalMemFree(chipDev);
396 EXIT:
397     FreeChipConfig(chipCfg);
398     return HDF_FAILURE;
399 }
400 
HdfGoodixChipRelease(struct HdfDeviceObject * device)401 static void HdfGoodixChipRelease(struct HdfDeviceObject *device)
402 {
403     if (device == NULL || device->priv == NULL) {
404         HDF_LOGE("%s: param is null", __func__);
405         return;
406     }
407     HDF_LOGI("%s: goodix chip is release", __func__);
408 }
409 
410 struct HdfDriverEntry g_touchGoodixChipEntry = {
411     .moduleVersion = 1,
412     .moduleName = "HDF_TOUCH_GT911",
413     .Init = HdfGoodixChipInit,
414     .Release = HdfGoodixChipRelease,
415 };
416 
417 HDF_INIT(g_touchGoodixChipEntry);
418