Skip to contentSkip to conversation

Type-C Port Manager

RFC PATCH Superseded Guenter Roeck 5 participants 24 messages Aug 23, 2016 – Oct 3, 2016

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Type-C Port Manager

The following series of patches implements a USB Type-C Port Manager using the pending USB Type-C class code as basis. The code is still WIP, but I think it is important to get feedback from the community at this point.

There are two patches in the series. The first patch implements the Type-C Port Manager state machine. The forth patch is an interface between the Type-C Port Manager and a TCPCI (Type-C Port Controller Interface) compliant USB Type-C Port Controller.

Patch 2/2 (the interface to a TCPCI compliant chip) is currently untested since I don't have the necessary hardware available. The port manager code was tested connecting to an Embedded Controller on a Chromebook, bypassing the Port Manager implementation in the EC.

Both Source and Sink operation was tested with various Type-C chargers, docks, and connectors. Alternate mode support is partially implemented (Alternate mode support is requested from the partner), but alternate modes are not actually selected. Implementing this will require more thought, since the actual alternate mode support has to be implemented elsewhere, such as in a dedicated Phy driver. It should be possible to implement the interface between phy driver and Type-C Port Controller driver using extcon, but I have not further explored the possibilities, and other options might be possible and/or better.

v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes while the state machine is in a transient change (waiting for a timeout)
- Update current limit after CC voltage level changes on a port which is not PD capable.
- Applies to v6 of "USB Type-C Connector class" patch series.

v2:
- Class code no longer uses locking, so the patch to remove it is no longer necessary.
- tcpm: Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- tcpm: All PD messages are little endian; convert to and from CPU endianness.
- tcpm: Avoid comparisons against NULL.
- tcpm: Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- tcpm/tcpc: Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- tcpm/tcpc: Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.
- Applies to v5 of "USB Type-C Connector class" patch series.

#1 lore raw

RFC PATCH v3 1/2 usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

The driver provides both a functional API as well as callbacks for lower level drivers.

---
v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes while the state machine is in a transient change (waiting for a timeout)
- Update current limit after CC voltage level changes on a port which is not PD capable.

v2:
- Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- All PD messages are little endian; convert to and from CPU endianness.
- Avoid comparisons against NULL.
- Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.

 drivers/usb/typec/Kconfig  |    7 +
 drivers/usb/typec/Makefile |    1 +
 drivers/usb/typec/tcpm.c   | 3163 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpm.h   |  137 ++
 include/linux/usb/pd.h     |  282 ++++
 include/linux/usb/pd_bdo.h |   31 +
 include/linux/usb/pd_vdo.h |  412 ++++++
 7 files changed, 4033 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm.c
 create mode 100644 drivers/usb/typec/tcpm.h
 create mode 100644 include/linux/usb/pd.h
 create mode 100644 include/linux/usb/pd_bdo.h
 create mode 100644 include/linux/usb/pd_vdo.h
drivers/usb/typec/Kconfig +7 -0
index 7a345a4b1e7a..113bb1b3589c 100644
@@ -18,4 +18,11 @@ config TYPEC_WCOVE
 	  To compile this driver as module, choose M here: the module will be
 	  called typec_wcove
 
+config TYPEC_TCPM
+	tristate "USB Type-C Port Controller Manager"
+	select TYPEC
+	help
+	  The Type-C Port Controller Manager provides a USB PD and USB Type-C
+	  state machine for use with Type-C Port Controllers.
+
 endmenu
drivers/usb/typec/Makefile +1 -0
index b9cb862221af..bbe45721cf52 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_TYPEC)		+= typec.o
 obj-$(CONFIG_TYPEC_WCOVE)	+= typec_wcove.o
+obj-$(CONFIG_TYPEC_TCPM)	+= tcpm.o
drivers/usb/typec/tcpm.c new file +3163 -0

2,986 lines not on this page · raw message

drivers/usb/typec/tcpm.h new file +137 -0

137 lines not on this page · raw message

include/linux/usb/pd.h new file +282 -0

282 lines not on this page · raw message

include/linux/usb/pd_bdo.h new file +31 -0
new file mode 100644
index 000000000000..17e9388cf955

Lines of this file are not shown. View the raw message.

include/linux/usb/pd_vdo.h new file +412 -0

412 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.8.0.rc3.226.g39d4020

Part of the diff is not shown here. View the raw message.

#2 lore raw

RE: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Hi Guenter,

Quoted 32 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

Quoted 5 lines

The driver provides both a functional API as well as callbacks for lower level drivers.

---

A specific question, if power sink wants to request a new power level after SNK_READY, how to handle it with this tcpm?

Thanks
Li Jun

#3 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 34 lines · Jun Li

Hi Guenter,

Quoted 32 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

Quoted 9 lines
Quoted 5 lines

The driver provides both a functional API as well as callbacks for lower level drivers.

---

A specific question, if power sink wants to request a new power level after SNK_READY, how to handle it with this tcpm?

So far I have considered the required power level to be static, based on our curent implementations. That should be easy to change, though, with an additional API function, to be called from a low level driver. Do you have that requirement, and would such a function meet your needs ?

Thanks,
Guenter

#4 lore raw

RE: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Hi Guenter

Quoted 55 lines

-----Original Message-----
From: Guenter Roeck [mailto:[email protected]]
Sent: Saturday, September 10, 2016 10:23 AM
To: Jun Li <[email protected]>
Cc: Guenter Roeck <[email protected]>; Felipe Balbi <[email protected]>; Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 41 lines · Jun Li

Hi Guenter,

Quoted 38 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

The driver provides both a functional API as well as callbacks for lower level drivers.

---

Quoted 9 lines
Quoted 3 lines

A specific question, if power sink wants to request a new power level after SNK_READY, how to handle it with this tcpm?

So far I have considered the required power level to be static, based on our curent implementations. That should be easy to change, though, with an additional API function, to be called from a low level driver. Do you have that requirement, and would such a function meet your needs ?

So you are going to make port->tcpc->config to be dynamic to meet my need?

Li Jun

Quoted 2 lines

Thanks,
Guenter

#5 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 61 lines · Jun Li

Hi Guenter

Quoted 59 lines

-----Original Message-----
From: Guenter Roeck [mailto:[email protected]]
Sent: Saturday, September 10, 2016 10:23 AM
To: Jun Li <[email protected]>
Cc: Guenter Roeck <[email protected]>; Felipe Balbi <[email protected]>; Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 44 lines · Jun Li

Hi Guenter,

Quoted 33 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

Quoted 5 lines

The driver provides both a functional API as well as callbacks for lower level drivers.

---

A specific question, if power sink wants to request a new power level after SNK_READY, how to handle it with this tcpm?

Quoted 8 lines
Quoted 5 lines

So far I have considered the required power level to be static, based on our curent implementations. That should be easy to change, though, with an additional API function, to be called from a low level driver. Do you have that requirement, and would such a function meet your needs ?

So you are going to make port->tcpc->config to be dynamic to meet my need?

What would that help ? How would tcpm get informed that the power requirements changed without an API function telling it that power requirements changed ?

Guenter

#6 lore raw

RE: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 87 lines

-----Original Message-----
From: Guenter Roeck [mailto:[email protected]]
Sent: Monday, September 12, 2016 10:24 AM
To: Jun Li <[email protected]>
Cc: Guenter Roeck <[email protected]>; Felipe Balbi <[email protected]>; Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 41 lines · Jun Li

Hi Guenter

Quoted 39 lines

-----Original Message-----
From: Guenter Roeck [mailto:[email protected]]
Sent: Saturday, September 10, 2016 10:23 AM
To: Jun Li <[email protected]>
Cc: Guenter Roeck <[email protected]>; Felipe Balbi <[email protected]>; Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 24 lines · Jun Li

Hi Guenter,

Quoted 22 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected];
linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C

PHY

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines

usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

The driver provides both a functional API as well as callbacks for lower level drivers.

---

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

A specific question, if power sink wants to request a new power level after SNK_READY, how to handle it with this tcpm?

So far I have considered the required power level to be static, based on our curent implementations. That should be easy to change, though, with an additional API function, to be called from a low level driver. Do you have that requirement, and would such a function meet your

needs ?

Quoted 2 lines
Quoted 1 line
Quoted 6 lines
Quoted 1 line

So you are going to make port->tcpc->config to be dynamic to meet my

need?

Quoted 1 line

What would that help ? How would tcpm get informed that the power requirements changed without an API function telling it that power requirements changed ?

Of cos I agree an additional API is required, I am just wondering how that API will be look like, as current request build is according to port->tcpc->config.

Li Jun

Quoted 2 lines

Guenter

#7 lore raw

RE: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

In reply to Guenter Roeck

Hi Guenter,

Quoted 72 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

The driver provides both a functional API as well as callbacks for lower level drivers.

---
v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes
while the state machine is in a transient change (waiting for a timeout)
- Update current limit after CC voltage level changes on a port which is not
PD capable.

v2:
- Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- All PD messages are little endian; convert to and from CPU endianness.
- Avoid comparisons against NULL.
- Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.

 drivers/usb/typec/Kconfig  |    7 +
 drivers/usb/typec/Makefile |    1 +
 drivers/usb/typec/tcpm.c   | 3163
++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpm.h   |  137 ++
 include/linux/usb/pd.h     |  282 ++++
 include/linux/usb/pd_bdo.h |   31 +
 include/linux/usb/pd_vdo.h |  412 ++++++
 7 files changed, 4033 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm.c
 create mode 100644 drivers/usb/typec/tcpm.h
 create mode 100644 include/linux/usb/pd.h
 create mode 100644 include/linux/usb/pd_bdo.h
 create mode 100644 include/linux/usb/pd_vdo.h

...

Quoted 4 lines
drivers/usb/typec/tcpm.c new file +4 -0
+
+static void run_state_machine(struct tcpm_port *port)
+{
+	int ret;

drivers/usb/typec/tcpm.c line 1964

Quoted 10 lines
drivers/usb/typec/tcpm.c new file +10 -0
+
+	port->enter_state = port->state;
+	switch (port->state) {
+	/* SRC states */
+	case SRC_UNATTACHED:
+		tcpm_swap_complete(port, -ENOTCONN);
+		tcpm_src_detach(port);
+		tcpm_set_cc(port, TYPEC_CC_RP_DEF);
+		if (port->typec_caps.type == TYPEC_PORT_DRP)
+			tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);

With this and below, after disconnect, the DRP port state machine will be in infinite loop of state transition between SRC_UNATTACHED <--> SNK_UNATTACHED, correct?

Li Jun
...

Quoted 9 lines
drivers/usb/typec/tcpm.c new file +8 -0
+	/* SNK states */
+	case SNK_UNATTACHED:
+		tcpm_swap_complete(port, -ENOTCONN);
+		tcpm_snk_detach(port);
+		tcpm_set_cc(port, TYPEC_CC_RD);
+		if (port->typec_caps.type == TYPEC_PORT_DRP)
+			tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
+		break;
#8 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 87 lines · Jun Li

Hi Guenter,

Quoted 72 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

The driver provides both a functional API as well as callbacks for lower level drivers.

---
v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes
while the state machine is in a transient change (waiting for a timeout)
- Update current limit after CC voltage level changes on a port which is not
PD capable.

v2:
- Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- All PD messages are little endian; convert to and from CPU endianness.
- Avoid comparisons against NULL.
- Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.

 drivers/usb/typec/Kconfig  |    7 +
 drivers/usb/typec/Makefile |    1 +
 drivers/usb/typec/tcpm.c   | 3163
++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpm.h   |  137 ++
 include/linux/usb/pd.h     |  282 ++++
 include/linux/usb/pd_bdo.h |   31 +
 include/linux/usb/pd_vdo.h |  412 ++++++
 7 files changed, 4033 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm.c
 create mode 100644 drivers/usb/typec/tcpm.h
 create mode 100644 include/linux/usb/pd.h
 create mode 100644 include/linux/usb/pd_bdo.h
 create mode 100644 include/linux/usb/pd_vdo.h

...

Quoted 10 lines
drivers/usb/typec/tcpm.c new file +10 -0
+
+static void run_state_machine(struct tcpm_port *port)
+{
+     int ret;
+
+     port->enter_state = port->state;
+     switch (port->state) {
+     /* SRC states */
+     case SRC_UNATTACHED:
+             tcpm_swap_complete(port, -ENOTCONN);
Quoted 9 lines

drivers/usb/typec/tcpm.c line 1964

Quoted 4 lines
drivers/usb/typec/tcpm.c new file +4 -0
+             tcpm_src_detach(port);
+             tcpm_set_cc(port, TYPEC_CC_RP_DEF);
+             if (port->typec_caps.type == TYPEC_PORT_DRP)
+                     tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);

With this and below, after disconnect, the DRP port state machine will be in infinite loop of state transition between SRC_UNATTACHED <--> SNK_UNATTACHED, correct?

Only while disconnected. It tries to alternatively connect as source and as sink (being configured as DRP). Once a CC line state change is reported it will transition out. I have a newer version of the patch (not yet published) which supports DRP toggling by the TCPC. With that enabled, TCPM does not change states until a CC state change is reported.

Guenter

Quoted 12 lines

Li Jun
...

Quoted 9 lines
drivers/usb/typec/tcpm.c new file +8 -0
+     /* SNK states */
+     case SNK_UNATTACHED:
+             tcpm_swap_complete(port, -ENOTCONN);
+             tcpm_snk_detach(port);
+             tcpm_set_cc(port, TYPEC_CC_RD);
+             if (port->typec_caps.type == TYPEC_PORT_DRP)
+                     tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
+             break;
#9 lore raw

RE: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Hi,

Quoted 112 lines

-----Original Message-----
From: Guenter Roeck [mailto:[email protected]]
Sent: Friday, September 30, 2016 12:37 AM
To: Jun Li <[email protected]>
Cc: Guenter Roeck <[email protected]>; Felipe Balbi <[email protected]>; Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 88 lines · Jun Li

Hi Guenter,

Quoted 73 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

The driver provides both a functional API as well as callbacks for lower level drivers.

---
v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes
while the state machine is in a transient change (waiting for a
timeout)
- Update current limit after CC voltage level changes on a port which is not
PD capable.

v2:
- Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- All PD messages are little endian; convert to and from CPU endianness.
- Avoid comparisons against NULL.
- Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.

 drivers/usb/typec/Kconfig  |    7 +
 drivers/usb/typec/Makefile |    1 +
 drivers/usb/typec/tcpm.c   | 3163
++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpm.h   |  137 ++
 include/linux/usb/pd.h     |  282 ++++
 include/linux/usb/pd_bdo.h |   31 +
 include/linux/usb/pd_vdo.h |  412 ++++++
 7 files changed, 4033 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm.c  create mode 100644

drivers/usb/typec/tcpm.h create mode 100644 include/linux/usb/pd.h create mode 100644 include/linux/usb/pd_bdo.h create mode 100644 include/linux/usb/pd_vdo.h

...

Quoted 10 lines
drivers/usb/typec/tcpm.c new file +10 -0
+
+static void run_state_machine(struct tcpm_port *port) {
+     int ret;
+
+     port->enter_state = port->state;
+     switch (port->state) {
+     /* SRC states */
+     case SRC_UNATTACHED:
+             tcpm_swap_complete(port, -ENOTCONN);
+             tcpm_src_detach(port);
Quoted 9 lines

drivers/usb/typec/tcpm.c

Quoted 4 lines
drivers/usb/typec/tcpm.c new file +4 -0
+             tcpm_set_cc(port, TYPEC_CC_RP_DEF);
+             if (port->typec_caps.type == TYPEC_PORT_DRP)
+                     tcpm_set_state(port, SNK_UNATTACHED,
+ PD_T_DRP_SNK);

With this and below, after disconnect, the DRP port state machine will be in infinite loop of state transition between SRC_UNATTACHED <--> SNK_UNATTACHED, correct?

Quoted 6 lines

Only while disconnected. It tries to alternatively connect as source and as sink (being configured as DRP). Once a CC line state change is reported it will transition out. I have a newer version of the patch (not yet published) which supports DRP toggling by the TCPC. With that enabled, TCPM does not change states until a CC state change is reported.

Great, that way will be better, do you plan post your newer patch recently?

Thanks
Li Jun

Quoted 15 lines

Guenter

Quoted 10 lines

Li Jun
...

Quoted 7 lines
drivers/usb/typec/tcpm.c new file +7 -0
+     /* SNK states */
+     case SNK_UNATTACHED:
+             tcpm_swap_complete(port, -ENOTCONN);
+             tcpm_snk_detach(port);
+             tcpm_set_cc(port, TYPEC_CC_RD);
+             if (port->typec_caps.type == TYPEC_PORT_DRP)
+                     tcpm_set_state(port, SRC_UNATTACHED,

PD_T_DRP_SRC);

Quoted 2 lines
Quoted 2 lines

+ break;

#10 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 114 lines · Jun Li

Hi,

Quoted 112 lines

-----Original Message-----
From: Guenter Roeck [mailto:[email protected]]
Sent: Friday, September 30, 2016 12:37 AM
To: Jun Li <[email protected]>
Cc: Guenter Roeck <[email protected]>; Felipe Balbi <[email protected]>; Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 97 lines · Jun Li

Hi Guenter,

Quoted 73 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

This driver implements the USB Type-C Power Delivery state machine for both source and sink ports. Alternate mode support is not fully implemented.

The driver attaches to the USB Type-C class code implemented in the following patches.

usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY
usb: USB Type-C connector class

This driver only implements the state machine. Lower level drivers are responsible for
- Reporting VBUS status and activating VBUS
- Setting CC lines and providing CC line status
- Setting line polarity
- Activating and deactivating VCONN
- Setting the current limit
- Activating and deactivating PD message transfers
- Sending and receiving PD messages

The driver provides both a functional API as well as callbacks for lower level drivers.

---
v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes
while the state machine is in a transient change (waiting for a
timeout)
- Update current limit after CC voltage level changes on a port which is not
PD capable.

v2:
- Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- All PD messages are little endian; convert to and from CPU endianness.
- Avoid comparisons against NULL.
- Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.

 drivers/usb/typec/Kconfig  |    7 +
 drivers/usb/typec/Makefile |    1 +
 drivers/usb/typec/tcpm.c   | 3163
++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpm.h   |  137 ++
 include/linux/usb/pd.h     |  282 ++++
 include/linux/usb/pd_bdo.h |   31 +
 include/linux/usb/pd_vdo.h |  412 ++++++
 7 files changed, 4033 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm.c  create mode 100644

drivers/usb/typec/tcpm.h create mode 100644 include/linux/usb/pd.h create mode 100644 include/linux/usb/pd_bdo.h create mode 100644 include/linux/usb/pd_vdo.h

...

Quoted 4 lines
drivers/usb/typec/tcpm.c new file +4 -0
+
+static void run_state_machine(struct tcpm_port *port) {
+     int ret;
+

drivers/usb/typec/tcpm.c

Quoted 10 lines
drivers/usb/typec/tcpm.c new file +10 -0
+     port->enter_state = port->state;
+     switch (port->state) {
+     /* SRC states */
+     case SRC_UNATTACHED:
+             tcpm_swap_complete(port, -ENOTCONN);
+             tcpm_src_detach(port);
+             tcpm_set_cc(port, TYPEC_CC_RP_DEF);
+             if (port->typec_caps.type == TYPEC_PORT_DRP)
+                     tcpm_set_state(port, SNK_UNATTACHED,
+ PD_T_DRP_SNK);

With this and below, after disconnect, the DRP port state machine will be in infinite loop of state transition between SRC_UNATTACHED <--> SNK_UNATTACHED, correct?

Quoted 8 lines
Quoted 6 lines

Only while disconnected. It tries to alternatively connect as source and as sink (being configured as DRP). Once a CC line state change is reported it will transition out. I have a newer version of the patch (not yet published) which supports DRP toggling by the TCPC. With that enabled, TCPM does not change states until a CC state change is reported.

Great, that way will be better, do you plan post your newer patch recently?

I should be able to send out a new version early next week, after applying and testing your suggested changes. I'll also try to make a test branch available in my repository at kernel.org.

Guenter

Quoted 18 lines

Thanks
Li Jun

Quoted 15 lines

Guenter

Quoted 10 lines

Li Jun
...

Quoted 7 lines
drivers/usb/typec/tcpm.c new file +7 -0
+     /* SNK states */
+     case SNK_UNATTACHED:
+             tcpm_swap_complete(port, -ENOTCONN);
+             tcpm_snk_detach(port);
+             tcpm_set_cc(port, TYPEC_CC_RD);
+             if (port->typec_caps.type == TYPEC_PORT_DRP)
+                     tcpm_set_state(port, SRC_UNATTACHED,

PD_T_DRP_SRC);

Quoted 2 lines
Quoted 2 lines

+ break;

#11 lore raw

RE: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

In reply to Guenter Roeck

Hi Guenter,

Quoted 12 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

...

Quoted 5 lines
include/linux/usb/pd.h new file +0 -0
new file mode 100644
index 000000000000..6b1679af7a25

...

include/linux/usb/pd.h line 159

Quoted 7 lines
include/linux/usb/pd.h new file +7 -0
+#define PDO_VAR(min_mv, max_mv, max_ma)					\
+	((PDO_TYPE_VAR << PDO_TYPE_SHIFT) |				\
+	 ((((min_mv) / 50) & PDO_VAR_MIN_VOLT_MASK) <<			\
+	  PDO_VAR_MIN_VOLT_SHIFT) |					\
+	 ((((max_mv) / 50) & PDO_VAR_MAX_VOLT_MASK) <<			\
+	  PDO_VAR_MAX_VOLT_SHIFT) |					\
+	 ((((max_ma) / 50) & PDO_VAR_MAX_CURR_MASK) <<			\

((((max_ma) / 10) & PDO_VAR_MAX_CURR_MASK) << \

Li Jun

#12 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 30 lines · Jun Li

Hi Guenter,

Quoted 12 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

...

Quoted 5 lines
include/linux/usb/pd.h new file +0 -0
new file mode 100644
index 000000000000..6b1679af7a25

...

include/linux/usb/pd.h line 159

Quoted 7 lines
include/linux/usb/pd.h new file +7 -0
+#define PDO_VAR(min_mv, max_mv, max_ma)                                      \
+     ((PDO_TYPE_VAR << PDO_TYPE_SHIFT) |                             \
+      ((((min_mv) / 50) & PDO_VAR_MIN_VOLT_MASK) <<                  \
+       PDO_VAR_MIN_VOLT_SHIFT) |                                     \
+      ((((max_mv) / 50) & PDO_VAR_MAX_VOLT_MASK) <<                  \
+       PDO_VAR_MAX_VOLT_SHIFT) |                                     \
+      ((((max_ma) / 50) & PDO_VAR_MAX_CURR_MASK) <<                  \
Quoted 2 lines

((((max_ma) / 10) & PDO_VAR_MAX_CURR_MASK) << \

Thanks, fixed. PDO_BATT has a similar problem, which I noticed while fixing the above.

Guenter

#13 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 1 line · Guenter Roeck

On Thu, Sep 29, 2016 at 11:37 PM, Jun Li <[email protected]> wrote:

[]

Quoted 1 line

include/linux/usb/pd.h

Quoted 1 line
include/linux/usb/pd.h +0 -0

[]

Quoted 10 lines

include/linux/usb/pd.h line 159

Quoted 7 lines
include/linux/usb/pd.h +7 -0
+#define PDO_VAR(min_mv, max_mv, max_ma)                                      \
+     ((PDO_TYPE_VAR << PDO_TYPE_SHIFT) |                             \
+      ((((min_mv) / 50) & PDO_VAR_MIN_VOLT_MASK) <<                  \
+       PDO_VAR_MIN_VOLT_SHIFT) |                                     \
+      ((((max_mv) / 50) & PDO_VAR_MAX_VOLT_MASK) <<                  \
+       PDO_VAR_MAX_VOLT_SHIFT) |                                     \
+      ((((max_ma) / 50) & PDO_VAR_MAX_CURR_MASK) <<                  \

((((max_ma) / 10) & PDO_VAR_MAX_CURR_MASK) << \

This would be easier to read if laid out differently.

#define PDO_VAR(min_mv, max_mv, max_ma)							\
	((PDO_TYPE_VAR << PDO_TYPE_SHIFT) |						\
	 ((((min_mv) / 50) & PDO_VAR_MIN_VOLT_MASK) << PDO_VAR_MIN_VOLT_SHIFT) |	\
	 ((((max_mv) / 50) & PDO_VAR_MAX_VOLT_MASK) << PDO_VAR_MAX_VOLT_SHIFT) |	\
	 ((((max_ma) / 10) & PDO_VAR_MAX_CURR_MASK) << PDO_VAR_MAX_CURR_SHIFT))
#14 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 16 lines · Joe Perches
Quoted 1 line · Guenter Roeck

On Thu, Sep 29, 2016 at 11:37 PM, Jun Li <[email protected]> wrote:

[]

Quoted 1 line

include/linux/usb/pd.h

Quoted 1 line
include/linux/usb/pd.h +0 -0

[]

Quoted 10 lines

include/linux/usb/pd.h line 159

Quoted 7 lines
include/linux/usb/pd.h +7 -0
+#define PDO_VAR(min_mv, max_mv, max_ma)                                      \
+     ((PDO_TYPE_VAR << PDO_TYPE_SHIFT) |                             \
+      ((((min_mv) / 50) & PDO_VAR_MIN_VOLT_MASK) <<                  \
+       PDO_VAR_MIN_VOLT_SHIFT) |                                     \
+      ((((max_mv) / 50) & PDO_VAR_MAX_VOLT_MASK) <<                  \
+       PDO_VAR_MAX_VOLT_SHIFT) |                                     \
+      ((((max_ma) / 50) & PDO_VAR_MAX_CURR_MASK) <<                  \

((((max_ma) / 10) & PDO_VAR_MAX_CURR_MASK) << \

Quoted 8 lines

This would be easier to read if laid out differently.

#define PDO_VAR(min_mv, max_mv, max_ma)                                                 \
        ((PDO_TYPE_VAR << PDO_TYPE_SHIFT) |                                             \
         ((((min_mv) / 50) & PDO_VAR_MIN_VOLT_MASK) << PDO_VAR_MIN_VOLT_SHIFT) |        \
         ((((max_mv) / 50) & PDO_VAR_MAX_VOLT_MASK) << PDO_VAR_MAX_VOLT_SHIFT) |        \
         ((((max_ma) / 10) & PDO_VAR_MAX_CURR_MASK) << PDO_VAR_MAX_CURR_SHIFT))

Code now looks as follows.

#define PDO_VAR_MIN_VOLT(mv) ((((mv) / 50) & PDO_VAR_MIN_VOLT_MASK) << \
PDO_VAR_MIN_VOLT_SHIFT)
#define PDO_VAR_MAX_VOLT(mv) ((((mv) / 50) & PDO_VAR_MAX_VOLT_MASK) << \
PDO_VAR_MAX_VOLT_SHIFT)
#define PDO_VAR_MAX_CURR(ma) ((((ma) / 10) & PDO_VAR_MAX_CURR_MASK) << \
PDO_VAR_MAX_CURR_SHIFT)

#define PDO_VAR(min_mv, max_mv, max_ma)                         \
        (PDO_TYPE(PDO_TYPE_VAR) | PDO_VAR_MIN_VOLT(min_mv) |    \
         PDO_VAR_MAX_VOLT(max_mv) | PDO_VAR_MAX_CURR(max_ma))

Though maybe I should just ignore line length limits or use shorter defines.

Guenter

#15 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

Quoted 8 lines · Guenter Roeck

Code now looks as follows.

#define PDO_VAR_MIN_VOLT(mv) ((((mv) / 50) & PDO_VAR_MIN_VOLT_MASK) << \
  PDO_VAR_MIN_VOLT_SHIFT) #define PDO_VAR_MAX_VOLT(mv) ((((mv) / 50) & PDO_VAR_MAX_VOLT_MASK) << \
  PDO_VAR_MAX_VOLT_SHIFT) #define PDO_VAR_MAX_CURR(ma) ((((ma) / 10) & PDO_VAR_MAX_CURR_MASK) << \
  PDO_VAR_MAX_CURR_SHIFT)

When #defines are continued, I generally find it nicer to have the entire definition on a separate line

#define PDO_VAR_MIN_VOLT(mv)						\
	((((mv) / 50) & PDO_VAR_MIN_VOLT_MASK) << PDO_VAR_MIN_VOLT_SHIFT)
Quoted 1 line

Though maybe I should just ignore line length limits or use shorter defines.

When it makes sense, yes please.

maintainer
#16 lore raw

Re: [RFC PATCH v3 1/2] usb: typec: USB Type-C Port Manager (tcpm)

In reply to Guenter Roeck

Hi Guenter,

drivers/usb/typec/Kconfig config TYPEC_WCOVE · line 26

Quoted 6 lines · Guenter Roeck
drivers/usb/typec/Kconfig +6 -0
+config TYPEC_TCPM
+	tristate "USB Type-C Port Controller Manager"
+	select TYPEC
+	help
+	  The Type-C Port Controller Manager provides a USB PD and USB Type-C
+	  state machine for use with Type-C Port Controllers.

Since this is "tristate", don't forget to add MODULE_*() stuff in the next version.

Thanks,

Signature · 1 line
heikki
#17 lore raw

RFC PATCH v3 2/2 usb: typec: Type-C Port Controller Interface driver (tcpci)

In reply to Guenter Roeck

The port controller interface driver interconnects the Type-C Port Manager with a Type-C Port Controller Interface (TCPCI) compliant port controller.

---
v3:
- No change

v2:
- Adjust to modified callbacks into tcpm code

 drivers/usb/typec/Kconfig  |   9 +
 drivers/usb/typec/Makefile |   1 +
 drivers/usb/typec/tcpci.c  | 487 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpci.h  | 133 +++++++++++++
 4 files changed, 630 insertions(+)
 create mode 100644 drivers/usb/typec/tcpci.c
 create mode 100644 drivers/usb/typec/tcpci.h
drivers/usb/typec/Kconfig +9 -0
index 113bb1b3589c..a92c9d1a3e00 100644
@@ -25,4 +25,13 @@ config TYPEC_TCPM
 	  The Type-C Port Controller Manager provides a USB PD and USB Type-C
 	  state machine for use with Type-C Port Controllers.
 
+if TYPEC_TCPM
+
+config TYPEC_TCPCI
+	tristate "Type-C Port Controller Interface driver"
+	help
+	  Type-C Port Controller driver for TCPCI-compliant controller.
+
+endif
+
 endmenu
drivers/usb/typec/Makefile +1 -0
index bbe45721cf52..7dbaf8c3911d 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_TYPEC)		+= typec.o
 obj-$(CONFIG_TYPEC_WCOVE)	+= typec_wcove.o
 obj-$(CONFIG_TYPEC_TCPM)	+= tcpm.o
+obj-$(CONFIG_TYPEC_TCPCI)	+= tcpci.o
drivers/usb/typec/tcpci.c new file +487 -0

487 lines not on this page · raw message

drivers/usb/typec/tcpci.h new file +133 -0

133 lines not on this page · raw message

Signature · 1 line
2.8.0.rc3.226.g39d4020
#18 lore raw

RE: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

Hi Guenter,

Quoted 293 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

The port controller interface driver interconnects the Type-C Port Manager with a Type-C Port Controller Interface (TCPCI) compliant port controller.

---
v3:
- No change

v2:
- Adjust to modified callbacks into tcpm code

 drivers/usb/typec/Kconfig  |   9 +
 drivers/usb/typec/Makefile |   1 +
 drivers/usb/typec/tcpci.c  | 487
+++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpci.h  | 133 +++++++++++++
 4 files changed, 630 insertions(+)
 create mode 100644 drivers/usb/typec/tcpci.c  create mode 100644

drivers/usb/typec/tcpci.h

drivers/usb/typec/Kconfig index renamed from drivers/usb/typec/Kconfig +0 -0

113bb1b3589c..a92c9d1a3e00 100644

drivers/usb/typec/Kconfig +9 -0
@@ -25,4 +25,13 @@ config TYPEC_TCPM
 	  The Type-C Port Controller Manager provides a USB PD and USB Type-C
 	  state machine for use with Type-C Port Controllers.
 
+if TYPEC_TCPM
+
+config TYPEC_TCPCI
+	tristate "Type-C Port Controller Interface driver"
+	help
+	  Type-C Port Controller driver for TCPCI-compliant controller.
+
+endif
+
 endmenu
drivers/usb/typec/Makefile index renamed from drivers/usb/typec/Makefile +0 -0

bbe45721cf52..7dbaf8c3911d 100644

drivers/usb/typec/Makefile +1 -0
@@ -1,3 +1,4 @@
 obj-$(CONFIG_TYPEC)		+= typec.o
 obj-$(CONFIG_TYPEC_WCOVE)	+= typec_wcove.o
 obj-$(CONFIG_TYPEC_TCPM)	+= tcpm.o
+obj-$(CONFIG_TYPEC_TCPCI)	+= tcpci.o
drivers/usb/typec/tcpci.c new renamed from drivers/usb/typec/tcpci.c +0 -0

file mode 100644 index 000000000000..af338218a1f3

drivers/usb/typec/tcpci.c new file +165 -0
@@ -0,0 +1,487 @@
+/*
+ * Copyright 2015-2016 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * USB Type-C Port Controller Interface.
+ */
+
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/regmap.h>
+#include <linux/usb/pd.h>
+#include <linux/usb/typec.h>
+
+#include "tcpci.h"
+#include "tcpm.h"
+
+#define PD_RETRY_COUNT 3
+
+struct tcpci {
+	struct device *dev;
+	struct i2c_client *client;
+
+	struct tcpm_port *port;
+
+	struct regmap *regmap;
+
+	bool controls_vbus;
+
+	struct tcpc_dev tcpc;
+};
+
+static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc) {
+	return container_of(tcpc, struct tcpci, tcpc); }
+
+static int tcpci_read16(struct tcpci *tcpci, unsigned int reg,
+			unsigned int *val)
+{
+	return regmap_raw_read(tcpci->regmap, reg, val, sizeof(u16)); }
+
+static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16
+val) {
+	return regmap_raw_write(tcpci->regmap, reg, &val, sizeof(u16)); }
+
+static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
+{
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	unsigned int reg;
+	int ret;
+
+	switch (cc) {
+	case TYPEC_CC_RA:
+		reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
+			(TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC2_SHIFT);
+		break;
+	case TYPEC_CC_RD:
+		reg = (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
+			(TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
+		break;
+	case TYPEC_CC_RP_DEF:
+		reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+			(TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+			(TCPC_ROLE_CTRL_RP_VAL_DEF <<
+			 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+		break;
+	case TYPEC_CC_RP_1_5:
+		reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+			(TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+			(TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+			 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+		break;
+	case TYPEC_CC_RP_3_0:
+		reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+			(TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+			(TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+			 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+		break;
+	case TYPEC_CC_OPEN:
+	default:
+		reg = (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT) |
+			(TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
+		break;
+	}
+
+	ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool
+sink) {
+	switch (cc) {
+	case 0x1:
+		return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RA;
+	case 0x2:
+		return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RD;
+	case 0x3:
+		if (sink)
+			return TYPEC_CC_RP_3_0;
+	case 0x0:
+	default:
+		return TYPEC_CC_OPEN;
+	}
+}
+
+static int tcpci_get_cc(struct tcpc_dev *tcpc,
+			enum typec_cc_status *cc1, enum typec_cc_status *cc2) {
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	unsigned int reg;
+	int ret;
+
+	ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, &reg);
+	if (ret < 0)
+		return ret;
+
+	*cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
+				 TCPC_CC_STATUS_CC1_MASK,
+				 reg & TCPC_CC_STATUS_TERM);
+	*cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
+				 TCPC_CC_STATUS_CC2_MASK,
+				 reg & TCPC_CC_STATUS_TERM);
+
+	return 0;
+}
+
+static int tcpci_set_polarity(struct tcpc_dev *tcpc,
+			      enum typec_cc_polarity polarity) {
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	int ret;
+
+	ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+			   (polarity == TYPEC_POLARITY_CC2) ?
+			   TCPC_TCPC_CTRL_ORIENTATION : 0);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable) {
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	int ret;
+
+	ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
+			   enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int tcpci_set_pd_header(struct tcpc_dev *tcpc, enum typec_role

role,

drivers/usb/typec/tcpci.c new file +62 -0
+			       enum typec_data_role data)
+{
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	unsigned int reg;
+	int ret;
+
+	reg = PD_REV20 << TCPC_MSG_HDR_INFO_REV_SHIFT;
+	if (role == TYPEC_SOURCE)
+		reg |= TCPC_MSG_HDR_INFO_PWR_ROLE;
+	if (data == TYPEC_HOST)
+		reg |= TCPC_MSG_HDR_INFO_DATA_ROLE;
+	ret = regmap_write(tcpci->regmap, TCPC_MSG_HDR_INFO, reg);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int tcpci_set_pd_rx(struct tcpc_dev *tcpc, bool enable) {
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	unsigned int reg = 0;
+	int ret;
+
+	if (enable)
+		reg = TCPC_RX_DETECT_SOP | TCPC_RX_DETECT_HARD_RESET;
+	ret = regmap_write(tcpci->regmap, TCPC_RX_DETECT, reg);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int tcpci_get_vbus(struct tcpc_dev *tcpc) {
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	unsigned int reg;
+	int ret;
+
+	ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+	if (ret < 0)
+		return ret;
+
+	return !!(reg & TCPC_POWER_STATUS_VBUS_PRES); }
+
+static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool
+sink) {
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	int ret;
+
+	if (source) {
+		ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+				   TCPC_CMD_SRC_VBUS_DEFAULT);
+		if (ret < 0)
+			return ret;
+	} else {
+		ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+				   TCPC_CMD_DISABLE_SRC_VBUS);
+		if (ret < 0)
+			return ret;
+	}
+
+	if (sink) {
+		ret = regmap_write(tcpci->regmap, TCPC_COMMAND,

drivers/usb/typec/tcpci.c line 247

Quoted 10 lines
drivers/usb/typec/tcpci.c new file +10 -0
+				   TCPC_CMD_SINK_VBUS);
+		if (ret < 0)
+			return ret;
+	} else {
+		ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+				   TCPC_CMD_DISABLE_SINK_VBUS);
+		if (ret < 0)
+			return ret;
+	}
+

Per tcpc spec, disable should be firstly done before enable, so should it be done like this?
if(!source) {};
if(!sink) {};
if(source) {};
if(sink) {};

drivers/usb/typec/tcpci.c line 260

Quoted 13 lines
drivers/usb/typec/tcpci.c new file +13 -0
+	return 0;
+}
+
+static int tcpci_pd_transmit(struct tcpc_dev *tcpc,
+			     enum tcpm_transmit_type type,
+			     const struct pd_message *msg)
+{
+	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	unsigned int reg, cnt, header;
+	int ret;
+
+	cnt = msg ? pd_header_cnt(msg->header) * 4 : 0;
+	ret = regmap_write(tcpci->regmap, TCPC_TX_BYTE_CNT, cnt);

Here miss the 2 bytes' header, so cnt should be cnt += 2;

Tcpc 4.4.13
TRANSMIT_BYTE_COUNT:
The number of bytes the TCPM will write
This is the number of bytes in the TX_BUFFER_DATA_OBJECTS plus two (for the TX_BUF_HEADER)

Quoted 24 lines
drivers/usb/typec/tcpci.c new file +24 -0
+	if (ret < 0)
+		return ret;
+
+	header = msg ? msg->header : 0;
+	ret = tcpci_write16(tcpci, TCPC_TX_HDR, header);
+	if (ret < 0)
+		return ret;
+
+	if (cnt > 0) {
+		ret = regmap_raw_write(tcpci->regmap, TCPC_TX_DATA,
+				       &msg->payload, cnt);
+		if (ret < 0)
+			return ret;
+	}
+
+	reg = (PD_RETRY_COUNT << TCPC_TRANSMIT_RETRY_SHIFT) |
+		(type << TCPC_TRANSMIT_TYPE_SHIFT);
+	ret = regmap_write(tcpci->regmap, TCPC_TRANSMIT, reg);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+

...

Quoted 23 lines
drivers/usb/typec/tcpci.c new file +23 -0
+
+static int tcpci_probe(struct i2c_client *client,
+		       const struct i2c_device_id *i2c_id) {
+	struct tcpci *tcpci;
+	int err;
+
+	tcpci = devm_kzalloc(&client->dev, sizeof(*tcpci), GFP_KERNEL);
+	if (!tcpci)
+		return -ENOMEM;
+
+	tcpci->client = client;
+	tcpci->dev = &client->dev;
+	i2c_set_clientdata(client, tcpci);
+	tcpci->regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config);
+	if (IS_ERR(tcpci->regmap))
+		return PTR_ERR(tcpci->regmap);
+
+	tcpci->tcpc.init = tcpci_init;
+	tcpci->tcpc.get_vbus = tcpci_get_vbus;
+	tcpci->tcpc.set_vbus = tcpci_set_vbus;
+	tcpci->tcpc.set_cc = tcpci_set_cc;
+	tcpci->tcpc.get_cc = tcpci_get_cc;
+	tcpci->tcpc.set_polarity = tcpci_set_polarity;

drivers/usb/typec/tcpci.c line 438

Quoted 10 lines
drivers/usb/typec/tcpci.c new file +10 -0
+	tcpci->tcpc.set_vconn = tcpci_set_vconn;
+
+	tcpci->tcpc.set_pd_rx = tcpci_set_pd_rx;
+	tcpci->tcpc.set_pd_header = tcpci_set_pd_header;
+	tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
+
+	err = tcpci_parse_config(tcpci);
+	if (err < 0)
+		return err;
+

As the alert mask reg is defaultly to be unmasked after power on, It's better clear and mask all before request irq.

drivers/usb/typec/tcpci.c line 440

Quoted 2 lines
drivers/usb/typec/tcpci.c new file +2 -0
+	err = devm_request_threaded_irq(tcpci->dev, client->irq, NULL,
+					tcpci_irq, IRQF_TRIGGER_FALLING,

IRQF_ONESHOT is required, and do we need the irq trigger to be IRQF_TRIGGER_LOW?

Li Jun

#19 lore raw

Re: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

Quoted 304 lines · Jun Li

Hi Guenter,

Quoted 302 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

The port controller interface driver interconnects the Type-C Port Manager with a Type-C Port Controller Interface (TCPCI) compliant port controller.

---
v3:
- No change

v2:
- Adjust to modified callbacks into tcpm code

 drivers/usb/typec/Kconfig  |   9 +
 drivers/usb/typec/Makefile |   1 +
 drivers/usb/typec/tcpci.c  | 487
+++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpci.h  | 133 +++++++++++++
 4 files changed, 630 insertions(+)
 create mode 100644 drivers/usb/typec/tcpci.c  create mode 100644

drivers/usb/typec/tcpci.h

drivers/usb/typec/Kconfig index renamed from drivers/usb/typec/Kconfig +0 -0

113bb1b3589c..a92c9d1a3e00 100644

drivers/usb/typec/Kconfig +9 -0
@@ -25,4 +25,13 @@ config TYPEC_TCPM
        The Type-C Port Controller Manager provides a USB PD and USB Type-C
        state machine for use with Type-C Port Controllers.
 
+if TYPEC_TCPM
+
+config TYPEC_TCPCI
+     tristate "Type-C Port Controller Interface driver"
+     help
+       Type-C Port Controller driver for TCPCI-compliant controller.
+
+endif
+
 endmenu
drivers/usb/typec/Makefile index renamed from drivers/usb/typec/Makefile +0 -0

bbe45721cf52..7dbaf8c3911d 100644

drivers/usb/typec/Makefile +1 -0
@@ -1,3 +1,4 @@
 obj-$(CONFIG_TYPEC)          += typec.o
 obj-$(CONFIG_TYPEC_WCOVE)    += typec_wcove.o
 obj-$(CONFIG_TYPEC_TCPM)     += tcpm.o
+obj-$(CONFIG_TYPEC_TCPCI)    += tcpci.o
drivers/usb/typec/tcpci.c new renamed from drivers/usb/typec/tcpci.c +0 -0

file mode 100644 index 000000000000..af338218a1f3

drivers/usb/typec/tcpci.c new file +165 -0
@@ -0,0 +1,487 @@
+/*
+ * Copyright 2015-2016 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * USB Type-C Port Controller Interface.
+ */
+
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/regmap.h>
+#include <linux/usb/pd.h>
+#include <linux/usb/typec.h>
+
+#include "tcpci.h"
+#include "tcpm.h"
+
+#define PD_RETRY_COUNT 3
+
+struct tcpci {
+     struct device *dev;
+     struct i2c_client *client;
+
+     struct tcpm_port *port;
+
+     struct regmap *regmap;
+
+     bool controls_vbus;
+
+     struct tcpc_dev tcpc;
+};
+
+static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc) {
+     return container_of(tcpc, struct tcpci, tcpc); }
+
+static int tcpci_read16(struct tcpci *tcpci, unsigned int reg,
+                     unsigned int *val)
+{
+     return regmap_raw_read(tcpci->regmap, reg, val, sizeof(u16)); }
+
+static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16
+val) {
+     return regmap_raw_write(tcpci->regmap, reg, &val, sizeof(u16)); }
+
+static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
+{
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     switch (cc) {
+     case TYPEC_CC_RA:
+             reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                     (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC2_SHIFT);
+             break;
+     case TYPEC_CC_RD:
+             reg = (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                     (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
+             break;
+     case TYPEC_CC_RP_DEF:
+             reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                     (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+                     (TCPC_ROLE_CTRL_RP_VAL_DEF <<
+                      TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+             break;
+     case TYPEC_CC_RP_1_5:
+             reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                     (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+                     (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+                      TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+             break;
+     case TYPEC_CC_RP_3_0:
+             reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                     (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
+                     (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+                      TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+             break;
+     case TYPEC_CC_OPEN:
+     default:
+             reg = (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT) |
+                     (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
+             break;
+     }
+
+     ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool
+sink) {
+     switch (cc) {
+     case 0x1:
+             return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RA;
+     case 0x2:
+             return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RD;
+     case 0x3:
+             if (sink)
+                     return TYPEC_CC_RP_3_0;
+     case 0x0:
+     default:
+             return TYPEC_CC_OPEN;
+     }
+}
+
+static int tcpci_get_cc(struct tcpc_dev *tcpc,
+                     enum typec_cc_status *cc1, enum typec_cc_status *cc2) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, &reg);
+     if (ret < 0)
+             return ret;
+
+     *cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
+                              TCPC_CC_STATUS_CC1_MASK,
+                              reg & TCPC_CC_STATUS_TERM);
+     *cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
+                              TCPC_CC_STATUS_CC2_MASK,
+                              reg & TCPC_CC_STATUS_TERM);
+
+     return 0;
+}
+
+static int tcpci_set_polarity(struct tcpc_dev *tcpc,
+                           enum typec_cc_polarity polarity) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     int ret;
+
+     ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+                        (polarity == TYPEC_POLARITY_CC2) ?
+                        TCPC_TCPC_CTRL_ORIENTATION : 0);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     int ret;
+
+     ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
+                        enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_set_pd_header(struct tcpc_dev *tcpc, enum typec_role

role,

drivers/usb/typec/tcpci.c new file +71 -0
+                            enum typec_data_role data)
+{
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     reg = PD_REV20 << TCPC_MSG_HDR_INFO_REV_SHIFT;
+     if (role == TYPEC_SOURCE)
+             reg |= TCPC_MSG_HDR_INFO_PWR_ROLE;
+     if (data == TYPEC_HOST)
+             reg |= TCPC_MSG_HDR_INFO_DATA_ROLE;
+     ret = regmap_write(tcpci->regmap, TCPC_MSG_HDR_INFO, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_set_pd_rx(struct tcpc_dev *tcpc, bool enable) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg = 0;
+     int ret;
+
+     if (enable)
+             reg = TCPC_RX_DETECT_SOP | TCPC_RX_DETECT_HARD_RESET;
+     ret = regmap_write(tcpci->regmap, TCPC_RX_DETECT, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_get_vbus(struct tcpc_dev *tcpc) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+     if (ret < 0)
+             return ret;
+
+     return !!(reg & TCPC_POWER_STATUS_VBUS_PRES); }
+
+static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool
+sink) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     int ret;
+
+     if (source) {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_SRC_VBUS_DEFAULT);
+             if (ret < 0)
+                     return ret;
+     } else {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_DISABLE_SRC_VBUS);
+             if (ret < 0)
+                     return ret;
+     }
+
+     if (sink) {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_SINK_VBUS);
+             if (ret < 0)
+                     return ret;
+     } else {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_DISABLE_SINK_VBUS);
+             if (ret < 0)
+                     return ret;
+     }
Quoted 9 lines
Quoted 1 line

+

Per tcpc spec, disable should be firstly done before enable, so should it be done like this?
if(!source) {};
if(!sink) {};
if(source) {};
if(sink) {};

Makes sense. Changed accordingly.

Quoted 16 lines

drivers/usb/typec/tcpci.c line 260

Quoted 13 lines
drivers/usb/typec/tcpci.c new file +13 -0
+     return 0;
+}
+
+static int tcpci_pd_transmit(struct tcpc_dev *tcpc,
+                          enum tcpm_transmit_type type,
+                          const struct pd_message *msg)
+{
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg, cnt, header;
+     int ret;
+
+     cnt = msg ? pd_header_cnt(msg->header) * 4 : 0;
+     ret = regmap_write(tcpci->regmap, TCPC_TX_BYTE_CNT, cnt);

Here miss the 2 bytes' header, so cnt should be cnt += 2;

Quoted 6 lines

Tcpc 4.4.13
TRANSMIT_BYTE_COUNT:
The number of bytes the TCPM will write
This is the number of bytes in the TX_BUFFER_DATA_OBJECTS plus two (for the TX_BUF_HEADER)

Yes, you are correct. Fixed.

Quoted 55 lines
Quoted 24 lines
drivers/usb/typec/tcpci.c new file +24 -0
+     if (ret < 0)
+             return ret;
+
+     header = msg ? msg->header : 0;
+     ret = tcpci_write16(tcpci, TCPC_TX_HDR, header);
+     if (ret < 0)
+             return ret;
+
+     if (cnt > 0) {
+             ret = regmap_raw_write(tcpci->regmap, TCPC_TX_DATA,
+                                    &msg->payload, cnt);
+             if (ret < 0)
+                     return ret;
+     }
+
+     reg = (PD_RETRY_COUNT << TCPC_TRANSMIT_RETRY_SHIFT) |
+             (type << TCPC_TRANSMIT_TYPE_SHIFT);
+     ret = regmap_write(tcpci->regmap, TCPC_TRANSMIT, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+

...

Quoted 28 lines
drivers/usb/typec/tcpci.c new file +28 -0
+
+static int tcpci_probe(struct i2c_client *client,
+                    const struct i2c_device_id *i2c_id) {
+     struct tcpci *tcpci;
+     int err;
+
+     tcpci = devm_kzalloc(&client->dev, sizeof(*tcpci), GFP_KERNEL);
+     if (!tcpci)
+             return -ENOMEM;
+
+     tcpci->client = client;
+     tcpci->dev = &client->dev;
+     i2c_set_clientdata(client, tcpci);
+     tcpci->regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config);
+     if (IS_ERR(tcpci->regmap))
+             return PTR_ERR(tcpci->regmap);
+
+     tcpci->tcpc.init = tcpci_init;
+     tcpci->tcpc.get_vbus = tcpci_get_vbus;
+     tcpci->tcpc.set_vbus = tcpci_set_vbus;
+     tcpci->tcpc.set_cc = tcpci_set_cc;
+     tcpci->tcpc.get_cc = tcpci_get_cc;
+     tcpci->tcpc.set_polarity = tcpci_set_polarity;
+     tcpci->tcpc.set_vconn = tcpci_set_vconn;
+
+     tcpci->tcpc.set_pd_rx = tcpci_set_pd_rx;
+     tcpci->tcpc.set_pd_header = tcpci_set_pd_header;
+     tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
Quoted 9 lines

drivers/usb/typec/tcpci.c line 438

Quoted 5 lines
drivers/usb/typec/tcpci.c new file +5 -0
+
+     err = tcpci_parse_config(tcpci);
+     if (err < 0)
+             return err;
+

As the alert mask reg is defaultly to be unmasked after power on, It's better clear and mask all before request irq.

Agreed and done.

Quoted 6 lines

drivers/usb/typec/tcpci.c line 440

Quoted 2 lines
drivers/usb/typec/tcpci.c new file +2 -0
+     err = devm_request_threaded_irq(tcpci->dev, client->irq, NULL,
+                                     tcpci_irq, IRQF_TRIGGER_FALLING,

IRQF_ONESHOT is required, and do we need the irq trigger to be IRQF_TRIGGER_LOW?

Good question; I don't really know. IRQF_TRIGGER_FALLING definitely seems to be wrong (and I don't recall where I got it from). Not sure about IRQF_TRIGGER_LOW. Can you test on real hardware, by any chance ?

Thanks,
Guenter

Quoted 1 line

Li Jun

#20 lore raw

RE: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

Hi,

Quoted 441 lines

-----Original Message-----
From: Guenter Roeck [mailto:[email protected]]
Sent: Saturday, October 01, 2016 2:45 AM
To: Jun Li <[email protected]>
Cc: Guenter Roeck <[email protected]>; Felipe Balbi <[email protected]>; Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

Quoted 17 lines · Jun Li

Hi Guenter,

Quoted 15 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Guenter Roeck
Sent: Wednesday, August 24, 2016 5:11 AM
To: Felipe Balbi <[email protected]>
Cc: Chandra Sekhar Anagani <[email protected]>; Bruce Ashfield <[email protected]>; Bin Gao <[email protected]>; Pranav Tipnis <[email protected]>; Heikki Krogerus <[email protected]>; [email protected]; linux- [email protected]; Guenter Roeck <[email protected]>
Subject: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

The port controller interface driver interconnects the Type-C Port Manager with a Type-C Port Controller Interface (TCPCI) compliant port

controller.

Quoted 18 lines
Quoted 18 lines

---
v3:
- No change

v2:
- Adjust to modified callbacks into tcpm code

 drivers/usb/typec/Kconfig  |   9 +
 drivers/usb/typec/Makefile |   1 +
 drivers/usb/typec/tcpci.c  | 487
+++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/typec/tcpci.h  | 133 +++++++++++++
 4 files changed, 630 insertions(+)
 create mode 100644 drivers/usb/typec/tcpci.c  create mode 100644

drivers/usb/typec/tcpci.h

Quoted 7 lines

drivers/usb/typec/Kconfig config TYPEC_TCPM · line 25

Quoted 7 lines
drivers/usb/typec/Kconfig +0 -0

index
113bb1b3589c..a92c9d1a3e00 100644

drivers/usb/typec/Kconfig +0 -0
@@ -25,4 +25,13 @@ config TYPEC_TCPM
        The Type-C Port Controller Manager provides a USB PD and USB

Type-C

Quoted 84 lines
Quoted 84 lines
drivers/usb/typec/Kconfig +9 -0
        state machine for use with Type-C Port Controllers.
 
+if TYPEC_TCPM
+
+config TYPEC_TCPCI
+     tristate "Type-C Port Controller Interface driver"
+     help
+       Type-C Port Controller driver for TCPCI-compliant controller.
+
+endif
+
 endmenu
drivers/usb/typec/Makefile +1 -0
index bbe45721cf52..7dbaf8c3911d 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_TYPEC)          += typec.o
 obj-$(CONFIG_TYPEC_WCOVE)    += typec_wcove.o
 obj-$(CONFIG_TYPEC_TCPM)     += tcpm.o
+obj-$(CONFIG_TYPEC_TCPCI)    += tcpci.o
drivers/usb/typec/tcpci.c new file +58 -0
new file mode 100644 index 000000000000..af338218a1f3
@@ -0,0 +1,487 @@
+/*
+ * Copyright 2015-2016 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or
+modify
+ * it under the terms of the GNU General Public License as published
+by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * USB Type-C Port Controller Interface.
+ */
+
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/regmap.h>
+#include <linux/usb/pd.h>
+#include <linux/usb/typec.h>
+
+#include "tcpci.h"
+#include "tcpm.h"
+
+#define PD_RETRY_COUNT 3
+
+struct tcpci {
+     struct device *dev;
+     struct i2c_client *client;
+
+     struct tcpm_port *port;
+
+     struct regmap *regmap;
+
+     bool controls_vbus;
+
+     struct tcpc_dev tcpc;
+};
+
+static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc) {
+     return container_of(tcpc, struct tcpci, tcpc); }
+
+static int tcpci_read16(struct tcpci *tcpci, unsigned int reg,
+                     unsigned int *val) {
+     return regmap_raw_read(tcpci->regmap, reg, val, sizeof(u16)); }
+
+static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16
+val) {
+     return regmap_raw_write(tcpci->regmap, reg, &val, sizeof(u16));
+}
+
+static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status
Quoted 8 lines

drivers/usb/typec/tcpci.c

Quoted 8 lines
drivers/usb/typec/tcpci.c new file +8 -0
+cc) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     switch (cc) {
+     case TYPEC_CC_RA:
+             reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT)

|

Quoted 1 line

drivers/usb/typec/tcpci.c line 67

Quoted 1 line
drivers/usb/typec/tcpci.c new file +1 -0
+                     (TCPC_ROLE_CTRL_CC_RA <<

TCPC_ROLE_CTRL_CC2_SHIFT);

Quoted 3 lines

drivers/usb/typec/tcpci.c

Quoted 3 lines
drivers/usb/typec/tcpci.c new file +3 -0
+             break;
+     case TYPEC_CC_RD:
+             reg = (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT)

|

Quoted 1 line

drivers/usb/typec/tcpci.c line 71

Quoted 1 line
drivers/usb/typec/tcpci.c new file +1 -0
+                     (TCPC_ROLE_CTRL_CC_RD <<

TCPC_ROLE_CTRL_CC2_SHIFT);

Quoted 3 lines

drivers/usb/typec/tcpci.c

Quoted 3 lines
drivers/usb/typec/tcpci.c new file +3 -0
+             break;
+     case TYPEC_CC_RP_DEF:
+             reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT)

|

Quoted 1 line

drivers/usb/typec/tcpci.c line 75

Quoted 1 line
drivers/usb/typec/tcpci.c new file +1 -0
+                     (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT)

|

Quoted 5 lines

drivers/usb/typec/tcpci.c

Quoted 5 lines
drivers/usb/typec/tcpci.c new file +5 -0
+                     (TCPC_ROLE_CTRL_RP_VAL_DEF <<
+                      TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+             break;
+     case TYPEC_CC_RP_1_5:
+             reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT)

|

Quoted 1 line

drivers/usb/typec/tcpci.c line 81

Quoted 1 line
drivers/usb/typec/tcpci.c new file +1 -0
+                     (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT)

|

Quoted 5 lines

drivers/usb/typec/tcpci.c

Quoted 5 lines
drivers/usb/typec/tcpci.c new file +5 -0
+                     (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+                      TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+             break;
+     case TYPEC_CC_RP_3_0:
+             reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT)

|

Quoted 1 line

drivers/usb/typec/tcpci.c line 87

Quoted 1 line
drivers/usb/typec/tcpci.c new file +1 -0
+                     (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT)

|

Quoted 6 lines

drivers/usb/typec/tcpci.c

Quoted 6 lines
drivers/usb/typec/tcpci.c new file +6 -0
+                     (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
+                      TCPC_ROLE_CTRL_RP_VAL_SHIFT);
+             break;
+     case TYPEC_CC_OPEN:
+     default:
+             reg = (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT)

|

Quoted 1 line

drivers/usb/typec/tcpci.c line 94

Quoted 1 line
drivers/usb/typec/tcpci.c new file +1 -0
+                     (TCPC_ROLE_CTRL_CC_OPEN <<

TCPC_ROLE_CTRL_CC2_SHIFT);

Quoted 20 lines
Quoted 20 lines
drivers/usb/typec/tcpci.c new file +20 -0
+             break;
+     }
+
+     ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool
+sink) {
+     switch (cc) {
+     case 0x1:
+             return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RA;
+     case 0x2:
+             return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RD;
+     case 0x3:
+             if (sink)
+                     return TYPEC_CC_RP_3_0;
Quoted 8 lines

drivers/usb/typec/tcpci.c

Quoted 8 lines
drivers/usb/typec/tcpci.c new file +8 -0
+     case 0x0:
+     default:
+             return TYPEC_CC_OPEN;
+     }
+}
+
+static int tcpci_get_cc(struct tcpc_dev *tcpc,
+                     enum typec_cc_status *cc1, enum typec_cc_status

*cc2) {

Quoted 117 lines
Quoted 117 lines
drivers/usb/typec/tcpci.c new file +46 -0
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, &reg);
+     if (ret < 0)
+             return ret;
+
+     *cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
+                              TCPC_CC_STATUS_CC1_MASK,
+                              reg & TCPC_CC_STATUS_TERM);
+     *cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
+                              TCPC_CC_STATUS_CC2_MASK,
+                              reg & TCPC_CC_STATUS_TERM);
+
+     return 0;
+}
+
+static int tcpci_set_polarity(struct tcpc_dev *tcpc,
+                           enum typec_cc_polarity polarity) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     int ret;
+
+     ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
+                        (polarity == TYPEC_POLARITY_CC2) ?
+                        TCPC_TCPC_CTRL_ORIENTATION : 0);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     int ret;
+
+     ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
+                        enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_set_pd_header(struct tcpc_dev *tcpc, enum
+typec_role

role,

drivers/usb/typec/tcpci.c new file +70 -0
+                            enum typec_data_role data) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     reg = PD_REV20 << TCPC_MSG_HDR_INFO_REV_SHIFT;
+     if (role == TYPEC_SOURCE)
+             reg |= TCPC_MSG_HDR_INFO_PWR_ROLE;
+     if (data == TYPEC_HOST)
+             reg |= TCPC_MSG_HDR_INFO_DATA_ROLE;
+     ret = regmap_write(tcpci->regmap, TCPC_MSG_HDR_INFO, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_set_pd_rx(struct tcpc_dev *tcpc, bool enable) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg = 0;
+     int ret;
+
+     if (enable)
+             reg = TCPC_RX_DETECT_SOP | TCPC_RX_DETECT_HARD_RESET;
+     ret = regmap_write(tcpci->regmap, TCPC_RX_DETECT, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+
+static int tcpci_get_vbus(struct tcpc_dev *tcpc) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg;
+     int ret;
+
+     ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+     if (ret < 0)
+             return ret;
+
+     return !!(reg & TCPC_POWER_STATUS_VBUS_PRES); }
+
+static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool
+sink) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     int ret;
+
+     if (source) {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_SRC_VBUS_DEFAULT);
+             if (ret < 0)
+                     return ret;
+     } else {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_DISABLE_SRC_VBUS);
+             if (ret < 0)
+                     return ret;
+     }
+
+     if (sink) {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_SINK_VBUS);
+             if (ret < 0)
+                     return ret;
+     } else {
+             ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
+                                TCPC_CMD_DISABLE_SINK_VBUS);
+             if (ret < 0)
+                     return ret;
+     }
Quoted 9 lines
Quoted 1 line

+

Per tcpc spec, disable should be firstly done before enable, so should it be done like this?
if(!source) {};
if(!sink) {};
if(source) {};
if(sink) {};

Makes sense. Changed accordingly.

Quoted 15 lines

drivers/usb/typec/tcpci.c line 260

Quoted 12 lines
drivers/usb/typec/tcpci.c new file +12 -0
+     return 0;
+}
+
+static int tcpci_pd_transmit(struct tcpc_dev *tcpc,
+                          enum tcpm_transmit_type type,
+                          const struct pd_message *msg) {
+     struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+     unsigned int reg, cnt, header;
+     int ret;
+
+     cnt = msg ? pd_header_cnt(msg->header) * 4 : 0;
+     ret = regmap_write(tcpci->regmap, TCPC_TX_BYTE_CNT, cnt);

Here miss the 2 bytes' header, so cnt should be cnt += 2;

Quoted 5 lines

Tcpc 4.4.13
TRANSMIT_BYTE_COUNT:
The number of bytes the TCPM will write This is the number of bytes in the TX_BUFFER_DATA_OBJECTS plus two (for the TX_BUF_HEADER)

Yes, you are correct. Fixed.

Quoted 27 lines
Quoted 24 lines
drivers/usb/typec/tcpci.c new file +24 -0
+     if (ret < 0)
+             return ret;
+
+     header = msg ? msg->header : 0;
+     ret = tcpci_write16(tcpci, TCPC_TX_HDR, header);
+     if (ret < 0)
+             return ret;
+
+     if (cnt > 0) {
+             ret = regmap_raw_write(tcpci->regmap, TCPC_TX_DATA,
+                                    &msg->payload, cnt);
+             if (ret < 0)
+                     return ret;
+     }
+
+     reg = (PD_RETRY_COUNT << TCPC_TRANSMIT_RETRY_SHIFT) |
+             (type << TCPC_TRANSMIT_TYPE_SHIFT);
+     ret = regmap_write(tcpci->regmap, TCPC_TRANSMIT, reg);
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+

...

Quoted 14 lines
Quoted 4 lines
drivers/usb/typec/tcpci.c new file +4 -0
+
+static int tcpci_probe(struct i2c_client *client,
+                    const struct i2c_device_id *i2c_id) {
+     struct tcpci *tcpci;

drivers/usb/typec/tcpci.c

Quoted 10 lines
drivers/usb/typec/tcpci.c new file +10 -0
+     int err;
+
+     tcpci = devm_kzalloc(&client->dev, sizeof(*tcpci), GFP_KERNEL);
+     if (!tcpci)
+             return -ENOMEM;
+
+     tcpci->client = client;
+     tcpci->dev = &client->dev;
+     i2c_set_clientdata(client, tcpci);
+     tcpci->regmap = devm_regmap_init_i2c(client,

&tcpci_regmap_config);

Quoted 14 lines
Quoted 14 lines
drivers/usb/typec/tcpci.c new file +14 -0
+     if (IS_ERR(tcpci->regmap))
+             return PTR_ERR(tcpci->regmap);
+
+     tcpci->tcpc.init = tcpci_init;
+     tcpci->tcpc.get_vbus = tcpci_get_vbus;
+     tcpci->tcpc.set_vbus = tcpci_set_vbus;
+     tcpci->tcpc.set_cc = tcpci_set_cc;
+     tcpci->tcpc.get_cc = tcpci_get_cc;
+     tcpci->tcpc.set_polarity = tcpci_set_polarity;
+     tcpci->tcpc.set_vconn = tcpci_set_vconn;
+
+     tcpci->tcpc.set_pd_rx = tcpci_set_pd_rx;
+     tcpci->tcpc.set_pd_header = tcpci_set_pd_header;
+     tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
Quoted 9 lines

drivers/usb/typec/tcpci.c line 438

Quoted 5 lines
drivers/usb/typec/tcpci.c new file +5 -0
+
+     err = tcpci_parse_config(tcpci);
+     if (err < 0)
+             return err;
+

As the alert mask reg is defaultly to be unmasked after power on, It's better clear and mask all before request irq.

Agreed and done.

Quoted 4 lines
Quoted 3 lines
drivers/usb/typec/tcpci.c new file +3 -0
+     err = devm_request_threaded_irq(tcpci->dev, client->irq, NULL,
+                                     tcpci_irq,
+ IRQF_TRIGGER_FALLING,
Quoted 8 lines
Quoted 3 lines

IRQF_ONESHOT is required, and do we need the irq trigger to be IRQF_TRIGGER_LOW?

Good question; I don't really know. IRQF_TRIGGER_FALLING definitely seems to be wrong (and I don't recall where I got it from). Not sure about IRQF_TRIGGER_LOW. Can you test on real hardware, by any chance ?

I tested on real hardware, IRQF_TRIGGER_LOW can work fine, so I think the right flag should be IRQF_TRIGGER_LOW | IRQF_ONESHOT.

Li Jun

Quoted 5 lines

Thanks,
Guenter

Quoted 1 line

Li Jun

#21 lore raw

Re: [RFC PATCH v3 2/2] usb: typec: Type-C Port Controller Interface driver (tcpci)

On Fri, Sep 30, 2016 at 5:12 PM, Jun Li <[email protected]> wrote:
[ ... ]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+     err = devm_request_threaded_irq(tcpci->dev, client->irq, NULL,
+                                     tcpci_irq,
+ IRQF_TRIGGER_FALLING,

IRQF_ONESHOT is required, and do we need the irq trigger to be IRQF_TRIGGER_LOW?

Quoted 8 lines
Quoted 4 lines

Good question; I don't really know. IRQF_TRIGGER_FALLING definitely seems to be wrong (and I don't recall where I got it from). Not sure about IRQF_TRIGGER_LOW. Can you test on real hardware, by any chance ?

I tested on real hardware, IRQF_TRIGGER_LOW can work fine, so I think the right flag should be IRQF_TRIGGER_LOW | IRQF_ONESHOT.

I had wondered if the trigger should be part of the devicetree file. However, TCPCI specifies (chapter 4, bullet point 6), that "The TCPC shall have an open drain output, active low Alert# Pin. This pin is used to indicate a change of state, where Alert# pin is asserted when a ny Alert Bits are set."

Given that, I agree, and I'll add IRQF_TRIGGER_LOW.

Thanks,
Guenter

maintainer
#22 lore raw
In reply to Guenter Roeck

Hi Guenter,

Quoted 30 lines · Guenter Roeck

The following series of patches implements a USB Type-C Port Manager using the pending USB Type-C class code as basis. The code is still WIP, but I think it is important to get feedback from the community at this point.

There are two patches in the series. The first patch implements the Type-C Port Manager state machine. The forth patch is an interface between the Type-C Port Manager and a TCPCI (Type-C Port Controller Interface) compliant USB Type-C Port Controller.

Patch 2/2 (the interface to a TCPCI compliant chip) is currently untested since I don't have the necessary hardware available. The port manager code was tested connecting to an Embedded Controller on a Chromebook, bypassing the Port Manager implementation in the EC.

Both Source and Sink operation was tested with various Type-C chargers, docks, and connectors. Alternate mode support is partially implemented (Alternate mode support is requested from the partner), but alternate modes are not actually selected. Implementing this will require more thought, since the actual alternate mode support has to be implemented elsewhere, such as in a dedicated Phy driver. It should be possible to implement the interface between phy driver and Type-C Port Controller driver using extcon, but I have not further explored the possibilities, and other options might be possible and/or better.

v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes while the state machine is in a transient change (waiting for a timeout)
- Update current limit after CC voltage level changes on a port which is not PD capable.
- Applies to v6 of "USB Type-C Connector class" patch series.

Quoted 14 lines

v2:
- Class code no longer uses locking, so the patch to remove it is no longer necessary.
- tcpm: Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- tcpm: All PD messages are little endian; convert to and from CPU endianness.
- tcpm: Avoid comparisons against NULL.
- tcpm: Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- tcpm/tcpc: Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- tcpm/tcpc: Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.
- Applies to v5 of "USB Type-C Connector class" patch series.

I have not reviewed these yet completely, but so far the series looks really good. Nice job!

There are a lot of things that look generic and not tied to TCPCs, for example the USB PD message handling. Couldn't those already be separated from TCPM code or made otherwise available for non-TCPC PHYs? The struct tcpc_dev looks to me like it would be usable for also non-TCPC PHYs as is and enough for most cases.

Thanks,

Signature · 1 line
heikki
#23 lore raw

Hello Heikki,

Quoted 48 lines · Heikki Krogerus

Hi Guenter,

Quoted 30 lines · Guenter Roeck

The following series of patches implements a USB Type-C Port Manager using the pending USB Type-C class code as basis. The code is still WIP, but I think it is important to get feedback from the community at this point.

There are two patches in the series. The first patch implements the Type-C Port Manager state machine. The forth patch is an interface between the Type-C Port Manager and a TCPCI (Type-C Port Controller Interface) compliant USB Type-C Port Controller.

Patch 2/2 (the interface to a TCPCI compliant chip) is currently untested since I don't have the necessary hardware available. The port manager code was tested connecting to an Embedded Controller on a Chromebook, bypassing the Port Manager implementation in the EC.

Both Source and Sink operation was tested with various Type-C chargers, docks, and connectors. Alternate mode support is partially implemented (Alternate mode support is requested from the partner), but alternate modes are not actually selected. Implementing this will require more thought, since the actual alternate mode support has to be implemented elsewhere, such as in a dedicated Phy driver. It should be possible to implement the interface between phy driver and Type-C Port Controller driver using extcon, but I have not further explored the possibilities, and other options might be possible and/or better.

v3:
- Improve TCPM state machine resiliency if there are spurious CC line changes while the state machine is in a transient change (waiting for a timeout)
- Update current limit after CC voltage level changes on a port which is not PD capable.
- Applies to v6 of "USB Type-C Connector class" patch series.

Quoted 14 lines

v2:
- Class code no longer uses locking, so the patch to remove it is no longer necessary.
- tcpm: Only update polarity if setting it was successful
If setting the CC line polarity in the driver was not successful, don't update the internal polarity state.
- tcpm: All PD messages are little endian; convert to and from CPU endianness.
- tcpm: Avoid comparisons against NULL.
- tcpm: Use u8/u16/u32 instead of uint8_t/uint16_t/uint32_t consistently.
- tcpm/tcpc: Callbacks into tcpm need to be lockless to avoid timing problems in low level drivers.
- tcpm/tcpc: Simplify callbacks; tcpm can request the current state of cc/vbus when it is ready to use it.
- Applies to v5 of "USB Type-C Connector class" patch series.

Quoted 9 lines

I have not reviewed these yet completely, but so far the series looks really good. Nice job!

There are a lot of things that look generic and not tied to TCPCs, for example the USB PD message handling. Couldn't those already be separated from TCPM code or made otherwise available for non-TCPC PHYs? The struct tcpc_dev looks to me like it would be usable for also non-TCPC PHYs as is and enough for most cases.

Makes sense, if not for anything else to reduce file size. I'll see if I can separate it.

Thanks,
Guenter