From patchwork Thu May 8 01:00:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Sunil Dhamne via B4 Relay X-Patchwork-Id: 888790 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A00A175A5; Thu, 8 May 2025 01:02:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746666155; cv=none; b=jP70LKPJ+wAHJAWtJGxfpuZn23Ki6fdJH4uwpfbTp3KUNuEvyiYDrdlY+z18MfuWdQEQSgStd3jxrFNCFc3FbdCbiizZE6+n2cSqqfdt6VsrK7K5SjHCZeO9d6cRozy3Wm5vtuk9Ukz7AkEYbe41WKBv4WofQ+52rT/jXaugr9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746666155; c=relaxed/simple; bh=d4slsEd4cKmuSOAFsQWaXdJfASlTswsfHKp1hoC2dgw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Zr+VkmeTH4gPqsZ1Ws5GDDprU5Uva2V96H3UnwH85bb5jrdtIPvEG9IqematyiqQuZ07Tn4hlLfdzvpXsxQdpiyxNM9wA85xHSfWpFNKv9jbljz17gHFgWmjVPitznRtwLC2bV2xXnymxju8bZ6n8IHtbLfrv6tVX3TyzqlwSO8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YPNK+4bW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YPNK+4bW" Received: by smtp.kernel.org (Postfix) with ESMTPS id 320B5C4CEE7; Thu, 8 May 2025 01:02:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746666155; bh=d4slsEd4cKmuSOAFsQWaXdJfASlTswsfHKp1hoC2dgw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=YPNK+4bWrL9bYqY5Uumx76t66XueUiJ9dU0C0G0Vb1j3AfZJwxSMexbfZOMWxtmnJ jTOHchmCaYxOJaFeyy+/Hrbja7xPLJWmNBrXYcWzAmQpIaWyx8N4FODv4fF7KmvtQs lxJp1WNtwjSQQmQYhl8c1PuojfndEJWEByKp8rZJs9KguHgZz3YBU0+pMPf6ub8C6V yFXh+7rLS5cErfnG+RDFLpEmUx5Ub5luLuJXUTTR9j62ZZxPuv93BeYukEVEUSKuap fNmxoQpDzhzi3ddbenV+T6twUmA6PXHYX/WvfAnbw8zY7waqXsvZibK+w1fma41wM8 27dpY4idyKufQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26647C3ABCA; Thu, 8 May 2025 01:02:35 +0000 (UTC) From: Amit Sunil Dhamne via B4 Relay Date: Wed, 07 May 2025 18:00:23 -0700 Subject: [PATCH v2 2/5] power: supply: core: add helper to get power supply given a fwnode Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250507-batt_ops-v2-2-8d06130bffe6@google.com> References: <20250507-batt_ops-v2-0-8d06130bffe6@google.com> In-Reply-To: <20250507-batt_ops-v2-0-8d06130bffe6@google.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Greg Kroah-Hartman , Badhri Jagan Sridharan , Sebastian Reichel , Heikki Krogerus , "Rafael J. Wysocki" , Len Brown , Pavel Machek Cc: Kyle Tso , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, linux-pm@vger.kernel.org, Amit Sunil Dhamne X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1746666154; l=2859; i=amitsd@google.com; s=20241031; h=from:subject:message-id; bh=n/LuhXwbMG+uq2FAx4zj5314rxmEnmyuEdKNHG3ZivU=; b=dTaz7OkB3YkPjDneIrBZe+0QEC+/53CmHhzD87wh4fYYWZ7UQ5H7LlQRrclRrlPHTp6tsTyXR iV8Z2mcwUyhCP0cZivtxXycLG8L081x/tLWCnn4Bz17G/s19koXnENJ X-Developer-Key: i=amitsd@google.com; a=ed25519; pk=wD+XZSST4dmnNZf62/lqJpLm7fiyT8iv462zmQ3H6bI= X-Endpoint-Received: by B4 Relay for amitsd@google.com/20241031 with auth_id=262 X-Original-From: Amit Sunil Dhamne Reply-To: amitsd@google.com From: Amit Sunil Dhamne Add a helper function power_supply_get_by_fwnode() to retrieve power_supply given a valid fwnode reference. Signed-off-by: Amit Sunil Dhamne --- drivers/power/supply/power_supply_core.c | 30 ++++++++++++++++++++++++++++++ include/linux/power_supply.h | 3 +++ 2 files changed, 33 insertions(+) diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index 76c340b38015af0a67a0d91305e6242a8646bf53..ef6ba22b837b0b9463f9a3065425e2720f40b9eb 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -496,6 +496,36 @@ struct power_supply *power_supply_get_by_name(const char *name) } EXPORT_SYMBOL_GPL(power_supply_get_by_name); +static int power_supply_match_device_by_fwnode(struct device *dev, const void *data) +{ + return dev->parent && dev_fwnode(dev->parent) == data; +} + +/** + * power_supply_get_by_fwnode() - Search for power supply given a fwnode ref. + * @fwnode: fwnode reference + * + * If power supply was found, it increases reference count for the internal + * power supply's device. The user should power_supply_put() after use. + * + * Return: Reference to power_supply node matching the fwnode on success or + * NULL on failure. + */ +struct power_supply *power_supply_get_by_fwnode(struct fwnode_handle *fwnode) +{ + struct power_supply *psy = NULL; + struct device *dev = class_find_device(&power_supply_class, NULL, fwnode, + power_supply_match_device_by_fwnode); + + if (dev) { + psy = dev_get_drvdata(dev); + atomic_inc(&psy->use_cnt); + } + + return psy; +} +EXPORT_SYMBOL_GPL(power_supply_get_by_fwnode); + /** * power_supply_put() - Drop reference obtained with power_supply_get_by_name * @psy: Reference to put diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 6ed53b292162469d7b357734d5589bff18a201d0..a35b08bd368e9305554e1a608dc8e526983cfa12 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -801,10 +801,13 @@ extern void power_supply_unreg_notifier(struct notifier_block *nb); #if IS_ENABLED(CONFIG_POWER_SUPPLY) extern struct power_supply *power_supply_get_by_name(const char *name); extern void power_supply_put(struct power_supply *psy); +extern struct power_supply *power_supply_get_by_fwnode(struct fwnode_handle *fwnode); #else static inline void power_supply_put(struct power_supply *psy) {} static inline struct power_supply *power_supply_get_by_name(const char *name) { return NULL; } +static inline struct power_supply *power_supply_get_by_fwnode(struct fwnode_handle *fwnode) +{ return NULL; } #endif #ifdef CONFIG_OF extern struct power_supply *power_supply_get_by_phandle(struct device_node *np, From patchwork Thu May 8 01:00:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Sunil Dhamne via B4 Relay X-Patchwork-Id: 888789 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF17417BA5; Thu, 8 May 2025 01:02:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746666155; cv=none; b=OUisAK39shywNWp5kckTLy5Zh3VsFf3HEoc4OhD4w0KohzW2eTNj5zdGwzmSHluYv4gxSbh8yNeNdE6Nm1SxnJl02D6RlUz7qY48iwl5A0AZnI1rDUKtNwzn8j0QB7Kph3SkvoJfzyzGPlrXXa9aCFKHhTcyBoIQncIVU1ngHRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746666155; c=relaxed/simple; bh=cyH7IxUCEHB0ZrS2Lyioh5lQfU1A+6U/rmiDbLsefuM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UqrQePrKhXVnir+rh6XD/JG1d/Phi0UTCTMNrBU0OjnkWcZFdk76Dp957tnoeogURqJbbLjuFWKtpk97sIZNfsUHd7++AYXJOLKMlGRaIe1S0pwQEx8JbSZWFv2AQBMXu3ts6j5R2aMyu82UFRoYaq4wjY8bUZhjzUe+/C4s8QM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h/jfd+JZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h/jfd+JZ" Received: by smtp.kernel.org (Postfix) with ESMTPS id 434EDC4CEF4; Thu, 8 May 2025 01:02:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746666155; bh=cyH7IxUCEHB0ZrS2Lyioh5lQfU1A+6U/rmiDbLsefuM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=h/jfd+JZ7pCDQ9L5RtiotTAToE3qq1TyP/ImA6M2990uAlkuogzOJFDIViAoqhDVo MyjY3tw9OVAzC2xovhm5lAIcxNXNzj4gpMLrjhsly0NbzJtDpXnoN/eLhiXFT2ieDa cBXcRsv0ZRX0g2ndL/HBg1rPoL4lw5b7f+lt317MoCaYzbNKwI+WSjr5fYGIcMK3xz yXs+qiErROe8ZzT6WCAcZLReLnsRNdUsThDAL6zg9MASx9opmjhJaVow3HO77rFvJS D5QnuhSaPnhD/eeI6QQVCeZh9ss6T5v95PnBHN3/V5bHjdF/vi3KAUhRfg8jCYpYul smNjAWCBzLVzA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37555C3ABC0; Thu, 8 May 2025 01:02:35 +0000 (UTC) From: Amit Sunil Dhamne via B4 Relay Date: Wed, 07 May 2025 18:00:24 -0700 Subject: [PATCH v2 3/5] usb: typec: tcpm: Add support for Battery Status response message Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250507-batt_ops-v2-3-8d06130bffe6@google.com> References: <20250507-batt_ops-v2-0-8d06130bffe6@google.com> In-Reply-To: <20250507-batt_ops-v2-0-8d06130bffe6@google.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Greg Kroah-Hartman , Badhri Jagan Sridharan , Sebastian Reichel , Heikki Krogerus , "Rafael J. Wysocki" , Len Brown , Pavel Machek Cc: Kyle Tso , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, linux-pm@vger.kernel.org, Amit Sunil Dhamne X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1746666154; l=9363; i=amitsd@google.com; s=20241031; h=from:subject:message-id; bh=IJb8psU8Rbmr6NwAmHKM5OkXyc99U1I5AvRRLV9LPV4=; b=84G1QffoybG6dF+JIhHNJ5OpxM0k8HIpS9zNHjdsC1oHsF7+Xzt+EGj/WU4eCL7uUe4N/EzlK KqdOfhqpIaUDb8jJeWCzt/o0Oj9dx4Oh13K1aliS5gJ1sy10/+8fX+A X-Developer-Key: i=amitsd@google.com; a=ed25519; pk=wD+XZSST4dmnNZf62/lqJpLm7fiyT8iv462zmQ3H6bI= X-Endpoint-Received: by B4 Relay for amitsd@google.com/20241031 with auth_id=262 X-Original-From: Amit Sunil Dhamne Reply-To: amitsd@google.com From: Amit Sunil Dhamne 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 Reviewed-by: Badhri Jagan Sridharan Reviewed-by: Kyle Tso --- drivers/usb/typec/tcpm/tcpm.c | 118 +++++++++++++++++++++++++++++++++++++++++- include/linux/usb/pd.h | 34 ++++++++++++ 2 files changed, 150 insertions(+), 2 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index 6bf1a22c785aff6b1ad77a20d85e22580527f5b1..98df0c7ce00e43f6c95ab49237a414e1b4413369 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,17 @@ 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 + +#define BATTERY_PROPERTY_UNKNOWN 0xffff + +/* Convert microwatt to watt */ +#define UWH_TO_WH(pow) ((pow) / 1000000) + struct tcpm_port { struct device *dev; @@ -580,6 +592,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 +1360,60 @@ static int tcpm_pd_send_sink_caps(struct tcpm_port *port) return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); } +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,26 @@ 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) +{ + struct fwnode_handle *ep = NULL, *fwnode_batt; + struct power_supply *psy; + + fwnode_graph_for_each_endpoint(fwnode, ep) { + fwnode_batt = fwnode_graph_get_remote_port_parent(ep); + if (!fwnode_batt) + continue; + + psy = power_supply_get_by_fwnode(fwnode_batt); + if (psy && psy->desc->type == POWER_SUPPLY_TYPE_BATTERY) + port->fixed_batt[port->fixed_batt_cnt++] = psy; + + fwnode_handle_put(fwnode_batt); + if (port->fixed_batt_cnt == MAX_NUM_FIXED_BATT) + break; + } +} + static int tcpm_fw_get_caps(struct tcpm_port *port, struct fwnode_handle *fwnode) { struct fwnode_handle *capabilities, *child, *caps = NULL; @@ -7746,6 +7854,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 +7936,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..4efa7bfd9863915dfc8048da264116d9b05a447b 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 0x0 +#define BSDO_BATTERY_INFO_DISCHARGING 0x1 +#define BSDO_BATTERY_INFO_IDLE 0x2 +#define BSDO_BATTERY_INFO_RSVD 0x3 + +/** + * 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 */