Linux USB
 help / color / mirror / Atom feed
* [PATCH v1 0/4] usb: typec: ucsi: Expand power supply support
@ 2024-07-17  0:49 Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 1/4] usb: typec: ucsi: Add status to UCSI power supply driver Jameson Thies
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Jameson Thies @ 2024-07-17  0:49 UTC (permalink / raw)
  To: heikki.krogerus, linux-usb
  Cc: jthies, bleung, abhishekpandit, andersson, dmitry.baryshkov,
	fabrice.gasnier, gregkh, hdegoede, neil.armstrong,
	rajaram.regupathy, saranya.gopal, linux-kernel

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
-- 
2.45.2.1089.g2a221341d9-goog


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v1 1/4] usb: typec: ucsi: Add status to UCSI power supply driver
  2024-07-17  0:49 [PATCH v1 0/4] usb: typec: ucsi: Expand power supply support Jameson Thies
@ 2024-07-17  0:49 ` Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 2/4] usb: typec: ucsi: Add USB PD DRP to USB type Jameson Thies
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Jameson Thies @ 2024-07-17  0:49 UTC (permalink / raw)
  To: heikki.krogerus, linux-usb
  Cc: jthies, bleung, abhishekpandit, andersson, dmitry.baryshkov,
	fabrice.gasnier, gregkh, hdegoede, neil.armstrong,
	rajaram.regupathy, saranya.gopal, linux-kernel

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

Signed-off-by: Jameson Thies <jthies@google.com>
---
 drivers/usb/typec/ucsi/psy.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/drivers/usb/typec/ucsi/psy.c b/drivers/usb/typec/ucsi/psy.c
index e623d80e177c..d0b52cee41d2 100644
--- a/drivers/usb/typec/ucsi/psy.c
+++ b/drivers/usb/typec/ucsi/psy.c
@@ -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;
 	}
-- 
2.45.2.1089.g2a221341d9-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v1 2/4] usb: typec: ucsi: Add USB PD DRP to USB type
  2024-07-17  0:49 [PATCH v1 0/4] usb: typec: ucsi: Expand power supply support Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 1/4] usb: typec: ucsi: Add status to UCSI power supply driver Jameson Thies
@ 2024-07-17  0:49 ` Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 3/4] usb: typec: ucsi: Set power role based on UCSI charge control Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 4/4] usb: typec: ucsi: Fix SET_PDR typo in UCSI header file Jameson Thies
  3 siblings, 0 replies; 7+ messages in thread
From: Jameson Thies @ 2024-07-17  0:49 UTC (permalink / raw)
  To: heikki.krogerus, linux-usb
  Cc: jthies, bleung, abhishekpandit, andersson, dmitry.baryshkov,
	fabrice.gasnier, gregkh, hdegoede, neil.armstrong,
	rajaram.regupathy, saranya.gopal, linux-kernel

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.

Signed-off-by: Jameson Thies <jthies@google.com>
---
 drivers/usb/typec/ucsi/psy.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/ucsi/psy.c b/drivers/usb/typec/ucsi/psy.c
index d0b52cee41d2..45113e013696 100644
--- a/drivers/usb/typec/ucsi/psy.c
+++ b/drivers/usb/typec/ucsi/psy.c
@@ -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)
-- 
2.45.2.1089.g2a221341d9-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v1 3/4] usb: typec: ucsi: Set power role based on UCSI charge control
  2024-07-17  0:49 [PATCH v1 0/4] usb: typec: ucsi: Expand power supply support Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 1/4] usb: typec: ucsi: Add status to UCSI power supply driver Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 2/4] usb: typec: ucsi: Add USB PD DRP to USB type Jameson Thies
@ 2024-07-17  0:49 ` Jameson Thies
  2024-07-22 22:03   ` Jameson Thies
  2024-07-17  0:49 ` [PATCH v1 4/4] usb: typec: ucsi: Fix SET_PDR typo in UCSI header file Jameson Thies
  3 siblings, 1 reply; 7+ messages in thread
From: Jameson Thies @ 2024-07-17  0:49 UTC (permalink / raw)
  To: heikki.krogerus, linux-usb
  Cc: jthies, bleung, abhishekpandit, andersson, dmitry.baryshkov,
	fabrice.gasnier, gregkh, hdegoede, neil.armstrong,
	rajaram.regupathy, saranya.gopal, linux-kernel

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.

Signed-off-by: Jameson Thies <jthies@google.com>
---
 drivers/usb/typec/ucsi/psy.c | 46 ++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/drivers/usb/typec/ucsi/psy.c b/drivers/usb/typec/ucsi/psy.c
index 45113e013696..feb344cb7ac8 100644
--- a/drivers/usb/typec/ucsi/psy.c
+++ b/drivers/usb/typec/ucsi/psy.c
@@ -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);
 
-- 
2.45.2.1089.g2a221341d9-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v1 4/4] usb: typec: ucsi: Fix SET_PDR typo in UCSI header file
  2024-07-17  0:49 [PATCH v1 0/4] usb: typec: ucsi: Expand power supply support Jameson Thies
                   ` (2 preceding siblings ...)
  2024-07-17  0:49 ` [PATCH v1 3/4] usb: typec: ucsi: Set power role based on UCSI charge control Jameson Thies
@ 2024-07-17  0:49 ` Jameson Thies
  2024-07-17  7:56   ` neil.armstrong
  3 siblings, 1 reply; 7+ messages in thread
From: Jameson Thies @ 2024-07-17  0:49 UTC (permalink / raw)
  To: heikki.krogerus, linux-usb
  Cc: jthies, bleung, abhishekpandit, andersson, dmitry.baryshkov,
	fabrice.gasnier, gregkh, hdegoede, neil.armstrong,
	rajaram.regupathy, saranya.gopal, linux-kernel

Fix SET_PDR typo in UCSI header file.

Signed-off-by: Jameson Thies <jthies@google.com>
---
 drivers/usb/typec/ucsi/ucsi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
index 57129f3c0814..375f1881c1e2 100644
--- a/drivers/usb/typec/ucsi/ucsi.h
+++ b/drivers/usb/typec/ucsi/ucsi.h
@@ -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)
 
-- 
2.45.2.1089.g2a221341d9-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 4/4] usb: typec: ucsi: Fix SET_PDR typo in UCSI header file
  2024-07-17  0:49 ` [PATCH v1 4/4] usb: typec: ucsi: Fix SET_PDR typo in UCSI header file Jameson Thies
@ 2024-07-17  7:56   ` neil.armstrong
  0 siblings, 0 replies; 7+ messages in thread
From: neil.armstrong @ 2024-07-17  7:56 UTC (permalink / raw)
  To: Jameson Thies, heikki.krogerus, linux-usb
  Cc: bleung, abhishekpandit, andersson, dmitry.baryshkov,
	fabrice.gasnier, gregkh, hdegoede, rajaram.regupathy,
	saranya.gopal, linux-kernel

On 17/07/2024 02:49, Jameson Thies wrote:
> Fix SET_PDR typo in UCSI header file.
> 
> Signed-off-by: Jameson Thies <jthies@google.com>
> ---
>   drivers/usb/typec/ucsi/ucsi.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
> index 57129f3c0814..375f1881c1e2 100644
> --- a/drivers/usb/typec/ucsi/ucsi.h
> +++ b/drivers/usb/typec/ucsi/ucsi.h
> @@ -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)
>   

Reviewed-by: Neil Armstrong <neil.armstrong@linaro.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 3/4] usb: typec: ucsi: Set power role based on UCSI charge control
  2024-07-17  0:49 ` [PATCH v1 3/4] usb: typec: ucsi: Set power role based on UCSI charge control Jameson Thies
@ 2024-07-22 22:03   ` Jameson Thies
  0 siblings, 0 replies; 7+ messages in thread
From: Jameson Thies @ 2024-07-22 22:03 UTC (permalink / raw)
  To: heikki.krogerus, linux-usb
  Cc: bleung, abhishekpandit, andersson, dmitry.baryshkov,
	fabrice.gasnier, gregkh, hdegoede, neil.armstrong,
	rajaram.regupathy, saranya.gopal, linux-kernel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-07-22 22:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-17  0:49 [PATCH v1 0/4] usb: typec: ucsi: Expand power supply support Jameson Thies
2024-07-17  0:49 ` [PATCH v1 1/4] usb: typec: ucsi: Add status to UCSI power supply driver Jameson Thies
2024-07-17  0:49 ` [PATCH v1 2/4] usb: typec: ucsi: Add USB PD DRP to USB type Jameson Thies
2024-07-17  0:49 ` [PATCH v1 3/4] usb: typec: ucsi: Set power role based on UCSI charge control Jameson Thies
2024-07-22 22:03   ` Jameson Thies
2024-07-17  0:49 ` [PATCH v1 4/4] usb: typec: ucsi: Fix SET_PDR typo in UCSI header file Jameson Thies
2024-07-17  7:56   ` neil.armstrong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox