From: Amit Sunil Dhamne via B4 Relay <devnull+amitsd.google.com@kernel.org>
To: Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Badhri Jagan Sridharan <badhri@google.com>,
Sebastian Reichel <sre@kernel.org>,
Heikki Krogerus <heikki.krogerus@linux.intel.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Len Brown <len.brown@intel.com>, Pavel Machek <pavel@kernel.org>
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-usb@vger.kernel.org, linux-pm@vger.kernel.org,
RD Babiera <rdbabiera@google.com>, Kyle Tso <kyletso@google.com>,
Amit Sunil Dhamne <amitsd@google.com>
Subject: [PATCH 3/5] usb: typec: tcpm: Add support for Battery Status response message
Date: Wed, 12 Mar 2025 16:42:03 -0700 [thread overview]
Message-ID: <20250312-batt_ops-v1-3-88e0bb3129fd@google.com> (raw)
In-Reply-To: <20250312-batt_ops-v1-0-88e0bb3129fd@google.com>
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
next prev parent reply other threads:[~2025-03-12 23:43 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-12 23:42 [PATCH 0/5] Add support for Battery Status & Battery Caps AMS in TCPM Amit Sunil Dhamne via B4 Relay
2025-03-12 23:42 ` [PATCH 1/5] dt-bindings: connector: add fixed-batteries property Amit Sunil Dhamne via B4 Relay
2025-03-13 8:48 ` Krzysztof Kozlowski
2025-03-15 0:56 ` Amit Sunil Dhamne
2025-03-16 16:49 ` Krzysztof Kozlowski
2025-03-20 19:49 ` Amit Sunil Dhamne
2025-03-16 16:55 ` Krzysztof Kozlowski
2025-03-20 19:45 ` Amit Sunil Dhamne
2025-03-12 23:42 ` [PATCH 2/5] power: supply: core: add function to get supplies from fwnode Amit Sunil Dhamne via B4 Relay
2025-03-19 13:54 ` Heikki Krogerus
2025-04-08 19:54 ` Amit Sunil Dhamne
2025-03-12 23:42 ` Amit Sunil Dhamne via B4 Relay [this message]
2025-03-12 23:42 ` [PATCH 4/5] power: supply: core: add vendor and product id properties Amit Sunil Dhamne via B4 Relay
2025-03-12 23:42 ` [PATCH 5/5] usb: typec: tcpm: Add support for Battery Cap response message Amit Sunil Dhamne via B4 Relay
2025-03-13 8:50 ` [PATCH 0/5] Add support for Battery Status & Battery Caps AMS in TCPM Krzysztof Kozlowski
2025-03-15 0:49 ` Amit Sunil Dhamne
2025-03-16 16:52 ` Krzysztof Kozlowski
2025-03-20 21:11 ` Amit Sunil Dhamne
2025-03-21 7:51 ` Krzysztof Kozlowski
2025-04-03 3:41 ` Amit Sunil Dhamne
2025-04-03 8:00 ` Krzysztof Kozlowski
2025-04-03 8:02 ` Krzysztof Kozlowski
2025-04-08 19:50 ` Amit Sunil Dhamne
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250312-batt_ops-v1-3-88e0bb3129fd@google.com \
--to=devnull+amitsd.google.com@kernel.org \
--cc=amitsd@google.com \
--cc=badhri@google.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=krzk+dt@kernel.org \
--cc=kyletso@google.com \
--cc=len.brown@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=pavel@kernel.org \
--cc=rafael@kernel.org \
--cc=rdbabiera@google.com \
--cc=robh@kernel.org \
--cc=sre@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox