gt657x.c 15 KB

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