touchpad.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /* Touchpads on the LPSS I2C buses.
  2. *
  3. * Two HID-over-I2C families are supported:
  4. * - Elan on the HP Chromebook 11 G5 EE (Braswell LPSS I2C6 at 0xD1327000,
  5. * slave 0x15, vendor report 0x5D);
  6. * - Synaptics 06CB:7F8F on the Lenovo Yoga 720-13IKB (Sunrise Point-LP
  7. * LPSS I2C0 at PCI 00:15.0, slave 0x2C, Windows-Precision report 3:
  8. * five 5-byte finger records, scan time, contact count and clickpad).
  9. *
  10. * The LPSS I2C functions are hidden from PCI in ACPI mode on Braswell, so
  11. * that path scans the two firmware BAR windows for DesignWare component
  12. * types; the Skylake/Kaby Lake path finds the PCI 00:15.0 function and uses
  13. * its BAR.
  14. *
  15. * The report line is a GPIO we have no driver for, so the input register is
  16. * polled. A read with no pending report returns a zero length immediately.
  17. * Reading the register back-to-back stalls the device: 25 ms (40 Hz) is the
  18. * rate it keeps up with, and the pointer still moves smoothly.
  19. *
  20. * Init follows the HID-over-I2C descriptor and the vendor wake sequence.
  21. * Report decoding is per-family; the contact behaviour (pointer deltas,
  22. * taps, the physical-button latch and two-finger scrolling) lives in
  23. * touchpad_gesture.c so the backends share it.
  24. */
  25. #include "cpu.h"
  26. #include "touchpad.h"
  27. #include "touchpad_gesture.h"
  28. #include "i2c.h"
  29. #include "io.h"
  30. #include "paging.h"
  31. #include "pci.h"
  32. #include "serial.h"
  33. #include "timer.h"
  34. #define TP_ADDR 0x15
  35. #define TP_SYN_ADDR 0x2c
  36. #define TP_CTRL_MAX 16
  37. #define TP_CTRL_STEP 0x1000u
  38. #define TP_HID_DESC_REG 0x0001
  39. #define TP_HID_DESC_LEN 30
  40. #define TP_CMD_REG 0x0005
  41. #define TP_CMD_RESET 0x0100
  42. #define TP_CMD_WAKE 0x0800
  43. #define TP_MODE_REG 0x0300
  44. #define TP_MODE_ABS 0x0001
  45. #define TP_DW_COMP_TYPE 0x44570140u
  46. #define TP_REPORT_ID 0x5d
  47. #define TP_SYN_REPORT_ID 3
  48. #define TP_SYN_REPORT_LEN 30
  49. #define TP_SYN_MAX_X 1219
  50. #define TP_SYN_MAX_Y 799
  51. /* Sunrise Point-LP GPIO community 1 (GPP_C) and the I2C0 pads. The
  52. * DSDT builds the community windows as SBRG + 0xAE0000 (community 1),
  53. * +0xAF0000 (0) and +0xAC0000 (2); P2SB's fixed 16 MB MMIO window on
  54. * this PCH starts at 0xFD000000. */
  55. #define TP_MAX_X_REG 0x0106
  56. #define TP_MAX_Y_REG 0x0107
  57. #define TP_ABS_MAX 4095
  58. #define TP_MAX_INPUT 64
  59. #define TP_POLL_MS 25
  60. enum {
  61. TP_KIND_NONE,
  62. TP_KIND_ELAN,
  63. TP_KIND_SYNAPTICS,
  64. };
  65. static unsigned int tp_base;
  66. static unsigned int tp_addr = TP_ADDR;
  67. static int tp_kind;
  68. static int tp_present;
  69. static int tp_invert_y;
  70. static unsigned int tp_in_reg = 0x0003;
  71. static unsigned int tp_max_in = 34;
  72. static unsigned int tp_cmd_reg = TP_CMD_REG;
  73. static unsigned short tp_vendor;
  74. static unsigned short tp_product;
  75. static int tp_max_x = TP_ABS_MAX;
  76. static int tp_max_y = TP_ABS_MAX;
  77. static const char *tp_err;
  78. static unsigned int tp_err_val;
  79. /* Probe diagnostics for `touchpad` when the controller/device is absent. */
  80. static unsigned int tp_dbg_id, tp_dbg_bar, tp_dbg_cmd, tp_dbg_comp;
  81. static unsigned int tp_dbg_priv0, tp_dbg_priv4;
  82. static unsigned int tp_dbg_pmec, tp_dbg_pmec_after;
  83. static int tp_dbg_seen;
  84. static struct tpg_state tp_gesture;
  85. static int tp_last_buttons;
  86. static unsigned int tp_next_poll;
  87. /* Last report, for the `touchpad` status command. */
  88. static int tp_stat_fingers, tp_stat_f1, tp_stat_x, tp_stat_y;
  89. static void put_dec(unsigned int v);
  90. static const char *rc_name(void);
  91. static void put_hex16(unsigned int v)
  92. {
  93. static const char hex[] = "0123456789ABCDEF";
  94. io_putchar(hex[(v >> 12) & 0xf]);
  95. io_putchar(hex[(v >> 8) & 0xf]);
  96. io_putchar(hex[(v >> 4) & 0xf]);
  97. io_putchar(hex[v & 0xf]);
  98. }
  99. static void print_summary(void)
  100. {
  101. io_putstr("touchpad: ");
  102. io_putstr(tp_kind == TP_KIND_SYNAPTICS ? "synaptics " : "elan ");
  103. put_hex16(tp_vendor);
  104. io_putchar(':');
  105. put_hex16(tp_product);
  106. io_putstr(" i2c 0x");
  107. io_puthex(tp_base);
  108. io_putstr(" addr 0x");
  109. io_puthex(tp_addr);
  110. io_putstr(" max ");
  111. put_dec((unsigned int)tp_max_x);
  112. io_putchar('x');
  113. put_dec((unsigned int)tp_max_y);
  114. io_putchar('\n');
  115. }
  116. static void print_status(void)
  117. {
  118. unsigned int opts = tpg_options(&tp_gesture);
  119. print_summary();
  120. io_putstr("touchpad: rightclick ");
  121. io_putstr(rc_name());
  122. io_putstr(" tapclick ");
  123. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  124. io_putstr(" scroll ");
  125. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  126. if (opts & TPG_SCROLL) {
  127. io_putchar(' ');
  128. io_putstr((opts & TPG_SCROLL_INVERT) ?
  129. "traditional" : "natural");
  130. }
  131. io_putstr(" invert ");
  132. io_putstr(tp_invert_y ? "on" : "off");
  133. io_putstr(" corner x>=");
  134. put_dec((unsigned int)(tp_max_x * TPG_CORNER_X_PCT / 100));
  135. io_putstr(" y<=");
  136. put_dec((unsigned int)(tp_max_y * TPG_CORNER_Y_PCT / 100));
  137. io_putchar('\n');
  138. io_putstr("touchpad: last f=");
  139. put_dec((unsigned int)tp_stat_fingers);
  140. if (tp_stat_f1) {
  141. io_putstr(" x=");
  142. put_dec((unsigned int)tp_stat_x);
  143. io_putstr(" y=");
  144. put_dec((unsigned int)tp_stat_y);
  145. }
  146. io_putstr(" taps left=");
  147. put_dec(tp_gesture.taps_left);
  148. io_putstr(" right=");
  149. put_dec(tp_gesture.taps_right);
  150. io_putchar('\n');
  151. }
  152. static void msleep(unsigned int ms)
  153. {
  154. unsigned int t0 = get_ticks_ms();
  155. while (get_ticks_ms() - t0 < ms)
  156. ;
  157. }
  158. static unsigned int get16(const unsigned char *p)
  159. {
  160. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  161. }
  162. static void put_dec(unsigned int v)
  163. {
  164. char buf[11];
  165. unsigned int i = 0;
  166. if (!v) {
  167. io_putchar('0');
  168. return;
  169. }
  170. while (v) {
  171. buf[i++] = (char)('0' + v % 10);
  172. v /= 10;
  173. }
  174. while (i)
  175. io_putchar(buf[--i]);
  176. }
  177. /* Match one whitespace-delimited word at s (end or space after it). */
  178. static int word_is(const char *s, const char *word)
  179. {
  180. while (*word) {
  181. if (*s != *word)
  182. return 0;
  183. s++;
  184. word++;
  185. }
  186. return *s == '\0' || *s == ' ';
  187. }
  188. static const char *skip_spaces(const char *s)
  189. {
  190. while (*s == ' ')
  191. s++;
  192. return s;
  193. }
  194. static unsigned int word_len(const char *s)
  195. {
  196. unsigned int n = 0;
  197. while (s[n] && s[n] != ' ')
  198. n++;
  199. return n;
  200. }
  201. static const char *rc_name(void)
  202. {
  203. unsigned int m = tpg_options(&tp_gesture) &
  204. (TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  205. if (m == TPG_RIGHT_TAP)
  206. return "tap";
  207. if (m == TPG_RIGHT_CORNER)
  208. return "corner";
  209. if (m == (TPG_RIGHT_TAP | TPG_RIGHT_CORNER))
  210. return "both";
  211. return "off";
  212. }
  213. static int hid_read_reg(unsigned int addr, unsigned int reg,
  214. unsigned char *buf, unsigned int len)
  215. {
  216. unsigned char r[2];
  217. r[0] = (unsigned char)(reg & 0xff);
  218. r[1] = (unsigned char)(reg >> 8);
  219. if (i2c_dw_transfer(tp_base, addr, r, 2, buf, len) == 0)
  220. return 0;
  221. /* Some devices do not answer the combined write+read (repeated
  222. * start). Retry with a STOP between the register address and the
  223. * data read. */
  224. if (i2c_dw_transfer(tp_base, addr, r, 2, 0, 0) != 0)
  225. return -1;
  226. return i2c_dw_transfer(tp_base, addr, 0, 0, buf, len);
  227. }
  228. static int hid_write_reg(unsigned int addr, unsigned int reg, unsigned int val)
  229. {
  230. unsigned char b[4];
  231. b[0] = (unsigned char)(reg & 0xff);
  232. b[1] = (unsigned char)(reg >> 8);
  233. b[2] = (unsigned char)(val & 0xff);
  234. b[3] = (unsigned char)(val >> 8);
  235. return i2c_dw_transfer(tp_base, addr, b, 4, 0, 0);
  236. }
  237. /* Native Elan max-coordinate commands (the same read-cmd path Linux
  238. * elan_i2c uses). They give the corner zone a physical frame instead of
  239. * assuming the full 12-bit range; on failure keep the 4095 fallback. */
  240. static void query_limits(void)
  241. {
  242. unsigned char v[2];
  243. if (hid_read_reg(tp_addr, TP_MAX_X_REG, v, 2) == 0) {
  244. unsigned int m = get16(v) & 0x0fff;
  245. if (m >= 256)
  246. tp_max_x = (int)m;
  247. }
  248. if (hid_read_reg(tp_addr, TP_MAX_Y_REG, v, 2) == 0) {
  249. unsigned int m = get16(v) & 0x0fff;
  250. if (m >= 256)
  251. tp_max_y = (int)m;
  252. }
  253. }
  254. static int probe_controller(unsigned int base)
  255. {
  256. unsigned char desc[TP_HID_DESC_LEN + 2];
  257. unsigned char ack[2];
  258. unsigned int off, len, ver;
  259. if (!i2c_dw_present(base))
  260. return 0;
  261. if (i2c_dw_init(base) != 0) {
  262. tp_err = "i2c init failed";
  263. return 0;
  264. }
  265. tp_base = base;
  266. /* The Elan address is fixed; a plain read is a harmless ACK check. */
  267. if (i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 1) != 0) {
  268. tp_err = "no device at 0x15";
  269. return 0;
  270. }
  271. /* Reset first and consume the acknowledgement, then read the HID
  272. * descriptor. The descriptor read is prefixed with two status
  273. * bytes and the 2-byte length, so accept either offset. */
  274. hid_write_reg(TP_ADDR, TP_CMD_REG, TP_CMD_RESET);
  275. msleep(150);
  276. (void)i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 2);
  277. if (hid_read_reg(TP_ADDR, TP_HID_DESC_REG, desc, sizeof(desc)) != 0) {
  278. tp_err = "descriptor read failed";
  279. tp_err_val = i2c_last_abort;
  280. return 0;
  281. }
  282. off = 0;
  283. if (get16(desc + 0) != TP_HID_DESC_LEN ||
  284. get16(desc + 2) != 0x0100) {
  285. if (get16(desc + 2) == TP_HID_DESC_LEN &&
  286. get16(desc + 4) == 0x0100)
  287. off = 2;
  288. }
  289. len = get16(desc + off);
  290. ver = get16(desc + off + 2);
  291. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  292. tp_err = "bad hid descriptor";
  293. return 0;
  294. }
  295. tp_in_reg = get16(desc + off + 8);
  296. tp_max_in = get16(desc + off + 10);
  297. tp_cmd_reg = get16(desc + off + 16);
  298. tp_vendor = (unsigned short)get16(desc + off + 20);
  299. tp_product = (unsigned short)get16(desc + off + 22);
  300. if (tp_max_in < 12 || tp_max_in > TP_MAX_INPUT)
  301. tp_max_in = 34;
  302. /* Absolute reporting, then wake: after the reset the Elan stays
  303. * quiet until the host selects a mode. */
  304. hid_write_reg(TP_ADDR, TP_MODE_REG, TP_MODE_ABS);
  305. msleep(40);
  306. hid_write_reg(TP_ADDR, tp_cmd_reg, TP_CMD_WAKE);
  307. msleep(60);
  308. /* Query the limits with the device awake (Linux reads them after
  309. * initialize()/wake and before enabling reports). */
  310. query_limits();
  311. tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
  312. tp_present = 1;
  313. tp_kind = TP_KIND_ELAN;
  314. tp_addr = TP_ADDR;
  315. tp_invert_y = 0;
  316. tp_err = 0;
  317. print_summary();
  318. return 1;
  319. }
  320. /* Synaptics 06CB:7F8F Windows-Precision touchpad (Yoga 720-13IKB) on the
  321. * Sunrise Point-LP LPSS I2C0. The HID descriptor is the standard 30-byte
  322. * HID-over-I2C block; report 3 is five 5-byte finger records (tip/confidence,
  323. * contact id, 16-bit X, 16-bit Y), then scan time, contact count and the
  324. * clickpad button. */
  325. static int probe_synaptics(unsigned int base)
  326. {
  327. unsigned char desc[TP_HID_DESC_LEN + 2];
  328. unsigned char ack[2];
  329. unsigned int off, len, ver, i;
  330. int tried_power = 0;
  331. if (i2c_dw_init(base) != 0) {
  332. tp_err = "i2c init failed";
  333. return 0;
  334. }
  335. /* The LPSS reset release at 0x204 clears the 0x200 clock gate, so
  336. * enable the functional clock after it. */
  337. i2c_dw_clock_spt(base);
  338. tp_base = base;
  339. tp_addr = TP_SYN_ADDR;
  340. if (!tp_dbg_seen) {
  341. tp_dbg_priv0 = *(volatile unsigned int *)(base + 0x200);
  342. tp_dbg_priv4 = *(volatile unsigned int *)(base + 0x204);
  343. }
  344. /* Linux's i2c-hid first checks the address and reads the HID
  345. * descriptor; only the report-descriptor stage powers the device on
  346. * and resets it. Do the same, with a power-on/retry fallback for a
  347. * device left asleep by a previous OS. */
  348. if (i2c_dw_transfer(base, tp_addr, 0, 0, ack, 1) != 0) {
  349. tp_err = "no device at 0x2c";
  350. tp_err_val = i2c_last_abort;
  351. i2c_dw_dump(base);
  352. return 0;
  353. }
  354. for (i = 0; i < 4; i++) {
  355. if (hid_read_reg(tp_addr, TP_HID_DESC_REG, desc,
  356. sizeof(desc)) == 0)
  357. break;
  358. if (!tried_power) {
  359. tried_power = 1;
  360. hid_write_reg(tp_addr, TP_CMD_REG, TP_CMD_WAKE);
  361. msleep(60);
  362. } else {
  363. hid_write_reg(tp_addr, TP_CMD_REG, TP_CMD_RESET);
  364. msleep(200);
  365. }
  366. }
  367. if (i == 4) {
  368. tp_err = "descriptor read failed";
  369. tp_err_val = i2c_last_abort;
  370. return 0;
  371. }
  372. off = 0;
  373. len = get16(desc + 0);
  374. ver = get16(desc + 2);
  375. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  376. if (get16(desc + 2) == TP_HID_DESC_LEN &&
  377. get16(desc + 4) == 0x0100)
  378. off = 2;
  379. len = get16(desc + off);
  380. ver = get16(desc + off + 2);
  381. }
  382. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  383. tp_err = "bad hid descriptor";
  384. return 0;
  385. }
  386. tp_in_reg = get16(desc + off + 8);
  387. tp_max_in = get16(desc + off + 10);
  388. tp_cmd_reg = get16(desc + off + 16);
  389. tp_vendor = (unsigned short)get16(desc + off + 20);
  390. tp_product = (unsigned short)get16(desc + off + 22);
  391. if (tp_vendor != 0x06CB || tp_product != 0x7F8F) {
  392. tp_err = "not a Synaptics 06CB:7F8F";
  393. return 0;
  394. }
  395. if (tp_max_in < TP_SYN_REPORT_LEN + 2 || tp_max_in > TP_MAX_INPUT)
  396. tp_max_in = TP_SYN_REPORT_LEN + 2;
  397. /* i2c_hid_hwreset(): power on (60 ms), reset, then wait for the
  398. * zero-length reset-complete input report before using the device. */
  399. hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_WAKE);
  400. msleep(60);
  401. hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_RESET);
  402. for (i = 0; i < 40; i++) {
  403. unsigned char z[TP_MAX_INPUT];
  404. if (i2c_dw_transfer(base, tp_addr, 0, 0, z, tp_max_in) == 0 &&
  405. z[0] == 0 && z[1] == 0)
  406. break;
  407. msleep(25);
  408. }
  409. hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_WAKE);
  410. msleep(60);
  411. tp_kind = TP_KIND_SYNAPTICS;
  412. /* The Windows-Precision report uses the logical frame from its
  413. * descriptor. Default to Y-down (the usual HID digitizer frame);
  414. * `touchpad invert off` fixes a unit that reports Y-up. */
  415. tp_invert_y = 1;
  416. tp_max_x = TP_SYN_MAX_X;
  417. tp_max_y = TP_SYN_MAX_Y;
  418. tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
  419. tp_present = 1;
  420. tp_err = 0;
  421. print_summary();
  422. return 1;
  423. }
  424. /* Sunrise Point-LP leaves the LPSS functions with memory decode off: Linux
  425. * logs "intel-lpss 0000:00:15.0: enabling device (0000 -> 0002)" before it
  426. * can even read the DesignWare component type. A warm reboot from another
  427. * OS can additionally leave the function in D3hot, so put it back in D0.
  428. * Both steps are no-ops when firmware already did them. */
  429. static void lpss_pci_prepare(unsigned int dev, unsigned int func)
  430. {
  431. unsigned int cmd, pmcsr;
  432. cmd = pci_cfg_read32(0, dev, func, 0x04) & 0xFFFFu;
  433. pci_cfg_write16(0, dev, func, 0x04,
  434. (unsigned short)(cmd | 0x0006u));
  435. /* The DSDT's I2C0._PS0() is LPD0(SB10), which clears bits 15 and 1
  436. * of the LPSS PMECTRLSTATUS at config offset 0x84 through the P2SB
  437. * alias. The same register is directly reachable as PCI config
  438. * 0x84. Writing the PMC's PWRMBASE + 0x84 instead (0xFE000084)
  439. * left the LPSS island power-gated: the DesignWare register file
  440. * answered, but the I2C FSM never drove the bus, so the touchpad
  441. * NAKed its address. */
  442. pmcsr = pci_cfg_read32(0, dev, func, 0x84);
  443. if (!tp_dbg_seen)
  444. tp_dbg_pmec = pmcsr;
  445. if (pmcsr != 0xFFFFFFFFu) {
  446. pci_cfg_write32(0, dev, func, 0x84, pmcsr & 0xFFFF7FFCu);
  447. if (!tp_dbg_seen)
  448. tp_dbg_pmec_after = pci_cfg_read32(0, dev, func,
  449. 0x84);
  450. }
  451. msleep(10);
  452. }
  453. int touchpad_init(void)
  454. {
  455. /* The ACPI _CRS address for the Braswell LPSS I2C blocks is
  456. * 0x9132xxxx on coreboot, but this firmware maps them at 0xD132xxxx.
  457. * Both windows are scanned; page 7 (I2C6) is tried first. */
  458. static const unsigned int ranges[2] = { 0x91320000u, 0xd1320000u };
  459. unsigned int r, k;
  460. if (tp_present)
  461. return 1;
  462. tp_err = 0;
  463. tp_err_val = 0;
  464. /* Braswell (HP Chromebook 11 G5 EE): LPSS I2C6 is hidden from PCI. */
  465. if (cpu_family_model() == 0x64Cu) {
  466. for (r = 0; r < 2; r++) {
  467. for (k = 0; k < TP_CTRL_MAX; k++) {
  468. unsigned int i = (k + 7) % TP_CTRL_MAX;
  469. unsigned int base = (unsigned int)ioremap(
  470. ranges[r] + i * TP_CTRL_STEP, 0x1000,
  471. IOREMAP_MMIO);
  472. if (!base)
  473. continue;
  474. /* A block left in functional reset keeps its
  475. * register file but never clocks the I2C FSM. */
  476. i2c_dw_reset_release(base);
  477. if (probe_controller(base))
  478. return 1;
  479. }
  480. }
  481. }
  482. /* Sunrise Point-LP LPSS I2C0/1/2 (Skylake/Kaby Lake): the functions
  483. * are visible at PCI 00:15.0/1/2. The Yoga's Synaptics touchpad is
  484. * on I2C0 at 0x2C; the Wacom touchscreen on I2C1 is rejected by the
  485. * vendor/product check in probe_synaptics(). */
  486. for (k = 0; k < 3; k++) {
  487. unsigned int id = pci_cfg_read32(0, 0x15, k, 0x00);
  488. unsigned int bar, base;
  489. if (id != 0x9d608086u && id != 0x9d618086u &&
  490. id != 0x9d628086u)
  491. continue;
  492. lpss_pci_prepare(0x15, k);
  493. bar = pci_cfg_read32(0, 0x15, k, 0x10) & ~0xFu;
  494. if (!bar)
  495. continue;
  496. base = (unsigned int)ioremap(bar, 0x1000, IOREMAP_MMIO);
  497. if (!base)
  498. continue;
  499. /* Release the LPSS function reset before reading the
  500. * DesignWare component type: the register file may answer
  501. * while the block is still in functional reset, and the
  502. * I2C FSM never clocks until 0x204 is released. */
  503. i2c_dw_reset_release(base);
  504. if (!tp_dbg_seen) {
  505. tp_dbg_id = id;
  506. tp_dbg_bar = bar;
  507. tp_dbg_cmd = pci_cfg_read32(0, 0x15, k, 0x04) &
  508. 0xFFFFu;
  509. tp_dbg_comp = i2c_dw_comp_type(base);
  510. }
  511. if (!i2c_dw_present(base)) {
  512. tp_err = "LPSS not a DesignWare I2C";
  513. tp_err_val = tp_dbg_comp;
  514. tp_dbg_seen = 1;
  515. continue;
  516. }
  517. if (probe_synaptics(base))
  518. return 1;
  519. tp_dbg_seen = 1;
  520. }
  521. if (!tp_err)
  522. tp_err = "no LPSS I2C touchpad";
  523. return 0;
  524. }
  525. int touchpad_present(void)
  526. {
  527. return tp_present;
  528. }
  529. unsigned int touchpad_addr(void)
  530. {
  531. return tp_addr;
  532. }
  533. unsigned short touchpad_vendor(void)
  534. {
  535. return tp_vendor;
  536. }
  537. unsigned short touchpad_product(void)
  538. {
  539. return tp_product;
  540. }
  541. static int elan_decode(const unsigned char *buf, struct tpg_frame *f,
  542. unsigned int now)
  543. {
  544. unsigned int b, ti, off, i, n = 0;
  545. for (b = 0; b < 6 && b + 6 < tp_max_in; b++)
  546. if (buf[b] == TP_REPORT_ID)
  547. break;
  548. if (b >= 6 || b + 6 >= tp_max_in)
  549. return 0;
  550. ti = buf[b + 1];
  551. off = b + 2;
  552. f->count = 0;
  553. f->mask = 0;
  554. f->button = ti & 0x01;
  555. f->now_ms = now;
  556. for (i = 0; i < 5; i++) {
  557. if (!(ti & (0x08u << i)))
  558. continue;
  559. f->count++;
  560. if (n < TPG_CONTACTS && off + 4 < tp_max_in) {
  561. unsigned int xh = buf[off];
  562. f->contact[n].x = ((int)(xh & 0xf0) << 4) |
  563. buf[off + 1];
  564. f->contact[n].y = ((int)(xh & 0x0f) << 8) |
  565. buf[off + 2];
  566. if (tp_invert_y)
  567. f->contact[n].y = tp_max_y - 1 -
  568. f->contact[n].y;
  569. f->mask |= 1u << n;
  570. n++;
  571. }
  572. off += 5;
  573. }
  574. return 1;
  575. }
  576. /* Windows-Precision report 3: five 5-byte finger records, then 2-byte scan
  577. * time, contact count and the clickpad button bit. */
  578. static int synaptics_decode(const unsigned char *buf, struct tpg_frame *f,
  579. unsigned int now)
  580. {
  581. unsigned int b, p, i, n = 0;
  582. for (b = 0; b < 6 && b + TP_SYN_REPORT_LEN <= tp_max_in; b++)
  583. if (buf[b] == TP_SYN_REPORT_ID)
  584. break;
  585. if (b >= 6 || b + TP_SYN_REPORT_LEN > tp_max_in)
  586. return 0;
  587. p = b + 1;
  588. f->count = buf[b + 28];
  589. f->mask = 0;
  590. f->button = buf[b + 29] & 1;
  591. f->now_ms = now;
  592. for (i = 0; i < 5; i++, p += 5) {
  593. unsigned int f0 = buf[p];
  594. int x, y;
  595. if (!(f0 & 0x01))
  596. continue;
  597. x = (int)get16(buf + p + 1);
  598. y = (int)get16(buf + p + 3);
  599. if (tp_invert_y)
  600. y = tp_max_y - 1 - y;
  601. if (n < TPG_CONTACTS) {
  602. f->contact[n].x = x;
  603. f->contact[n].y = y;
  604. f->mask |= 1u << n;
  605. n++;
  606. }
  607. }
  608. if (f->count > 5)
  609. f->count = 5;
  610. return 1;
  611. }
  612. int touchpad_poll(int *buttons, int *dx, int *dy, int *scroll)
  613. {
  614. unsigned char buf[TP_MAX_INPUT];
  615. unsigned int now;
  616. struct tpg_result res;
  617. *buttons = 0;
  618. *dx = 0;
  619. *dy = 0;
  620. *scroll = 0;
  621. if (!tp_present)
  622. return 0;
  623. now = get_ticks_ms();
  624. /* The synthetic tap press is released by time, whether or not a new
  625. * report arrives (the finger is already up). */
  626. tpg_tick(&tp_gesture, now, &res);
  627. if (res.buttons != (unsigned int)tp_last_buttons) {
  628. tp_last_buttons = (int)res.buttons;
  629. *buttons = res.buttons;
  630. return 1;
  631. }
  632. if ((int)(now - tp_next_poll) < 0)
  633. return 0;
  634. tp_next_poll = now + TP_POLL_MS;
  635. if (hid_read_reg(tp_addr, tp_in_reg, buf, tp_max_in) != 0)
  636. return 0;
  637. {
  638. struct tpg_frame f;
  639. if (tp_kind == TP_KIND_SYNAPTICS) {
  640. if (!synaptics_decode(buf, &f, now))
  641. return 0;
  642. } else {
  643. if (!elan_decode(buf, &f, now))
  644. return 0;
  645. }
  646. tp_stat_fingers = (int)f.count;
  647. tp_stat_f1 = (f.mask & 1u) != 0;
  648. if (f.mask & 1u) {
  649. tp_stat_x = f.contact[0].x;
  650. tp_stat_y = f.contact[0].y;
  651. }
  652. tpg_frame(&tp_gesture, &f, &res);
  653. }
  654. *buttons = res.buttons;
  655. *dx = res.dx;
  656. *dy = res.dy;
  657. *scroll = res.scroll;
  658. if (res.buttons != (unsigned int)tp_last_buttons) {
  659. tp_last_buttons = (int)res.buttons;
  660. return 1;
  661. }
  662. return res.dx || res.dy || res.scroll;
  663. }
  664. static void print_absent(void)
  665. {
  666. io_putstr("touchpad: absent (");
  667. io_putstr(tp_err ? tp_err : "unknown");
  668. if (tp_err_val) {
  669. io_putstr(" val=");
  670. io_puthex(tp_err_val);
  671. }
  672. io_putstr(")\n");
  673. if (tp_dbg_id) {
  674. io_putstr("touchpad: lpss id ");
  675. io_puthex(tp_dbg_id);
  676. io_putstr(" bar 0x");
  677. io_puthex(tp_dbg_bar);
  678. io_putstr(" cmd ");
  679. io_puthex(tp_dbg_cmd);
  680. io_putstr(" comp ");
  681. io_puthex(tp_dbg_comp);
  682. io_putstr(" priv ");
  683. io_puthex(tp_dbg_priv0);
  684. io_putchar('/');
  685. io_puthex(tp_dbg_priv4);
  686. io_putstr(" pmec ");
  687. io_puthex(tp_dbg_pmec);
  688. io_putchar('/');
  689. io_puthex(tp_dbg_pmec_after);
  690. io_putchar('\n');
  691. }
  692. }
  693. static void tp_serial_status(void)
  694. {
  695. serial_puts(SERIAL_COM1, "touchpad: ");
  696. if (tp_present) {
  697. serial_puts(SERIAL_COM1, tp_kind == TP_KIND_SYNAPTICS ?
  698. "present synaptics " : "present elan ");
  699. serial_puthex(SERIAL_COM1, tp_vendor);
  700. serial_putc(SERIAL_COM1, ':');
  701. serial_puthex(SERIAL_COM1, tp_product);
  702. serial_puts(SERIAL_COM1, " i2c 0x");
  703. serial_puthex(SERIAL_COM1, tp_base);
  704. serial_puts(SERIAL_COM1, " addr 0x");
  705. serial_puthex(SERIAL_COM1, tp_addr);
  706. } else {
  707. serial_puts(SERIAL_COM1, "absent ");
  708. serial_puts(SERIAL_COM1, tp_err ? tp_err : "unknown");
  709. serial_puts(SERIAL_COM1, " val=");
  710. serial_puthex(SERIAL_COM1, tp_err_val);
  711. if (tp_dbg_id) {
  712. serial_puts(SERIAL_COM1, " id=");
  713. serial_puthex(SERIAL_COM1, tp_dbg_id);
  714. serial_puts(SERIAL_COM1, " bar=");
  715. serial_puthex(SERIAL_COM1, tp_dbg_bar);
  716. serial_puts(SERIAL_COM1, " cmd=");
  717. serial_puthex(SERIAL_COM1, tp_dbg_cmd);
  718. serial_puts(SERIAL_COM1, " comp=");
  719. serial_puthex(SERIAL_COM1, tp_dbg_comp);
  720. serial_puts(SERIAL_COM1, " priv=");
  721. serial_puthex(SERIAL_COM1, tp_dbg_priv0);
  722. serial_putc(SERIAL_COM1, '/');
  723. serial_puthex(SERIAL_COM1, tp_dbg_priv4);
  724. serial_puts(SERIAL_COM1, " pmec=");
  725. serial_puthex(SERIAL_COM1, tp_dbg_pmec);
  726. serial_putc(SERIAL_COM1, '/');
  727. serial_puthex(SERIAL_COM1, tp_dbg_pmec_after);
  728. }
  729. }
  730. serial_putc(SERIAL_COM1, '\n');
  731. }
  732. void program_touchpad_main(const char *arg)
  733. {
  734. arg = arg ? skip_spaces(arg) : 0;
  735. if (arg && word_is(arg, "probe")) {
  736. if (!tp_present)
  737. touchpad_init();
  738. if (tp_present)
  739. print_status();
  740. else
  741. print_absent();
  742. tp_serial_status();
  743. return;
  744. }
  745. if (!tp_present) {
  746. print_absent();
  747. tp_serial_status();
  748. return;
  749. }
  750. arg = arg ? skip_spaces(arg) : 0;
  751. if (!arg || !*arg) {
  752. print_status();
  753. return;
  754. }
  755. while (*arg) {
  756. unsigned int opts = tpg_options(&tp_gesture);
  757. if (word_is(arg, "rightclick")) {
  758. const char *v = skip_spaces(arg + 10);
  759. if (!*v) {
  760. io_putstr("touchpad: rightclick ");
  761. io_putstr(rc_name());
  762. io_putchar('\n');
  763. return;
  764. }
  765. opts &= ~(TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  766. if (word_is(v, "off")) {
  767. /* keep both bits clear */
  768. } else if (word_is(v, "tap")) {
  769. opts |= TPG_RIGHT_TAP;
  770. } else if (word_is(v, "corner")) {
  771. opts |= TPG_RIGHT_CORNER;
  772. } else if (word_is(v, "both")) {
  773. opts |= TPG_RIGHT_TAP | TPG_RIGHT_CORNER;
  774. } else {
  775. io_putstr("touchpad: rightclick off|tap|corner|both\n");
  776. return;
  777. }
  778. tpg_set_options(&tp_gesture, opts);
  779. io_putstr("touchpad: rightclick ");
  780. io_putstr(rc_name());
  781. io_putchar('\n');
  782. arg = skip_spaces(v + word_len(v));
  783. } else if (word_is(arg, "tapclick")) {
  784. const char *v = skip_spaces(arg + 8);
  785. if (!*v) {
  786. io_putstr("touchpad: tapclick ");
  787. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  788. io_putchar('\n');
  789. return;
  790. }
  791. if (word_is(v, "on"))
  792. opts |= TPG_TAPCLICK;
  793. else if (word_is(v, "off"))
  794. opts &= ~TPG_TAPCLICK;
  795. else {
  796. io_putstr("touchpad: tapclick on|off\n");
  797. return;
  798. }
  799. tpg_set_options(&tp_gesture, opts);
  800. io_putstr("touchpad: tapclick ");
  801. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  802. io_putchar('\n');
  803. arg = skip_spaces(v + word_len(v));
  804. } else if (word_is(arg, "scroll")) {
  805. const char *v = skip_spaces(arg + 6);
  806. if (!*v) {
  807. io_putstr("touchpad: scroll ");
  808. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  809. if (opts & TPG_SCROLL) {
  810. io_putchar(' ');
  811. io_putstr((opts & TPG_SCROLL_INVERT) ?
  812. "traditional" : "natural");
  813. }
  814. io_putchar('\n');
  815. return;
  816. }
  817. if (word_is(v, "on")) {
  818. opts |= TPG_SCROLL;
  819. } else if (word_is(v, "off")) {
  820. opts &= ~TPG_SCROLL;
  821. } else if (word_is(v, "natural")) {
  822. opts |= TPG_SCROLL;
  823. opts &= ~TPG_SCROLL_INVERT;
  824. } else if (word_is(v, "traditional")) {
  825. opts |= TPG_SCROLL;
  826. opts |= TPG_SCROLL_INVERT;
  827. } else {
  828. io_putstr("touchpad: scroll on|off|natural|traditional\n");
  829. return;
  830. }
  831. tpg_set_options(&tp_gesture, opts);
  832. io_putstr("touchpad: scroll ");
  833. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  834. if (opts & TPG_SCROLL) {
  835. io_putchar(' ');
  836. io_putstr((opts & TPG_SCROLL_INVERT) ?
  837. "traditional" : "natural");
  838. }
  839. io_putchar('\n');
  840. arg = skip_spaces(v + word_len(v));
  841. } else if (word_is(arg, "invert")) {
  842. const char *v = skip_spaces(arg + 6);
  843. if (!*v) {
  844. io_putstr("touchpad: invert ");
  845. io_putstr(tp_invert_y ? "on" : "off");
  846. io_putchar('\n');
  847. return;
  848. }
  849. if (word_is(v, "on"))
  850. tp_invert_y = 1;
  851. else if (word_is(v, "off"))
  852. tp_invert_y = 0;
  853. else {
  854. io_putstr("touchpad: invert on|off\n");
  855. return;
  856. }
  857. io_putstr("touchpad: invert ");
  858. io_putstr(tp_invert_y ? "on" : "off");
  859. io_putchar('\n');
  860. arg = skip_spaces(v + word_len(v));
  861. } else {
  862. io_putstr("touchpad: usage: touchpad [probe | rightclick off|tap|corner|both | tapclick on|off | scroll on|off|natural|traditional | invert on|off]\n");
  863. return;
  864. }
  865. }
  866. }