[PATCH v3 2/2] usb: typec: tcpci: support setting orientation via GPIO

Xu Yang posted 2 patches 3 weeks ago
There is a newer version of this series
[PATCH v3 2/2] usb: typec: tcpci: support setting orientation via GPIO
Posted by Xu Yang 3 weeks ago
If the chip indicates its "Connection Orientation" standard output control
in STANDARD_OUTPUT_CAPABILITIES register, it can do the thing by
programming CONFIG_STANDARD_OUTPUT register. Due to the optional feature,
the chip which not present this capability currently doesn't have a way to
correctly set the data path. This add the support to set orientation via
a simple GPIO.

Signed-off-by: Xu Yang <xu.yang_2@nxp.com>

---
Changes in v3:
 - use "err = !!orient_gpio"
Changes in v2:
 - return early in tcpci_set_orientation() if using gpio method
---
 drivers/usb/typec/tcpm/tcpci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/drivers/usb/typec/tcpm/tcpci.c b/drivers/usb/typec/tcpm/tcpci.c
index 8b7e6eb92ca2..c3f478202fbf 100644
--- a/drivers/usb/typec/tcpm/tcpci.c
+++ b/drivers/usb/typec/tcpm/tcpci.c
@@ -7,6 +7,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/delay.h>
+#include <linux/gpio/consumer.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/i2c.h>
@@ -42,6 +43,7 @@ struct tcpci {
 
 	struct tcpc_dev tcpc;
 	struct tcpci_data *data;
+	struct gpio_desc *orientation_gpio;
 };
 
 struct tcpci_chip {
@@ -316,6 +318,10 @@ static int tcpci_set_orientation(struct tcpc_dev *tcpc,
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
 	unsigned int reg;
 
+	if (tcpci->orientation_gpio)
+		return gpiod_set_value_cansleep(tcpci->orientation_gpio,
+						orientation == TYPEC_ORIENTATION_NORMAL ? 0 : 1);
+
 	switch (orientation) {
 	case TYPEC_ORIENTATION_NONE:
 		/* We can't put a single output into high impedance */
@@ -903,6 +909,7 @@ EXPORT_SYMBOL_GPL(tcpci_unregister_port);
 static int tcpci_probe(struct i2c_client *client)
 {
 	struct tcpci_chip *chip;
+	struct gpio_desc *orient_gpio = NULL;
 	int err;
 	u16 val = 0;
 
@@ -931,12 +938,23 @@ static int tcpci_probe(struct i2c_client *client)
 	if (err < 0)
 		return err;
 
+	if (err == 0) {
+		orient_gpio = devm_gpiod_get_optional(&client->dev, "orientation",
+						      GPIOD_OUT_LOW);
+		if (IS_ERR(orient_gpio))
+			return dev_err_probe(&client->dev, PTR_ERR(orient_gpio),
+					"unable to acquire orientation gpio\n");
+		err = !!orient_gpio;
+	}
+
 	chip->data.set_orientation = err;
 
 	chip->tcpci = tcpci_register_port(&client->dev, &chip->data);
 	if (IS_ERR(chip->tcpci))
 		return PTR_ERR(chip->tcpci);
 
+	chip->tcpci->orientation_gpio = orient_gpio;
+
 	err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
 					_tcpci_irq,
 					IRQF_SHARED | IRQF_ONESHOT,

-- 
2.34.1
Re: [PATCH v3 2/2] usb: typec: tcpci: support setting orientation via GPIO
Posted by Heikki Krogerus 2 weeks, 5 days ago
Hi,

Mon, Mar 16, 2026 at 05:41:56PM +0800, Xu Yang wrote:
> @@ -316,6 +318,10 @@ static int tcpci_set_orientation(struct tcpc_dev *tcpc,
>  	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
>  	unsigned int reg;
>  
> +	if (tcpci->orientation_gpio)
> +		return gpiod_set_value_cansleep(tcpci->orientation_gpio,
> +						orientation == TYPEC_ORIENTATION_NORMAL ? 0 : 1);

Sorry, I though this was covered in the last version. The condition
looks unnecessary, so:

		return gpiod_set_value_cansleep(tcpci->orientation_gpio,
                                        	orientation != TYPEC_ORIENTATION_NORMAL);

thanks,

-- 
heikki
Re: [PATCH v3 2/2] usb: typec: tcpci: support setting orientation via GPIO
Posted by Xu Yang 2 weeks, 4 days ago
On Wed, Mar 18, 2026 at 02:10:01PM +0200, Heikki Krogerus wrote:
> Hi,
> 
> Mon, Mar 16, 2026 at 05:41:56PM +0800, Xu Yang wrote:
> > @@ -316,6 +318,10 @@ static int tcpci_set_orientation(struct tcpc_dev *tcpc,
> >  	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
> >  	unsigned int reg;
> >  
> > +	if (tcpci->orientation_gpio)
> > +		return gpiod_set_value_cansleep(tcpci->orientation_gpio,
> > +						orientation == TYPEC_ORIENTATION_NORMAL ? 0 : 1);
> 
> Sorry, I though this was covered in the last version. The condition
> looks unnecessary, so:
> 
> 		return gpiod_set_value_cansleep(tcpci->orientation_gpio,
>                                         	orientation != TYPEC_ORIENTATION_NORMAL);

OK. It's better now.

Thanks,
Xu Yang