ideapad.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* IdeaPad/Yoga ACPI EC battery and AC (Lenovo Yoga 720-13IKB).
  2. *
  3. * The DSDT BAT1 device reads the battery fields from the PCH EC memory
  4. * window at 0xFF000800 (SystemMemory OperationRegion ECMM), not from the
  5. * 0x62/0x66 EmbeddedControl region. The field layout, straight from the
  6. * DSDT:
  7. *
  8. * 0xC0 BTY0:1 BAM0:1 BAL0:1 0:1 BMF0:4 (BMF0 6 = SIMPLO)
  9. * 0xC1 BST0 battery state (bit 0 discharging, bit 1 charging)
  10. * 0xC2 BRC0 remaining capacity (16-bit)
  11. * 0xC6 BPV0 present voltage, mV (16-bit)
  12. * 0xC8 BDV0 design voltage, mV (16-bit)
  13. * 0xCA BDC0 design capacity (16-bit)
  14. * 0xCC BFC0 last full capacity (16-bit)
  15. * 0xCF CYC0 cycle count
  16. * 0xD2 BAC0 present rate, signed 16-bit (mA when BAM0 == 0)
  17. * 0xED BFCB alternate last-full capacity (16-bit)
  18. * 0xF1 bit 6 ORRF (use BFCB when set)
  19. * 0xA3 bit 7 ADPT (AC adapter online)
  20. * 0xA3 bits 0..3 OSTY, bit 6 ECRD (written once, see ideapad_os_ready)
  21. *
  22. * The DSDT scales capacities by BASC = 10 when BAM0 == 0 (the pack reports
  23. * in 10 mWh units) and converts BAC0 from mA to mW with BPV0. The _BST rate
  24. * is signed; we mirror that.
  25. */
  26. #include "ideapad.h"
  27. #include "paging.h"
  28. #include "serial.h"
  29. #include "syscall.h"
  30. #define ECMM_BASE 0xFF000800u
  31. #define ECMM_SIZE 0x1000u
  32. static unsigned char *ecmm;
  33. static unsigned char *ecmm_map(void)
  34. {
  35. if (!ecmm)
  36. ecmm = (unsigned char *)ioremap(ECMM_BASE, ECMM_SIZE,
  37. IOREMAP_MMIO);
  38. return ecmm;
  39. }
  40. static unsigned int rd16(const unsigned char *p)
  41. {
  42. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  43. }
  44. static void copy_str(char *dst, unsigned int size, const char *src)
  45. {
  46. unsigned int i = 0;
  47. while (i + 1 < size && src[i]) {
  48. dst[i] = src[i];
  49. i++;
  50. }
  51. dst[i] = '\0';
  52. }
  53. /* GUBI() in the DSDT maps BMF0 to the pack vendor. */
  54. static const char *battery_vendor(unsigned int bmf)
  55. {
  56. switch (bmf) {
  57. case 1: return "SANYO";
  58. case 2: return "SONY";
  59. case 3: return "SDI";
  60. case 4: return "PANASONIC";
  61. case 5: return "SAMSUNG";
  62. case 6: return "SIMPLO";
  63. case 7: return "DYNAPACK";
  64. default: return "LENOVO";
  65. }
  66. }
  67. int ideapad_present(void)
  68. {
  69. unsigned char *p = ecmm_map();
  70. unsigned int bdc, bfc, bpv;
  71. if (!p)
  72. return 0;
  73. bdc = rd16(p + 0xCA);
  74. bfc = rd16(p + 0xCC);
  75. bpv = rd16(p + 0xC6);
  76. if (bdc == 0 || bdc > 0x7FFF || bfc == 0 || bfc > 0x7FFF ||
  77. bpv < 3000 || bpv > 20000)
  78. return 0;
  79. /* Only the low three status bits are defined, and BMF0 is a small
  80. * pack-vendor id; anything else means 0xFF000800 is not this EC. */
  81. if (p[0xC1] > 0x07)
  82. return 0;
  83. if (((p[0xC0] >> 4) & 0xF) > 7)
  84. return 0;
  85. return 1;
  86. }
  87. /* EC0._REG(3, 1) in the DSDT: OSTP() reports the OS type (OSTY, 0xA3 bits
  88. * 0..3; Linux and Windows 10 make OSYS 0x07DF, OSTY 6), then ECFG(1) sets
  89. * ECRD (0xA3 bit 6), "the OS EC driver is ready". Until the OS does this
  90. * the EC keeps the I2C touchpad to itself (the Synaptics NAKs its address
  91. * on the LPSS I2C0 bus), the way an ACPI-less OS finds it. Both fields are
  92. * Preserve, so this is the same byte read-modify-write the AML does. */
  93. static void ideapad_os_ready(unsigned char *p)
  94. {
  95. unsigned int v = p[0xA3];
  96. p[0xA3] = (unsigned char)((v & ~0x4Fu) | 0x06u | 0x40u);
  97. serial_puts(SERIAL_COM1, "touchpad: ideapad EC os ready a3 ");
  98. serial_puthex(SERIAL_COM1, v);
  99. serial_puts(SERIAL_COM1, " -> ");
  100. serial_puthex(SERIAL_COM1, p[0xA3]);
  101. serial_putc(SERIAL_COM1, '\n');
  102. }
  103. void ideapad_init(void)
  104. {
  105. if (!ideapad_present())
  106. return;
  107. serial_puts(SERIAL_COM1,
  108. "ideapad: EC battery/AC at 0xFF000800\n");
  109. ideapad_os_ready(ecmm);
  110. }
  111. void ideapad_power_read(struct power_info *out, int detail)
  112. {
  113. unsigned char *p;
  114. unsigned int bst, bam, bmf, basc;
  115. unsigned int brc, bfc, bfi, bdc, bpv, bac, cyc, orrf;
  116. unsigned int i;
  117. int rate;
  118. struct power_battery *b;
  119. for (i = 0; i < sizeof(*out); i++)
  120. ((unsigned char *)out)[i] = 0;
  121. out->ac_online = -1;
  122. p = ecmm_map();
  123. if (!p || !ideapad_present())
  124. return;
  125. out->present = 1;
  126. out->source = SYS_POWER_SRC_IDEAPAD;
  127. out->ac_online = (p[0xA3] >> 7) & 1;
  128. out->battery_count = 1;
  129. b = &out->battery[0];
  130. b->present = 1;
  131. bst = p[0xC1];
  132. bam = (p[0xC0] >> 1) & 1;
  133. bmf = (p[0xC0] >> 4) & 0xF;
  134. basc = bam ? 1 : 10;
  135. bdc = rd16(p + 0xCA);
  136. bfc = rd16(p + 0xCC);
  137. bfi = rd16(p + 0xED);
  138. orrf = (p[0xF1] >> 6) & 1;
  139. if (orrf && bfi)
  140. bfc = bfi;
  141. brc = rd16(p + 0xC2);
  142. bpv = rd16(p + 0xC6);
  143. bac = rd16(p + 0xD2);
  144. cyc = p[0xCF];
  145. rate = (int)(short)bac;
  146. if (!bam)
  147. rate = rate * (int)bpv / 1000;
  148. b->status = bst;
  149. b->charging = (bst & 2) != 0;
  150. b->discharging = (bst & 1) != 0;
  151. b->remaining = brc * basc;
  152. b->full = bfc * basc;
  153. b->design = bdc * basc;
  154. b->voltage_mv = bpv;
  155. b->rate = rate;
  156. b->unit_known = 1;
  157. b->unit_mwh = !bam;
  158. b->percent = b->full ?
  159. (int)(((unsigned long)b->remaining * 100 + b->full / 2) /
  160. b->full) : 0;
  161. b->percent_valid = b->full != 0;
  162. b->cycle_count = cyc;
  163. copy_str(b->manufacturer, sizeof(b->manufacturer),
  164. battery_vendor(bmf));
  165. copy_str(b->model, sizeof(b->model), "PABAS0241231");
  166. copy_str(b->serial, sizeof(b->serial), "41167");
  167. if (detail) {
  168. copy_str(out->vendor, sizeof(out->vendor), "LENOVO");
  169. copy_str(out->product, sizeof(out->product),
  170. "Yoga 720-13IKB");
  171. }
  172. }