touchpad.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 = (unsigned int)ioremap(
  309. ranges[r] + i * TP_CTRL_STEP, 0x1000,
  310. IOREMAP_MMIO);
  311. if (!base)
  312. continue;
  313. /* A block left in functional reset keeps its
  314. * register file but never clocks the I2C FSM. */
  315. i2c_dw_reset_release(base);
  316. if (probe_controller(base))
  317. return 1;
  318. }
  319. }
  320. return 0;
  321. }
  322. int touchpad_present(void)
  323. {
  324. return tp_present;
  325. }
  326. unsigned int touchpad_addr(void)
  327. {
  328. return tp_addr;
  329. }
  330. unsigned short touchpad_vendor(void)
  331. {
  332. return tp_vendor;
  333. }
  334. unsigned short touchpad_product(void)
  335. {
  336. return tp_product;
  337. }
  338. int touchpad_poll(int *buttons, int *dx, int *dy, int *scroll)
  339. {
  340. unsigned char buf[TP_MAX_INPUT];
  341. unsigned int now, b;
  342. struct tpg_result res;
  343. *buttons = 0;
  344. *dx = 0;
  345. *dy = 0;
  346. *scroll = 0;
  347. if (!tp_present)
  348. return 0;
  349. now = get_ticks_ms();
  350. /* The synthetic tap press is released by time, whether or not a new
  351. * report arrives (the finger is already up). */
  352. tpg_tick(&tp_gesture, now, &res);
  353. if (res.buttons != (unsigned int)tp_last_buttons) {
  354. tp_last_buttons = (int)res.buttons;
  355. *buttons = res.buttons;
  356. return 1;
  357. }
  358. if ((int)(now - tp_next_poll) < 0)
  359. return 0;
  360. tp_next_poll = now + TP_POLL_MS;
  361. if (hid_read_reg(tp_addr, tp_in_reg, buf, tp_max_in) != 0)
  362. return 0;
  363. /* The addressed read returns two status bytes, the 2-byte length
  364. * and then the report, so locate the 0x5D report ID instead of
  365. * trusting a fixed offset. Layout after it (Linux hid-elan.c):
  366. * touch info (f5..f1 at bits 7..3, clickpad at bit 0), then 5
  367. * bytes per *active* finger (packed): x11:8/y11:9, x7:0, y7:0,
  368. * size, pressure. */
  369. for (b = 0; b < 6 && b + 6 < tp_max_in; b++)
  370. if (buf[b] == TP_REPORT_ID)
  371. break;
  372. if (b >= 6 || b + 6 >= tp_max_in)
  373. return 0;
  374. {
  375. struct tpg_frame f;
  376. unsigned int ti = buf[b + 1];
  377. unsigned int off = b + 2;
  378. unsigned int i, n = 0;
  379. f.count = 0;
  380. f.mask = 0;
  381. f.button = ti & 0x01;
  382. f.now_ms = now;
  383. for (i = 0; i < 5; i++) {
  384. if (!(ti & (0x08u << i)))
  385. continue;
  386. f.count++;
  387. if (n < TPG_CONTACTS && off + 4 < tp_max_in) {
  388. unsigned int xh = buf[off];
  389. f.contact[n].x = ((int)(xh & 0xf0) << 4) |
  390. buf[off + 1];
  391. f.contact[n].y = ((int)(xh & 0x0f) << 8) |
  392. buf[off + 2];
  393. f.mask |= 1u << n;
  394. n++;
  395. }
  396. off += 5;
  397. }
  398. tp_stat_fingers = (int)f.count;
  399. tp_stat_f1 = (f.mask & 1u) != 0;
  400. if (f.mask & 1u) {
  401. tp_stat_x = f.contact[0].x;
  402. tp_stat_y = f.contact[0].y;
  403. }
  404. tpg_frame(&tp_gesture, &f, &res);
  405. }
  406. *buttons = res.buttons;
  407. *dx = res.dx;
  408. *dy = res.dy;
  409. *scroll = res.scroll;
  410. if (res.buttons != (unsigned int)tp_last_buttons) {
  411. tp_last_buttons = (int)res.buttons;
  412. return 1;
  413. }
  414. return res.dx || res.dy || res.scroll;
  415. }
  416. void program_touchpad_main(const char *arg)
  417. {
  418. if (!tp_present) {
  419. io_putstr("touchpad: absent (");
  420. io_putstr(tp_err ? tp_err : "unknown");
  421. if (tp_err_val) {
  422. io_putstr(" ab=");
  423. io_puthex(tp_err_val);
  424. }
  425. io_putstr(")\n");
  426. return;
  427. }
  428. arg = arg ? skip_spaces(arg) : 0;
  429. if (!arg || !*arg) {
  430. print_status();
  431. return;
  432. }
  433. while (*arg) {
  434. unsigned int opts = tpg_options(&tp_gesture);
  435. if (word_is(arg, "rightclick")) {
  436. const char *v = skip_spaces(arg + 10);
  437. if (!*v) {
  438. io_putstr("touchpad: rightclick ");
  439. io_putstr(rc_name());
  440. io_putchar('\n');
  441. return;
  442. }
  443. opts &= ~(TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  444. if (word_is(v, "off")) {
  445. /* keep both bits clear */
  446. } else if (word_is(v, "tap")) {
  447. opts |= TPG_RIGHT_TAP;
  448. } else if (word_is(v, "corner")) {
  449. opts |= TPG_RIGHT_CORNER;
  450. } else if (word_is(v, "both")) {
  451. opts |= TPG_RIGHT_TAP | TPG_RIGHT_CORNER;
  452. } else {
  453. io_putstr("touchpad: rightclick off|tap|corner|both\n");
  454. return;
  455. }
  456. tpg_set_options(&tp_gesture, opts);
  457. io_putstr("touchpad: rightclick ");
  458. io_putstr(rc_name());
  459. io_putchar('\n');
  460. arg = skip_spaces(v + word_len(v));
  461. } else if (word_is(arg, "tapclick")) {
  462. const char *v = skip_spaces(arg + 8);
  463. if (!*v) {
  464. io_putstr("touchpad: tapclick ");
  465. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  466. io_putchar('\n');
  467. return;
  468. }
  469. if (word_is(v, "on"))
  470. opts |= TPG_TAPCLICK;
  471. else if (word_is(v, "off"))
  472. opts &= ~TPG_TAPCLICK;
  473. else {
  474. io_putstr("touchpad: tapclick on|off\n");
  475. return;
  476. }
  477. tpg_set_options(&tp_gesture, opts);
  478. io_putstr("touchpad: tapclick ");
  479. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  480. io_putchar('\n');
  481. arg = skip_spaces(v + word_len(v));
  482. } else if (word_is(arg, "scroll")) {
  483. const char *v = skip_spaces(arg + 6);
  484. if (!*v) {
  485. io_putstr("touchpad: scroll ");
  486. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  487. if (opts & TPG_SCROLL) {
  488. io_putchar(' ');
  489. io_putstr((opts & TPG_SCROLL_INVERT) ?
  490. "traditional" : "natural");
  491. }
  492. io_putchar('\n');
  493. return;
  494. }
  495. if (word_is(v, "on")) {
  496. opts |= TPG_SCROLL;
  497. } else if (word_is(v, "off")) {
  498. opts &= ~TPG_SCROLL;
  499. } else if (word_is(v, "natural")) {
  500. opts |= TPG_SCROLL;
  501. opts &= ~TPG_SCROLL_INVERT;
  502. } else if (word_is(v, "traditional")) {
  503. opts |= TPG_SCROLL;
  504. opts |= TPG_SCROLL_INVERT;
  505. } else {
  506. io_putstr("touchpad: scroll on|off|natural|traditional\n");
  507. return;
  508. }
  509. tpg_set_options(&tp_gesture, opts);
  510. io_putstr("touchpad: scroll ");
  511. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  512. if (opts & TPG_SCROLL) {
  513. io_putchar(' ');
  514. io_putstr((opts & TPG_SCROLL_INVERT) ?
  515. "traditional" : "natural");
  516. }
  517. io_putchar('\n');
  518. arg = skip_spaces(v + word_len(v));
  519. } else {
  520. io_putstr("touchpad: usage: touchpad [rightclick off|tap|corner|both | tapclick on|off | scroll on|off|natural|traditional]\n");
  521. return;
  522. }
  523. }
  524. }