diff mbox series

[v9,10/14] USB: typec: tps6598x: Add port registration for tps25750

Message ID 20231001081134.37101-11-alkuor@gmail.com (mailing list archive)
State New, archived
Headers show
Series Add TPS25750 USB type-C PD controller support | expand

Commit Message

Abdel Alkuor Oct. 1, 2023, 8:11 a.m. UTC
From: Abdel Alkuor <abdelalkuor@geotab.com>

TPS25750 doesn't have system configuration register to get dr/pr of the
current applied binary configuration.

Get data role from the device node and power role from PD status register.

Signed-off-by: Abdel Alkuor <abdelalkuor@geotab.com>
---
Changes in v9:
  - Move device data structs to the top of of_device_id
Changes in v8:
  - Change tps->cb to tps->data
Changes in v7:
  - Add driver name to commit subject
Changes in v6:
  - Add port registration to tipd data factory
Changes in v5:
  - Incorporating tps25750 into tps6598x driver

 drivers/usb/typec/tipd/core.c     | 68 ++++++++++++++++++++++++++++++-
 drivers/usb/typec/tipd/tps6598x.h | 10 +++++
 2 files changed, 77 insertions(+), 1 deletion(-)

Comments

Heikki Krogerus Oct. 3, 2023, 6:50 a.m. UTC | #1
On Sun, Oct 01, 2023 at 04:11:30AM -0400, Abdel Alkuor wrote:
> From: Abdel Alkuor <abdelalkuor@geotab.com>
> 
> TPS25750 doesn't have system configuration register to get dr/pr of the
> current applied binary configuration.
> 
> Get data role from the device node and power role from PD status register.
> 
> Signed-off-by: Abdel Alkuor <abdelalkuor@geotab.com>

This also has to be done at the same time you enable TPS25750.

> ---
> Changes in v9:
>   - Move device data structs to the top of of_device_id
> Changes in v8:
>   - Change tps->cb to tps->data
> Changes in v7:
>   - Add driver name to commit subject
> Changes in v6:
>   - Add port registration to tipd data factory
> Changes in v5:
>   - Incorporating tps25750 into tps6598x driver
> 
>  drivers/usb/typec/tipd/core.c     | 68 ++++++++++++++++++++++++++++++-
>  drivers/usb/typec/tipd/tps6598x.h | 10 +++++
>  2 files changed, 77 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c
> index 0195eabd96bf..ae08c5306707 100644
> --- a/drivers/usb/typec/tipd/core.c
> +++ b/drivers/usb/typec/tipd/core.c
> @@ -40,6 +40,7 @@
>  #define TPS_REG_CTRL_CONF		0x29
>  #define TPS_REG_BOOT_STATUS		0x2D
>  #define TPS_REG_POWER_STATUS		0x3f
> +#define TPS_REG_PD_STATUS		0x40
>  #define TPS_REG_RX_IDENTITY_SOP		0x48
>  #define TPS_REG_DATA_STATUS		0x5f
>  
> @@ -102,8 +103,11 @@ static const char *const modes[] = {
>  /* Unrecognized commands will be replaced with "!CMD" */
>  #define INVALID_CMD(_cmd_)		(_cmd_ == 0x444d4321)
>  
> +struct tps6598x;
> +
>  struct tipd_data {
>  	irq_handler_t irq_handler;
> +	int (*register_port)(struct tps6598x *tps, struct fwnode_handle *node);
>  };
>  
>  struct tps6598x {
> @@ -208,6 +212,11 @@ static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
>  	return tps6598x_block_read(tps, reg, val, sizeof(u64));
>  }
>  
> +static inline int tps6598x_write8(struct tps6598x *tps, u8 reg, u8 val)
> +{
> +	return tps6598x_block_write(tps, reg, &val, sizeof(u8));
> +}
> +
>  static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
>  {
>  	return tps6598x_block_write(tps, reg, &val, sizeof(u64));
> @@ -1084,6 +1093,60 @@ tps6598x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
>  	return 0;
>  }
>  
> +static int
> +tps25750_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
> +{
> +	struct typec_capability typec_cap = { };
> +	const char *data_role;
> +	u8 pd_status;
> +	int ret;
> +
> +	ret = tps6598x_read8(tps, TPS_REG_PD_STATUS, &pd_status);
> +	if (ret)
> +		return ret;
> +
> +	ret = fwnode_property_read_string(fwnode, "data-role", &data_role);
> +	if (ret) {
> +		dev_err(tps->dev, "data-role not found: %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = typec_find_port_data_role(data_role);
> +	if (ret < 0) {
> +		dev_err(tps->dev, "unknown data-role: %s\n", data_role);
> +		return ret;
> +	}
> +
> +	typec_cap.data = ret;
> +	typec_cap.revision = USB_TYPEC_REV_1_3;
> +	typec_cap.pd_revision = 0x300;
> +	typec_cap.driver_data = tps;
> +	typec_cap.ops = &tps6598x_ops;
> +	typec_cap.fwnode = fwnode;
> +	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
> +
> +	switch (TPS_PD_STATUS_PORT_TYPE(pd_status)) {
> +	case TPS_PD_STATUS_PORT_TYPE_SINK_SOURCE:
> +	case TPS_PD_STATUS_PORT_TYPE_SOURCE_SINK:
> +		typec_cap.type = TYPEC_PORT_DRP;
> +		break;
> +	case TPS_PD_STATUS_PORT_TYPE_SINK:
> +		typec_cap.type = TYPEC_PORT_SNK;
> +		break;
> +	case TPS_PD_STATUS_PORT_TYPE_SOURCE:
> +		typec_cap.type = TYPEC_PORT_SRC;
> +		break;
> +	default:
> +		return -ENODEV;
> +	}
> +
> +	tps->port = typec_register_port(tps->dev, &typec_cap);
> +	if (IS_ERR(tps->port))
> +		return PTR_ERR(tps->port);
> +
> +	return 0;
> +}
> +
>  static int tps6598x_probe(struct i2c_client *client)
>  {
>  	struct device_node *np = client->dev.of_node;
> @@ -1183,7 +1246,7 @@ static int tps6598x_probe(struct i2c_client *client)
>  	if (ret)
>  		goto err_role_put;
>  
> -	ret = tps6598x_register_port(tps, fwnode);
> +	ret = tps->data->register_port(tps, fwnode);
>  	if (ret)
>  		goto err_role_put;
>  
> @@ -1308,14 +1371,17 @@ static const struct dev_pm_ops tps6598x_pm_ops = {
>  
>  static const struct tipd_data cd321x_data = {
>  	.irq_handler = cd321x_interrupt,
> +	.register_port = tps6598x_register_port,
>  };
>  
>  static const struct tipd_data tps6598x_data = {
>  	.irq_handler = tps6598x_interrupt,
> +	.register_port = tps6598x_register_port,
>  };
>  
>  static const struct tipd_data tps25750_data = {
>  	.irq_handler = tps25750_interrupt,
> +	.register_port = tps25750_register_port,
>  };
>  
>  static const struct of_device_id tps6598x_of_match[] = {
> diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h
> index c000170f4547..3a9a43394134 100644
> --- a/drivers/usb/typec/tipd/tps6598x.h
> +++ b/drivers/usb/typec/tipd/tps6598x.h
> @@ -203,4 +203,14 @@
>  #define TPS_BOOT_STATUS_DEAD_BATTERY_FLAG	BIT(2)
>  #define TPS_BOOT_STATUS_I2C_EEPROM_PRESENT	BIT(3)
>  
> +/* PD STATUS REG */
> +#define TPS_REG_PD_STATUS_PORT_TYPE_MASK	GENMASK(5, 4)
> +#define TPS_PD_STATUS_PORT_TYPE(x) \
> +	TPS_FIELD_GET(TPS_REG_PD_STATUS_PORT_TYPE_MASK, x)
> +
> +#define TPS_PD_STATUS_PORT_TYPE_SINK_SOURCE	0
> +#define TPS_PD_STATUS_PORT_TYPE_SINK		1
> +#define TPS_PD_STATUS_PORT_TYPE_SOURCE		2
> +#define TPS_PD_STATUS_PORT_TYPE_SOURCE_SINK	3
> +
>  #endif /* __TPS6598X_H__ */
> -- 
> 2.34.1
diff mbox series

Patch

diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c
index 0195eabd96bf..ae08c5306707 100644
--- a/drivers/usb/typec/tipd/core.c
+++ b/drivers/usb/typec/tipd/core.c
@@ -40,6 +40,7 @@ 
 #define TPS_REG_CTRL_CONF		0x29
 #define TPS_REG_BOOT_STATUS		0x2D
 #define TPS_REG_POWER_STATUS		0x3f
+#define TPS_REG_PD_STATUS		0x40
 #define TPS_REG_RX_IDENTITY_SOP		0x48
 #define TPS_REG_DATA_STATUS		0x5f
 
@@ -102,8 +103,11 @@  static const char *const modes[] = {
 /* Unrecognized commands will be replaced with "!CMD" */
 #define INVALID_CMD(_cmd_)		(_cmd_ == 0x444d4321)
 
+struct tps6598x;
+
 struct tipd_data {
 	irq_handler_t irq_handler;
+	int (*register_port)(struct tps6598x *tps, struct fwnode_handle *node);
 };
 
 struct tps6598x {
@@ -208,6 +212,11 @@  static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
 	return tps6598x_block_read(tps, reg, val, sizeof(u64));
 }
 
+static inline int tps6598x_write8(struct tps6598x *tps, u8 reg, u8 val)
+{
+	return tps6598x_block_write(tps, reg, &val, sizeof(u8));
+}
+
 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
 {
 	return tps6598x_block_write(tps, reg, &val, sizeof(u64));
@@ -1084,6 +1093,60 @@  tps6598x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
 	return 0;
 }
 
+static int
+tps25750_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
+{
+	struct typec_capability typec_cap = { };
+	const char *data_role;
+	u8 pd_status;
+	int ret;
+
+	ret = tps6598x_read8(tps, TPS_REG_PD_STATUS, &pd_status);
+	if (ret)
+		return ret;
+
+	ret = fwnode_property_read_string(fwnode, "data-role", &data_role);
+	if (ret) {
+		dev_err(tps->dev, "data-role not found: %d\n", ret);
+		return ret;
+	}
+
+	ret = typec_find_port_data_role(data_role);
+	if (ret < 0) {
+		dev_err(tps->dev, "unknown data-role: %s\n", data_role);
+		return ret;
+	}
+
+	typec_cap.data = ret;
+	typec_cap.revision = USB_TYPEC_REV_1_3;
+	typec_cap.pd_revision = 0x300;
+	typec_cap.driver_data = tps;
+	typec_cap.ops = &tps6598x_ops;
+	typec_cap.fwnode = fwnode;
+	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
+
+	switch (TPS_PD_STATUS_PORT_TYPE(pd_status)) {
+	case TPS_PD_STATUS_PORT_TYPE_SINK_SOURCE:
+	case TPS_PD_STATUS_PORT_TYPE_SOURCE_SINK:
+		typec_cap.type = TYPEC_PORT_DRP;
+		break;
+	case TPS_PD_STATUS_PORT_TYPE_SINK:
+		typec_cap.type = TYPEC_PORT_SNK;
+		break;
+	case TPS_PD_STATUS_PORT_TYPE_SOURCE:
+		typec_cap.type = TYPEC_PORT_SRC;
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	tps->port = typec_register_port(tps->dev, &typec_cap);
+	if (IS_ERR(tps->port))
+		return PTR_ERR(tps->port);
+
+	return 0;
+}
+
 static int tps6598x_probe(struct i2c_client *client)
 {
 	struct device_node *np = client->dev.of_node;
@@ -1183,7 +1246,7 @@  static int tps6598x_probe(struct i2c_client *client)
 	if (ret)
 		goto err_role_put;
 
-	ret = tps6598x_register_port(tps, fwnode);
+	ret = tps->data->register_port(tps, fwnode);
 	if (ret)
 		goto err_role_put;
 
@@ -1308,14 +1371,17 @@  static const struct dev_pm_ops tps6598x_pm_ops = {
 
 static const struct tipd_data cd321x_data = {
 	.irq_handler = cd321x_interrupt,
+	.register_port = tps6598x_register_port,
 };
 
 static const struct tipd_data tps6598x_data = {
 	.irq_handler = tps6598x_interrupt,
+	.register_port = tps6598x_register_port,
 };
 
 static const struct tipd_data tps25750_data = {
 	.irq_handler = tps25750_interrupt,
+	.register_port = tps25750_register_port,
 };
 
 static const struct of_device_id tps6598x_of_match[] = {
diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h
index c000170f4547..3a9a43394134 100644
--- a/drivers/usb/typec/tipd/tps6598x.h
+++ b/drivers/usb/typec/tipd/tps6598x.h
@@ -203,4 +203,14 @@ 
 #define TPS_BOOT_STATUS_DEAD_BATTERY_FLAG	BIT(2)
 #define TPS_BOOT_STATUS_I2C_EEPROM_PRESENT	BIT(3)
 
+/* PD STATUS REG */
+#define TPS_REG_PD_STATUS_PORT_TYPE_MASK	GENMASK(5, 4)
+#define TPS_PD_STATUS_PORT_TYPE(x) \
+	TPS_FIELD_GET(TPS_REG_PD_STATUS_PORT_TYPE_MASK, x)
+
+#define TPS_PD_STATUS_PORT_TYPE_SINK_SOURCE	0
+#define TPS_PD_STATUS_PORT_TYPE_SINK		1
+#define TPS_PD_STATUS_PORT_TYPE_SOURCE		2
+#define TPS_PD_STATUS_PORT_TYPE_SOURCE_SINK	3
+
 #endif /* __TPS6598X_H__ */