Skip to contentSkip to conversation

usb: typec: ucsi: psy: Add support for the charge type property

PATCH Superseded Heikki Krogerus 2 participants 3 messages Jun 18, 2024 – Jun 19, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Heikki Krogerus

author maintainer
#0 lore raw

PATCH v2 1/1 usb: typec: ucsi: psy: Add support for the charge type property

Adding support for the charge type Linux power supply class property (POWER_SUPPLY_PROP_CHARGE_TYPE) to the UCSI driver. That will make the charge type visible in the charge_type power supply class sysfs attribute file.

UCSI has the charge type specified in the Battery Charging Status field of the response to the GET_CONNECTOR_STATUS command.

---
Changed since the last version:
The commit message is completely rewritten. The subject line was also changed.

The original patch:
https://lore.kernel.org/linux-usb/[email protected]/

---
 drivers/usb/typec/ucsi/psy.c  | 27 +++++++++++++++++++++++++++
 drivers/usb/typec/ucsi/ucsi.c |  3 +++
 2 files changed, 30 insertions(+)
drivers/usb/typec/ucsi/psy.c +27 -0
index b35c6e07911e..b3910f37e171 100644
@@ -20,6 +20,7 @@ enum ucsi_psy_online_states {
 };
 
 static enum power_supply_property ucsi_psy_props[] = {
+	POWER_SUPPLY_PROP_CHARGE_TYPE,
 	POWER_SUPPLY_PROP_USB_TYPE,
 	POWER_SUPPLY_PROP_ONLINE,
 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
@@ -194,6 +195,30 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
 	return 0;
 }
 
+static int ucsi_psy_get_charge_type(struct ucsi_connector *con, union power_supply_propval *val)
+{
+	if (!(con->status.flags & UCSI_CONSTAT_CONNECTED) ||
+	    (con->status.flags & UCSI_CONSTAT_PWR_DIR) != TYPEC_SINK) {
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
+		return 0;
+	}
+
+	switch (UCSI_CONSTAT_BC_STATUS(con->status.pwr_status)) {
+	case UCSI_CONSTAT_BC_NOMINAL_CHARGING:
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
+		break;
+	case UCSI_CONSTAT_BC_SLOW_CHARGING:
+	case UCSI_CONSTAT_BC_TRICKLE_CHARGING:
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+		break;
+	default:
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
+		break;
+	}
+
+	return 0;
+}
+
 static int ucsi_psy_get_prop(struct power_supply *psy,
 			     enum power_supply_property psp,
 			     union power_supply_propval *val)
@@ -201,6 +226,8 @@ static int ucsi_psy_get_prop(struct power_supply *psy,
 	struct ucsi_connector *con = power_supply_get_drvdata(psy);
 
 	switch (psp) {
+	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+		return ucsi_psy_get_charge_type(con, val);
 	case POWER_SUPPLY_PROP_USB_TYPE:
 		return ucsi_psy_get_usb_type(con, val);
 	case POWER_SUPPLY_PROP_ONLINE:
drivers/usb/typec/ucsi/ucsi.c +3 -0
index 6bbaad4b89a5..51135e3502cf 100644
@@ -1257,6 +1257,9 @@ static void ucsi_handle_connector_change(struct work_struct *work)
 	if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
 		ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
 
+	if (con->status.change & UCSI_CONSTAT_BC_CHANGE)
+		ucsi_port_psy_changed(con);
+
 out_unlock:
 	mutex_unlock(&con->lock);
 }
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 36 lines · Heikki Krogerus

Adding support for the charge type Linux power supply class property (POWER_SUPPLY_PROP_CHARGE_TYPE) to the UCSI driver. That will make the charge type visible in the charge_type power supply class sysfs attribute file.

UCSI has the charge type specified in the Battery Charging Status field of the response to the GET_CONNECTOR_STATUS command.

---
Changed since the last version:
The commit message is completely rewritten. The subject line was also changed.

The original patch:
https://lore.kernel.org/linux-usb/[email protected]/

---
  drivers/usb/typec/ucsi/psy.c  | 27 +++++++++++++++++++++++++++
  drivers/usb/typec/ucsi/ucsi.c |  3 +++
  2 files changed, 30 insertions(+)
drivers/usb/typec/ucsi/psy.c +1 -0
index b35c6e07911e..b3910f37e171 100644
@@ -20,6 +20,7 @@ enum ucsi_psy_online_states {
  };
 
  static enum power_supply_property ucsi_psy_props[] = {
+	POWER_SUPPLY_PROP_CHARGE_TYPE,
  	POWER_SUPPLY_PROP_USB_TYPE,
  	POWER_SUPPLY_PROP_ONLINE,
  	POWER_SUPPLY_PROP_VOLTAGE_MIN,

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

Quoted 8 lines
drivers/usb/typec/ucsi/psy.c +5 -0
@@ -194,6 +195,30 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
  	return 0;
  }
 
+static int ucsi_psy_get_charge_type(struct ucsi_connector *con, union power_supply_propval *val)
+{
+	if (!(con->status.flags & UCSI_CONSTAT_CONNECTED) ||
+	    (con->status.flags & UCSI_CONSTAT_PWR_DIR) != TYPEC_SINK) {
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;

The not connected state obviously makes sense for POWER_SUPPLY_CHARGE_TYPE_NONE, but what exactly is the other situation? A UCSI state machine failure?

I'm mostly wondering if POWER_SUPPLY_CHARGE_TYPE_UNKNOWN makes sense for that or not.

Besides this question the patch looks fine to me and you can add my tag with your decision.

Quoted 45 lines
drivers/usb/typec/ucsi/psy.c +21 -0
+		return 0;
+	}
+
+	switch (UCSI_CONSTAT_BC_STATUS(con->status.pwr_status)) {
+	case UCSI_CONSTAT_BC_NOMINAL_CHARGING:
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
+		break;
+	case UCSI_CONSTAT_BC_SLOW_CHARGING:
+	case UCSI_CONSTAT_BC_TRICKLE_CHARGING:
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+		break;
+	default:
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
+		break;
+	}
+
+	return 0;
+}
+
  static int ucsi_psy_get_prop(struct power_supply *psy,
  			     enum power_supply_property psp,
  			     union power_supply_propval *val)
@@ -201,6 +226,8 @@ static int ucsi_psy_get_prop(struct power_supply *psy,
  	struct ucsi_connector *con = power_supply_get_drvdata(psy);
 
  	switch (psp) {
+	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+		return ucsi_psy_get_charge_type(con, val);
  	case POWER_SUPPLY_PROP_USB_TYPE:
  		return ucsi_psy_get_usb_type(con, val);
  	case POWER_SUPPLY_PROP_ONLINE:
drivers/usb/typec/ucsi/ucsi.c +3 -0
index 6bbaad4b89a5..51135e3502cf 100644
@@ -1257,6 +1257,9 @@ static void ucsi_handle_connector_change(struct work_struct *work)
  	if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
  		ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
 
+	if (con->status.change & UCSI_CONSTAT_BC_CHANGE)
+		ucsi_port_psy_changed(con);
+
  out_unlock:
  	mutex_unlock(&con->lock);
  }
author maintainer
#2 lore raw
Quoted 48 lines · Mario Limonciello
Quoted 35 lines · Heikki Krogerus

Adding support for the charge type Linux power supply class property (POWER_SUPPLY_PROP_CHARGE_TYPE) to the UCSI driver. That will make the charge type visible in the charge_type power supply class sysfs attribute file.

UCSI has the charge type specified in the Battery Charging Status field of the response to the GET_CONNECTOR_STATUS command.

---
Changed since the last version:
The commit message is completely rewritten. The subject line was also changed.

The original patch:
https://lore.kernel.org/linux-usb/[email protected]/

---
  drivers/usb/typec/ucsi/psy.c  | 27 +++++++++++++++++++++++++++
  drivers/usb/typec/ucsi/ucsi.c |  3 +++
  2 files changed, 30 insertions(+)
drivers/usb/typec/ucsi/psy.c +1 -0
index b35c6e07911e..b3910f37e171 100644
@@ -20,6 +20,7 @@ enum ucsi_psy_online_states {
  };
  static enum power_supply_property ucsi_psy_props[] = {
+	POWER_SUPPLY_PROP_CHARGE_TYPE,
  	POWER_SUPPLY_PROP_USB_TYPE,
  	POWER_SUPPLY_PROP_ONLINE,
  	POWER_SUPPLY_PROP_VOLTAGE_MIN,

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

Quoted 7 lines
drivers/usb/typec/ucsi/psy.c +5 -0
@@ -194,6 +195,30 @@ static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
  	return 0;
  }
+static int ucsi_psy_get_charge_type(struct ucsi_connector *con, union power_supply_propval *val)
+{
+	if (!(con->status.flags & UCSI_CONSTAT_CONNECTED) ||
+	    (con->status.flags & UCSI_CONSTAT_PWR_DIR) != TYPEC_SINK) {
+		val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;

The not connected state obviously makes sense for POWER_SUPPLY_CHARGE_TYPE_NONE, but what exactly is the other situation? A UCSI state machine failure?

Quoted 2 lines

I'm mostly wondering if POWER_SUPPLY_CHARGE_TYPE_UNKNOWN makes sense for that or not.

That works for me. I'll change it to POWER_SUPPLY_CHARGE_TYPE_UNKNOWN in that case.

Quoted 4 lines

Besides this question the patch looks fine to me and you can add my tag with your decision.

thanks,

Signature · 1 line
heikki