touchpad.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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, select absolute mode (0x0300)
  15. * and wake the device (0x0800). Resetting last leaves it silent until the
  16. * next power cycle.
  17. */
  18. #include "touchpad.h"
  19. #include "i2c.h"
  20. #include "io.h"
  21. #include "paging.h"
  22. #include "timer.h"
  23. #define TP_ADDR 0x15
  24. #define TP_CTRL_MAX 16
  25. #define TP_CTRL_STEP 0x1000u
  26. #define TP_HID_DESC_REG 0x0001
  27. #define TP_HID_DESC_LEN 30
  28. #define TP_CMD_REG 0x0005
  29. #define TP_CMD_RESET 0x0100
  30. #define TP_CMD_WAKE 0x0800
  31. #define TP_MODE_REG 0x0300
  32. #define TP_MODE_ABS 0x0001
  33. #define TP_DW_COMP_TYPE 0x44570140u
  34. #define TP_REPORT_ID 0x5d
  35. #define TP_MAX_INPUT 64
  36. #define TP_POLL_MS 25
  37. static unsigned int tp_base;
  38. static unsigned int tp_addr = TP_ADDR;
  39. static int tp_present;
  40. static unsigned int tp_in_reg = 0x0003;
  41. static unsigned int tp_max_in = 34;
  42. static unsigned int tp_cmd_reg = TP_CMD_REG;
  43. static unsigned short tp_vendor;
  44. static unsigned short tp_product;
  45. static const char *tp_err;
  46. static unsigned int tp_err_val;
  47. static int tp_last_x, tp_last_y, tp_acc_x, tp_acc_y;
  48. static int tp_down, tp_last_buttons;
  49. static unsigned int tp_next_poll;
  50. static void put_hex16(unsigned int v)
  51. {
  52. static const char hex[] = "0123456789ABCDEF";
  53. io_putchar(hex[(v >> 12) & 0xf]);
  54. io_putchar(hex[(v >> 8) & 0xf]);
  55. io_putchar(hex[(v >> 4) & 0xf]);
  56. io_putchar(hex[v & 0xf]);
  57. }
  58. static void print_summary(void)
  59. {
  60. io_putstr("touchpad: elan ");
  61. put_hex16(tp_vendor);
  62. io_putchar(':');
  63. put_hex16(tp_product);
  64. io_putstr(" i2c 0x");
  65. io_puthex(tp_base);
  66. io_putstr(" addr 0x");
  67. io_puthex(tp_addr);
  68. io_putchar('\n');
  69. }
  70. static void msleep(unsigned int ms)
  71. {
  72. unsigned int t0 = get_ticks_ms();
  73. while (get_ticks_ms() - t0 < ms)
  74. ;
  75. }
  76. static unsigned int get16(const unsigned char *p)
  77. {
  78. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  79. }
  80. static int hid_read_reg(unsigned int addr, unsigned int reg,
  81. unsigned char *buf, unsigned int len)
  82. {
  83. unsigned char r[2];
  84. r[0] = (unsigned char)(reg & 0xff);
  85. r[1] = (unsigned char)(reg >> 8);
  86. if (i2c_dw_transfer(tp_base, addr, r, 2, buf, len) == 0)
  87. return 0;
  88. /* Some devices do not answer the combined write+read (repeated
  89. * start). Retry with a STOP between the register address and the
  90. * data read. */
  91. if (i2c_dw_transfer(tp_base, addr, r, 2, 0, 0) != 0)
  92. return -1;
  93. return i2c_dw_transfer(tp_base, addr, 0, 0, buf, len);
  94. }
  95. static int hid_write_reg(unsigned int addr, unsigned int reg, unsigned int val)
  96. {
  97. unsigned char b[4];
  98. b[0] = (unsigned char)(reg & 0xff);
  99. b[1] = (unsigned char)(reg >> 8);
  100. b[2] = (unsigned char)(val & 0xff);
  101. b[3] = (unsigned char)(val >> 8);
  102. return i2c_dw_transfer(tp_base, addr, b, 4, 0, 0);
  103. }
  104. static int probe_controller(unsigned int base)
  105. {
  106. unsigned char desc[TP_HID_DESC_LEN + 2];
  107. unsigned char ack[2];
  108. unsigned int off, len, ver;
  109. if (!i2c_dw_present(base))
  110. return 0;
  111. if (i2c_dw_init(base) != 0) {
  112. tp_err = "i2c init failed";
  113. return 0;
  114. }
  115. tp_base = base;
  116. /* The Elan address is fixed; a plain read is a harmless ACK check. */
  117. if (i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 1) != 0) {
  118. tp_err = "no device at 0x15";
  119. return 0;
  120. }
  121. /* Reset first and consume the acknowledgement, then read the HID
  122. * descriptor. The descriptor read is prefixed with two status
  123. * bytes and the 2-byte length, so accept either offset. */
  124. hid_write_reg(TP_ADDR, TP_CMD_REG, TP_CMD_RESET);
  125. msleep(150);
  126. (void)i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 2);
  127. if (hid_read_reg(TP_ADDR, TP_HID_DESC_REG, desc, sizeof(desc)) != 0) {
  128. tp_err = "descriptor read failed";
  129. tp_err_val = i2c_last_abort;
  130. return 0;
  131. }
  132. off = 0;
  133. if (get16(desc + 0) != TP_HID_DESC_LEN ||
  134. get16(desc + 2) != 0x0100) {
  135. if (get16(desc + 2) == TP_HID_DESC_LEN &&
  136. get16(desc + 4) == 0x0100)
  137. off = 2;
  138. }
  139. len = get16(desc + off);
  140. ver = get16(desc + off + 2);
  141. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  142. tp_err = "bad hid descriptor";
  143. return 0;
  144. }
  145. tp_in_reg = get16(desc + off + 8);
  146. tp_max_in = get16(desc + off + 10);
  147. tp_cmd_reg = get16(desc + off + 16);
  148. tp_vendor = (unsigned short)get16(desc + off + 20);
  149. tp_product = (unsigned short)get16(desc + off + 22);
  150. if (tp_max_in < 12 || tp_max_in > TP_MAX_INPUT)
  151. tp_max_in = 34;
  152. /* Absolute reporting, then wake: after the reset the Elan stays
  153. * quiet until the host selects a mode. */
  154. hid_write_reg(TP_ADDR, TP_MODE_REG, TP_MODE_ABS);
  155. msleep(40);
  156. hid_write_reg(TP_ADDR, tp_cmd_reg, TP_CMD_WAKE);
  157. msleep(60);
  158. tp_present = 1;
  159. tp_err = 0;
  160. print_summary();
  161. return 1;
  162. }
  163. int touchpad_init(void)
  164. {
  165. /* The ACPI _CRS address for the LPSS I2C blocks is 0x9132xxxx on
  166. * coreboot, but this firmware maps them at 0xD132xxxx. Both
  167. * windows are scanned; page 7 (I2C6) is tried first. */
  168. static const unsigned int ranges[2] = { 0x91320000u, 0xd1320000u };
  169. unsigned int r, k;
  170. if (tp_present)
  171. return 1;
  172. for (r = 0; r < 2; r++) {
  173. for (k = 0; k < TP_CTRL_MAX; k++) {
  174. unsigned int i = (k + 7) % TP_CTRL_MAX;
  175. unsigned int base = ranges[r] + i * TP_CTRL_STEP;
  176. paging_map_mmio(base, 0x1000);
  177. /* A block left in functional reset keeps its
  178. * register file but never clocks the I2C FSM. */
  179. i2c_dw_reset_release(base);
  180. if (probe_controller(base))
  181. return 1;
  182. }
  183. }
  184. return 0;
  185. }
  186. int touchpad_present(void)
  187. {
  188. return tp_present;
  189. }
  190. unsigned int touchpad_addr(void)
  191. {
  192. return tp_addr;
  193. }
  194. unsigned short touchpad_vendor(void)
  195. {
  196. return tp_vendor;
  197. }
  198. unsigned short touchpad_product(void)
  199. {
  200. return tp_product;
  201. }
  202. int touchpad_poll(int *buttons, int *dx, int *dy)
  203. {
  204. unsigned char buf[TP_MAX_INPUT];
  205. unsigned int now, b;
  206. if (!tp_present)
  207. return 0;
  208. now = get_ticks_ms();
  209. if ((int)(now - tp_next_poll) < 0)
  210. return 0;
  211. tp_next_poll = now + TP_POLL_MS;
  212. if (hid_read_reg(tp_addr, tp_in_reg, buf, tp_max_in) != 0)
  213. return 0;
  214. /* The addressed read returns two status bytes, the 2-byte length
  215. * and then the report, so locate the 0x5D report ID instead of
  216. * trusting a fixed offset. Layout after it (Linux hid-elan.c):
  217. * touch info (f5..f1 at bits 7..3, clickpad at bit 0), then 5
  218. * bytes per finger: x11:8/y11:9, x7:0, y7:0, size, pressure. */
  219. for (b = 0; b < 6 && b + 4 < tp_max_in; b++)
  220. if (buf[b] == TP_REPORT_ID)
  221. break;
  222. if (b >= 6 || b + 4 >= tp_max_in)
  223. return 0;
  224. {
  225. unsigned int ti = buf[b + 1];
  226. unsigned int xh = buf[b + 2];
  227. int x = ((int)(xh & 0xf0) << 4) | buf[b + 3];
  228. int y = ((int)(xh & 0x0f) << 8) | buf[b + 4];
  229. *buttons = ti & 0x01;
  230. if (!(ti & 0x08)) { /* finger 1 lifted */
  231. tp_down = 0;
  232. tp_acc_x = 0;
  233. tp_acc_y = 0;
  234. if (tp_last_buttons != *buttons) {
  235. tp_last_buttons = *buttons;
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. if (!tp_down) {
  241. tp_down = 1;
  242. tp_last_x = x;
  243. tp_last_y = y;
  244. *dx = 0;
  245. *dy = 0;
  246. } else {
  247. tp_acc_x += x - tp_last_x;
  248. /* The Elan's Y axis increases upwards, already the
  249. * PS/2 convention: dragging up gives a positive dy. */
  250. tp_acc_y += y - tp_last_y;
  251. tp_last_x = x;
  252. tp_last_y = y;
  253. *dx = tp_acc_x / 2;
  254. tp_acc_x -= *dx * 2;
  255. *dy = tp_acc_y / 2;
  256. tp_acc_y -= *dy * 2;
  257. }
  258. if (*dx > 64)
  259. *dx = 64;
  260. if (*dx < -64)
  261. *dx = -64;
  262. if (*dy > 64)
  263. *dy = 64;
  264. if (*dy < -64)
  265. *dy = -64;
  266. if (tp_last_buttons != *buttons) {
  267. tp_last_buttons = *buttons;
  268. return 1;
  269. }
  270. return *dx || *dy;
  271. }
  272. }
  273. void program_touchpad_main(void)
  274. {
  275. if (!tp_present) {
  276. io_putstr("touchpad: absent (");
  277. io_putstr(tp_err ? tp_err : "unknown");
  278. if (tp_err_val) {
  279. io_putstr(" ab=");
  280. io_puthex(tp_err_val);
  281. }
  282. io_putstr(")\n");
  283. return;
  284. }
  285. print_summary();
  286. }