2023-06-16 08:04:52

by Frank Wang

[permalink] [raw]
Subject: [v4,1/2] usb: typec: tcpm: fix cc role at port reset

In the current implementation, the tcpm set CC1/CC2 role to open when
it do port reset would cause the VBUS removed by the Type-C partner.

This sets CC1/CC2 according to the default state of port to fix it.

Signed-off-by: Frank Wang <[email protected]>
---
Changelog:
(no changes since v3)

v2:
- Make some tweaking based on the default state of port, commented by Guenter Roeck.

v1:
- https://patchwork.kernel.org/project/linux-usb/patch/[email protected]/

drivers/usb/typec/tcpm/tcpm.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 3c6b0c8e2d3ae..9f6aaa3e70ca8 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -4885,7 +4885,8 @@ static void run_state_machine(struct tcpm_port *port)
break;
case PORT_RESET:
tcpm_reset_port(port);
- tcpm_set_cc(port, TYPEC_CC_OPEN);
+ tcpm_set_cc(port, tcpm_default_state(port) == SNK_UNATTACHED ?
+ TYPEC_CC_RD : tcpm_rp_cc(port));
tcpm_set_state(port, PORT_RESET_WAIT_OFF,
PD_T_ERROR_RECOVERY);
break;
--
2.17.1



2023-06-16 08:31:50

by Frank Wang

[permalink] [raw]
Subject: [v4,2/2] usb: typec: tcpm: add get max power support

Traverse fixed pdos to calculate the maximum power that the charger
can provide, and it can be get by POWER_SUPPLY_PROP_INPUT_POWER_LIMIT
property.

Signed-off-by: Frank Wang <[email protected]>
---
Changelog:
v4:
- No change

v3:
- Use Microwatts instead of Milliwatts to follow the ABI, commented by Sebastian Reichel.

v2:
- No change

v1:
- https://patchwork.kernel.org/project/linux-usb/patch/[email protected]/

drivers/usb/typec/tcpm/tcpm.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 9f6aaa3e70ca8..829d75ebab422 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -6340,6 +6340,27 @@ static int tcpm_psy_get_current_now(struct tcpm_port *port,
return 0;
}

+static int tcpm_psy_get_input_power_limit(struct tcpm_port *port,
+ union power_supply_propval *val)
+{
+ unsigned int src_mv, src_ma, max_src_uw = 0;
+ unsigned int i, tmp;
+
+ for (i = 0; i < port->nr_source_caps; i++) {
+ u32 pdo = port->source_caps[i];
+
+ if (pdo_type(pdo) == PDO_TYPE_FIXED) {
+ src_mv = pdo_fixed_voltage(pdo);
+ src_ma = pdo_max_current(pdo);
+ tmp = src_mv * src_ma;
+ max_src_uw = tmp > max_src_uw ? tmp : max_src_uw;
+ }
+ }
+
+ val->intval = max_src_uw;
+ return 0;
+}
+
static int tcpm_psy_get_prop(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
@@ -6369,6 +6390,9 @@ static int tcpm_psy_get_prop(struct power_supply *psy,
case POWER_SUPPLY_PROP_CURRENT_NOW:
ret = tcpm_psy_get_current_now(port, val);
break;
+ case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
+ tcpm_psy_get_input_power_limit(port, val);
+ break;
default:
ret = -EINVAL;
break;
--
2.17.1


2023-06-16 08:51:16

by Guenter Roeck

[permalink] [raw]
Subject: Re: [v4,2/2] usb: typec: tcpm: add get max power support

On 6/16/23 00:52, Frank Wang wrote:
> Traverse fixed pdos to calculate the maximum power that the charger
> can provide, and it can be get by POWER_SUPPLY_PROP_INPUT_POWER_LIMIT
> property.
>
> Signed-off-by: Frank Wang <[email protected]>

Reviewed-by: Guenter Roeck <[email protected]>

> ---
> Changelog:
> v4:
> - No change
>
> v3:
> - Use Microwatts instead of Milliwatts to follow the ABI, commented by Sebastian Reichel.
>
> v2:
> - No change
>
> v1:
> - https://patchwork.kernel.org/project/linux-usb/patch/[email protected]/
>
> drivers/usb/typec/tcpm/tcpm.c | 24 ++++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 9f6aaa3e70ca8..829d75ebab422 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -6340,6 +6340,27 @@ static int tcpm_psy_get_current_now(struct tcpm_port *port,
> return 0;
> }
>
> +static int tcpm_psy_get_input_power_limit(struct tcpm_port *port,
> + union power_supply_propval *val)
> +{
> + unsigned int src_mv, src_ma, max_src_uw = 0;
> + unsigned int i, tmp;
> +
> + for (i = 0; i < port->nr_source_caps; i++) {
> + u32 pdo = port->source_caps[i];
> +
> + if (pdo_type(pdo) == PDO_TYPE_FIXED) {
> + src_mv = pdo_fixed_voltage(pdo);
> + src_ma = pdo_max_current(pdo);
> + tmp = src_mv * src_ma;
> + max_src_uw = tmp > max_src_uw ? tmp : max_src_uw;
> + }
> + }
> +
> + val->intval = max_src_uw;
> + return 0;
> +}
> +
> static int tcpm_psy_get_prop(struct power_supply *psy,
> enum power_supply_property psp,
> union power_supply_propval *val)
> @@ -6369,6 +6390,9 @@ static int tcpm_psy_get_prop(struct power_supply *psy,
> case POWER_SUPPLY_PROP_CURRENT_NOW:
> ret = tcpm_psy_get_current_now(port, val);
> break;
> + case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
> + tcpm_psy_get_input_power_limit(port, val);
> + break;
> default:
> ret = -EINVAL;
> break;


2023-06-16 08:53:55

by Guenter Roeck

[permalink] [raw]
Subject: Re: [v4,1/2] usb: typec: tcpm: fix cc role at port reset

On 6/16/23 00:52, Frank Wang wrote:
> In the current implementation, the tcpm set CC1/CC2 role to open when
> it do port reset would cause the VBUS removed by the Type-C partner.
>
> This sets CC1/CC2 according to the default state of port to fix it.
>
> Signed-off-by: Frank Wang <[email protected]>

Reviewed-by: Guenter Roeck <[email protected]>

> ---
> Changelog:
> (no changes since v3)
>
> v2:
> - Make some tweaking based on the default state of port, commented by Guenter Roeck.
>
Specifically: Do not set the state to TYPEC_CC_RD unconditionally
but make it dependent on the port's default state.

Guenter

> v1:
> - https://patchwork.kernel.org/project/linux-usb/patch/[email protected]/
>
> drivers/usb/typec/tcpm/tcpm.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 3c6b0c8e2d3ae..9f6aaa3e70ca8 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -4885,7 +4885,8 @@ static void run_state_machine(struct tcpm_port *port)
> break;
> case PORT_RESET:
> tcpm_reset_port(port);
> - tcpm_set_cc(port, TYPEC_CC_OPEN);
> + tcpm_set_cc(port, tcpm_default_state(port) == SNK_UNATTACHED ?
> + TYPEC_CC_RD : tcpm_rp_cc(port));
> tcpm_set_state(port, PORT_RESET_WAIT_OFF,
> PD_T_ERROR_RECOVERY);
> break;


2023-06-26 08:48:42

by Heikki Krogerus

[permalink] [raw]
Subject: Re: [v4,2/2] usb: typec: tcpm: add get max power support

On Fri, Jun 16, 2023 at 03:52:41PM +0800, Frank Wang wrote:
> Traverse fixed pdos to calculate the maximum power that the charger
> can provide, and it can be get by POWER_SUPPLY_PROP_INPUT_POWER_LIMIT
> property.
>
> Signed-off-by: Frank Wang <[email protected]>

Acked-by: Heikki Krogerus <[email protected]>

> ---
> Changelog:
> v4:
> - No change
>
> v3:
> - Use Microwatts instead of Milliwatts to follow the ABI, commented by Sebastian Reichel.
>
> v2:
> - No change
>
> v1:
> - https://patchwork.kernel.org/project/linux-usb/patch/[email protected]/
>
> drivers/usb/typec/tcpm/tcpm.c | 24 ++++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 9f6aaa3e70ca8..829d75ebab422 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -6340,6 +6340,27 @@ static int tcpm_psy_get_current_now(struct tcpm_port *port,
> return 0;
> }
>
> +static int tcpm_psy_get_input_power_limit(struct tcpm_port *port,
> + union power_supply_propval *val)
> +{
> + unsigned int src_mv, src_ma, max_src_uw = 0;
> + unsigned int i, tmp;
> +
> + for (i = 0; i < port->nr_source_caps; i++) {
> + u32 pdo = port->source_caps[i];
> +
> + if (pdo_type(pdo) == PDO_TYPE_FIXED) {
> + src_mv = pdo_fixed_voltage(pdo);
> + src_ma = pdo_max_current(pdo);
> + tmp = src_mv * src_ma;
> + max_src_uw = tmp > max_src_uw ? tmp : max_src_uw;
> + }
> + }
> +
> + val->intval = max_src_uw;
> + return 0;
> +}
> +
> static int tcpm_psy_get_prop(struct power_supply *psy,
> enum power_supply_property psp,
> union power_supply_propval *val)
> @@ -6369,6 +6390,9 @@ static int tcpm_psy_get_prop(struct power_supply *psy,
> case POWER_SUPPLY_PROP_CURRENT_NOW:
> ret = tcpm_psy_get_current_now(port, val);
> break;
> + case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
> + tcpm_psy_get_input_power_limit(port, val);
> + break;
> default:
> ret = -EINVAL;
> break;
> --
> 2.17.1

--
heikki

2023-06-26 08:50:33

by Heikki Krogerus

[permalink] [raw]
Subject: Re: [v4,1/2] usb: typec: tcpm: fix cc role at port reset

On Fri, Jun 16, 2023 at 03:52:40PM +0800, Frank Wang wrote:
> In the current implementation, the tcpm set CC1/CC2 role to open when
> it do port reset would cause the VBUS removed by the Type-C partner.
>
> This sets CC1/CC2 according to the default state of port to fix it.
>
> Signed-off-by: Frank Wang <[email protected]>

Acked-by: Heikki Krogerus <[email protected]>

> ---
> Changelog:
> (no changes since v3)
>
> v2:
> - Make some tweaking based on the default state of port, commented by Guenter Roeck.
>
> v1:
> - https://patchwork.kernel.org/project/linux-usb/patch/[email protected]/
>
> drivers/usb/typec/tcpm/tcpm.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 3c6b0c8e2d3ae..9f6aaa3e70ca8 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -4885,7 +4885,8 @@ static void run_state_machine(struct tcpm_port *port)
> break;
> case PORT_RESET:
> tcpm_reset_port(port);
> - tcpm_set_cc(port, TYPEC_CC_OPEN);
> + tcpm_set_cc(port, tcpm_default_state(port) == SNK_UNATTACHED ?
> + TYPEC_CC_RD : tcpm_rp_cc(port));
> tcpm_set_state(port, PORT_RESET_WAIT_OFF,
> PD_T_ERROR_RECOVERY);
> break;

thanks,

--
heikki