Skip to content

Commit c1f7375

Browse files
jellysre
authored andcommitted
power: supply: support charge_types in extensions
Similar to charge_behaviour, charge_types is an enum option where reading the property shows the supported values, with the active value surrounded by brackets. To be able to use it with a power_supply extension a bitmask with the supported charge_Types values has to be added to power_supply_ext. Signed-off-by: Jelle van der Waa <[email protected]> Reviewed-by: Hans de Goede <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sebastian Reichel <[email protected]>
1 parent 11741b8 commit c1f7375

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

drivers/power/supply/power_supply_sysfs.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,27 @@ static ssize_t power_supply_show_charge_behaviour(struct device *dev,
321321
value->intval, buf);
322322
}
323323

324+
static ssize_t power_supply_show_charge_types(struct device *dev,
325+
struct power_supply *psy,
326+
enum power_supply_charge_type current_type,
327+
char *buf)
328+
{
329+
struct power_supply_ext_registration *reg;
330+
331+
scoped_guard(rwsem_read, &psy->extensions_sem) {
332+
power_supply_for_each_extension(reg, psy) {
333+
if (power_supply_ext_has_property(reg->ext,
334+
POWER_SUPPLY_PROP_CHARGE_TYPES))
335+
return power_supply_charge_types_show(dev,
336+
reg->ext->charge_types,
337+
current_type, buf);
338+
}
339+
}
340+
341+
return power_supply_charge_types_show(dev, psy->desc->charge_types,
342+
current_type, buf);
343+
}
344+
324345
static ssize_t power_supply_format_property(struct device *dev,
325346
bool uevent,
326347
struct device_attribute *attr,
@@ -365,7 +386,7 @@ static ssize_t power_supply_format_property(struct device *dev,
365386
case POWER_SUPPLY_PROP_CHARGE_TYPES:
366387
if (uevent) /* no possible values in uevents */
367388
goto default_format;
368-
ret = power_supply_charge_types_show(dev, psy->desc->charge_types,
389+
ret = power_supply_show_charge_types(dev, psy,
369390
value.intval, buf);
370391
break;
371392
case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:

drivers/power/supply/test_power.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ static int battery_charge_counter = -1000;
3737
static int battery_current = -1600;
3838
static enum power_supply_charge_behaviour battery_charge_behaviour =
3939
POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
40+
static enum power_supply_charge_type battery_charge_types =
41+
POWER_SUPPLY_CHARGE_TYPE_STANDARD;
4042
static bool battery_extension;
4143

4244
static bool module_initialized;
@@ -87,7 +89,7 @@ static int test_power_get_battery_property(struct power_supply *psy,
8789
val->intval = battery_status;
8890
break;
8991
case POWER_SUPPLY_PROP_CHARGE_TYPE:
90-
val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
92+
val->intval = battery_charge_types;
9193
break;
9294
case POWER_SUPPLY_PROP_HEALTH:
9395
val->intval = battery_health;
@@ -129,6 +131,9 @@ static int test_power_get_battery_property(struct power_supply *psy,
129131
case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
130132
val->intval = battery_charge_behaviour;
131133
break;
134+
case POWER_SUPPLY_PROP_CHARGE_TYPES:
135+
val->intval = battery_charge_types;
136+
break;
132137
default:
133138
pr_info("%s: some properties deliberately report errors.\n",
134139
__func__);
@@ -140,7 +145,7 @@ static int test_power_get_battery_property(struct power_supply *psy,
140145
static int test_power_battery_property_is_writeable(struct power_supply *psy,
141146
enum power_supply_property psp)
142147
{
143-
return psp == POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR;
148+
return psp == POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR || psp == POWER_SUPPLY_PROP_CHARGE_TYPES;
144149
}
145150

146151
static int test_power_set_battery_property(struct power_supply *psy,
@@ -156,6 +161,14 @@ static int test_power_set_battery_property(struct power_supply *psy,
156161
}
157162
battery_charge_behaviour = val->intval;
158163
break;
164+
case POWER_SUPPLY_PROP_CHARGE_TYPES:
165+
if (val->intval < 0 ||
166+
val->intval >= BITS_PER_TYPE(typeof(psy->desc->charge_types)) ||
167+
!(BIT(val->intval) & psy->desc->charge_types)) {
168+
return -EINVAL;
169+
}
170+
battery_charge_types = val->intval;
171+
break;
159172
default:
160173
return -EINVAL;
161174
}
@@ -188,6 +201,7 @@ static enum power_supply_property test_power_battery_props[] = {
188201
POWER_SUPPLY_PROP_CURRENT_AVG,
189202
POWER_SUPPLY_PROP_CURRENT_NOW,
190203
POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
204+
POWER_SUPPLY_PROP_CHARGE_TYPES,
191205
};
192206

193207
static char *test_power_ac_supplied_to[] = {
@@ -215,6 +229,8 @@ static const struct power_supply_desc test_power_desc[] = {
215229
.charge_behaviours = BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO)
216230
| BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)
217231
| BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE),
232+
.charge_types = BIT(POWER_SUPPLY_CHARGE_TYPE_STANDARD)
233+
| BIT(POWER_SUPPLY_CHARGE_TYPE_LONGLIFE)
218234
},
219235
[TEST_USB] = {
220236
.name = "test_usb",

include/linux/power_supply.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ struct power_supply_desc {
288288
struct power_supply_ext {
289289
const char *const name;
290290
u8 charge_behaviours;
291+
u32 charge_types;
291292
const enum power_supply_property *properties;
292293
size_t num_properties;
293294

0 commit comments

Comments
 (0)