charging_priority: Remember state and re-write on power change event

This commit is contained in:
Christoffer Sandberg 2023-02-13 09:56:07 +01:00
parent 1bab01f93b
commit b35bc84de0

View file

@ -57,6 +57,7 @@
#define UNIWILL_BRIGHTNESS_DEFAULT UNIWILL_BRIGHTNESS_MAX * 0.30
#define UNIWILL_COLOR_DEFAULT 0xffffff
static void uw_charging_priority_write_state(void);
static void uw_charging_profile_write_state(void);
struct tuxedo_keyboard_driver uniwill_keyboard_driver;
@ -386,6 +387,7 @@ void uniwill_event_callb(u32 code)
case UNIWILL_OSD_DC_ADAPTER_CHANGE:
uniwill_write_kbd_bl_state();
uw_charging_profile_write_state();
uw_charging_priority_write_state();
break;
}
}
@ -769,6 +771,35 @@ static int uw_lightbar_remove(struct platform_device *dev)
}
static bool uw_charging_prio_loaded = false;
static bool uw_charging_prio_value;
static ssize_t uw_charging_prios_available_show(struct device *child,
struct device_attribute *attr,
char *buffer);
static ssize_t uw_charging_prio_show(struct device *child,
struct device_attribute *attr, char *buffer);
static ssize_t uw_charging_prio_store(struct device *child,
struct device_attribute *attr,
const char *buffer, size_t size);
struct uw_charging_prio_attrs_t {
struct device_attribute charging_prios_available;
struct device_attribute charging_prio;
} uw_charging_prio_attrs = {
.charging_prios_available = __ATTR(charging_prios_available, 0444, uw_charging_prios_available_show, NULL),
.charging_prio = __ATTR(charging_prio, 0644, uw_charging_prio_show, uw_charging_prio_store)
};
static struct attribute *uw_charging_prio_attrs_list[] = {
&uw_charging_prio_attrs.charging_prios_available.attr,
&uw_charging_prio_attrs.charging_prio.attr,
NULL
};
static struct attribute_group uw_charging_prio_attr_group = {
.name = "charging_priority",
.attrs = uw_charging_prio_attrs_list
};
/*
* charging_prio values
@ -788,6 +819,8 @@ static int uw_set_charging_priority(u8 charging_priority)
next_data = (previous_data & ~(1 << 7)) | charging_priority;
result = uniwill_write_ec_ram(0x07cc, next_data);
if (result == 0)
uw_charging_prio_value = charging_priority;
return result;
}
@ -796,6 +829,8 @@ static int uw_get_charging_priority(u8 *charging_priority)
{
int result = uniwill_read_ec_ram(0x07cc, charging_priority);
*charging_priority = (*charging_priority >> 7) & 0x01;
if (result == 0)
uw_charging_prio_value = *charging_priority;
return result;
}
@ -824,6 +859,25 @@ static int uw_has_charging_priority(bool *status)
return result;
}
static void uw_charging_priority_write_state(void)
{
if (uw_charging_prio_loaded)
uw_set_charging_priority(uw_charging_prio_value);
}
static void uw_charging_priority_init(struct platform_device *dev)
{
u8 dummy;
struct uniwill_device_features_t *uw_feats = &uniwill_device_features;
if (uw_feats->uniwill_has_charging_prio)
uw_charging_prio_loaded = sysfs_create_group(&dev->dev.kobj, &uw_charging_prio_attr_group) == 0;
// Trigger read for state init
if (uw_charging_prio_loaded)
uw_get_charging_priority(&dummy);
}
static bool uw_charging_profile_loaded = false;
static bool uw_charging_profile_value;
@ -1093,25 +1147,6 @@ static ssize_t uw_charging_prio_store(struct device *child,
return -EINVAL;
}
struct uw_charging_prio_attrs_t {
struct device_attribute charging_prios_available;
struct device_attribute charging_prio;
} uw_charging_prio_attrs = {
.charging_prios_available = __ATTR(charging_prios_available, 0444, uw_charging_prios_available_show, NULL),
.charging_prio = __ATTR(charging_prio, 0644, uw_charging_prio_show, uw_charging_prio_store)
};
static struct attribute *uw_charging_prio_attrs_list[] = {
&uw_charging_prio_attrs.charging_prios_available.attr,
&uw_charging_prio_attrs.charging_prio.attr,
NULL
};
static struct attribute_group uw_charging_prio_attr_group = {
.name = "charging_priority",
.attrs = uw_charging_prio_attrs_list
};
struct uniwill_device_features_t *uniwill_get_device_features(void)
{
struct uniwill_device_features_t *uw_feats = &uniwill_device_features;
@ -1210,9 +1245,7 @@ static int uniwill_keyboard_probe(struct platform_device *dev)
status = uw_lightbar_init(dev);
uw_lightbar_loaded = (status >= 0);
if (uw_feats->uniwill_has_charging_prio)
uw_charging_prio_loaded = sysfs_create_group(&dev->dev.kobj, &uw_charging_prio_attr_group) == 0;
uw_charging_priority_init(dev);
uw_charging_profile_init(dev);
return 0;