Skip to contentSkip to conversation

usb: typec: ucsi: Expand power supply support

PATCH Superseded Jameson Thies 2 participants 7 messages Jul 17, 2024 – Jul 22, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover usb: typec: ucsi: Expand power supply support

Hi Heikki,

This series makes the following updates to the UCSI power supply driver.

1. Adds support for the power supply status property.
2. Updates the driver to distinguish between PD and PD DRP power supply types.
3. Adds the charge control limit max property which can be used to request a PR swap from sysfs.
4. Fixes a simple SET_PRD typo in the ucsi.h header.

I've checked that the series builds on top of the usb-next branch and manually tested functionality on top of a 6.10-rc5 ChromeOS kernel. Let me know if you have any questions.

Thanks,
Jameson

Jameson Thies (4):
usb: typec: ucsi: Add status to ucsi power supply driver
usb: typec: ucsi: Add USB PD DRP to USB type
usb: typec: ucsi: Set power role based on UCSI charge control
usb: typec: ucsi: Fix SET_PDR typo in UCSI header file

 drivers/usb/typec/ucsi/psy.c  | 72 ++++++++++++++++++++++++++++++++++-
 drivers/usb/typec/ucsi/ucsi.h |  2 +-
 2 files changed, 71 insertions(+), 3 deletions(-)

base-commit: b727493011123db329e2901e3abf81a8d146b6fe

Signature · 1 line
2.45.2.1089.g2a221341d9-goog
#1 lore raw

PATCH 1/4 usb: typec: ucsi: Add status to UCSI power supply driver

Add status to UCSI power supply driver properties based on the port's connection and power direction states.

---
 drivers/usb/typec/ucsi/psy.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/usb/typec/ucsi/psy.c +17 -0
index e623d80e177c..d0b52cee41d2 100644
@@ -29,6 +29,7 @@ static enum power_supply_property ucsi_psy_props[] = {
 	POWER_SUPPLY_PROP_CURRENT_MAX,
 	POWER_SUPPLY_PROP_CURRENT_NOW,
 	POWER_SUPPLY_PROP_SCOPE,
+	POWER_SUPPLY_PROP_STATUS,
 };
 
 static int ucsi_psy_get_scope(struct ucsi_connector *con,
@@ -51,6 +52,20 @@ static int ucsi_psy_get_scope(struct ucsi_connector *con,
 	return 0;
 }
 
+static int ucsi_psy_get_status(struct ucsi_connector *con,
+			       union power_supply_propval *val)
+{
+	val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+	if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
+		if ((con->status.flags & UCSI_CONSTAT_PWR_DIR) == TYPEC_SINK)
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+		else
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+	}
+
+	return 0;
+}
+
 static int ucsi_psy_get_online(struct ucsi_connector *con,
 			       union power_supply_propval *val)
 {
@@ -249,6 +264,8 @@ static int ucsi_psy_get_prop(struct power_supply *psy,
 		return ucsi_psy_get_current_now(con, val);
 	case POWER_SUPPLY_PROP_SCOPE:
 		return ucsi_psy_get_scope(con, val);
+	case POWER_SUPPLY_PROP_STATUS:
+		return ucsi_psy_get_status(con, val);
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
#2 lore raw

PATCH 2/4 usb: typec: ucsi: Add USB PD DRP to USB type

In reply to Jameson Thies

Add POWER_SUPPLY_USB_TYPE_PD_DRP as a USB type in the UCSI power supply driver. The DRP type is set when the partner supports USB PD and offers both source and sink PDOs.

---
 drivers/usb/typec/ucsi/psy.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/usb/typec/ucsi/psy.c +7 -2
index d0b52cee41d2..45113e013696 100644
@@ -204,8 +204,12 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
 
 	val->intval = POWER_SUPPLY_USB_TYPE_C;
 	if (flags & UCSI_CONSTAT_CONNECTED &&
-	    UCSI_CONSTAT_PWR_OPMODE(flags) == UCSI_CONSTAT_PWR_OPMODE_PD)
-		val->intval = POWER_SUPPLY_USB_TYPE_PD;
+	    UCSI_CONSTAT_PWR_OPMODE(flags) == UCSI_CONSTAT_PWR_OPMODE_PD) {
+		if (!con->partner_source_caps || !con->partner_sink_caps)
+			val->intval = POWER_SUPPLY_USB_TYPE_PD;
+		else
+			val->intval = POWER_SUPPLY_USB_TYPE_PD_DRP;
+	}
 
 	return 0;
 }
@@ -275,6 +279,7 @@ static enum power_supply_usb_type ucsi_psy_usb_types[] = {
 	POWER_SUPPLY_USB_TYPE_C,
 	POWER_SUPPLY_USB_TYPE_PD,
 	POWER_SUPPLY_USB_TYPE_PD_PPS,
+	POWER_SUPPLY_USB_TYPE_PD_DRP,
 };
 
 int ucsi_register_port_psy(struct ucsi_connector *con)
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
#3 lore raw

PATCH 3/4 usb: typec: ucsi: Set power role based on UCSI charge control

In reply to Jameson Thies

Add POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX as a property to the UCSI power supply driver. When set to a negative value, set power role to TYPEC_SOURCE, otherwise set the power role to TYPEC_SINK.

---
 drivers/usb/typec/ucsi/psy.c | 46 ++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
drivers/usb/typec/ucsi/psy.c +46 -0
index 45113e013696..feb344cb7ac8 100644
@@ -30,6 +30,7 @@ static enum power_supply_property ucsi_psy_props[] = {
 	POWER_SUPPLY_PROP_CURRENT_NOW,
 	POWER_SUPPLY_PROP_SCOPE,
 	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
 };
 
 static int ucsi_psy_get_scope(struct ucsi_connector *con,
@@ -270,11 +271,54 @@ static int ucsi_psy_get_prop(struct power_supply *psy,
 		return ucsi_psy_get_scope(con, val);
 	case POWER_SUPPLY_PROP_STATUS:
 		return ucsi_psy_get_status(con, val);
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
+		val->intval = 0;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int ucsi_psy_set_charge_control_limit_max(struct ucsi_connector *con,
+				 const union power_supply_propval *val)
+{
+	enum typec_role role;
+	/*
+	 * Writing a negative value to the charge control limit max implies the
+	 * port should not accept charge. Set the power role to source for a
+	 * negative charge control limit, and sink otherwise.
+	 */
+	if (val->intval < 0)
+		role = TYPEC_SOURCE;
+	else
+		role = TYPEC_SINK;
+
+	if (!con->typec_cap.ops || !con->typec_cap.ops->pr_set)
+		return -EINVAL;
+
+	return con->typec_cap.ops->pr_set(con->port, role);
+}
+
+static int ucsi_psy_set_prop(struct power_supply *psy,
+			     enum power_supply_property psp,
+			     const union power_supply_propval *val)
+{
+	struct ucsi_connector *con = power_supply_get_drvdata(psy);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
+		return ucsi_psy_set_charge_control_limit_max(con, val);
 	default:
 		return -EINVAL;
 	}
 }
 
+static int ucsi_psy_prop_is_writeable(struct power_supply *psy,
+			     enum power_supply_property psp)
+{
+	return psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX;
+}
+
 static enum power_supply_usb_type ucsi_psy_usb_types[] = {
 	POWER_SUPPLY_USB_TYPE_C,
 	POWER_SUPPLY_USB_TYPE_PD,
@@ -303,6 +347,8 @@ int ucsi_register_port_psy(struct ucsi_connector *con)
 	con->psy_desc.properties = ucsi_psy_props;
 	con->psy_desc.num_properties = ARRAY_SIZE(ucsi_psy_props);
 	con->psy_desc.get_property = ucsi_psy_get_prop;
+	con->psy_desc.set_property = ucsi_psy_set_prop;
+	con->psy_desc.property_is_writeable = ucsi_psy_prop_is_writeable;
 
 	con->psy = power_supply_register(dev, &con->psy_desc, &psy_cfg);
 
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
#4 lore raw

Re: [PATCH v1 3/4] usb: typec: ucsi: Set power role based on UCSI charge control

Hi everyone.
After some internal discussion on this patch, I'm going to update it to send SET_SINK_PATH when handling a charge_control_limit_max write. The intention here is to use this for selecting a charge port and calling "pr_set" alone won't always work. I'll upload a v2 series with this change shortly, let me know if you have any questions.

Thanks,
Jameson

#5 lore raw

PATCH 4/4 usb: typec: ucsi: Fix SET_PDR typo in UCSI header file

In reply to Jameson Thies

Fix SET_PDR typo in UCSI header file.

---
 drivers/usb/typec/ucsi/ucsi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/ucsi/ucsi.h +1 -1
index 57129f3c0814..375f1881c1e2 100644
@@ -152,7 +152,7 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num);
 #define UCSI_SET_UOR_ROLE(_r_)		(((_r_) == TYPEC_HOST ? 1 : 2) << 23)
 #define UCSI_SET_UOR_ACCEPT_ROLE_SWAPS		BIT(25)
 
-/* SET_PDF command bits */
+/* SET_PDR command bits */
 #define UCSI_SET_PDR_ROLE(_r_)		(((_r_) == TYPEC_SOURCE ? 1 : 2) << 23)
 #define UCSI_SET_PDR_ACCEPT_ROLE_SWAPS		BIT(25)
 
Signature · 1 line
2.45.2.1089.g2a221341d9-goog
Reviewed-by on v1 4/4
#6 lore raw

Re: [PATCH v1 4/4] usb: typec: ucsi: Fix SET_PDR typo in UCSI header file

Quoted 12 lines · Jameson Thies

Fix SET_PDR typo in UCSI header file.

---
  drivers/usb/typec/ucsi/ucsi.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/ucsi/ucsi.h +0 -0
index 57129f3c0814..375f1881c1e2 100644

drivers/usb/typec/ucsi/ucsi.h void ucsi_connector_change(struct ucsi *ucsi, u8 num); · line 158

Quoted 8 lines
drivers/usb/typec/ucsi/ucsi.h +1 -1
@@ -152,7 +152,7 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num);
  #define UCSI_SET_UOR_ROLE(_r_)		(((_r_) == TYPEC_HOST ? 1 : 2) << 23)
  #define UCSI_SET_UOR_ACCEPT_ROLE_SWAPS		BIT(25)
 
-/* SET_PDF command bits */
+/* SET_PDR command bits */
  #define UCSI_SET_PDR_ROLE(_r_)		(((_r_) == TYPEC_SOURCE ? 1 : 2) << 23)
  #define UCSI_SET_PDR_ACCEPT_ROLE_SWAPS		BIT(25)