usb: typec: tcpm: Support non-PD mode
Subsystems (3)
PATCH 1/1 usb: typec: tcpm: Support non-PD mode
tcpm.c could work well without PD capabilities. Do not block the probe if capabilities are not defined in fwnode and skip the PD power negotiation in the state machine.
--- drivers/usb/typec/tcpm/tcpm.c | 50 ++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 21 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +29 -21
@@ -3914,6 +3914,8 @@ 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->nr_src_pdo) +tcpm_set_state (port,SRC_READY ,0 ); port->upcoming_state =SRC_SEND_CAPABILITIES ;tcpm_ams_start (port,POWER_NEGOTIATION );break ;
@@ -4161,7 +4163,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->nr_snk_pdo) +tcpm_set_state (port,SNK_READY ,0 ); +else +tcpm_set_state (port,SNK_WAIT_CAPABILITIES ,0 );break ; }/*
@@ -5939,15 +5944,17 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,/* Get source pdos */ ret =fwnode_property_count_u32 (fwnode,"source-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ; 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 ; +if (port->nr_src_pdo) { + 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 ; + }if (port->port_type ==TYPEC_PORT_SRC )return 0 ;
@@ -5963,19 +5970,21 @@ static int tcpm_fw_get_caps(struct tcpm_port *port, sink:/* Get sink pdos */ ret =fwnode_property_count_u32 (fwnode,"sink-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ; 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 (port->nr_snk_pdo) { + 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 ; + } port->self_powered =fwnode_property_read_bool (fwnode,"self-powered" );
@@ -6283,9 +6292,8 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)int err;if (!dev || !tcpc || - !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || - !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus || - !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit) + !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || !tcpc->set_polarity || + !tcpc->set_vconn || !tcpc->set_vbus || !tcpc->set_roles)return ERR_PTR (-EINVAL ); port =devm_kzalloc (dev,sizeof (*port),GFP_KERNEL );
Signature · 1 line
2.32.0.402.g57bb445576-goog
Quoted 7 lines · Kyle Tso
tcpm.ccould work well without PD capabilities. Do not block the probe if capabilities are not defined in fwnode and skip the PD power negotiation in the state machine.Signed-off-by Kyle Tso <[email protected]>---
Hi, any comments about this patch?
thanks,
Kyle
Quoted 100 lines
drivers/usb/typec/tcpm/tcpm.c | 50 ++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 21 deletions(-)drivers/usb/typec/tcpm/tcpm.c +29
-22@@ -3914,6 +3914,8 @@ 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->nr_src_pdo) +tcpm_set_state (port,SRC_READY ,0 ); port->upcoming_state =SRC_SEND_CAPABILITIES ;tcpm_ams_start (port,POWER_NEGOTIATION );break ;@@ -4161,7 +4163,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->nr_snk_pdo) +tcpm_set_state (port,SNK_READY ,0 ); +else +tcpm_set_state (port,SNK_WAIT_CAPABILITIES ,0 );break ; }/* @@ -5939,15 +5944,17 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,/* Get source pdos */ ret =fwnode_property_count_u32 (fwnode,"source-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ; 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 ; +if (port->nr_src_pdo) { + 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 ; + }if (port->port_type ==TYPEC_PORT_SRC )return 0 ;@@ -5963,19 +5970,21 @@ static int tcpm_fw_get_caps(struct tcpm_port *port, sink:/* Get sink pdos */ ret =fwnode_property_count_u32 (fwnode,"sink-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ; 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 (port->nr_snk_pdo) { + 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 ; + } port->self_powered =fwnode_property_read_bool (fwnode,"self-powered" );@@ -6283,9 +6292,8 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)int err;if (!dev || !tcpc || - !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || - !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus || - !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit) + !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || !tcpc->set_polarity || + !tcpc->set_vconn || !tcpc->set_vbus || !tcpc->set_roles)return ERR_PTR (-EINVAL ); port =devm_kzalloc (dev,sizeof (*port),GFP_KERNEL ); --2.32.0.402.g57bb445576-goog
Quoted 3 lines · Kyle Tso
Quoted 2 lines · Kyle Tso
tcpm.ccould work well without PD capabilities. Do not block the probe
"could" is a bit vague. What is the use case ?
Quoted 8 lines
Quoted 5 lines
if capabilities are not defined in fwnode and skip the PD power negotiation in the state machine.
Signed-off-by Kyle Tso <[email protected]>---
Hi, any comments about this patch?
First question would be if this is documented/standardized. More comments below.
Quoted 33 lines
thanks,
KyleQuoted 30 lines
drivers/usb/typec/tcpm/tcpm.c | 50 ++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 21 deletions(-)drivers/usb/typec/tcpm/tcpm.c +6
-1@@ -3914,6 +3914,8 @@ 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->nr_src_pdo) +tcpm_set_state (port,SRC_READY ,0 ); port->upcoming_state =SRC_SEND_CAPABILITIES ;tcpm_ams_start (port,POWER_NEGOTIATION );break ;@@ -4161,7 +4163,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->nr_snk_pdo) +tcpm_set_state (port,SNK_READY ,0 ); +else +tcpm_set_state (port,SNK_WAIT_CAPABILITIES ,0 );break ; }/* @@ -5939,15 +5944,17 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
Quoted 7 lines
drivers/usb/typec/tcpm/tcpm.c static int tcpm_fw_get_caps(struct tcpm_port *port, · line 5949
Quoted 7 lines
drivers/usb/typec/tcpm/tcpm.c +2
-2/* Get source pdos */ ret =fwnode_property_count_u32 (fwnode,"source-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ;
This makes the capability properties optional. I think that would have to be documented.
Quoted 51 lines
Quoted 51 lines
drivers/usb/typec/tcpm/tcpm.c +19
-15port->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 ; +if (port->nr_src_pdo) { + 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 ; + }if (port->port_type ==TYPEC_PORT_SRC )return 0 ;@@ -5963,19 +5970,21 @@ static int tcpm_fw_get_caps(struct tcpm_port *port, sink:/* Get sink pdos */ ret =fwnode_property_count_u32 (fwnode,"sink-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ; 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 (port->nr_snk_pdo) { + 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 ; + } port->self_powered =fwnode_property_read_bool (fwnode,"self-powered" );@@ -6283,9 +6292,8 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)int err;
Quoted 6 lines
drivers/usb/typec/tcpm/tcpm.c struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) · line 6296
Quoted 6 lines
drivers/usb/typec/tcpm/tcpm.c +2
-3if (!dev || !tcpc || - !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || - !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus || - !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit) + !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || !tcpc->set_polarity || + !tcpc->set_vconn || !tcpc->set_vbus || !tcpc->set_roles)
With this change, if a driver does not define the necessary pd callbacks (set_pd_rx, pd_transmit), but its devicetree data does provide pdo properties, we'll get a nice crash.
On top of that, I am quite sure that the set_pd_rx() callback is still called from various places even if neither sink-pdos nor source-pdos properties are defined.
I think this really tries to handle two conditions: A low level driver that doesn't support PD, and a system where the low level driver does support PD but the system itself doesn't. And then there is the odd case where the system only supports either source or sink PD while claiming to support the other. Maybe it would make sense to separate both conditions, for example by introducing a new flag such as pd_supported to indicate that the system doesn't support the PD protocol.
Guenter
Quoted 6 lines
Quoted 6 lines
drivers/usb/typec/tcpm/tcpm.c +0
-1return ERR_PTR (-EINVAL ); port =devm_kzalloc (dev,sizeof (*port),GFP_KERNEL ); --2.32.0.402.g57bb445576-goog
Quoted 8 lines · Guenter Roeck
Quoted 3 lines · Kyle Tso
Quoted 2 lines · Kyle Tso
tcpm.ccould work well without PD capabilities. Do not block the probe"could" is a bit vague. What is the use case ?
Just to enable the "capability" of a type-c port without PD support. I will rephrase the message to be more specific.
Quoted 44 lines
Quoted 8 lines
Quoted 5 lines
if capabilities are not defined in fwnode and skip the PD power negotiation in the state machine.
Signed-off-by Kyle Tso <[email protected]>---
Hi, any comments about this patch?
First question would be if this is documented/standardized. More comments below.
Quoted 33 lines
thanks,
KyleQuoted 30 lines
drivers/usb/typec/tcpm/tcpm.c | 50 ++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 21 deletions(-)drivers/usb/typec/tcpm/tcpm.c +6
-1@@ -3914,6 +3914,8 @@ 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->nr_src_pdo) +tcpm_set_state (port,SRC_READY ,0 ); port->upcoming_state =SRC_SEND_CAPABILITIES ;tcpm_ams_start (port,POWER_NEGOTIATION );break ;@@ -4161,7 +4163,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->nr_snk_pdo) +tcpm_set_state (port,SNK_READY ,0 ); +else +tcpm_set_state (port,SNK_WAIT_CAPABILITIES ,0 );break ; }/* @@ -5939,15 +5944,17 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
Quoted 11 lines
Quoted 7 lines
drivers/usb/typec/tcpm/tcpm.c static int tcpm_fw_get_caps(struct tcpm_port *port, · line 5949
Quoted 7 lines
drivers/usb/typec/tcpm/tcpm.c +2
-2/* Get source pdos */ ret =fwnode_property_count_u32 (fwnode,"source-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ;This makes the capability properties optional. I think that would have to be documented.
Do you mean the documentation in dt-bindings? I think they have already been optional?
Quoted 74 lines
Quoted 51 lines
Quoted 51 lines
drivers/usb/typec/tcpm/tcpm.c +19
-15port->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 ; +if (port->nr_src_pdo) { + 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 ; + }if (port->port_type ==TYPEC_PORT_SRC )return 0 ;@@ -5963,19 +5970,21 @@ static int tcpm_fw_get_caps(struct tcpm_port *port, sink:/* Get sink pdos */ ret =fwnode_property_count_u32 (fwnode,"sink-pdos" ); -if (ret <=0 ) -return -EINVAL ; +if (ret <0 ) + ret =0 ; 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 (port->nr_snk_pdo) { + 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 ; + } port->self_powered =fwnode_property_read_bool (fwnode,"self-powered" );@@ -6283,9 +6292,8 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)int err;Quoted 6 lines
drivers/usb/typec/tcpm/tcpm.c struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) · line 6296
Quoted 6 lines
drivers/usb/typec/tcpm/tcpm.c +2
-3if (!dev || !tcpc || - !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || - !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus || - !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit) + !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || !tcpc->set_polarity || + !tcpc->set_vconn || !tcpc->set_vbus || !tcpc->set_roles)With this change, if a driver does not define the necessary pd callbacks (
set_pd_rx,pd_transmit), but its devicetree data does provide pdo properties, we'll get a nice crash.On top of that, I am quite sure that the
set_pd_rx()callback is still called from various places even if neither sink-pdos nor source-pdos properties are defined.I think this really tries to handle two conditions: A low level driver that doesn't support PD, and a system where the low level driver does support PD but the system itself doesn't. And then there is the odd case where the system only supports either source or sink PD while claiming to support the other. Maybe it would make sense to separate both conditions, for example by introducing a new flag such as
pd_supportedto indicate that the system doesn't support the PD protocol.
Quoted 2 lines
Guenter
Yes, your concern is correct and reasonable. I will send v2 to fix these.
thanks,
Kyle
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
drivers/usb/typec/tcpm/tcpm.c +0
-1return ERR_PTR (-EINVAL ); port =devm_kzalloc (dev,sizeof (*port),GFP_KERNEL ); --2.32.0.402.g57bb445576-goog