[PATCH RFC 6/6] platform/x86: system76: Use power_supply extension API
From: Thomas Weißschuh
Date: Thu Jun 06 2024 - 11:14:14 EST
Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
drivers/platform/x86/system76_acpi.c | 83 +++++++++++++++++++-----------------
1 file changed, 44 insertions(+), 39 deletions(-)
diff --git a/drivers/platform/x86/system76_acpi.c b/drivers/platform/x86/system76_acpi.c
index 3da753b3d00d..c26f42c917dd 100644
--- a/drivers/platform/x86/system76_acpi.c
+++ b/drivers/platform/x86/system76_acpi.c
@@ -162,7 +162,7 @@ enum {
THRESHOLD_END,
};
-static ssize_t battery_get_threshold(int which, char *buf)
+static int battery_get_threshold(int which, int *val)
{
struct acpi_object_list input;
union acpi_object param;
@@ -186,29 +186,21 @@ static ssize_t battery_get_threshold(int which, char *buf)
if (ret == BATTERY_THRESHOLD_INVALID)
return -EINVAL;
- return sysfs_emit(buf, "%d\n", (int)ret);
+ *val = ret;
+ return 0;
}
-static ssize_t battery_set_threshold(int which, const char *buf, size_t count)
+static int battery_set_threshold(int which, unsigned int value)
{
struct acpi_object_list input;
union acpi_object params[2];
acpi_handle handle;
acpi_status status;
- unsigned int value;
- int ret;
handle = ec_get_handle();
if (!handle)
return -ENODEV;
- ret = kstrtouint(buf, 10, &value);
- if (ret)
- return ret;
-
- if (value > 100)
- return -EINVAL;
-
input.count = 2;
input.pointer = params;
// Start/stop selection
@@ -222,52 +214,65 @@ static ssize_t battery_set_threshold(int which, const char *buf, size_t count)
if (ACPI_FAILURE(status))
return -EIO;
- return count;
+ return 0;
}
-static ssize_t charge_control_start_threshold_show(struct device *dev,
- struct device_attribute *attr, char *buf)
-{
- return battery_get_threshold(THRESHOLD_START, buf);
-}
+static const enum power_supply_property system76_battery_properties[] = {
+ POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD,
+ POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
+};
-static ssize_t charge_control_start_threshold_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t count)
+static int system76_property_is_writeable(struct power_supply *psy,
+ enum power_supply_property psp)
{
- return battery_set_threshold(THRESHOLD_START, buf, count);
+ return true;
}
-static DEVICE_ATTR_RW(charge_control_start_threshold);
-
-static ssize_t charge_control_end_threshold_show(struct device *dev,
- struct device_attribute *attr, char *buf)
+static int system76_get_property(struct power_supply *psy, enum power_supply_property psp,
+ union power_supply_propval *val)
{
- return battery_get_threshold(THRESHOLD_END, buf);
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
+ return battery_get_threshold(THRESHOLD_START, &val->intval);
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
+ return battery_get_threshold(THRESHOLD_END, &val->intval);
+ default:
+ return -EINVAL;
+ };
}
-static ssize_t charge_control_end_threshold_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t count)
+static int system76_set_property(struct power_supply *psy, enum power_supply_property psp,
+ const union power_supply_propval *val)
{
- return battery_set_threshold(THRESHOLD_END, buf, count);
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
+ if (val->intval < 0 || val->intval > 100)
+ return -EINVAL;
+ return battery_set_threshold(THRESHOLD_START, val->intval);
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
+ if (val->intval < 0 || val->intval > 100)
+ return -EINVAL;
+ return battery_set_threshold(THRESHOLD_END, val->intval);
+ default:
+ return -EINVAL;
+ };
}
-static DEVICE_ATTR_RW(charge_control_end_threshold);
-
-static struct attribute *system76_battery_attrs[] = {
- &dev_attr_charge_control_start_threshold.attr,
- &dev_attr_charge_control_end_threshold.attr,
- NULL,
+static const struct power_supply_ext system76_power_supply_ext = {
+ .properties = system76_battery_properties,
+ .num_properties = ARRAY_SIZE(system76_battery_properties),
+ .property_is_writeable = system76_property_is_writeable,
+ .get_property = system76_get_property,
+ .set_property = system76_set_property,
};
-ATTRIBUTE_GROUPS(system76_battery);
-
static int system76_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
{
// System76 EC only supports 1 battery
if (strcmp(battery->desc->name, "BAT0") != 0)
return -ENODEV;
- if (device_add_groups(&battery->dev, system76_battery_groups))
+ if (power_supply_register_extension(battery, &system76_power_supply_ext))
return -ENODEV;
return 0;
@@ -275,7 +280,7 @@ static int system76_battery_add(struct power_supply *battery, struct acpi_batter
static int system76_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
{
- device_remove_groups(&battery->dev, system76_battery_groups);
+ power_supply_unregister_extension(battery, &system76_power_supply_ext);
return 0;
}
--
2.45.2