From patchwork Thu May 8 01:00:22 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: 889045 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 99F9C28F4; 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=uEYxbuJqRx5iWmPrlH9JGSFYZGB18ukrJMvHvGJdWeBzegYpuYbdgQlTFn3JrzvQRfXs/5txId6ER9bmubChavk32vXJ+vQz3XAEryQFI58SDXHwxKyMFQpm3rh5BhHf5ZXRnJttWS4yRbMKpduH5sGqfumLAoG51FtwrOfinEc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746666155; c=relaxed/simple; bh=82zQ2CvBZVOxHNGr6D3/jsJpXis3IqM1RlJcYLFTOnE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OVXYfTXDRkDGh09zfo3zATyvxH1E4dQXu1q/rVsHpXPelFgkRlWzZEfJBedoCNNsk4QsuC7swb5//PgK1sX6tlvKD1dKUrxctTQd+BVgjJPeIFbc2D6p7ieKjzu66YrADaLIiKRoyadljSEqTBKnW++lXgdhIJFyV7s4SbfO5FM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i3zHAlxl; 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="i3zHAlxl" Received: by smtp.kernel.org (Postfix) with ESMTPS id 25016C4CEE8; 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=82zQ2CvBZVOxHNGr6D3/jsJpXis3IqM1RlJcYLFTOnE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=i3zHAlxl6fa4U0YW7H9Rz8P8O4mAsEIUmsPbqF072HpKXzVFnCFx0FXPfXE8Bcfu2 GkJbZg1M5hXfoTpryIFYZXH/6dfGQiGsrDhiGDdjI4EMfEINNgrjeVcSmgHe5KGrz3 fa7dBMIpYnLXeIHP/zZuBOr8SAZc1y6GDi66DXUnjYi2OQj1hjACqWmDNbPWSxXm1I vbI5O/idhHZUS6L3rol05LgaN1azgHpzvZXrpCn5YhgX410zgWlTVN0fVqZeze4tcx pvLSmTxo6YC0baz6GR8XZziWaBP8Wmf1gNESR+oHPFgzhjY0w9Oc8QW7BSKur6U8w8 jVFWi03dXTLoQ== 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 138F7C3ABC3; Thu, 8 May 2025 01:02:35 +0000 (UTC) From: Amit Sunil Dhamne via B4 Relay Date: Wed, 07 May 2025 18:00:22 -0700 Subject: [PATCH v2 1/5] dt-bindings: connector: extend ports property to model power connections 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-1-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=4228; i=amitsd@google.com; s=20241031; h=from:subject:message-id; bh=gzckttDcjypwX3XJfOW+9CCJ+fMJ1rLKGVz78d8daH4=; b=nPp1lTueEhju1m5Sn4i9ftpiX0yNxkrNTExn3+TI2bSc6CCDa5b+PnGXV35Ik0pWDji4yb3lP dukbFeUi908AI9W+JaRxXkmXwwON4Wu5Do633UkqwdLTgVCYS6VGd0y 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 Extend ports property to model power lines going between connector to charger or battery/batteries. As an example, connector VBUS can supply power in & out of the battery for a DRP. Additionally, add ports property to maxim,max33359 controller example. Signed-off-by: Amit Sunil Dhamne --- .../bindings/connector/usb-connector.yaml | 20 +++++++++++------ .../devicetree/bindings/usb/maxim,max33359.yaml | 25 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Documentation/devicetree/bindings/connector/usb-connector.yaml b/Documentation/devicetree/bindings/connector/usb-connector.yaml index 11e40d225b9f3a0d0aeea7bf764f1c00a719d615..706094f890026d324e6ece8b0c1e831d04d51eb7 100644 --- a/Documentation/devicetree/bindings/connector/usb-connector.yaml +++ b/Documentation/devicetree/bindings/connector/usb-connector.yaml @@ -181,16 +181,16 @@ properties: port: $ref: /schemas/graph.yaml#/properties/port - description: OF graph bindings modeling a data bus to the connector, e.g. - there is a single High Speed (HS) port present in this connector. If there - is more than one bus (several port, with 'reg' property), they can be grouped - under 'ports'. + description: OF graph binding to model a logical connection between a device + and connector. This connection may represent a data bus or power line. For + e.g. a High Speed (HS) data port present in this connector or VBUS line. + If there is more than one connection (several port, with 'reg' property), + they can be grouped under 'ports'. ports: $ref: /schemas/graph.yaml#/properties/ports - description: OF graph bindings modeling any data bus to the connector - unless the bus is between parent node and the connector. Since a single - connector can have multiple data buses every bus has an assigned OF graph + description: OF graph bindings to model multiple "port". Since a connector + may have multiple logical connections each one has an assigned OF graph port number as described below. properties: @@ -207,6 +207,12 @@ properties: description: Sideband Use (SBU), present in USB-C. This describes the alternate mode connection of which SBU is a part. + port@3: + $ref: /schemas/graph.yaml#/properties/port + description: VBUS/VCHGIN present in USB-C connector to model power line + going in and/or out of the charger/battery. If there are multiple + batteries then this port should contain those many endpoints. + required: - port@0 diff --git a/Documentation/devicetree/bindings/usb/maxim,max33359.yaml b/Documentation/devicetree/bindings/usb/maxim,max33359.yaml index 3de4dc40b79192b60443421b557bd2fb18683bf7..730d5c1cc9ddf1ddeff055c00ee172745297633d 100644 --- a/Documentation/devicetree/bindings/usb/maxim,max33359.yaml +++ b/Documentation/devicetree/bindings/usb/maxim,max33359.yaml @@ -75,6 +75,31 @@ examples: PDO_FIXED(9000, 2000, 0)>; sink-bc12-completion-time-ms = <500>; pd-revision = /bits/ 8 <0x03 0x01 0x01 0x08>; + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + usbc0_orien_sw: endpoint { + remote-endpoint = <&usbdrd31_phy_orien_switch>; + }; + }; + + port@1 { + reg = <1>; + usbc0_role_sw: endpoint { + remote-endpoint = <&usbdrd31_dwc3_role_switch>; + }; + }; + + port@3 { + reg = <3>; + vbus_batt: endpoint { + remote-endpoint = <&max17201_fg>; + }; + }; + }; }; }; }; 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 */ From patchwork Thu May 8 01:00:25 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: 889044 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 AF1A9208D0; 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=o/opnrqq0Vj4UK9T5ECALmL7+3lfL+jk27jpAlpDX3okzYR43x3Nr41wdC2vvOzyNKW4kvyFWF4ulQ/JiRQO5HjpLv73CjpxxWqQ6qHyHI+Wsmxsrw4M2SuK57ijnrhL4C+r9kz0yE04XDO/wyt91k/xrLdEfJU5YmZi/rJ8f/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746666155; c=relaxed/simple; bh=/7YObtV03/PH0CZkIklZ+NjmOVa5AkOnBVyqgm4mkLY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JTQvDBmmLwVyhUgLo/rW/OLzLRHIBQ51n6/XpEFDJwAsrN22MO8Rs4jLKh/PKDx+NZRmJv9BCdCFDaOLeGrD2hsoWZ4bYYUsJbmGz39YfITkktHGf5ecTPLEhXjfH94f1X4hVNBHGgRbGnEQ5NsrgOwLAXwipn7xIW5RpXU5Llk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N3XYQq57; 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="N3XYQq57" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4E4E8C4CEFA; 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=/7YObtV03/PH0CZkIklZ+NjmOVa5AkOnBVyqgm4mkLY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=N3XYQq57EQanZ0lGH4IfohlciKYAzxUIuXUM1a/Jlj7QQuWHeDLVQ4RPcHwecMQBR 1/5kZFJ0/Dt96jkfeEitgtGioByy7sz+v03Eu85gWLNr/ZclRfYNQS2Q7dXxgM18jP 1T7cKl35lLrqrH/Hd0cfxIisxEgjgc6Be86q3Bq6y3IRSdYuNcUKRlo7qNpYde4j3+ pA16L+j90wI6v7widpuUQ/5Ln1AGPmS2AKM0F0X/QyTLxd8RDV4qzucvqp6lke14Q1 JefYMj5im5BKG1WUwuCUN4La3tOdKR7l9oWkUbWUUGvs2/UPBZ28n+IqMghqBc0dkY Z0MKIh1/5skIg== 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 460A5C3ABC6; Thu, 8 May 2025 01:02:35 +0000 (UTC) From: Amit Sunil Dhamne via B4 Relay Date: Wed, 07 May 2025 18:00:25 -0700 Subject: [PATCH v2 4/5] power: supply: core: add vendor and product id properties 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-4-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=4662; i=amitsd@google.com; s=20241031; h=from:subject:message-id; bh=g9p/DCU+bGgdFEAH/ycTnIAAglqng7UgwLdvZfFI4iw=; b=Yz1RUaHYwcb+mtpsdlRbZycn+wl+0jQ9bh7UaEA2QIuNOyFQIc6NuhyTAOLMs6jeypEDdXsYd NUTZ8vjF+LJBaA7Zzp/r+mHGOgXh7QxsTX/iShsM5v+JAT4FQ/mOZTV 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 the following properties: * Vendor Identifier (VID): Assigned to the battery manufacturer by USB Implementers Forum (USB-IF). * Product Identifier (PID) assigned by the manufacturer to the battery. This info is required by USB Type-C PD devices containing batteries. This enables the USB Type C devices to respond to a Battery capacity request from the port partner by querying for the PID & VID assigned to the batteries. Refer to "USB Power Delivery Specification Rev3.1 v1.8" Chapter 5.5 Battery_Capabilities Message. Signed-off-by: Amit Sunil Dhamne --- Documentation/ABI/testing/sysfs-class-power | 19 +++++++++++++++---- Documentation/power/power_supply_class.rst | 11 +++++++++++ drivers/power/supply/power_supply_sysfs.c | 2 ++ include/linux/power_supply.h | 2 ++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-class-power b/Documentation/ABI/testing/sysfs-class-power index 2a5c1a09a28f91beec6b18ca7b4492093026bc81..5495e82885b2294cdfd5ace0e7e5fcbeadfccb5f 100644 --- a/Documentation/ABI/testing/sysfs-class-power +++ b/Documentation/ABI/testing/sysfs-class-power @@ -814,11 +814,22 @@ Description: Access: Read Valid values: 1-31 -What: /sys/class/power_supply//extensions/ -Date: March 2025 +What: /sys/class/power_supply//usbif_vendor_id +Date: May 2025 Contact: linux-pm@vger.kernel.org Description: - Reports the extensions registered to the power supply. - Each entry is a link to the device which registered the extension. + Reports the vendor id assigned to the battery manufacturer by USB + Implementers Forum (USB-IF). Access: Read + Valid values: 0x0-0xffff + +What: /sys/class/power_supply//usbif_product_id +Date: May 2025 +Contact: linux-pm@vger.kernel.org +Description: + Reports the product id assigned to the battery by the manufacturer + (associated with usbif_vendor_id). + + Access: Read + Valid values: 0x0-0xffff diff --git a/Documentation/power/power_supply_class.rst b/Documentation/power/power_supply_class.rst index da8e275a14ffb9f84bae9ae1efc4720a55ea3010..6d0a6bcf501e719fa4454845b583a8b38d371bb4 100644 --- a/Documentation/power/power_supply_class.rst +++ b/Documentation/power/power_supply_class.rst @@ -213,6 +213,17 @@ TIME_TO_FULL seconds left for battery to be considered full (i.e. while battery is charging) +USBIF_VENDOR_ID + Vendor ID (VID) assigned to manufacturer or device vendor associated with the + battery by USB Implementers Forum (USB-IF). This property is described in + "USB Power Delivery Specification Rev3.1 V1.8" Chapter 6.5.5 Battery + Capabilities, Section 6.5.5.1 Vendor ID (VID). +USBIF_PRODUCT_ID + Product ID (PID) assigned to the battery, such that if the VID belongs to the + manufacturer then the PID will be designated by it. Similarly if the VID + belongs to the device vendor then the PID will be designated by it. This + property is described in "USB Power Delivery Specification Rev3.1 V1.8" + Chapter 6.5.5 Battery Capabilities, Section 6.5.5.2 Product ID (PID). Battery <-> external power supply interaction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c index edb058c19c9c44ad9ad97a626fc8f59e3d3735a6..534ed3cd049866fa747455bb6dae1ec2dc5e2da6 100644 --- a/drivers/power/supply/power_supply_sysfs.c +++ b/drivers/power/supply/power_supply_sysfs.c @@ -211,6 +211,8 @@ static struct power_supply_attr power_supply_attrs[] __ro_after_init = { POWER_SUPPLY_ATTR(TIME_TO_EMPTY_AVG), POWER_SUPPLY_ATTR(TIME_TO_FULL_NOW), POWER_SUPPLY_ATTR(TIME_TO_FULL_AVG), + POWER_SUPPLY_ATTR(USBIF_VENDOR_ID), + POWER_SUPPLY_ATTR(USBIF_PRODUCT_ID), POWER_SUPPLY_ENUM_ATTR(TYPE), POWER_SUPPLY_ENUM_ATTR(USB_TYPE), POWER_SUPPLY_ENUM_ATTR(SCOPE), diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index a35b08bd368e9305554e1a608dc8e526983cfa12..100eb559dcede938595ffbf83bc5ef3645a5a172 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -165,6 +165,8 @@ enum power_supply_property { POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, + POWER_SUPPLY_PROP_USBIF_VENDOR_ID, + POWER_SUPPLY_PROP_USBIF_PRODUCT_ID, POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */ POWER_SUPPLY_PROP_USB_TYPE, POWER_SUPPLY_PROP_SCOPE, From patchwork Thu May 8 01:00:26 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: 889043 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 1F57D19EED3; 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=1746666156; cv=none; b=RpQ/LwKTXZoak+zmiMOSjZuVFEuf5tJ1CF1qOnjfTkjlozoKU5XXJOSzjfsEn3rNIzNd4hb1WMaxAnon7ZIoY8CJPEA2uj42G9mcDI64ZlMQIyrEcBDLYKD6lCSOEvCABFi7dO4eET3ujNa+FGV9wr7hcOmXAnIgyJK3vqj1nf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746666156; c=relaxed/simple; bh=+STG5ShdUtz33BaUGXhezEcq64LbHRhrK4VC6uqXvy0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jb5I6WBLkh+irN0JAHGcwIND5E9JS02F0oJieGYR86VObV2A/9k4oK7BeNzyPpkCbz4w9zrpX/UTGLiHiNmedQME0PZ3xTqarmBcDQ4rq5PASx1TJQ6bAyfyrgPNKSs/H9JPE/p2wSOuI1z0O/h+xojLKl+Lx1RTlghGboPUnwE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R8VuVxEY; 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="R8VuVxEY" Received: by smtp.kernel.org (Postfix) with ESMTPS id 5C8BCC4CEFD; 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=+STG5ShdUtz33BaUGXhezEcq64LbHRhrK4VC6uqXvy0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=R8VuVxEY+SmkzM8vw2JS/+on9tDO+FzU8wO72hJfGHZSHkSRN/llSWkFAnYG97P+l R0qMqeQl2b/qvKbH2lnXtnGlNRiq38qOqfd96GC6bPGoOjttasniR30yzRtvN39Kd3 g5JBoGnLFN1VlPNu3p+Q9hgg8Y5fyrolm3LjwrIOBPqsljiLF+BiNAFhi8tZx9PliR qoRa1pawWBgMoaIvLa8LIuNQjvIMRT3tMmFQPOAGyHps5Q69SUsybrBTVlTF/ciG4+ ya5lEDfitlx6WvHfjnBG2CbxZqyTYWzJmC/NmjI/qTFtnroUSWWzWOshzV0jEydTg5 joBtTOrbKB1Sw== 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 5422AC3ABCA; Thu, 8 May 2025 01:02:35 +0000 (UTC) From: Amit Sunil Dhamne via B4 Relay Date: Wed, 07 May 2025 18:00:26 -0700 Subject: [PATCH v2 5/5] usb: typec: tcpm: Add support for Battery Cap 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-5-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=6782; i=amitsd@google.com; s=20241031; h=from:subject:message-id; bh=B5Z1TgNEutKpwFmhUKlqLMwh2csQDHuFOD4jLxln/2I=; b=aJc/rEl2IzAJabyp4WcVnvPNkacPjz5hhoAO38TwyUp386o6LnogwRsEycR5NxPDx/tq9Jpip 4vzR/FQ7YWAA/tem+HTZiQyBanjk83x6bwtzOj5Fn988g9X5wZMYgTw 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_Cap (extended) request with a a Battery_Capabilities (extended) msg. The requester will request Battery Cap for a specific battery using an index in Get_Battery_Cap. In case of failure to identify battery, TCPM shall reply with an appropriate message indicating so. Support has been added only for fixed batteries and not hot swappable ones. As the Battery Cap Data Block size is 9 Bytes (lesser than MaxExtendedMsgChunkLen of 26B), only a single chunk is required to complete the AMS. Support for Battery_Capabilities 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 | 96 +++++++++++++++++++++++++++++++++++++++++-- include/linux/usb/pd.h | 31 ++++++++++++++ 2 files changed, 123 insertions(+), 4 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index 98df0c7ce00e43f6c95ab49237a414e1b4413369..4731126fbf19a50178be0cf8867b3fe08595724c 100644 --- a/drivers/usb/typec/tcpm/tcpm.c +++ b/drivers/usb/typec/tcpm/tcpm.c @@ -228,7 +228,8 @@ enum pd_msg_request { PD_MSG_DATA_SINK_CAP, PD_MSG_DATA_SOURCE_CAP, PD_MSG_DATA_REV, - PD_MSG_DATA_BATT_STATUS + PD_MSG_DATA_BATT_STATUS, + PD_MSG_EXT_BATT_CAP, }; enum adev_actions { @@ -597,8 +598,8 @@ struct tcpm_port { u8 fixed_batt_cnt; /* - * Variable used to store battery_ref from the Get_Battery_Status - * request to process Battery_Status messages. + * Variable used to store battery_ref from the Get_Battery_Status & + * Get_Battery_Caps request to process Battery_Status messages. */ u8 batt_request; #ifdef CONFIG_DEBUG_FS @@ -1414,6 +1415,81 @@ static int tcpm_pd_send_batt_status(struct tcpm_port *port) return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); } +static int tcpm_pd_send_batt_cap(struct tcpm_port *port) +{ + struct pd_message msg; + struct power_supply *batt; + struct batt_cap_ext_msg bcdb; + u32 batt_id = port->batt_request; + int ret; + union power_supply_propval val; + bool batt_present = false; + u16 batt_design_cap = BATTERY_PROPERTY_UNKNOWN; + u16 batt_charge_cap = BATTERY_PROPERTY_UNKNOWN; + u8 data_obj_cnt; + /* + * As per USB PD Rev3.1 v1.8, if battery reference is incorrect, + * then set the VID field to 0xffff. + * If VID field is set to 0xffff, always set the PID field to + * 0x0000. + */ + u16 vid = BATTERY_PROPERTY_UNKNOWN; + u16 pid = 0x0; + + 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_USBIF_VENDOR_ID, + &val); + if (!ret) + vid = val.intval; + + if (vid != BATTERY_PROPERTY_UNKNOWN) { + ret = power_supply_get_property(batt, + POWER_SUPPLY_PROP_USBIF_PRODUCT_ID, + &val); + if (!ret) + pid = val.intval; + } + + ret = power_supply_get_property(batt, + POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, + &val); + if (!ret) + batt_design_cap = (u16)UWH_TO_WH(val.intval * 10); + + ret = power_supply_get_property(batt, + POWER_SUPPLY_PROP_ENERGY_FULL, + &val); + if (!ret) + batt_charge_cap = (u16)UWH_TO_WH(val.intval * 10); + } + + bcdb.vid = cpu_to_le16(vid); + bcdb.pid = cpu_to_le16(pid); + bcdb.batt_design_cap = cpu_to_le16(batt_design_cap); + bcdb.batt_last_chg_cap = cpu_to_le16(batt_charge_cap); + bcdb.batt_type = !batt_present ? BATT_CAP_BATT_TYPE_INVALID_REF : 0; + memcpy(msg.ext_msg.data, &bcdb, sizeof(bcdb)); + msg.ext_msg.header = PD_EXT_HDR_LE(sizeof(bcdb), + 0, /* Denotes if request chunk */ + 0, /* Chunk number */ + 1 /* Chunked */); + + data_obj_cnt = count_chunked_data_objs(sizeof(bcdb)); + msg.header = cpu_to_le16(PD_HEADER(PD_EXT_BATT_CAP, + port->pwr_role, + port->data_role, + port->negotiated_rev, + port->message_id, + data_obj_cnt, + 1 /* Denotes if ext header */)); + 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) { @@ -3711,8 +3787,12 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port, 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: + port->batt_request = ext_msg->data[0]; + tcpm_pd_handle_msg(port, PD_MSG_EXT_BATT_CAP, + GETTING_BATTERY_CAPABILITIES); + break; + case PD_EXT_SOURCE_CAP_EXT: case PD_EXT_BATT_CAP: case PD_EXT_GET_MANUFACTURER_INFO: case PD_EXT_MANUFACTURER_INFO: @@ -3921,6 +4001,14 @@ static bool tcpm_send_queued_message(struct tcpm_port *port) ret); tcpm_ams_finish(port); break; + case PD_MSG_EXT_BATT_CAP: + ret = tcpm_pd_send_batt_cap(port); + if (ret) + tcpm_log(port, + "Failed to send battery cap ret=%d", + ret); + tcpm_ams_finish(port); + break; default: break; } diff --git a/include/linux/usb/pd.h b/include/linux/usb/pd.h index 4efa7bfd9863915dfc8048da264116d9b05a447b..c89594177da57f4398b75c89c1991b4937614a70 100644 --- a/include/linux/usb/pd.h +++ b/include/linux/usb/pd.h @@ -204,6 +204,37 @@ struct pd_message { }; } __packed; +/* + * count_chunked_data_objs: Helper to calculate number of Data Objects on a 4 + * byte boundary. + * @size: Size of data block for extended message. Should *not* include extended + * header size. + */ +static inline u8 count_chunked_data_objs(u32 size) +{ + size += offsetof(struct pd_chunked_ext_message_data, data); + return ((size / 4) + (size % 4 ? 1 : 0)); +} + +/** + * batt_cap_ext_msg - Battery capability extended PD message + * @vid: Battery Vendor ID (assigned by USB-IF) + * @pid: Battery Product ID (assigned by battery or device vendor) + * @batt_design_cap: Battery design capacity in 0.1Wh + * @batt_last_chg_cap: Battery last full charge capacity in 0.1Wh + * @batt_type: Battery Type. bit0 when set indicates invalid battery reference. + * Rest of the bits are reserved. + */ +struct batt_cap_ext_msg { + __le16 vid; + __le16 pid; + __le16 batt_design_cap; + __le16 batt_last_chg_cap; + u8 batt_type; +} __packed; + +#define BATT_CAP_BATT_TYPE_INVALID_REF BIT(0) + /* PDO: Power Data Object */ #define PDO_MAX_OBJECTS 7