touchpad.c 26 KB

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