/* IdeaPad/Yoga ACPI EC battery and AC (Lenovo Yoga 720-13IKB). * * The DSDT BAT1 device reads the battery fields from the PCH EC memory * window at 0xFF000800 (SystemMemory OperationRegion ECMM), not from the * 0x62/0x66 EmbeddedControl region. The field layout, straight from the * DSDT: * * 0xC0 BTY0:1 BAM0:1 BAL0:1 0:1 BMF0:4 (BMF0 6 = SIMPLO) * 0xC1 BST0 battery state (bit 0 discharging, bit 1 charging) * 0xC2 BRC0 remaining capacity (16-bit) * 0xC6 BPV0 present voltage, mV (16-bit) * 0xC8 BDV0 design voltage, mV (16-bit) * 0xCA BDC0 design capacity (16-bit) * 0xCC BFC0 last full capacity (16-bit) * 0xCF CYC0 cycle count * 0xD2 BAC0 present rate, signed 16-bit (mA when BAM0 == 0) * 0xED BFCB alternate last-full capacity (16-bit) * 0xF1 bit 6 ORRF (use BFCB when set) * 0xA3 bit 7 ADPT (AC adapter online) * * The DSDT scales capacities by BASC = 10 when BAM0 == 0 (the pack reports * in 10 mWh units) and converts BAC0 from mA to mW with BPV0. The _BST rate * is signed; we mirror that. */ #include "ideapad.h" #include "paging.h" #include "serial.h" #include "syscall.h" #define ECMM_BASE 0xFF000800u #define ECMM_SIZE 0x1000u static unsigned char *ecmm; static unsigned char *ecmm_map(void) { if (!ecmm) ecmm = (unsigned char *)ioremap(ECMM_BASE, ECMM_SIZE, IOREMAP_MMIO); return ecmm; } static unsigned int rd16(const unsigned char *p) { return (unsigned int)p[0] | ((unsigned int)p[1] << 8); } static void copy_str(char *dst, unsigned int size, const char *src) { unsigned int i = 0; while (i + 1 < size && src[i]) { dst[i] = src[i]; i++; } dst[i] = '\0'; } /* GUBI() in the DSDT maps BMF0 to the pack vendor. */ static const char *battery_vendor(unsigned int bmf) { switch (bmf) { case 1: return "SANYO"; case 2: return "SONY"; case 3: return "SDI"; case 4: return "PANASONIC"; case 5: return "SAMSUNG"; case 6: return "SIMPLO"; case 7: return "DYNAPACK"; default: return "LENOVO"; } } int ideapad_present(void) { unsigned char *p = ecmm_map(); unsigned int bdc, bfc, bpv; if (!p) return 0; bdc = rd16(p + 0xCA); bfc = rd16(p + 0xCC); bpv = rd16(p + 0xC6); if (bdc == 0 || bdc > 0x7FFF || bfc == 0 || bfc > 0x7FFF || bpv < 3000 || bpv > 20000) return 0; /* Only the low three status bits are defined, and BMF0 is a small * pack-vendor id; anything else means 0xFF000800 is not this EC. */ if (p[0xC1] > 0x07) return 0; if (((p[0xC0] >> 4) & 0xF) > 7) return 0; return 1; } void ideapad_init(void) { if (!ideapad_present()) return; serial_puts(SERIAL_COM1, "ideapad: EC battery/AC at 0xFF000800\n"); } void ideapad_power_read(struct power_info *out, int detail) { unsigned char *p; unsigned int bst, bam, bmf, basc; unsigned int brc, bfc, bfi, bdc, bpv, bac, cyc, orrf; unsigned int i; int rate; struct power_battery *b; for (i = 0; i < sizeof(*out); i++) ((unsigned char *)out)[i] = 0; out->ac_online = -1; p = ecmm_map(); if (!p || !ideapad_present()) return; out->present = 1; out->source = SYS_POWER_SRC_IDEAPAD; out->ac_online = (p[0xA3] >> 7) & 1; out->battery_count = 1; b = &out->battery[0]; b->present = 1; bst = p[0xC1]; bam = (p[0xC0] >> 1) & 1; bmf = (p[0xC0] >> 4) & 0xF; basc = bam ? 1 : 10; bdc = rd16(p + 0xCA); bfc = rd16(p + 0xCC); bfi = rd16(p + 0xED); orrf = (p[0xF1] >> 6) & 1; if (orrf && bfi) bfc = bfi; brc = rd16(p + 0xC2); bpv = rd16(p + 0xC6); bac = rd16(p + 0xD2); cyc = p[0xCF]; rate = (int)(short)bac; if (!bam) rate = rate * (int)bpv / 1000; b->status = bst; b->charging = (bst & 2) != 0; b->discharging = (bst & 1) != 0; b->remaining = brc * basc; b->full = bfc * basc; b->design = bdc * basc; b->voltage_mv = bpv; b->rate = rate; b->unit_known = 1; b->unit_mwh = !bam; b->percent = b->full ? (int)(((unsigned long)b->remaining * 100 + b->full / 2) / b->full) : 0; b->percent_valid = b->full != 0; b->cycle_count = cyc; copy_str(b->manufacturer, sizeof(b->manufacturer), battery_vendor(bmf)); copy_str(b->model, sizeof(b->model), "PABAS0241231"); copy_str(b->serial, sizeof(b->serial), "41167"); if (detail) { copy_str(out->vendor, sizeof(out->vendor), "LENOVO"); copy_str(out->product, sizeof(out->product), "Yoga 720-13IKB"); } }