]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
platform/x86: uniwill-laptop: Properly initialize charging threshold
authorArmin Wolf <W_Armin@gmx.de>
Tue, 12 May 2026 23:21:38 +0000 (01:21 +0200)
committerIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Tue, 19 May 2026 13:52:43 +0000 (16:52 +0300)
The EC might initialize the charge threshold with 0 to signal that
said threshold is uninitialized. Detect this and replace said value
with 100 to signal the EC that we want to take control of battery
charging. Also set the threshold to 100 if the EC-provided value
is invalid.

Fixes: d050479693bb ("platform/x86: Add Uniwill laptop driver")
Reviewed-by: Werner Sembach <wse@tuxedocomputers.com>
Signed-off-by: Armin Wolf <W_Armin@gmx.de>
Link: https://patch.msgid.link/20260512232145.329260-2-W_Armin@gmx.de
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
drivers/platform/x86/uniwill/uniwill-acpi.c

index 945df509263715639b119638281a464a5b61a456..d9c202fc8c7127a3df488908370e898197063e23 100644 (file)
@@ -1359,6 +1359,16 @@ static int uniwill_led_init(struct uniwill_data *data)
                                                         &init_data);
 }
 
+static unsigned int uniwill_sanitize_battery_threshold(unsigned int value)
+{
+       /* 0 means "charging threshold not active" */
+       if (!value)
+               return 100;
+
+       /* Guard against invalid values */
+       return min(value, 100);
+}
+
 static int uniwill_get_property(struct power_supply *psy, const struct power_supply_ext *ext,
                                void *drvdata, enum power_supply_property psp,
                                union power_supply_propval *val)
@@ -1405,7 +1415,8 @@ static int uniwill_get_property(struct power_supply *psy, const struct power_sup
                if (ret < 0)
                        return ret;
 
-               val->intval = clamp_val(FIELD_GET(CHARGE_CTRL_MASK, regval), 0, 100);
+               regval = FIELD_GET(CHARGE_CTRL_MASK, regval);
+               val->intval = uniwill_sanitize_battery_threshold(regval);
                return 0;
        default:
                return -EINVAL;
@@ -1500,11 +1511,33 @@ static int uniwill_remove_battery(struct power_supply *battery, struct acpi_batt
 
 static int uniwill_battery_init(struct uniwill_data *data)
 {
+       unsigned int value, threshold, sanitized;
        int ret;
 
        if (!uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY))
                return 0;
 
+       ret = regmap_read(data->regmap, EC_ADDR_CHARGE_CTRL, &value);
+       if (ret < 0)
+               return ret;
+
+       /*
+        * The charge control threshold might be initialized with 0 by
+        * the EC to signal that said threshold is uninitialized. We thus
+        * need to replace this placeholder value with a valid one (100)
+        * to signal that we want to take control of battery charging.
+        * For the sake of completeness we also apply this to other
+        * invalid threshold values.
+        */
+       threshold = FIELD_GET(CHARGE_CTRL_MASK, value);
+       sanitized = uniwill_sanitize_battery_threshold(threshold);
+       if (threshold != sanitized) {
+               FIELD_MODIFY(CHARGE_CTRL_MASK, &value, sanitized);
+               ret = regmap_write(data->regmap, EC_ADDR_CHARGE_CTRL, value);
+               if (ret < 0)
+                       return ret;
+       }
+
        ret = devm_mutex_init(data->dev, &data->battery_lock);
        if (ret < 0)
                return ret;