From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
To: Krishna Kurapati <krishna.kurapati@oss.qualcomm.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Biju Das <biju.das.jz@bp.renesas.com>,
Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>,
linux-usb@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v6 2/2] usb: typec: hd3ss3220: Enable VBUS based on ID pin state
Date: Thu, 6 Nov 2025 12:05:12 +0200 [thread overview]
Message-ID: <aQxyfjYosVd_kPKC@kuha.fi.intel.com> (raw)
In-Reply-To: <20251102164819.2798754-3-krishna.kurapati@oss.qualcomm.com>
Hi Krishna,
Sun, Nov 02, 2025 at 10:18:19PM +0530, Krishna Kurapati kirjoitti:
> There is a ID pin present on HD3SS3220 controller that can be routed
> to SoC. As per the datasheet:
>
> "Upon detecting a UFP device, HD3SS3220 will keep ID pin high if VBUS is
> not at VSafe0V. Once VBUS is at VSafe0V, the HD3SS3220 will assert ID pin
> low. This is done to enforce Type-C requirement that VBUS must be at
> VSafe0V before re-enabling VBUS"
>
> Add support to read the ID pin state and enable VBUS accordingly.
>
> Signed-off-by: Krishna Kurapati <krishna.kurapati@oss.qualcomm.com>
> ---
> drivers/usb/typec/hd3ss3220.c | 72 +++++++++++++++++++++++++++++++++++
> 1 file changed, 72 insertions(+)
>
> diff --git a/drivers/usb/typec/hd3ss3220.c b/drivers/usb/typec/hd3ss3220.c
> index 3ecc688dda82..75fbda42eaf4 100644
> --- a/drivers/usb/typec/hd3ss3220.c
> +++ b/drivers/usb/typec/hd3ss3220.c
> @@ -15,6 +15,9 @@
> #include <linux/usb/typec.h>
> #include <linux/delay.h>
> #include <linux/workqueue.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/of_graph.h>
>
> #define HD3SS3220_REG_CN_STAT 0x08
> #define HD3SS3220_REG_CN_STAT_CTRL 0x09
> @@ -54,6 +57,11 @@ struct hd3ss3220 {
> struct delayed_work output_poll_work;
> enum usb_role role_state;
> bool poll;
> +
> + struct gpio_desc *id_gpiod;
> + int id_irq;
> +
> + struct regulator *vbus;
> };
>
> static int hd3ss3220_set_power_opmode(struct hd3ss3220 *hd3ss3220, int power_opmode)
> @@ -319,6 +327,44 @@ static const struct regmap_config config = {
> .max_register = 0x0A,
> };
>
> +static irqreturn_t hd3ss3220_id_isr(int irq, void *dev_id)
> +{
> + struct hd3ss3220 *hd3ss3220 = dev_id;
> + int ret;
> + int id;
> +
> + if (!hd3ss3220->vbus)
> + return IRQ_HANDLED;
If you don't need this routine unless there is a vbus regulator, then
don't register it at all if there is no vbus regulator.
> + id = hd3ss3220->id_gpiod ? gpiod_get_value_cansleep(hd3ss3220->id_gpiod) : 1;
You still don't need to check for hd3ss3220->id_gpiod - this function
will not get called unless it's there.
if (gpiod_get_value_cansleep(hd3ss3220->id_gpiod))
ret = regulator_disable(hd3ss3220->vbus);
else
ret = regulator_enable(hd3ss3220->vbus);
Note:
If you are concerned that the reference to the id_gpiod may be
released before this routine is unregistered, then that condition will
not help. The hd3ss3220->id_gpiod member is _not_ NULL after the
reference is released.
If you need a specific order in which the references are released,
then you can't use the resource management (devm_*) to automate things
for you.
> + if (!id) {
> + ret = regulator_enable(hd3ss3220->vbus);
> + if (ret)
> + dev_err(hd3ss3220->dev, "enable vbus regulator failed\n");
> + } else {
> + regulator_disable(hd3ss3220->vbus);
> + }
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int hd3ss3220_get_vbus_supply(struct hd3ss3220 *hd3ss3220,
> + struct fwnode_handle *connector)
> +{
> + int ret = 0;
> +
> + hd3ss3220->vbus = devm_of_regulator_get_optional(hd3ss3220->dev,
> + to_of_node(connector),
> + "vbus");
> + if (PTR_ERR(hd3ss3220->vbus) == -ENODEV)
> + hd3ss3220->vbus = NULL;
> + else if (IS_ERR(hd3ss3220->vbus))
> + ret = PTR_ERR(hd3ss3220->vbus);
So the regulator API's optional functions return -ENODEV instead of NULL :(
In any case, don't double assign the member. Use local variable.
struct regulator *vbus;
vbus = devm_of_regulator_get_optional(...
if (IS_ERR(vbus) && vbus != ERR_PTR(-ENODEV))
return PTR_ERR(vbus);
hd3ss3220->vbus = vbus;
return 0;
I don't think you need this function - just do that in the probe function.
> + return ret;
> +}
> +
> static int hd3ss3220_probe(struct i2c_client *client)
> {
> struct typec_capability typec_cap = { };
> @@ -354,11 +400,37 @@ static int hd3ss3220_probe(struct i2c_client *client)
> hd3ss3220->role_sw = usb_role_switch_get(hd3ss3220->dev);
> }
>
> + hd3ss3220->id_gpiod = devm_gpiod_get_optional(hd3ss3220->dev, "id", GPIOD_IN);
> + if (IS_ERR(hd3ss3220->id_gpiod))
> + return PTR_ERR(hd3ss3220->id_gpiod);
> +
> + if (hd3ss3220->id_gpiod) {
> + hd3ss3220->id_irq = gpiod_to_irq(hd3ss3220->id_gpiod);
> + if (hd3ss3220->id_irq < 0)
> + return dev_err_probe(hd3ss3220->dev, hd3ss3220->id_irq,
> + "failed to get ID gpio\n");
> +
> + ret = devm_request_threaded_irq(hd3ss3220->dev,
> + hd3ss3220->id_irq, NULL,
> + hd3ss3220_id_isr,
> + IRQF_TRIGGER_RISING |
> + IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
> + dev_name(hd3ss3220->dev), hd3ss3220);
> + if (ret < 0)
> + return dev_err_probe(hd3ss3220->dev, ret, "failed to get ID irq\n");
> + }
> +
> if (IS_ERR(hd3ss3220->role_sw)) {
> ret = PTR_ERR(hd3ss3220->role_sw);
> goto err_put_fwnode;
> }
>
> + ret = hd3ss3220_get_vbus_supply(hd3ss3220, connector);
> + if (ret) {
> + dev_err(hd3ss3220->dev, "failed to get vbus: %d\n", ret);
> + goto err_put_fwnode;
> + }
> +
> typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
> typec_cap.driver_data = hd3ss3220;
> typec_cap.type = TYPEC_PORT_DRP;
> --
> 2.34.1
thanks,
--
heikki
next prev parent reply other threads:[~2025-11-06 10:05 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-02 16:48 [PATCH v6 0/2] Implement vbus support for HD3SS3220 port controller Krishna Kurapati
2025-11-02 16:48 ` [PATCH v6 1/2] dt-bindings: usb: ti,hd3ss3220: Add support for VBUS based on ID state Krishna Kurapati
2025-11-02 16:48 ` [PATCH v6 2/2] usb: typec: hd3ss3220: Enable VBUS based on ID pin state Krishna Kurapati
2025-11-06 10:05 ` Heikki Krogerus [this message]
2025-11-06 10:16 ` Heikki Krogerus
2025-11-06 15:23 ` Krishna Kurapati PSSNV
2025-11-06 15:31 ` Krishna Kurapati PSSNV
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=aQxyfjYosVd_kPKC@kuha.fi.intel.com \
--to=heikki.krogerus@linux.intel.com \
--cc=biju.das.jz@bp.renesas.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dmitry.baryshkov@oss.qualcomm.com \
--cc=gregkh@linuxfoundation.org \
--cc=krishna.kurapati@oss.qualcomm.com \
--cc=krzk+dt@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=robh@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