gt9xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include "FreeRTOS.h"
  5. #include "chip.h"
  6. #include "board.h"
  7. #ifdef TP_USE_GT9XX
  8. #include <xm_base.h>
  9. #include <xm_event.h>
  10. #define GOODIX_GPIO_INT_NAME "irq"
  11. #define GOODIX_GPIO_RST_NAME "reset"
  12. #define GOODIX_MAX_HEIGHT 4096
  13. #define GOODIX_MAX_WIDTH 4096
  14. #define GOODIX_INT_TRIGGER 1
  15. #define GOODIX_CONTACT_SIZE 8
  16. #define GOODIX_MAX_CONTACTS 10
  17. #define GOODIX_CONFIG_MAX_LENGTH 240
  18. #define GOODIX_CONFIG_911_LENGTH 186
  19. #define GOODIX_CONFIG_967_LENGTH 228
  20. /* Register defines */
  21. #define GOODIX_REG_COMMAND 0x8040
  22. #define GOODIX_CMD_SCREEN_OFF 0x05
  23. #define GOODIX_READ_COOR_ADDR 0x814E
  24. #define GOODIX_GT1X_REG_CONFIG_DATA 0x8050
  25. #define GOODIX_GT9X_REG_CONFIG_DATA 0x8047
  26. #define GOODIX_REG_ID 0x8140
  27. #define GOODIX_BUFFER_STATUS_READY BIT(7)
  28. #define GOODIX_BUFFER_STATUS_TIMEOUT 20
  29. #define RESOLUTION_LOC 1
  30. #define MAX_CONTACTS_LOC 5
  31. #define TRIGGER_LOC 6
  32. #define GT9XX_SLAVE_ADDR 0x5d
  33. #define GT9XX_GPIO_INT TP_GPIO_INT
  34. #define GT9XX_GPIO_RST TP_GPIO_RST
  35. #define GT9XX_INVERTED_X TP_INV_X
  36. #define GT9XX_INVERTED_Y TP_INV_Y
  37. #define GT9XX_MT_TOUCH TP_MT_TOUCH//支持多点触摸
  38. struct goodix_ts_data;
  39. struct goodix_chip_data {
  40. u16 config_addr;
  41. int config_len;
  42. };
  43. struct goodix_ts_data {
  44. struct i2c_adapter *adap;
  45. const struct goodix_chip_data *chip;
  46. unsigned int max_touch_num;
  47. unsigned int x_max;
  48. unsigned int y_max;
  49. unsigned int int_trigger_type;
  50. int gpio_int;
  51. int gpio_rst;
  52. u16 id;
  53. u16 version;
  54. const char cfg_name[32];
  55. unsigned long irq_flags;
  56. TaskHandle_t irq_task;
  57. };
  58. static const struct goodix_chip_data gt1x_chip_data = {
  59. .config_addr = GOODIX_GT1X_REG_CONFIG_DATA,
  60. .config_len = GOODIX_CONFIG_MAX_LENGTH,
  61. };
  62. static const struct goodix_chip_data gt911_chip_data = {
  63. .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
  64. .config_len = GOODIX_CONFIG_911_LENGTH,
  65. };
  66. static const struct goodix_chip_data gt967_chip_data = {
  67. .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
  68. .config_len = GOODIX_CONFIG_967_LENGTH,
  69. };
  70. static const struct goodix_chip_data gt9x_chip_data = {
  71. .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
  72. .config_len = GOODIX_CONFIG_MAX_LENGTH,
  73. };
  74. static const unsigned long goodix_irq_flags[] = {
  75. IRQ_TYPE_EDGE_RISING,
  76. IRQ_TYPE_EDGE_FALLING,
  77. IRQ_TYPE_LEVEL_LOW,
  78. IRQ_TYPE_LEVEL_HIGH,
  79. };
  80. static int last_input_x, last_input_y;
  81. static inline int get_unaligned_le16(void *p)
  82. {
  83. u8 *tmp = (u8*)p;
  84. return (tmp[1] << 8) | tmp[0];
  85. }
  86. /**
  87. * goodix_i2c_read - read data from a register of the i2c slave device.
  88. *
  89. * @adap: i2c device.
  90. * @reg: the register to read from.
  91. * @buf: raw write data buffer.
  92. * @len: length of the buffer to write
  93. */
  94. static int goodix_i2c_read(struct i2c_adapter *adap,
  95. u16 reg, u8 *buf, int len)
  96. {
  97. struct i2c_msg msgs[2];
  98. u8 wbuf[2];
  99. int ret;
  100. wbuf[0] = reg >> 8;
  101. wbuf[1] = reg & 0xFF;
  102. msgs[0].flags = 0;
  103. msgs[0].addr = GT9XX_SLAVE_ADDR;
  104. msgs[0].len = 2;
  105. msgs[0].buf = (u8 *)&wbuf;
  106. msgs[1].flags = I2C_M_RD;
  107. msgs[1].addr = GT9XX_SLAVE_ADDR;
  108. msgs[1].len = len;
  109. msgs[1].buf = buf;
  110. ret = i2c_transfer(adap, msgs, 2);
  111. return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
  112. }
  113. /**
  114. * goodix_i2c_write - write data to a register of the i2c slave device.
  115. *
  116. * @adap: i2c device.
  117. * @reg: the register to write to.
  118. * @buf: raw data buffer to write.
  119. * @len: length of the buffer to write
  120. */
  121. static int goodix_i2c_write(struct i2c_adapter *adap, u16 reg, const u8 *buf,
  122. unsigned len)
  123. {
  124. u8 *addr_buf;
  125. struct i2c_msg msg;
  126. int ret;
  127. addr_buf = pvPortMalloc(len + 2);
  128. if (!addr_buf)
  129. return -ENOMEM;
  130. addr_buf[0] = reg >> 8;
  131. addr_buf[1] = reg & 0xFF;
  132. memcpy(&addr_buf[2], buf, len);
  133. msg.flags = 0;
  134. msg.addr = GT9XX_SLAVE_ADDR;
  135. msg.buf = addr_buf;
  136. msg.len = len + 2;
  137. ret = i2c_transfer(adap, &msg, 1);
  138. vPortFree(addr_buf);
  139. return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
  140. }
  141. static int goodix_i2c_write_u8(struct i2c_adapter *adap, u16 reg, u8 value)
  142. {
  143. return goodix_i2c_write(adap, reg, &value, sizeof(value));
  144. }
  145. static const struct goodix_chip_data *goodix_get_chip_data(u16 id)
  146. {
  147. switch (id) {
  148. case 1151:
  149. return &gt1x_chip_data;
  150. case 911:
  151. case 9271:
  152. case 9110:
  153. case 927:
  154. case 928:
  155. return &gt911_chip_data;
  156. case 912:
  157. case 967:
  158. return &gt967_chip_data;
  159. default:
  160. return &gt9x_chip_data;
  161. }
  162. }
  163. static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
  164. {
  165. unsigned long max_timeout;
  166. int touch_num;
  167. int error;
  168. /*
  169. * The 'buffer status' bit, which indicates that the data is valid, is
  170. * not set as soon as the interrupt is raised, but slightly after.
  171. * This takes around 10 ms to happen, so we poll for 20 ms.
  172. */
  173. max_timeout = xTaskGetTickCount() + pdMS_TO_TICKS(GOODIX_BUFFER_STATUS_TIMEOUT);
  174. do {
  175. error = goodix_i2c_read(ts->adap, GOODIX_READ_COOR_ADDR,
  176. data, GOODIX_CONTACT_SIZE + 1);
  177. if (error) {
  178. TRACE_ERROR("I2C transfer error: %d\n", error);
  179. return error;
  180. }
  181. if (data[0] & GOODIX_BUFFER_STATUS_READY) {
  182. touch_num = data[0] & 0x0f;
  183. if (touch_num > ts->max_touch_num)
  184. return -EPROTO;
  185. #if GT9XX_MT_TOUCH
  186. if (touch_num > 1) {
  187. data += 1 + GOODIX_CONTACT_SIZE;
  188. error = goodix_i2c_read(ts->adap,
  189. GOODIX_READ_COOR_ADDR +
  190. 1 + GOODIX_CONTACT_SIZE,
  191. data,
  192. GOODIX_CONTACT_SIZE *
  193. (touch_num - 1));
  194. if (error)
  195. return error;
  196. }
  197. return touch_num;
  198. #else
  199. return touch_num > 0 ? 1 : 0;
  200. #endif
  201. }
  202. vTaskDelay(pdMS_TO_TICKS(1)); /* Poll every 1 - 2 ms */
  203. } while (xTaskGetTickCount() < max_timeout);
  204. /*
  205. * The Goodix panel will send spurious interrupts after a
  206. * 'finger up' event, which will always cause a timeout.
  207. */
  208. return 0;
  209. }
  210. static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
  211. {
  212. //int id = coor_data[0] & 0x0F;
  213. int input_x = get_unaligned_le16(&coor_data[1]);
  214. int input_y = get_unaligned_le16(&coor_data[3]);
  215. int input_w = get_unaligned_le16(&coor_data[5]);
  216. /* printf("id=0x%x, input_x=%d, input_y=%d, input_w=%d.\n", id, input_x,
  217. input_y, input_w); */
  218. #if GT9XX_INVERTED_X
  219. input_x = ts->x_max - input_x;
  220. #endif
  221. #if GT9XX_INVERTED_Y
  222. input_y = ts->y_max - input_y;
  223. #endif
  224. //send press event
  225. XM_TpEventProc (XM_EVENT_TOUCHDOWN, input_x, input_y, 0);
  226. last_input_x = input_x;
  227. last_input_y = input_y;
  228. }
  229. /**
  230. * goodix_process_events - Process incoming events
  231. *
  232. * @ts: our goodix_ts_data pointer
  233. *
  234. * Called when the IRQ is triggered. Read the current device state, and push
  235. * the input events to the user space.
  236. */
  237. static void goodix_process_events(struct goodix_ts_data *ts)
  238. {
  239. u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
  240. int touch_num;
  241. int i;
  242. touch_num = goodix_ts_read_input_report(ts, point_data);
  243. if (touch_num < 0)
  244. return;
  245. /*
  246. * Bit 4 of the first byte reports the status of the capacitive
  247. * Windows/Home button.
  248. */
  249. //input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
  250. for (i = 0; i < touch_num; i++)
  251. goodix_ts_report_touch(ts,
  252. &point_data[1 + GOODIX_CONTACT_SIZE * i]);
  253. if (touch_num == 0) {
  254. //send release event
  255. XM_TpEventProc (XM_EVENT_TOUCHUP, last_input_x, last_input_y, 0);
  256. }
  257. /* input_mt_sync_frame(ts->input_dev);
  258. input_sync(ts->input_dev); */
  259. }
  260. static void goodix_ts_irq_task(void *param)
  261. {
  262. uint32_t ulNotifiedValue;
  263. struct goodix_ts_data *ts;
  264. for (;;) {
  265. xTaskNotifyWait( 0x00, /* Don't clear any notification bits on entry. */
  266. 0xffffffff, /* Reset the notification value to 0 on exit. */
  267. &ulNotifiedValue, /* Notified value pass out in ulNotifiedValue. */
  268. portMAX_DELAY);
  269. ts = (struct goodix_ts_data *)ulNotifiedValue;
  270. goodix_process_events(ts);
  271. if (goodix_i2c_write_u8(ts->adap, GOODIX_READ_COOR_ADDR, 0) < 0)
  272. TRACE_ERROR("I2C write end_cmd error\n");
  273. }
  274. }
  275. /**
  276. * goodix_ts_irq_handler - The IRQ handler
  277. *
  278. * @param: private data pointer.
  279. */
  280. static void goodix_ts_irq_handler(void *param)
  281. {
  282. struct goodix_ts_data *ts = param;
  283. xTaskNotifyFromISR(ts->irq_task, (uint32_t)ts, eSetValueWithOverwrite, 0);
  284. return;
  285. }
  286. static int goodix_request_irq(struct goodix_ts_data *ts)
  287. {
  288. if (xTaskCreate(goodix_ts_irq_task, "gt9xx", 1024, ts,
  289. 10, &ts->irq_task) != pdPASS) {
  290. printf("create gt9xx irq task fail.\n");
  291. }
  292. return gpio_irq_request(ts->gpio_int, ts->irq_flags, goodix_ts_irq_handler, ts);
  293. }
  294. static int goodix_int_sync(struct goodix_ts_data *ts)
  295. {
  296. gpio_direction_output(ts->gpio_int, 0);
  297. vTaskDelay(pdMS_TO_TICKS(50)); /* T5: 50ms */
  298. gpio_direction_input(ts->gpio_int);
  299. return 0;
  300. }
  301. /**
  302. * goodix_reset - Reset device during power on
  303. *
  304. * @ts: goodix_ts_data pointer
  305. */
  306. static int goodix_reset(struct goodix_ts_data *ts)
  307. {
  308. int error;
  309. /* begin select I2C slave addr */
  310. gpio_direction_output(ts->gpio_rst, 0);
  311. vTaskDelay(pdMS_TO_TICKS(20)); /* T2: > 10ms */
  312. /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
  313. gpio_direction_output(ts->gpio_int, GT9XX_SLAVE_ADDR == 0x14);
  314. vTaskDelay(pdMS_TO_TICKS(1)); /* T3: > 100us */
  315. gpio_direction_output(ts->gpio_rst, 1);
  316. vTaskDelay(pdMS_TO_TICKS(6)); /* T4: > 5ms */
  317. /* end select I2C slave addr */
  318. gpio_direction_input(ts->gpio_rst);
  319. error = goodix_int_sync(ts);
  320. if (error)
  321. return error;
  322. return 0;
  323. }
  324. /**
  325. * goodix_read_config - Read the embedded configuration of the panel
  326. *
  327. * @ts: our goodix_ts_data pointer
  328. *
  329. * Must be called during probe
  330. */
  331. static void goodix_read_config(struct goodix_ts_data *ts)
  332. {
  333. u8 config[GOODIX_CONFIG_MAX_LENGTH];
  334. int error;
  335. error = goodix_i2c_read(ts->adap, ts->chip->config_addr,
  336. config, ts->chip->config_len);
  337. if (error) {
  338. TRACE_WARNING("Error reading config: %d\n", error);
  339. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  340. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  341. return;
  342. }
  343. ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
  344. ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
  345. ts->x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
  346. ts->y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
  347. }
  348. /**
  349. * goodix_read_version - Read goodix touchscreen version
  350. *
  351. * @ts: our goodix_ts_data pointer
  352. */
  353. static int goodix_read_version(struct goodix_ts_data *ts)
  354. {
  355. int error;
  356. u8 buf[6];
  357. char id_str[5];
  358. error = goodix_i2c_read(ts->adap, GOODIX_REG_ID, buf, sizeof(buf));
  359. if (error) {
  360. TRACE_ERROR("read version failed: %d\n", error);
  361. return error;
  362. }
  363. memcpy(id_str, buf, 4);
  364. id_str[4] = 0;
  365. char *ptr;
  366. ts->id = strtoul(id_str, &ptr, 10);
  367. if (ts->id == 0xffff)
  368. ts->id = 0x1001;
  369. ts->version = get_unaligned_le16(&buf[4]);
  370. TRACE_INFO("ID %d, version: %04x\n", ts->id, ts->version);
  371. return 0;
  372. }
  373. /**
  374. * goodix_i2c_test - I2C test function to check if the device answers.
  375. *
  376. * @adap: the i2c adapter
  377. */
  378. static int goodix_i2c_test(struct i2c_adapter *adap)
  379. {
  380. int retry = 0;
  381. int error;
  382. u8 test;
  383. while (retry++ < 2) {
  384. error = goodix_i2c_read(adap, GOODIX_REG_ID,
  385. &test, 1);
  386. if (!error)
  387. return 0;
  388. TRACE_ERROR("i2c test failed attempt %d: %d\n",
  389. retry, error);
  390. vTaskDelay(pdMS_TO_TICKS(20));
  391. }
  392. return error;
  393. }
  394. /**
  395. * goodix_configure_dev - Finish device initialization
  396. *
  397. * @ts: our goodix_ts_data pointer
  398. *
  399. * Must be called from probe to finish initialization of the device.
  400. * Contains the common initialization code for both devices that
  401. * declare gpio pins and devices that do not. It is either called
  402. * directly from probe or from request_firmware_wait callback.
  403. */
  404. static int goodix_configure_dev(struct goodix_ts_data *ts)
  405. {
  406. int error;
  407. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  408. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  409. /* Read configuration and apply touchscreen parameters */
  410. goodix_read_config(ts);
  411. ts->irq_flags = goodix_irq_flags[ts->int_trigger_type];
  412. error = goodix_request_irq(ts);
  413. if (error) {
  414. TRACE_ERROR("request IRQ failed: %d\n", error);
  415. return error;
  416. }
  417. return 0;
  418. }
  419. int goodix_ts_probe(struct i2c_adapter *adap)
  420. {
  421. struct goodix_ts_data *ts;
  422. int error;
  423. TRACE_DEBUG("I2C Address: 0x%02x\n", GT9XX_SLAVE_ADDR);
  424. ts = pvPortMalloc(sizeof(*ts));
  425. if (!ts)
  426. return -ENOMEM;
  427. memset(ts, 0, sizeof(*ts));
  428. ts->adap = adap;
  429. ts->gpio_int = GT9XX_GPIO_INT;
  430. ts->gpio_rst = GT9XX_GPIO_RST;
  431. if (ts->gpio_int && ts->gpio_rst) {
  432. /* reset the controller */
  433. error = goodix_reset(ts);
  434. if (error) {
  435. TRACE_ERROR("Controller reset failed.\n");
  436. return error;
  437. }
  438. }
  439. error = goodix_i2c_test(adap);
  440. if (error) {
  441. TRACE_ERROR("I2C communication failure: %d\n", error);
  442. return error;
  443. }
  444. error = goodix_read_version(ts);
  445. if (error) {
  446. TRACE_ERROR("Read version failed.\n");
  447. return error;
  448. }
  449. ts->chip = goodix_get_chip_data(ts->id);
  450. error = goodix_configure_dev(ts);
  451. if (error)
  452. return error;
  453. return 0;
  454. }
  455. int goodix_init(void)
  456. {
  457. struct i2c_adapter *adap = NULL;
  458. if (!(adap = i2c_open("i2c0"))) {
  459. printf("open i2c0 fail.\n");
  460. return -1;
  461. }
  462. goodix_ts_probe(adap);
  463. return 0;
  464. }
  465. #endif