From: Szilard Fabian <szfabian@bluemarch.art>
To: linux-kernel@vger.kernel.org,
platform-driver-x86@vger.kernel.org, jwoithe@just42.net,
hdegoede@redhat.com, ilpo.jarvinen@linux.intel.com
Cc: Szilard Fabian <szfabian@bluemarch.art>
Subject: [RFC PATCH] platform/x86/fujitsu-laptop: Add battery charge control support
Date: Mon, 29 Jan 2024 16:36:21 +0000 [thread overview]
Message-ID: <20240129163502.161409-2-szfabian@bluemarch.art> (raw)
This patch adds battery charge control support on Fujitsu notebooks
via the S006 method of the FUJ02E3 ACPI device. With this method it's
possible to set charge_control_end_threshold between 50 and 100%.
Tested on Lifebook E5411 and Lifebook U728. Sadly I can't test this
patch on a dual battery one, but I didn't find any clue about
independent battery charge control on dual battery Fujitsu notebooks
either. And by that I mean checking the DSDT table of various Lifebook
notebooks and reverse engineering FUJ02E3.dll.
---
drivers/platform/x86/fujitsu-laptop.c | 95 +++++++++++++++++++++++++++
1 file changed, 95 insertions(+)
diff --git a/drivers/platform/x86/fujitsu-laptop.c b/drivers/platform/x86/fujitsu-laptop.c
index 085e044e888e..bf3df74e4d63 100644
--- a/drivers/platform/x86/fujitsu-laptop.c
+++ b/drivers/platform/x86/fujitsu-laptop.c
@@ -49,6 +49,8 @@
#include <linux/kfifo.h>
#include <linux/leds.h>
#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <acpi/battery.h>
#include <acpi/video.h>
#define FUJITSU_DRIVER_VERSION "0.6.0"
@@ -97,6 +99,10 @@
#define BACKLIGHT_OFF (BIT(0) | BIT(1))
#define BACKLIGHT_ON 0
+/* FUNC interface - battery control interface */
+#define FUNC_S006_METHOD 0x1006
+#define CHARGE_CONTROL_RW 0x21
+
/* Scancodes read from the GIRB register */
#define KEY1_CODE 0x410
#define KEY2_CODE 0x411
@@ -164,6 +170,91 @@ static int call_fext_func(struct acpi_device *device,
return value;
}
+/* Battery charge control code */
+
+static ssize_t charge_control_end_threshold_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int value, ret;
+
+ ret = kstrtouint(buf, 10, &value);
+ if (ret)
+ return ret;
+
+ if (value < 50 || value > 100)
+ return -EINVAL;
+
+ int cc_end_value, s006_cc_return;
+
+ cc_end_value = value * 0x100 + 0x20;
+ s006_cc_return = call_fext_func(fext, FUNC_S006_METHOD,
+ CHARGE_CONTROL_RW, cc_end_value, 0x0);
+
+ /*
+ * The S006 0x21 method returns 0x00 in case the provided value
+ * is invalid.
+ */
+ if (s006_cc_return == 0x00)
+ return -EINVAL;
+
+ return count;
+}
+
+static ssize_t charge_control_end_threshold_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ int status;
+ status = call_fext_func(fext, FUNC_S006_METHOD,
+ CHARGE_CONTROL_RW, 0x21, 0x0);
+
+ return sprintf(buf, "%d\n", status);
+}
+
+static DEVICE_ATTR_RW(charge_control_end_threshold);
+
+/* ACPI battery hook */
+
+static int fujitsu_battery_add(struct power_supply *battery,
+ struct acpi_battery_hook *hook)
+{
+ /* Check if there is an existing FUJ02E3 ACPI device. */
+ if (fext == NULL)
+ return -ENODEV;
+
+ /*
+ * Check if the S006 0x21 method exists by trying to get the current
+ * battery charge limit.
+ */
+ int s006_cc_return;
+ s006_cc_return = call_fext_func(fext, FUNC_S006_METHOD,
+ CHARGE_CONTROL_RW, 0x21, 0x0);
+ if (s006_cc_return == UNSUPPORTED_CMD)
+ return -ENODEV;
+
+ if (device_create_file(&battery->dev,
+ &dev_attr_charge_control_end_threshold))
+ return -ENODEV;
+
+ return 0;
+}
+
+static int fujitsu_battery_remove(struct power_supply *battery,
+ struct acpi_battery_hook *hook)
+{
+ device_remove_file(&battery->dev,
+ &dev_attr_charge_control_end_threshold);
+
+ return 0;
+}
+
+static struct acpi_battery_hook battery_hook = {
+ .add_battery = fujitsu_battery_add,
+ .remove_battery = fujitsu_battery_remove,
+ .name = "Fujitsu Battery Extension",
+};
+
/* Hardware access for LCD brightness control */
static int set_lcd_level(struct acpi_device *device, int level)
@@ -839,6 +930,8 @@ static int acpi_fujitsu_laptop_add(struct acpi_device *device)
if (ret)
goto err_free_fifo;
+ battery_hook_register(&battery_hook);
+
return 0;
err_free_fifo:
@@ -851,6 +944,8 @@ static void acpi_fujitsu_laptop_remove(struct acpi_device *device)
{
struct fujitsu_laptop *priv = acpi_driver_data(device);
+ battery_hook_unregister(&battery_hook);
+
fujitsu_laptop_platform_remove(device);
kfifo_free(&priv->fifo);
--
2.43.0
next reply other threads:[~2024-01-29 16:36 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-29 16:36 Szilard Fabian [this message]
2024-01-29 18:00 ` [RFC PATCH v2] platform/x86/fujitsu-laptop: Add battery charge control support Szilard Fabian
2024-01-30 2:02 ` Armin Wolf
2024-02-03 0:17 ` Szilard Fabian
2024-02-05 17:07 ` Armin Wolf
2024-02-05 23:37 ` Jonathan Woithe
2024-02-07 2:32 ` [RFC PATCH v3] " Szilard Fabian
2024-02-07 8:57 ` Hans de Goede
2024-02-08 1:56 ` Szilard Fabian
2024-02-08 9:11 ` Hans de Goede
2024-02-15 11:34 ` Ilpo Järvinen
2024-02-15 20:12 ` Szilard Fabian
2024-02-16 9:27 ` Ilpo Järvinen
2024-02-15 20:31 ` [PATCH v4] " Szilard Fabian
2024-02-18 4:47 ` Jonathan Woithe
2024-02-19 12:06 ` Ilpo Järvinen
2024-02-27 10:18 ` Ilpo Järvinen
2024-02-27 12:04 ` Szilard Fabian
2024-02-27 12:05 ` [PATCH v5] " Szilard Fabian
2024-02-27 12:53 ` Ilpo Järvinen
2024-02-27 21:47 ` Jonathan Woithe
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=20240129163502.161409-2-szfabian@bluemarch.art \
--to=szfabian@bluemarch.art \
--cc=hdegoede@redhat.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=jwoithe@just42.net \
--cc=linux-kernel@vger.kernel.org \
--cc=platform-driver-x86@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).