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