#define EC_ADDR_OEM_4 0x07A6
#define OVERBOOST_DYN_TEMP_OFF BIT(1)
+#define CHARGING_PROFILE_MASK GENMASK(5, 4)
+#define CHARGING_PROFILE_HIGH_CAPACITY 0x00
+#define CHARGING_PROFILE_BALANCED 0x01
+#define CHARGING_PROFILE_STATIONARY 0x02
#define TOUCHPAD_TOGGLE_OFF BIT(6)
#define EC_ADDR_CHARGE_CTRL 0x07B9
#define UNIWILL_FEATURE_SUPER_KEY BIT(1)
#define UNIWILL_FEATURE_TOUCHPAD_TOGGLE BIT(2)
#define UNIWILL_FEATURE_LIGHTBAR BIT(3)
-#define UNIWILL_FEATURE_BATTERY BIT(4)
-#define UNIWILL_FEATURE_CPU_TEMP BIT(5)
-#define UNIWILL_FEATURE_GPU_TEMP BIT(6)
-#define UNIWILL_FEATURE_PRIMARY_FAN BIT(7)
-#define UNIWILL_FEATURE_SECONDARY_FAN BIT(8)
-#define UNIWILL_FEATURE_NVIDIA_CTGP_CONTROL BIT(9)
-#define UNIWILL_FEATURE_USB_C_POWER_PRIORITY BIT(10)
+#define UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT BIT(4)
+/* Mutually exclusive with the charge limit feature */
+#define UNIWILL_FEATURE_BATTERY_CHARGE_MODES BIT(5)
+#define UNIWILL_FEATURE_CPU_TEMP BIT(6)
+#define UNIWILL_FEATURE_GPU_TEMP BIT(7)
+#define UNIWILL_FEATURE_PRIMARY_FAN BIT(8)
+#define UNIWILL_FEATURE_SECONDARY_FAN BIT(9)
+#define UNIWILL_FEATURE_NVIDIA_CTGP_CONTROL BIT(10)
+#define UNIWILL_FEATURE_USB_C_POWER_PRIORITY BIT(11)
enum usb_c_power_priority_options {
USB_C_POWER_PRIORITY_CHARGING = 0,
struct regmap *regmap;
unsigned int features;
struct acpi_battery_hook hook;
- unsigned int last_charge_ctrl;
struct mutex battery_lock; /* Protects the list of currently registered batteries */
+ union {
+ struct {
+ /* Protects writes to last_charge_type */
+ struct mutex charge_type_lock;
+ enum power_supply_charge_type last_charge_type;
+ };
+ unsigned int last_charge_ctrl;
+ };
bool last_fn_lock_state;
bool last_super_key_enable_state;
bool last_touchpad_toggle_enable_state;
return (data->features & features) == features;
}
+static inline bool uniwill_device_supports_any(const struct uniwill_data *data,
+ unsigned int features)
+{
+ return data->features & features;
+}
+
static int uniwill_ec_reg_write(void *context, unsigned int reg, unsigned int val)
{
union acpi_object params[2] = {
return min(value, 100);
}
+static int uniwill_read_charge_type(struct uniwill_data *data, enum power_supply_charge_type *type)
+{
+ unsigned int value;
+ int ret;
+
+ ret = regmap_read(data->regmap, EC_ADDR_OEM_4, &value);
+ if (ret < 0)
+ return ret;
+
+ switch (FIELD_GET(CHARGING_PROFILE_MASK, value)) {
+ case CHARGING_PROFILE_HIGH_CAPACITY:
+ *type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
+ return 0;
+ case CHARGING_PROFILE_BALANCED:
+ *type = POWER_SUPPLY_CHARGE_TYPE_LONGLIFE;
+ return 0;
+ case CHARGING_PROFILE_STATIONARY:
+ *type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+ return 0;
+ default:
+ return -EPROTO;
+ }
+}
+
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)
int ret;
switch (psp) {
+ case POWER_SUPPLY_PROP_CHARGE_TYPES:
+ /*
+ * We need to use the cached value here because the charging mode
+ * reported by the EC might temporarily change when a external power
+ * source has been connected.
+ */
+ mutex_lock(&data->charge_type_lock);
+ val->intval = data->last_charge_type;
+ mutex_unlock(&data->charge_type_lock);
+ return 0;
case POWER_SUPPLY_PROP_HEALTH:
ret = power_supply_get_property_direct(psy, POWER_SUPPLY_PROP_PRESENT, &prop);
if (ret < 0)
}
}
+static int uniwill_write_charge_type(struct uniwill_data *data, enum power_supply_charge_type type)
+{
+ unsigned int value;
+
+ switch (type) {
+ case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
+ value = FIELD_PREP(CHARGING_PROFILE_MASK, CHARGING_PROFILE_STATIONARY);
+ break;
+ case POWER_SUPPLY_CHARGE_TYPE_STANDARD:
+ value = FIELD_PREP(CHARGING_PROFILE_MASK, CHARGING_PROFILE_HIGH_CAPACITY);
+ break;
+ case POWER_SUPPLY_CHARGE_TYPE_LONGLIFE:
+ value = FIELD_PREP(CHARGING_PROFILE_MASK, CHARGING_PROFILE_BALANCED);
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return regmap_update_bits(data->regmap, EC_ADDR_OEM_4, CHARGING_PROFILE_MASK, value);
+}
+
+static int uniwill_restore_charge_type(struct uniwill_data *data)
+{
+ guard(mutex)(&data->charge_type_lock);
+
+ return uniwill_write_charge_type(data, data->last_charge_type);
+}
+
static int uniwill_set_property(struct power_supply *psy, const struct power_supply_ext *ext,
void *drvdata, enum power_supply_property psp,
const union power_supply_propval *val)
{
struct uniwill_data *data = drvdata;
+ int ret;
switch (psp) {
+ case POWER_SUPPLY_PROP_CHARGE_TYPES:
+ mutex_lock(&data->charge_type_lock);
+
+ ret = uniwill_write_charge_type(data, val->intval);
+ if (ret >= 0)
+ data->last_charge_type = val->intval;
+
+ mutex_unlock(&data->charge_type_lock);
+
+ return ret;
case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
if (val->intval < 0 || val->intval > 100)
return -EINVAL;
const struct power_supply_ext *ext, void *drvdata,
enum power_supply_property psp)
{
- if (psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD)
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CHARGE_TYPES:
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
return true;
-
- return false;
+ default:
+ return false;
+ }
}
-static const enum power_supply_property uniwill_properties[] = {
+static const enum power_supply_property uniwill_charge_limit_properties[] = {
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
};
-static const struct power_supply_ext uniwill_extension = {
+static const struct power_supply_ext uniwill_charge_limit_extension = {
.name = DRIVER_NAME,
- .properties = uniwill_properties,
- .num_properties = ARRAY_SIZE(uniwill_properties),
+ .properties = uniwill_charge_limit_properties,
+ .num_properties = ARRAY_SIZE(uniwill_charge_limit_properties),
+ .get_property = uniwill_get_property,
+ .set_property = uniwill_set_property,
+ .property_is_writeable = uniwill_property_is_writeable,
+};
+
+static const enum power_supply_property uniwill_charge_modes_properties[] = {
+ POWER_SUPPLY_PROP_CHARGE_TYPES,
+ POWER_SUPPLY_PROP_HEALTH,
+};
+
+static const struct power_supply_ext uniwill_charge_modes_extension = {
+ .name = DRIVER_NAME,
+ .charge_types = BIT(POWER_SUPPLY_CHARGE_TYPE_TRICKLE) |
+ BIT(POWER_SUPPLY_CHARGE_TYPE_STANDARD) |
+ BIT(POWER_SUPPLY_CHARGE_TYPE_LONGLIFE),
+ .properties = uniwill_charge_modes_properties,
+ .num_properties = ARRAY_SIZE(uniwill_charge_modes_properties),
.get_property = uniwill_get_property,
.set_property = uniwill_set_property,
.property_is_writeable = uniwill_property_is_writeable,
if (!entry)
return -ENOMEM;
- ret = power_supply_register_extension(battery, &uniwill_extension, data->dev, data);
+ if (uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT))
+ ret = power_supply_register_extension(battery, &uniwill_charge_limit_extension,
+ data->dev, data);
+ else
+ ret = power_supply_register_extension(battery, &uniwill_charge_modes_extension,
+ data->dev, data);
+
if (ret < 0) {
kfree(entry);
return ret;
}
}
- power_supply_unregister_extension(battery, &uniwill_extension);
+ if (uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT))
+ power_supply_unregister_extension(battery, &uniwill_charge_limit_extension);
+ else
+ power_supply_unregister_extension(battery, &uniwill_charge_modes_extension);
return 0;
}
unsigned int value, threshold, sanitized;
int ret;
- if (!uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY))
- return 0;
+ if (uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT)) {
+ ret = regmap_read(data->regmap, EC_ADDR_CHARGE_CTRL, &value);
+ if (ret < 0)
+ return ret;
- 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;
+ }
+ } else if (uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_MODES)) {
+ ret = devm_mutex_init(data->dev, &data->charge_type_lock);
+ 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);
+ ret = uniwill_read_charge_type(data, &data->last_charge_type);
if (ret < 0)
return ret;
+ } else {
+ return 0;
}
ret = devm_mutex_init(data->dev, &data->battery_lock);
{
struct uniwill_data *data = container_of(nb, struct uniwill_data, nb);
struct uniwill_battery_entry *entry;
+ int ret;
switch (action) {
case UNIWILL_OSD_BATTERY_ALERT:
- if (!uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY))
+ if (!uniwill_device_supports_any(data,
+ UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT |
+ UNIWILL_FEATURE_BATTERY_CHARGE_MODES))
return NOTIFY_DONE;
mutex_lock(&data->battery_lock);
return NOTIFY_OK;
case UNIWILL_OSD_DC_ADAPTER_CHANGED:
- if (!uniwill_device_supports(data, UNIWILL_FEATURE_USB_C_POWER_PRIORITY))
+ if (!uniwill_device_supports_any(data,
+ UNIWILL_FEATURE_BATTERY_CHARGE_MODES |
+ UNIWILL_FEATURE_USB_C_POWER_PRIORITY))
return NOTIFY_DONE;
- return notifier_from_errno(usb_c_power_priority_restore(data));
+ if (uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_MODES)) {
+ ret = uniwill_restore_charge_type(data);
+ if (ret < 0)
+ return notifier_from_errno(ret);
+ }
+
+ if (uniwill_device_supports(data, UNIWILL_FEATURE_USB_C_POWER_PRIORITY)) {
+ ret = usb_c_power_priority_restore(data);
+ if (ret < 0)
+ return notifier_from_errno(ret);
+ }
+
+ return NOTIFY_OK;
case UNIWILL_OSD_FN_LOCK:
if (!uniwill_device_supports(data, UNIWILL_FEATURE_FN_LOCK))
return NOTIFY_DONE;
static int uniwill_suspend_battery(struct uniwill_data *data)
{
- if (!uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY))
+ if (!uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT))
return 0;
/*
static int uniwill_resume_battery(struct uniwill_data *data)
{
- if (!uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY))
- return 0;
+ if (uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_MODES))
+ return uniwill_restore_charge_type(data);
- return regmap_update_bits(data->regmap, EC_ADDR_CHARGE_CTRL, CHARGE_CTRL_MASK,
- data->last_charge_ctrl);
+ if (uniwill_device_supports(data, UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT))
+ return regmap_update_bits(data->regmap, EC_ADDR_CHARGE_CTRL, CHARGE_CTRL_MASK,
+ data->last_charge_ctrl);
+
+ return 0;
}
static int uniwill_resume_nvidia_ctgp(struct uniwill_data *data)
static struct uniwill_device_descriptor lapqc71a_lapqc71b_descriptor __initdata = {
.features = UNIWILL_FEATURE_SUPER_KEY |
- UNIWILL_FEATURE_BATTERY |
+ UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT |
UNIWILL_FEATURE_CPU_TEMP |
UNIWILL_FEATURE_GPU_TEMP |
UNIWILL_FEATURE_PRIMARY_FAN |
.features = UNIWILL_FEATURE_FN_LOCK |
UNIWILL_FEATURE_SUPER_KEY |
UNIWILL_FEATURE_TOUCHPAD_TOGGLE |
- UNIWILL_FEATURE_BATTERY |
+ UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT |
UNIWILL_FEATURE_CPU_TEMP |
UNIWILL_FEATURE_GPU_TEMP |
UNIWILL_FEATURE_PRIMARY_FAN |
UNIWILL_FEATURE_SUPER_KEY |
UNIWILL_FEATURE_TOUCHPAD_TOGGLE |
UNIWILL_FEATURE_LIGHTBAR |
- UNIWILL_FEATURE_BATTERY |
+ UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT |
UNIWILL_FEATURE_CPU_TEMP |
UNIWILL_FEATURE_GPU_TEMP |
UNIWILL_FEATURE_PRIMARY_FAN |
if (force) {
/* Assume that the device supports all features except the charge limit */
- device_descriptor.features = UINT_MAX & ~UNIWILL_FEATURE_BATTERY;
+ device_descriptor.features = UINT_MAX & ~UNIWILL_FEATURE_BATTERY_CHARGE_LIMIT;
pr_warn("Enabling potentially unsupported features\n");
}