tpec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* ThinkPad embedded-controller sensors and power sources (see tpec.h).
  2. *
  3. * The X201 (i5 M 520, Arrandale) has no IT87xx Super I/O like the
  4. * AT3IONT-I. Its board temperatures, fan tachometer, battery and AC state
  5. * live behind the ACPI embedded controller, which Linux's thinkpad_acpi
  6. * reads through the same ports: command/status 0x66, data 0x62. The EC
  7. * protocol is simple enough to drive directly:
  8. *
  9. * write 0x80 (read) to 0x66, then the register index to 0x62,
  10. * wait for OBF (0x01) and read the byte back from 0x62.
  11. *
  12. * The SMBIOS type 1 entry gates the probe to Lenovo/IBM machines, so the
  13. * EC ports are never touched on the ASUS board, QEMU or other hardware.
  14. *
  15. * Two details matter on real hardware:
  16. * - There is no SCI handler, so hotkey events queue up in the output buffer.
  17. * A pending byte is drained before every command; otherwise the EC would
  18. * never replace it with the register we asked for.
  19. * - Firmware keeps running in legacy/SMM mode (we have no ACPI interpreter),
  20. * so an SMI can access the EC between our port accesses. The status
  21. * handshake plus bounded polling keeps that to an occasional bad sample.
  22. * Temperature/fan reads are read-only; the battery reader writes only the
  23. * H8 information-page selector at 0x81 and restores it afterwards.
  24. *
  25. * EC register map (thinkpad_acpi/H8): 0x78..0x7F and 0xC0..0xC7 temperatures
  26. * (0x80 = sensor not present, 0x00 = register not implemented), 0x84/0x85
  27. * fan tachometer LSB/MSB and 0x2F fan control (bit 7 automatic, bit 6
  28. * disengaged/full speed, bits 2:0 level). EC 0xEF is the functional
  29. * revision: revision 3 and later only implement the 0x78..0x7F sensors.
  30. *
  31. * Battery/AC: 0x38/0x39 are the battery 0/1 status bytes (bit 7 present,
  32. * bit 6 discharging, bit 5 charging) and 0x46 bit 4 is the AC-adapter
  33. * state. Battery data is a 16-byte window at 0xA0..0xAF selected by 0x81:
  34. * the high nibble is the battery (0x00/0x10), the low nibble the page
  35. * (0 dynamic, 1 mode, 2 static design data, 5 manufacturer, 6 model).
  36. */
  37. #include "tpec.h"
  38. #include "paging.h"
  39. #include "pci.h" /* port_inb/port_outb */
  40. #include "serial.h"
  41. #define EC_DATA 0x62
  42. #define EC_STS 0x66
  43. #define EC_OBF 0x01 /* output buffer full */
  44. #define EC_IBF 0x02 /* input buffer full */
  45. #define EC_READ 0x80
  46. #define EC_WRITE 0x81
  47. #define TP_FAN_LEVEL 0x2F
  48. #define TP_FAN_LSB 0x84
  49. #define TP_FAN_MSB 0x85
  50. #define TP_TEMP_LO 0x78 /* temp_c[0..7] */
  51. #define TP_TEMP_HI 0xC0 /* temp_c[8..15] */
  52. #define TP_EC_REV 0xEF /* EC functional revision */
  53. #define TP_BAT0_STATUS 0x38 /* bit 7 present, 6 discharging, 5 charging */
  54. #define TP_BAT1_STATUS 0x39
  55. #define TP_AC_STATUS 0x46 /* bit 4: AC adapter online */
  56. #define TP_BAT_PAGE 0x81 /* information-page selector */
  57. #define TP_BAT_DATA 0xA0 /* 16-byte selected page */
  58. #define TP_BAT_PAGE_DYNAMIC 0
  59. #define TP_BAT_PAGE_MODE 1
  60. #define TP_BAT_PAGE_STATIC 2
  61. #define TP_BAT_PAGE_MFR 5
  62. #define TP_BAT_PAGE_MODEL 6
  63. #define TP_BAT_REMAINING 0x00
  64. #define TP_BAT_FULL 0x02
  65. #define TP_BAT_RSOC 0x06
  66. #define TP_BAT_RATE 0x08
  67. #define TP_BAT_VOLTAGE 0x0A
  68. /* Enough for a slow EC command (tens of milliseconds of port polls) but
  69. * short enough that a dead controller cannot stall the boot or a syscall. */
  70. #define EC_POLL_MAX 20000
  71. static int tpec_state = -1; /* -1 unprobed, 0 absent, 1 present */
  72. static unsigned int tpec_ec_rev;
  73. static char tpec_vendor[16];
  74. static char tpec_product[32];
  75. static char tpec_version[32];
  76. static unsigned int rd32(const unsigned char *p)
  77. {
  78. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  79. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  80. }
  81. static void copy_trim(char *dst, unsigned int size, const unsigned char *src)
  82. {
  83. unsigned int i = 0;
  84. while (i + 1 < size && src && src[i]) {
  85. dst[i] = (char)src[i];
  86. i++;
  87. }
  88. while (i && dst[i - 1] == ' ')
  89. i--;
  90. dst[i] = '\0';
  91. }
  92. /* Map every 4 MB page touched by [addr, addr+len); SMBIOS tables can sit
  93. * above the 128 MB identity map on larger machines (same approach as
  94. * acpi.c). */
  95. static void map_range(unsigned int addr, unsigned int len)
  96. {
  97. unsigned int p = addr & 0xFFC00000u;
  98. unsigned int last = (addr + len - 1u) & 0xFFC00000u;
  99. for (;;) {
  100. paging_map_ram(p, 0x400000u);
  101. if (p >= last || p > 0xFFC00000u - 0x400000u)
  102. break;
  103. p += 0x400000u;
  104. }
  105. }
  106. /* The n-th (1-based) string that follows a formatted SMBIOS structure. */
  107. static const unsigned char *smbios_string(const unsigned char *p,
  108. const unsigned char *end,
  109. unsigned int index)
  110. {
  111. unsigned int n = 1;
  112. if (!index)
  113. return 0;
  114. while (p < end && *p) {
  115. const unsigned char *s = p;
  116. if (n == index)
  117. return s;
  118. while (p < end && *p)
  119. p++;
  120. p++;
  121. n++;
  122. }
  123. return 0;
  124. }
  125. static int smbios_type1(char vendor[16], char product[32], char version[32])
  126. {
  127. const unsigned char *ep = 0;
  128. const unsigned char *table, *end, *p;
  129. unsigned int addr, table_addr, table_len, i;
  130. for (addr = 0xF0000; addr < 0x100000; addr += 16) {
  131. const unsigned char *q = (const unsigned char *)addr;
  132. unsigned char sum = 0;
  133. unsigned int len;
  134. if (q[0] != '_' || q[1] != 'S' || q[2] != 'M' || q[3] != '_')
  135. continue;
  136. len = q[5];
  137. if (len < 0x1F || len > 0x40)
  138. continue;
  139. for (i = 0; i < len; i++)
  140. sum = (unsigned char)(sum + q[i]);
  141. if (sum)
  142. continue;
  143. if (q[0x10] != '_' || q[0x11] != 'D' || q[0x12] != 'M' ||
  144. q[0x13] != 'I' || q[0x14] != '_')
  145. continue;
  146. ep = q;
  147. break;
  148. }
  149. if (!ep)
  150. return -1;
  151. table_len = (unsigned int)ep[0x16] | ((unsigned int)ep[0x17] << 8);
  152. table_addr = rd32(ep + 0x18);
  153. if (table_addr < 0x1000 || table_addr >= 0xFF000000u ||
  154. table_len < 4 || table_len > 0x10000u ||
  155. table_addr + table_len < table_addr)
  156. return -1;
  157. map_range(table_addr, table_len);
  158. table = (const unsigned char *)table_addr;
  159. end = table + table_len;
  160. for (p = table; p + 4 <= end; ) {
  161. unsigned char type = p[0];
  162. unsigned int len = p[1];
  163. const unsigned char *strs, *next;
  164. if (len < 4)
  165. break;
  166. if (type == 127) /* end-of-table */
  167. break;
  168. strs = p + len;
  169. if (strs > end)
  170. break;
  171. if (type == 1) {
  172. if (len >= 8) {
  173. copy_trim(vendor, 16,
  174. smbios_string(strs, end, p[4]));
  175. copy_trim(product, 32,
  176. smbios_string(strs, end, p[5]));
  177. copy_trim(version, 32,
  178. smbios_string(strs, end, p[6]));
  179. return 0;
  180. }
  181. break;
  182. }
  183. next = strs;
  184. while (next + 1 < end && !(next[0] == 0 && next[1] == 0))
  185. next++;
  186. next += 2;
  187. if (next > end)
  188. break;
  189. p = next;
  190. }
  191. return -1;
  192. }
  193. static int ec_wait(unsigned char mask, int set)
  194. {
  195. unsigned int i;
  196. for (i = 0; i < EC_POLL_MAX; i++) {
  197. unsigned char st = port_inb(EC_STS);
  198. if (st == 0xFF) /* no controller on these ports */
  199. return -1;
  200. if (set ? (st & mask) != 0 : (st & mask) == 0)
  201. return 0;
  202. }
  203. return -1;
  204. }
  205. /* Keep a stale output byte (a hotkey event) from shadowing the register we
  206. * are about to read. */
  207. static int ec_drain(void)
  208. {
  209. unsigned int i;
  210. for (i = 0; i < 64; i++) {
  211. unsigned char st = port_inb(EC_STS);
  212. if (st == 0xFF)
  213. return -1;
  214. if (!(st & EC_OBF))
  215. return 0;
  216. (void)port_inb(EC_DATA);
  217. }
  218. return -1;
  219. }
  220. static int ec_read(unsigned char reg, unsigned char *out)
  221. {
  222. if (ec_drain())
  223. return -1;
  224. if (ec_wait(EC_IBF, 0))
  225. return -1;
  226. port_outb(EC_STS, EC_READ);
  227. if (ec_wait(EC_IBF, 0))
  228. return -1;
  229. port_outb(EC_DATA, reg);
  230. if (ec_wait(EC_OBF, 1))
  231. return -1;
  232. *out = port_inb(EC_DATA);
  233. return 0;
  234. }
  235. /* ACPI EC "write EC RAM": command 0x81, then the register index and value. */
  236. static int ec_write(unsigned char reg, unsigned char val)
  237. {
  238. if (ec_drain())
  239. return -1;
  240. if (ec_wait(EC_IBF, 0))
  241. return -1;
  242. port_outb(EC_STS, EC_WRITE);
  243. if (ec_wait(EC_IBF, 0))
  244. return -1;
  245. port_outb(EC_DATA, reg);
  246. if (ec_wait(EC_IBF, 0))
  247. return -1;
  248. port_outb(EC_DATA, val);
  249. if (ec_wait(EC_IBF, 0))
  250. return -1;
  251. return 0;
  252. }
  253. static int ec_read16(unsigned char reg, unsigned int *out)
  254. {
  255. unsigned char lo, hi;
  256. if (ec_read(reg, &lo) != 0 || ec_read((unsigned char)(reg + 1), &hi) != 0)
  257. return -1;
  258. *out = (unsigned int)lo | ((unsigned int)hi << 8);
  259. return 0;
  260. }
  261. /* Select an H8 battery information page. Only writes 0x81 when the page
  262. * already differs; *dirty remembers that the original page must be restored. */
  263. static int ec_battery_page(unsigned int battery, unsigned int page, int *dirty)
  264. {
  265. unsigned char cur;
  266. unsigned char want = (unsigned char)((battery << 4) | page);
  267. if (ec_read(TP_BAT_PAGE, &cur) == 0 && cur == want)
  268. return 0;
  269. if (ec_write(TP_BAT_PAGE, want) != 0)
  270. return -1;
  271. *dirty = 1;
  272. return 0;
  273. }
  274. static void ec_read_string(char *dst, unsigned int size, unsigned char reg)
  275. {
  276. unsigned int i = 0;
  277. while (i + 1 < size) {
  278. unsigned char b;
  279. if (ec_read((unsigned char)(reg + i), &b) != 0)
  280. break;
  281. if (b == 0x00 || b == 0xFF)
  282. break;
  283. dst[i++] = (char)b;
  284. }
  285. while (i && dst[i - 1] == ' ')
  286. i--;
  287. dst[i] = '\0';
  288. }
  289. static int str_eq(const char *a, const char *b)
  290. {
  291. while (*a && *a == *b) {
  292. a++;
  293. b++;
  294. }
  295. return *a == *b;
  296. }
  297. static void log_info(void)
  298. {
  299. serial_puts(SERIAL_COM1, "tpec: ");
  300. if (tpec_vendor[0]) {
  301. serial_puts(SERIAL_COM1, tpec_vendor);
  302. serial_puts(SERIAL_COM1, " ");
  303. }
  304. if (tpec_product[0]) {
  305. serial_puts(SERIAL_COM1, tpec_product);
  306. serial_puts(SERIAL_COM1, " ");
  307. }
  308. if (tpec_version[0])
  309. serial_puts(SERIAL_COM1, tpec_version);
  310. }
  311. void tpec_init(void)
  312. {
  313. char vendor[16], product[32], version[32];
  314. unsigned char probe, rev;
  315. tpec_state = 0;
  316. tpec_vendor[0] = '\0';
  317. tpec_product[0] = '\0';
  318. tpec_version[0] = '\0';
  319. if (smbios_type1(vendor, product, version) != 0) {
  320. serial_puts(SERIAL_COM1, "tpec: no SMBIOS type 1, disabled\n");
  321. return;
  322. }
  323. copy_trim(tpec_vendor, sizeof(tpec_vendor), (const unsigned char *)vendor);
  324. copy_trim(tpec_product, sizeof(tpec_product),
  325. (const unsigned char *)product);
  326. copy_trim(tpec_version, sizeof(tpec_version),
  327. (const unsigned char *)version);
  328. if (!str_eq(vendor, "LENOVO") && !str_eq(vendor, "IBM")) {
  329. log_info();
  330. serial_puts(SERIAL_COM1, ": not a ThinkPad vendor, no EC probe\n");
  331. return;
  332. }
  333. if (ec_read(TP_FAN_LEVEL, &probe) != 0) {
  334. log_info();
  335. serial_puts(SERIAL_COM1, ": EC not responding\n");
  336. return;
  337. }
  338. if (ec_read(TP_EC_REV, &rev) == 0)
  339. tpec_ec_rev = rev;
  340. tpec_state = 1;
  341. log_info();
  342. serial_puts(SERIAL_COM1, ": EC ok, fw rev ");
  343. serial_putdec(SERIAL_COM1, tpec_ec_rev);
  344. serial_puts(SERIAL_COM1, ", fan level ");
  345. serial_puthex(SERIAL_COM1, probe);
  346. serial_puts(SERIAL_COM1, "\n");
  347. }
  348. void tpec_read(struct tpec_info *out)
  349. {
  350. unsigned char b;
  351. unsigned int i;
  352. out->present = 0;
  353. out->vendor[0] = '\0';
  354. out->product[0] = '\0';
  355. out->version[0] = '\0';
  356. for (i = 0; i < TPEC_TEMP_COUNT; i++)
  357. out->temp_c[i] = -128;
  358. out->fan_rpm = 0;
  359. out->fan_level = 0;
  360. if (tpec_state < 0)
  361. tpec_init();
  362. if (!tpec_state)
  363. return;
  364. out->present = 1;
  365. for (i = 0; i < sizeof(out->vendor) - 1 && tpec_vendor[i]; i++)
  366. out->vendor[i] = tpec_vendor[i];
  367. out->vendor[i] = '\0';
  368. for (i = 0; i < sizeof(out->product) - 1 && tpec_product[i]; i++)
  369. out->product[i] = tpec_product[i];
  370. out->product[i] = '\0';
  371. for (i = 0; i < sizeof(out->version) - 1 && tpec_version[i]; i++)
  372. out->version[i] = tpec_version[i];
  373. out->version[i] = '\0';
  374. for (i = 0; i < 8; i++) {
  375. if (ec_read((unsigned char)(TP_TEMP_LO + i), &b) != 0)
  376. continue;
  377. /* 0x80 = sensor present but unavailable, 0x00 = register not
  378. * implemented (thinkpad_acpi treats a block of zeros as an EC
  379. * that does not expose the register map at all). */
  380. if (b != 0x80 && b != 0x00)
  381. out->temp_c[i] = (signed char)b;
  382. }
  383. /* EC functional revision 3 and later only uses 0x78..0x7F; the 0xC0
  384. * block may hold unrelated values (e.g. a power supply ID) there. */
  385. if (tpec_ec_rev < 3) {
  386. for (i = 0; i < 8; i++) {
  387. if (ec_read((unsigned char)(TP_TEMP_HI + i), &b) != 0)
  388. continue;
  389. if (b != 0x80 && b != 0x00)
  390. out->temp_c[8 + i] = (signed char)b;
  391. }
  392. }
  393. {
  394. unsigned char lo = 0, hi = 0;
  395. if (ec_read(TP_FAN_LSB, &lo) == 0 &&
  396. ec_read(TP_FAN_MSB, &hi) == 0) {
  397. if (!(lo == 0xFF && hi == 0xFF))
  398. out->fan_rpm = ((unsigned int)hi << 8) | lo;
  399. }
  400. }
  401. if (ec_read(TP_FAN_LEVEL, &b) == 0)
  402. out->fan_level = b;
  403. }
  404. static void tpec_copy_ident(char *dst, unsigned int size, const char *src)
  405. {
  406. unsigned int i;
  407. for (i = 0; i + 1 < size && src[i]; i++)
  408. dst[i] = src[i];
  409. dst[i] = '\0';
  410. }
  411. static void tpec_read_battery(struct tpec_battery *b, unsigned int index,
  412. int detail, int *page_dirty)
  413. {
  414. unsigned char st;
  415. unsigned int v;
  416. unsigned int remaining = 0, full = 0, rsoc = 0xFFFF;
  417. int have_remaining = 0, have_full = 0, have_rsoc = 0;
  418. int have_voltage = 0;
  419. if (ec_read((unsigned char)(TP_BAT0_STATUS + index), &st) != 0) {
  420. b->error = 1;
  421. return;
  422. }
  423. b->status = st;
  424. b->present = (st & 0x80) ? 1 : 0;
  425. b->charging = (st & 0x20) ? 1 : 0;
  426. b->discharging = (st & 0x40) ? 1 : 0;
  427. if (!b->present)
  428. return;
  429. if (ec_battery_page(index, TP_BAT_PAGE_DYNAMIC, page_dirty) != 0) {
  430. b->error = 1;
  431. return;
  432. }
  433. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_REMAINING), &v) == 0) {
  434. remaining = v;
  435. have_remaining = (v != 0xFFFF);
  436. }
  437. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_FULL), &v) == 0) {
  438. full = v;
  439. have_full = (v != 0xFFFF);
  440. }
  441. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_RSOC), &v) == 0) {
  442. rsoc = v;
  443. have_rsoc = (v != 0xFFFF);
  444. }
  445. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_RATE), &v) == 0) {
  446. if (v != 0xFFFF)
  447. b->rate = (int)(short)v;
  448. } else {
  449. b->error = 1;
  450. }
  451. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_VOLTAGE), &v) == 0 &&
  452. v != 0xFFFF) {
  453. b->voltage_mv = v;
  454. have_voltage = 1;
  455. } else {
  456. b->error = 1;
  457. }
  458. b->remaining = have_remaining ? remaining : 0;
  459. b->full = have_full ? full : 0;
  460. if (!have_remaining || !have_full || !have_voltage)
  461. b->error = 1;
  462. /* Percentage is unit-independent (both capacities use the same unit).
  463. * Reject the 0xFFFF "unknown" marker and obviously inconsistent reads;
  464. * fall back to the SBS relative state of charge if that is sane. */
  465. if (have_full && have_remaining && full > 0 && full != 0xFFFF &&
  466. remaining != 0xFFFF && remaining <= full + full / 4) {
  467. b->percent = (int)((remaining * 100u + full / 2u) / full);
  468. if (b->percent > 100)
  469. b->percent = 100;
  470. b->percent_valid = 1;
  471. } else if (have_rsoc && rsoc <= 100) {
  472. b->percent = (int)rsoc;
  473. b->percent_valid = 1;
  474. }
  475. if (!detail)
  476. return;
  477. if (ec_battery_page(index, TP_BAT_PAGE_MODE, page_dirty) == 0) {
  478. if (ec_read16(TP_BAT_DATA, &v) == 0 && v != 0xFFFF) {
  479. b->unit_known = 1;
  480. b->unit_mwh = (int)((v >> 15) & 1);
  481. }
  482. } else {
  483. b->error = 1;
  484. }
  485. if (ec_battery_page(index, TP_BAT_PAGE_STATIC, page_dirty) == 0) {
  486. if (ec_read16(TP_BAT_DATA, &v) == 0 && v != 0xFFFF)
  487. b->design = v;
  488. } else {
  489. b->error = 1;
  490. }
  491. /* The vendor DSDT's GBIF/GBST report mWh/mW when the mode bit is set:
  492. * capacities are scaled by 10 (the 10.8 V pack) and the rate by the
  493. * measured voltage. Match that so powerinfo shows the same units as
  494. * the firmware's ACPI battery interface. The percentage above was
  495. * already computed from the raw values, so the scale does not matter. */
  496. if (b->unit_mwh) {
  497. b->remaining *= 10;
  498. b->full *= 10;
  499. b->design *= 10;
  500. if (b->voltage_mv)
  501. b->rate = (b->rate * (int)b->voltage_mv) / 1000;
  502. }
  503. if (ec_battery_page(index, TP_BAT_PAGE_MFR, page_dirty) == 0)
  504. ec_read_string(b->manufacturer, sizeof(b->manufacturer),
  505. TP_BAT_DATA);
  506. else
  507. b->error = 1;
  508. if (ec_battery_page(index, TP_BAT_PAGE_MODEL, page_dirty) == 0)
  509. ec_read_string(b->model, sizeof(b->model), TP_BAT_DATA);
  510. else
  511. b->error = 1;
  512. }
  513. void tpec_power_read(struct tpec_power *out, int detail)
  514. {
  515. unsigned int i;
  516. unsigned char saved_page = 0, ac;
  517. int have_saved = 0, page_dirty = 0;
  518. for (i = 0; i < sizeof(*out); i++)
  519. ((unsigned char *)out)[i] = 0;
  520. out->ac_online = -1;
  521. if (tpec_state < 0)
  522. tpec_init();
  523. if (!tpec_state)
  524. return;
  525. out->present = 1;
  526. out->ec_rev = tpec_ec_rev;
  527. tpec_copy_ident(out->vendor, sizeof(out->vendor), tpec_vendor);
  528. tpec_copy_ident(out->product, sizeof(out->product), tpec_product);
  529. tpec_copy_ident(out->version, sizeof(out->version), tpec_version);
  530. if (ec_read(TP_AC_STATUS, &ac) == 0)
  531. out->ac_online = (ac & 0x10) ? 1 : 0;
  532. if (ec_read(TP_BAT_PAGE, &saved_page) == 0)
  533. have_saved = 1;
  534. for (i = 0; i < TPEC_BATTERY_COUNT; i++) {
  535. tpec_read_battery(&out->battery[i], i, detail, &page_dirty);
  536. if (out->battery[i].present)
  537. out->battery_count++;
  538. }
  539. if (page_dirty)
  540. (void)ec_write(TP_BAT_PAGE, have_saved ? saved_page : 0);
  541. }