All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] TCPM non-pd mode
@ 2021-07-27 15:13 Kyle Tso
  2021-07-27 15:13 ` [PATCH v2 1/2] dt-bindings: connector: Add pd-supported property Kyle Tso
  2021-07-27 15:13 ` [PATCH v2 2/2] usb: typec: tcpm: Support non-PD mode Kyle Tso
  0 siblings, 2 replies; 6+ messages in thread
From: Kyle Tso @ 2021-07-27 15:13 UTC (permalink / raw)
  To: linux, heikki.krogerus, gregkh, robh+dt
  Cc: badhri, linux-usb, linux-kernel, devicetree, Kyle Tso

The reason for this patch is to let the device/system policy decide
whether PD is going to be supported using devicetree properties.

A new dt property "pd-supported" is introduced and TCPM uses this
property as a flag to decide whether PD is supported. If the flag is
false, the RX functionality of the low-level driver will not be enabled.
The power negotiation related states will be skipped as well. If the
flag is true, everything is as what it was before.

If "pd-supported" is not present, and the port is SRC or DRP, another
existing dt property "typec-power-opmode" needs to be specified to
indicate which Rp value should be used when the port is SRC.

changes since v1:
- revise the patch to use dt properties

Kyle Tso (2):
  dt-bindings: connector: Add pd-supported property
  usb: typec: tcpm: Support non-PD mode

 .../bindings/connector/usb-connector.yaml     |   5 +
 drivers/usb/typec/tcpm/tcpm.c                 | 108 +++++++++++++-----
 2 files changed, 84 insertions(+), 29 deletions(-)

-- 
2.32.0.432.gabb21c7263-goog


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/2] dt-bindings: connector: Add pd-supported property
  2021-07-27 15:13 [PATCH v2 0/2] TCPM non-pd mode Kyle Tso
@ 2021-07-27 15:13 ` Kyle Tso
  2021-07-27 16:29   ` Guenter Roeck
  2021-07-27 15:13 ` [PATCH v2 2/2] usb: typec: tcpm: Support non-PD mode Kyle Tso
  1 sibling, 1 reply; 6+ messages in thread
From: Kyle Tso @ 2021-07-27 15:13 UTC (permalink / raw)
  To: linux, heikki.krogerus, gregkh, robh+dt
  Cc: badhri, linux-usb, linux-kernel, devicetree, Kyle Tso

Set pd-supported property if the Type-C connector has power delivery
support.

Signed-off-by: Kyle Tso <kyletso@google.com>
---
 .../devicetree/bindings/connector/usb-connector.yaml         | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/Documentation/devicetree/bindings/connector/usb-connector.yaml b/Documentation/devicetree/bindings/connector/usb-connector.yaml
index 92b49bc37939..8ed271feea08 100644
--- a/Documentation/devicetree/bindings/connector/usb-connector.yaml
+++ b/Documentation/devicetree/bindings/connector/usb-connector.yaml
@@ -111,6 +111,10 @@ properties:
       - 1.5A
       - 3.0A
 
+  pd-supported:
+    description: Set this property if the Type-C connector has power delivery support.
+    type: boolean
+
   # The following are optional properties for "usb-c-connector" with power
   # delivery support.
   source-pdos:
@@ -312,6 +316,7 @@ examples:
         label = "USB-C";
         power-role = "dual";
         try-power-role = "sink";
+        pd-supported;
         source-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)>;
         sink-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)
                      PDO_VAR(5000, 12000, 2000)>;
-- 
2.32.0.432.gabb21c7263-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/2] usb: typec: tcpm: Support non-PD mode
  2021-07-27 15:13 [PATCH v2 0/2] TCPM non-pd mode Kyle Tso
  2021-07-27 15:13 ` [PATCH v2 1/2] dt-bindings: connector: Add pd-supported property Kyle Tso
@ 2021-07-27 15:13 ` Kyle Tso
  2021-07-27 22:46   ` Guenter Roeck
  1 sibling, 1 reply; 6+ messages in thread
From: Kyle Tso @ 2021-07-27 15:13 UTC (permalink / raw)
  To: linux, heikki.krogerus, gregkh, robh+dt
  Cc: badhri, linux-usb, linux-kernel, devicetree, Kyle Tso

Even if the Type-C controller supports PD, it is doable to disable PD
capabilities with the current state machine in TCPM. Without enabling RX
in low-level drivers and with skipping the power negotiation, the port
is eligible to be a non-PD Type-C port. Use new flags whose values are
populated from the device tree to decide the port PD capability. Adding
"pd-supported" property in device tree indicates that the port supports
PD. If "pd-supported" is not found, "typec-power-opmode" shall be added
to specify the advertised Rp value if the port supports SRC role.

Signed-off-by: Kyle Tso <kyletso@google.com>
---
changes since v1:
- Revised the patch to use dt properties
- Added back the checks of callbacks set_pd_rx and pd_transmit
- Added src_rp to indicate which Rp value should be used in SRC. This
  variable is derived from dt property "typec-power-opmode"

 drivers/usb/typec/tcpm/tcpm.c | 108 +++++++++++++++++++++++++---------
 1 file changed, 79 insertions(+), 29 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 5b22a1c931a9..4cf77c787f54 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -316,6 +316,7 @@ struct tcpm_port {
 	struct typec_partner *partner;
 
 	enum typec_cc_status cc_req;
+	enum typec_cc_status src_rp;	/* work only if pd_supported == false */
 
 	enum typec_cc_status cc1;
 	enum typec_cc_status cc2;
@@ -323,6 +324,7 @@ struct tcpm_port {
 
 	bool attached;
 	bool connected;
+	bool pd_supported;
 	enum typec_port_type port_type;
 
 	/*
@@ -804,6 +806,7 @@ static void tcpm_apply_rc(struct tcpm_port *port)
 	}
 }
 
+
 /*
  * Determine RP value to set based on maximum current supported
  * by a port if configured as source.
@@ -815,6 +818,9 @@ static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
 	int nr_pdo = port->nr_src_pdo;
 	int i;
 
+	if (!port->pd_supported)
+		return port->src_rp;
+
 	/*
 	 * Search for first entry with matching voltage.
 	 * It should report the maximum supported current.
@@ -3568,9 +3574,11 @@ static int tcpm_src_attach(struct tcpm_port *port)
 	if (ret < 0)
 		return ret;
 
-	ret = port->tcpc->set_pd_rx(port->tcpc, true);
-	if (ret < 0)
-		goto out_disable_mux;
+	if (port->pd_supported) {
+		ret = port->tcpc->set_pd_rx(port->tcpc, true);
+		if (ret < 0)
+			goto out_disable_mux;
+	}
 
 	/*
 	 * USB Type-C specification, version 1.2,
@@ -3600,7 +3608,8 @@ static int tcpm_src_attach(struct tcpm_port *port)
 out_disable_vconn:
 	tcpm_set_vconn(port, false);
 out_disable_pd:
-	port->tcpc->set_pd_rx(port->tcpc, false);
+	if (port->pd_supported)
+		port->tcpc->set_pd_rx(port->tcpc, false);
 out_disable_mux:
 	tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
 		     TYPEC_ORIENTATION_NONE);
@@ -3804,6 +3813,20 @@ static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
 	}
 }
 
+static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)
+{
+	switch (opmode) {
+	case TYPEC_PWR_MODE_USB:
+		return TYPEC_CC_RP_DEF;
+	case TYPEC_PWR_MODE_1_5A:
+		return TYPEC_CC_RP_1_5;
+	case TYPEC_PWR_MODE_3_0A:
+	case TYPEC_PWR_MODE_PD:
+	default:
+		return TYPEC_CC_RP_3_0;
+	}
+}
+
 static void run_state_machine(struct tcpm_port *port)
 {
 	int ret;
@@ -3914,6 +3937,10 @@ static void run_state_machine(struct tcpm_port *port)
 		if (port->ams == POWER_ROLE_SWAP ||
 		    port->ams == FAST_ROLE_SWAP)
 			tcpm_ams_finish(port);
+		if (!port->pd_supported) {
+			tcpm_set_state(port, SRC_READY, 0);
+			break;
+		}
 		port->upcoming_state = SRC_SEND_CAPABILITIES;
 		tcpm_ams_start(port, POWER_NEGOTIATION);
 		break;
@@ -4161,7 +4188,10 @@ static void run_state_machine(struct tcpm_port *port)
 				current_lim = PD_P_SNK_STDBY_MW / 5;
 			tcpm_set_current_limit(port, current_lim, 5000);
 			tcpm_set_charge(port, true);
-			tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
+			if (!port->pd_supported)
+				tcpm_set_state(port, SNK_READY, 0);
+			else
+				tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
 			break;
 		}
 		/*
@@ -4389,7 +4419,8 @@ static void run_state_machine(struct tcpm_port *port)
 		tcpm_set_vbus(port, true);
 		if (port->ams == HARD_RESET)
 			tcpm_ams_finish(port);
-		port->tcpc->set_pd_rx(port->tcpc, true);
+		if (port->pd_supported)
+			port->tcpc->set_pd_rx(port->tcpc, true);
 		tcpm_set_attached_state(port, true);
 		tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
 		break;
@@ -5898,6 +5929,7 @@ EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
 static int tcpm_fw_get_caps(struct tcpm_port *port,
 			    struct fwnode_handle *fwnode)
 {
+	const char *opmode_str;
 	const char *cap_str;
 	int ret;
 	u32 mw, frs_current;
@@ -5932,22 +5964,36 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
 		return ret;
 	port->typec_caps.type = ret;
 	port->port_type = port->typec_caps.type;
+	port->pd_supported = fwnode_property_read_bool(fwnode, "pd-supported");
 
 	port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop");
 	if (port->port_type == TYPEC_PORT_SNK)
 		goto sink;
 
 	/* Get source pdos */
-	ret = fwnode_property_count_u32(fwnode, "source-pdos");
-	if (ret <= 0)
-		return -EINVAL;
+	if (port->pd_supported) {
+		ret = fwnode_property_count_u32(fwnode, "source-pdos");
+		if (ret <= 0)
+			return -EINVAL;
 
-	port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
-	ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
-					     port->src_pdo, port->nr_src_pdo);
-	if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
-					    port->nr_src_pdo))
-		return -EINVAL;
+		port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
+		ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
+						     port->src_pdo, port->nr_src_pdo);
+		if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
+						    port->nr_src_pdo))
+			return -EINVAL;
+	} else {
+		port->nr_src_pdo = 0;
+		ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str);
+		if (ret == 0) {
+			ret = typec_find_pwr_opmode(opmode_str);
+			if (ret < 0)
+				return ret;
+			port->src_rp = tcpm_pwr_opmode_to_rp(ret);
+		} else {
+			return ret;
+		}
+	}
 
 	if (port->port_type == TYPEC_PORT_SRC)
 		return 0;
@@ -5962,25 +6008,29 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
 		return -EINVAL;
 sink:
 	/* Get sink pdos */
-	ret = fwnode_property_count_u32(fwnode, "sink-pdos");
-	if (ret <= 0)
-		return -EINVAL;
+	if (port->pd_supported) {
+		ret = fwnode_property_count_u32(fwnode, "sink-pdos");
+		if (ret <= 0)
+			return -EINVAL;
 
-	port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
-	ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
-					     port->snk_pdo, port->nr_snk_pdo);
-	if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
-					    port->nr_snk_pdo))
-		return -EINVAL;
+		port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
+		ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
+						     port->snk_pdo, port->nr_snk_pdo);
+		if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
+						    port->nr_snk_pdo))
+			return -EINVAL;
 
-	if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
-		return -EINVAL;
-	port->operating_snk_mw = mw / 1000;
+		if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
+			return -EINVAL;
+		port->operating_snk_mw = mw / 1000;
+	} else {
+		port->nr_snk_pdo = 0;
+	}
 
 	port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
 
-	/* FRS can only be supported byb DRP ports */
-	if (port->port_type == TYPEC_PORT_DRP) {
+	/* FRS can only be supported by DRP ports */
+	if (port->port_type == TYPEC_PORT_DRP && port->pd_supported) {
 		ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current",
 					       &frs_current);
 		if (ret >= 0 && frs_current <= FRS_5V_3A)
-- 
2.32.0.432.gabb21c7263-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] dt-bindings: connector: Add pd-supported property
  2021-07-27 15:13 ` [PATCH v2 1/2] dt-bindings: connector: Add pd-supported property Kyle Tso
@ 2021-07-27 16:29   ` Guenter Roeck
  0 siblings, 0 replies; 6+ messages in thread
From: Guenter Roeck @ 2021-07-27 16:29 UTC (permalink / raw)
  To: Kyle Tso, heikki.krogerus, gregkh, robh+dt
  Cc: badhri, linux-usb, linux-kernel, devicetree

On 7/27/21 8:13 AM, Kyle Tso wrote:
> Set pd-supported property if the Type-C connector has power delivery
> support.
> 
> Signed-off-by: Kyle Tso <kyletso@google.com>
> ---
>   .../devicetree/bindings/connector/usb-connector.yaml         | 5 +++++
>   1 file changed, 5 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/connector/usb-connector.yaml b/Documentation/devicetree/bindings/connector/usb-connector.yaml
> index 92b49bc37939..8ed271feea08 100644
> --- a/Documentation/devicetree/bindings/connector/usb-connector.yaml
> +++ b/Documentation/devicetree/bindings/connector/usb-connector.yaml
> @@ -111,6 +111,10 @@ properties:
>         - 1.5A
>         - 3.0A
>   
> +  pd-supported:

I think that would have to be a property indicating that pd is _not_
supported, for compatibility reasons. Otherwise all existing bindings
would indicate that pd is not supported.

> +    description: Set this property if the Type-C connector has power delivery support.
> +    type: boolean
> +
>     # The following are optional properties for "usb-c-connector" with power
>     # delivery support.
>     source-pdos:
> @@ -312,6 +316,7 @@ examples:
>           label = "USB-C";
>           power-role = "dual";
>           try-power-role = "sink";
> +        pd-supported;
>           source-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)>;
>           sink-pdos = <PDO_FIXED(5000, 2000, PDO_FIXED_USB_COMM)
>                        PDO_VAR(5000, 12000, 2000)>;
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 2/2] usb: typec: tcpm: Support non-PD mode
  2021-07-27 15:13 ` [PATCH v2 2/2] usb: typec: tcpm: Support non-PD mode Kyle Tso
@ 2021-07-27 22:46   ` Guenter Roeck
  2021-07-28  2:11     ` Kyle Tso
  0 siblings, 1 reply; 6+ messages in thread
From: Guenter Roeck @ 2021-07-27 22:46 UTC (permalink / raw)
  To: Kyle Tso, heikki.krogerus, gregkh, robh+dt
  Cc: badhri, linux-usb, linux-kernel, devicetree

On 7/27/21 8:13 AM, Kyle Tso wrote:
> Even if the Type-C controller supports PD, it is doable to disable PD
> capabilities with the current state machine in TCPM. Without enabling RX
> in low-level drivers and with skipping the power negotiation, the port
> is eligible to be a non-PD Type-C port. Use new flags whose values are
> populated from the device tree to decide the port PD capability. Adding
> "pd-supported" property in device tree indicates that the port supports
> PD. If "pd-supported" is not found, "typec-power-opmode" shall be added
> to specify the advertised Rp value if the port supports SRC role.
> 
> Signed-off-by: Kyle Tso <kyletso@google.com>
> ---
> changes since v1:
> - Revised the patch to use dt properties
> - Added back the checks of callbacks set_pd_rx and pd_transmit
> - Added src_rp to indicate which Rp value should be used in SRC. This
>    variable is derived from dt property "typec-power-opmode"
> 
>   drivers/usb/typec/tcpm/tcpm.c | 108 +++++++++++++++++++++++++---------
>   1 file changed, 79 insertions(+), 29 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 5b22a1c931a9..4cf77c787f54 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -316,6 +316,7 @@ struct tcpm_port {
>   	struct typec_partner *partner;
>   
>   	enum typec_cc_status cc_req;
> +	enum typec_cc_status src_rp;	/* work only if pd_supported == false */
>   
>   	enum typec_cc_status cc1;
>   	enum typec_cc_status cc2;
> @@ -323,6 +324,7 @@ struct tcpm_port {
>   
>   	bool attached;
>   	bool connected;
> +	bool pd_supported;
>   	enum typec_port_type port_type;
>   
>   	/*
> @@ -804,6 +806,7 @@ static void tcpm_apply_rc(struct tcpm_port *port)
>   	}
>   }
>   
> +
>   /*
>    * Determine RP value to set based on maximum current supported
>    * by a port if configured as source.
> @@ -815,6 +818,9 @@ static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
>   	int nr_pdo = port->nr_src_pdo;
>   	int i;
>   
> +	if (!port->pd_supported)
> +		return port->src_rp;
> +
>   	/*
>   	 * Search for first entry with matching voltage.
>   	 * It should report the maximum supported current.
> @@ -3568,9 +3574,11 @@ static int tcpm_src_attach(struct tcpm_port *port)
>   	if (ret < 0)
>   		return ret;
>   
> -	ret = port->tcpc->set_pd_rx(port->tcpc, true);
> -	if (ret < 0)
> -		goto out_disable_mux;
> +	if (port->pd_supported) {
> +		ret = port->tcpc->set_pd_rx(port->tcpc, true);
> +		if (ret < 0)
> +			goto out_disable_mux;
> +	}
>   
>   	/*
>   	 * USB Type-C specification, version 1.2,
> @@ -3600,7 +3608,8 @@ static int tcpm_src_attach(struct tcpm_port *port)
>   out_disable_vconn:
>   	tcpm_set_vconn(port, false);
>   out_disable_pd:
> -	port->tcpc->set_pd_rx(port->tcpc, false);
> +	if (port->pd_supported)
> +		port->tcpc->set_pd_rx(port->tcpc, false);
>   out_disable_mux:
>   	tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
>   		     TYPEC_ORIENTATION_NONE);
> @@ -3804,6 +3813,20 @@ static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
>   	}
>   }
>   
> +static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)
> +{
> +	switch (opmode) {
> +	case TYPEC_PWR_MODE_USB:
> +		return TYPEC_CC_RP_DEF;
> +	case TYPEC_PWR_MODE_1_5A:
> +		return TYPEC_CC_RP_1_5;
> +	case TYPEC_PWR_MODE_3_0A:
> +	case TYPEC_PWR_MODE_PD:
> +	default:
> +		return TYPEC_CC_RP_3_0;
> +	}
> +}
> +
>   static void run_state_machine(struct tcpm_port *port)
>   {
>   	int ret;
> @@ -3914,6 +3937,10 @@ static void run_state_machine(struct tcpm_port *port)
>   		if (port->ams == POWER_ROLE_SWAP ||
>   		    port->ams == FAST_ROLE_SWAP)
>   			tcpm_ams_finish(port);
> +		if (!port->pd_supported) {
> +			tcpm_set_state(port, SRC_READY, 0);
> +			break;
> +		}
>   		port->upcoming_state = SRC_SEND_CAPABILITIES;
>   		tcpm_ams_start(port, POWER_NEGOTIATION);
>   		break;
> @@ -4161,7 +4188,10 @@ static void run_state_machine(struct tcpm_port *port)
>   				current_lim = PD_P_SNK_STDBY_MW / 5;
>   			tcpm_set_current_limit(port, current_lim, 5000);
>   			tcpm_set_charge(port, true);
> -			tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
> +			if (!port->pd_supported)
> +				tcpm_set_state(port, SNK_READY, 0);
> +			else
> +				tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
>   			break;
>   		}
>   		/*
> @@ -4389,7 +4419,8 @@ static void run_state_machine(struct tcpm_port *port)
>   		tcpm_set_vbus(port, true);
>   		if (port->ams == HARD_RESET)
>   			tcpm_ams_finish(port);
> -		port->tcpc->set_pd_rx(port->tcpc, true);
> +		if (port->pd_supported)
> +			port->tcpc->set_pd_rx(port->tcpc, true);
>   		tcpm_set_attached_state(port, true);
>   		tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
>   		break;
> @@ -5898,6 +5929,7 @@ EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
>   static int tcpm_fw_get_caps(struct tcpm_port *port,
>   			    struct fwnode_handle *fwnode)
>   {
> +	const char *opmode_str;
>   	const char *cap_str;
>   	int ret;
>   	u32 mw, frs_current;
> @@ -5932,22 +5964,36 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
>   		return ret;
>   	port->typec_caps.type = ret;
>   	port->port_type = port->typec_caps.type;
> +	port->pd_supported = fwnode_property_read_bool(fwnode, "pd-supported");
>   

As mentioned in the other patch, I think this needs to be negated.
Not the variable itself, but the meaning of the property.

>   	port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop");

It might make sense to move the properties needed with !pd_supported to here
and return immediately after they are read. This would reduce the number of
required changes and avoid trying to read irrelevant properties.
Something like

	if (port->port_type != TYPEC_PORT_SRC)
		port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");

	if (!port->pd_supported)
		return 0;

>   	if (port->port_type == TYPEC_PORT_SNK)
>   		goto sink;
>   
>   	/* Get source pdos */
> -	ret = fwnode_property_count_u32(fwnode, "source-pdos");
> -	if (ret <= 0)
> -		return -EINVAL;
> +	if (port->pd_supported) {
> +		ret = fwnode_property_count_u32(fwnode, "source-pdos");
> +		if (ret <= 0)
> +			return -EINVAL;
>   
> -	port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
> -	ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
> -					     port->src_pdo, port->nr_src_pdo);
> -	if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
> -					    port->nr_src_pdo))
> -		return -EINVAL;
> +		port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
> +		ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
> +						     port->src_pdo, port->nr_src_pdo);
> +		if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
> +						    port->nr_src_pdo))
> +			return -EINVAL;
> +	} else {
> +		port->nr_src_pdo = 0;
> +		ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str);
> +		if (ret == 0) {
> +			ret = typec_find_pwr_opmode(opmode_str);
> +			if (ret < 0)
> +				return ret;
> +			port->src_rp = tcpm_pwr_opmode_to_rp(ret);
> +		} else {
> +			return ret;
> +		}
> +	}
>   
>   	if (port->port_type == TYPEC_PORT_SRC)
>   		return 0;
> @@ -5962,25 +6008,29 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
>   		return -EINVAL;
>   sink:
>   	/* Get sink pdos */
> -	ret = fwnode_property_count_u32(fwnode, "sink-pdos");
> -	if (ret <= 0)
> -		return -EINVAL;
> +	if (port->pd_supported) {
> +		ret = fwnode_property_count_u32(fwnode, "sink-pdos");
> +		if (ret <= 0)
> +			return -EINVAL;
>   
> -	port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
> -	ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
> -					     port->snk_pdo, port->nr_snk_pdo);
> -	if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
> -					    port->nr_snk_pdo))
> -		return -EINVAL;
> +		port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
> +		ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
> +						     port->snk_pdo, port->nr_snk_pdo);
> +		if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
> +						    port->nr_snk_pdo))
> +			return -EINVAL;
>   
> -	if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
> -		return -EINVAL;
> -	port->operating_snk_mw = mw / 1000;
> +		if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
> +			return -EINVAL;
> +		port->operating_snk_mw = mw / 1000;
> +	} else {
> +		port->nr_snk_pdo = 0;
> +	}
>   
>   	port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
>   
> -	/* FRS can only be supported byb DRP ports */
> -	if (port->port_type == TYPEC_PORT_DRP) {
> +	/* FRS can only be supported by DRP ports */
> +	if (port->port_type == TYPEC_PORT_DRP && port->pd_supported) {
>   		ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current",
>   					       &frs_current);
>   		if (ret >= 0 && frs_current <= FRS_5V_3A)
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 2/2] usb: typec: tcpm: Support non-PD mode
  2021-07-27 22:46   ` Guenter Roeck
@ 2021-07-28  2:11     ` Kyle Tso
  0 siblings, 0 replies; 6+ messages in thread
From: Kyle Tso @ 2021-07-28  2:11 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: heikki.krogerus, gregkh, robh+dt, badhri, linux-usb,
	linux-kernel, devicetree

On Wed, Jul 28, 2021 at 6:46 AM Guenter Roeck <linux@roeck-us.net> wrote:
>
> On 7/27/21 8:13 AM, Kyle Tso wrote:
> > Even if the Type-C controller supports PD, it is doable to disable PD
> > capabilities with the current state machine in TCPM. Without enabling RX
> > in low-level drivers and with skipping the power negotiation, the port
> > is eligible to be a non-PD Type-C port. Use new flags whose values are
> > populated from the device tree to decide the port PD capability. Adding
> > "pd-supported" property in device tree indicates that the port supports
> > PD. If "pd-supported" is not found, "typec-power-opmode" shall be added
> > to specify the advertised Rp value if the port supports SRC role.
> >
> > Signed-off-by: Kyle Tso <kyletso@google.com>
> > ---
> > changes since v1:
> > - Revised the patch to use dt properties
> > - Added back the checks of callbacks set_pd_rx and pd_transmit
> > - Added src_rp to indicate which Rp value should be used in SRC. This
> >    variable is derived from dt property "typec-power-opmode"
> >
> >   drivers/usb/typec/tcpm/tcpm.c | 108 +++++++++++++++++++++++++---------
> >   1 file changed, 79 insertions(+), 29 deletions(-)
> >
> > diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> > index 5b22a1c931a9..4cf77c787f54 100644
> > --- a/drivers/usb/typec/tcpm/tcpm.c
> > +++ b/drivers/usb/typec/tcpm/tcpm.c
> > @@ -316,6 +316,7 @@ struct tcpm_port {
> >       struct typec_partner *partner;
> >
> >       enum typec_cc_status cc_req;
> > +     enum typec_cc_status src_rp;    /* work only if pd_supported == false */
> >
> >       enum typec_cc_status cc1;
> >       enum typec_cc_status cc2;
> > @@ -323,6 +324,7 @@ struct tcpm_port {
> >
> >       bool attached;
> >       bool connected;
> > +     bool pd_supported;
> >       enum typec_port_type port_type;
> >
> >       /*
> > @@ -804,6 +806,7 @@ static void tcpm_apply_rc(struct tcpm_port *port)
> >       }
> >   }
> >
> > +
> >   /*
> >    * Determine RP value to set based on maximum current supported
> >    * by a port if configured as source.
> > @@ -815,6 +818,9 @@ static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
> >       int nr_pdo = port->nr_src_pdo;
> >       int i;
> >
> > +     if (!port->pd_supported)
> > +             return port->src_rp;
> > +
> >       /*
> >        * Search for first entry with matching voltage.
> >        * It should report the maximum supported current.
> > @@ -3568,9 +3574,11 @@ static int tcpm_src_attach(struct tcpm_port *port)
> >       if (ret < 0)
> >               return ret;
> >
> > -     ret = port->tcpc->set_pd_rx(port->tcpc, true);
> > -     if (ret < 0)
> > -             goto out_disable_mux;
> > +     if (port->pd_supported) {
> > +             ret = port->tcpc->set_pd_rx(port->tcpc, true);
> > +             if (ret < 0)
> > +                     goto out_disable_mux;
> > +     }
> >
> >       /*
> >        * USB Type-C specification, version 1.2,
> > @@ -3600,7 +3608,8 @@ static int tcpm_src_attach(struct tcpm_port *port)
> >   out_disable_vconn:
> >       tcpm_set_vconn(port, false);
> >   out_disable_pd:
> > -     port->tcpc->set_pd_rx(port->tcpc, false);
> > +     if (port->pd_supported)
> > +             port->tcpc->set_pd_rx(port->tcpc, false);
> >   out_disable_mux:
> >       tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
> >                    TYPEC_ORIENTATION_NONE);
> > @@ -3804,6 +3813,20 @@ static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
> >       }
> >   }
> >
> > +static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)
> > +{
> > +     switch (opmode) {
> > +     case TYPEC_PWR_MODE_USB:
> > +             return TYPEC_CC_RP_DEF;
> > +     case TYPEC_PWR_MODE_1_5A:
> > +             return TYPEC_CC_RP_1_5;
> > +     case TYPEC_PWR_MODE_3_0A:
> > +     case TYPEC_PWR_MODE_PD:
> > +     default:
> > +             return TYPEC_CC_RP_3_0;
> > +     }
> > +}
> > +
> >   static void run_state_machine(struct tcpm_port *port)
> >   {
> >       int ret;
> > @@ -3914,6 +3937,10 @@ static void run_state_machine(struct tcpm_port *port)
> >               if (port->ams == POWER_ROLE_SWAP ||
> >                   port->ams == FAST_ROLE_SWAP)
> >                       tcpm_ams_finish(port);
> > +             if (!port->pd_supported) {
> > +                     tcpm_set_state(port, SRC_READY, 0);
> > +                     break;
> > +             }
> >               port->upcoming_state = SRC_SEND_CAPABILITIES;
> >               tcpm_ams_start(port, POWER_NEGOTIATION);
> >               break;
> > @@ -4161,7 +4188,10 @@ static void run_state_machine(struct tcpm_port *port)
> >                               current_lim = PD_P_SNK_STDBY_MW / 5;
> >                       tcpm_set_current_limit(port, current_lim, 5000);
> >                       tcpm_set_charge(port, true);
> > -                     tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
> > +                     if (!port->pd_supported)
> > +                             tcpm_set_state(port, SNK_READY, 0);
> > +                     else
> > +                             tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
> >                       break;
> >               }
> >               /*
> > @@ -4389,7 +4419,8 @@ static void run_state_machine(struct tcpm_port *port)
> >               tcpm_set_vbus(port, true);
> >               if (port->ams == HARD_RESET)
> >                       tcpm_ams_finish(port);
> > -             port->tcpc->set_pd_rx(port->tcpc, true);
> > +             if (port->pd_supported)
> > +                     port->tcpc->set_pd_rx(port->tcpc, true);
> >               tcpm_set_attached_state(port, true);
> >               tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
> >               break;
> > @@ -5898,6 +5929,7 @@ EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
> >   static int tcpm_fw_get_caps(struct tcpm_port *port,
> >                           struct fwnode_handle *fwnode)
> >   {
> > +     const char *opmode_str;
> >       const char *cap_str;
> >       int ret;
> >       u32 mw, frs_current;
> > @@ -5932,22 +5964,36 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
> >               return ret;
> >       port->typec_caps.type = ret;
> >       port->port_type = port->typec_caps.type;
> > +     port->pd_supported = fwnode_property_read_bool(fwnode, "pd-supported");
> >
>
> As mentioned in the other patch, I think this needs to be negated.
> Not the variable itself, but the meaning of the property.
>

Makes sense. I will send another patch.

> >       port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop");
>
> It might make sense to move the properties needed with !pd_supported to here
> and return immediately after they are read. This would reduce the number of
> required changes and avoid trying to read irrelevant properties.
> Something like
>
>         if (port->port_type != TYPEC_PORT_SRC)
>                 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
>
>         if (!port->pd_supported)
>                 return 0;
>

I will try to modify the flow control as well.

thanks,
Kyle

> >       if (port->port_type == TYPEC_PORT_SNK)
> >               goto sink;
> >
> >       /* Get source pdos */
> > -     ret = fwnode_property_count_u32(fwnode, "source-pdos");
> > -     if (ret <= 0)
> > -             return -EINVAL;
> > +     if (port->pd_supported) {
> > +             ret = fwnode_property_count_u32(fwnode, "source-pdos");
> > +             if (ret <= 0)
> > +                     return -EINVAL;
> >
> > -     port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
> > -     ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
> > -                                          port->src_pdo, port->nr_src_pdo);
> > -     if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
> > -                                         port->nr_src_pdo))
> > -             return -EINVAL;
> > +             port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
> > +             ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
> > +                                                  port->src_pdo, port->nr_src_pdo);
> > +             if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
> > +                                                 port->nr_src_pdo))
> > +                     return -EINVAL;
> > +     } else {
> > +             port->nr_src_pdo = 0;
> > +             ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str);
> > +             if (ret == 0) {
> > +                     ret = typec_find_pwr_opmode(opmode_str);
> > +                     if (ret < 0)
> > +                             return ret;
> > +                     port->src_rp = tcpm_pwr_opmode_to_rp(ret);
> > +             } else {
> > +                     return ret;
> > +             }
> > +     }
> >
> >       if (port->port_type == TYPEC_PORT_SRC)
> >               return 0;
> > @@ -5962,25 +6008,29 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
> >               return -EINVAL;
> >   sink:
> >       /* Get sink pdos */
> > -     ret = fwnode_property_count_u32(fwnode, "sink-pdos");
> > -     if (ret <= 0)
> > -             return -EINVAL;
> > +     if (port->pd_supported) {
> > +             ret = fwnode_property_count_u32(fwnode, "sink-pdos");
> > +             if (ret <= 0)
> > +                     return -EINVAL;
> >
> > -     port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
> > -     ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
> > -                                          port->snk_pdo, port->nr_snk_pdo);
> > -     if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
> > -                                         port->nr_snk_pdo))
> > -             return -EINVAL;
> > +             port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
> > +             ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
> > +                                                  port->snk_pdo, port->nr_snk_pdo);
> > +             if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
> > +                                                 port->nr_snk_pdo))
> > +                     return -EINVAL;
> >
> > -     if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
> > -             return -EINVAL;
> > -     port->operating_snk_mw = mw / 1000;
> > +             if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
> > +                     return -EINVAL;
> > +             port->operating_snk_mw = mw / 1000;
> > +     } else {
> > +             port->nr_snk_pdo = 0;
> > +     }
> >
> >       port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
> >
> > -     /* FRS can only be supported byb DRP ports */
> > -     if (port->port_type == TYPEC_PORT_DRP) {
> > +     /* FRS can only be supported by DRP ports */
> > +     if (port->port_type == TYPEC_PORT_DRP && port->pd_supported) {
> >               ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current",
> >                                              &frs_current);
> >               if (ret >= 0 && frs_current <= FRS_5V_3A)
> >
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-07-28  2:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-27 15:13 [PATCH v2 0/2] TCPM non-pd mode Kyle Tso
2021-07-27 15:13 ` [PATCH v2 1/2] dt-bindings: connector: Add pd-supported property Kyle Tso
2021-07-27 16:29   ` Guenter Roeck
2021-07-27 15:13 ` [PATCH v2 2/2] usb: typec: tcpm: Support non-PD mode Kyle Tso
2021-07-27 22:46   ` Guenter Roeck
2021-07-28  2:11     ` Kyle Tso

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.