Skip to contentSkip to conversation

TCPM support for FRS and AutoDischarge Disconnect

PATCH Superseded Badhri Jagan Sridharan 3 participants 30 messages Sep 29, 2020 – Oct 20, 2020

Subsystems (4)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Badhri Jagan Sridharan

author maintainer
#0 lore raw

v9 cover TCPM support for FRS and AutoDischarge Disconnect

Hi,

Addressed three comments from v8:
1. Fixed the make dt_binding_check that Rob Herring pointed out in "dt-bindings: usb: Maxim type-c controller device tree binding document"
https://lkml.org/lkml/2020/9/22/720

2. Moved to string based enums for new-source-frs-typec-current to address comments from Rob herring for:
"dt-bindings: connector: Add property to set initial current cap for FRS". As a result, introduced another patch:
"usb: typec: tcpm: Parse frs type-c current from device tree" to refactor tcpm code to read new-source-frs-typec-current from device tree. Previously this is was a uint32.
https://lkml.org/lkml/2020/9/22/729

3. To address Heikki's comment on Auto discharge disconnect threshold
https://lkml.org/lkml/2020/9/21/1857
a. Removed the call to tcpm_set_auto_vbus_discharge_threshold
from source attach path.
b. Moved logic of setting TCPC_VBUS_SINK_DISCONNECT_THRESH_MAX to
tcpci.c as the tcpci_maxim was actually setting the default values. Removed the get_auto_vbus_discharge_threshold callback from tcpci_data for now. Can be added later if needed.

4. Added additional patches to address an isssue where vbus was being left post disconnect after Fast Role Swap operation is executed. a. usb: typec: tcpm: frs sourcing vbus callback
b. usb: typec: tcpci: frs sourcing vbus callback
c. usb: typec: tcpci_max77759: Fix vbus stuck on upon diconnecting sink

Thanks,
Badhri.

Badhri Jagan Sridharan (15):
usb: typec: tcpci: Add a getter method to retrieve tcpm_port reference
usb: typec: tcpci: Add set_vbus tcpci callback
dt-bindings: usb: Maxim type-c controller device tree binding document
usb: typec: tcpci_maxim: Chip level TCPC driver
dt-bindings: connector: Add property to set initial current cap for
FRS
usb: typec: tcpm: Add support for Sink Fast Role SWAP(FRS)
usb: typec: tcpci: Implement callbacks for FRS
usb: typec: tcpci_maxim: Add support for Sink FRS
usb: typec: tcpm: frs sourcing vbus callback
usb: typec: tcpci: frs sourcing vbus callback
usb: typec: tcpci_max77759: Fix vbus stuck on upon diconnecting sink
usb: typec: tcpm: Parse frs type-c current from device tree
usb: typec: tcpm: Implement enabling Auto Discharge disconnect support
usb: typec: tcpci: Implement Auto discharge disconnect callbacks
usb: typec: tcpci_maxim: Enable auto discharge disconnect

 .../bindings/connector/usb-connector.yaml     |  26 +
 .../devicetree/bindings/usb/maxim,tcpci.yaml  |  68 +++
 drivers/usb/typec/tcpm/Kconfig                |   6 +
 drivers/usb/typec/tcpm/Makefile               |  15 +-
 drivers/usb/typec/tcpm/tcpci.c                | 102 +++-
 drivers/usb/typec/tcpm/tcpci.h                |  30 +-
 drivers/usb/typec/tcpm/tcpci_maxim.c          | 504 ++++++++++++++++++
 drivers/usb/typec/tcpm/tcpm.c                 | 299 ++++++++++-
 include/dt-bindings/usb/pd.h                  |  10 +
 include/linux/usb/pd.h                        |  19 +-
 include/linux/usb/tcpm.h                      |  27 +-
 include/linux/usb/typec.h                     |  12 +
 12 files changed, 1093 insertions(+), 25 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/usb/maxim,tcpci.yaml
 create mode 100644 drivers/usb/typec/tcpm/tcpci_maxim.c
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#1 lore raw

PATCH v9 1/15 usb: typec: tcpci: Add a getter method to retrieve tcpm_port reference

Allow chip level drivers to retrieve reference to tcpm_port.

---
Change since v1:
- Changing patch version to v6 to fix version number confusion.

Change since v6:
- Rebase on usb-next
- Added Reviewed-by from Heikki.

Change since v7:
- Rebase on usb-next

Change since v8:
- None

---
 drivers/usb/typec/tcpm/tcpci.c | 6 ++++++
 drivers/usb/typec/tcpm/tcpci.h | 2 ++
 2 files changed, 8 insertions(+)
drivers/usb/typec/tcpm/tcpci.c +6 -0
index 7d9491ba62fb..b960fe5a0f28 100644
@@ -38,6 +38,12 @@ struct tcpci_chip {
 	struct tcpci_data data;
 };
 
+struct tcpm_port *tcpci_get_tcpm_port(struct tcpci *tcpci)
+{
+	return tcpci->port;
+}
+EXPORT_SYMBOL_GPL(tcpci_get_tcpm_port);
+
 static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc)
 {
 	return container_of(tcpc, struct tcpci, tcpc);
drivers/usb/typec/tcpm/tcpci.h +2 -0
index cf9d8b63adcb..04c49a0b0368 100644
@@ -150,4 +150,6 @@ struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data);
 void tcpci_unregister_port(struct tcpci *tcpci);
 irqreturn_t tcpci_irq(struct tcpci *tcpci);
 
+struct tcpm_port;
+struct tcpm_port *tcpci_get_tcpm_port(struct tcpci *tcpci);
 #endif /* __LINUX_USB_TCPCI_H */
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#2 lore raw

PATCH v9 2/15 usb: typec: tcpci: Add set_vbus tcpci callback

In reply to Badhri Jagan Sridharan

set_vbus callback allows TCPC which are TCPCI based, however, does not support turning on sink and source mode through Command.SinkVbus and Command.SourceVbusDefaultVoltage.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Rebase on usb-next

Changes since v7:
- Added Reviewed-by: Heikki
- Rebase change

Change since v8:
- None

---
 drivers/usb/typec/tcpm/tcpci.c | 7 +++++++
 drivers/usb/typec/tcpm/tcpci.h | 1 +
 2 files changed, 8 insertions(+)
drivers/usb/typec/tcpm/tcpci.c +7 -0
index b960fe5a0f28..d6a6fac82d48 100644
@@ -328,6 +328,13 @@ static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool sink)
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
 	int ret;
 
+	if (tcpci->data->set_vbus) {
+		ret = tcpci->data->set_vbus(tcpci, tcpci->data, source, sink);
+		/* Bypass when ret > 0 */
+		if (ret != 0)
+			return ret < 0 ? ret : 0;
+	}
+
 	/* Disable both source and sink first before enabling anything */
 
 	if (!source) {
drivers/usb/typec/tcpm/tcpci.h +1 -0
index 04c49a0b0368..4d441bdf24d5 100644
@@ -144,6 +144,7 @@ struct tcpci_data {
 			 bool enable);
 	int (*start_drp_toggling)(struct tcpci *tcpci, struct tcpci_data *data,
 				  enum typec_cc_status cc);
+	int (*set_vbus)(struct tcpci *tcpci, struct tcpci_data *data, bool source, bool sink);
 };
 
 struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data);
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
#3 lore raw

Re: [PATCH v9 02/15] usb: typec: tcpci: Add set_vbus tcpci callback

Quoted 6 lines · Badhri Jagan Sridharan

set_vbus callback allows TCPC which are TCPCI based, however, does not support turning on sink and source mode through Command.SinkVbus and Command.SourceVbusDefaultVoltage.

This patch breaks the build, are you sure you tested it?

Can you respin it against my latest tree so that I can take the patches that have been reviewed?

thanks,

greg k-h

#4 lore raw

Re: [PATCH v9 02/15] usb: typec: tcpci: Add set_vbus tcpci callback

Quoted 9 lines · Greg Kroah-Hartman
Quoted 6 lines · Badhri Jagan Sridharan

set_vbus callback allows TCPC which are TCPCI based, however, does not support turning on sink and source mode through Command.SinkVbus and Command.SourceVbusDefaultVoltage.

This patch breaks the build, are you sure you tested it?

Sorry, not this patch, patch 4:

drivers/usb/typec/tcpm/tcpci.c: In function ‘tcpci_register_port’:
drivers/usb/typec/tcpm/tcpci.c:630:13: error: ‘struct tcpc_dev’ has no member named ‘enable_frs’
630 | tcpci->tcpc.enable_frs = tcpci_enable_frs;
| ^

thanks,

greg k-h

author maintainer
#5 lore raw

Re: [PATCH v9 02/15] usb: typec: tcpci: Add set_vbus tcpci callback

Hi Greg,

Yes I tested it on usb-next before sending it out.

  630 |  tcpci->tcpc.enable_frs = tcpci_enable_frs;

In https://patchwork.kernel.org/project/linux-usb/list/?series=356837
i.e v9 version of this series,
Patch 7 i.e. https://patchwork.kernel.org/patch/11804847/ is where the above line is added.

I restested in combinations [1] [2] [3] [4]. All of them were clear cherry-picks. I didnt any merge conflicts.

Maybe you are applying patches in a different order ?
If so can you post the git log for me to apply in the same order and test ?

Or Do you want me to rebase on top of usb-testing ? I didnt see any merge conflicts though.

Thanks,
Badhri

[1] usb-testing all patches in series
https://patchwork.kernel.org/project/linux-usb/list/?series=356837
07684bb88ed4 (HEAD -> usb-testing) usb: typec: tcpci_maxim: Enable auto discharge disconnect
1c97f5e32ba6 usb: typec: tcpci: Implement Auto discharge disconnect callbacks 1b829a062e6e usb: typec: tcpm: Implement enabling Auto Discharge disconnect support
7ba4edfd9155 usb: typec: tcpm: Parse frs type-c current from device tree 01d47f2e98ba usb: typec: tcpci_max77759: Fix vbus stuck on upon diconnecting sink
9e8ed3d8809c usb: typec: tcpci: frs sourcing vbus callback 8804a3f75563 usb: typec: tcpm: frs sourcing vbus callback 69fe6c1c7648 usb: typec: tcpci_maxim: Add support for Sink FRS 0a22d446c026 usb: typec: tcpci: Implement callbacks for FRS 93c622006aa6 usb: typec: tcpm: Add support for Sink Fast Role SWAP(FRS) 14672081f2fd dt-bindings: connector: Add property to set initial current cap for FRS
58372bd1d8e8 usb: typec: tcpci_maxim: Chip level TCPC driver c16b09eaf60f dt-bindings: usb: Maxim type-c controller device tree binding document
32d66c0449e1 usb: typec: tcpci: Add set_vbus tcpci callback 85e90e5054d6 usb: typec: tcpci: Add a getter method to retrieve tcpm_port reference
97b65223c18f (origin/usb-testing) USB: core: remove polling for /sys/kernel/debug/usb/devices
da0cb6310094 usb: typec: add support for STUSB160x Type-C controller family

[2] usb-testing: till patch4 in series
https://patchwork.kernel.org/project/linux-usb/list/?series=356837
58372bd1d8e8 (HEAD -> usb-testing) usb: typec: tcpci_maxim: Chip level TCPC driver
c16b09eaf60f dt-bindings: usb: Maxim type-c controller device tree binding document
32d66c0449e1 usb: typec: tcpci: Add set_vbus tcpci callback 85e90e5054d6 usb: typec: tcpci: Add a getter method to retrieve tcpm_port reference
97b65223c18f (origin/usb-testing) USB: core: remove polling for /sys/kernel/debug/usb/devices
da0cb6310094 usb: typec: add support for STUSB160x Type-C controller family

[3] usb-next all patches in series
https://patchwork.kernel.org/project/linux-usb/list/?series=356837
62b5171538da (HEAD -> usb-next) usb: typec: tcpci_maxim: Enable auto discharge disconnect
ea8987805ba6 usb: typec: tcpci: Implement Auto discharge disconnect callbacks af9a12b19352 usb: typec: tcpm: Implement enabling Auto Discharge disconnect support
33aec604a529 usb: typec: tcpm: Parse frs type-c current from device tree 31df45f3df20 usb: typec: tcpci_max77759: Fix vbus stuck on upon diconnecting sink
0945795f170d usb: typec: tcpci: frs sourcing vbus callback c49080982064 usb: typec: tcpm: frs sourcing vbus callback 67a3ff254cc1 usb: typec: tcpci_maxim: Add support for Sink FRS 299582bbf78f usb: typec: tcpci: Implement callbacks for FRS bb4eb3fb65df usb: typec: tcpm: Add support for Sink Fast Role SWAP(FRS) 5aad64e80460 dt-bindings: connector: Add property to set initial current cap for FRS
6c59a16ddee2 usb: typec: tcpci_maxim: Chip level TCPC driver 172274d3e327 dt-bindings: usb: Maxim type-c controller device tree binding document
bdba308a7164 usb: typec: tcpci: Add set_vbus tcpci callback 66b7b0d83399 usb: typec: tcpci: Add a getter method to retrieve tcpm_port reference
59ee364bafb2 (origin/usb-next) Merge tag 'thunderbolt-for-v5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt
into usb-next
bf1c67449833 USB: cdc-acm: clean up no-union-descriptor handling

[4] usb-next till patch4 in series
https://patchwork.kernel.org/project/linux-usb/list/?series=356837
6c59a16ddee2 (HEAD -> usb-next) usb: typec: tcpci_maxim: Chip level TCPC driver 172274d3e327 dt-bindings: usb: Maxim type-c controller device tree binding document
bdba308a7164 usb: typec: tcpci: Add set_vbus tcpci callback 66b7b0d83399 usb: typec: tcpci: Add a getter method to retrieve tcpm_port reference
59ee364bafb2 (origin/usb-next) Merge tag 'thunderbolt-for-v5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt
into usb-next
bf1c67449833 USB: cdc-acm: clean up no-union-descriptor handling

Quoted 23 lines · Greg Kroah-Hartman
Quoted 9 lines · Greg Kroah-Hartman
Quoted 6 lines · Badhri Jagan Sridharan

set_vbus callback allows TCPC which are TCPCI based, however, does not support turning on sink and source mode through Command.SinkVbus and Command.SourceVbusDefaultVoltage.

This patch breaks the build, are you sure you tested it?

Sorry, not this patch, patch 4:

drivers/usb/typec/tcpm/tcpci.c: In function ‘tcpci_register_port’:
drivers/usb/typec/tcpm/tcpci.c:630:13: error: ‘struct tcpc_dev’ has no member named ‘enable_frs’
630 | tcpci->tcpc.enable_frs = tcpci_enable_frs;
| ^

thanks,

greg k-h

#6 lore raw

Re: [PATCH v9 02/15] usb: typec: tcpci: Add set_vbus tcpci callback

Quoted 11 lines · Badhri Jagan Sridharan

Hi Greg,

Yes I tested it on usb-next before sending it out.

  630 |  tcpci->tcpc.enable_frs = tcpci_enable_frs;

In https://patchwork.kernel.org/project/linux-usb/list/?series=356837
i.e v9 version of this series,
Patch 7 i.e. https://patchwork.kernel.org/patch/11804847/ is where the above line is added.

Quoted 4 lines

I restested in combinations [1] [2] [3] [4]. All of them were clear cherry-picks. I didnt any merge conflicts.

Maybe you are applying patches in a different order ?

I didn't think so. Can you try applying the patches, in order, and building after each one to see if you get the same error or not?

My usb-next branch is up to date with all of the recent patches.

thanks,

greg k-h

author maintainer
#7 lore raw

Re: [PATCH v9 02/15] usb: typec: tcpci: Add set_vbus tcpci callback

Quoted 13 lines · Greg Kroah-Hartman
Quoted 11 lines · Badhri Jagan Sridharan

Hi Greg,

Yes I tested it on usb-next before sending it out.

  630 |  tcpci->tcpc.enable_frs = tcpci_enable_frs;

In https://patchwork.kernel.org/project/linux-usb/list/?series=356837
i.e v9 version of this series,
Patch 7 i.e. https://patchwork.kernel.org/patch/11804847/ is where the above line is added.

Quoted 7 lines
Quoted 4 lines

I restested in combinations [1] [2] [3] [4]. All of them were clear cherry-picks. I didnt any merge conflicts.

Maybe you are applying patches in a different order ?

I didn't think so. Can you try applying the patches, in order, and building after each one to see if you get the same error or not?

Tried this as well. Not sure what I am doing differently. Also was manually looking for "tcpci_enable_frs" in the series and it's first occurrence is in:
"[PATCH v9 07/15] usb: typec: tcpci: Implement callbacks for FRS".

Just sent out the v10 after addressing a couple of comments from Rob Herring.

Thanks,
Badhri

Quoted 6 lines

My usb-next branch is up to date with all of the recent patches.

thanks,

greg k-h

author maintainer
#8 lore raw

PATCH v9 3/15 dt-bindings: usb: Maxim type-c controller device tree binding document

In reply to Badhri Jagan Sridharan

Add device tree binding document for Maxim TCPCI based Type-C chip driver

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Migrated to yaml format.

Changes since v7:
- Rebase on usb-next

Changes since v8:
- Fix errors from make dt_binding_check as suggested by Rob Herring.

---
 .../devicetree/bindings/usb/maxim,tcpci.yaml  | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/usb/maxim,tcpci.yaml
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +68 -0
new file mode 100644
index 000000000000..f4b5f1a09b98
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/maxim,tcpci.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Maxim TCPCI Type-C PD controller DT bindings
+
+maintainers:
+  - Badhri Jagan Sridharan <[email protected]>
+
+description: Maxim TCPCI Type-C PD controller
+
+properties:
+  compatible:
+    enum:
+      - maxim,tcpci
+
+  interrupts:
+    maxItems: 1
+
+  connector:
+    type: object
+    $ref: ../connector/usb-connector.yaml#
+    description:
+      Properties for usb c connector.
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/usb/pd.h>
+    i2c0 {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        maxtcpc@25 {
+            compatible = "maxim,tcpc";
+            reg = <0x25>;
+            interrupt-parent = <&gpa8>;
+            interrupts = <2 IRQ_TYPE_LEVEL_LOW>;
+
+            connector {
+                compatible = "usb-c-connector";
+                label = "USB-C";
+                data-role = "dual";
+                power-role = "dual";
+                try-power-role = "sink";
+                self-powered;
+                op-sink-microwatt = <2600000>;
+                source-pdos = <PDO_FIXED(5000, 900,
+                                         PDO_FIXED_SUSPEND |
+                                         PDO_FIXED_USB_COMM |
+                                         PDO_FIXED_DATA_SWAP |
+                                         PDO_FIXED_DUAL_ROLE)>;
+                sink-pdos = <PDO_FIXED(5000, 3000,
+                                       PDO_FIXED_USB_COMM |
+                                       PDO_FIXED_DATA_SWAP |
+                                       PDO_FIXED_DUAL_ROLE)
+                                       PDO_FIXED(9000, 2000, 0)>;
+            };
+        };
+    };
+...
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
maintainer
#9 lore raw

Re: [PATCH v9 03/15] dt-bindings: usb: Maxim type-c controller device tree binding document

Quoted 34 lines · Badhri Jagan Sridharan

Add device tree binding document for Maxim TCPCI based Type-C chip driver

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Migrated to yaml format.

Changes since v7:
- Rebase on usb-next

Changes since v8:
- Fix errors from make dt_binding_check as suggested by Rob Herring.

---
 .../devicetree/bindings/usb/maxim,tcpci.yaml  | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/usb/maxim,tcpci.yaml
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +7 -0
new file mode 100644
index 000000000000..f4b5f1a09b98
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/maxim,tcpci.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Maxim TCPCI Type-C PD controller DT bindings

Documentation/devicetree/bindings/usb/maxim,tcpci.yaml line 17

Quoted 10 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +10 -0
+
+maintainers:
+  - Badhri Jagan Sridharan <[email protected]>
+
+description: Maxim TCPCI Type-C PD controller
+
+properties:
+  compatible:
+    enum:
+      - maxim,tcpci

Is there a datasheet for this? Searching for 'tcpci' doesn't really come up with anything other than this patch. Only chip I found is MAX77958. Bindings are for specific h/w devices.

Quoted 4 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +4 -0
+
+  interrupts:
+    maxItems: 1
+

Documentation/devicetree/bindings/usb/maxim,tcpci.yaml line 31

Quoted 10 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +10 -0
+  connector:
+    type: object
+    $ref: ../connector/usb-connector.yaml#
+    description:
+      Properties for usb c connector.
+
+required:
+  - compatible
+  - reg
+  - interrupts

additionalProperties: false

Quoted 40 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +37 -0
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/usb/pd.h>
+    i2c0 {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        maxtcpc@25 {
+            compatible = "maxim,tcpc";
+            reg = <0x25>;
+            interrupt-parent = <&gpa8>;
+            interrupts = <2 IRQ_TYPE_LEVEL_LOW>;
+
+            connector {
+                compatible = "usb-c-connector";
+                label = "USB-C";
+                data-role = "dual";
+                power-role = "dual";
+                try-power-role = "sink";
+                self-powered;
+                op-sink-microwatt = <2600000>;
+                source-pdos = <PDO_FIXED(5000, 900,
+                                         PDO_FIXED_SUSPEND |
+                                         PDO_FIXED_USB_COMM |
+                                         PDO_FIXED_DATA_SWAP |
+                                         PDO_FIXED_DUAL_ROLE)>;
+                sink-pdos = <PDO_FIXED(5000, 3000,
+                                       PDO_FIXED_USB_COMM |
+                                       PDO_FIXED_DATA_SWAP |
+                                       PDO_FIXED_DUAL_ROLE)
+                                       PDO_FIXED(9000, 2000, 0)>;
+            };
+        };
+    };
+...
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#10 lore raw

Re: [PATCH v9 03/15] dt-bindings: usb: Maxim type-c controller device tree binding document

Hi Robb,

Thanks for the reviews ! Responses inline.

Regards,
Badhri

Quoted 42 lines · Rob Herring
Quoted 40 lines · Badhri Jagan Sridharan

Add device tree binding document for Maxim TCPCI based Type-C chip driver

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Migrated to yaml format.

Changes since v7:
- Rebase on usb-next

Changes since v8:
- Fix errors from make dt_binding_check as suggested by Rob Herring.

---
 .../devicetree/bindings/usb/maxim,tcpci.yaml  | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/usb/maxim,tcpci.yaml
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +13 -0
new file mode 100644
index 000000000000..f4b5f1a09b98
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/maxim,tcpci.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Maxim TCPCI Type-C PD controller DT bindings
+
+maintainers:
+  - Badhri Jagan Sridharan <[email protected]>
+
+description: Maxim TCPCI Type-C PD controller
+
Quoted 8 lines

Documentation/devicetree/bindings/usb/maxim,tcpci.yaml line 17

Quoted 4 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +4 -0
+properties:
+  compatible:
+    enum:
+      - maxim,tcpci

Is there a datasheet for this? Searching for 'tcpci' doesn't really come up with anything other than this patch. Only chip I found is MAX77958. Bindings are for specific h/w devices.

Unfortunately the datasheet cannot be made public yet. Has the datasheet have to be made public before sending the bindings ?

Quoted 17 lines
Quoted 5 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +4 -0
+
+  interrupts:
+    maxItems: 1
+

Documentation/devicetree/bindings/usb/maxim,tcpci.yaml line 31

Quoted 10 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +10 -0
+  connector:
+    type: object
+    $ref: ../connector/usb-connector.yaml#
+    description:
+      Properties for usb c connector.
+
+required:
+  - compatible
+  - reg
+  - interrupts

additionalProperties: false

ACK. Adding to the next version of the patch.

Quoted 41 lines
Quoted 41 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +37 -1
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/usb/pd.h>
+    i2c0 {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        maxtcpc@25 {
+            compatible = "maxim,tcpc";
+            reg = <0x25>;
+            interrupt-parent = <&gpa8>;
+            interrupts = <2 IRQ_TYPE_LEVEL_LOW>;
+
+            connector {
+                compatible = "usb-c-connector";
+                label = "USB-C";
+                data-role = "dual";
+                power-role = "dual";
+                try-power-role = "sink";
+                self-powered;
+                op-sink-microwatt = <2600000>;
+                source-pdos = <PDO_FIXED(5000, 900,
+                                         PDO_FIXED_SUSPEND |
+                                         PDO_FIXED_USB_COMM |
+                                         PDO_FIXED_DATA_SWAP |
+                                         PDO_FIXED_DUAL_ROLE)>;
+                sink-pdos = <PDO_FIXED(5000, 3000,
+                                       PDO_FIXED_USB_COMM |
+                                       PDO_FIXED_DATA_SWAP |
+                                       PDO_FIXED_DUAL_ROLE)
+                                       PDO_FIXED(9000, 2000, 0)>;
+            };
+        };
+    };
+...
--

2.28.0.709.gb0816b6eb0-goog

maintainer
#11 lore raw

Re: [PATCH v9 03/15] dt-bindings: usb: Maxim type-c controller device tree binding document

Quoted 60 lines · Badhri Jagan Sridharan

Hi Robb,

Thanks for the reviews ! Responses inline.

Regards,
Badhri

Quoted 42 lines · Rob Herring
Quoted 40 lines · Badhri Jagan Sridharan

Add device tree binding document for Maxim TCPCI based Type-C chip driver

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Migrated to yaml format.

Changes since v7:
- Rebase on usb-next

Changes since v8:
- Fix errors from make dt_binding_check as suggested by Rob Herring.

---
 .../devicetree/bindings/usb/maxim,tcpci.yaml  | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/usb/maxim,tcpci.yaml
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +13 -0
new file mode 100644
index 000000000000..f4b5f1a09b98
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/maxim,tcpci.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Maxim TCPCI Type-C PD controller DT bindings
+
+maintainers:
+  - Badhri Jagan Sridharan <[email protected]>
+
+description: Maxim TCPCI Type-C PD controller
+
Quoted 8 lines

Documentation/devicetree/bindings/usb/maxim,tcpci.yaml line 17

Quoted 4 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +4 -0
+properties:
+  compatible:
+    enum:
+      - maxim,tcpci

Is there a datasheet for this? Searching for 'tcpci' doesn't really come up with anything other than this patch. Only chip I found is MAX77958. Bindings are for specific h/w devices.

Quoted 2 lines

Unfortunately the datasheet cannot be made public yet. Has the datasheet have to be made public before sending the bindings ?

No, but we need a part number or some assurance that 'tcpci' is a specific part.

Rob

maintainer
#12 lore raw

Re: [PATCH v9 03/15] dt-bindings: usb: Maxim type-c controller device tree binding document

Quoted 62 lines · Rob Herring
Quoted 60 lines · Badhri Jagan Sridharan

Hi Robb,

Thanks for the reviews ! Responses inline.

Regards,
Badhri

Quoted 50 lines · Rob Herring
Quoted 34 lines · Badhri Jagan Sridharan

Add device tree binding document for Maxim TCPCI based Type-C chip driver

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Migrated to yaml format.

Changes since v7:
- Rebase on usb-next

Changes since v8:
- Fix errors from make dt_binding_check as suggested by Rob Herring.

---
 .../devicetree/bindings/usb/maxim,tcpci.yaml  | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/usb/maxim,tcpci.yaml
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +7 -0
new file mode 100644
index 000000000000..f4b5f1a09b98
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/maxim,tcpci.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Maxim TCPCI Type-C PD controller DT bindings

Documentation/devicetree/bindings/usb/maxim,tcpci.yaml line 17

Quoted 10 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +10 -0
+
+maintainers:
+  - Badhri Jagan Sridharan <[email protected]>
+
+description: Maxim TCPCI Type-C PD controller
+
+properties:
+  compatible:
+    enum:
+      - maxim,tcpci

Is there a datasheet for this? Searching for 'tcpci' doesn't really come up with anything other than this patch. Only chip I found is MAX77958. Bindings are for specific h/w devices.

Quoted 4 lines
Quoted 2 lines

Unfortunately the datasheet cannot be made public yet. Has the datasheet have to be made public before sending the bindings ?

No, but we need a part number or some assurance that 'tcpci' is a specific part.

I guess TCPCI is USB Type-C Port Controller Interface Specification.

That's just a protocol definition, not a chip. DT describes h/w which is more than just the protocol.

Rob

author maintainer
#13 lore raw

Re: [PATCH v9 03/15] dt-bindings: usb: Maxim type-c controller device tree binding document

Hi Rob,

Apologies for the delay. Was coordinating care for my parents who caught the COVID bug.

Thanks,
Badhri

Quoted 64 lines · Rob Herring
Quoted 62 lines · Rob Herring
Quoted 60 lines · Badhri Jagan Sridharan

Hi Robb,

Thanks for the reviews ! Responses inline.

Regards,
Badhri

Quoted 50 lines · Rob Herring
Quoted 34 lines · Badhri Jagan Sridharan

Add device tree binding document for Maxim TCPCI based Type-C chip driver

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Migrated to yaml format.

Changes since v7:
- Rebase on usb-next

Changes since v8:
- Fix errors from make dt_binding_check as suggested by Rob Herring.

---
 .../devicetree/bindings/usb/maxim,tcpci.yaml  | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/usb/maxim,tcpci.yaml
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +7 -0
new file mode 100644
index 000000000000..f4b5f1a09b98
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/maxim,tcpci.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Maxim TCPCI Type-C PD controller DT bindings

Documentation/devicetree/bindings/usb/maxim,tcpci.yaml line 17

Quoted 10 lines
Documentation/devicetree/bindings/usb/maxim,tcpci.yaml new file +10 -0
+
+maintainers:
+  - Badhri Jagan Sridharan <[email protected]>
+
+description: Maxim TCPCI Type-C PD controller
+
+properties:
+  compatible:
+    enum:
+      - maxim,tcpci

Is there a datasheet for this? Searching for 'tcpci' doesn't really come up with anything other than this patch. Only chip I found is MAX77958. Bindings are for specific h/w devices.

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Unfortunately the datasheet cannot be made public yet. Has the datasheet have to be made public before sending the bindings ?

No, but we need a part number or some assurance that 'tcpci' is a specific part.

Sure. Added the part number to the binding and changed the compatible string. Sending this as part of v11

Quoted 7 lines

I guess TCPCI is USB Type-C Port Controller Interface Specification.

That's just a protocol definition, not a chip. DT describes h/w which is more than just the protocol.

Rob

author maintainer
#14 lore raw

PATCH v9 4/15 usb: typec: tcpci_maxim: Chip level TCPC driver

In reply to Badhri Jagan Sridharan

Chip level TCPC driver for Maxim's TCPCI implementation. This TCPC implementation does not support the following
commands: COMMAND.SinkVbus, COMMAND.SourceVbusDefaultVoltage, COMMAND.SourceVbusHighVoltage. Instead the sinking and sourcing from vbus is supported by writes to custom registers.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.
- Removed setting USB_PSY and terminating description with period as suggested by Randy.

Changes since v6:
- Addressed Heikki comments:
- Removed TX discarded message
- Removed the redundant TCPC_POWER_STATUS_UNINIT check
- Cleaned up irq setup routine

Changes since v7:
- Added reviewed-by: Heikki

Changes since v8:
- None

---
 drivers/usb/typec/tcpm/Kconfig       |   6 +
 drivers/usb/typec/tcpm/Makefile      |  15 +-
 drivers/usb/typec/tcpm/tcpci.h       |   1 +
 drivers/usb/typec/tcpm/tcpci_maxim.c | 461 +++++++++++++++++++++++++++
 4 files changed, 476 insertions(+), 7 deletions(-)
 create mode 100644 drivers/usb/typec/tcpm/tcpci_maxim.c
drivers/usb/typec/tcpm/Kconfig +6 -0
index 58a64e1bf627..557f392fe24d 100644
@@ -35,6 +35,12 @@ config TYPEC_MT6360
 	  USB Type-C. It works with Type-C Port Controller Manager
 	  to provide USB PD and USB Type-C functionalities.
 
+config TYPEC_TCPCI_MAXIM
+	tristate "Maxim TCPCI based Type-C chip driver"
+	help
+	  MAXIM TCPCI based Type-C/PD chip driver. Works with
+	  with Type-C Port Controller Manager.
+
 endif # TYPEC_TCPCI
 
 config TYPEC_FUSB302
drivers/usb/typec/tcpm/Makefile +8 -7
index 7592ccb8c526..7d499f3569fd 100644
@@ -1,8 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
-obj-$(CONFIG_TYPEC_TCPM)	+= tcpm.o
-obj-$(CONFIG_TYPEC_FUSB302)	+= fusb302.o
-obj-$(CONFIG_TYPEC_WCOVE)	+= typec_wcove.o
-typec_wcove-y			:= wcove.o
-obj-$(CONFIG_TYPEC_TCPCI)	+= tcpci.o
-obj-$(CONFIG_TYPEC_RT1711H)	+= tcpci_rt1711h.o
-obj-$(CONFIG_TYPEC_MT6360)	+= tcpci_mt6360.o
+obj-$(CONFIG_TYPEC_TCPM)		+= tcpm.o
+obj-$(CONFIG_TYPEC_FUSB302)		+= fusb302.o
+obj-$(CONFIG_TYPEC_WCOVE)		+= typec_wcove.o
+typec_wcove-y				:= wcove.o
+obj-$(CONFIG_TYPEC_TCPCI)		+= tcpci.o
+obj-$(CONFIG_TYPEC_RT1711H)		+= tcpci_rt1711h.o
+obj-$(CONFIG_TYPEC_MT6360)		+= tcpci_mt6360.o
+obj-$(CONFIG_TYPEC_TCPCI_MAXIM)		+= tcpci_maxim.o
drivers/usb/typec/tcpm/tcpci.h +1 -0
index 4d441bdf24d5..82f021a82456 100644
@@ -109,6 +109,7 @@
 
 #define TCPC_RX_BYTE_CNT		0x30
 #define TCPC_RX_BUF_FRAME_TYPE		0x31
+#define TCPC_RX_BUF_FRAME_TYPE_SOP	0
 #define TCPC_RX_HDR			0x32
 #define TCPC_RX_DATA			0x34 /* through 0x4f */
 
drivers/usb/typec/tcpm/tcpci_maxim.c new file +461 -0

461 lines not on this page · raw message

Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#15 lore raw

PATCH v9 5/15 dt-bindings: connector: Add property to set initial current cap for FRS

In reply to Badhri Jagan Sridharan

This change adds frs-typec-current which allows setting the initial current capability of the new source when vSafe5V is applied during PD3.0 sink Fast Role Swap.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Removed the redundant usb-connector.txt that I created by mistake.
- Moved to yaml.

Changes since v7:
- Rebase

Changes since v8:
- Redefine new-source-frs-typec-current as string enums to address Rob Herring's comment.

---
 .../bindings/connector/usb-connector.yaml     | 26 +++++++++++++++++++
 include/dt-bindings/usb/pd.h                  | 10 +++++++
 2 files changed, 36 insertions(+)
Documentation/devicetree/bindings/connector/usb-connector.yaml +26 -0
index 9bd52e63c935..0b8cd08a8678 100644
@@ -142,6 +142,32 @@ properties:
     required:
       - port@0
 
+  new-source-frs-typec-current:
+    description: Initial current capability of the new source when vSafe5V
+      is applied during PD3.0 Fast Role Swap. "Table 6-14 Fixed Supply PDO - Sink"
+      of "USB Power Delivery Specification Revision 3.0, Version 1.2" provides the
+      different power levels and "6.4.1.3.1.6 Fast Role Swap USB Type-C Current"
+      provides a detailed description of the field. The sink PDO from current source
+      reflects the current source's(i.e. transmitter of the FRS signal) power
+      requirement during fr swap. The current sink (i.e. receiver of the FRS signal),
+      a.k.a new source, should check if it will be able to satisfy the current source's,
+      new sink's, requirement during frswap before enabling the frs signal reception.
+      This property refers to maximum current capability that the current sink can
+      satisfy. During FRS, VBUS voltage is at 5V, as the partners are in implicit
+      contract, hence, the power level is only a function of the current capability.
+      "not-supported" implies sink to source fast role swap not supported.
+      "default" refers to default USB power level as described by
+      "Table 6-14 Fixed Supply PDO - Sink".
+      "1.5A" refers to 1.5A@5V.
+      "3.0A" refers to 3.0A@5V.
+
+    $ref: /schemas/types.yaml#/definitions/string
+    enum:
+      - not-supported
+      - default
+      - 1.5A
+      - 3.0A
+
 required:
   - compatible
 
include/dt-bindings/usb/pd.h +10 -0
index 985f2bbd4d24..db1ad4532197 100644
@@ -35,6 +35,16 @@
 
 #define VSAFE5V 5000 /* mv units */
 
+/*
+ * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
+ * Version 1.2"
+ * Initial current capability of the new source when vSafe5V is applied.
+ */
+#define FRS_NOT_SUPPORTED	0
+#define FRS_DEFAULT_POWER	1
+#define FRS_5V_1P5A		2
+#define FRS_5V_3A		3
+
 #define PDO_BATT_MAX_VOLT_SHIFT	20	/* 50mV units */
 #define PDO_BATT_MIN_VOLT_SHIFT	10	/* 50mV units */
 #define PDO_BATT_MAX_PWR_SHIFT	0	/* 250mW units */
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
maintainer
#16 lore raw

Re: [PATCH v9 05/15] dt-bindings: connector: Add property to set initial current cap for FRS

Quoted 3 lines · Badhri Jagan Sridharan

This change adds frs-typec-current which allows setting the initial current capability of the new source when vSafe5V is applied during PD3.0 sink Fast Role Swap.

Shouldn't you Cc the person you copied this from?

Quoted 45 lines

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Removed the redundant usb-connector.txt that I created by mistake.
- Moved to yaml.

Changes since v7:
- Rebase

Changes since v8:
- Redefine new-source-frs-typec-current as string enums to address Rob Herring's comment.

---
 .../bindings/connector/usb-connector.yaml     | 26 +++++++++++++++++++
 include/dt-bindings/usb/pd.h                  | 10 +++++++
 2 files changed, 36 insertions(+)
Documentation/devicetree/bindings/connector/usb-connector.yaml +17 -0
index 9bd52e63c935..0b8cd08a8678 100644
@@ -142,6 +142,32 @@ properties:
     required:
       - port@0
 
+  new-source-frs-typec-current:
+    description: Initial current capability of the new source when vSafe5V
+      is applied during PD3.0 Fast Role Swap. "Table 6-14 Fixed Supply PDO - Sink"
+      of "USB Power Delivery Specification Revision 3.0, Version 1.2" provides the
+      different power levels and "6.4.1.3.1.6 Fast Role Swap USB Type-C Current"
+      provides a detailed description of the field. The sink PDO from current source
+      reflects the current source's(i.e. transmitter of the FRS signal) power
+      requirement during fr swap. The current sink (i.e. receiver of the FRS signal),
+      a.k.a new source, should check if it will be able to satisfy the current source's,
+      new sink's, requirement during frswap before enabling the frs signal reception.
+      This property refers to maximum current capability that the current sink can
+      satisfy. During FRS, VBUS voltage is at 5V, as the partners are in implicit
+      contract, hence, the power level is only a function of the current capability.
+      "not-supported" implies sink to source fast role swap not supported.
+      "default" refers to default USB power level as described by
+      "Table 6-14 Fixed Supply PDO - Sink".
+      "1.5A" refers to 1.5A@5V.

Documentation/devicetree/bindings/connector/usb-connector.yaml properties: · line 171

Quoted 10 lines
Documentation/devicetree/bindings/connector/usb-connector.yaml +8 -0
+      "3.0A" refers to 3.0A@5V.
 
 
+
+    $ref: /schemas/types.yaml#/definitions/string
+    enum:
+      - not-supported
+      - default
+      - 1.5A
+      - 3.0A

What happens if the property is not present?

I'm not crazy about mixing strings and what could be a number.

Quoted 11 lines
Documentation/devicetree/bindings/connector/usb-connector.yaml +1 -0
+
 required:

- compatible

include/dt-bindings/usb/pd.h +0 -0
index 985f2bbd4d24..db1ad4532197 100644
@@ -35,6 +35,16 @@
 
 #define VSAFE5V 5000 /* mv units */

include/dt-bindings/usb/pd.h line 46

Quoted 10 lines
include/dt-bindings/usb/pd.h +9 -0
 
+/*
+ * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
+ * Version 1.2"
+ * Initial current capability of the new source when vSafe5V is applied.
+ */
+#define FRS_NOT_SUPPORTED	0
+#define FRS_DEFAULT_POWER	1
+#define FRS_5V_1P5A		2
+#define FRS_5V_3A		3

Why are these in a DT header, but not used by the binding? Though perhaps they should be. Are these numbers from the spec or made up?

Quoted 7 lines
include/dt-bindings/usb/pd.h +1 -0
+
 #define PDO_BATT_MAX_VOLT_SHIFT	20	/* 50mV units */
 #define PDO_BATT_MIN_VOLT_SHIFT	10	/* 50mV units */
 #define PDO_BATT_MAX_PWR_SHIFT	0	/* 250mW units */
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#17 lore raw

Re: [PATCH v9 05/15] dt-bindings: connector: Add property to set initial current cap for FRS

Quoted 7 lines · Rob Herring
Quoted 3 lines · Badhri Jagan Sridharan

This change adds frs-typec-current which allows setting the initial current capability of the new source when vSafe5V is applied during PD3.0 sink Fast Role Swap.

Shouldn't you Cc the person you copied this from?

Not sure whether I get this comment. The patch wasn't copied. Maybe you were expecting me to CC [email protected] ? if so, just now CC'ed.

Quoted 58 lines
Quoted 47 lines

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Removed the redundant usb-connector.txt that I created by mistake.
- Moved to yaml.

Changes since v7:
- Rebase

Changes since v8:
- Redefine new-source-frs-typec-current as string enums to address Rob Herring's comment.

---
 .../bindings/connector/usb-connector.yaml     | 26 +++++++++++++++++++
 include/dt-bindings/usb/pd.h                  | 10 +++++++
 2 files changed, 36 insertions(+)
Documentation/devicetree/bindings/connector/usb-connector.yaml +17 -0
index 9bd52e63c935..0b8cd08a8678 100644
@@ -142,6 +142,32 @@ properties:
     required:
       - port@0
 
+  new-source-frs-typec-current:
+    description: Initial current capability of the new source when vSafe5V
+      is applied during PD3.0 Fast Role Swap. "Table 6-14 Fixed Supply PDO - Sink"
+      of "USB Power Delivery Specification Revision 3.0, Version 1.2" provides the
+      different power levels and "6.4.1.3.1.6 Fast Role Swap USB Type-C Current"
+      provides a detailed description of the field. The sink PDO from current source
+      reflects the current source's(i.e. transmitter of the FRS signal) power
+      requirement during fr swap. The current sink (i.e. receiver of the FRS signal),
+      a.k.a new source, should check if it will be able to satisfy the current source's,
+      new sink's, requirement during frswap before enabling the frs signal reception.
+      This property refers to maximum current capability that the current sink can
+      satisfy. During FRS, VBUS voltage is at 5V, as the partners are in implicit
+      contract, hence, the power level is only a function of the current capability.
+      "not-supported" implies sink to source fast role swap not supported.
+      "default" refers to default USB power level as described by
+      "Table 6-14 Fixed Supply PDO - Sink".
+      "1.5A" refers to 1.5A@5V.

Documentation/devicetree/bindings/connector/usb-connector.yaml properties: · line 171

Quoted 10 lines
Documentation/devicetree/bindings/connector/usb-connector.yaml +8 -0
+      "3.0A" refers to 3.0A@5V.
 
 
+
+    $ref: /schemas/types.yaml#/definitions/string
+    enum:
+      - not-supported
+      - default
+      - 1.5A
+      - 3.0A
Quoted 1 line

What happens if the property is not present?

The behavior would be the same as "not-supported".

Quoted 2 lines

I'm not crazy about mixing strings and what could be a number.

v8 version[1] of the patch was using uint32. I moved to using strings as you were asking to unify with the approach in [2]. Since [3] was using string enums, I moved to that. I don't have a strong preference here, so I can move back to using u32 if you feel so.

[1] https://lore.kernel.org/linux-usb/[email protected]/
[2] https://lore.kernel.org/linux-usb/CAPTae5+Pe1m=TUhmPJY91eJbP+B6UroqBo3u7m0AC8YS1WPu9g@mail.gmail.com/
[3] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Quoted 13 lines
Quoted 13 lines
Documentation/devicetree/bindings/connector/usb-connector.yaml +1 -0
+
 required:

- compatible

include/dt-bindings/usb/pd.h +0 -0
index 985f2bbd4d24..db1ad4532197 100644
@@ -35,6 +35,16 @@
 
 #define VSAFE5V 5000 /* mv units */
 
Quoted 11 lines

include/dt-bindings/usb/pd.h line 46

Quoted 9 lines
include/dt-bindings/usb/pd.h +9 -0
+/*
+ * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
+ * Version 1.2"
+ * Initial current capability of the new source when vSafe5V is applied.
+ */
+#define FRS_NOT_SUPPORTED    0
+#define FRS_DEFAULT_POWER    1
+#define FRS_5V_1P5A          2
+#define FRS_5V_3A            3

Why are these in a DT header, but not used by the binding? Though

This is a mistake on my part. v8 of the patch which had new-source-frs-typec-current
defined as uint32 were making use of these constants. Now that new-source-frs-typec-current
is a string, these constants dont have any more significance in the binding. Will remove in the next version of the patch.

Quoted 1 line

perhaps they should be. Are these numbers from the spec or made up?

Yes they are from the spec and are from "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0, Version 1.2". Not made up.

Quoted 8 lines
Quoted 8 lines
include/dt-bindings/usb/pd.h +1 -1
+
 #define PDO_BATT_MAX_VOLT_SHIFT      20      /* 50mV units */
 #define PDO_BATT_MIN_VOLT_SHIFT      10      /* 50mV units */
 #define PDO_BATT_MAX_PWR_SHIFT       0       /* 250mW units */
--

2.28.0.709.gb0816b6eb0-goog

maintainer
#18 lore raw

Re: [PATCH v9 05/15] dt-bindings: connector: Add property to set initial current cap for FRS

Quoted 70 lines · Badhri Jagan Sridharan
Quoted 7 lines · Rob Herring
Quoted 3 lines · Badhri Jagan Sridharan

This change adds frs-typec-current which allows setting the initial current capability of the new source when vSafe5V is applied during PD3.0 sink Fast Role Swap.

Shouldn't you Cc the person you copied this from?

Not sure whether I get this comment. The patch wasn't copied. Maybe you were expecting me to CC [email protected] ? if so, just now CC'ed.

Quoted 58 lines
Quoted 57 lines

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Removed the redundant usb-connector.txt that I created by mistake.
- Moved to yaml.

Changes since v7:
- Rebase

Changes since v8:
- Redefine new-source-frs-typec-current as string enums to address Rob Herring's comment.

---
 .../bindings/connector/usb-connector.yaml     | 26 +++++++++++++++++++
 include/dt-bindings/usb/pd.h                  | 10 +++++++
 2 files changed, 36 insertions(+)
Documentation/devicetree/bindings/connector/usb-connector.yaml +25 -0
index 9bd52e63c935..0b8cd08a8678 100644
@@ -142,6 +142,32 @@ properties:
     required:
       - port@0
 
+  new-source-frs-typec-current:
+    description: Initial current capability of the new source when vSafe5V
+      is applied during PD3.0 Fast Role Swap. "Table 6-14 Fixed Supply PDO - Sink"
+      of "USB Power Delivery Specification Revision 3.0, Version 1.2" provides the
+      different power levels and "6.4.1.3.1.6 Fast Role Swap USB Type-C Current"
+      provides a detailed description of the field. The sink PDO from current source
+      reflects the current source's(i.e. transmitter of the FRS signal) power
+      requirement during fr swap. The current sink (i.e. receiver of the FRS signal),
+      a.k.a new source, should check if it will be able to satisfy the current source's,
+      new sink's, requirement during frswap before enabling the frs signal reception.
+      This property refers to maximum current capability that the current sink can
+      satisfy. During FRS, VBUS voltage is at 5V, as the partners are in implicit
+      contract, hence, the power level is only a function of the current capability.
+      "not-supported" implies sink to source fast role swap not supported.
+      "default" refers to default USB power level as described by
+      "Table 6-14 Fixed Supply PDO - Sink".
+      "1.5A" refers to 1.5A@5V.
+      "3.0A" refers to 3.0A@5V.
 
 
+
+    $ref: /schemas/types.yaml#/definitions/string
+    enum:
+      - not-supported
+      - default
+      - 1.5A
+      - 3.0A
Quoted 3 lines
Quoted 1 line

What happens if the property is not present?

The behavior would be the same as "not-supported".

Then you don't need 'not-supported'.

Quoted 8 lines
Quoted 3 lines

I'm not crazy about mixing strings and what could be a number.

v8 version[1] of the patch was using uint32. I moved to using strings as you were asking to unify with the approach in [2]. Since [3] was using string enums, I moved to that. I don't have a strong preference here, so I can move back to using u32 if you feel so.

Since the u32 values are based on the USB spec, I think I prefer that. Should be easier to deal with in the driver than doing strcmp's.

Rob

author maintainer
#19 lore raw

Re: [PATCH v9 05/15] dt-bindings: connector: Add property to set initial current cap for FRS

Quoted 82 lines · Rob Herring
Quoted 70 lines · Badhri Jagan Sridharan
Quoted 7 lines · Rob Herring
Quoted 3 lines · Badhri Jagan Sridharan

This change adds frs-typec-current which allows setting the initial current capability of the new source when vSafe5V is applied during PD3.0 sink Fast Role Swap.

Shouldn't you Cc the person you copied this from?

Not sure whether I get this comment. The patch wasn't copied. Maybe you were expecting me to CC [email protected] ? if so, just now CC'ed.

Quoted 58 lines
Quoted 57 lines

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Removed the redundant usb-connector.txt that I created by mistake.
- Moved to yaml.

Changes since v7:
- Rebase

Changes since v8:
- Redefine new-source-frs-typec-current as string enums to address Rob Herring's comment.

---
 .../bindings/connector/usb-connector.yaml     | 26 +++++++++++++++++++
 include/dt-bindings/usb/pd.h                  | 10 +++++++
 2 files changed, 36 insertions(+)
Documentation/devicetree/bindings/connector/usb-connector.yaml +25 -0
index 9bd52e63c935..0b8cd08a8678 100644
@@ -142,6 +142,32 @@ properties:
     required:
       - port@0
 
+  new-source-frs-typec-current:
+    description: Initial current capability of the new source when vSafe5V
+      is applied during PD3.0 Fast Role Swap. "Table 6-14 Fixed Supply PDO - Sink"
+      of "USB Power Delivery Specification Revision 3.0, Version 1.2" provides the
+      different power levels and "6.4.1.3.1.6 Fast Role Swap USB Type-C Current"
+      provides a detailed description of the field. The sink PDO from current source
+      reflects the current source's(i.e. transmitter of the FRS signal) power
+      requirement during fr swap. The current sink (i.e. receiver of the FRS signal),
+      a.k.a new source, should check if it will be able to satisfy the current source's,
+      new sink's, requirement during frswap before enabling the frs signal reception.
+      This property refers to maximum current capability that the current sink can
+      satisfy. During FRS, VBUS voltage is at 5V, as the partners are in implicit
+      contract, hence, the power level is only a function of the current capability.
+      "not-supported" implies sink to source fast role swap not supported.
+      "default" refers to default USB power level as described by
+      "Table 6-14 Fixed Supply PDO - Sink".
+      "1.5A" refers to 1.5A@5V.
+      "3.0A" refers to 3.0A@5V.
 
 
+
+    $ref: /schemas/types.yaml#/definitions/string
+    enum:
+      - not-supported
+      - default
+      - 1.5A
+      - 3.0A
Quoted 3 lines
Quoted 1 line

What happens if the property is not present?

The behavior would be the same as "not-supported".

Then you don't need 'not-supported'.

Quoted 4 lines
Quoted 3 lines

I'm not crazy about mixing strings and what could be a number.

Quoted 7 lines
Quoted 4 lines

v8 version[1] of the patch was using uint32. I moved to using strings as you were asking to unify with the approach in [2]. Since [3] was using string enums, I moved to that. I don't have a strong preference here, so I can move back to using u32 if you feel so.

Since the u32 values are based on the USB spec, I think I prefer that. Should be easier to deal with in the driver than doing strcmp's.

Done. Sent out as v11 of the patch.

Thanks,
Badhri

Quoted 2 lines

Rob

author maintainer
#20 lore raw

PATCH v9 6/15 usb: typec: tcpm: Add support for Sink Fast Role SWAP(FRS)

In reply to Badhri Jagan Sridharan

PD 3.0 spec defines a new mechanism for power role swap called Fast role swap. This change enables TCPM to support FRS when acting as sink.

Once the explicit contract is negotiated, sink port is expected to query the source port for sink caps to determine whether the source is FRS capable.
Bits 23 & 24 of fixed pdo of the sink caps from the source, when set, indicates the current needed by the source when fast role swap is in progress(Implicit contract phasae). 0 indicates that the source does not support Fast Role Swap.

Upon receiving the FRS signal from the source, TCPC(TCPM_FRS_EVENT) informs TCPM to start the Fast role swap sequence.

1. TCPM sends FRS PD message: FR_SWAP_SEND
2. If response is not received within the expiry of SenderResponseTimer, Error recovery is triggered.:
FR_SWAP_SEND_TIMEOUT
3. Upon receipt of the accept message, TCPM waits for PSSourceOffTimer for PS_READY message from the partner:
FR_SWAP_SNK_SRC_NEW_SINK_READY.

TCPC is expected to autonomously turn on vbus once the FRS signal is received and vbus voltage falls below vsafe5v within tSrcFrSwap. This is different from traditional power role swap where the vbus sourcing is turned on by TCPM.

4. By this time, TCPC most likely would have started to source vbus, TCPM waits for tSrcFrSwap to see if the lower level TCPC driver signals TCPM_SOURCING_VBUS event:
FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED.
5. When TCPC signals sourcing vbus, TCPM sends PS_READY msg and changes the CC pin from Rd to Rp. This is the end of fast role swap sequence and TCPM initiates the sequnce to negotiate explicit contract by transitioning into SRC_STARTUP after SwapSrcStart.

The code is written based on the sequence described in "Figure 8-107:
Dual-role Port in Sink to Source Fast Role Swap State Diagram" of USB Power Delivery Specification Revision 3.0, Version 1.2.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.
- Rebased on top of usb-next and resolved conflicts due to the below
changes:
3ed8e1c2ac99 usb: typec: tcpm: Migrate workqueue to RT priority for processing events 6bbe2a90a0bb usb: typec: tcpm: During PR_SWAP, source caps should be sent only after tSwapSourceStart
- enable_frs sequence is now run as part of the same kthread that runs
the state machines.
- Fixed the implicit fallthrough warning in the switch case for
FR_SWAP_CANCEL case.

Changes since v6:
- Moved frs_current from caps to tcpm_port as Heikki suggested.

Changes since v7:
- Added Reviewed-by: Heikki

Changes since v8:
- No change.

---
 drivers/usb/typec/tcpm/tcpm.c | 229 +++++++++++++++++++++++++++++++++-
 include/linux/usb/pd.h        |  19 +--
 include/linux/usb/tcpm.h      |   8 +-
 3 files changed, 244 insertions(+), 12 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +226 -3
index 92806547f485..55535c4f66bf 100644
@@ -106,6 +106,13 @@
 	S(VCONN_SWAP_TURN_ON_VCONN),		\
 	S(VCONN_SWAP_TURN_OFF_VCONN),		\
 						\
+	S(FR_SWAP_SEND),			\
+	S(FR_SWAP_SEND_TIMEOUT),		\
+	S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF),			\
+	S(FR_SWAP_SNK_SRC_NEW_SINK_READY),		\
+	S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED),	\
+	S(FR_SWAP_CANCEL),			\
+						\
 	S(SNK_TRY),				\
 	S(SNK_TRY_WAIT),			\
 	S(SNK_TRY_WAIT_DEBOUNCE),               \
@@ -127,6 +134,9 @@
 	S(GET_PPS_STATUS_SEND),			\
 	S(GET_PPS_STATUS_SEND_TIMEOUT),		\
 						\
+	S(GET_SINK_CAP),			\
+	S(GET_SINK_CAP_TIMEOUT),		\
+						\
 	S(ERROR_RECOVERY),			\
 	S(PORT_RESET),				\
 	S(PORT_RESET_WAIT_OFF)
@@ -170,11 +180,25 @@ enum adev_actions {
 	ADEV_ATTENTION,
 };
 
+/*
+ * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap.
+ * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
+ * Version 1.2"
+ */
+enum frs_typec_current {
+	FRS_NOT_SUPPORTED,
+	FRS_DEFAULT_POWER,
+	FRS_5V_1P5A,
+	FRS_5V_3A,
+};
+
 /* Events from low level driver */
 
 #define TCPM_CC_EVENT		BIT(0)
 #define TCPM_VBUS_EVENT		BIT(1)
 #define TCPM_RESET_EVENT	BIT(2)
+#define TCPM_FRS_EVENT		BIT(3)
+#define TCPM_SOURCING_VBUS	BIT(4)
 
 #define LOG_BUFFER_ENTRIES	1024
 #define LOG_BUFFER_ENTRY_SIZE	128
@@ -184,6 +208,8 @@ enum adev_actions {
 #define SVID_DISCOVERY_MAX	16
 #define ALTMODE_DISCOVERY_MAX	(SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
 
+#define GET_SINK_CAP_RETRY_MS	100
+
 struct pd_mode_data {
 	int svid_index;		/* current SVID index		*/
 	int nsvids;
@@ -261,6 +287,8 @@ struct tcpm_port {
 	struct kthread_work state_machine;
 	struct hrtimer vdm_state_machine_timer;
 	struct kthread_work vdm_state_machine;
+	struct hrtimer enable_frs_timer;
+	struct kthread_work enable_frs;
 	bool state_machine_running;
 
 	struct completion tx_complete;
@@ -335,6 +363,12 @@ struct tcpm_port {
 	/* port belongs to a self powered device */
 	bool self_powered;
 
+	/* FRS */
+	enum frs_typec_current frs_current;
+
+	/* Sink caps have been queried */
+	bool sink_cap_done;
+
 #ifdef CONFIG_DEBUG_FS
 	struct dentry *dentry;
 	struct mutex logbuffer_lock;	/* log buffer access lock */
@@ -940,6 +974,16 @@ static void mod_vdm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
 	}
 }
 
+static void mod_enable_frs_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
+{
+	if (delay_ms) {
+		hrtimer_start(&port->enable_frs_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
+	} else {
+		hrtimer_cancel(&port->enable_frs_timer);
+		kthread_queue_work(port->wq, &port->enable_frs);
+	}
+}
+
 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
 			   unsigned int delay_ms)
 {
@@ -1669,6 +1713,9 @@ static void tcpm_pd_data_request(struct tcpm_port *port,
 	unsigned int cnt = pd_header_cnt_le(msg->header);
 	unsigned int rev = pd_header_rev_le(msg->header);
 	unsigned int i;
+	enum frs_typec_current frs_current;
+	bool frs_enable;
+	int ret;
 
 	switch (type) {
 	case PD_DATA_SOURCE_CAP:
@@ -1738,7 +1785,21 @@ static void tcpm_pd_data_request(struct tcpm_port *port,
 		/* We don't do anything with this at the moment... */
 		for (i = 0; i < cnt; i++)
 			port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
+
+		frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >>
+			PDO_FIXED_FRS_CURR_SHIFT;
+		frs_enable = frs_current && (frs_current <= port->frs_current);
+		tcpm_log(port,
+			 "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c",
+			 frs_current, port->frs_current, frs_enable ? 'y' : 'n');
+		if (frs_enable) {
+			ret  = port->tcpc->enable_frs(port->tcpc, true);
+			tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret);
+		}
+
 		port->nr_sink_caps = cnt;
+		port->sink_cap_done = true;
+		tcpm_set_state(port, SNK_READY, 0);
 		break;
 	case PD_DATA_VENDOR_DEF:
 		tcpm_handle_vdm_request(port, msg->payload, cnt);
@@ -1833,6 +1894,9 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
 		case VCONN_SWAP_WAIT_FOR_VCONN:
 			tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
 			break;
+		case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
+			tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0);
+			break;
 		default:
 			break;
 		}
@@ -1872,6 +1936,13 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
 					     -EAGAIN : -EOPNOTSUPP);
 			tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
 			break;
+		case FR_SWAP_SEND:
+			tcpm_set_state(port, FR_SWAP_CANCEL, 0);
+			break;
+		case GET_SINK_CAP:
+			port->sink_cap_done = true;
+			tcpm_set_state(port, ready_state(port), 0);
+			break;
 		default:
 			break;
 		}
@@ -1906,6 +1977,9 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
 		case VCONN_SWAP_SEND:
 			tcpm_set_state(port, VCONN_SWAP_START, 0);
 			break;
+		case FR_SWAP_SEND:
+			tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0);
+			break;
 		default:
 			break;
 		}
@@ -2806,6 +2880,10 @@ static void tcpm_reset_port(struct tcpm_port *port)
 	port->try_src_count = 0;
 	port->try_snk_count = 0;
 	port->usb_type = POWER_SUPPLY_USB_TYPE_C;
+	port->nr_sink_caps = 0;
+	port->sink_cap_done = false;
+	if (port->tcpc->enable_frs)
+		port->tcpc->enable_frs(port->tcpc, false);
 
 	power_supply_changed(port->psy);
 }
@@ -3356,10 +3434,9 @@ static void run_state_machine(struct tcpm_port *port)
 		tcpm_swap_complete(port, 0);
 		tcpm_typec_connect(port);
 		tcpm_check_send_discover(port);
+		mod_enable_frs_delayed_work(port, 0);
 		tcpm_pps_complete(port, port->pps_status);
-
 		power_supply_changed(port->psy);
-
 		break;
 
 	/* Accessory states */
@@ -3383,9 +3460,13 @@ static void run_state_machine(struct tcpm_port *port)
 		tcpm_set_state(port, HARD_RESET_START, 0);
 		break;
 	case HARD_RESET_START:
+		port->sink_cap_done = false;
+		if (port->tcpc->enable_frs)
+			port->tcpc->enable_frs(port->tcpc, false);
 		port->hard_reset_count++;
 		port->tcpc->set_pd_rx(port->tcpc, false);
 		tcpm_unregister_altmodes(port);
+		port->nr_sink_caps = 0;
 		port->send_discover = true;
 		if (port->pwr_role == TYPEC_SOURCE)
 			tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
@@ -3517,6 +3598,35 @@ static void run_state_machine(struct tcpm_port *port)
 		tcpm_set_state(port, ready_state(port), 0);
 		break;
 
+	case FR_SWAP_SEND:
+		if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP)) {
+			tcpm_set_state(port, ERROR_RECOVERY, 0);
+			break;
+		}
+		tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE);
+		break;
+	case FR_SWAP_SEND_TIMEOUT:
+		tcpm_set_state(port, ERROR_RECOVERY, 0);
+		break;
+	case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
+		tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_OFF);
+		break;
+	case FR_SWAP_SNK_SRC_NEW_SINK_READY:
+		if (port->vbus_source)
+			tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
+		else
+			tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE);
+		break;
+	case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
+		tcpm_set_pwr_role(port, TYPEC_SOURCE);
+		if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
+			tcpm_set_state(port, ERROR_RECOVERY, 0);
+			break;
+		}
+		tcpm_set_cc(port, tcpm_rp_cc(port));
+		tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
+		break;
+
 	/* PR_Swap states */
 	case PR_SWAP_ACCEPT:
 		tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
@@ -3640,6 +3750,12 @@ static void run_state_machine(struct tcpm_port *port)
 		else
 			tcpm_set_state(port, SNK_READY, 0);
 		break;
+	case FR_SWAP_CANCEL:
+		if (port->pwr_role == TYPEC_SOURCE)
+			tcpm_set_state(port, SRC_READY, 0);
+		else
+			tcpm_set_state(port, SNK_READY, 0);
+		break;
 
 	case BIST_RX:
 		switch (BDO_MODE_MASK(port->bist_request)) {
@@ -3674,6 +3790,14 @@ static void run_state_machine(struct tcpm_port *port)
 	case GET_PPS_STATUS_SEND_TIMEOUT:
 		tcpm_set_state(port, ready_state(port), 0);
 		break;
+	case GET_SINK_CAP:
+		tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP);
+		tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE);
+		break;
+	case GET_SINK_CAP_TIMEOUT:
+		port->sink_cap_done = true;
+		tcpm_set_state(port, ready_state(port), 0);
+		break;
 	case ERROR_RECOVERY:
 		tcpm_swap_complete(port, -EPROTO);
 		tcpm_pps_complete(port, -EPROTO);
@@ -3889,6 +4013,13 @@ static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
 		 * Ignore it.
 		 */
 		break;
+	case FR_SWAP_SEND:
+	case FR_SWAP_SEND_TIMEOUT:
+	case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
+	case FR_SWAP_SNK_SRC_NEW_SINK_READY:
+	case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
+		/* Do nothing, CC change expected */
+		break;
 
 	case PORT_RESET:
 	case PORT_RESET_WAIT_OFF:
@@ -3959,6 +4090,9 @@ static void _tcpm_pd_vbus_on(struct tcpm_port *port)
 	case SRC_TRY_DEBOUNCE:
 		/* Do nothing, waiting for sink detection */
 		break;
+	case FR_SWAP_SNK_SRC_NEW_SINK_READY:
+		tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
+		break;
 
 	case PORT_RESET:
 	case PORT_RESET_WAIT_OFF:
@@ -4038,6 +4172,14 @@ static void _tcpm_pd_vbus_off(struct tcpm_port *port)
 		 */
 		break;
 
+	case FR_SWAP_SEND:
+	case FR_SWAP_SEND_TIMEOUT:
+	case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
+	case FR_SWAP_SNK_SRC_NEW_SINK_READY:
+	case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
+		/* Do nothing, vbus drop expected */
+		break;
+
 	default:
 		if (port->pwr_role == TYPEC_SINK &&
 		    port->attached)
@@ -4092,6 +4234,25 @@ static void tcpm_pd_event_handler(struct kthread_work *work)
 			if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
 				_tcpm_cc_change(port, cc1, cc2);
 		}
+		if (events & TCPM_FRS_EVENT) {
+			if (port->state == SNK_READY)
+				tcpm_set_state(port, FR_SWAP_SEND, 0);
+			else
+				tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready");
+		}
+		if (events & TCPM_SOURCING_VBUS) {
+			tcpm_log(port, "sourcing vbus");
+			/*
+			 * In fast role swap case TCPC autonomously sources vbus. Set vbus_source
+			 * true as TCPM wouldn't have called tcpm_set_vbus.
+			 *
+			 * When vbus is sourced on the command on TCPM i.e. TCPM called
+			 * tcpm_set_vbus to source vbus, vbus_source would already be true.
+			 */
+			port->vbus_source = true;
+			_tcpm_pd_vbus_on(port);
+		}
+
 		spin_lock(&port->pd_event_lock);
 	}
 	spin_unlock(&port->pd_event_lock);
@@ -4125,6 +4286,50 @@ void tcpm_pd_hard_reset(struct tcpm_port *port)
 }
 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
 
+void tcpm_sink_frs(struct tcpm_port *port)
+{
+	spin_lock(&port->pd_event_lock);
+	port->pd_events = TCPM_FRS_EVENT;
+	spin_unlock(&port->pd_event_lock);
+	kthread_queue_work(port->wq, &port->event_work);
+}
+EXPORT_SYMBOL_GPL(tcpm_sink_frs);
+
+void tcpm_sourcing_vbus(struct tcpm_port *port)
+{
+	spin_lock(&port->pd_event_lock);
+	port->pd_events = TCPM_SOURCING_VBUS;
+	spin_unlock(&port->pd_event_lock);
+	kthread_queue_work(port->wq, &port->event_work);
+}
+EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus);
+
+static void tcpm_enable_frs_work(struct kthread_work *work)
+{
+	struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs);
+
+	mutex_lock(&port->lock);
+	/* Not FRS capable */
+	if (!port->connected || port->port_type != TYPEC_PORT_DRP ||
+	    port->pwr_opmode != TYPEC_PWR_MODE_PD ||
+	    !port->tcpc->enable_frs ||
+	    /* Sink caps queried */
+	    port->sink_cap_done || port->negotiated_rev < PD_REV30)
+		goto unlock;
+
+	/* Send when the state machine is idle */
+	if (port->state != SNK_READY || port->vdm_state != VDM_STATE_DONE || port->send_discover)
+		goto resched;
+
+	tcpm_set_state(port, GET_SINK_CAP, 0);
+	port->sink_cap_done = true;
+
+resched:
+	mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS);
+unlock:
+	mutex_unlock(&port->lock);
+}
+
 static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
 {
 	struct tcpm_port *port = typec_get_drvdata(p);
@@ -4532,7 +4737,7 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
 {
 	const char *cap_str;
 	int ret;
-	u32 mw;
+	u32 mw, frs_current;
 
 	if (!fwnode)
 		return -EINVAL;
@@ -4601,6 +4806,13 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
 
 	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) {
+		ret = fwnode_property_read_u32(fwnode, "frs-typec-current", &frs_current);
+		if (ret >= 0 && frs_current <= FRS_5V_3A)
+			port->frs_current = frs_current;
+	}
+
 	return 0;
 }
 
@@ -4845,6 +5057,14 @@ static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *time
 	return HRTIMER_NORESTART;
 }
 
+static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer)
+{
+	struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer);
+
+	kthread_queue_work(port->wq, &port->enable_frs);
+	return HRTIMER_NORESTART;
+}
+
 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 {
 	struct tcpm_port *port;
@@ -4874,10 +5094,13 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 	kthread_init_work(&port->state_machine, tcpm_state_machine_work);
 	kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work);
 	kthread_init_work(&port->event_work, tcpm_pd_event_handler);
+	kthread_init_work(&port->enable_frs, tcpm_enable_frs_work);
 	hrtimer_init(&port->state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 	port->state_machine_timer.function = state_machine_timer_handler;
 	hrtimer_init(&port->vdm_state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 	port->vdm_state_machine_timer.function = vdm_state_machine_timer_handler;
+	hrtimer_init(&port->enable_frs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	port->enable_frs_timer.function = enable_frs_timer_handler;
 
 	spin_lock_init(&port->pd_event_lock);
 
include/linux/usb/pd.h +11 -8
index f842e4589bd2..3a805e2ecbc9 100644
@@ -219,14 +219,16 @@ enum pd_pdo_type {
 #define PDO_CURR_MASK		0x3ff
 #define PDO_PWR_MASK		0x3ff
 
-#define PDO_FIXED_DUAL_ROLE	BIT(29)	/* Power role swap supported */
-#define PDO_FIXED_SUSPEND	BIT(28) /* USB Suspend supported (Source) */
-#define PDO_FIXED_HIGHER_CAP	BIT(28) /* Requires more than vSafe5V (Sink) */
-#define PDO_FIXED_EXTPOWER	BIT(27) /* Externally powered */
-#define PDO_FIXED_USB_COMM	BIT(26) /* USB communications capable */
-#define PDO_FIXED_DATA_SWAP	BIT(25) /* Data role swap supported */
-#define PDO_FIXED_VOLT_SHIFT	10	/* 50mV units */
-#define PDO_FIXED_CURR_SHIFT	0	/* 10mA units */
+#define PDO_FIXED_DUAL_ROLE		BIT(29)	/* Power role swap supported */
+#define PDO_FIXED_SUSPEND		BIT(28) /* USB Suspend supported (Source) */
+#define PDO_FIXED_HIGHER_CAP		BIT(28) /* Requires more than vSafe5V (Sink) */
+#define PDO_FIXED_EXTPOWER		BIT(27) /* Externally powered */
+#define PDO_FIXED_USB_COMM		BIT(26) /* USB communications capable */
+#define PDO_FIXED_DATA_SWAP		BIT(25) /* Data role swap supported */
+#define PDO_FIXED_FRS_CURR_MASK		(BIT(24) | BIT(23)) /* FR_Swap Current (Sink) */
+#define PDO_FIXED_FRS_CURR_SHIFT	23
+#define PDO_FIXED_VOLT_SHIFT		10	/* 50mV units */
+#define PDO_FIXED_CURR_SHIFT		0	/* 10mA units */
 
 #define PDO_FIXED_VOLT(mv)	((((mv) / 50) & PDO_VOLT_MASK) << PDO_FIXED_VOLT_SHIFT)
 #define PDO_FIXED_CURR(ma)	((((ma) / 10) & PDO_CURR_MASK) << PDO_FIXED_CURR_SHIFT)
@@ -454,6 +456,7 @@ static inline unsigned int rdo_max_power(u32 rdo)
 #define PD_T_DB_DETECT		10000	/* 10 - 15 seconds */
 #define PD_T_SEND_SOURCE_CAP	150	/* 100 - 200 ms */
 #define PD_T_SENDER_RESPONSE	60	/* 24 - 30 ms, relaxed */
+#define PD_T_RECEIVER_RESPONSE	15	/* 15ms max */
 #define PD_T_SOURCE_ACTIVITY	45
 #define PD_T_SINK_ACTIVITY	135
 #define PD_T_SINK_WAIT_CAP	240
include/linux/usb/tcpm.h +7 -1
index 89f58760cf48..09762d26fa0c 100644
@@ -78,8 +78,11 @@ enum tcpm_transmit_type {
  *		automatically if a connection is established.
  * @try_role:	Optional; called to set a preferred role
  * @pd_transmit:Called to transmit PD message
- * @mux:	Pointer to multiplexer data
  * @set_bist_data: Turn on/off bist data mode for compliance testing
+ * @enable_frs:
+ *		Optional; Called to enable/disable PD 3.0 fast role swap.
+ *		Enabling frs is accessory dependent as not all PD3.0
+ *		accessories support fast role swap.
  */
 struct tcpc_dev {
 	struct fwnode_handle *fwnode;
@@ -105,6 +108,7 @@ struct tcpc_dev {
 	int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
 			   const struct pd_message *msg);
 	int (*set_bist_data)(struct tcpc_dev *dev, bool on);
+	int (*enable_frs)(struct tcpc_dev *dev, bool enable);
 };
 
 struct tcpm_port;
@@ -114,6 +118,8 @@ void tcpm_unregister_port(struct tcpm_port *port);
 
 void tcpm_vbus_change(struct tcpm_port *port);
 void tcpm_cc_change(struct tcpm_port *port);
+void tcpm_sink_frs(struct tcpm_port *port);
+void tcpm_sourcing_vbus(struct tcpm_port *port);
 void tcpm_pd_receive(struct tcpm_port *port,
 		     const struct pd_message *msg);
 void tcpm_pd_transmit_complete(struct tcpm_port *port,
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#21 lore raw

PATCH v9 7/15 usb: typec: tcpci: Implement callbacks for FRS

In reply to Badhri Jagan Sridharan

Implement tcpc.enable_frs to enable TCPC to receive Fast role swap signal.

Additionally set the sink disconnect threshold to 4v to prevent disconnect during Fast Role swap.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Rebase on usb-next.
- Fixed formatting error.
- Added Reviewed-by: Heikki.

Changes since v7:
- Rebase on usb-next.

Changes since v8:
- None.

---
 drivers/usb/typec/tcpm/tcpci.c | 17 +++++++++++++++++
 drivers/usb/typec/tcpm/tcpci.h |  8 ++++++++
 2 files changed, 25 insertions(+)
drivers/usb/typec/tcpm/tcpci.c +17 -0
index d6a6fac82d48..f9f0af64da5f 100644
@@ -268,6 +268,22 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
 				enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
 }
 
+static int tcpci_enable_frs(struct tcpc_dev *dev, bool enable)
+{
+	struct tcpci *tcpci = tcpc_to_tcpci(dev);
+	int ret;
+
+	/* To prevent disconnect during FRS, set disconnect threshold to 3.5V */
+	ret = tcpci_write16(tcpci, TCPC_VBUS_SINK_DISCONNECT_THRESH, enable ? 0 : 0x8c);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL, TCPC_FAST_ROLE_SWAP_EN, enable ?
+				 TCPC_FAST_ROLE_SWAP_EN : 0);
+
+	return ret;
+}
+
 static int tcpci_set_bist_data(struct tcpc_dev *tcpc, bool enable)
 {
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
@@ -611,6 +627,7 @@ struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data)
 	tcpci->tcpc.set_roles = tcpci_set_roles;
 	tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
 	tcpci->tcpc.set_bist_data = tcpci_set_bist_data;
+	tcpci->tcpc.enable_frs = tcpci_enable_frs;
 
 	err = tcpci_parse_config(tcpci);
 	if (err < 0)
drivers/usb/typec/tcpm/tcpci.h +8 -0
index 82f021a82456..5ef07a56d67a 100644
@@ -16,6 +16,7 @@
 #define TCPC_PD_INT_REV			0xa
 
 #define TCPC_ALERT			0x10
+#define TCPC_ALERT_EXTND		BIT(14)
 #define TCPC_ALERT_EXTENDED_STATUS	BIT(13)
 #define TCPC_ALERT_VBUS_DISCNCT		BIT(11)
 #define TCPC_ALERT_RX_BUF_OVF		BIT(10)
@@ -37,6 +38,9 @@
 #define TCPC_EXTENDED_STATUS_MASK		0x16
 #define TCPC_EXTENDED_STATUS_MASK_VSAFE0V	BIT(0)
 
+#define TCPC_ALERT_EXTENDED_MASK	0x17
+#define TCPC_SINK_FAST_ROLE_SWAP	BIT(0)
+
 #define TCPC_CONFIG_STD_OUTPUT		0x18
 
 #define TCPC_TCPC_CTRL			0x19
@@ -63,6 +67,7 @@
 
 #define TCPC_POWER_CTRL			0x1c
 #define TCPC_POWER_CTRL_VCONN_ENABLE	BIT(0)
+#define TCPC_FAST_ROLE_SWAP_EN		BIT(7)
 
 #define TCPC_CC_STATUS			0x1d
 #define TCPC_CC_STATUS_TOGGLING		BIT(5)
@@ -74,11 +79,14 @@
 
 #define TCPC_POWER_STATUS		0x1e
 #define TCPC_POWER_STATUS_UNINIT	BIT(6)
+#define TCPC_POWER_STATUS_SOURCING_VBUS	BIT(4)
 #define TCPC_POWER_STATUS_VBUS_DET	BIT(3)
 #define TCPC_POWER_STATUS_VBUS_PRES	BIT(2)
 
 #define TCPC_FAULT_STATUS		0x1f
 
+#define TCPC_ALERT_EXTENDED		0x21
+
 #define TCPC_COMMAND			0x23
 #define TCPC_CMD_WAKE_I2C		0x11
 #define TCPC_CMD_DISABLE_VBUS_DETECT	0x22
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#22 lore raw

PATCH v9 8/15 usb: typec: tcpci_maxim: Add support for Sink FRS

In reply to Badhri Jagan Sridharan

Upon receiving ALERT_EXTENDED.TCPC_SINK_FAST_ROLE_SWAP signal tcpm to start Sink fast role swap signal.

Inform when TCPM is sourcing vbus.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- rebase on usb-next
- Added Reviewed-by: Heikki

Changes since v7:
- Rebase on usb-next

Changes since v8:
- None.

---
 drivers/usb/typec/tcpm/tcpci_maxim.c | 50 +++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/tcpci_maxim.c +46 -4
index 91337ddb4962..723d7dd38f75 100644
@@ -106,13 +106,22 @@ static void max_tcpci_init_regs(struct max_tcpci_chip *chip)
 		return;
 	}
 
+	ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, 0xff);
+	if (ret < 0) {
+		dev_err(chip->dev, "Unable to clear TCPC_ALERT_EXTENDED ret:%d\n", ret);
+		return;
+	}
+
 	alert_mask = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_TX_FAILED |
 		TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_RX_STATUS | TCPC_ALERT_CC_STATUS |
-		TCPC_ALERT_VBUS_DISCNCT | TCPC_ALERT_RX_BUF_OVF | TCPC_ALERT_POWER_STATUS;
+		TCPC_ALERT_VBUS_DISCNCT | TCPC_ALERT_RX_BUF_OVF | TCPC_ALERT_POWER_STATUS |
+		/* Enable Extended alert for detecting Fast Role Swap Signal */
+		TCPC_ALERT_EXTND;
 
 	ret = max_tcpci_write16(chip, TCPC_ALERT_MASK, alert_mask);
 	if (ret < 0) {
-		dev_err(chip->dev, "Error writing to TCPC_ALERT_MASK ret:%d\n", ret);
+		dev_err(chip->dev,
+			"Error enabling TCPC_ALERT: TCPC_ALERT_MASK write failed ret:%d\n", ret);
 		return;
 	}
 
@@ -122,6 +131,10 @@ static void max_tcpci_init_regs(struct max_tcpci_chip *chip)
 		dev_err(chip->dev, "Error writing to TCPC_POWER_CTRL ret:%d\n", ret);
 		return;
 	}
+
+	ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED_MASK, TCPC_SINK_FAST_ROLE_SWAP);
+	if (ret < 0)
+		return;
 }
 
 static void process_rx(struct max_tcpci_chip *chip, u16 status)
@@ -225,10 +238,23 @@ static void process_power_status(struct max_tcpci_chip *chip)
 	if (ret < 0)
 		return;
 
-	if (pwr_status == 0xff)
+	if (pwr_status == 0xff) {
 		max_tcpci_init_regs(chip);
-	else
+	} else if (pwr_status & TCPC_POWER_STATUS_SOURCING_VBUS) {
+		tcpm_sourcing_vbus(chip->port);
+		/*
+		 * Alawys re-enable boost here.
+		 * In normal case, when say an headset is attached, TCPM would
+		 * have instructed to TCPC to enable boost, so the call is a
+		 * no-op.
+		 * But for Fast Role Swap case, Boost turns on autonomously without
+		 * AP intervention, but, needs AP to enable source mode explicitly
+		 * for AP to regain control.
+		 */
+		max_tcpci_set_vbus(chip->tcpci, &chip->data, true, false);
+	} else {
 		tcpm_vbus_change(chip->port);
+	}
 }
 
 static void process_tx(struct max_tcpci_chip *chip, u16 status)
@@ -249,6 +275,7 @@ static irqreturn_t _max_tcpci_irq(struct max_tcpci_chip *chip, u16 status)
 {
 	u16 mask;
 	int ret;
+	u8 reg_status;
 
 	/*
 	 * Clear alert status for everything except RX_STATUS, which shouldn't
@@ -274,6 +301,21 @@ static irqreturn_t _max_tcpci_irq(struct max_tcpci_chip *chip, u16 status)
 		}
 	}
 
+	if (status & TCPC_ALERT_EXTND) {
+		ret = max_tcpci_read8(chip, TCPC_ALERT_EXTENDED, &reg_status);
+		if (ret < 0)
+			return ret;
+
+		ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, reg_status);
+		if (ret < 0)
+			return ret;
+
+		if (reg_status & TCPC_SINK_FAST_ROLE_SWAP) {
+			dev_info(chip->dev, "FRS Signal");
+			tcpm_sink_frs(chip->port);
+		}
+	}
+
 	if (status & TCPC_ALERT_RX_STATUS)
 		process_rx(chip, status);
 
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#23 lore raw

PATCH v9 9/15 usb: typec: tcpm: frs sourcing vbus callback

In reply to Badhri Jagan Sridharan

During FRS hardware autonomously starts to source vbus. Provide callback to perform chip specific operations.

---
 drivers/usb/typec/tcpm/tcpm.c | 9 +++++++++
 include/linux/usb/tcpm.h      | 4 ++++
 2 files changed, 13 insertions(+)
drivers/usb/typec/tcpm/tcpm.c +9 -0
index 55535c4f66bf..02b7f623f584 100644
@@ -4090,7 +4090,16 @@ static void _tcpm_pd_vbus_on(struct tcpm_port *port)
 	case SRC_TRY_DEBOUNCE:
 		/* Do nothing, waiting for sink detection */
 		break;
+	case FR_SWAP_SEND:
+	case FR_SWAP_SEND_TIMEOUT:
+	case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
+	case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
+		if (port->tcpc->frs_sourcing_vbus)
+			port->tcpc->frs_sourcing_vbus(port->tcpc);
+		break;
 	case FR_SWAP_SNK_SRC_NEW_SINK_READY:
+		if (port->tcpc->frs_sourcing_vbus)
+			port->tcpc->frs_sourcing_vbus(port->tcpc);
 		tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
 		break;
 
include/linux/usb/tcpm.h +4 -0
index 09762d26fa0c..7303f518ba49 100644
@@ -83,6 +83,9 @@ enum tcpm_transmit_type {
  *		Optional; Called to enable/disable PD 3.0 fast role swap.
  *		Enabling frs is accessory dependent as not all PD3.0
  *		accessories support fast role swap.
+ * @frs_sourcing_vbus:
+ *		Optional; Called to notify that vbus is now being sourced.
+ *		Low level drivers can perform chip specific operations, if any.
  */
 struct tcpc_dev {
 	struct fwnode_handle *fwnode;
@@ -109,6 +112,7 @@ struct tcpc_dev {
 			   const struct pd_message *msg);
 	int (*set_bist_data)(struct tcpc_dev *dev, bool on);
 	int (*enable_frs)(struct tcpc_dev *dev, bool enable);
+	void (*frs_sourcing_vbus)(struct tcpc_dev *dev);
 };
 
 struct tcpm_port;
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#24 lore raw

PATCH v9 10/15 usb: typec: tcpci: frs sourcing vbus callback

In reply to Badhri Jagan Sridharan

During FRS hardware autonomously starts to source vbus. Provide callback to perform chip specific operations.

---
v9 is the first version of this patch in the series. Added to fix occasional bug of vbus turning back on when disconnecting the FRS accessory after disconnect.

---
 drivers/usb/typec/tcpm/tcpci.c | 9 +++++++++
 drivers/usb/typec/tcpm/tcpci.h | 4 ++++
 2 files changed, 13 insertions(+)
drivers/usb/typec/tcpm/tcpci.c +9 -0
index f9f0af64da5f..f91688e43991 100644
@@ -284,6 +284,14 @@ static int tcpci_enable_frs(struct tcpc_dev *dev, bool enable)
 	return ret;
 }
 
+static void tcpci_frs_sourcing_vbus(struct tcpc_dev *dev)
+{
+	struct tcpci *tcpci = tcpc_to_tcpci(dev);
+
+	if (tcpci->data->frs_sourcing_vbus)
+		tcpci->data->frs_sourcing_vbus(tcpci, tcpci->data);
+}
+
 static int tcpci_set_bist_data(struct tcpc_dev *tcpc, bool enable)
 {
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
@@ -628,6 +636,7 @@ struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data)
 	tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
 	tcpci->tcpc.set_bist_data = tcpci_set_bist_data;
 	tcpci->tcpc.enable_frs = tcpci_enable_frs;
+	tcpci->tcpc.frs_sourcing_vbus = tcpci_frs_sourcing_vbus;
 
 	err = tcpci_parse_config(tcpci);
 	if (err < 0)
drivers/usb/typec/tcpm/tcpci.h +4 -0
index 5ef07a56d67a..b418fe11b527 100644
@@ -143,6 +143,9 @@
 /*
  * @TX_BUF_BYTE_x_hidden
  *		optional; Set when TX_BUF_BYTE_x can only be accessed through I2C_WRITE_BYTE_COUNT.
+ * @frs_sourcing_vbus:
+ *		Optional; Callback to perform chip specific operations when FRS
+ *		is sourcing vbus.
  */
 struct tcpci;
 struct tcpci_data {
@@ -154,6 +157,7 @@ struct tcpci_data {
 	int (*start_drp_toggling)(struct tcpci *tcpci, struct tcpci_data *data,
 				  enum typec_cc_status cc);
 	int (*set_vbus)(struct tcpci *tcpci, struct tcpci_data *data, bool source, bool sink);
+	void (*frs_sourcing_vbus)(struct tcpci *tcpci, struct tcpci_data *data);
 };
 
 struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data);
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#25 lore raw

PATCH v9 11/15 usb: typec: tcpci_max77759: Fix vbus stuck on upon diconnecting sink

In reply to Badhri Jagan Sridharan

Occasionally, POWER_STATUS.sourcing_vbus takes a while to clear after writing to MAX_BUCK_BOOST_OP register. This causes vbus to turn back on while disconnecting the sink. Overcome this issue by writing into MAX_BUCK_BOOST_OP during frs while sourcing vbu, instead of always into the register whenever POWER_STATUS.sourcing_vbus is set.

---
v9 is the first version of this patch. Added to fix
occasional bug of vbus turning back on when disconnecting the FRS accessory after disconnect.

---
 drivers/usb/typec/tcpm/tcpci_maxim.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/usb/typec/tcpm/tcpci_maxim.c +14 -14
index 723d7dd38f75..43dcad95e897 100644
@@ -238,23 +238,22 @@ static void process_power_status(struct max_tcpci_chip *chip)
 	if (ret < 0)
 		return;
 
-	if (pwr_status == 0xff) {
+	if (pwr_status == 0xff)
 		max_tcpci_init_regs(chip);
-	} else if (pwr_status & TCPC_POWER_STATUS_SOURCING_VBUS) {
+	else if (pwr_status & TCPC_POWER_STATUS_SOURCING_VBUS)
 		tcpm_sourcing_vbus(chip->port);
-		/*
-		 * Alawys re-enable boost here.
-		 * In normal case, when say an headset is attached, TCPM would
-		 * have instructed to TCPC to enable boost, so the call is a
-		 * no-op.
-		 * But for Fast Role Swap case, Boost turns on autonomously without
-		 * AP intervention, but, needs AP to enable source mode explicitly
-		 * for AP to regain control.
-		 */
-		max_tcpci_set_vbus(chip->tcpci, &chip->data, true, false);
-	} else {
+	else
 		tcpm_vbus_change(chip->port);
-	}
+}
+
+static void max_tcpci_frs_sourcing_vbus(struct tcpci *tcpci, struct tcpci_data *tdata)
+{
+	/*
+	 * For Fast Role Swap case, Boost turns on autonomously without
+	 * AP intervention, but, needs AP to enable source mode explicitly
+	 * for AP to regain control.
+	 */
+	max_tcpci_set_vbus(tcpci, tdata, true, false);
 }
 
 static void process_tx(struct max_tcpci_chip *chip, u16 status)
@@ -441,6 +440,7 @@ static int max_tcpci_probe(struct i2c_client *client, const struct i2c_device_id
 	chip->data.start_drp_toggling = max_tcpci_start_toggling;
 	chip->data.TX_BUF_BYTE_x_hidden = true;
 	chip->data.init = tcpci_init;
+	chip->data.frs_sourcing_vbus = max_tcpci_frs_sourcing_vbus;
 
 	max_tcpci_init_regs(chip);
 	chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#26 lore raw

PATCH v9 12/15 usb: typec: tcpm: Parse frs type-c current from device tree

In reply to Badhri Jagan Sridharan

New source's current capability is now defined as string based device tree property through new-source-frs-typec-current. Refactor tcpm code to parse new-source-frs-typec-current and infer local port's new source current capability during frs.

---
v9 is the first version of this patch in this series to rebase TCPM code to read new source frs current from new-source-frs-typec-current.

---
 drivers/usb/typec/tcpm/tcpm.c | 41 +++++++++++++++++++----------------
 include/linux/usb/typec.h     | 12 ++++++++++
 2 files changed, 34 insertions(+), 19 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +22 -19
index 02b7f623f584..d5a3e2b3bea2 100644
@@ -180,16 +180,11 @@ enum adev_actions {
 	ADEV_ATTENTION,
 };
 
-/*
- * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap.
- * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
- * Version 1.2"
- */
-enum frs_typec_current {
-	FRS_NOT_SUPPORTED,
-	FRS_DEFAULT_POWER,
-	FRS_5V_1P5A,
-	FRS_5V_3A,
+static const char * const typec_new_source_frs_current[] = {
+	[FRS_NOT_SUPPORTED]	= "not-supported",
+	[FRS_DEFAULT_POWER]	= "default",
+	[FRS_5V_1P5A]		= "1.5A",
+	[FRS_5V_3A]		= "3.0A",
 };
 
 /* Events from low level driver */
@@ -364,7 +359,7 @@ struct tcpm_port {
 	bool self_powered;
 
 	/* FRS */
-	enum frs_typec_current frs_current;
+	enum typec_new_source_frs_current new_source_frs_current;
 
 	/* Sink caps have been queried */
 	bool sink_cap_done;
@@ -1713,7 +1708,7 @@ static void tcpm_pd_data_request(struct tcpm_port *port,
 	unsigned int cnt = pd_header_cnt_le(msg->header);
 	unsigned int rev = pd_header_rev_le(msg->header);
 	unsigned int i;
-	enum frs_typec_current frs_current;
+	enum typec_new_source_frs_current partner_frs_current;
 	bool frs_enable;
 	int ret;
 
@@ -1786,12 +1781,13 @@ static void tcpm_pd_data_request(struct tcpm_port *port,
 		for (i = 0; i < cnt; i++)
 			port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
 
-		frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >>
+		partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >>
 			PDO_FIXED_FRS_CURR_SHIFT;
-		frs_enable = frs_current && (frs_current <= port->frs_current);
+		frs_enable = partner_frs_current && (partner_frs_current <=
+						     port->new_source_frs_current);
 		tcpm_log(port,
 			 "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c",
-			 frs_current, port->frs_current, frs_enable ? 'y' : 'n');
+			 partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n');
 		if (frs_enable) {
 			ret  = port->tcpc->enable_frs(port->tcpc, true);
 			tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret);
@@ -4746,7 +4742,8 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
 {
 	const char *cap_str;
 	int ret;
-	u32 mw, frs_current;
+	u32 mw;
+	const char *new_source_frs_current;
 
 	if (!fwnode)
 		return -EINVAL;
@@ -4817,9 +4814,15 @@ static int tcpm_fw_get_caps(struct tcpm_port *port,
 
 	/* FRS can only be supported byb DRP ports */
 	if (port->port_type == TYPEC_PORT_DRP) {
-		ret = fwnode_property_read_u32(fwnode, "frs-typec-current", &frs_current);
-		if (ret >= 0 && frs_current <= FRS_5V_3A)
-			port->frs_current = frs_current;
+		ret = fwnode_property_read_string(fwnode, "new-source-frs-typec-current",
+						  &new_source_frs_current);
+		if (!ret) {
+			ret = match_string(typec_new_source_frs_current,
+					   ARRAY_SIZE(typec_new_source_frs_current),
+					   new_source_frs_current);
+			if (ret >= 0)
+				port->new_source_frs_current = ret;
+		}
 	}
 
 	return 0;
include/linux/usb/typec.h +12 -0
index 9cb1bec94b71..76bb078a433c 100644
@@ -72,6 +72,18 @@ enum typec_orientation {
 	TYPEC_ORIENTATION_REVERSE,
 };
 
+/*
+ * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
+ * Version 1.2"
+ * Initial current capability of the new source when vSafe5V is applied.
+ */
+enum typec_new_source_frs_current {
+	FRS_NOT_SUPPORTED,
+	FRS_DEFAULT_POWER,
+	FRS_5V_1P5A,
+	FRS_5V_3A,
+};
+
 /*
  * struct enter_usb_data - Enter_USB Message details
  * @eudo: Enter_USB Data Object
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#27 lore raw

PATCH v9 13/15 usb: typec: tcpm: Implement enabling Auto Discharge disconnect support

In reply to Badhri Jagan Sridharan

TCPCI spec allows TCPC hardware to autonomously discharge the vbus capacitance upon disconnect. The expectation is that the TCPM enables AutoDischargeDisconnect while entering SNK/SRC_ATTACHED states. Hardware then automously discharges vbus when the vbus falls below a certain threshold i.e. VBUS_SINK_DISCONNECT_THRESHOLD.

Apart from enabling the vbus discharge circuit, AutoDischargeDisconnect is also used a flag to move TCPCI based TCPC implementations into Attached.Snk/Attached.Src state as mentioned in
Figure 4-15. TCPC State Diagram before a Connection of the USB Type-C Port Controller Interface Specification.
In such TCPC implementations, setting AutoDischargeDisconnect would prevent TCPC into entering "Connection_Invalid" state as well.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Fixed incorrect data_role error that I introduced by mistake in the previous version.

Changes since v7:
- Rebase on usb-next

Changes since v8:
- Removing the call to tcpm_set_auto_vbus_discharge_threshold in the source path.

---
 drivers/usb/typec/tcpm/tcpm.c | 60 ++++++++++++++++++++++++++++++++---
 include/linux/usb/tcpm.h      | 15 +++++++++
 2 files changed, 71 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +56 -4
index d5a3e2b3bea2..51a14d282109 100644
@@ -1701,6 +1701,24 @@ static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
 	}
 }
 
+static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port,
+						  enum typec_pwr_opmode mode, bool pps_active,
+						  u32 requested_vbus_voltage)
+{
+	int ret;
+
+	if (!port->tcpc->set_auto_vbus_discharge_threshold)
+		return 0;
+
+	ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active,
+							    requested_vbus_voltage);
+	tcpm_log_force(port,
+		       "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u ret:%d",
+		       mode, pps_active ? 'y' : 'n', requested_vbus_voltage, ret);
+
+	return ret;
+}
+
 static void tcpm_pd_data_request(struct tcpm_port *port,
 				 const struct pd_message *msg)
 {
@@ -1871,6 +1889,10 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
 						       port->current_limit,
 						       port->supply_voltage);
 				port->explicit_contract = true;
+				tcpm_set_auto_vbus_discharge_threshold(port,
+								       TYPEC_PWR_MODE_PD,
+								       port->pps_data.active,
+								       port->supply_voltage);
 				tcpm_set_state(port, SNK_READY, 0);
 			} else {
 				/*
@@ -2785,8 +2807,12 @@ static int tcpm_src_attach(struct tcpm_port *port)
 	if (ret < 0)
 		return ret;
 
-	ret = tcpm_set_roles(port, true, TYPEC_SOURCE,
-			     tcpm_data_role_for_source(port));
+	if (port->tcpc->enable_auto_vbus_discharge) {
+		ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, true);
+		tcpm_log_force(port, "enable vbus discharge ret:%d", ret);
+	}
+
+	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port));
 	if (ret < 0)
 		return ret;
 
@@ -2853,6 +2879,12 @@ static void tcpm_unregister_altmodes(struct tcpm_port *port)
 
 static void tcpm_reset_port(struct tcpm_port *port)
 {
+	int ret;
+
+	if (port->tcpc->enable_auto_vbus_discharge) {
+		ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, false);
+		tcpm_log_force(port, "Disable vbus discharge ret:%d", ret);
+	}
 	tcpm_unregister_altmodes(port);
 	tcpm_typec_disconnect(port);
 	port->attached = false;
@@ -2917,8 +2949,13 @@ static int tcpm_snk_attach(struct tcpm_port *port)
 	if (ret < 0)
 		return ret;
 
-	ret = tcpm_set_roles(port, true, TYPEC_SINK,
-			     tcpm_data_role_for_sink(port));
+	if (port->tcpc->enable_auto_vbus_discharge) {
+		tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
+		ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, true);
+		tcpm_log_force(port, "enable vbus discharge ret:%d", ret);
+	}
+
+	ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port));
 	if (ret < 0)
 		return ret;
 
@@ -3502,6 +3539,8 @@ static void run_state_machine(struct tcpm_port *port)
 		tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
 		break;
 	case SNK_HARD_RESET_SINK_OFF:
+		/* Do not discharge/disconnect during hard reseet */
+		tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
 		memset(&port->pps_data, 0, sizeof(port->pps_data));
 		tcpm_set_vconn(port, false);
 		if (port->pd_capable)
@@ -3544,6 +3583,7 @@ static void run_state_machine(struct tcpm_port *port)
 			tcpm_set_charge(port, true);
 		}
 		tcpm_set_attached_state(port, true);
+		tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
 		tcpm_set_state(port, SNK_STARTUP, 0);
 		break;
 
@@ -3645,6 +3685,10 @@ static void run_state_machine(struct tcpm_port *port)
 			tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
 		break;
 	case PR_SWAP_SRC_SNK_TRANSITION_OFF:
+		/*
+		 * Prevent vbus discharge circuit from turning on during PR_SWAP
+		 * as this is not a disconnect.
+		 */
 		tcpm_set_vbus(port, false);
 		port->explicit_contract = false;
 		/* allow time for Vbus discharge, must be < tSrcSwapStdby */
@@ -3673,9 +3717,17 @@ static void run_state_machine(struct tcpm_port *port)
 		tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
 		break;
 	case PR_SWAP_SRC_SNK_SINK_ON:
+		/* Set the vbus disconnect threshold for implicit contract */
+		tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
 		tcpm_set_state(port, SNK_STARTUP, 0);
 		break;
 	case PR_SWAP_SNK_SRC_SINK_OFF:
+		/*
+		 * Prevent vbus discharge circuit from turning on during PR_SWAP
+		 * as this is not a disconnect.
+		 */
+		tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB,
+						       port->pps_data.active, 0);
 		tcpm_set_charge(port, false);
 		tcpm_set_state(port, hard_reset_state(port),
 			       PD_T_PS_SOURCE_OFF);
include/linux/usb/tcpm.h +15 -0
index 7303f518ba49..e68aaa12886f 100644
@@ -86,6 +86,18 @@ enum tcpm_transmit_type {
  * @frs_sourcing_vbus:
  *		Optional; Called to notify that vbus is now being sourced.
  *		Low level drivers can perform chip specific operations, if any.
+ * @enable_auto_vbus_discharge:
+ *		Optional; TCPCI spec based TCPC implementations can optionally
+ *		support hardware to autonomously dischrge vbus upon disconnecting
+ *		as sink or source. TCPM signals TCPC to enable the mechanism upon
+ *		entering connected state and signals disabling upon disconnect.
+ * @set_auto_vbus_discharge_threshold:
+ *		Mandatory when enable_auto_vbus_discharge is implemented. TCPM
+ *		calls this function to allow lower levels drivers to program the
+ *		vbus threshold voltage below which the vbus discharge circuit
+ *		will be turned on. requested_vbus_voltage is set to 0 when vbus
+ *		is going to disappear knowingly i.e. during PR_SWAP and
+ *		HARD_RESET etc.
  */
 struct tcpc_dev {
 	struct fwnode_handle *fwnode;
@@ -113,6 +125,9 @@ struct tcpc_dev {
 	int (*set_bist_data)(struct tcpc_dev *dev, bool on);
 	int (*enable_frs)(struct tcpc_dev *dev, bool enable);
 	void (*frs_sourcing_vbus)(struct tcpc_dev *dev);
+	int (*enable_auto_vbus_discharge)(struct tcpc_dev *dev, bool enable);
+	int (*set_auto_vbus_discharge_threshold)(struct tcpc_dev *dev, enum typec_pwr_opmode mode,
+						 bool pps_active, u32 requested_vbus_voltage);
 };
 
 struct tcpm_port;
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#28 lore raw

PATCH v9 14/15 usb: typec: tcpci: Implement Auto discharge disconnect callbacks

In reply to Badhri Jagan Sridharan

vImplement callbacks for enabling/disabling
POWER_CONTROL.AutoDischargeDisconnect.

Programs VBUS_SINK_DISCONNECT_THRESHOLD based on the voltage requested as sink, mode of operation.

The programmed threshold is based on vSinkDisconnect and vSinkDisconnectPD values.

Add auto_discharge_disconnect to tdata to allow TCPC chip level drivers enable AutoDischargeDisconnect.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Rebase on usb-next.

Changes since v7:
Heikki's suggestion:
- Moved the actual write to TCPC_VBUS_SINK_DISCONNECT_THRESH as it's common to all chip drivers.
- Renaming the tcpci_data callback as get_auto_vbus_discharge_threshold

Changes since v8:
- Removed get_auto_vbus_discharge_threshold callback and moved the logic to program the default threshold for TCPC_VBUS_SINK_DISCONNECT_THRESH into the TCPCI code.

---
 drivers/usb/typec/tcpm/tcpci.c | 63 +++++++++++++++++++++++++++++++++-
 drivers/usb/typec/tcpm/tcpci.h | 14 ++++++--
 2 files changed, 74 insertions(+), 3 deletions(-)
drivers/usb/typec/tcpm/tcpci.c +62 -1
index f91688e43991..12d983a75510 100644
@@ -18,7 +18,10 @@
 
 #include "tcpci.h"
 
-#define PD_RETRY_COUNT 3
+#define	PD_RETRY_COUNT				3
+#define	AUTO_DISCHARGE_DEFAULT_THRESHOLD_MV	3500
+#define	AUTO_DISCHARGE_PD_HEADROOM_MV		850
+#define	AUTO_DISCHARGE_PPS_HEADROOM_MV		1250
 
 struct tcpci {
 	struct device *dev;
@@ -268,6 +271,58 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
 				enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
 }
 
+static int tcpci_enable_auto_vbus_discharge(struct tcpc_dev *dev, bool enable)
+{
+	struct tcpci *tcpci = tcpc_to_tcpci(dev);
+	int ret;
+
+	ret = regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL, TCPC_POWER_CTRL_AUTO_DISCHARGE,
+				 enable ? TCPC_POWER_CTRL_AUTO_DISCHARGE : 0);
+	return ret;
+}
+
+static int tcpci_set_auto_vbus_discharge_threshold(struct tcpc_dev *dev, enum typec_pwr_opmode mode,
+						   bool pps_active, u32 requested_vbus_voltage_mv)
+{
+	struct tcpci *tcpci = tcpc_to_tcpci(dev);
+	unsigned int pwr_ctrl, threshold = 0;
+	int ret;
+
+	/*
+	 * Indicates that vbus is going to go away due PR_SWAP, hard reset etc.
+	 * Do not discharge vbus here.
+	 */
+	if (requested_vbus_voltage_mv == 0)
+		goto write_thresh;
+
+	ret = regmap_read(tcpci->regmap, TCPC_POWER_CTRL, &pwr_ctrl);
+	if (ret < 0)
+		return ret;
+
+	if (pwr_ctrl & TCPC_FAST_ROLE_SWAP_EN) {
+		/* To prevent disconnect when the source is fast role swap is capable. */
+		threshold = AUTO_DISCHARGE_DEFAULT_THRESHOLD_MV;
+	} else if (mode == TYPEC_PWR_MODE_PD) {
+		if (pps_active)
+			threshold = (95 * requested_vbus_voltage_mv / 100) -
+				AUTO_DISCHARGE_PD_HEADROOM_MV;
+		else
+			threshold = (95 * requested_vbus_voltage_mv / 100) -
+				AUTO_DISCHARGE_PPS_HEADROOM_MV;
+	} else {
+		/* 3.5V for non-pd sink */
+		threshold = AUTO_DISCHARGE_DEFAULT_THRESHOLD_MV;
+	}
+
+	threshold = threshold / TCPC_VBUS_SINK_DISCONNECT_THRESH_LSB_MV;
+
+	if (threshold > TCPC_VBUS_SINK_DISCONNECT_THRESH_MAX)
+		return -EINVAL;
+
+write_thresh:
+	return tcpci_write16(tcpci, TCPC_VBUS_SINK_DISCONNECT_THRESH, threshold);
+}
+
 static int tcpci_enable_frs(struct tcpc_dev *dev, bool enable)
 {
 	struct tcpci *tcpci = tcpc_to_tcpci(dev);
@@ -638,6 +693,12 @@ struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data)
 	tcpci->tcpc.enable_frs = tcpci_enable_frs;
 	tcpci->tcpc.frs_sourcing_vbus = tcpci_frs_sourcing_vbus;
 
+	if (tcpci->data->auto_discharge_disconnect) {
+		tcpci->tcpc.enable_auto_vbus_discharge = tcpci_enable_auto_vbus_discharge;
+		tcpci->tcpc.set_auto_vbus_discharge_threshold =
+			tcpci_set_auto_vbus_discharge_threshold;
+	}
+
 	err = tcpci_parse_config(tcpci);
 	if (err < 0)
 		return ERR_PTR(err);
drivers/usb/typec/tcpm/tcpci.h +12 -2
index b418fe11b527..3fe313655f0c 100644
@@ -8,6 +8,8 @@
 #ifndef __LINUX_USB_TCPCI_H
 #define __LINUX_USB_TCPCI_H
 
+#include <linux/usb/typec.h>
+
 #define TCPC_VENDOR_ID			0x0
 #define TCPC_PRODUCT_ID			0x2
 #define TCPC_BCD_DEV			0x4
@@ -67,6 +69,7 @@
 
 #define TCPC_POWER_CTRL			0x1c
 #define TCPC_POWER_CTRL_VCONN_ENABLE	BIT(0)
+#define TCPC_POWER_CTRL_AUTO_DISCHARGE	BIT(4)
 #define TCPC_FAST_ROLE_SWAP_EN		BIT(7)
 
 #define TCPC_CC_STATUS			0x1d
@@ -133,6 +136,8 @@
 
 #define TCPC_VBUS_VOLTAGE			0x70
 #define TCPC_VBUS_SINK_DISCONNECT_THRESH	0x72
+#define TCPC_VBUS_SINK_DISCONNECT_THRESH_LSB_MV	25
+#define TCPC_VBUS_SINK_DISCONNECT_THRESH_MAX	0x3ff
 #define TCPC_VBUS_STOP_DISCHARGE_THRESH		0x74
 #define TCPC_VBUS_VOLTAGE_ALARM_HI_CFG		0x76
 #define TCPC_VBUS_VOLTAGE_ALARM_LO_CFG		0x78
@@ -140,17 +145,22 @@
 /* I2C_WRITE_BYTE_COUNT + 1 when TX_BUF_BYTE_x is only accessible I2C_WRITE_BYTE_COUNT */
 #define TCPC_TRANSMIT_BUFFER_MAX_LEN		31
 
+struct tcpci;
+
 /*
- * @TX_BUF_BYTE_x_hidden
+ * @TX_BUF_BYTE_x_hidden:
  *		optional; Set when TX_BUF_BYTE_x can only be accessed through I2C_WRITE_BYTE_COUNT.
  * @frs_sourcing_vbus:
  *		Optional; Callback to perform chip specific operations when FRS
  *		is sourcing vbus.
+ * @auto_discharge_disconnect:
+ *		Optional; Enables TCPC to autonously discharge vbus on disconnect.
  */
-struct tcpci;
 struct tcpci_data {
 	struct regmap *regmap;
 	unsigned char TX_BUF_BYTE_x_hidden:1;
+	unsigned char auto_discharge_disconnect:1;
+
 	int (*init)(struct tcpci *tcpci, struct tcpci_data *data);
 	int (*set_vconn)(struct tcpci *tcpci, struct tcpci_data *data,
 			 bool enable);
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog
author maintainer
#29 lore raw

PATCH v9 15/15 usb: typec: tcpci_maxim: Enable auto discharge disconnect

In reply to Badhri Jagan Sridharan

Enable auto discharge disconnect for Maxim TCPC.

---
Changes since v1:
- Changing patch version to v6 to fix version number confusion.

Changes since v6:
- Rebase on usb-next.

Changes since v7:
- Heikki's suggestion:
Moved the actual write of TCPC_VBUS_SINK_DISCONNECT_THRES register to tcpci code.

Changes since v8:
- Moved the logic to program the default values of TCPC_VBUS_SINK_DISCONNECT_THRESH into the tcpci code.

---
 drivers/usb/typec/tcpm/tcpci_maxim.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/typec/tcpm/tcpci_maxim.c +1 -0
index 43dcad95e897..55dea33387ec 100644
@@ -441,6 +441,7 @@ static int max_tcpci_probe(struct i2c_client *client, const struct i2c_device_id
 	chip->data.TX_BUF_BYTE_x_hidden = true;
 	chip->data.init = tcpci_init;
 	chip->data.frs_sourcing_vbus = max_tcpci_frs_sourcing_vbus;
+	chip->data.auto_discharge_disconnect = true;
 
 	max_tcpci_init_regs(chip);
 	chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
Signature · 1 line
2.28.0.709.gb0816b6eb0-goog