touchpad.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* Elan HID-over-I2C touchpad on the Braswell LPSS I2C bus.
  2. *
  3. * The HP Chromebook 11 G5 EE (setzer) wires its touchpad to LPSS I2C6 at
  4. * 0xD1327000, slave address 0x15. The LPSS I2C functions are hidden from
  5. * PCI in ACPI mode, so the driver scans the two firmware BAR windows for
  6. * DesignWare component types instead of enumerating PCI.
  7. *
  8. * The report line is a GPIO we have no driver for, so the input register is
  9. * polled. A read with no pending report returns a zero length immediately.
  10. * Reading the register back-to-back stalls the device: 25 ms (40 Hz) is the
  11. * rate it keeps up with, and the pointer still moves smoothly.
  12. *
  13. * Init follows Linux's elan_i2c: reset, consume the 2-byte reset
  14. * acknowledgement, read the HID descriptor and the max-X/Y commands, select
  15. * absolute mode (0x0300) and wake the device (0x0800).
  16. *
  17. * Report decoding is Elan-specific; the contact behaviour (pointer deltas,
  18. * taps, the physical-button latch and two-finger scrolling) lives in
  19. * touchpad_gesture.c so another touchpad driver can reuse it.
  20. */
  21. #include "cpu.h"
  22. #include "touchpad.h"
  23. #include "touchpad_gesture.h"
  24. #include "i2c.h"
  25. #include "io.h"
  26. #include "paging.h"
  27. #include "timer.h"
  28. #define TP_ADDR 0x15
  29. #define TP_CTRL_MAX 16
  30. #define TP_CTRL_STEP 0x1000u
  31. #define TP_HID_DESC_REG 0x0001
  32. #define TP_HID_DESC_LEN 30
  33. #define TP_CMD_REG 0x0005
  34. #define TP_CMD_RESET 0x0100
  35. #define TP_CMD_WAKE 0x0800
  36. #define TP_MODE_REG 0x0300
  37. #define TP_MODE_ABS 0x0001
  38. #define TP_DW_COMP_TYPE 0x44570140u
  39. #define TP_REPORT_ID 0x5d
  40. #define TP_MAX_X_REG 0x0106
  41. #define TP_MAX_Y_REG 0x0107
  42. #define TP_ABS_MAX 4095
  43. #define TP_MAX_INPUT 64
  44. #define TP_POLL_MS 25
  45. static unsigned int tp_base;
  46. static unsigned int tp_addr = TP_ADDR;
  47. static int tp_present;
  48. static unsigned int tp_in_reg = 0x0003;
  49. static unsigned int tp_max_in = 34;
  50. static unsigned int tp_cmd_reg = TP_CMD_REG;
  51. static unsigned short tp_vendor;
  52. static unsigned short tp_product;
  53. static int tp_max_x = TP_ABS_MAX;
  54. static int tp_max_y = TP_ABS_MAX;
  55. static const char *tp_err;
  56. static unsigned int tp_err_val;
  57. static struct tpg_state tp_gesture;
  58. static int tp_last_buttons;
  59. static unsigned int tp_next_poll;
  60. /* Last report, for the `touchpad` status command. */
  61. static int tp_stat_fingers, tp_stat_f1, tp_stat_x, tp_stat_y;
  62. static void put_dec(unsigned int v);
  63. static const char *rc_name(void);
  64. static void put_hex16(unsigned int v)
  65. {
  66. static const char hex[] = "0123456789ABCDEF";
  67. io_putchar(hex[(v >> 12) & 0xf]);
  68. io_putchar(hex[(v >> 8) & 0xf]);
  69. io_putchar(hex[(v >> 4) & 0xf]);
  70. io_putchar(hex[v & 0xf]);
  71. }
  72. static void print_summary(void)
  73. {
  74. io_putstr("touchpad: elan ");
  75. put_hex16(tp_vendor);
  76. io_putchar(':');
  77. put_hex16(tp_product);
  78. io_putstr(" i2c 0x");
  79. io_puthex(tp_base);
  80. io_putstr(" addr 0x");
  81. io_puthex(tp_addr);
  82. io_putstr(" max ");
  83. put_dec((unsigned int)tp_max_x);
  84. io_putchar('x');
  85. put_dec((unsigned int)tp_max_y);
  86. io_putchar('\n');
  87. }
  88. static void print_status(void)
  89. {
  90. unsigned int opts = tpg_options(&tp_gesture);
  91. print_summary();
  92. io_putstr("touchpad: rightclick ");
  93. io_putstr(rc_name());
  94. io_putstr(" tapclick ");
  95. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  96. io_putstr(" scroll ");
  97. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  98. if (opts & TPG_SCROLL) {
  99. io_putchar(' ');
  100. io_putstr((opts & TPG_SCROLL_INVERT) ?
  101. "traditional" : "natural");
  102. }
  103. io_putstr(" corner x>=");
  104. put_dec((unsigned int)(tp_max_x * TPG_CORNER_X_PCT / 100));
  105. io_putstr(" y<=");
  106. put_dec((unsigned int)(tp_max_y * TPG_CORNER_Y_PCT / 100));
  107. io_putchar('\n');
  108. io_putstr("touchpad: last f=");
  109. put_dec((unsigned int)tp_stat_fingers);
  110. if (tp_stat_f1) {
  111. io_putstr(" x=");
  112. put_dec((unsigned int)tp_stat_x);
  113. io_putstr(" y=");
  114. put_dec((unsigned int)tp_stat_y);
  115. }
  116. io_putstr(" taps left=");
  117. put_dec(tp_gesture.taps_left);
  118. io_putstr(" right=");
  119. put_dec(tp_gesture.taps_right);
  120. io_putchar('\n');
  121. }
  122. static void msleep(unsigned int ms)
  123. {
  124. unsigned int t0 = get_ticks_ms();
  125. while (get_ticks_ms() - t0 < ms)
  126. ;
  127. }
  128. static unsigned int get16(const unsigned char *p)
  129. {
  130. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  131. }
  132. static void put_dec(unsigned int v)
  133. {
  134. char buf[11];
  135. unsigned int i = 0;
  136. if (!v) {
  137. io_putchar('0');
  138. return;
  139. }
  140. while (v) {
  141. buf[i++] = (char)('0' + v % 10);
  142. v /= 10;
  143. }
  144. while (i)
  145. io_putchar(buf[--i]);
  146. }
  147. /* Match one whitespace-delimited word at s (end or space after it). */
  148. static int word_is(const char *s, const char *word)
  149. {
  150. while (*word) {
  151. if (*s != *word)
  152. return 0;
  153. s++;
  154. word++;
  155. }
  156. return *s == '\0' || *s == ' ';
  157. }
  158. static const char *skip_spaces(const char *s)
  159. {
  160. while (*s == ' ')
  161. s++;
  162. return s;
  163. }
  164. static unsigned int word_len(const char *s)
  165. {
  166. unsigned int n = 0;
  167. while (s[n] && s[n] != ' ')
  168. n++;
  169. return n;
  170. }
  171. static const char *rc_name(void)
  172. {
  173. unsigned int m = tpg_options(&tp_gesture) &
  174. (TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  175. if (m == TPG_RIGHT_TAP)
  176. return "tap";
  177. if (m == TPG_RIGHT_CORNER)
  178. return "corner";
  179. if (m == (TPG_RIGHT_TAP | TPG_RIGHT_CORNER))
  180. return "both";
  181. return "off";
  182. }
  183. static int hid_read_reg(unsigned int addr, unsigned int reg,
  184. unsigned char *buf, unsigned int len)
  185. {
  186. unsigned char r[2];
  187. r[0] = (unsigned char)(reg & 0xff);
  188. r[1] = (unsigned char)(reg >> 8);
  189. if (i2c_dw_transfer(tp_base, addr, r, 2, buf, len) == 0)
  190. return 0;
  191. /* Some devices do not answer the combined write+read (repeated
  192. * start). Retry with a STOP between the register address and the
  193. * data read. */
  194. if (i2c_dw_transfer(tp_base, addr, r, 2, 0, 0) != 0)
  195. return -1;
  196. return i2c_dw_transfer(tp_base, addr, 0, 0, buf, len);
  197. }
  198. static int hid_write_reg(unsigned int addr, unsigned int reg, unsigned int val)
  199. {
  200. unsigned char b[4];
  201. b[0] = (unsigned char)(reg & 0xff);
  202. b[1] = (unsigned char)(reg >> 8);
  203. b[2] = (unsigned char)(val & 0xff);
  204. b[3] = (unsigned char)(val >> 8);
  205. return i2c_dw_transfer(tp_base, addr, b, 4, 0, 0);
  206. }
  207. /* Native Elan max-coordinate commands (the same read-cmd path Linux
  208. * elan_i2c uses). They give the corner zone a physical frame instead of
  209. * assuming the full 12-bit range; on failure keep the 4095 fallback. */
  210. static void query_limits(void)
  211. {
  212. unsigned char v[2];
  213. if (hid_read_reg(tp_addr, TP_MAX_X_REG, v, 2) == 0) {
  214. unsigned int m = get16(v) & 0x0fff;
  215. if (m >= 256)
  216. tp_max_x = (int)m;
  217. }
  218. if (hid_read_reg(tp_addr, TP_MAX_Y_REG, v, 2) == 0) {
  219. unsigned int m = get16(v) & 0x0fff;
  220. if (m >= 256)
  221. tp_max_y = (int)m;
  222. }
  223. }
  224. static int probe_controller(unsigned int base)
  225. {
  226. unsigned char desc[TP_HID_DESC_LEN + 2];
  227. unsigned char ack[2];
  228. unsigned int off, len, ver;
  229. if (!i2c_dw_present(base))
  230. return 0;
  231. if (i2c_dw_init(base) != 0) {
  232. tp_err = "i2c init failed";
  233. return 0;
  234. }
  235. tp_base = base;
  236. /* The Elan address is fixed; a plain read is a harmless ACK check. */
  237. if (i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 1) != 0) {
  238. tp_err = "no device at 0x15";
  239. return 0;
  240. }
  241. /* Reset first and consume the acknowledgement, then read the HID
  242. * descriptor. The descriptor read is prefixed with two status
  243. * bytes and the 2-byte length, so accept either offset. */
  244. hid_write_reg(TP_ADDR, TP_CMD_REG, TP_CMD_RESET);
  245. msleep(150);
  246. (void)i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 2);
  247. if (hid_read_reg(TP_ADDR, TP_HID_DESC_REG, desc, sizeof(desc)) != 0) {
  248. tp_err = "descriptor read failed";
  249. tp_err_val = i2c_last_abort;
  250. return 0;
  251. }
  252. off = 0;
  253. if (get16(desc + 0) != TP_HID_DESC_LEN ||
  254. get16(desc + 2) != 0x0100) {
  255. if (get16(desc + 2) == TP_HID_DESC_LEN &&
  256. get16(desc + 4) == 0x0100)
  257. off = 2;
  258. }
  259. len = get16(desc + off);
  260. ver = get16(desc + off + 2);
  261. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  262. tp_err = "bad hid descriptor";
  263. return 0;
  264. }
  265. tp_in_reg = get16(desc + off + 8);
  266. tp_max_in = get16(desc + off + 10);
  267. tp_cmd_reg = get16(desc + off + 16);
  268. tp_vendor = (unsigned short)get16(desc + off + 20);
  269. tp_product = (unsigned short)get16(desc + off + 22);
  270. if (tp_max_in < 12 || tp_max_in > TP_MAX_INPUT)
  271. tp_max_in = 34;
  272. /* Absolute reporting, then wake: after the reset the Elan stays
  273. * quiet until the host selects a mode. */
  274. hid_write_reg(TP_ADDR, TP_MODE_REG, TP_MODE_ABS);
  275. msleep(40);
  276. hid_write_reg(TP_ADDR, tp_cmd_reg, TP_CMD_WAKE);
  277. msleep(60);
  278. /* Query the limits with the device awake (Linux reads them after
  279. * initialize()/wake and before enabling reports). */
  280. query_limits();
  281. tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
  282. tp_present = 1;
  283. tp_err = 0;
  284. print_summary();
  285. return 1;
  286. }
  287. int touchpad_init(void)
  288. {
  289. /* The ACPI _CRS address for the LPSS I2C blocks is 0x9132xxxx on
  290. * coreboot, but this firmware maps them at 0xD132xxxx. Both
  291. * windows are scanned; page 7 (I2C6) is tried first. */
  292. static const unsigned int ranges[2] = { 0x91320000u, 0xd1320000u };
  293. unsigned int r, k;
  294. if (tp_present)
  295. return 1;
  296. /* The Elan touchpad sits behind the Braswell LPSS I2C blocks (the HP
  297. * Chromebook, Airmont family 6 model 0x4C). Everywhere else the scan
  298. * below would only map and reset-cycle 32 fixed MMIO windows that
  299. * belong to nobody (2 ms each, ~64 ms of boot) or, worse, to some
  300. * other device, so skip it. */
  301. if (cpu_family_model() != 0x64Cu) {
  302. tp_err = "not an Airmont (Braswell) platform";
  303. return 0;
  304. }
  305. for (r = 0; r < 2; r++) {
  306. for (k = 0; k < TP_CTRL_MAX; k++) {
  307. unsigned int i = (k + 7) % TP_CTRL_MAX;
  308. unsigned int base = ranges[r] + i * TP_CTRL_STEP;
  309. paging_map_mmio(base, 0x1000);
  310. /* A block left in functional reset keeps its
  311. * register file but never clocks the I2C FSM. */
  312. i2c_dw_reset_release(base);
  313. if (probe_controller(base))
  314. return 1;
  315. }
  316. }
  317. return 0;
  318. }
  319. int touchpad_present(void)
  320. {
  321. return tp_present;
  322. }
  323. unsigned int touchpad_addr(void)
  324. {
  325. return tp_addr;
  326. }
  327. unsigned short touchpad_vendor(void)
  328. {
  329. return tp_vendor;
  330. }
  331. unsigned short touchpad_product(void)
  332. {
  333. return tp_product;
  334. }
  335. int touchpad_poll(int *buttons, int *dx, int *dy, int *scroll)
  336. {
  337. unsigned char buf[TP_MAX_INPUT];
  338. unsigned int now, b;
  339. struct tpg_result res;
  340. *buttons = 0;
  341. *dx = 0;
  342. *dy = 0;
  343. *scroll = 0;
  344. if (!tp_present)
  345. return 0;
  346. now = get_ticks_ms();
  347. /* The synthetic tap press is released by time, whether or not a new
  348. * report arrives (the finger is already up). */
  349. tpg_tick(&tp_gesture, now, &res);
  350. if (res.buttons != (unsigned int)tp_last_buttons) {
  351. tp_last_buttons = (int)res.buttons;
  352. *buttons = res.buttons;
  353. return 1;
  354. }
  355. if ((int)(now - tp_next_poll) < 0)
  356. return 0;
  357. tp_next_poll = now + TP_POLL_MS;
  358. if (hid_read_reg(tp_addr, tp_in_reg, buf, tp_max_in) != 0)
  359. return 0;
  360. /* The addressed read returns two status bytes, the 2-byte length
  361. * and then the report, so locate the 0x5D report ID instead of
  362. * trusting a fixed offset. Layout after it (Linux hid-elan.c):
  363. * touch info (f5..f1 at bits 7..3, clickpad at bit 0), then 5
  364. * bytes per *active* finger (packed): x11:8/y11:9, x7:0, y7:0,
  365. * size, pressure. */
  366. for (b = 0; b < 6 && b + 6 < tp_max_in; b++)
  367. if (buf[b] == TP_REPORT_ID)
  368. break;
  369. if (b >= 6 || b + 6 >= tp_max_in)
  370. return 0;
  371. {
  372. struct tpg_frame f;
  373. unsigned int ti = buf[b + 1];
  374. unsigned int off = b + 2;
  375. unsigned int i, n = 0;
  376. f.count = 0;
  377. f.mask = 0;
  378. f.button = ti & 0x01;
  379. f.now_ms = now;
  380. for (i = 0; i < 5; i++) {
  381. if (!(ti & (0x08u << i)))
  382. continue;
  383. f.count++;
  384. if (n < TPG_CONTACTS && off + 4 < tp_max_in) {
  385. unsigned int xh = buf[off];
  386. f.contact[n].x = ((int)(xh & 0xf0) << 4) |
  387. buf[off + 1];
  388. f.contact[n].y = ((int)(xh & 0x0f) << 8) |
  389. buf[off + 2];
  390. f.mask |= 1u << n;
  391. n++;
  392. }
  393. off += 5;
  394. }
  395. tp_stat_fingers = (int)f.count;
  396. tp_stat_f1 = (f.mask & 1u) != 0;
  397. if (f.mask & 1u) {
  398. tp_stat_x = f.contact[0].x;
  399. tp_stat_y = f.contact[0].y;
  400. }
  401. tpg_frame(&tp_gesture, &f, &res);
  402. }
  403. *buttons = res.buttons;
  404. *dx = res.dx;
  405. *dy = res.dy;
  406. *scroll = res.scroll;
  407. if (res.buttons != (unsigned int)tp_last_buttons) {
  408. tp_last_buttons = (int)res.buttons;
  409. return 1;
  410. }
  411. return res.dx || res.dy || res.scroll;
  412. }
  413. void program_touchpad_main(const char *arg)
  414. {
  415. if (!tp_present) {
  416. io_putstr("touchpad: absent (");
  417. io_putstr(tp_err ? tp_err : "unknown");
  418. if (tp_err_val) {
  419. io_putstr(" ab=");
  420. io_puthex(tp_err_val);
  421. }
  422. io_putstr(")\n");
  423. return;
  424. }
  425. arg = arg ? skip_spaces(arg) : 0;
  426. if (!arg || !*arg) {
  427. print_status();
  428. return;
  429. }
  430. while (*arg) {
  431. unsigned int opts = tpg_options(&tp_gesture);
  432. if (word_is(arg, "rightclick")) {
  433. const char *v = skip_spaces(arg + 10);
  434. if (!*v) {
  435. io_putstr("touchpad: rightclick ");
  436. io_putstr(rc_name());
  437. io_putchar('\n');
  438. return;
  439. }
  440. opts &= ~(TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  441. if (word_is(v, "off")) {
  442. /* keep both bits clear */
  443. } else if (word_is(v, "tap")) {
  444. opts |= TPG_RIGHT_TAP;
  445. } else if (word_is(v, "corner")) {
  446. opts |= TPG_RIGHT_CORNER;
  447. } else if (word_is(v, "both")) {
  448. opts |= TPG_RIGHT_TAP | TPG_RIGHT_CORNER;
  449. } else {
  450. io_putstr("touchpad: rightclick off|tap|corner|both\n");
  451. return;
  452. }
  453. tpg_set_options(&tp_gesture, opts);
  454. io_putstr("touchpad: rightclick ");
  455. io_putstr(rc_name());
  456. io_putchar('\n');
  457. arg = skip_spaces(v + word_len(v));
  458. } else if (word_is(arg, "tapclick")) {
  459. const char *v = skip_spaces(arg + 8);
  460. if (!*v) {
  461. io_putstr("touchpad: tapclick ");
  462. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  463. io_putchar('\n');
  464. return;
  465. }
  466. if (word_is(v, "on"))
  467. opts |= TPG_TAPCLICK;
  468. else if (word_is(v, "off"))
  469. opts &= ~TPG_TAPCLICK;
  470. else {
  471. io_putstr("touchpad: tapclick on|off\n");
  472. return;
  473. }
  474. tpg_set_options(&tp_gesture, opts);
  475. io_putstr("touchpad: tapclick ");
  476. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  477. io_putchar('\n');
  478. arg = skip_spaces(v + word_len(v));
  479. } else if (word_is(arg, "scroll")) {
  480. const char *v = skip_spaces(arg + 6);
  481. if (!*v) {
  482. io_putstr("touchpad: scroll ");
  483. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  484. if (opts & TPG_SCROLL) {
  485. io_putchar(' ');
  486. io_putstr((opts & TPG_SCROLL_INVERT) ?
  487. "traditional" : "natural");
  488. }
  489. io_putchar('\n');
  490. return;
  491. }
  492. if (word_is(v, "on")) {
  493. opts |= TPG_SCROLL;
  494. } else if (word_is(v, "off")) {
  495. opts &= ~TPG_SCROLL;
  496. } else if (word_is(v, "natural")) {
  497. opts |= TPG_SCROLL;
  498. opts &= ~TPG_SCROLL_INVERT;
  499. } else if (word_is(v, "traditional")) {
  500. opts |= TPG_SCROLL;
  501. opts |= TPG_SCROLL_INVERT;
  502. } else {
  503. io_putstr("touchpad: scroll on|off|natural|traditional\n");
  504. return;
  505. }
  506. tpg_set_options(&tp_gesture, opts);
  507. io_putstr("touchpad: scroll ");
  508. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  509. if (opts & TPG_SCROLL) {
  510. io_putchar(' ');
  511. io_putstr((opts & TPG_SCROLL_INVERT) ?
  512. "traditional" : "natural");
  513. }
  514. io_putchar('\n');
  515. arg = skip_spaces(v + word_len(v));
  516. } else {
  517. io_putstr("touchpad: usage: touchpad [rightclick off|tap|corner|both | tapclick on|off | scroll on|off|natural|traditional]\n");
  518. return;
  519. }
  520. }
  521. }