typec: tcpm: Add sink side support for PPS
Subsystems (4)
cover typec: tcpm: Add sink side support for PPS
This patch set adds sink side support for the PPS feature introduced in the USB PD 3.0 specification.
The source PPS supply is represented using the Power Supply framework to provide access and control APIs for dealing with it's operating voltage and current, and switching between a standard PDO and PPS APDO operation. During standard PDO operation the voltage and current is read-only, but for APDO PPS these are writable as well to allow for control.
It should be noted that the keepalive for PPS is not handled within TCPM. The expectation is that the external user will be required to ensure re-requests occur regularly to ensure PPS remains and the source does not hard reset.
So with all of that covered, please feel free to rip this apart as you see fit.
Note: Code changes are based on linux-next tag 'next-20171017' to pick up the move out of staging of TCPM related code.
Adam Thomson (7):
typec: tcpm: Add PD Rev 3.0 definitions to PD header
typec: tcpm: Add ADO header for Alert message handling
typec: tcpm: Add SDB header for Status message handling
typec: tcpm: Add core support for sink side PPS
power: supply: Add type for USB PD PPS chargers
typec: tcpm: Represent source supply through power_supply class
typec: tcpm: Add support for sink PPS related messages
drivers/power/supply/power_supply_sysfs.c | 2 +- drivers/usb/typec/Kconfig | 1 + drivers/usb/typec/fusb302/Kconfig | 2 +- drivers/usb/typec/fusb302/fusb302.c | 63 +-- drivers/usb/typec/tcpm.c | 816 +++++++++++++++++++++++++++++- include/linux/power_supply.h | 1 + include/linux/usb/pd.h | 162 +++++- include/linux/usb/pd_ado.h | 49 ++ include/linux/usb/pd_ext_sdb.h | 40 ++ include/linux/usb/tcpm.h | 2 +- 10 files changed, 1041 insertions(+), 97 deletions(-) create mode 100644 include/linux/usb/pd_ado.h create mode 100644 include/linux/usb/pd_ext_sdb.h
Signature · 1 line
1.9.1
RFC PATCH 1/7 typec: tcpm: Add PD Rev 3.0 definitions to PD header
This commit adds definitions for PD Rev 3.0 messages, including APDO PPS and extended message support for TCPM.
--- include/linux/usb/pd.h | 162 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 151 insertions(+), 11 deletions(-)
include/linux/usb/pd.h +151 -11
@@ -35,6 +35,13 @@ enum pd_ctrl_msg_type {PD_CTRL_WAIT =12 ,PD_CTRL_SOFT_RESET =13 ,/* 14-15 Reserved */ +PD_CTRL_NOT_SUPP =16 , +PD_CTRL_GET_SOURCE_CAP_EXT =17 , +PD_CTRL_GET_STATUS =18 , +PD_CTRL_FR_SWAP =19 , +PD_CTRL_GET_PPS_STATUS =20 , + PD_CTRL_GET_COUNTRY_CODES = 21, + /* 22-31 Reserved */ }; enum pd_data_msg_type {
@@ -43,13 +50,38 @@ enum pd_data_msg_type {PD_DATA_REQUEST =2 ,PD_DATA_BIST =3 ,PD_DATA_SINK_CAP =4 , -/* 5-14 Reserved */ +PD_DATA_BATT_STATUS =5 , +PD_DATA_ALERT =6 , +PD_DATA_GET_COUNTRY_INFO =7 , +/* 8-14 Reserved */ PD_DATA_VENDOR_DEF = 15, + /* 16-31 Reserved */ +}; + +enum pd_ext_msg_type { +/* 0 Reserved */ +PD_EXT_SOURCE_CAP_EXT =1 , +PD_EXT_STATUS =2 , +PD_EXT_GET_BATT_CAP =3 , +PD_EXT_GET_BATT_STATUS =4 , +PD_EXT_BATT_CAP =5 , +PD_EXT_GET_MANUFACTURER_INFO =6 , +PD_EXT_MANUFACTURER_INFO =7 , +PD_EXT_SECURITY_REQUEST =8 , +PD_EXT_SECURITY_RESPONSE =9 , +PD_EXT_FW_UPDATE_REQUEST =10 , +PD_EXT_FW_UPDATE_RESPONSE =11 , +PD_EXT_PPS_STATUS =12 , +PD_EXT_COUNTRY_INFO =13 , +PD_EXT_COUNTRY_CODES =14 , +/* 15-31 Reserved */ };#define PD_REV10 0x0#define PD_REV20 0x1 +#define PD_REV30 0x2 +#define PD_HEADER_EXT_HDR BIT(15)#define PD_HEADER_CNT_SHIFT 12#define PD_HEADER_CNT_MASK 0x7#define PD_HEADER_ID_SHIFT 9
@@ -59,18 +91,19 @@ enum pd_data_msg_type {#define PD_HEADER_REV_MASK 0x3#define PD_HEADER_DATA_ROLE BIT(5)#define PD_HEADER_TYPE_SHIFT 0 -#define PD_HEADER_TYPE_MASK 0xf +#define PD_HEADER_TYPE_MASK 0x1f -#define PD_HEADER (type, pwr, data, id, cnt) \ +#define PD_HEADER (type, pwr, data, id, cnt, ext_hdr) \ ((((type) & PD_HEADER_TYPE_MASK) << PD_HEADER_TYPE_SHIFT) | \ ((pwr) == TYPEC_SOURCE ? PD_HEADER_PWR_ROLE : 0) | \ ((data) == TYPEC_HOST ? PD_HEADER_DATA_ROLE : 0) | \ - (PD_REV20 << PD_HEADER_REV_SHIFT) | \ + (PD_REV30 << PD_HEADER_REV_SHIFT) | \ (((id) & PD_HEADER_ID_MASK) << PD_HEADER_ID_SHIFT) | \ - (((cnt) & PD_HEADER_CNT_MASK) << PD_HEADER_CNT_SHIFT)) + (((cnt) & PD_HEADER_CNT_MASK) << PD_HEADER_CNT_SHIFT) | \ + ((ext_hdr) ? PD_HEADER_EXT_HDR : 0))#define PD_HEADER_LE (type, pwr, data, id, cnt) \ - cpu_to_le16(PD_HEADER((type), (pwr), (data), (id), (cnt))) + cpu_to_le16(PD_HEADER((type), (pwr), (data), (id), (cnt), (0)))static inline unsigned int pd_header_cnt (u16 header) {
@@ -102,16 +135,66 @@ static inline unsigned int pd_header_msgid_le(__le16 header)return pd_header_msgid (le16_to_cpu (header)); } +#define PD_EXT_HDR_CHUNKED BIT(15) +#define PD_EXT_HDR_CHUNK_NUM_SHIFT 11 +#define PD_EXT_HDR_CHUNK_NUM_MASK 0xf +#define PD_EXT_HDR_REQ_CHUNK BIT(10) +#define PD_EXT_HDR_DATA_SIZE_SHIFT 0 +#define PD_EXT_HDR_DATA_SIZE_MASK 0x1ff + +#define PD_EXT_HDR (data_size, req_chunk, chunk_num, chunked) \ + ((((data_size) & PD_EXT_HDR_DATA_SIZE_MASK) << PD_EXT_HDR_DATA_SIZE_SHIFT) | \ + ((req_chunk) ? PD_EXT_HDR_REQ_CHUNK : 0) | \ + (((chunk_num) & PD_EXT_HDR_CHUNK_NUM_MASK) << PD_EXT_HDR_CHUNK_NUM_SHIFT) | \ + ((chunked) ? PD_EXT_HDR_CHUNKED : 0)) + +#define PD_EXT_HDR_LE (data_size, req_chunk, chunk_num, chunked) \ + cpu_to_le16(PD_EXT_HDR((data_size), (req_chunk), (chunk_num), (chunked))) + +static inline unsigned int pd_ext_header_chunk_num (u16 ext_header) +{ +return (ext_header >>PD_EXT_HDR_CHUNK_NUM_SHIFT ) & +PD_EXT_HDR_CHUNK_NUM_MASK ; +} + +static inline unsigned int pd_ext_header_data_size (u16 ext_header) +{ +return (ext_header >>PD_EXT_HDR_DATA_SIZE_SHIFT ) & +PD_EXT_HDR_DATA_SIZE_MASK ; +} + +static inline unsigned int pd_ext_header_data_size_le (__le16 ext_header) +{ +return pd_ext_header_data_size (le16_to_cpu (ext_header)); +} +#define PD_MAX_PAYLOAD 7 +#define PD_EXT_MAX_LEGACY_DATA 26 +#define PD_EXT_MAX_CHUNK_DATA 26 +#define PD_EXT_MAX_DATA 260/** -* struct pd_message - PD message as seen on wire -* @header: PD message header -* @payload: PD message payload -*/ +* struct pd_ext_message_data - PD extended message data as seen on wire +* @header: PD extended message header +* @data: PD extended message data +*/ +struct pd_ext_message_data { +__le16 header; +u8 data[PD_EXT_MAX_DATA ]; +} __packed; + +/** +* struct pd_message - PD message as seen on wire +* @header: PD message header +* @payload: PD message payload +* @ext_msg: PD message extended message data +*/ struct pd_message {__le16 header; -__le32 payload[PD_MAX_PAYLOAD ]; +union { +__le32 payload[PD_MAX_PAYLOAD ]; +struct pd_ext_message_data ext_msg; + }; } __packed;/* PDO: Power Data Object */
@@ -121,6 +204,7 @@ enum pd_pdo_type {PDO_TYPE_FIXED =0 ,PDO_TYPE_BATT =1 ,PDO_TYPE_VAR =2 , + PDO_TYPE_APDO = 3, };#define PDO_TYPE_SHIFT 30
@@ -172,6 +256,29 @@ enum pd_pdo_type { (PDO_TYPE (PDO_TYPE_VAR ) |PDO_VAR_MIN_VOLT (min_mv) | \ PDO_VAR_MAX_VOLT(max_mv) | PDO_VAR_MAX_CURR(max_ma)) +enum pd_apdo_type { +APDO_TYPE_PPS =0 , +}; + +#define PDO_APDO_TYPE_SHIFT 28/* Only valid value currently is 0x0 - PPS */ +#define PDO_APDO_MAX_VOLT_SHIFT 17/* 100mV units */ +#define PDO_APDO_MIN_VOLT_SHIFT 8/* 100mV units */ +#define PDO_APDO_MAX_CURR_SHIFT 0/* 50mA units */ + +#define PDO_APDO_TYPE_MASK 0x3 +#define PDO_APDO_VOLT_MASK 0xff +#define PDO_APDO_CURR_MASK 0x7f + +#define PDO_APDO_TYPE (t) ((t) << PDO_APDO_TYPE_SHIFT) +#define PDO_APDO_MIN_VOLT (mv) ((((mv) / 100) & PDO_APDO_VOLT_MASK) << PDO_APDO_MIN_VOLT_SHIFT) +#define PDO_APDO_MAX_VOLT (mv) ((((mv) / 100) & PDO_APDO_VOLT_MASK) << PDO_APDO_MAX_VOLT_SHIFT) +#define PDO_APDO_MAX_CURR (ma) ((((ma) / 50) & PDO_APDO_CURR_MASK) << PDO_APDO_MAX_CURR_SHIFT) + +#define PDO_APDO (min_mv, max_mv, max_ma) \ + (PDO_TYPE(PDO_TYPE_APDO) | PDO_APDO_TYPE(ADPO_TYPE_PPS) | \ + PDO_APDO_MIN_VOLT(min_mv) | PDO_APDO_MAX_VOLT(max_mv) | \ + PDO_APDO_MAX_CURR(max_ma)) +static inline enum pd_pdo_type pdo_type (u32 pdo) {return (pdo >>PDO_TYPE_SHIFT ) &PDO_TYPE_MASK ;
@@ -202,6 +309,26 @@ static inline unsigned int pdo_max_power(u32 pdo)return ((pdo >>PDO_BATT_MAX_PWR_SHIFT ) &PDO_PWR_MASK ) *250 ; } +static inline enum pd_apdo_type pdo_apdo_type (u32 pdo) +{ +return (pdo >>PDO_APDO_TYPE_SHIFT ) &PDO_APDO_TYPE_MASK ; +} + +static inline unsigned int pdo_apdo_min_voltage (u32 pdo) +{ +return ((pdo >>PDO_APDO_MIN_VOLT_SHIFT ) &PDO_APDO_VOLT_MASK ) *100 ; +} + +static inline unsigned int pdo_apdo_max_voltage (u32 pdo) +{ +return ((pdo >>PDO_APDO_MAX_VOLT_SHIFT ) &PDO_APDO_VOLT_MASK ) *100 ; +} + +static inline unsigned int pdo_apdo_max_current (u32 pdo) +{ +return ((pdo >>PDO_APDO_MAX_CURR_SHIFT ) &PDO_APDO_CURR_MASK ) *50 ; +} +/* RDO: Request Data Object */ #define RDO_OBJ_POS_SHIFT 28#define RDO_OBJ_POS_MASK 0x7
@@ -235,6 +362,19 @@ static inline unsigned int pdo_max_power(u32 pdo) (RDO_OBJ (idx) | (flags) | \ RDO_BATT_OP_PWR(op_mw) | RDO_BATT_MAX_PWR(max_mw)) +#define RDO_PROG_VOLT_MASK 0x7ff +#define RDO_PROG_CURR_MASK 0x7f + +#define RDO_PROG_VOLT_SHIFT 9 +#define RDO_PROG_CURR_SHIFT 0 + +#define PDO_PROG_OUT_VOLT (mv) ((((mv) / 20) & RDO_PROG_VOLT_MASK) << RDO_PROG_VOLT_SHIFT) +#define PDO_PROG_OP_CURR (mv) ((((ma) / 50) & RDO_PROG_CURR_MASK) << RDO_PROG_CURR_SHIFT) + +#define RDO_PROG (idx, out_mv, op_ma, flags) \ + (RDO_OBJ(idx) | (flags) | \ + PDO_PROG_OUT_VOLT(out_mv) | PDO_PROG_OP_CURR(op_ma)) +static inline unsigned int rdo_index (u32 rdo) {return (rdo >>RDO_OBJ_POS_SHIFT ) &RDO_OBJ_POS_MASK ;
Signature · 1 line
1.9.1
Re: [RFC PATCH 1/7] typec: tcpm: Add PD Rev 3.0 definitions to PD header
Hi Adam,
Quoted 12 lines · Adam Thomson
This commit adds definitions for PD Rev 3.0 messages, including APDO PPS and extended message support for TCPM.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd.h | 162 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 151 insertions(+), 11 deletions(-)include/linux/usb/pd.h +0
-0
<snip>
Quoted 12 lines
#define
PD_REV100x0
#definePD_REV200x1include/linux/usb/pd.h +2
-0+#define PD_REV30 0x2 +#define PD_HEADER_EXT_HDR BIT(15)#define PD_HEADER_CNT_SHIFT 12#define PD_HEADER_CNT_MASK 0x7#define PD_HEADER_ID_SHIFT 9@@ -59,18 +91,19 @@ enum pd_data_msg_type {#define PD_HEADER_REV_MASK 0x3#define PD_HEADER_DATA_ROLE BIT(5)#define PD_HEADER_TYPE_SHIFT 0
include/linux/usb/pd.h enum pd_data_msg_type { · line 100
Quoted 10 lines
include/linux/usb/pd.h +3
-3-#define PD_HEADER_TYPE_MASK 0xf +#define PD_HEADER_TYPE_MASK 0x1f -#define PD_HEADER (type, pwr, data, id, cnt) \ +#define PD_HEADER (type, pwr, data, id, cnt, ext_hdr) \ ((((type) & PD_HEADER_TYPE_MASK) << PD_HEADER_TYPE_SHIFT) | \ ((pwr) == TYPEC_SOURCE ? PD_HEADER_PWR_ROLE : 0) | \ ((data) == TYPEC_HOST ? PD_HEADER_DATA_ROLE : 0) | \ - (PD_REV20 << PD_HEADER_REV_SHIFT) | \ + (PD_REV30 << PD_HEADER_REV_SHIFT) | \
You are making a hardcoded change for the Spec Rev field of every outgoing message to be 3.0. However, this needs to be flexible in order to support backwards compatibility when communicating with a 2.0 peer. The revision "negotiation" would need to be done at the time the first Request is sent such that both source & sink settle on the highest supported revision of both. (PD 3.0 spec section 6.2.1.1.5)
Quoted 71 lines
include/linux/usb/pd.h +52
-6(((id) & PD_HEADER_ID_MASK) << PD_HEADER_ID_SHIFT) | \ - (((cnt) & PD_HEADER_CNT_MASK) << PD_HEADER_CNT_SHIFT)) + (((cnt) & PD_HEADER_CNT_MASK) << PD_HEADER_CNT_SHIFT) | \ + ((ext_hdr) ? PD_HEADER_EXT_HDR : 0))#define PD_HEADER_LE (type, pwr, data, id, cnt) \ - cpu_to_le16(PD_HEADER((type), (pwr), (data), (id), (cnt))) + cpu_to_le16(PD_HEADER((type), (pwr), (data), (id), (cnt), (0)))static inline unsigned int pd_header_cnt (u16 header) {@@ -102,16 +135,66 @@ static inline unsigned int pd_header_msgid_le(__le16 header)return pd_header_msgid (le16_to_cpu (header)); } +#define PD_EXT_HDR_CHUNKED BIT(15) +#define PD_EXT_HDR_CHUNK_NUM_SHIFT 11 +#define PD_EXT_HDR_CHUNK_NUM_MASK 0xf +#define PD_EXT_HDR_REQ_CHUNK BIT(10) +#define PD_EXT_HDR_DATA_SIZE_SHIFT 0 +#define PD_EXT_HDR_DATA_SIZE_MASK 0x1ff + +#define PD_EXT_HDR (data_size, req_chunk, chunk_num, chunked) \ + ((((data_size) & PD_EXT_HDR_DATA_SIZE_MASK) << PD_EXT_HDR_DATA_SIZE_SHIFT) | \ + ((req_chunk) ? PD_EXT_HDR_REQ_CHUNK : 0) | \ + (((chunk_num) & PD_EXT_HDR_CHUNK_NUM_MASK) << PD_EXT_HDR_CHUNK_NUM_SHIFT) | \ + ((chunked) ? PD_EXT_HDR_CHUNKED : 0)) + +#define PD_EXT_HDR_LE (data_size, req_chunk, chunk_num, chunked) \ + cpu_to_le16(PD_EXT_HDR((data_size), (req_chunk), (chunk_num), (chunked))) + +static inline unsigned int pd_ext_header_chunk_num (u16 ext_header) +{ +return (ext_header >>PD_EXT_HDR_CHUNK_NUM_SHIFT ) & +PD_EXT_HDR_CHUNK_NUM_MASK ; +} + +static inline unsigned int pd_ext_header_data_size (u16 ext_header) +{ +return (ext_header >>PD_EXT_HDR_DATA_SIZE_SHIFT ) & +PD_EXT_HDR_DATA_SIZE_MASK ; +} + +static inline unsigned int pd_ext_header_data_size_le (__le16 ext_header) +{ +return pd_ext_header_data_size (le16_to_cpu (ext_header)); +} +#define PD_MAX_PAYLOAD 7 +#define PD_EXT_MAX_LEGACY_DATA 26 +#define PD_EXT_MAX_CHUNK_DATA 26 +#define PD_EXT_MAX_DATA 260/** -* struct pd_message - PD message as seen on wire -* @header: PD message header -* @payload: PD message payload -*/ +* struct pd_ext_message_data - PD extended message data as seen on wire +* @header: PD extended message header +* @data: PD extended message data +*/ +struct pd_ext_message_data { +__le16 header; +u8 data[PD_EXT_MAX_DATA ]; +} __packed; + +/** +* struct pd_message - PD message as seen on wire +* @header: PD message header +* @payload: PD message payload
include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16 header) · line 198
Quoted 10 lines
include/linux/usb/pd.h +6
-1+* @ext_msg: PD message extended message data +*/ struct pd_message {__le16 header; -__le32 payload[PD_MAX_PAYLOAD ]; +union { +__le32 payload[PD_MAX_PAYLOAD ]; +struct pd_ext_message_data ext_msg; + }; } __packed;
It seems that this structure just got ~9-10x fatter (28 byte payload -> 262 bytes). Just wondering if this has a noticeable impact on (performance, memory) considering the various places in TCPM where struct pd_message is stack-allocated? And for RX, we have more to malloc/memcpy in tcpm_pd_receive().
Thanks,
Jack
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
RE: [RFC PATCH 1/7] typec: tcpm: Add PD Rev 3.0 definitions to PD header
Quoted 34 lines · Jack Pham
Hi Adam,
Quoted 6 lines · Adam Thomson
This commit adds definitions for PD Rev 3.0 messages, including APDO PPS and extended message support for TCPM.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd.h | 162+++++++++++++++++++++++++++++++++++++++++++++----
Quoted 6 lines
1 file changed, 151 insertions(+), 11 deletions(-)include/linux/usb/pd.h +0
-0<snip>
Quoted 15 lines
#define
PD_REV100x0
#definePD_REV200x1include/linux/usb/pd.h +3
-1+#define PD_REV30 0x2 +#define PD_HEADER_EXT_HDR BIT(15)#define PD_HEADER_CNT_SHIFT 12#define PD_HEADER_CNT_MASK 0x7#define PD_HEADER_ID_SHIFT 9@@ -59,18 +91,19 @@ enum pd_data_msg_type {#define PD_HEADER_REV_MASK 0x3#define PD_HEADER_DATA_ROLE BIT(5)#define PD_HEADER_TYPE_SHIFT 0 -#define PD_HEADER_TYPE_MASK 0xf +#define PD_HEADER_TYPE_MASK 0x1f
Quoted 14 lines
include/linux/usb/pd.h enum pd_data_msg_type { · line 100
Quoted 7 lines
include/linux/usb/pd.h +2
-2-#define PD_HEADER (type, pwr, data, id, cnt) \ +#define PD_HEADER (type, pwr, data, id, cnt, ext_hdr) \ ((((type) & PD_HEADER_TYPE_MASK) << PD_HEADER_TYPE_SHIFT) | \ ((pwr) == TYPEC_SOURCE ? PD_HEADER_PWR_ROLE : 0) | \ ((data) == TYPEC_HOST ? PD_HEADER_DATA_ROLE : 0) | \ - (PD_REV20 << PD_HEADER_REV_SHIFT) | \ + (PD_REV30 << PD_HEADER_REV_SHIFT) | \You are making a hardcoded change for the Spec Rev field of every outgoing message to be 3.0. However, this needs to be flexible in order to support backwards compatibility when communicating with a 2.0 peer. The revision "negotiation" would need to be done at the time the first Request is sent such that both source & sink settle on the highest supported revision of both. (PD 3.0 spec section 6.2.1.1.5)
Thanks for the prompt comments. Much appreciated.
This is a fair point. The existing code was hard coded to Rev 2.0 but this should really be based on capabilities of both sides, if we're truly following the spec. Will take a look at this and try to address the problem.
Quoted 86 lines
include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16
Quoted 13 lines
include/linux/usb/pd.h +3
-2(((id) & PD_HEADER_ID_MASK) << PD_HEADER_ID_SHIFT) | \ - (((cnt) & PD_HEADER_CNT_MASK) << PD_HEADER_CNT_SHIFT)) + (((cnt) & PD_HEADER_CNT_MASK) << PD_HEADER_CNT_SHIFT) | \ + ((ext_hdr) ? PD_HEADER_EXT_HDR : 0))#define PD_HEADER_LE (type, pwr, data, id, cnt) \ - cpu_to_le16(PD_HEADER((type), (pwr), (data), (id), (cnt))) + cpu_to_le16(PD_HEADER((type), (pwr), (data), (id), (cnt), (0)))static inline unsigned int pd_header_cnt (u16 header) {@@ -102,16 +135,66 @@ static inline unsigned int pd_header_msgid_le(__le16header)
include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16 header)
Quoted 11 lines
include/linux/usb/pd.h +8
-0return pd_header_msgid (le16_to_cpu (header)); } +#define PD_EXT_HDR_CHUNKED BIT(15) +#define PD_EXT_HDR_CHUNK_NUM_SHIFT 11 +#define PD_EXT_HDR_CHUNK_NUM_MASK 0xf +#define PD_EXT_HDR_REQ_CHUNK BIT(10) +#define PD_EXT_HDR_DATA_SIZE_SHIFT 0 +#define PD_EXT_HDR_DATA_SIZE_MASK 0x1ff + +#define PD_EXT_HDR (data_size, req_chunk, chunk_num, chunked)\include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16 · line 146
Quoted 1 line
include/linux/usb/pd.h +1
-0+ ((((data_size) & PD_EXT_HDR_DATA_SIZE_MASK) <<
PD_EXT_HDR_DATA_SIZE_SHIFT) | \include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16 · line 147
Quoted 1 line
include/linux/usb/pd.h +1
-0+ ((req_chunk) ? PD_EXT_HDR_REQ_CHUNK : 0) |\include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16 · line 148
Quoted 1 line
include/linux/usb/pd.h +1
-0+ (((chunk_num) & PD_EXT_HDR_CHUNK_NUM_MASK) <<
PD_EXT_HDR_CHUNK_NUM_SHIFT) | \include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16 header)
Quoted 4 lines
include/linux/usb/pd.h +4
-0+ ((chunked) ? PD_EXT_HDR_CHUNKED : 0)) + +#define PD_EXT_HDR_LE (data_size, req_chunk, chunk_num, chunked) \ + cpu_to_le16(PD_EXT_HDR((data_size), (req_chunk), (chunk_num),(chunked)))
Quoted 49 lines
include/linux/usb/pd.h +39
-5+ +static inline unsigned int pd_ext_header_chunk_num (u16 ext_header) +{ +return (ext_header >>PD_EXT_HDR_CHUNK_NUM_SHIFT ) & +PD_EXT_HDR_CHUNK_NUM_MASK ; +} + +static inline unsigned int pd_ext_header_data_size (u16 ext_header) +{ +return (ext_header >>PD_EXT_HDR_DATA_SIZE_SHIFT ) & +PD_EXT_HDR_DATA_SIZE_MASK ; +} + +static inline unsigned int pd_ext_header_data_size_le (__le16 ext_header) +{ +return pd_ext_header_data_size (le16_to_cpu (ext_header)); +} +#define PD_MAX_PAYLOAD 7 +#define PD_EXT_MAX_LEGACY_DATA 26 +#define PD_EXT_MAX_CHUNK_DATA 26 +#define PD_EXT_MAX_DATA 260/** -* struct pd_message - PD message as seen on wire -* @header: PD message header -* @payload: PD message payload -*/ +* struct pd_ext_message_data - PD extended message data as seen on wire +* @header: PD extended message header +* @data: PD extended message data +*/ +struct pd_ext_message_data { +__le16 header; +u8 data[PD_EXT_MAX_DATA ]; +} __packed; + +/** +* struct pd_message - PD message as seen on wire +* @header: PD message header +* @payload: PD message payload +* @ext_msg: PD message extended message data +*/ struct pd_message {__le16 header; -__le32 payload[PD_MAX_PAYLOAD ]; +union { +__le32 payload[PD_MAX_PAYLOAD ]; +struct pd_ext_message_data ext_msg;
Quoted 8 lines
include/linux/usb/pd.h static inline unsigned int pd_header_msgid_le(__le16 header) · line 198
Quoted 2 lines
include/linux/usb/pd.h +1
-0+ }; } __packed;It seems that this structure just got ~9-10x fatter (28 byte payload -> 262 bytes). Just wondering if this has a noticeable impact on (performance, memory) considering the various places in TCPM where struct
pd_messageis stack-allocated? And for RX, we have more to malloc/memcpy intcpm_pd_receive().
In my testing I've not noticed any problems as a result of this, but of course we are dealing with larger data to support PD 3.0 extended messages so there will be overhead. I'll take another and see if there are ways we can help to reduce the impact. One possibility for now would be to keep the message size as before as right now we don't have and PD3.0 controllers capable of unchunked extended messages, at least not in the kernel, and this patch set doesn't allow for unchunked messaging anyway.
RFC PATCH 2/7 typec: tcpm: Add ADO header for Alert message handling
In reply to Adam ThomsonThis commit adds a header providing definitions for handling Alert messages. Currently the header only focuses on handling incoming alerts.
--- include/linux/usb/pd_ado.h | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 include/linux/usb/pd_ado.h
include/linux/usb/pd_ado.h new file +49 -0
@@ -0,0 +1,49 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* +* Author: Adam Thomson <[email protected]> +* +* 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. +*/ + +#ifndef __LINUX_USB_PD_ADO_H +#define __LINUX_USB_PD_ADO_H + +/* ADO : Alert Data Object */ +#define ADO_TYPE_SHIFT 24 +#define ADO_TYPE_MASK 0xff +#define ADO_FIXED_BATT_SHIFT 20 +#define ADO_FIXED_BATT_MASK 0xf +#define ADO_HOT_SWAP_BATT_SHIFT 16 +#define ADO_HOT_SWAP_BATT_MASK 0xf + +#define ADO_TYPE_BATT_STATUS_CHANGE BIT(1) +#define ADO_TYPE_OCP BIT(2) +#define ADO_TYPE_OTP BIT(3) +#define ADO_TYPE_OP_COND_CHANGE BIT(4) +#define ADO_TYPE_SRC_INPUT_CHANGE BIT(5) +#define ADO_TYPE_OVP BIT(6) + +static inline unsigned int ado_type (u32 ado) +{ +return (ado >>ADO_TYPE_SHIFT ) &ADO_TYPE_MASK ; +} + +static inline unsigned int ado_fixed_batt (u32 ado) +{ +return (ado >>ADO_FIXED_BATT_SHIFT ) &ADO_FIXED_BATT_MASK ; +} + +static inline unsigned int ado_hot_swap_batt (u32 ado) +{ +return (ado >>ADO_HOT_SWAP_BATT_SHIFT ) &ADO_HOT_SWAP_BATT_MASK ; +} +#endif /* __LINUX_USB_PD_ADO_H */
Signature · 1 line
1.9.1
Greg Kroah-Hartman
maintainerRe: [RFC PATCH 2/7] typec: tcpm: Add ADO header for Alert message handling
Quoted 16 lines · Adam Thomson
This commit adds a header providing definitions for handling Alert messages. Currently the header only focuses on handling incoming alerts.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd_ado.h | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 include/linux/usb/pd_ado.hinclude/linux/usb/pd_ado.h new file +0
-0
include/linux/usb/pd_ado.h line 9
Quoted 9 lines
include/linux/usb/pd_ado.h new file +9
-0@@ -0,0 +1,49 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* +* Author: Adam Thomson <[email protected]> +* +* 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.
I have to ask, do you _really_ mean "any later version"?
Quoted 7 lines
include/linux/usb/pd_ado.h new file +7
-0+* +* 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. +*/ +
include/linux/usb/pd_ado.h line 26
Quoted 10 lines
include/linux/usb/pd_ado.h new file +10
-0+#ifndef __LINUX_USB_PD_ADO_H +#define __LINUX_USB_PD_ADO_H + +/* ADO : Alert Data Object */ +#define ADO_TYPE_SHIFT 24 +#define ADO_TYPE_MASK 0xff +#define ADO_FIXED_BATT_SHIFT 20 +#define ADO_FIXED_BATT_MASK 0xf +#define ADO_HOT_SWAP_BATT_SHIFT 16 +#define ADO_HOT_SWAP_BATT_MASK 0xf
USB_ prefixes perhaps?
include/linux/usb/pd_ado.h line 38
Quoted 12 lines
include/linux/usb/pd_ado.h new file +12
-0+ +#define ADO_TYPE_BATT_STATUS_CHANGE BIT(1) +#define ADO_TYPE_OCP BIT(2) +#define ADO_TYPE_OTP BIT(3) +#define ADO_TYPE_OP_COND_CHANGE BIT(4) +#define ADO_TYPE_SRC_INPUT_CHANGE BIT(5) +#define ADO_TYPE_OVP BIT(6) + +static inline unsigned int ado_type (u32 ado) +{ +return (ado >>ADO_TYPE_SHIFT ) &ADO_TYPE_MASK ; +}
usb_ prefix for this and the other inline functions as well?
thanks,
greg k-h
RE: [RFC PATCH 2/7] typec: tcpm: Add ADO header for Alert message handling
Quoted 21 lines · Greg Kroah-Hartman
Quoted 7 lines · Adam Thomson
This commit adds a header providing definitions for handling Alert messages. Currently the header only focuses on handling incoming alerts.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd_ado.h | 49++++++++++++++++++++++++++++++++++++++++++++++
Quoted 12 lines
1 file changed, 49 insertions(+) create mode 100644 include/linux/usb/pd_ado.hinclude/linux/usb/pd_ado.h new file +3
-0@@ -0,0 +1,49 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +*
Quoted 8 lines
include/linux/usb/pd_ado.h line 9
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+* Author: Adam Thomson <[email protected]> +* +* 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. I have to ask, do you _really_ mean "any later version"?
Many thanks for the prompt comments.
So this is the same copyright header applied for the other PD related code (bit of a copy and paste to follow existing tcpm practice), and have tended to use this header for other driver development as well. Within the context of the Linux kernel I believe this should be fine and will resolve to GPLv2, and should someone use the code elsewhere then they can choose a later GPL license, not that I suspect many would. Do you see any problems here as am happy to be corrected? :)
Quoted 12 lines
Quoted 12 lines
include/linux/usb/pd_ado.h new file +11
-0+* +* 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. +*/ + +#ifndef __LINUX_USB_PD_ADO_H +#define __LINUX_USB_PD_ADO_H + +/* ADO : Alert Data Object */
Quoted 8 lines
include/linux/usb/pd_ado.h line 26
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+#define ADO_TYPE_SHIFT 24 +#define ADO_TYPE_MASK 0xff +#define ADO_FIXED_BATT_SHIFT 20 +#define ADO_FIXED_BATT_MASK 0xf +#define ADO_HOT_SWAP_BATT_SHIFT 16 +#define ADO_HOT_SWAP_BATT_MASK 0xf
USB_prefixes perhaps?
Again here I was following the existing TCPM approach, but if it's a problem then I can update this.
Quoted 15 lines
include/linux/usb/pd_ado.h line 38
Quoted 13 lines
include/linux/usb/pd_ado.h new file +12
-0+ +#define ADO_TYPE_BATT_STATUS_CHANGE BIT(1) +#define ADO_TYPE_OCP BIT(2) +#define ADO_TYPE_OTP BIT(3) +#define ADO_TYPE_OP_COND_CHANGE BIT(4) +#define ADO_TYPE_SRC_INPUT_CHANGE BIT(5) +#define ADO_TYPE_OVP BIT(6) + +static inline unsigned int ado_type (u32 ado) +{ +return (ado >>ADO_TYPE_SHIFT ) &ADO_TYPE_MASK ; +}
usb_prefix for this and the other inline functions as well?
Same comment here.
Greg Kroah-Hartman
maintainerRe: [RFC PATCH 2/7] typec: tcpm: Add ADO header for Alert message handling
Quoted 34 lines · Adam Thomson
Quoted 21 lines · Greg Kroah-Hartman
Quoted 7 lines · Adam Thomson
This commit adds a header providing definitions for handling Alert messages. Currently the header only focuses on handling incoming alerts.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd_ado.h | 49++++++++++++++++++++++++++++++++++++++++++++++
Quoted 12 lines
1 file changed, 49 insertions(+) create mode 100644 include/linux/usb/pd_ado.hinclude/linux/usb/pd_ado.h new file +3
-0@@ -0,0 +1,49 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* Quoted 8 lines
include/linux/usb/pd_ado.h line 9
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+* Author: Adam Thomson <[email protected]> +* +* 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. I have to ask, do you _really_ mean "any later version"?
Many thanks for the prompt comments.
Quoted 7 lines
So this is the same copyright header applied for the other PD related code (bit of a copy and paste to follow existing tcpm practice), and have tended to use this header for other driver development as well. Within the context of the Linux kernel I believe this should be fine and will resolve to GPLv2, and should someone use the code elsewhere then they can choose a later GPL license, not that I suspect many would. Do you see any problems here as am happy to be corrected? :)
Please consult your lawyers as to what license you should use for new kernel code. Never just blindly copy/paste without knowing what you are supposed to be doing :)
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
include/linux/usb/pd_ado.h new file +11
-0+* +* 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. +*/ + +#ifndef __LINUX_USB_PD_ADO_H +#define __LINUX_USB_PD_ADO_H + +/* ADO : Alert Data Object */ Quoted 8 lines
include/linux/usb/pd_ado.h line 26
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+#define ADO_TYPE_SHIFT 24 +#define ADO_TYPE_MASK 0xff +#define ADO_FIXED_BATT_SHIFT 20 +#define ADO_FIXED_BATT_MASK 0xf +#define ADO_HOT_SWAP_BATT_SHIFT 16 +#define ADO_HOT_SWAP_BATT_MASK 0xf
USB_prefixes perhaps?
Quoted 2 lines
Again here I was following the existing TCPM approach, but if it's a problem then I can update this.
What does TCPM use here? I missed those defines...
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+ +#define ADO_TYPE_BATT_STATUS_CHANGE BIT(1) +#define ADO_TYPE_OCP BIT(2) +#define ADO_TYPE_OTP BIT(3) +#define ADO_TYPE_OP_COND_CHANGE BIT(4) +#define ADO_TYPE_SRC_INPUT_CHANGE BIT(5)Quoted 8 lines
include/linux/usb/pd_ado.h line 38
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+#define ADO_TYPE_OVP BIT(6) + +static inline unsigned int ado_type (u32 ado) +{ +return (ado >>ADO_TYPE_SHIFT ) &ADO_TYPE_MASK ; +}
usb_prefix for this and the other inline functions as well?
Quoted 1 line
Same comment here.
But it would make more sense, right?
thanks,
greg k-h
RE: [RFC PATCH 2/7] typec: tcpm: Add ADO header for Alert message handling
Quoted 43 lines · Greg Kroah-Hartman
Quoted 34 lines · Adam Thomson
Quoted 21 lines · Greg Kroah-Hartman
Quoted 7 lines · Adam Thomson
This commit adds a header providing definitions for handling Alert messages. Currently the header only focuses on handling incoming alerts.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd_ado.h | 49++++++++++++++++++++++++++++++++++++++++++++++
Quoted 12 lines
1 file changed, 49 insertions(+) create mode 100644 include/linux/usb/pd_ado.hinclude/linux/usb/pd_ado.h new file +3
-0@@ -0,0 +1,49 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* Quoted 8 lines
include/linux/usb/pd_ado.h line 9
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+* Author: Adam Thomson <[email protected]> +* +* 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. I have to ask, do you _really_ mean "any later version"?
Many thanks for the prompt comments.
Quoted 7 lines
So this is the same copyright header applied for the other PD related code (bit of a copy and paste to follow existing tcpm practice), and have tended to use this header for other driver development as well. Within the context of the Linux kernel I believe this should be fine and will resolve to GPLv2, and should someone use the code elsewhere then they can choose a later GPL license, not that I suspect many would. Do you see any problems here as am happy to be corrected? :)
Quoted 3 lines
Please consult your lawyers as to what license you should use for new kernel code. Never just blindly copy/paste without knowing what you are supposed to be doing :)
Yes, our legal department are aware of the use of this header, from previous driver submissions, and have no problems. I certainly wouldn't have applied this otherwise, but completely understand why you raised this :)
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 7 lines
include/linux/usb/pd_ado.h new file +7
-0+* +* 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. +*/ +include/linux/usb/pd_ado.h line 26
Quoted 10 lines
include/linux/usb/pd_ado.h new file +10
-0+#ifndef __LINUX_USB_PD_ADO_H +#define __LINUX_USB_PD_ADO_H + +/* ADO : Alert Data Object */ +#define ADO_TYPE_SHIFT 24 +#define ADO_TYPE_MASK 0xff +#define ADO_FIXED_BATT_SHIFT 20 +#define ADO_FIXED_BATT_MASK 0xf +#define ADO_HOT_SWAP_BATT_SHIFT 16 +#define ADO_HOT_SWAP_BATT_MASK 0xf
USB_prefixes perhaps?
Quoted 4 lines
Quoted 2 lines
Again here I was following the existing TCPM approach, but if it's a problem then I can update this.
What does TCPM use here? I missed those defines...
For example, include/linux/usb/pd_bdo.h uses defines such as:
#define BDO_MODE_TRANSMIT (1 << 28)
and include/linux/usb/pd.h has defines such as:
#define PDO_FIXED_DUAL_ROLE BIT(29)
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
include/linux/usb/pd_ado.h line 38
Quoted 13 lines
include/linux/usb/pd_ado.h new file +12
-0+ +#define ADO_TYPE_BATT_STATUS_CHANGE BIT(1) +#define ADO_TYPE_OCP BIT(2) +#define ADO_TYPE_OTP BIT(3) +#define ADO_TYPE_OP_COND_CHANGE BIT(4) +#define ADO_TYPE_SRC_INPUT_CHANGE BIT(5) +#define ADO_TYPE_OVP BIT(6) + +static inline unsigned int ado_type (u32 ado) +{ +return (ado >>ADO_TYPE_SHIFT ) &ADO_TYPE_MASK ; +}
usb_prefix for this and the other inline functions as well?
Quoted 3 lines
Quoted 1 line
Same comment here.
But it would make more sense, right?
I'm happy with that. Actually I'd probably opt for 'usb_pd_' if I was going that route as it's PD specific information. Would that be reasonable?
Greg Kroah-Hartman
maintainerRe: [RFC PATCH 2/7] typec: tcpm: Add ADO header for Alert message handling
Quoted 45 lines · Adam Thomson
Quoted 43 lines · Greg Kroah-Hartman
Quoted 41 lines · Adam Thomson
Quoted 21 lines · Greg Kroah-Hartman
Quoted 7 lines · Adam Thomson
This commit adds a header providing definitions for handling Alert messages. Currently the header only focuses on handling incoming alerts.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd_ado.h | 49++++++++++++++++++++++++++++++++++++++++++++++
Quoted 12 lines
1 file changed, 49 insertions(+) create mode 100644 include/linux/usb/pd_ado.hinclude/linux/usb/pd_ado.h new file +3
-0@@ -0,0 +1,49 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* Quoted 8 lines
include/linux/usb/pd_ado.h line 9
Quoted 6 lines
include/linux/usb/pd_ado.h new file +6
-0+* Author: Adam Thomson <[email protected]> +* +* 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. I have to ask, do you _really_ mean "any later version"?
Many thanks for the prompt comments.
So this is the same copyright header applied for the other PD related code (bit of a copy and paste to follow existing tcpm practice), and have tended to use this header for other driver development as well. Within the context of the Linux kernel I believe this should be fine and will resolve to GPLv2, and should someone use the code elsewhere then they can choose a later GPL license, not that I suspect many would. Do you see any problems here as am happy to be corrected? :)
Quoted 7 lines
Quoted 3 lines
Please consult your lawyers as to what license you should use for new kernel code. Never just blindly copy/paste without knowing what you are supposed to be doing :)
Yes, our legal department are aware of the use of this header, from previous driver submissions, and have no problems. I certainly wouldn't have applied this otherwise, but completely understand why you raised this :)
Great, thanks for the info.
Quoted 25 lines
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 7 lines
include/linux/usb/pd_ado.h new file +7
-0+* +* 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. +*/ +include/linux/usb/pd_ado.h line 26
Quoted 10 lines
include/linux/usb/pd_ado.h new file +10
-0+#ifndef __LINUX_USB_PD_ADO_H +#define __LINUX_USB_PD_ADO_H + +/* ADO : Alert Data Object */ +#define ADO_TYPE_SHIFT 24 +#define ADO_TYPE_MASK 0xff +#define ADO_FIXED_BATT_SHIFT 20 +#define ADO_FIXED_BATT_MASK 0xf +#define ADO_HOT_SWAP_BATT_SHIFT 16 +#define ADO_HOT_SWAP_BATT_MASK 0xf
USB_prefixes perhaps?Quoted 4 lines
Quoted 2 lines
Again here I was following the existing TCPM approach, but if it's a problem then I can update this.
What does TCPM use here? I missed those defines...
Quoted 7 lines
For example,
include/linux/usb/pd_bdo.huses defines such as:#define
BDO_MODE_TRANSMIT(1 << 28)and
include/linux/usb/pd.hhas defines such as:#define
PDO_FIXED_DUAL_ROLEBIT(29)
Hm, ok, I guess that's ok for a define, but for a function:
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
include/linux/usb/pd_ado.h line 38
Quoted 11 lines
include/linux/usb/pd_ado.h new file +11
-0+#define ADO_TYPE_BATT_STATUS_CHANGE BIT(1) +#define ADO_TYPE_OCP BIT(2) +#define ADO_TYPE_OTP BIT(3) +#define ADO_TYPE_OP_COND_CHANGE BIT(4) +#define ADO_TYPE_SRC_INPUT_CHANGE BIT(5) +#define ADO_TYPE_OVP BIT(6) + +static inline unsigned int ado_type (u32 ado) +{ +return (ado >>ADO_TYPE_SHIFT ) &ADO_TYPE_MASK ; +}
usb_prefix for this and the other inline functions as well?
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Same comment here.
But it would make more sense, right?
I'm happy with that. Actually I'd probably opt for '
usb_pd_' if I was going that route as it's PD specific information. Would that be reasonable?
Yes, that would be much better, thanks a lot.
greg k-h
RFC PATCH 3/7 typec: tcpm: Add SDB header for Status message handling
In reply to Adam ThomsonThis commit adds a header providing definitions for handling Status messages. Currently the header only focuses on handling incoming Status messages.
--- include/linux/usb/pd_ext_sdb.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 include/linux/usb/pd_ext_sdb.h
include/linux/usb/pd_ext_sdb.h new file +40 -0
@@ -0,0 +1,40 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* +* Author: Adam Thomson <[email protected]> +* +* 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. +*/ + +#ifndef __LINUX_USB_PD_EXT_SDB_H +#define __LINUX_USB_PD_EXT_SDB_H + +/* SDB : Status Data Block */ +enum pd_ext_sdb_fields { +EXT_SDB_INTERNAL_TEMP =0 , +EXT_SDB_PRESENT_INPUT , +EXT_SDB_PRESENT_BATT_INPUT , +EXT_SDB_EVENT_FLAGS , +EXT_SDB_TEMP_STATUS , +EXT_SDB_DATA_SIZE , +}; + +/* Event Flags */ +#define EXT_SDB_EVENT_OCP BIT(1) +#define EXT_SDB_EVENT_OTP BIT(2) +#define EXT_SDB_EVENT_OVP BIT(3) +#define EXT_SDB_EVENT_CF_CV_MODE BIT(4) + +#define EXT_SDB_PPS_EVENTS (EXT_SDB_EVENT_OCP | \ + EXT_SDB_EVENT_OTP | \ + EXT_SDB_EVENT_OVP) + +#endif /* __LINUX_USB_PD_EXT_SDB_H */
Signature · 1 line
1.9.1
Greg Kroah-Hartman
maintainerRe: [RFC PATCH 3/7] typec: tcpm: Add SDB header for Status message handling
Quoted 16 lines · Adam Thomson
This commit adds a header providing definitions for handling Status messages. Currently the header only focuses on handling incoming Status messages.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd_ext_sdb.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 include/linux/usb/pd_ext_sdb.hinclude/linux/usb/pd_ext_sdb.h new file +0
-0
include/linux/usb/pd_ext_sdb.h line 9
Quoted 9 lines
include/linux/usb/pd_ext_sdb.h new file +9
-0@@ -0,0 +1,40 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* +* Author: Adam Thomson <[email protected]> +* +* 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.
Same license question here.
include/linux/usb/pd_ext_sdb.h line 18
Quoted 9 lines
include/linux/usb/pd_ext_sdb.h new file +9
-0+* +* 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. +*/ + +#ifndef __LINUX_USB_PD_EXT_SDB_H +#define __LINUX_USB_PD_EXT_SDB_H
No need for __LINUX_ right?
thanks,
greg k-h
RE: [RFC PATCH 3/7] typec: tcpm: Add SDB header for Status message handling
Quoted 31 lines · Greg Kroah-Hartman
Quoted 7 lines · Adam Thomson
This commit adds a header providing definitions for handling Status messages. Currently the header only focuses on handling incoming Status messages.
Signed-off-by Adam Thomson <[email protected]>--- include/linux/usb/pd_ext_sdb.h | 40++++++++++++++++++++++++++++++++++++++++
Quoted 9 lines
1 file changed, 40 insertions(+) create mode 100644 include/linux/usb/pd_ext_sdb.hinclude/linux/usb/pd_ext_sdb.h new file +0
-0include/linux/usb/pd_ext_sdb.h line 9
Quoted 9 lines
include/linux/usb/pd_ext_sdb.h new file +9
-0@@ -0,0 +1,40 @@ +/* +* Copyright (c) 2017 Dialog Semiconductor +* +* Author: Adam Thomson <[email protected]> +* +* 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. Same license question here.
Quoted 11 lines
include/linux/usb/pd_ext_sdb.h line 18
Quoted 9 lines
include/linux/usb/pd_ext_sdb.h new file +9
-0+* +* 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. +*/ + +#ifndef __LINUX_USB_PD_EXT_SDB_H +#define __LINUX_USB_PD_EXT_SDB_HNo need for
__LINUX_right?
As for patch 2, was following existing TCPM code but can remove the LINUX part if that's preferred.
RFC PATCH 4/7 typec: tcpm: Add core support for sink side PPS
In reply to Adam ThomsonThis commit adds code to handle requesting of PPS APDOs. Switching between standard PDOs and APDOs, and re-requesting an APDO to modify operating voltage/current will be triggered by an external call into TCPM.
--- drivers/usb/typec/tcpm.c | 441 +++++++++++++++++++++++++++++++++++++++++++++-- include/linux/usb/tcpm.h | 2 +- 2 files changed, 429 insertions(+), 14 deletions(-)
drivers/usb/typec/tcpm.c +428 -13
@@ -56,6 +56,7 @@S (SNK_DISCOVERY_DEBOUNCE_DONE ), \S (SNK_WAIT_CAPABILITIES ), \S (SNK_NEGOTIATE_CAPABILITIES ), \ +S (SNK_NEGOTIATE_PPS_CAPABILITIES ), \S (SNK_TRANSITION_SINK ), \S (SNK_TRANSITION_SINK_VBUS ), \ S(SNK_READY), \
@@ -175,6 +176,16 @@ struct pd_mode_data {struct typec_altmode_desc altmode_desc[SVID_DISCOVERY_MAX ]; }; +struct pd_pps_data { +u16 min_volt; +u16 max_volt; +u16 max_curr; +u16 out_volt; +u16 op_curr; +bool supported; +bool active; +}; +struct tcpm_port {struct device *dev;
@@ -268,6 +279,7 @@ struct tcpm_port {unsigned int max_snk_ma;unsigned int max_snk_mw;unsigned int operating_snk_mw; +bool update_sink_caps;/* Requested current / voltage */ u32 current_limit;
@@ -284,8 +296,13 @@ struct tcpm_port {/* VDO to retry if UFP responder replied busy */ u32 vdo_retry; -/* Alternate mode data */ +/* PPS */ +struct pd_pps_data pps_data; +struct completion pps_complete; +bool pps_pending; +int pps_status; +/* Alternate mode data */ struct pd_mode_data mode_data;struct typec_altmode *partner_altmode[SVID_DISCOVERY_MAX ];struct typec_altmode *port_altmode[SVID_DISCOVERY_MAX ];
@@ -503,6 +520,13 @@ static void tcpm_log_source_caps(struct tcpm_port *port)pdo_max_voltage (pdo), pdo_max_power(pdo));break ; +case PDO_TYPE_APDO : +scnprintf (msg,sizeof (msg), +"%u-%u mV, %u mA" , +pdo_apdo_min_voltage (pdo), +pdo_apdo_max_voltage (pdo), +pdo_apdo_max_current (pdo)); +break ;default :strcpy (msg,"undefined" );break ;
@@ -1259,6 +1283,10 @@ static void vdm_state_machine_work(struct work_struct *work)/* * PD (data, control) command handling functions */ + +static int tcpm_pd_send_control (struct tcpm_port *port, +enum pd_ctrl_msg_type type); +static void tcpm_pd_data_request (struct tcpm_port *port,const struct pd_message *msg) {
@@ -1323,6 +1351,15 @@ static void tcpm_pd_data_request(struct tcpm_port *port, } } +static void tcpm_pps_complete (struct tcpm_port *port,int result) +{ +if (port->pps_pending) { + port->pps_status = result; + port->pps_pending = false; +complete (&port->pps_complete); + } +} +static void tcpm_pd_ctrl_request (struct tcpm_port *port,const struct pd_message *msg) {
@@ -1399,6 +1436,11 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port, next_state =SNK_WAIT_CAPABILITIES ;tcpm_set_state (port, next_state,0 );break ; +case SNK_NEGOTIATE_PPS_CAPABILITIES : + port->pps_status = (type ==PD_CTRL_WAIT ? + -EAGAIN : -EOPNOTSUPP ); +tcpm_set_state (port,SNK_READY ,0 ); +break ;case DR_SWAP_SEND : port->swap_status = (type ==PD_CTRL_WAIT ? -EAGAIN : -EOPNOTSUPP );
@@ -1421,6 +1463,13 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,case PD_CTRL_ACCEPT :switch (port->state) {case SNK_NEGOTIATE_CAPABILITIES : + port->pps_data.active = false; +tcpm_set_state (port,SNK_TRANSITION_SINK ,0 ); +break ; +case SNK_NEGOTIATE_PPS_CAPABILITIES : + port->pps_data.active = true; + port->pps_data.out_volt = port->supply_voltage; + port->pps_data.op_curr = port->current_limit;tcpm_set_state (port,SNK_TRANSITION_SINK ,0 );break ;case SOFT_RESET_SEND :
@@ -1684,6 +1733,8 @@ static int tcpm_pd_select_pdo(struct tcpm_port *port)unsigned int i, max_mw =0 , max_mv =0 ;int ret = -EINVAL ; + port->pps_data.supported = false; +/* * Select the source PDO providing the most power while staying within * the board's voltage limits. Prefer PDO providing exp
@@ -1693,17 +1744,38 @@ static int tcpm_pd_select_pdo(struct tcpm_port *port)enum pd_pdo_type type =pdo_type (pdo);unsigned int mv, ma, mw; -if (type ==PDO_TYPE_FIXED ) +switch (type) { +case PDO_TYPE_FIXED : mv =pdo_fixed_voltage (pdo); -else +break ; +case PDO_TYPE_BATT : +case PDO_TYPE_VAR : mv =pdo_min_voltage (pdo); +break ; +case PDO_TYPE_APDO : +if (pdo_apdo_type (pdo) ==APDO_TYPE_PPS ) + port->pps_data.supported = true; +continue ; +default : +tcpm_log (port,"Invalid PDO type, ignoring" ); +continue ; + } -if (type ==PDO_TYPE_BATT ) { - mw =pdo_max_power (pdo); - }else { +switch (type) { +case PDO_TYPE_FIXED : +case PDO_TYPE_VAR : ma =min (pdo_max_current (pdo), port->max_snk_ma); mw = ma * mv /1000 ; +break ; +case PDO_TYPE_BATT : + mw =pdo_max_power (pdo); +break ; +case PDO_TYPE_APDO : +continue ; +default : +tcpm_log (port,"Invalid PDO type, ignoring" ); +continue ; }/* Perfer higher voltages if available */
@@ -1718,6 +1790,64 @@ static int tcpm_pd_select_pdo(struct tcpm_port *port)return ret; } +static unsigned int tcpm_pd_select_pps_apdo (struct tcpm_port *port) +{ +unsigned int i, max_mw =0 , max_mv =0 ; +unsigned int pps_min_mv, pps_max_mv, ma, mw; +enum pd_pdo_type type; +u32 pdo; +unsigned int index =0 ; + +/* +* Select the source PPS APDO providing the most power while staying +* within the board's limits. We skip the first PDO as this is always +* 5V 3A. +*/ +for (i =1 ; i < port->nr_source_caps; ++i) { + pdo = port->source_caps[i]; + type =pdo_type (pdo); + +switch (type) { +case PDO_TYPE_APDO : +if (pdo_apdo_type (pdo) !=APDO_TYPE_PPS ) { +tcpm_log (port,"Not PPS APDO, ignoring" ); +continue ; + } + + pps_min_mv =pdo_apdo_min_voltage (pdo); + pps_max_mv =pdo_apdo_max_voltage (pdo); + ma =min (pdo_apdo_max_current (pdo), port->max_snk_ma); + mw = (ma * pps_max_mv) /1000 ; +break ; +default : +tcpm_log (port,"Not APDO type, ignoring" ); +continue ; + } + +/* Perfer higher voltages if available */ +if ((mw > max_mw || (mw == max_mw && pps_max_mv > max_mv)) && + pps_max_mv <= port->max_snk_mv) { + index = i; + max_mw = mw; + max_mv = pps_max_mv; + } + } + +if (index) { + pdo = port->source_caps[index]; + + port->pps_data.min_volt =pdo_apdo_min_voltage (pdo); + port->pps_data.max_volt =pdo_apdo_max_voltage (pdo); + port->pps_data.max_curr =pdo_apdo_max_current (pdo); + port->pps_data.out_volt = +min (port->supply_voltage,pdo_apdo_max_voltage (pdo)); + port->pps_data.op_curr = +min (port->current_limit,pdo_apdo_max_current (pdo)); + } + +return index; +} +static int tcpm_pd_build_request (struct tcpm_port *port,u32 *rdo) {unsigned int mv, ma, mw, flags;
@@ -1729,13 +1859,22 @@ static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo) index =tcpm_pd_select_pdo (port);if (index <0 )return -EINVAL ; + pdo = port->source_caps[index]; type =pdo_type (pdo); -if (type ==PDO_TYPE_FIXED ) +switch (type) { +case PDO_TYPE_FIXED : mv =pdo_fixed_voltage (pdo); -else +break ; +case PDO_TYPE_BATT : +case PDO_TYPE_VAR : mv =pdo_min_voltage (pdo); +break ; +default : +tcpm_log (port,"Invalid PDO selected!" ); +return -EINVAL ; + }/* Select maximum available current within the board's power limit */ if (type ==PDO_TYPE_BATT ) {
@@ -1804,6 +1943,90 @@ static int tcpm_pd_send_request(struct tcpm_port *port)return tcpm_pd_transmit (port,TCPC_TX_SOP , &msg); } +static int tcpm_pd_build_pps_request (struct tcpm_port *port,u32 *rdo) +{ +unsigned int min_mv, max_mv, max_ma, max_mw, ma, mw, flags; +unsigned int out_mv, op_ma; +enum pd_pdo_type type; +unsigned int index; +u32 pdo; + + index =tcpm_pd_select_pps_apdo (port); +if (!index) +return -EOPNOTSUPP ; + + pdo = port->source_caps[index]; + type =pdo_type (pdo); + +switch (type) { +case PDO_TYPE_APDO : + min_mv =pdo_apdo_min_voltage (pdo); + max_mv =pdo_apdo_max_voltage (pdo); + max_ma =pdo_apdo_max_current (pdo); + out_mv =min (port->supply_voltage, max_mv); + op_ma =min (port->current_limit, max_ma); +break ; +default : +tcpm_log (port,"Invalid PDO selected!" ); +return -EINVAL ; + } + +if ((out_mv < min_mv) || (out_mv > max_mv) || (op_ma > max_ma)) +return -EINVAL ; + +/* Select maximum available current within the board's power limit */ + ma =min (op_ma, (1000 * port->max_snk_mw) / out_mv); + ma =min (ma, port->max_snk_ma); + + flags =RDO_USB_COMM |RDO_NO_SUSPEND ; + +/* Set mismatch bit if offered power is less than operating power */ + mw = (ma * out_mv) /1000 ; + max_ma = ma; + max_mw = mw; +if (mw < port->operating_snk_mw) { + flags |=RDO_CAP_MISMATCH ; + max_mw = port->operating_snk_mw; + max_ma = (max_mw *1000 ) / out_mv; + } + +tcpm_log (port,"cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d" , + port->cc_req, port->cc1, port->cc2, port->vbus_source, + port->vconn_role ==TYPEC_SOURCE ?"source" :"sink" , + port->polarity); + + *rdo =RDO_PROG (index +1 , out_mv, max_ma, flags); + +tcpm_log (port,"Requesting APDO %d: %u mV, %u mA%s" , + index, out_mv, max_ma, + flags &RDO_CAP_MISMATCH ?" [mismatch]" :"" ); + + port->current_limit = max_ma; + port->supply_voltage = out_mv; + +return 0 ; +} + +static int tcpm_pd_send_pps_request (struct tcpm_port *port) +{ +struct pd_message msg; +int ret; +u32 rdo; + + ret =tcpm_pd_build_pps_request (port, &rdo); +if (ret <0 ) +return ret; + +memset (&msg,0 ,sizeof (msg)); + msg.header =PD_HEADER_LE (PD_DATA_REQUEST , + port->pwr_role, + port->data_role, + port->message_id,1 ); + msg.payload[0 ] =cpu_to_le32 (rdo); + +return tcpm_pd_transmit (port,TCPC_TX_SOP , &msg); +} +static int tcpm_set_vbus (struct tcpm_port *port,bool enable) {int ret;
@@ -1983,6 +2206,7 @@ static void tcpm_reset_port(struct tcpm_port *port)tcpm_typec_disconnect (port); port->attached = false; port->pd_capable = false; + port->pps_data.supported = false;/* * First Rx ID should be 0; set this to a sentinel of -1 so that
@@ -1998,6 +2222,8 @@ static void tcpm_reset_port(struct tcpm_port *port)tcpm_set_attached_state (port, false); port->try_src_count =0 ; port->try_snk_count =0 ; + port->supply_voltage =0 ; + port->current_limit =0 ; }static void tcpm_detach (struct tcpm_port *port)
@@ -2304,6 +2530,7 @@ static void run_state_machine(struct tcpm_port *port)tcpm_swap_complete (port,0 );tcpm_typec_connect (port); +tcpm_check_send_discover (port);/* * 6.3.5
@@ -2327,6 +2554,7 @@ static void run_state_machine(struct tcpm_port *port)case SNK_UNATTACHED :if (!port->non_pd_role_swap)tcpm_swap_complete (port, -ENOTCONN ); +tcpm_pps_complete (port, -ENOTCONN );tcpm_snk_detach (port);if (tcpm_start_drp_toggling (port)) {tcpm_set_state (port,DRP_TOGGLING ,0 );
@@ -2335,6 +2563,7 @@ static void run_state_machine(struct tcpm_port *port)tcpm_set_cc (port,TYPEC_CC_RD );if (port->port_type ==TYPEC_PORT_DRP )tcpm_set_state (port,SRC_UNATTACHED ,PD_T_DRP_SRC ); +break ;case SNK_ATTACH_WAIT : if ((port->cc1 == TYPEC_CC_OPEN &&
@@ -2486,6 +2715,24 @@ static void run_state_machine(struct tcpm_port *port) PD_T_SENDER_RESPONSE); }break ; +case SNK_NEGOTIATE_PPS_CAPABILITIES : + ret =tcpm_pd_send_pps_request (port); +if (ret <0 ) { + port->pps_status = ret; +/* +* If this was called due to updates to sink +* capabilities, and pps is no longer valid, we should +* safely fall back to a standard PDO. +*/ +if (port->update_sink_caps) +tcpm_set_state (port,SNK_NEGOTIATE_CAPABILITIES ,0 ); +else +tcpm_set_state (port,SNK_READY ,0 ); + }else { +tcpm_set_state_cond (port,hard_reset_state (port), +PD_T_SENDER_RESPONSE ); + } +break ;case SNK_TRANSITION_SINK :case SNK_TRANSITION_SINK_VBUS : tcpm_set_state(port, hard_reset_state(port),
@@ -2493,6 +2740,7 @@ static void run_state_machine(struct tcpm_port *port)break ;case SNK_READY : port->try_snk_count =0 ; + port->update_sink_caps = false;if (port->explicit_contract) {typec_set_pwr_opmode (port->typec_port,TYPEC_PWR_MODE_PD );
@@ -2501,7 +2749,11 @@ static void run_state_machine(struct tcpm_port *port)tcpm_swap_complete (port,0 );tcpm_typec_connect (port); +tcpm_check_send_discover (port); + +tcpm_pps_complete (port, port->pps_status); +break ;/* Accessory states */
@@ -2548,6 +2800,7 @@ static void run_state_machine(struct tcpm_port *port)tcpm_set_state (port,SRC_UNATTACHED ,PD_T_PS_SOURCE_ON );break ;case SNK_HARD_RESET_SINK_OFF : +memset (&port->pps_data,0 ,sizeof (port->pps_data));tcpm_set_vconn (port, false);tcpm_set_charge (port, false);tcpm_set_roles (port, false,TYPEC_SINK ,TYPEC_DEVICE );
@@ -2768,6 +3021,7 @@ static void run_state_machine(struct tcpm_port *port)break ;case ERROR_RECOVERY :tcpm_swap_complete (port, -EPROTO ); +tcpm_pps_complete (port, -EPROTO );tcpm_set_state (port,PORT_RESET ,0 );break ;case PORT_RESET :
@@ -3233,7 +3487,7 @@ static int tcpm_dr_set(const struct typec_capability *cap,mutex_unlock (&port->lock);if (!wait_for_completion_timeout (&port->swap_complete, -msecs_to_jiffies (PD_ROLE_SWAP_TIMEOUT ))) +msecs_to_jiffies (PD_STATE_MACHINE_TIMEOUT ))) ret = -ETIMEDOUT ;else ret = port->swap_status;
@@ -3278,7 +3532,7 @@ static int tcpm_pr_set(const struct typec_capability *cap,mutex_unlock (&port->lock);if (!wait_for_completion_timeout (&port->swap_complete, -msecs_to_jiffies (PD_ROLE_SWAP_TIMEOUT ))) +msecs_to_jiffies (PD_STATE_MACHINE_TIMEOUT ))) ret = -ETIMEDOUT ;else ret = port->swap_status;
@@ -3318,7 +3572,7 @@ static int tcpm_vconn_set(const struct typec_capability *cap,mutex_unlock (&port->lock);if (!wait_for_completion_timeout (&port->swap_complete, -msecs_to_jiffies (PD_ROLE_SWAP_TIMEOUT ))) +msecs_to_jiffies (PD_STATE_MACHINE_TIMEOUT ))) ret = -ETIMEDOUT ;else ret = port->swap_status;
@@ -3350,6 +3604,161 @@ static int tcpm_try_role(const struct typec_capability *cap, int role)return ret; } +static int tcpm_pps_set_op_curr (struct tcpm_port *port,u16 op_curr) +{ +unsigned int target_mw; +int ret =0 ; + +mutex_lock (&port->swap_lock); +mutex_lock (&port->lock); + +if (!port->pps_data.active) { + ret = -EOPNOTSUPP ; + goto port_unlock; + } + +if (port->state !=SNK_READY ) { + ret = -EAGAIN ; + goto port_unlock; + } + +if (op_curr > port->pps_data.max_curr) { + ret = -EINVAL ; + goto port_unlock; + } + + target_mw = (op_curr * port->pps_data.out_volt) /1000 ; +if ((target_mw < port->operating_snk_mw) || + (target_mw > port->max_snk_mw)) { + ret = -EINVAL ; + goto port_unlock; + } + +reinit_completion (&port->pps_complete); + port->current_limit = op_curr; + port->pps_status =0 ; + port->pps_pending = true; +tcpm_set_state (port,SNK_NEGOTIATE_PPS_CAPABILITIES ,0 ); +mutex_unlock (&port->lock); + +if (!wait_for_completion_timeout (&port->pps_complete, +msecs_to_jiffies (PD_STATE_MACHINE_TIMEOUT ))) + ret = -ETIMEDOUT ; +else + ret = port->pps_status; + + goto swap_unlock; + +port_unlock: +mutex_unlock (&port->lock); +swap_unlock: +mutex_unlock (&port->swap_lock); + +return ret; +} + +static int tcpm_pps_set_out_volt (struct tcpm_port *port,u16 out_volt) +{ +unsigned int target_mw; +int ret =0 ; + +mutex_lock (&port->swap_lock); +mutex_lock (&port->lock); + +if (!port->pps_data.active) { + ret = -EOPNOTSUPP ; + goto port_unlock; + } + +if (port->state !=SNK_READY ) { + ret = -EAGAIN ; + goto port_unlock; + } + +if ((out_volt < port->pps_data.min_volt) || + (out_volt > port->pps_data.max_volt)) { + ret = -EINVAL ; + goto port_unlock; + } + + target_mw = (port->pps_data.op_curr * out_volt) /1000 ; +if ((target_mw < port->operating_snk_mw) || + (target_mw > port->max_snk_mw)) { + ret = -EINVAL ; + goto port_unlock; + } + +reinit_completion (&port->pps_complete); + port->supply_voltage = out_volt; + port->pps_status =0 ; + port->pps_pending = true; +tcpm_set_state (port,SNK_NEGOTIATE_PPS_CAPABILITIES ,0 ); +mutex_unlock (&port->lock); + +if (!wait_for_completion_timeout (&port->pps_complete, +msecs_to_jiffies (PD_STATE_MACHINE_TIMEOUT ))) + ret = -ETIMEDOUT ; +else + ret = port->pps_status; + + goto swap_unlock; + +port_unlock: +mutex_unlock (&port->lock); +swap_unlock: +mutex_unlock (&port->swap_lock); + +return ret; +} + +static int tcpm_pps_activate (struct tcpm_port *port,bool activate) +{ +int ret =0 ; + +mutex_lock (&port->swap_lock); +mutex_lock (&port->lock); + +if (!port->pps_data.supported) { + ret = -EOPNOTSUPP ; + goto port_unlock; + } + +/* Trying to deactivate PPS when already deactivated so just bail */ +if ((!port->pps_data.active) && (!activate)) + goto port_unlock; + +if (port->state !=SNK_READY ) { + ret = -EAGAIN ; + goto port_unlock; + } + +reinit_completion (&port->pps_complete); + port->pps_status =0 ; + port->pps_pending = true; + +/* Trigger PPS request or move back to standard PDO contract */ +if (activate) +tcpm_set_state (port,SNK_NEGOTIATE_PPS_CAPABILITIES ,0 ); +else +tcpm_set_state (port,SNK_NEGOTIATE_CAPABILITIES ,0 ); +mutex_unlock (&port->lock); + +if (!wait_for_completion_timeout (&port->pps_complete, +msecs_to_jiffies (PD_STATE_MACHINE_TIMEOUT ))) + ret = -ETIMEDOUT ; +else + ret = port->pps_status; + + goto swap_unlock; + +port_unlock: +mutex_unlock (&port->lock); +swap_unlock: +mutex_unlock (&port->swap_lock); + +return ret; +} +static void tcpm_init (struct tcpm_port *port) {enum typec_cc_status cc1, cc2;
@@ -3482,13 +3891,18 @@ void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, port->max_snk_ma = max_snk_ma; port->max_snk_mw = max_snk_mw; port->operating_snk_mw = operating_snk_mw; + port->update_sink_caps = true;switch (port->state) {case SNK_NEGOTIATE_CAPABILITIES : +case SNK_NEGOTIATE_PPS_CAPABILITIES :case SNK_READY :case SNK_TRANSITION_SINK :case SNK_TRANSITION_SINK_VBUS : -tcpm_set_state (port,SNK_NEGOTIATE_CAPABILITIES ,0 ); +if (port->pps_data.active) +tcpm_set_state (port,SNK_NEGOTIATE_PPS_CAPABILITIES ,0 ); +else +tcpm_set_state (port,SNK_NEGOTIATE_CAPABILITIES ,0 );break ;default :break ;
@@ -3529,6 +3943,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)init_completion (&port->tx_complete);init_completion (&port->swap_complete); +init_completion (&port->pps_complete); port->nr_src_pdo =tcpm_copy_pdos (port->src_pdo, tcpc->config->src_pdo, tcpc->config->nr_src_pdo);
@@ -3549,7 +3964,7 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) port->typec_caps.prefer_role = tcpc->config->default_role; port->typec_caps.type = tcpc->config->type; port->typec_caps.revision =0x0120 ;/* Type-C spec release 1.2 */ - port->typec_caps.pd_revision =0x0200 ;/* USB-PD spec release 2.0 */ + port->typec_caps.pd_revision =0x0300 ;/* USB-PD spec release 3.0 */ port->typec_caps.dr_set = tcpm_dr_set; port->typec_caps.pr_set = tcpm_pr_set; port->typec_caps.vconn_set = tcpm_vconn_set;
include/linux/usb/tcpm.h +1 -1
@@ -35,7 +35,7 @@ enum typec_cc_polarity {/* Time to wait for TCPC to complete transmit */ #define PD_T_TCPC_TX_TIMEOUT 100/* in ms */ -#define PD_ROLE_SWAP_TIMEOUT (MSEC_PER_SEC * 10) +#define PD_STATE_MACHINE_TIMEOUT (MSEC_PER_SEC * 10)enum tcpm_transmit_status {TCPC_TX_SUCCESS =0 ,
Signature · 1 line
1.9.1
RFC PATCH 5/7 power: supply: Add type for USB PD PPS chargers
In reply to Adam ThomsonThis adds a type to represent USB PPS chargers as defined in the USB Power Delivery Specification Revision 3.0 V1.1
--- drivers/power/supply/power_supply_sysfs.c | 2 +- include/linux/power_supply.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/power/supply/power_supply_sysfs.c +1 -1
@@ -43,7 +43,7 @@static const char *const power_supply_type_text[] = {"Unknown" ,"Battery" ,"UPS" ,"Mains" ,"USB" ,"USB_DCP" ,"USB_CDP" ,"USB_ACA" ,"USB_C" , -"USB_PD" ,"USB_PD_DRP" ,"BrickID" +"USB_PD" ,"USB_PD_DRP" ,"USB_PD_PPS" ,"BrickID" };static const char *const power_supply_status_text[] = {
include/linux/power_supply.h +1 -0
@@ -167,6 +167,7 @@ enum power_supply_type {POWER_SUPPLY_TYPE_USB_TYPE_C ,/* Type C Port */ POWER_SUPPLY_TYPE_USB_PD ,/* Power Delivery Port */ POWER_SUPPLY_TYPE_USB_PD_DRP ,/* PD Dual Role Port */ +POWER_SUPPLY_TYPE_USB_PD_PPS ,/* PD Programmable Power Supply */ POWER_SUPPLY_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */ };
Signature · 1 line
1.9.1
Re: [RFC PATCH 5/7] power: supply: Add type for USB PD PPS chargers
Hi,
Quoted 4 lines · Adam Thomson
This adds a type to represent USB PPS chargers as defined in the USB Power Delivery Specification Revision 3.0 V1.1
Signed-off-by Adam Thomson <[email protected]>
-- Sebastian
Quoted 32 lines
drivers/power/supply/power_supply_sysfs.c | 2 +- include/linux/power_supply.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)drivers/power/supply/power_supply_sysfs.c +1
-1@@ -43,7 +43,7 @@static const char *const power_supply_type_text[] = {"Unknown" ,"Battery" ,"UPS" ,"Mains" ,"USB" ,"USB_DCP" ,"USB_CDP" ,"USB_ACA" ,"USB_C" , -"USB_PD" ,"USB_PD_DRP" ,"BrickID" +"USB_PD" ,"USB_PD_DRP" ,"USB_PD_PPS" ,"BrickID" };static const char *const power_supply_status_text[] = {include/linux/power_supply.h +1
-0@@ -167,6 +167,7 @@ enum power_supply_type {POWER_SUPPLY_TYPE_USB_TYPE_C ,/* Type C Port */ POWER_SUPPLY_TYPE_USB_PD ,/* Power Delivery Port */ POWER_SUPPLY_TYPE_USB_PD_DRP ,/* PD Dual Role Port */ +POWER_SUPPLY_TYPE_USB_PD_PPS ,/* PD Programmable Power Supply */ POWER_SUPPLY_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */ };Signature · 1 line
1.9.1
RE: [RFC PATCH 5/7] power: supply: Add type for USB PD PPS chargers
Quoted 12 lines · Sebastian Reichel
Hi,
Quoted 4 lines · Adam Thomson
This adds a type to represent USB PPS chargers as defined in the USB Power Delivery Specification Revision 3.0 V1.1
Signed-off-by Adam Thomson <[email protected]>-- Sebastian
Thanks for reviewing
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RFC PATCH 6/7 typec: tcpm: Represent source supply through power_supply class
In reply to Adam ThomsonThis commit adds a power_supply class instance to represent a PD source's voltage and current properties. This provides an interface for reading these properties from user-space or other drivers.
For PPS enabled Sources, this also provides write access to set the current and voltage and allows for swapping between standard PDO and PPS APDO.
As this represents a superset of the information provided in the fusb302 driver, the power_supply instance in that code is removed as part of this change, so reverting the commit titled 'typec: tcpm: Represent source supply through power_supply class'
--- drivers/usb/typec/Kconfig | 1 + drivers/usb/typec/fusb302/Kconfig | 2 +- drivers/usb/typec/fusb302/fusb302.c | 63 +--------- drivers/usb/typec/tcpm.c | 225 +++++++++++++++++++++++++++++++++++- 4 files changed, 228 insertions(+), 63 deletions(-)
drivers/usb/typec/Kconfig +1 -0
@@ -8,6 +8,7 @@ config TYPEC_TCPM tristate "USB Type-C Port Controller Manager" depends on USB select TYPEC + select POWER_SUPPLY help The Type-C Port Controller Manager provides a USB PD and USB Type-C state machine for use with Type-C Port Controllers.
drivers/usb/typec/fusb302/Kconfig +1 -1
@@ -1,6 +1,6 @@ config TYPEC_FUSB302 tristate "Fairchild FUSB302 Type-C chip driver" - depends on I2C && POWER_SUPPLY + depends on I2C help The Fairchild FUSB302 Type-C chip driver that works with Type-C Port Controller Manager to provide USB PD and USB
drivers/usb/typec/fusb302/fusb302.c +2 -61
108 lines not on this page · raw message
drivers/usb/typec/tcpm.c +224 -1
274 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH 7/7 typec: tcpm: Add support for sink PPS related messages
In reply to Adam ThomsonThis commit adds sink side support for Get_Status, Status, Get_PPS_Status and PPS_Status handling. As there's the potential for a partner to respond with Not_Supported handling of this message is also added. Sending of Not_Supported is added is added to handle messages received but not yet handled.
--- drivers/usb/typec/tcpm.c | 152 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 143 insertions(+), 9 deletions(-)
drivers/usb/typec/tcpm.c +143 -9
@@ -28,7 +28,9 @@#include <linux/slab.h> #include <linux/spinlock.h> #include <linux/usb/pd.h> +#include <linux/usb/pd_ado.h> #include <linux/usb/pd_bdo.h> +#include <linux/usb/pd_ext_sdb.h> #include <linux/usb/pd_vdo.h> #include <linux/usb/tcpm.h> #include <linux/usb/typec.h>
@@ -122,6 +124,11 @@S (SNK_TRYWAIT_VBUS ), \S (BIST_RX ), \ \ +S (GET_STATUS_SEND ), \ +S (GET_STATUS_SEND_TIMEOUT ), \ +S (GET_PPS_STATUS_SEND ), \ +S (GET_PPS_STATUS_SEND_TIMEOUT ), \ + \S (ERROR_RECOVERY ), \S (PORT_RESET ), \S (PORT_RESET_WAIT_OFF )
@@ -152,6 +159,7 @@ enum pd_msg_request {PD_MSG_NONE =0 ,PD_MSG_CTRL_REJECT ,PD_MSG_CTRL_WAIT , +PD_MSG_CTRL_NOT_SUPP ,PD_MSG_DATA_SINK_CAP , PD_MSG_DATA_SOURCE_CAP, };
@@ -1288,10 +1296,42 @@ static void vdm_state_machine_work(struct work_struct *work)/* * PD (data, control) command handling functions */ +static inline enum tcpm_state ready_state (struct tcpm_port *port) +{ +if (port->pwr_role ==TYPEC_SOURCE ) +return SRC_READY ; +else +return SNK_READY ; +}static int tcpm_pd_send_control (struct tcpm_port *port,enum pd_ctrl_msg_type type); +static void tcpm_handle_alert (struct tcpm_port *port,const __le32 *payload, +int cnt) +{ +u32 p0 =le32_to_cpu (payload[0 ]); +unsigned int type =ado_type (p0); + +if (!type) { +tcpm_log (port,"Alert message received with no type" ); +return ; + } + +/* Just handling non-battery alerts for now */ +if (!(type &ADO_TYPE_BATT_STATUS_CHANGE )) { +switch (port->state) { +case SRC_READY : +case SNK_READY : +tcpm_set_state (port,GET_STATUS_SEND ,0 ); +break ; +default : +tcpm_queue_message (port,PD_MSG_CTRL_WAIT ); +break ; + } + } +} +static void tcpm_pd_data_request (struct tcpm_port *port,const struct pd_message *msg) {
@@ -1350,6 +1390,14 @@ static void tcpm_pd_data_request(struct tcpm_port *port,tcpm_set_state (port,BIST_RX ,0 ); }break ; +case PD_DATA_ALERT : +tcpm_handle_alert (port, msg->payload, cnt); +break ; +case PD_DATA_BATT_STATUS : +case PD_DATA_GET_COUNTRY_INFO : +tcpm_queue_message (port,PD_MSG_CTRL_NOT_SUPP ); +/* Currently unsupported */ +break ;default :tcpm_log (port,"Unhandled data message type %#x" , type);break ;
@@ -1432,6 +1480,7 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,break ;case PD_CTRL_REJECT :case PD_CTRL_WAIT : +case PD_CTRL_NOT_SUPP :switch (port->state) {case SNK_NEGOTIATE_CAPABILITIES :/* USB PD specification, Figure 8-43 */
@@ -1548,12 +1597,84 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,break ; }break ; +case PD_CTRL_GET_SOURCE_CAP_EXT : +case PD_CTRL_GET_STATUS : +case PD_CTRL_FR_SWAP : +case PD_CTRL_GET_PPS_STATUS : +case PD_CTRL_GET_COUNTRY_CODES : +/* Currently not supported */ +tcpm_queue_message (port,PD_MSG_CTRL_NOT_SUPP ); +break ;default :tcpm_log (port,"Unhandled ctrl message type %#x" , type);break ; } } +static void tcpm_pd_ext_msg_request (struct tcpm_port *port, +const struct pd_message *msg) +{ +enum pd_ext_msg_type type =pd_header_type_le (msg->header); +unsigned int data_size =pd_ext_header_data_size_le (msg->ext_msg.header); +u8 *data; + +if (!(msg->ext_msg.header &&PD_EXT_HDR_CHUNKED )) { +tcpm_log (port,"Unchunked extended messages unsupported" ); +return ; + } + +if (data_size > (PD_EXT_MAX_LEGACY_DATA )) { +tcpm_log (port,"Chunk handling not yet supported" ); +return ; + } + + data =kzalloc (data_size,GFP_KERNEL ); +if (!data) { +tcpm_log (port,"Failed to allocate memory for ext msg data" ); +return ; + } +memcpy (data, msg->ext_msg.data, data_size); + +switch (type) { +case PD_EXT_STATUS : +/* +* If PPS related events raised then get PPS status to clear +* (see USB PD 3.0 Spec, 6.5.2.4) +*/ +if (data[EXT_SDB_EVENT_FLAGS ] &EXT_SDB_PPS_EVENTS ) +tcpm_set_state (port,GET_PPS_STATUS_SEND ,0 ); +else +tcpm_set_state (port,ready_state (port),0 ); +break ; +case PD_EXT_PPS_STATUS : +/* +* For now the PPS status message is used to clear events +* and nothing more. +*/ +tcpm_set_state (port,ready_state (port),0 ); +break ; +case PD_EXT_SOURCE_CAP_EXT : +case PD_EXT_GET_BATT_CAP : +case PD_EXT_GET_BATT_STATUS : +case PD_EXT_BATT_CAP : +case PD_EXT_GET_MANUFACTURER_INFO : +case PD_EXT_MANUFACTURER_INFO : +case PD_EXT_SECURITY_REQUEST : +case PD_EXT_SECURITY_RESPONSE : +case PD_EXT_FW_UPDATE_REQUEST : +case PD_EXT_FW_UPDATE_RESPONSE : +case PD_EXT_COUNTRY_INFO : +case PD_EXT_COUNTRY_CODES : +tcpm_queue_message (port,PD_MSG_CTRL_NOT_SUPP ); +break ; +default : +tcpm_log (port,"Unhandle extended message type %#x" , type); +break ; + } + +kfree (data); +} +static void tcpm_pd_rx_handler (struct work_struct *work) { struct pd_rx_event *event = container_of(work,
@@ -1594,7 +1715,9 @@ static void tcpm_pd_rx_handler(struct work_struct *work) "Data role mismatch, initiating error recovery");tcpm_set_state (port,ERROR_RECOVERY ,0 ); }else { -if (cnt) +if (msg->header &PD_HEADER_EXT_HDR ) +tcpm_pd_ext_msg_request (port, msg); +else if (cnt)tcpm_pd_data_request (port, msg);else tcpm_pd_ctrl_request (port, msg);
@@ -1654,6 +1777,9 @@ static bool tcpm_send_queued_message(struct tcpm_port *port)case PD_MSG_CTRL_REJECT :tcpm_pd_send_control (port,PD_CTRL_REJECT );break ; +case PD_MSG_CTRL_NOT_SUPP : +tcpm_pd_send_control (port,PD_CTRL_NOT_SUPP ); +break ;case PD_MSG_DATA_SINK_CAP :tcpm_pd_send_sink_caps (port);break ;
@@ -2324,14 +2450,6 @@ static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)return SNK_UNATTACHED ; } -static inline enum tcpm_state ready_state (struct tcpm_port *port) -{ -if (port->pwr_role ==TYPEC_SOURCE ) -return SRC_READY ; -else -return SNK_READY ; -} -static inline enum tcpm_state unattached_state (struct tcpm_port *port) {if (port->port_type ==TYPEC_PORT_DRP ) {
@@ -3032,6 +3150,22 @@ static void run_state_machine(struct tcpm_port *port)/* Always switch to unattached state */ tcpm_set_state (port,unattached_state (port),0 );break ; +case GET_STATUS_SEND : +tcpm_pd_send_control (port,PD_CTRL_GET_STATUS ); +tcpm_set_state (port,GET_STATUS_SEND_TIMEOUT , +PD_T_SENDER_RESPONSE ); +break ; +case GET_STATUS_SEND_TIMEOUT : +tcpm_set_state (port,ready_state (port),0 ); +break ; +case GET_PPS_STATUS_SEND : +tcpm_pd_send_control (port,PD_CTRL_GET_PPS_STATUS ); +tcpm_set_state (port,GET_PPS_STATUS_SEND_TIMEOUT , +PD_T_SENDER_RESPONSE ); +break ; +case GET_PPS_STATUS_SEND_TIMEOUT : +tcpm_set_state (port,ready_state (port),0 ); +break ;case ERROR_RECOVERY :tcpm_swap_complete (port, -EPROTO );tcpm_pps_complete (port, -EPROTO );
Signature · 1 line
1.9.1