Skip to contentSkip to conversation

Add sysfs entry for system load control

PATCH Applied Waqar Hameed 3 participants 15 messages Jun 29 – Jul 27

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Add sysfs entry for system load control

During discussions of the development of a new driver [1], it was concluded that a new sysfs ABI for controlling the connection from power to system load is wanted (often called BATFET). This patchset introduces such one.

There are already several drivers that are controlling this, but with their own custom sysfs entries. The drivers in this patchset were find with a simple grep for bat\s?fet (there might be more?), and then converted to use this new sysfs ABI. The old custom ABIs are left untouched of course, for backward compatibility.

[1] https://lore.kernel.org/lkml/[email protected]/

Waqar Hameed (10):
power: supply: Add sysfs entry for system load control
selftests: power_supply: Add tests for load_switch ABI
power: supply: ltc4162-l: Use POWER_SUPPLY_PROP_LOAD_SWITCH
power: supply: rt9471: Remove superfluous unpacking of propval
power: supply: rt9471: Use POWER_SUPPLY_PROP_LOAD_SWITCH
power: supply: rt9467: Use POWER_SUPPLY_PROP_LOAD_SWITCH
power: supply: bq24257: Use POWER_SUPPLY_PROP_LOAD_SWITCH
power: supply: bq24190: Remove unused watchdog struct field
power: supply: bq24190: Disable watchdog with bq24190_write_mask()
power: supply: bq24190: Use POWER_SUPPLY_PROP_LOAD_SWITCH

 Documentation/ABI/testing/sysfs-class-power   |  24 +++++
 .../ABI/testing/sysfs-class-power-ltc4162l    |   2 +
 .../ABI/testing/sysfs-class-power-rt9467      |   2 +
 .../ABI/testing/sysfs-class-power-rt9471      |   2 +
 drivers/power/supply/bq24190_charger.c        |  74 +++++++++++--
 drivers/power/supply/bq24257_charger.c        |  41 ++++++-
 drivers/power/supply/ltc4162-l-charger.c      |  54 ++++++++--
 drivers/power/supply/power_supply_sysfs.c     |   9 ++
 drivers/power/supply/rt9467-charger.c         |  52 +++++++--
 drivers/power/supply/rt9471.c                 | 101 +++++++++++++-----
 include/linux/power_supply.h                  |   9 ++
 .../test_power_supply_properties.sh           |   2 +
 12 files changed, 322 insertions(+), 50 deletions(-)

base-commit: ab9de95c9cf952332ab79453b4b5d1bfca8e514f

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/10 power: supply: Add sysfs entry for system load control

There are devices that can control the connection from power to system load. For example, with a field-effect transistor between a battery and the system load (BATFET). Drivers for these devices are currently enrolling their own custom sysfs property to control this.

In order to unify this, add a new sysfs entry for controlling such switch and corresponding power_supply_property with enum values. The obvious states are "on" and "off", i.e. there is a connection or not, respectively. However, many devices can also enter special modes such as "low-power", "shipping" or "deep sleep".

---
 Documentation/ABI/testing/sysfs-class-power | 24 +++++++++++++++++++++
 drivers/power/supply/power_supply_sysfs.c   |  9 ++++++++
 include/linux/power_supply.h                |  9 ++++++++
 3 files changed, 42 insertions(+)
Documentation/ABI/testing/sysfs-class-power +24 -0
index 32697b926cc8c..2287b207311cd 100644
@@ -590,6 +590,30 @@ Description:
 
 		Valid values: 0 - 100 (percent)
 
+What:		/sys/class/power_supply/<supply_name>/load_switch
+Date:		June 2026
+Contact:	[email protected]
+Description:
+		Devices can control the connection from power to system load.
+		For example, with a field-effect transistor between a battery
+		and the system load (BATFET). This entry controls such switch.
+		The obvious states are "on" and "off", i.e. there is a
+		connection or not, respectively. However, many devices can also
+		enter special modes such as "low-power", "shipping" or "deep
+		sleep". In these modes the switch is usually off and the
+		quiescent current quite low.
+
+		Access: Read, Write
+
+		Valid values:
+
+		============= ==================================
+		"On"      (1) Power is connected to the load.
+		"Off"     (2) Power is disconnected to the load.
+		"Standby" (3) Low-power mode.
+		"Ship"    (4) Ship mode.
+		============= ==================================
+
 **USB Properties**
 
 What:		/sys/class/power_supply/<supply_name>/input_current_limit
drivers/power/supply/power_supply_sysfs.c +9 -0
index f30a7b9ccd5e9..243d6e57704f6 100644
@@ -148,6 +148,14 @@ static const char * const POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[] = {
 	[POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE]		= "force-discharge",
 };
 
+static const char *const POWER_SUPPLY_LOAD_SWITCH_TEXT[] = {
+	[POWER_SUPPLY_LOAD_SWITCH_UNKNOWN] = "Unknown",
+	[POWER_SUPPLY_LOAD_SWITCH_ON]	   = "On",
+	[POWER_SUPPLY_LOAD_SWITCH_OFF]	   = "Off",
+	[POWER_SUPPLY_LOAD_SWITCH_STANDBY] = "Standby",
+	[POWER_SUPPLY_LOAD_SWITCH_SHIP]	   = "Ship",
+};
+
 static struct power_supply_attr power_supply_attrs[] __ro_after_init = {
 	/* Properties of type `int' */
 	POWER_SUPPLY_ENUM_ATTR(STATUS),
@@ -227,6 +235,7 @@ static struct power_supply_attr power_supply_attrs[] __ro_after_init = {
 	POWER_SUPPLY_ATTR(MANUFACTURE_DAY),
 	POWER_SUPPLY_ATTR(INTERNAL_RESISTANCE),
 	POWER_SUPPLY_ATTR(STATE_OF_HEALTH),
+	POWER_SUPPLY_ENUM_ATTR(LOAD_SWITCH),
 	/* Properties of type `const char *' */
 	POWER_SUPPLY_ATTR(MODEL_NAME),
 	POWER_SUPPLY_ATTR(MANUFACTURER),
include/linux/power_supply.h +9 -0
index 7a5e4c3242a01..eff5de03fd2ab 100644
@@ -100,6 +100,14 @@ enum {
 	POWER_SUPPLY_SCOPE_DEVICE,
 };
 
+enum {
+	POWER_SUPPLY_LOAD_SWITCH_UNKNOWN = 0,
+	POWER_SUPPLY_LOAD_SWITCH_ON,
+	POWER_SUPPLY_LOAD_SWITCH_OFF,
+	POWER_SUPPLY_LOAD_SWITCH_STANDBY,
+	POWER_SUPPLY_LOAD_SWITCH_SHIP,
+};
+
 enum power_supply_property {
 	/* Properties of type `int' */
 	POWER_SUPPLY_PROP_STATUS = 0,
@@ -178,6 +186,7 @@ enum power_supply_property {
 	POWER_SUPPLY_PROP_MANUFACTURE_DAY,
 	POWER_SUPPLY_PROP_INTERNAL_RESISTANCE,
 	POWER_SUPPLY_PROP_STATE_OF_HEALTH,
+	POWER_SUPPLY_PROP_LOAD_SWITCH,
 	/* Properties of type `const char *' */
 	POWER_SUPPLY_PROP_MODEL_NAME,
 	POWER_SUPPLY_PROP_MANUFACTURER,
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 01/10] power: supply: Add sysfs entry for system load control

On Mon, Jun 29, 2026 at 17:44 +0200 Waqar Hameed <[email protected]> wrote:

[...]

Quoted 21 lines
Documentation/ABI/testing/sysfs-class-power +13 -0
index 32697b926cc8c..2287b207311cd 100644
@@ -590,6 +590,30 @@ Description:
 
 		Valid values: 0 - 100 (percent)
 
+What:		/sys/class/power_supply/<supply_name>/load_switch
+Date:		June 2026
+Contact:	[email protected]
+Description:
+		Devices can control the connection from power to system load.
+		For example, with a field-effect transistor between a battery
+		and the system load (BATFET). This entry controls such switch.
+		The obvious states are "on" and "off", i.e. there is a
+		connection or not, respectively. However, many devices can also
+		enter special modes such as "low-power", "shipping" or "deep
+		sleep". In these modes the switch is usually off and the
+		quiescent current quite low.
+

Documentation/ABI/testing/sysfs-class-power Description: · line 615

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-power +10 -0
+		Access: Read, Write
+
+		Valid values:
+
+		============= ==================================
+		"On"      (1) Power is connected to the load.
+		"Off"     (2) Power is disconnected to the load.
+		"Standby" (3) Low-power mode.
+		"Ship"    (4) Ship mode.
+		============= ==================================

"Unknown" (0) needs to be added here as well...

#3 lore raw

Re: [PATCH 01/10] power: supply: Add sysfs entry for system load control

Hi,

Quoted 29 lines · Waqar Hameed

On Mon, Jun 29, 2026 at 17:44 +0200 Waqar Hameed <[email protected]> wrote:

[...]

Quoted 25 lines
Documentation/ABI/testing/sysfs-class-power +17 -0
index 32697b926cc8c..2287b207311cd 100644
@@ -590,6 +590,30 @@ Description:
 
 		Valid values: 0 - 100 (percent)
 
+What:		/sys/class/power_supply/<supply_name>/load_switch
+Date:		June 2026
+Contact:	[email protected]
+Description:
+		Devices can control the connection from power to system load.
+		For example, with a field-effect transistor between a battery
+		and the system load (BATFET). This entry controls such switch.
+		The obvious states are "on" and "off", i.e. there is a
+		connection or not, respectively. However, many devices can also
+		enter special modes such as "low-power", "shipping" or "deep
+		sleep". In these modes the switch is usually off and the
+		quiescent current quite low.
+
+		Access: Read, Write
+
+		Valid values:
+
Quoted 8 lines

Documentation/ABI/testing/sysfs-class-power Description: · line 615

Quoted 6 lines
Documentation/ABI/testing/sysfs-class-power +6 -0
+		============= ==================================
+		"On"      (1) Power is connected to the load.
+		"Off"     (2) Power is disconnected to the load.
+		"Standby" (3) Low-power mode.
+		"Ship"    (4) Ship mode.
+		============= ==================================

"Unknown" (0) needs to be added here as well...

I see the issue, that userspace has no idea, which values are supported by a specific device. I think it's better to follow the same style, that is already used for e.g. charge_types, so that the file reads something like this:

[On] Standby Ship

E.g. provide all supported values for the specific device and mark the current value with square brackets.

P.S.: I've applied the unrelated cleanup patches from this series.

Greetings,

-- Sebastian

#4 lore raw

Re: [PATCH 01/10] power: supply: Add sysfs entry for system load control

Quoted 46 lines · On Wed, Jul 22, 2026 at 03:25 +0200 Sebastian Reichel <[email protected]> wrote:

Hi,

Quoted 29 lines · Waqar Hameed

On Mon, Jun 29, 2026 at 17:44 +0200 Waqar Hameed <[email protected]> wrote:

[...]

Quoted 25 lines
Documentation/ABI/testing/sysfs-class-power +17 -0
index 32697b926cc8c..2287b207311cd 100644
@@ -590,6 +590,30 @@ Description:
 
 		Valid values: 0 - 100 (percent)
 
+What:		/sys/class/power_supply/<supply_name>/load_switch
+Date:		June 2026
+Contact:	[email protected]
+Description:
+		Devices can control the connection from power to system load.
+		For example, with a field-effect transistor between a battery
+		and the system load (BATFET). This entry controls such switch.
+		The obvious states are "on" and "off", i.e. there is a
+		connection or not, respectively. However, many devices can also
+		enter special modes such as "low-power", "shipping" or "deep
+		sleep". In these modes the switch is usually off and the
+		quiescent current quite low.
+
+		Access: Read, Write
+
+		Valid values:
+
Quoted 8 lines

Documentation/ABI/testing/sysfs-class-power Description: · line 615

Quoted 6 lines
Documentation/ABI/testing/sysfs-class-power +6 -0
+		============= ==================================
+		"On"      (1) Power is connected to the load.
+		"Off"     (2) Power is disconnected to the load.
+		"Standby" (3) Low-power mode.
+		"Ship"    (4) Ship mode.
+		============= ==================================

"Unknown" (0) needs to be added here as well...

I see the issue, that userspace has no idea, which values are supported by a specific device. I think it's better to follow the same style, that is already used for e.g. charge_types, so that the file reads something like this:

Quoted 4 lines

[On] Standby Ship

E.g. provide all supported values for the specific device and mark the current value with square brackets.

Alright, let's do that then.

Quoted 2 lines

P.S.: I've applied the unrelated cleanup patches from this series.

Thanks, I'll make sure to rebase then. Will send soon after the vacation period (a month).

#5 lore raw

PATCH 2/10 selftests: power_supply: Add tests for load_switch ABI

In reply to Waqar Hameed

Test the different valid values for the property.

---
 .../selftests/power_supply/test_power_supply_properties.sh      | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/power_supply/test_power_supply_properties.sh +2 -0
index a66b1313ed882..d3a546f95ae67 100755
@@ -80,6 +80,8 @@ for DEVNAME in $supplies; do
 
 	test_sysfs_prop_optional_list scope "Unknown","System","Device"
 
+    test_sysfs_prop_optional_list load_switch "Unknown","On","Off","Standby","Ship"
+
 	test_sysfs_prop_optional input_current_limit "uA"
 	test_sysfs_prop_optional input_voltage_limit "uV"
 
Signature · 1 line
2.43.0
#6 lore raw

PATCH 3/10 power: supply: ltc4162-l: Use POWER_SUPPLY_PROP_LOAD_SWITCH

In reply to Waqar Hameed

The framework supports controlling system load with POWER_SUPPLY_PROP_LOAD_SWITCH. Use this to select ship mode, but keep old custom sysfs ABI for backward compatibility. However, add a note in ABI documentation that one should prefer the property load_switch instead.

---
 .../ABI/testing/sysfs-class-power-ltc4162l    |  2 +
 drivers/power/supply/ltc4162-l-charger.c      | 54 ++++++++++++++++---
 2 files changed, 50 insertions(+), 6 deletions(-)
Documentation/ABI/testing/sysfs-class-power-ltc4162l +2 -0
index ba30db93052bf..ad46ad834af05 100644
@@ -77,6 +77,8 @@ Description:
 		The ship mode, when armed, activates once the input voltage
 		drops below 1V.
 
+		Note: use /sys/class/power_supply/ltc4162-l/load_switch instead.
+
 		Access: Read, Write
 
 		Valid values: 0 (disable) or 1 (enable)
drivers/power/supply/ltc4162-l-charger.c +48 -6

101 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

PATCH 4/10 power: supply: rt9471: Remove superfluous unpacking of propval

In reply to Waqar Hameed

Remove extra variable in set/get_property-functions that is only supplied directly to other functions. The value is not used for anything else.

Moreover, wrap these lines. Otherwise checkpatch warns about too long lines.

---
 drivers/power/supply/rt9471.c | 49 +++++++++++++++++++++--------------
 1 file changed, 29 insertions(+), 20 deletions(-)
drivers/power/supply/rt9471.c +29 -20
index e7f843f12c98e..ca3b0b57675d0 100644
@@ -370,23 +370,28 @@ static int rt9471_charger_set_property(struct power_supply *psy,
 				       const union power_supply_propval *val)
 {
 	struct rt9471_chip *chip = power_supply_get_drvdata(psy);
-	int value = val->intval;
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_STATUS:
-		return regmap_field_write(chip->rm_fields[F_CHG_EN], !!value);
+		return regmap_field_write(chip->rm_fields[F_CHG_EN],
+					  !!val->intval);
 	case POWER_SUPPLY_PROP_ONLINE:
-		return regmap_field_write(chip->rm_fields[F_HZ], !value);
+		return regmap_field_write(chip->rm_fields[F_HZ], !val->intval);
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
-		return rt9471_set_value_by_field_range(chip, F_ICHG_REG, RT9471_RANGE_ICHG, value);
+		return rt9471_set_value_by_field_range(
+			chip, F_ICHG_REG, RT9471_RANGE_ICHG, val->intval);
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
-		return rt9471_set_value_by_field_range(chip, F_VBAT_REG, RT9471_RANGE_VCHG, value);
+		return rt9471_set_value_by_field_range(
+			chip, F_VBAT_REG, RT9471_RANGE_VCHG, val->intval);
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
-		return rt9471_set_value_by_field_range(chip, F_AICR, RT9471_RANGE_AICR, value);
+		return rt9471_set_value_by_field_range(
+			chip, F_AICR, RT9471_RANGE_AICR, val->intval);
 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
-		return rt9471_set_value_by_field_range(chip, F_MIVR, RT9471_RANGE_MIVR, value);
+		return rt9471_set_value_by_field_range(
+			chip, F_MIVR, RT9471_RANGE_MIVR, val->intval);
 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
-		return rt9471_set_value_by_field_range(chip, F_IPRE_CHG, RT9471_RANGE_IPRE, value);
+		return rt9471_set_value_by_field_range(
+			chip, F_IPRE_CHG, RT9471_RANGE_IPRE, val->intval);
 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
 		return rt9471_set_ieoc(chip, val->intval);
 	default:
@@ -402,35 +407,39 @@ static int rt9471_charger_get_property(struct power_supply *psy,
 				       union power_supply_propval *val)
 {
 	struct rt9471_chip *chip = power_supply_get_drvdata(psy);
-	int *pvalue = &val->intval;
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_STATUS:
-		return rt9471_get_status(chip, pvalue);
+		return rt9471_get_status(chip, &val->intval);
 	case POWER_SUPPLY_PROP_ONLINE:
-		return rt9471_get_vbus_good(chip, pvalue);
+		return rt9471_get_vbus_good(chip, &val->intval);
 	case POWER_SUPPLY_PROP_CURRENT_MAX:
-		return rt9471_get_usb_type_current(chip, pvalue);
+		return rt9471_get_usb_type_current(chip, &val->intval);
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
-		return rt9471_get_value_by_field_range(chip, F_ICHG_REG, RT9471_RANGE_ICHG, pvalue);
+		return rt9471_get_value_by_field_range(
+			chip, F_ICHG_REG, RT9471_RANGE_ICHG, &val->intval);
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
-		*pvalue = RT9471_ICHG_MAXUA;
+		val->intval = RT9471_ICHG_MAXUA;
 		return 0;
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
-		return rt9471_get_value_by_field_range(chip, F_VBAT_REG, RT9471_RANGE_VCHG, pvalue);
+		return rt9471_get_value_by_field_range(
+			chip, F_VBAT_REG, RT9471_RANGE_VCHG, &val->intval);
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
 		val->intval = RT9471_VCHG_MAXUV;
 		return 0;
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
-		return rt9471_get_value_by_field_range(chip, F_AICR, RT9471_RANGE_AICR, pvalue);
+		return rt9471_get_value_by_field_range(
+			chip, F_AICR, RT9471_RANGE_AICR, &val->intval);
 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
-		return rt9471_get_value_by_field_range(chip, F_MIVR, RT9471_RANGE_MIVR, pvalue);
+		return rt9471_get_value_by_field_range(
+			chip, F_MIVR, RT9471_RANGE_MIVR, &val->intval);
 	case POWER_SUPPLY_PROP_USB_TYPE:
-		return rt9471_get_usb_type(chip, pvalue);
+		return rt9471_get_usb_type(chip, &val->intval);
 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
-		return rt9471_get_value_by_field_range(chip, F_IPRE_CHG, RT9471_RANGE_IPRE, pvalue);
+		return rt9471_get_value_by_field_range(
+			chip, F_IPRE_CHG, RT9471_RANGE_IPRE, &val->intval);
 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
-		return rt9471_get_ieoc(chip, pvalue);
+		return rt9471_get_ieoc(chip, &val->intval);
 	case POWER_SUPPLY_PROP_MODEL_NAME:
 		val->strval = rt9471_model;
 		return 0;
Signature · 1 line
2.43.0
#8 lore raw

PATCH 5/10 power: supply: rt9471: Use POWER_SUPPLY_PROP_LOAD_SWITCH

In reply to Waqar Hameed

The framework supports controlling system load with POWER_SUPPLY_PROP_LOAD_SWITCH. Use this to control the BATFET, but keep old custom sysfs ABI for backward compatibility. However, add a note in ABI documentation that one should prefer the property load_switch instead.

---
 .../ABI/testing/sysfs-class-power-rt9471      |  2 +
 drivers/power/supply/rt9471.c                 | 52 ++++++++++++++++---
 2 files changed, 48 insertions(+), 6 deletions(-)
Documentation/ABI/testing/sysfs-class-power-rt9471 +2 -0
index 0a390ee5ac21c..90cf94c1cbf6b 100644
@@ -11,6 +11,8 @@ Description:
 		mode. 'Disable' also can help to leave it, but it's more like to abort
 		the action before the device really enter shipping mode.
 
+		Note: use /sys/class/power_supply/rt9471-*/load_switch instead.
+
 		Access: Read, Write
 		Valid values:
 		- 1: enabled
drivers/power/supply/rt9471.c +46 -6

98 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

PATCH 6/10 power: supply: rt9467: Use POWER_SUPPLY_PROP_LOAD_SWITCH

In reply to Waqar Hameed

The framework supports controlling system load with POWER_SUPPLY_PROP_LOAD_SWITCH. Use this to control the BATFET, but keep old custom sysfs ABI for backward compatibility. However, add a note in ABI documentation that one should prefer the property load_switch instead.

---
 .../ABI/testing/sysfs-class-power-rt9467      |  2 +
 drivers/power/supply/rt9467-charger.c         | 52 ++++++++++++++++---
 2 files changed, 48 insertions(+), 6 deletions(-)
Documentation/ABI/testing/sysfs-class-power-rt9467 +2 -0
index 619b7c45d145d..bfbfd78ec1c19 100644
@@ -13,6 +13,8 @@ Description:
 		'Disable' also can help to leave it, but it's more like to
 		abort the action before the device really enter shipping mode.
 
+		Note: use /sys/class/power_supply/rt9467-*/load_switch instead.
+
 		Access: Read, Write
 		Valid values:
 		- 1: enabled
drivers/power/supply/rt9467-charger.c +46 -6

98 lines not on this page · raw message

Signature · 1 line
2.43.0
#10 lore raw

PATCH 7/10 power: supply: bq24257: Use POWER_SUPPLY_PROP_LOAD_SWITCH

In reply to Waqar Hameed

The framework supports controlling system load with POWER_SUPPLY_PROP_LOAD_SWITCH. Use this to control SYSOFF, but keep old custom sysfs ABI for backward compatibility.

---
 drivers/power/supply/bq24257_charger.c | 41 +++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)
drivers/power/supply/bq24257_charger.c +40 -1
index 72f1bfea8d541..0ba690e9adb7b 100644
@@ -294,6 +294,40 @@ static int bq24257_set_input_current_limit(struct bq24257_device *bq,
 						    BQ24257_IILIMIT_MAP_SIZE));
 }
 
+static int bq24257_get_sys_switch(struct bq24257_device *bq,
+				  union power_supply_propval *val)
+{
+	int ret;
+
+	ret = bq24257_field_read(bq, F_SYSOFF);
+	if (ret < 0)
+		return ret;
+
+	val->intval = ret ? POWER_SUPPLY_LOAD_SWITCH_OFF :
+			    POWER_SUPPLY_LOAD_SWITCH_ON;
+
+	return 0;
+}
+
+static int bq24257_set_sys_switch(struct bq24257_device *bq,
+				  const union power_supply_propval *val)
+{
+	u8 regval;
+
+	switch (val->intval) {
+	case POWER_SUPPLY_LOAD_SWITCH_ON:
+		regval = 0;
+		break;
+	case POWER_SUPPLY_LOAD_SWITCH_OFF:
+		regval = 1;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return bq24257_field_write(bq, F_SYSOFF, regval);
+}
+
 static int bq24257_power_supply_get_property(struct power_supply *psy,
 					     enum power_supply_property psp,
 					     union power_supply_propval *val)
@@ -380,7 +414,8 @@ static int bq24257_power_supply_get_property(struct power_supply *psy,
 
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 		return bq24257_get_input_current_limit(bq, val);
-
+	case POWER_SUPPLY_PROP_LOAD_SWITCH:
+		return bq24257_get_sys_switch(bq, val);
 	default:
 		return -EINVAL;
 	}
@@ -397,6 +432,8 @@ static int bq24257_power_supply_set_property(struct power_supply *psy,
 	switch (prop) {
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 		return bq24257_set_input_current_limit(bq, val);
+	case POWER_SUPPLY_PROP_LOAD_SWITCH:
+		return bq24257_set_sys_switch(bq, val);
 	default:
 		return -EINVAL;
 	}
@@ -407,6 +444,7 @@ static int bq24257_power_supply_property_is_writeable(struct power_supply *psy,
 {
 	switch (psp) {
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+	case POWER_SUPPLY_PROP_LOAD_SWITCH:
 		return true;
 	default:
 		return false;
@@ -739,6 +777,7 @@ static enum power_supply_property bq24257_power_supply_props[] = {
 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
+	POWER_SUPPLY_PROP_LOAD_SWITCH,
 };
 
 static char *bq24257_charger_supplied_to[] = {
Signature · 1 line
2.43.0
#11 lore raw

PATCH 8/10 power: supply: bq24190: Remove unused watchdog struct field

In reply to Waqar Hameed

The field member watchdog in struct bq24190_dev_info is only set once in bq24190_set_config() during probe, and never read again. Remove this unnecessary field.

---
 drivers/power/supply/bq24190_charger.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/power/supply/bq24190_charger.c +0 -4
index 6700d578a98f3..6f8000cc700e7 100644
@@ -243,7 +243,6 @@ struct bq24190_dev_info {
 	struct mutex			f_reg_lock;
 	u8				f_reg;
 	u8				ss_reg;
-	u8				watchdog;
 	const struct bq24190_chip_info	*info;
 };
 
@@ -756,9 +755,6 @@ static int bq24190_set_config(struct bq24190_dev_info *bdi)
 	if (ret < 0)
 		return ret;
 
-	bdi->watchdog = ((v & BQ24190_REG_CTTC_WATCHDOG_MASK) >>
-					BQ24190_REG_CTTC_WATCHDOG_SHIFT);
-
 	/*
 	 * According to the "Host Mode and default Mode" section of the
 	 * manual, a write to any register causes the bq24190 to switch
Signature · 1 line
2.43.0
#12 lore raw

PATCH 9/10 power: supply: bq24190: Disable watchdog with bq24190_write_mask()

In reply to Waqar Hameed

The watchdog is disabled by updating the register field with a bq24190_read() and bq24190_write(). Combine this instead with bq24190_write_mask(..., 0).

---
 drivers/power/supply/bq24190_charger.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/power/supply/bq24190_charger.c +3 -7
index 6f8000cc700e7..987ef0e29eb15 100644
@@ -751,10 +751,6 @@ static int bq24190_set_config(struct bq24190_dev_info *bdi)
 	int ret;
 	u8 v;
 
-	ret = bq24190_read(bdi, BQ24190_REG_CTTC, &v);
-	if (ret < 0)
-		return ret;
-
 	/*
 	 * According to the "Host Mode and default Mode" section of the
 	 * manual, a write to any register causes the bq24190 to switch
@@ -763,9 +759,9 @@ static int bq24190_set_config(struct bq24190_dev_info *bdi)
 	 * So, by simply turning off the WDT, we accomplish both with the
 	 * same write.
 	 */
-	v &= ~BQ24190_REG_CTTC_WATCHDOG_MASK;
-
-	ret = bq24190_write(bdi, BQ24190_REG_CTTC, v);
+	ret = bq24190_write_mask(bdi, BQ24190_REG_CTTC,
+				 BQ24190_REG_CTTC_WATCHDOG_MASK,
+				 BQ24190_REG_CTTC_WATCHDOG_SHIFT, 0);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
2.43.0
#13 lore raw

PATCH 10/10 power: supply: bq24190: Use POWER_SUPPLY_PROP_LOAD_SWITCH

In reply to Waqar Hameed

The online-property has historically been (ab)used for controlling the BATFET in this driver. The framework supports controlling system load with POWER_SUPPLY_PROP_LOAD_SWITCH. Use this to control the BATFET, but keep the support for online in order to have ABI backward compatibility.

Moreover, don't bother with converting the online-functions to use the new charger_get/set_batfet_ctrl() to keep the code more readable. It's already quite messy, e.g. charge_set_online() calls battery_set_online().

---
 drivers/power/supply/bq24190_charger.c | 60 ++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
drivers/power/supply/bq24190_charger.c +60 -0
index 987ef0e29eb15..1e77816c49a3b 100644
@@ -1295,6 +1295,58 @@ static int bq24190_charger_set_iinlimit(struct bq24190_dev_info *bdi,
 			ARRAY_SIZE(bq24190_isc_iinlim_values), val->intval);
 }
 
+static int bq24190_charger_get_batfet_ctrl(struct bq24190_dev_info *bdi,
+					   union power_supply_propval *val)
+{
+	u8 regval;
+	int ret;
+
+	ret = bq24190_read_mask(bdi, BQ24190_REG_MOC,
+				BQ24190_REG_MOC_BATFET_DISABLE_MASK,
+				BQ24190_REG_MOC_BATFET_DISABLE_SHIFT,
+				&regval);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Datasheet defines shipping mode as BATFET off _and_ the watchdog
+	 * disabled. Since the watchdog is disabled during the whole lifetime
+	 * (from probe), shipping mode is the only "off" state that can be
+	 * achievable.
+	 */
+	val->intval = regval ? POWER_SUPPLY_LOAD_SWITCH_SHIP :
+			       POWER_SUPPLY_LOAD_SWITCH_ON;
+	return 0;
+}
+
+static int
+bq24190_charger_set_batfet_ctrl(struct bq24190_dev_info *bdi,
+				const union power_supply_propval *val)
+{
+	u8 regval;
+
+	/*
+	 * Datasheet defines shipping mode as BATFET off _and_ the watchdog
+	 * disabled. Since the watchdog is disabled during the whole lifetime
+	 * (from probe), shipping mode is the only "off" state that can be
+	 * achievable.
+	 */
+	switch (val->intval) {
+	case POWER_SUPPLY_LOAD_SWITCH_ON:
+		regval = 0;
+		break;
+	case POWER_SUPPLY_LOAD_SWITCH_SHIP:
+		regval = 1;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return bq24190_write_mask(bdi, BQ24190_REG_MOC,
+				  BQ24190_REG_MOC_BATFET_DISABLE_MASK,
+				  BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, regval);
+}
+
 static int bq24190_charger_get_property(struct power_supply *psy,
 		enum power_supply_property psp, union power_supply_propval *val)
 {
@@ -1351,6 +1403,9 @@ static int bq24190_charger_get_property(struct power_supply *psy,
 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
 		ret = 0;
 		break;
+	case POWER_SUPPLY_PROP_LOAD_SWITCH:
+		ret = bq24190_charger_get_batfet_ctrl(bdi, val);
+		break;
 	case POWER_SUPPLY_PROP_MODEL_NAME:
 		val->strval = bdi->model_name;
 		ret = 0;
@@ -1401,6 +1456,9 @@ static int bq24190_charger_set_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 		ret = bq24190_charger_set_iinlimit(bdi, val);
 		break;
+	case POWER_SUPPLY_PROP_LOAD_SWITCH:
+		ret = bq24190_charger_set_batfet_ctrl(bdi, val);
+		break;
 	default:
 		ret = -EINVAL;
 	}
@@ -1421,6 +1479,7 @@ static int bq24190_charger_property_is_writeable(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+	case POWER_SUPPLY_PROP_LOAD_SWITCH:
 		return 1;
 	default:
 		return 0;
@@ -1481,6 +1540,7 @@ static enum power_supply_property bq24190_charger_properties[] = {
 	POWER_SUPPLY_PROP_SCOPE,
 	POWER_SUPPLY_PROP_MODEL_NAME,
 	POWER_SUPPLY_PROP_MANUFACTURER,
+	POWER_SUPPLY_PROP_LOAD_SWITCH,
 };
 
 static char *bq24190_charger_supplied_to[] = {
Signature · 1 line
2.43.0
#14 lore raw

Re: (subset) [PATCH 00/10] Add sysfs entry for system load control

In reply to Waqar Hameed
Quoted 5 lines · Waqar Hameed

During discussions of the development of a new driver [1], it was concluded that a new sysfs ABI for controlling the connection from power to system load is wanted (often called BATFET). This patchset introduces such one.

Quoted 7 lines

There are already several drivers that are controlling this, but with their own custom sysfs entries. The drivers in this patchset were find with a simple grep for bat\s?fet (there might be more?), and then converted to use this new sysfs ABI. The old custom ABIs are left untouched of course, for backward compatibility.

[...]

Applied, thanks!

[04/10] power: supply: rt9471: Remove superfluous unpacking of propval

        commit: 45413165bef33bb7d5544a3331f39492cdde9418
[08/10] power: supply: bq24190: Remove unused watchdog struct field
        commit: 0e2a6f7c0fa69293fa6f53dcf3406a9d7a1b1816
[09/10] power: supply: bq24190: Disable watchdog with bq24190_write_mask()
        commit: 50ffe6eedbd8d1ff8fbc2b451bbade8ca3063487

Best regards,

Signature · 1 line
Sebastian Reichel <[email protected]>