[PATCH 3/5] usb: typec: tcpm: Add support for Battery Status response message

Amit Sunil Dhamne via B4 Relay
2025-03-12T23:43:20Z

From: Amit Sunil Dhamne <amitsd@google.com>

Add support for responding to Get_Battery_Status (extended) request with
a Battery_Status (data) msg. The requester shall request the status of
an individual battery by providing an index in Get_Battery_Status. In
case of failure to identify battery, the responder shall reply with an
appropriate message indicating so.

Battery status support is only provided for fixed batteries indexed from
0 - 3.

Support for Battery_Status message is required for sinks that contain
battery as specified in USB PD Rev3.1 v1.8
("Applicability of Data Messages" section).

Signed-off-by: Amit Sunil Dhamne <amitsd@google.com>
Reviewed-by: Kyle Tso <kyletso@google.com>
Reviewed-by: Badhri Jagan Sridharan <badhri@google.com>
---
drivers/usb/typec/tcpm/tcpm.c | 127 +++++++++++++++++++++++++++++++++++++++++-
include/linux/usb/pd.h | 34 +++++++++++
2 files changed, 159 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 6bf1a22c785aff6b1ad77a20d85e22580527f5b1..2d0dcb998608e25c308159873c6b10e178e0a7a1 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -228,6 +228,7 @@ enum pd_msg_request {
PD_MSG_DATA_SINK_CAP,
PD_MSG_DATA_SOURCE_CAP,
PD_MSG_DATA_REV,
+ PD_MSG_DATA_BATT_STATUS
};

enum adev_actions {
@@ -332,6 +333,15 @@ struct pd_timings {
u32 snk_bc12_cmpletion_time;
};

+/*
+ * As per USB PD Spec Rev 3.18 (Sec. 6.5.13.11), a sink can have a maximum
+ * of 4 fixed batteries indexed [0, 3].
+ */
+#define MAX_NUM_FIXED_BATT 4
+
+/* Convert microwatt to watt */
+#define UWH_TO_WH(pow) ((pow) / 1000000)
+
struct tcpm_port {
struct device *dev;

@@ -580,6 +590,15 @@ struct tcpm_port {

/* Indicates maximum (revision, version) supported */
struct pd_revision_info pd_rev;
+
+ struct power_supply *fixed_batt[MAX_NUM_FIXED_BATT];
+ u8 fixed_batt_cnt;
+
+ /*
+ * Variable used to store battery_ref from the Get_Battery_Status
+ * request to process Battery_Status messages.
+ */
+ u8 batt_request;
#ifdef CONFIG_DEBUG_FS
struct dentry *dentry;
struct mutex logbuffer_lock; /* log buffer access lock */
@@ -1339,6 +1358,62 @@ static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
}

+#define BATTERY_PROPERTY_UNKNOWN 0xffff
+
+static int tcpm_pd_send_batt_status(struct tcpm_port *port)
+{
+ struct pd_message msg;
+ struct power_supply *batt;
+ u32 bsdo;
+ u32 batt_id = port->batt_request;
+ union power_supply_propval val;
+ int ret;
+ bool batt_present = false;
+ u8 charging_status = BSDO_BATTERY_INFO_RSVD;
+ u16 present_charge = BATTERY_PROPERTY_UNKNOWN;
+
+ memset(&msg, 0, sizeof(msg));
+ if (batt_id < MAX_NUM_FIXED_BATT && port->fixed_batt[batt_id]) {
+ batt_present = true;
+ batt = port->fixed_batt[batt_id];
+ ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_ENERGY_NOW, &val);
+ /* Battery Present Charge is reported in increments of 0.1WH */
+ if (!ret)
+ present_charge = (u16)(UWH_TO_WH(val.intval) * 10);
+
+ ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_STATUS,
+ &val);
+ if (!ret) {
+ switch (val.intval) {
+ case POWER_SUPPLY_STATUS_CHARGING:
+ case POWER_SUPPLY_STATUS_FULL:
+ charging_status = BSDO_BATTERY_INFO_CHARGING;
+ break;
+ case POWER_SUPPLY_STATUS_DISCHARGING:
+ charging_status = BSDO_BATTERY_INFO_DISCHARGING;
+ break;
+ case POWER_SUPPLY_STATUS_NOT_CHARGING:
+ charging_status = BSDO_BATTERY_INFO_IDLE;
+ break;
+ default:
+ charging_status = BSDO_BATTERY_INFO_RSVD;
+ break;
+ }
+ }
+ }
+
+ bsdo = BSDO(present_charge, batt_present ? charging_status : 0,
+ batt_present, !batt_present);
+ msg.payload[0] = cpu_to_le32(bsdo);
+ msg.header = PD_HEADER_LE(PD_DATA_BATT_STATUS,
+ port->pwr_role,
+ port->data_role,
+ port->negotiated_rev,
+ port->message_id,
+ 1);
+ return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
+}
+
static void mod_tcpm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
{
if (delay_ms) {
@@ -3597,6 +3672,7 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
{
enum pd_ext_msg_type type = pd_header_type_le(msg->header);
unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
+ const struct pd_chunked_ext_message_data *ext_msg = &msg->ext_msg;

/* stopping VDM state machine if interrupted by other Messages */
if (tcpm_vdm_ams(port)) {
@@ -3605,7 +3681,7 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
mod_vdm_delayed_work(port, 0);
}

- if (!(le16_to_cpu(msg->ext_msg.header) & PD_EXT_HDR_CHUNKED)) {
+ if (!(le16_to_cpu(ext_msg->header) & PD_EXT_HDR_CHUNKED)) {
tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
tcpm_log(port, "Unchunked extended messages unsupported");
return;
@@ -3630,9 +3706,13 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
NONE_AMS, 0);
}
break;
+ case PD_EXT_GET_BATT_STATUS:
+ port->batt_request = ext_msg->data[0];
+ tcpm_pd_handle_msg(port, PD_MSG_DATA_BATT_STATUS,
+ GETTING_BATTERY_STATUS);
+ break;
case PD_EXT_SOURCE_CAP_EXT:
case PD_EXT_GET_BATT_CAP:
- case PD_EXT_GET_BATT_STATUS:
case PD_EXT_BATT_CAP:
case PD_EXT_GET_MANUFACTURER_INFO:
case PD_EXT_MANUFACTURER_INFO:
@@ -3833,6 +3913,14 @@ static bool tcpm_send_queued_message(struct tcpm_port *port)
ret);
tcpm_ams_finish(port);
break;
+ case PD_MSG_DATA_BATT_STATUS:
+ ret = tcpm_pd_send_batt_status(port);
+ if (ret)
+ tcpm_log(port,
+ "Failed to send battery status ret=%d",
+ ret);
+ tcpm_ams_finish(port);
+ break;
default:
break;
}
@@ -7164,6 +7252,35 @@ static void tcpm_fw_get_timings(struct tcpm_port *port, struct fwnode_handle *fw
port->timings.snk_bc12_cmpletion_time = val;
}

+static void tcpm_fw_get_batt(struct tcpm_port *port, struct fwnode_handle *fwnode)
+{
+ int i, ret;
+ enum power_supply_type psy_type;
+
+ ret = power_supply_get_by_fwnode_reference_array(fwnode,
+ "fixed-batteries",
+ port->fixed_batt,
+ MAX_NUM_FIXED_BATT);
+ if (ret < 0) {
+ tcpm_log(port,
+ "Unable to parse or find batteries property, ret=%d",
+ ret);
+ return;
+ }
+
+ port->fixed_batt_cnt = ret;
+ for (i = 0; i < port->fixed_batt_cnt; i++) {
+ if (!port->fixed_batt[i])
+ continue;
+
+ psy_type = port->fixed_batt[i]->desc->type;
+ if (psy_type != POWER_SUPPLY_TYPE_BATTERY)
+ tcpm_log(port,
+ "Wrong power supply type (%u) at idx:%d. Should be battery type.",
+ psy_type, i);
+ }
+}
+
static int tcpm_fw_get_caps(struct tcpm_port *port, struct fwnode_handle *fwnode)
{
struct fwnode_handle *capabilities, *child, *caps = NULL;
@@ -7746,6 +7863,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)

tcpm_fw_get_timings(port, tcpc->fwnode);
tcpm_fw_get_pd_revision(port, tcpc->fwnode);
+ tcpm_fw_get_batt(port, tcpc->fwnode);

port->try_role = port->typec_caps.prefer_role;

@@ -7827,6 +7945,11 @@ void tcpm_unregister_port(struct tcpm_port *port)
hrtimer_cancel(&port->vdm_state_machine_timer);
hrtimer_cancel(&port->state_machine_timer);

+ for (i = 0; i < port->fixed_batt_cnt; i++) {
+ if (port->fixed_batt[i])
+ power_supply_put(port->fixed_batt[i]);
+ }
+
tcpm_reset_port(port);

tcpm_port_unregister_pd(port);
diff --git a/include/linux/usb/pd.h b/include/linux/usb/pd.h
index 3068c3084eb6176d7d9184c3959a4110282a9fa0..299e7c20127cd5b7dcdae4f24468df4b34b072b5 100644
--- a/include/linux/usb/pd.h
+++ b/include/linux/usb/pd.h
@@ -553,4 +553,38 @@ void usb_power_delivery_unlink_device(struct usb_power_delivery *pd, struct devi

#endif /* CONFIG_TYPEC */

+/* Battery Status Data Object */
+#define BSDO_PRESENT_CAPACITY_SHIFT 16
+#define BSDO_PRESENT_CAPACITY_MASK GENMASK(31, 16)
+#define BSDO_CHG_STATUS_SHIFT 10
+#define BSDO_CHG_STATUS_MASK GENMASK(11, 10)
+#define BSDO_BATTERY_PRESENT BIT(9)
+#define BSDO_INVALID_BATTERY_REFERENCE BIT(8)
+
+/*
+ * Battery Charge Status: Battery Charging Status Values as defined in
+ * "USB PD Spec Rev3.1 Ver1.8", "Table 6-46 Battery Status Data Object (BSDO)".
+ */
+#define BSDO_BATTERY_INFO_CHARGING 0x1
+#define BSDO_BATTERY_INFO_DISCHARGING 0x2
+#define BSDO_BATTERY_INFO_IDLE 0x3
+#define BSDO_BATTERY_INFO_RSVD 0x4
+
+/**
+ * BSDO() - Pack data into Battery Status Data Object format
+ * @batt_charge: Battery's present state of charge in 0.1WH increment
+ * @chg_status: Battery charge status
+ * @batt_present: When set, this indicates battery is present/attached.
+ * Otherwise:
+ * - Non hot-swappable battery: Indicates absence of battery
+ * - Hot-swappable battery: Indicates battery is unattached
+ * @invalid_ref: Set when invalid battery reference is made in
+ * Get_Battery_Status request, else 0
+ */
+#define BSDO(batt_charge, chg_status, batt_present, invalid_ref) \
+ ((((batt_charge) << BSDO_PRESENT_CAPACITY_SHIFT) & BSDO_PRESENT_CAPACITY_MASK) | \
+ (((chg_status) << BSDO_CHG_STATUS_SHIFT) & BSDO_CHG_STATUS_MASK) | \
+ ((batt_present) ? BSDO_BATTERY_PRESENT : 0) | \
+ ((invalid_ref) ? BSDO_INVALID_BATTERY_REFERENCE : 0))
+
#endif /* __LINUX_USB_PD_H */

--
2.49.0.rc0.332.g42c0ae87b1-goog