From: Antheas Kapenekakis <lkml@antheas.dev>
To: linux-hwmon@vger.kernel.org
Cc: linux-doc@vger.kernel.org, linux-pm@vger.kernel.org,
platform-driver-x86@vger.kernel.org,
Guenter Roeck <linux@roeck-us.net>,
Jean Delvare <jdelvare@suse.com>,
Jonathan Corbet <corbet@lwn.net>,
Joaquin Ignacio Aramendia <samsagax@gmail.com>,
Derek J Clark <derekjohn.clark@gmail.com>,
Kevin Greenberg <kdgreenberg234@protonmail.com>,
Joshua Tam <csinaction@pm.me>,
Parth Menon <parthasarathymenon@gmail.com>,
Eileen <eileen@one-netbook.com>,
Antheas Kapenekakis <lkml@antheas.dev>
Subject: [PATCH v2 04/12] hwmon: (oxp-sensors) Add charge threshold and bypass to OneXPlayer
Date: Sat, 22 Feb 2025 17:18:15 +0100 [thread overview]
Message-ID: <20250222161824.172511-5-lkml@antheas.dev> (raw)
In-Reply-To: <20250222161824.172511-1-lkml@antheas.dev>
With the X1 (AMD), OneXPlayer added a charge limit and charge bypass to
their devices. Charge limit allows for choosing an arbitrary battery
charge setpoint in percentages. Charge bypass allows to instruct the
device to stop charging either when its on or always.
This feature was then extended for the F1Pro as well. OneXPlayer also
released BIOS updates for the X1 Mini, X1 (Intel), and F1 devices that
add support for this feature. Therefore, enable it for all F1 and
X1 devices.
Add both of these under the standard sysfs battery endpoints for them,
by looking for the battery. OneXPlayer devices have a single battery.
Signed-off-by: Antheas Kapenekakis <lkml@antheas.dev>
---
drivers/hwmon/oxp-sensors.c | 206 ++++++++++++++++++++++++++++++++++++
1 file changed, 206 insertions(+)
diff --git a/drivers/hwmon/oxp-sensors.c b/drivers/hwmon/oxp-sensors.c
index f7a64fbc8f33..f8ffc78deedd 100644
--- a/drivers/hwmon/oxp-sensors.c
+++ b/drivers/hwmon/oxp-sensors.c
@@ -26,6 +26,7 @@
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/processor.h>
+#include <acpi/battery.h>
/* Handle ACPI lock mechanism */
static u32 oxp_mutex;
@@ -89,6 +90,35 @@ static enum oxp_board board;
#define OXP_TURBO_RETURN_VAL 0x00 /* Common return val */
+#define OXP_X1_CHARGE_LIMIT_REG 0xA3 /* X1 charge limit (%) */
+#define OXP_X1_CHARGE_BYPASS_REG 0xA4 /* X1 bypass charging */
+
+#define OXP_X1_CHARGE_BYPASS_MASK_S0 0x01
+/*
+ * Cannot control S3, S5 individually.
+ * X1 Mask is 0x0A, OneXFly F1Pro is just 0x02
+ * but the extra bit on the X1 does nothing.
+ */
+#define OXP_X1_CHARGE_BYPASS_MASK_S3S5 0x02
+
+enum charge_type_value_index {
+ CT_OFF,
+ CT_S0,
+ CT_S5,
+};
+
+static u8 charge_type_values_x1[] = {
+ [CT_OFF] = 0x00,
+ [CT_S0] = OXP_X1_CHARGE_BYPASS_MASK_S0,
+ [CT_S5] = OXP_X1_CHARGE_BYPASS_MASK_S0 | OXP_X1_CHARGE_BYPASS_MASK_S3S5,
+};
+
+static const char * const charge_type_strings[] = {
+ [CT_OFF] = "Standard",
+ [CT_S0] = "BypassS0",
+ [CT_S5] = "Bypass",
+};
+
static const struct dmi_system_id dmi_table[] = {
{
.matches = {
@@ -436,6 +466,177 @@ static ssize_t tt_toggle_show(struct device *dev,
static DEVICE_ATTR_RW(tt_toggle);
+/* Callbacks for turbo toggle attribute */
+static bool charge_control_supported(void)
+{
+ switch (board) {
+ case oxp_x1:
+ case oxp_fly:
+ return 1;
+ default:
+ break;
+ }
+ return 0;
+}
+
+static ssize_t charge_type_store(struct device *dev,
+ struct device_attribute *attr, const char *buf,
+ size_t count)
+{
+ u64 val, reg;
+ int ret;
+
+ ret = __sysfs_match_string(charge_type_strings,
+ ARRAY_SIZE(charge_type_strings), buf);
+ if (ret < 0)
+ return ret;
+
+ switch (board) {
+ case oxp_x1:
+ case oxp_fly:
+ val = charge_type_values_x1[ret];
+ reg = OXP_X1_CHARGE_BYPASS_REG;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = write_to_ec(reg, val);
+ if (ret < 0)
+ return ret;
+
+ return count;
+}
+
+static ssize_t charge_type_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ int ret;
+ u8 reg;
+ long val;
+ const u8 *vals;
+ char *str;
+
+ switch (board) {
+ case oxp_x1:
+ case oxp_fly:
+ vals = charge_type_values_x1;
+ reg = OXP_X1_CHARGE_BYPASS_REG;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = read_from_ec(reg, 1, &val);
+ if (ret < 0)
+ return ret;
+
+ str = (char *) charge_type_strings[0];
+ for (ret = 0; ret < ARRAY_SIZE(charge_type_strings); ret++) {
+ if (val == vals[ret]) {
+ str = (char *) charge_type_strings[ret];
+ break;
+ }
+ }
+
+ return sysfs_emit(buf, "%s\n", str);
+}
+
+static DEVICE_ATTR_RW(charge_type);
+
+static ssize_t charge_control_end_threshold_store(struct device *dev,
+ struct device_attribute *attr, const char *buf,
+ size_t count)
+{
+ u64 val, reg;
+ int ret;
+
+ ret = kstrtou64(buf, 10, &val);
+ if (ret < 0)
+ return ret;
+
+ if (val > 100)
+ return -EINVAL;
+
+ switch (board) {
+ case oxp_x1:
+ case oxp_fly:
+ reg = OXP_X1_CHARGE_LIMIT_REG;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = write_to_ec(reg, val);
+ if (ret < 0)
+ return ret;
+
+ return count;
+}
+
+static ssize_t charge_control_end_threshold_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ int ret;
+ u8 reg;
+ long val;
+
+ switch (board) {
+ case oxp_x1:
+ case oxp_fly:
+ reg = OXP_X1_CHARGE_LIMIT_REG;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = read_from_ec(reg, 1, &val);
+ if (ret < 0)
+ return ret;
+
+ return sysfs_emit(buf, "%ld\n", val);
+}
+
+static DEVICE_ATTR_RW(charge_control_end_threshold);
+
+static int oxp_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
+{
+ /* OneXPlayer devices only have one battery. */
+ if (strcmp(battery->desc->name, "BAT0") != 0 &&
+ strcmp(battery->desc->name, "BAT1") != 0 &&
+ strcmp(battery->desc->name, "BATC") != 0 &&
+ strcmp(battery->desc->name, "BATT") != 0)
+ return -ENODEV;
+
+ if (device_create_file(&battery->dev,
+ &dev_attr_charge_control_end_threshold))
+ return -ENODEV;
+
+ if (device_create_file(&battery->dev,
+ &dev_attr_charge_type)) {
+ device_remove_file(&battery->dev,
+ &dev_attr_charge_control_end_threshold);
+ return -ENODEV;
+ }
+
+ return 0;
+}
+
+static int oxp_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
+{
+ device_remove_file(&battery->dev,
+ &dev_attr_charge_control_end_threshold);
+ device_remove_file(&battery->dev,
+ &dev_attr_charge_type);
+ return 0;
+}
+
+static struct acpi_battery_hook battery_hook = {
+ .add_battery = oxp_battery_add,
+ .remove_battery = oxp_battery_remove,
+ .name = "OneXPlayer Battery",
+};
+
/* PWM enable/disable functions */
static int oxp_pwm_enable(void)
{
@@ -753,6 +954,9 @@ static int __init oxp_platform_init(void)
platform_create_bundle(&oxp_platform_driver,
oxp_platform_probe, NULL, 0, NULL, 0);
+ if (charge_control_supported())
+ battery_hook_register(&battery_hook);
+
return PTR_ERR_OR_ZERO(oxp_platform_device);
}
@@ -760,6 +964,8 @@ static void __exit oxp_platform_exit(void)
{
platform_device_unregister(oxp_platform_device);
platform_driver_unregister(&oxp_platform_driver);
+ if (charge_control_supported())
+ battery_hook_unregister(&battery_hook);
}
MODULE_DEVICE_TABLE(dmi, dmi_table);
--
2.48.1
next prev parent reply other threads:[~2025-02-22 16:30 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-22 16:18 [PATCH v2 00/12] hwmon: (oxpsensors) Add devices, features, fix ABI and move to platform/x86 Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 01/12] hwmon: (oxp-sensors) Distinguish the X1 variants Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 02/12] hwmon: (oxp-sensors) Add all OneXFly variants Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 03/12] ABI: testing: sysfs-class-power: add BypassS0 charge_type Antheas Kapenekakis
2025-02-22 16:18 ` Antheas Kapenekakis [this message]
2025-02-22 16:18 ` [PATCH v2 05/12] hwmon: (oxp-sensors) Rename ec group to tt_toggle Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 06/12] hwmon: (oxp-sensors) Add turbo led support to X1 devices Antheas Kapenekakis
2025-03-01 15:13 ` Derek J. Clark
2025-03-01 15:54 ` Antheas Kapenekakis
2025-03-01 16:13 ` Derek J. Clark
2025-03-01 16:52 ` Antheas Kapenekakis
2025-03-01 17:01 ` Derek J. Clark
2025-02-22 16:18 ` [PATCH v2 07/12] hwmon: (oxp-sensors) Move pwm_enable read to its own function Antheas Kapenekakis
2025-03-01 15:21 ` Derek J. Clark
2025-03-01 15:55 ` Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 08/12] hwmon: (oxp-sensors) Move pwm value read/write to separate functions Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 09/12] hwmon: (oxp-sensors) Move fan speed read to separate function Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 10/12] hwmon: (oxp-sensors) Adhere to sysfs-class-hwmon and enable pwm on 2 Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 11/12] platform/x86: oxpec: Move hwmon/oxp-sensors to platform/x86 Antheas Kapenekakis
2025-03-01 14:31 ` kernel test robot
2025-03-01 14:40 ` Antheas Kapenekakis
2025-03-03 14:06 ` Guenter Roeck
2025-03-03 17:47 ` Antheas Kapenekakis
2025-02-22 16:18 ` [PATCH v2 12/12] ABI: testing: add tt_toggle and tt_led entries Antheas Kapenekakis
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=20250222161824.172511-5-lkml@antheas.dev \
--to=lkml@antheas.dev \
--cc=corbet@lwn.net \
--cc=csinaction@pm.me \
--cc=derekjohn.clark@gmail.com \
--cc=eileen@one-netbook.com \
--cc=jdelvare@suse.com \
--cc=kdgreenberg234@protonmail.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=parthasarathymenon@gmail.com \
--cc=platform-driver-x86@vger.kernel.org \
--cc=samsagax@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox