All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicolo' Piazzalunga <nicolopiazzalunga@gmail.com>
To: platform-driver-x86@vger.kernel.org
Cc: jwrdegoede@fedoraproject.org, smclt30p@gmail.com, linrunner@gmx.net
Subject: [PATCH 2/3] thinkpad_acpi: add support for inhibit_charge
Date: Wed, 17 Mar 2021 15:04:09 +0100	[thread overview]
Message-ID: <d2808930-5840-6ffb-3a59-d235cdb1fe16@gmail.com> (raw)

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@gmail.com>
Signed-off-by: Thomas Koch <linrunner@gmx.net>
Signed-off-by: Nicolo' Piazzalunga <nicolopiazzalunga@gmail.com>
---
 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

                 reply	other threads:[~2021-03-17 14:05 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d2808930-5840-6ffb-3a59-d235cdb1fe16@gmail.com \
    --to=nicolopiazzalunga@gmail.com \
    --cc=jwrdegoede@fedoraproject.org \
    --cc=linrunner@gmx.net \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=smclt30p@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.