Lenovo ThinkPad systems support to hold battery charging via a manual override called Inhibit Charge. This patch implements that feature and exposes it via the generic ACPI battery driver in the generic location: /sys/class/power_supply/BATx/inhibit_charge Signed-off-by: Ognjen Galic <smclt30p@xxxxxxxxx> Signed-off-by: Thomas Koch <linrunner@xxxxxxx> Signed-off-by: Nicolo' Piazzalunga <nicolopiazzalunga@xxxxxxxxx> --- drivers/platform/x86/thinkpad_acpi.c | 68 ++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c index 6c7dca3a10d2..a13feb1d7313 100644 --- a/drivers/platform/x86/thinkpad_acpi.c +++ b/drivers/platform/x86/thinkpad_acpi.c @@ -9319,6 +9319,8 @@ static struct ibm_struct mute_led_driver_data = { #define SET_STOP "BCSS" #define GET_DISCHARGE "BDSG" #define SET_DISCHARGE "BDSS" +#define GET_INHIBIT "PSSG" +#define SET_INHIBIT "BICS" enum { BAT_ANY = 0, @@ -9335,7 +9337,8 @@ enum { /* This is used in the get/set helpers */ THRESHOLD_START, THRESHOLD_STOP, - FORCE_DISCHARGE + FORCE_DISCHARGE, + INHIBIT_CHARGE }; struct tpacpi_battery_data { @@ -9344,6 +9347,7 @@ struct tpacpi_battery_data { int charge_stop; int stop_support; int discharge_support; + int inhibit_support; }; struct tpacpi_battery_driver_data { @@ -9407,6 +9411,13 @@ static int tpacpi_battery_get(int what, int battery, int *ret) /* The force discharge status is in bit 0 */ *ret = *ret & 0x01; return 0; + case INHIBIT_CHARGE: + /* This is actually reading peak shift state, like tpacpi-bat does */ + if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery)) + return -ENODEV; + /* The inhibit charge status is in bit 0 */ + *ret = *ret & 0x01; + return 0; default: pr_crit("wrong parameter: %d", what); return -EINVAL; @@ -9445,6 +9456,22 @@ static int tpacpi_battery_set(int what, int battery, int value) return -ENODEV; } return 0; + case INHIBIT_CHARGE: + /* When setting inhibit charge, we set a default value of + * always breaking on AC detach and the effective time is set to + * be permanent. + * The battery ID is in bits 4-5, 2 bits, + * the effective time is in bits 8-23, 2 bytes. + * A time of FFFF indicates forever. + */ + param = value; + param |= battery << 4; + param |= 0xFFFF << 8; + if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param)) { + pr_err("failed to set inhibit charge on %d", battery); + return -ENODEV; + } + return 0; default: pr_crit("wrong parameter: %d", what); return -EINVAL; @@ -9465,6 +9492,8 @@ static int tpacpi_battery_probe(int battery) * 4) Check for support * 5) Get the current force discharge status * 6) Check for support + * 7) Get the current inhibit charge status + * 8) Check for support */ if (acpi_has_method(hkey_handle, GET_START)) { if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) { @@ -9505,12 +9534,17 @@ static int tpacpi_battery_probe(int battery) if (!ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) /* Support is marked in bit 8 */ battery_info.batteries[battery].discharge_support = ret & BIT(8); + if (acpi_has_method(hkey_handle, GET_INHIBIT)) + if (!ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) + /* Support is marked in bit 5 */ + battery_info.batteries[battery].inhibit_support = ret & BIT(5); - pr_info("battery %d registered (start %d, stop %d, force: %d)", + pr_info("battery %d registered (start %d, stop %d, force: %d, inhibit: %d)", battery, battery_info.batteries[battery].charge_start, battery_info.batteries[battery].charge_stop, - battery_info.batteries[battery].discharge_support); + battery_info.batteries[battery].discharge_support, + battery_info.batteries[battery].inhibit_support); return 0; } @@ -9576,7 +9610,6 @@ static ssize_t tpacpi_battery_store(int what, return -ENODEV; battery_info.batteries[battery].charge_start = value; return count; - case THRESHOLD_STOP: if (!battery_info.batteries[battery].stop_support) return -ENODEV; @@ -9605,6 +9638,15 @@ static ssize_t tpacpi_battery_store(int what, if (tpacpi_battery_set(FORCE_DISCHARGE, battery, value)) return -ENODEV; return count; + case INHIBIT_CHARGE: + if (!battery_info.batteries[battery].inhibit_support) + return -ENODEV; + /* The only valid values are 1 and 0 */ + if (value != 0 && value != 1) + return -EINVAL; + if (tpacpi_battery_set(INHIBIT_CHARGE, battery, value)) + return -ENODEV; + return count; default: pr_crit("Wrong parameter: %d", what); return -EINVAL; @@ -9660,6 +9702,13 @@ static ssize_t force_discharge_show(struct device *device, return tpacpi_battery_show(FORCE_DISCHARGE, device, buf); } +static ssize_t inhibit_charge_show(struct device *device, + struct device_attribute *attr, + char *buf) +{ + return tpacpi_battery_show(INHIBIT_CHARGE, device, buf); +} + static ssize_t charge_control_start_threshold_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -9681,9 +9730,17 @@ static ssize_t force_discharge_store(struct device *dev, return tpacpi_battery_store(FORCE_DISCHARGE, dev, buf, count); } +static ssize_t inhibit_charge_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + return tpacpi_battery_store(INHIBIT_CHARGE, dev, buf, count); +} + static DEVICE_ATTR_RW(charge_control_start_threshold); static DEVICE_ATTR_RW(charge_control_end_threshold); static DEVICE_ATTR_RW(force_discharge); +static DEVICE_ATTR_RW(inhibit_charge); static struct device_attribute dev_attr_charge_start_threshold = __ATTR( charge_start_threshold, 0644, @@ -9702,7 +9759,8 @@ static struct attribute *tpacpi_battery_attrs[] = { &dev_attr_charge_start_threshold.attr, &dev_attr_charge_stop_threshold.attr, &dev_attr_force_discharge.attr, - NULL, + &dev_attr_inhibit_charge.attr, + NULL }; ATTRIBUTE_GROUPS(tpacpi_battery); -- 2.25.1