Skip to contentSkip to conversation

UCSI Power Supply Updates and Bug Fixes

PATCH Superseded Jameson Thies 5 participants 27 messages Oct 7, 2025 – Oct 14, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover UCSI Power Supply Updates and Bug Fixes

This series includes the following minor changes to power supply handling by the UCSI driver.

1) Adds the "Status" property to power supplies registered by the UCSI driver.
2) Reports power supply USB type as "DRP" when the port partner is a DRP.
3) Updates ucsi.c to report a power supply change all power opmode changes. Currently this only gets reported when opmode is PD.
4) Updates ucsi.c to report a power supply change when the PPM signals a sink path change.
5) Set max current to 0 when no device is connected. Currently this defaults to 0.1A.
6) Updates connector status after initiating a PR swap. Following the UCSI spec, there is no reported change following a successful SET_PDR command. This results in the stored connector status being outdated.

Abhishek Pandit-Subedi (1):
usb: typec: ucsi: pr_swap should check connector_status

Jameson Thies (5):
usb: typec: ucsi: psy: Add power supply status
usb: typec: ucsi: psy: Add support for DRP USB type
usb: typec: ucsi: Report power supply changes on power opmode changes
usb: typec: ucsi: Report power supply change on sink path change
usb: typec: ucsi: psy: Set max current to zero when disconnected

 drivers/usb/typec/ucsi/psy.c  | 41 ++++++++++++++++++++++++++++++++++-
 drivers/usb/typec/ucsi/ucsi.c | 35 +++++++++++++++++++++++++-----
 drivers/usb/typec/ucsi/ucsi.h |  4 ++++
 3 files changed, 73 insertions(+), 7 deletions(-)

base-commit: e40b984b6c4ce3f80814f39f86f87b2a48f2e662

Signature · 1 line
2.51.0.618.g983fd99d29-goog
#1 lore raw

PATCH 1/6 usb: typec: ucsi: psy: Add power supply status

Add support for power supply status. If a port is acting as a sink with the sink path enabled, report it is charging. If a port is source, report it is discharging. If there is no connection or the port hasn't enabled the sink path, report not charging.

---
 drivers/usb/typec/ucsi/psy.c  | 26 ++++++++++++++++++++++++++
 drivers/usb/typec/ucsi/ucsi.h |  3 +++
 2 files changed, 29 insertions(+)
drivers/usb/typec/ucsi/psy.c +26 -0
index 62a9d68bb66d..2b0225821502 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,29 @@ 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)
+{
+	bool is_sink = UCSI_CONSTAT(con, PWR_DIR) == TYPEC_SINK;
+	bool sink_path_enabled = true;
+
+	val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+
+	if (con->ucsi->version >= UCSI_VERSION_2_0)
+		sink_path_enabled =
+			UCSI_CONSTAT(con, SINK_PATH_STATUS_V2_0) ==
+			UCSI_CONSTAT_SINK_PATH_ENABLED;
+
+	if (UCSI_CONSTAT(con, CONNECTED)) {
+		if (is_sink && sink_path_enabled)
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+		else if (!is_sink)
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+	}
+
+	return 0;
+}
+
 static int ucsi_psy_get_online(struct ucsi_connector *con,
 			       union power_supply_propval *val)
 {
@@ -245,6 +269,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;
 	}
drivers/usb/typec/ucsi/ucsi.h +3 -0
index e301d9012936..cce93af7461b 100644
@@ -360,6 +360,9 @@ struct ucsi_cable_property {
 #define   UCSI_CONSTAT_BC_SLOW_CHARGING		2
 #define   UCSI_CONSTAT_BC_TRICKLE_CHARGING	3
 #define UCSI_CONSTAT_PD_VERSION_V1_2		UCSI_DECLARE_BITFIELD_V1_2(70, 16)
+#define UCSI_CONSTAT_SINK_PATH_STATUS_V2_0	UCSI_DECLARE_BITFIELD_V2_0(87, 1)
+#define   UCSI_CONSTAT_SINK_PATH_DISABLED   0
+#define   UCSI_CONSTAT_SINK_PATH_ENABLED    1
 #define UCSI_CONSTAT_PWR_READING_READY_V2_1	UCSI_DECLARE_BITFIELD_V2_1(89, 1)
 #define UCSI_CONSTAT_CURRENT_SCALE_V2_1		UCSI_DECLARE_BITFIELD_V2_1(90, 3)
 #define UCSI_CONSTAT_PEAK_CURRENT_V2_1		UCSI_DECLARE_BITFIELD_V2_1(93, 16)
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#3 lore raw

PATCH 2/6 usb: typec: ucsi: psy: Add support for DRP USB type

In reply to Jameson Thies

The USB Type registered with the power supply class is based on the current power operation mode of the port. When the port is using USB PD and is connected to a DRP partner, report that the power supply's USB type is USB PD DRP.

---
 drivers/usb/typec/ucsi/psy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/usb/typec/ucsi/psy.c +9 -1
index 2b0225821502..985a90d3f898 100644
@@ -209,8 +209,16 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
 {
 	val->intval = POWER_SUPPLY_USB_TYPE_C;
 	if (UCSI_CONSTAT(con, CONNECTED) &&
-	    UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD)
+	    UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
+		for (int i = 0; i < con->num_pdos; i++) {
+			if (pdo_type(con->src_pdos[i]) == PDO_TYPE_FIXED &&
+			    con->src_pdos[i] & PDO_FIXED_DUAL_ROLE) {
+				val->intval = POWER_SUPPLY_USB_TYPE_PD_DRP;
+				return 0;
+			}
+		}
 		val->intval = POWER_SUPPLY_USB_TYPE_PD;
+	}
 
 	return 0;
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#7 lore raw

PATCH 3/6 usb: typec: ucsi: Report power supply changes on power opmode changes

In reply to Jameson Thies

Report opmode changes from the PPM to the power supply class by calling ucsi_port_psy_changed(). If the current opmode is USB PD, do not call ucsi_port_psy_changed(). The power supply class will be updated after requesting partner source PDOs.

---
 drivers/usb/typec/ucsi/ucsi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/typec/ucsi/ucsi.c +3 -0
index 3f568f790f39..7b718049d0d1 100644
@@ -1022,14 +1022,17 @@ static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
+		ucsi_port_psy_changed(con);
 		break;
 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
+		ucsi_port_psy_changed(con);
 		break;
 	default:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
+		ucsi_port_psy_changed(con);
 		break;
 	}
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#10 lore raw

PATCH 4/6 usb: typec: ucsi: Report power supply change on sink path change

In reply to Jameson Thies

Update the UCSI interface driver to report a power supply change when the PPM sets the Sink Path Change bit.

---
 drivers/usb/typec/ucsi/ucsi.c | 2 +-
 drivers/usb/typec/ucsi/ucsi.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/usb/typec/ucsi/ucsi.c +1 -1
index 7b718049d0d1..1a7d850b11ea 100644
@@ -1293,7 +1293,7 @@ static void ucsi_handle_connector_change(struct work_struct *work)
 	if (change & UCSI_CONSTAT_CAM_CHANGE)
 		ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
 
-	if (change & UCSI_CONSTAT_BC_CHANGE)
+	if (change & UCSI_CONSTAT_BC_CHANGE || change & UCSI_CONSTAT_SINK_PATH_CHANGE)
 		ucsi_port_psy_changed(con);
 
 	if (con->ucsi->version >= UCSI_VERSION_2_1 &&
drivers/usb/typec/ucsi/ucsi.h +1 -0
index cce93af7461b..35993bc34d4d 100644
@@ -382,6 +382,7 @@ struct ucsi_cable_property {
 #define UCSI_CONSTAT_BC_CHANGE			BIT(9)
 #define UCSI_CONSTAT_PARTNER_CHANGE		BIT(11)
 #define UCSI_CONSTAT_POWER_DIR_CHANGE		BIT(12)
+#define UCSI_CONSTAT_SINK_PATH_CHANGE		BIT(13)
 #define UCSI_CONSTAT_CONNECT_CHANGE		BIT(14)
 #define UCSI_CONSTAT_ERROR			BIT(15)
 
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#14 lore raw

PATCH 5/6 usb: typec: ucsi: psy: Set max current to zero when disconnected

In reply to Jameson Thies

The ucsi_psy_get_current_max function defaults to 0.1A when it is not clear how much current the partner device can support. But this does not check the port is connected, and will report 0.1A max current when nothing is connected. Update ucsi_psy_get_current_max to report 0A when there is no connection.

---
 drivers/usb/typec/ucsi/psy.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/typec/ucsi/psy.c +5 -0
index 985a90d3f898..3a209de55c14 100644
@@ -169,6 +169,11 @@ static int ucsi_psy_get_current_max(struct ucsi_connector *con,
 {
 	u32 pdo;
 
+	if (!UCSI_CONSTAT(con, CONNECTED)) {
+		val->intval = 0;
+		return 0;
+	}
+
 	switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
 	case UCSI_CONSTAT_PWR_OPMODE_PD:
 		if (con->num_pdos > 0) {
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#17 lore raw

PATCH 6/6 usb: typec: ucsi: pr_swap should check connector_status

In reply to Jameson Thies

From: Abhishek Pandit-Subedi <[email protected]>

Power role swaps initiated by the host system doesn't generate connection status change notifications.

From UCSIv3.0 spec, section 6.5.10 Set Power Direction Role:

The execution of this command might require PPM to initiate a power role swap. If the power role swap fails for any reason, the command returns, and error and the power direction should remain unchanged. Note that if the execution of the command resulted in a successful power role swap, it should not result in a connector status change notification.

---
 drivers/usb/typec/ucsi/ucsi.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +25 -5
index 1a7d850b11ea..6e3797d7a144 100644
@@ -1526,20 +1526,40 @@ static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
 	if (ret < 0)
 		goto out_unlock;
 
-	mutex_unlock(&con->lock);
+	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
+	ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
+	if (ret < 0)
+		goto out_unlock;
 
-	if (!wait_for_completion_timeout(&con->complete,
-					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
-		return -ETIMEDOUT;
+	cur_role = !!UCSI_CONSTAT(con, PWR_DIR);
 
-	mutex_lock(&con->lock);
+	/* Execution of SET_PDR should not result in connector status
+	 * notifications. However, some legacy implementations may still defer
+	 * the actual role swap and return immediately. Thus, check the
+	 * connector status in case it immediately succeeded or wait for a later
+	 * connector status change.
+	 */
+	if (cur_role != role) {
+		mutex_unlock(&con->lock);
+
+		if (!wait_for_completion_timeout(
+			    &con->complete,
+			    msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
+			return -ETIMEDOUT;
+
+		mutex_lock(&con->lock);
+	}
 
 	/* Something has gone wrong while swapping the role */
 	if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) {
 		ucsi_reset_connector(con, true);
 		ret = -EPROTO;
+		goto out_unlock;
 	}
 
+	/* Indicate successful power role swap */
+	typec_set_pwr_role(con->port, role);
+
 out_unlock:
 	mutex_unlock(&con->lock);
 
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#4 lore raw

Re: [PATCH 2/6] usb: typec: ucsi: psy: Add support for DRP USB type

In reply to Jameson Thies

Hi Jameson,

Quick nit about looking through and finding the Dual-Role Power bit.

Quoted 15 lines · Jameson Thies

The USB Type registered with the power supply class is based on the current power operation mode of the port. When the port is using USB PD and is connected to a DRP partner, report that the power supply's USB type is USB PD DRP.

---
 drivers/usb/typec/ucsi/psy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/usb/typec/ucsi/psy.c +0 -0
index 2b0225821502..985a90d3f898 100644

drivers/usb/typec/ucsi/psy.c static int ucsi_psy_get_usb_type(struct ucsi_connector *con, · line 213

Quoted 6 lines
drivers/usb/typec/ucsi/psy.c +2 -1
@@ -209,8 +209,16 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
 {
 	val->intval = POWER_SUPPLY_USB_TYPE_C;
 	if (UCSI_CONSTAT(con, CONNECTED) &&
-	    UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD)
+	    UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
+		for (int i = 0; i < con->num_pdos; i++) {

Do you really need to iterate through the whole list of pdos to find this?

The DRP bit is guaranteed to be always in the 5V PDO, which is guaranteed to be the 0th one.

See USB PD R3.2 V1.1 Section 6.4.1.3.1 Sink Fixed Supply Power Data Object

"Since all USB Consumers support vSafe5V, the required vSafe5V Fixed Supply Power Data Object is also used to convey additional information that is returned in bits 29 through 20. All other Fixed Supply Power Data Objects Shall set bits 29…20 to zero."

Quoted 14 lines
drivers/usb/typec/ucsi/psy.c +7 -0
+			if (pdo_type(con->src_pdos[i]) == PDO_TYPE_FIXED &&
+			    con->src_pdos[i] & PDO_FIXED_DUAL_ROLE) {
+				val->intval = POWER_SUPPLY_USB_TYPE_PD_DRP;
+				return 0;
+			}
+		}
 		val->intval = POWER_SUPPLY_USB_TYPE_PD;
+	}
 
 	return 0;
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 3/6
#8 lore raw

Re: [PATCH 3/6] usb: typec: ucsi: Report power supply changes on power opmode changes

In reply to Jameson Thies
Quoted 6 lines · Jameson Thies

Report opmode changes from the PPM to the power supply class by calling ucsi_port_psy_changed(). If the current opmode is USB PD, do not call ucsi_port_psy_changed(). The power supply class will be updated after requesting partner source PDOs.

Quoted 29 lines
---
 drivers/usb/typec/ucsi/ucsi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/typec/ucsi/ucsi.c +3 -0
index 3f568f790f39..7b718049d0d1 100644
@@ -1022,14 +1022,17 @@ static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
+		ucsi_port_psy_changed(con);
 		break;
 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
+		ucsi_port_psy_changed(con);
 		break;
 	default:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
+		ucsi_port_psy_changed(con);
 		break;
 	}
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 4/6
#11 lore raw

Re: [PATCH 4/6] usb: typec: ucsi: Report power supply change on sink path change

In reply to Jameson Thies
Quoted 4 lines · Jameson Thies

Update the UCSI interface driver to report a power supply change when the PPM sets the Sink Path Change bit.

Quoted 33 lines
---
 drivers/usb/typec/ucsi/ucsi.c | 2 +-
 drivers/usb/typec/ucsi/ucsi.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/usb/typec/ucsi/ucsi.c +1 -1
index 7b718049d0d1..1a7d850b11ea 100644
@@ -1293,7 +1293,7 @@ static void ucsi_handle_connector_change(struct work_struct *work)
 	if (change & UCSI_CONSTAT_CAM_CHANGE)
 		ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
 
-	if (change & UCSI_CONSTAT_BC_CHANGE)
+	if (change & UCSI_CONSTAT_BC_CHANGE || change & UCSI_CONSTAT_SINK_PATH_CHANGE)
 		ucsi_port_psy_changed(con);
 
 	if (con->ucsi->version >= UCSI_VERSION_2_1 &&
drivers/usb/typec/ucsi/ucsi.h +1 -0
index cce93af7461b..35993bc34d4d 100644
@@ -382,6 +382,7 @@ struct ucsi_cable_property {
 #define UCSI_CONSTAT_BC_CHANGE			BIT(9)
 #define UCSI_CONSTAT_PARTNER_CHANGE		BIT(11)
 #define UCSI_CONSTAT_POWER_DIR_CHANGE		BIT(12)
+#define UCSI_CONSTAT_SINK_PATH_CHANGE		BIT(13)
 #define UCSI_CONSTAT_CONNECT_CHANGE		BIT(14)
 #define UCSI_CONSTAT_ERROR			BIT(15)
 
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 5/6
#15 lore raw

Re: [PATCH 5/6] usb: typec: ucsi: psy: Set max current to zero when disconnected

In reply to Jameson Thies
Quoted 7 lines · Jameson Thies

The ucsi_psy_get_current_max function defaults to 0.1A when it is not clear how much current the partner device can support. But this does not check the port is connected, and will report 0.1A max current when nothing is connected. Update ucsi_psy_get_current_max to report 0A when there is no connection.

Quoted 23 lines
---
 drivers/usb/typec/ucsi/psy.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/typec/ucsi/psy.c +5 -0
index 985a90d3f898..3a209de55c14 100644
@@ -169,6 +169,11 @@ static int ucsi_psy_get_current_max(struct ucsi_connector *con,
 {
 	u32 pdo;
 
+	if (!UCSI_CONSTAT(con, CONNECTED)) {
+		val->intval = 0;
+		return 0;
+	}
+
 	switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
 	case UCSI_CONSTAT_PWR_OPMODE_PD:
 		if (con->num_pdos > 0) {
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 6/6
#18 lore raw

Re: [PATCH 6/6] usb: typec: ucsi: pr_swap should check connector_status

In reply to Jameson Thies
Quoted 14 lines · Jameson Thies

From: Abhishek Pandit-Subedi <[email protected]>

Power role swaps initiated by the host system doesn't generate connection status change notifications.

From UCSIv3.0 spec, section 6.5.10 Set Power Direction Role:

The execution of this command might require PPM to initiate a power role swap. If the power role swap fails for any reason, the command returns, and error and the power direction should remain unchanged. Note that if the execution of the command resulted in a successful power role swap, it should not result in a connector status change notification.

Quoted 2 lines
Quoted 57 lines
---
 drivers/usb/typec/ucsi/ucsi.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +25 -5
index 1a7d850b11ea..6e3797d7a144 100644
@@ -1526,20 +1526,40 @@ static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
 	if (ret < 0)
 		goto out_unlock;
 
-	mutex_unlock(&con->lock);
+	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
+	ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
+	if (ret < 0)
+		goto out_unlock;
 
-	if (!wait_for_completion_timeout(&con->complete,
-					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
-		return -ETIMEDOUT;
+	cur_role = !!UCSI_CONSTAT(con, PWR_DIR);
 
-	mutex_lock(&con->lock);
+	/* Execution of SET_PDR should not result in connector status
+	 * notifications. However, some legacy implementations may still defer
+	 * the actual role swap and return immediately. Thus, check the
+	 * connector status in case it immediately succeeded or wait for a later
+	 * connector status change.
+	 */
+	if (cur_role != role) {
+		mutex_unlock(&con->lock);
+
+		if (!wait_for_completion_timeout(
+			    &con->complete,
+			    msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
+			return -ETIMEDOUT;
+
+		mutex_lock(&con->lock);
+	}
 
 	/* Something has gone wrong while swapping the role */
 	if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) {
 		ucsi_reset_connector(con, true);
 		ret = -EPROTO;
+		goto out_unlock;
 	}
 
+	/* Indicate successful power role swap */
+	typec_set_pwr_role(con->port, role);
+
 out_unlock:
 	mutex_unlock(&con->lock);
 
Signature · 1 line
2.51.0.618.g983fd99d29-goog
maintainer Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] usb: typec: ucsi: psy: Add power supply status

In reply to Jameson Thies
Quoted 6 lines · Jameson Thies

Add support for power supply status. If a port is acting as a sink with the sink path enabled, report it is charging. If a port is source, report it is discharging. If there is no connection or the port hasn't enabled the sink path, report not charging.

Quoted 72 lines
---
 drivers/usb/typec/ucsi/psy.c  | 26 ++++++++++++++++++++++++++
 drivers/usb/typec/ucsi/ucsi.h |  3 +++
 2 files changed, 29 insertions(+)
drivers/usb/typec/ucsi/psy.c +26 -0
index 62a9d68bb66d..2b0225821502 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,29 @@ 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)
+{
+	bool is_sink = UCSI_CONSTAT(con, PWR_DIR) == TYPEC_SINK;
+	bool sink_path_enabled = true;
+
+	val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+
+	if (con->ucsi->version >= UCSI_VERSION_2_0)
+		sink_path_enabled =
+			UCSI_CONSTAT(con, SINK_PATH_STATUS_V2_0) ==
+			UCSI_CONSTAT_SINK_PATH_ENABLED;
+
+	if (UCSI_CONSTAT(con, CONNECTED)) {
+		if (is_sink && sink_path_enabled)
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+		else if (!is_sink)
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+	}
+
+	return 0;
+}
+
 static int ucsi_psy_get_online(struct ucsi_connector *con,
 			       union power_supply_propval *val)
 {
@@ -245,6 +269,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;
 	}
drivers/usb/typec/ucsi/ucsi.h +3 -0
index e301d9012936..cce93af7461b 100644
@@ -360,6 +360,9 @@ struct ucsi_cable_property {
 #define   UCSI_CONSTAT_BC_SLOW_CHARGING		2
 #define   UCSI_CONSTAT_BC_TRICKLE_CHARGING	3
 #define UCSI_CONSTAT_PD_VERSION_V1_2		UCSI_DECLARE_BITFIELD_V1_2(70, 16)
+#define UCSI_CONSTAT_SINK_PATH_STATUS_V2_0	UCSI_DECLARE_BITFIELD_V2_0(87, 1)
+#define   UCSI_CONSTAT_SINK_PATH_DISABLED   0
+#define   UCSI_CONSTAT_SINK_PATH_ENABLED    1
 #define UCSI_CONSTAT_PWR_READING_READY_V2_1	UCSI_DECLARE_BITFIELD_V2_1(89, 1)
 #define UCSI_CONSTAT_CURRENT_SCALE_V2_1		UCSI_DECLARE_BITFIELD_V2_1(90, 3)
 #define UCSI_CONSTAT_PEAK_CURRENT_V2_1		UCSI_DECLARE_BITFIELD_V2_1(93, 16)
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Signature · 1 line
heikki
maintainer Reviewed-by on v1 2/6
#5 lore raw

Re: [PATCH 2/6] usb: typec: ucsi: psy: Add support for DRP USB type

In reply to Jameson Thies
Quoted 6 lines · Jameson Thies

The USB Type registered with the power supply class is based on the current power operation mode of the port. When the port is using USB PD and is connected to a DRP partner, report that the power supply's USB type is USB PD DRP.

Quoted 28 lines
---
 drivers/usb/typec/ucsi/psy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/usb/typec/ucsi/psy.c +9 -1
index 2b0225821502..985a90d3f898 100644
@@ -209,8 +209,16 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
 {
 	val->intval = POWER_SUPPLY_USB_TYPE_C;
 	if (UCSI_CONSTAT(con, CONNECTED) &&
-	    UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD)
+	    UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
+		for (int i = 0; i < con->num_pdos; i++) {
+			if (pdo_type(con->src_pdos[i]) == PDO_TYPE_FIXED &&
+			    con->src_pdos[i] & PDO_FIXED_DUAL_ROLE) {
+				val->intval = POWER_SUPPLY_USB_TYPE_PD_DRP;
+				return 0;
+			}
+		}
 		val->intval = POWER_SUPPLY_USB_TYPE_PD;
+	}
 
 	return 0;
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Signature · 1 line
heikki
maintainer Reviewed-by on v1 3/6
#9 lore raw

Re: [PATCH 3/6] usb: typec: ucsi: Report power supply changes on power opmode changes

In reply to Jameson Thies
Quoted 6 lines · Jameson Thies

Report opmode changes from the PPM to the power supply class by calling ucsi_port_psy_changed(). If the current opmode is USB PD, do not call ucsi_port_psy_changed(). The power supply class will be updated after requesting partner source PDOs.

Quoted 28 lines
---
 drivers/usb/typec/ucsi/ucsi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/typec/ucsi/ucsi.c +3 -0
index 3f568f790f39..7b718049d0d1 100644
@@ -1022,14 +1022,17 @@ static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
+		ucsi_port_psy_changed(con);
 		break;
 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
+		ucsi_port_psy_changed(con);
 		break;
 	default:
 		con->rdo = 0;
 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
+		ucsi_port_psy_changed(con);
 		break;
 	}
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Signature · 1 line
heikki
maintainer
#12 lore raw

Re: [PATCH 4/6] usb: typec: ucsi: Report power supply change on sink path change

In reply to Jameson Thies
Quoted 14 lines · Jameson Thies

Update the UCSI interface driver to report a power supply change when the PPM sets the Sink Path Change bit.

---
 drivers/usb/typec/ucsi/ucsi.c | 2 +-
 drivers/usb/typec/ucsi/ucsi.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/usb/typec/ucsi/ucsi.c +0 -0
index 7b718049d0d1..1a7d850b11ea 100644

drivers/usb/typec/ucsi/ucsi.c static void ucsi_handle_connector_change(struct work_struct *work) · line 1296

Quoted 5 lines
drivers/usb/typec/ucsi/ucsi.c +1 -1
@@ -1293,7 +1293,7 @@ static void ucsi_handle_connector_change(struct work_struct *work)
 	if (change & UCSI_CONSTAT_CAM_CHANGE)
 		ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
 
-	if (change & UCSI_CONSTAT_BC_CHANGE)
+	if (change & UCSI_CONSTAT_BC_CHANGE || change & UCSI_CONSTAT_SINK_PATH_CHANGE)

How about:

if (change & (UCSI_CONSTAT_BC_CHANGE | UCSI_CONSTAT_SINK_PATH_CHANGE))

Quoted 17 lines
drivers/usb/typec/ucsi/ucsi.c +0 -0
 		ucsi_port_psy_changed(con);
 
 	if (con->ucsi->version >= UCSI_VERSION_2_1 &&
drivers/usb/typec/ucsi/ucsi.h +1 -0
index cce93af7461b..35993bc34d4d 100644
@@ -382,6 +382,7 @@ struct ucsi_cable_property {
 #define UCSI_CONSTAT_BC_CHANGE			BIT(9)
 #define UCSI_CONSTAT_PARTNER_CHANGE		BIT(11)
 #define UCSI_CONSTAT_POWER_DIR_CHANGE		BIT(12)
+#define UCSI_CONSTAT_SINK_PATH_CHANGE		BIT(13)
 #define UCSI_CONSTAT_CONNECT_CHANGE		BIT(14)
 #define UCSI_CONSTAT_ERROR			BIT(15)
 
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Signature · 1 line
heikki
maintainer Reviewed-by on v1 5/6
#16 lore raw

Re: [PATCH 5/6] usb: typec: ucsi: psy: Set max current to zero when disconnected

In reply to Jameson Thies
Quoted 7 lines · Jameson Thies

The ucsi_psy_get_current_max function defaults to 0.1A when it is not clear how much current the partner device can support. But this does not check the port is connected, and will report 0.1A max current when nothing is connected. Update ucsi_psy_get_current_max to report 0A when there is no connection.

Quoted 22 lines
---
 drivers/usb/typec/ucsi/psy.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/typec/ucsi/psy.c +5 -0
index 985a90d3f898..3a209de55c14 100644
@@ -169,6 +169,11 @@ static int ucsi_psy_get_current_max(struct ucsi_connector *con,
 {
 	u32 pdo;
 
+	if (!UCSI_CONSTAT(con, CONNECTED)) {
+		val->intval = 0;
+		return 0;
+	}
+
 	switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
 	case UCSI_CONSTAT_PWR_OPMODE_PD:
 		if (con->num_pdos > 0) {
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Signature · 1 line
heikki
maintainer
#19 lore raw

Re: [PATCH 6/6] usb: typec: ucsi: pr_swap should check connector_status

In reply to Jameson Thies

Hi,

Quoted 25 lines · Jameson Thies

From: Abhishek Pandit-Subedi <[email protected]>

Power role swaps initiated by the host system doesn't generate connection status change notifications.

Quoted 1 line

From UCSIv3.0 spec, section 6.5.10 Set Power Direction Role:

The execution of this command might require PPM to initiate a power role swap. If the power role swap fails for any reason, the command returns, and error and the power direction should remain unchanged. Note that if the execution of the command resulted in a successful power role swap, it should not result in a connector status change notification.

---
 drivers/usb/typec/ucsi/ucsi.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +0 -0
index 1a7d850b11ea..6e3797d7a144 100644

drivers/usb/typec/ucsi/ucsi.c static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) · line 1532

Quoted 8 lines
drivers/usb/typec/ucsi/ucsi.c +4 -1
@@ -1526,20 +1526,40 @@ static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
 	if (ret < 0)
 		goto out_unlock;
 
-	mutex_unlock(&con->lock);
+	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
+	ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
+	if (ret < 0)
+		goto out_unlock;

Couldn't you use the helper ucsi_get_connector_status() ?

Quoted 8 lines
drivers/usb/typec/ucsi/ucsi.c +3 -4
-	if (!wait_for_completion_timeout(&con->complete,
-					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
-		return -ETIMEDOUT;
+	cur_role = !!UCSI_CONSTAT(con, PWR_DIR);
 
-	mutex_lock(&con->lock);
+	/* Execution of SET_PDR should not result in connector status
+	 * notifications. However, some legacy implementations may still defer

drivers/usb/typec/ucsi/ucsi.c static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) · line 1547

Quoted 10 lines
drivers/usb/typec/ucsi/ucsi.c +10 -0
+	 * the actual role swap and return immediately. Thus, check the
+	 * connector status in case it immediately succeeded or wait for a later
+	 * connector status change.
+	 */
+	if (cur_role != role) {
+		mutex_unlock(&con->lock);
+
+		if (!wait_for_completion_timeout(
+			    &con->complete,
+			    msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))

Please align those properly.

Quoted 8 lines
drivers/usb/typec/ucsi/ucsi.c +4 -0
+			return -ETIMEDOUT;
+
+		mutex_lock(&con->lock);
+	}
 
 	/* Something has gone wrong while swapping the role */
 	if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) {
 		ucsi_reset_connector(con, true);

drivers/usb/typec/ucsi/ucsi.c static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) · line 1565

Quoted 10 lines
drivers/usb/typec/ucsi/ucsi.c +4 -0
 		ret = -EPROTO;
+		goto out_unlock;
 	}
 
+	/* Indicate successful power role swap */
+	typec_set_pwr_role(con->port, role);
+
 out_unlock:
 	mutex_unlock(&con->lock);
 

Maybe this could be send separately? It does not seem to be directly ucsi psy related.

thanks,

Signature · 1 line
heikki
maintainer
#21 lore raw
In reply to Jameson Thies
Quoted 20 lines · Jameson Thies

This series includes the following minor changes to power supply handling by the UCSI driver.

1) Adds the "Status" property to power supplies registered by the UCSI driver.
2) Reports power supply USB type as "DRP" when the port partner is a DRP.
3) Updates ucsi.c to report a power supply change all power opmode changes. Currently this only gets reported when opmode is PD.
4) Updates ucsi.c to report a power supply change when the PPM signals a sink path change.
5) Set max current to 0 when no device is connected. Currently this defaults to 0.1A.
6) Updates connector status after initiating a PR swap. Following the UCSI spec, there is no reported change following a successful SET_PDR command. This results in the stored connector status being outdated.

Abhishek Pandit-Subedi (1):
usb: typec: ucsi: pr_swap should check connector_status

Quoted 6 lines

Jameson Thies (5):
usb: typec: ucsi: psy: Add power supply status
usb: typec: ucsi: psy: Add support for DRP USB type
usb: typec: ucsi: Report power supply changes on power opmode changes
usb: typec: ucsi: Report power supply change on sink path change
usb: typec: ucsi: psy: Set max current to zero when disconnected

Please do not mix bug fixes and updates to the driver in the same patch series, as that means the bug fixes will be delayed until the next release, not the current one, as I can't take them all at once.

thanks,

greg k-h

#6 lore raw

Re: [PATCH 2/6] usb: typec: ucsi: psy: Add support for DRP USB type

In reply to Heikki Krogerus

Hi Benson

Quoted 4 lines

Do you really need to iterate through the whole list of pdos to find this?

The DRP bit is guaranteed to be always in the 5V PDO, which is guaranteed to be the 0th one.

Thanks for taking a look. This sounds like a good optimization, I'll apply it to the v2 series.

#13 lore raw

Re: [PATCH 4/6] usb: typec: ucsi: Report power supply change on sink path change

In reply to Heikki Krogerus
Quoted 3 lines

How about:

if (change & (UCSI_CONSTAT_BC_CHANGE | UCSI_CONSTAT_SINK_PATH_CHANGE))

Thank you for the suggestion, this is neater. I'll apply it to the v2 series.

#20 lore raw

Re: [PATCH 6/6] usb: typec: ucsi: pr_swap should check connector_status

In reply to Heikki Krogerus
Quoted 1 line

Couldn't you use the helper ucsi_get_connector_status() ?

That work, I'll switch to using the helper here.

Quoted 2 lines

Maybe this could be send separately? It does not seem to be directly ucsi psy related.

Sounds good to me. The same issue can happen with DR swaps, so I'll also update the patch to refresh connector status after sending SET_UOR.

#22 lore raw
In reply to Greg KH

Thank you for this feedback. The only patch here which is a clear bug fix is 5/6. Currently the driver reports 0.1A max current when nothing is connected on the port. I'll send that patch separately.

#24 lore raw
In reply to Jameson Thies
Quoted 2 lines · Jameson Thies

This series includes the following minor changes to power supply handling by the UCSI driver.

...

Quoted 1 line

base-commit: e40b984b6c4ce3f80814f39f86f87b2a48f2e662

I wanted to let you know that on my Dell XPS-9320, this patchset ended up spamming (i.e., hundreds) my dmesg with the following:

power_supply ucsi-source-psy-USBC000:002: driver reporting unavailable enum value 7

... which I believe to be POWER_SUPPLY_USB_TYPE_PD_DRP .

In my case it was coming from the call to power_supply_show_enum_with_available() on/around line 380 in .../drivers/power/supply/power_supply_sysfs.c ; I'd tried adding POWER_SUPPLY_USB_TYPE_PD_DRP to con->psy_desc.usb_types in ucsi_register_port_psy() in .../drivers/usb/typec/ucsi/psy.c thinking that may fix it with no success.

LMK if you need any further info,

-Kenny

Signature · 1 line
Kenneth R. Crudup / Sr. SW Engineer, Scott County Consulting, Orange County CA
#25 lore raw

Hi Kenneth,
You are correct that adding POWER_SUPPLY_USB_TYPE_PD_DRP in ucsi_register_port_psy() is missing here. I would have expected that adding it resolves the issue. I'll take a closer look, thanks for raising this.

#26 lore raw
Quoted 3 lines · Jameson Thies

You are correct that adding POWER_SUPPLY_USB_TYPE_PD_DRP in ucsi_register_port_psy() is missing here. I would have expected that adding it resolves the issue.

The issue must be deeper than that, as I even went as far as making the check for "allowed types" pass all the time (as the dmesg was swamped making booting difficult so debugging was somewhat painful) and was still getting "enum" errors.

... that being said, I had a flight to catch on Friday so didn't keep working on a fix.

-Kenny

Signature · 1 line
Kenneth R. Crudup / Sr. SW Engineer, Scott County Consulting, Orange County CA
maintainer
#23 lore raw
In reply to Jameson Thies
Quoted 3 lines · Jameson Thies

Thank you for this feedback. The only patch here which is a clear bug fix is 5/6. Currently the driver reports 0.1A max current when nothing is connected on the port. I'll send that patch separately.

meta-comment, you lost all context here and I have no idea what you are responding to, or what this is about :(

Remember, some of us get 1000+ emails a day to handle, context matters.

thanks,

greg k-h