Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2016-2017, Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/hrtimer.h> |
| 16 | #include <linux/ipc_logging.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/power_supply.h> |
| 23 | #include <linux/regulator/consumer.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/extcon.h> |
| 28 | #include <linux/usb/class-dual-role.h> |
| 29 | #include <linux/usb/usbpd.h> |
| 30 | #include "usbpd.h" |
| 31 | |
Vijayavardhan Vennapusa | 34a1f1d | 2017-05-31 12:00:37 +0530 | [diff] [blame] | 32 | /* To start USB stack for USB3.1 complaince testing */ |
| 33 | static bool usb_compliance_mode; |
| 34 | module_param(usb_compliance_mode, bool, 0644); |
| 35 | MODULE_PARM_DESC(usb_compliance_mode, "Start USB stack for USB3.1 compliance testing"); |
| 36 | |
Vijayavardhan Vennapusa | 49f21ce | 2017-06-05 14:47:56 +0530 | [diff] [blame] | 37 | static bool disable_usb_pd; |
| 38 | module_param(disable_usb_pd, bool, 0644); |
| 39 | MODULE_PARM_DESC(disable_usb_pd, "Disable USB PD for USB3.1 compliance testing"); |
| 40 | |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 41 | static bool rev3_sink_only; |
| 42 | module_param(rev3_sink_only, bool, 0644); |
| 43 | MODULE_PARM_DESC(rev3_sink_only, "Enable power delivery rev3.0 sink only mode"); |
| 44 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 45 | enum usbpd_state { |
| 46 | PE_UNKNOWN, |
| 47 | PE_ERROR_RECOVERY, |
| 48 | PE_SRC_DISABLED, |
| 49 | PE_SRC_STARTUP, |
| 50 | PE_SRC_SEND_CAPABILITIES, |
| 51 | PE_SRC_SEND_CAPABILITIES_WAIT, /* substate to wait for Request */ |
| 52 | PE_SRC_NEGOTIATE_CAPABILITY, |
| 53 | PE_SRC_TRANSITION_SUPPLY, |
| 54 | PE_SRC_READY, |
| 55 | PE_SRC_HARD_RESET, |
| 56 | PE_SRC_SOFT_RESET, |
| 57 | PE_SRC_SEND_SOFT_RESET, |
| 58 | PE_SRC_DISCOVERY, |
| 59 | PE_SRC_TRANSITION_TO_DEFAULT, |
| 60 | PE_SNK_STARTUP, |
| 61 | PE_SNK_DISCOVERY, |
| 62 | PE_SNK_WAIT_FOR_CAPABILITIES, |
| 63 | PE_SNK_EVALUATE_CAPABILITY, |
| 64 | PE_SNK_SELECT_CAPABILITY, |
| 65 | PE_SNK_TRANSITION_SINK, |
| 66 | PE_SNK_READY, |
| 67 | PE_SNK_HARD_RESET, |
| 68 | PE_SNK_SOFT_RESET, |
| 69 | PE_SNK_SEND_SOFT_RESET, |
| 70 | PE_SNK_TRANSITION_TO_DEFAULT, |
| 71 | PE_DRS_SEND_DR_SWAP, |
| 72 | PE_PRS_SNK_SRC_SEND_SWAP, |
| 73 | PE_PRS_SNK_SRC_TRANSITION_TO_OFF, |
| 74 | PE_PRS_SNK_SRC_SOURCE_ON, |
| 75 | PE_PRS_SRC_SNK_SEND_SWAP, |
| 76 | PE_PRS_SRC_SNK_TRANSITION_TO_OFF, |
| 77 | PE_PRS_SRC_SNK_WAIT_SOURCE_ON, |
| 78 | PE_VCS_WAIT_FOR_VCONN, |
| 79 | }; |
| 80 | |
| 81 | static const char * const usbpd_state_strings[] = { |
| 82 | "UNKNOWN", |
| 83 | "ERROR_RECOVERY", |
| 84 | "SRC_Disabled", |
| 85 | "SRC_Startup", |
| 86 | "SRC_Send_Capabilities", |
| 87 | "SRC_Send_Capabilities (Wait for Request)", |
| 88 | "SRC_Negotiate_Capability", |
| 89 | "SRC_Transition_Supply", |
| 90 | "SRC_Ready", |
| 91 | "SRC_Hard_Reset", |
| 92 | "SRC_Soft_Reset", |
| 93 | "SRC_Send_Soft_Reset", |
| 94 | "SRC_Discovery", |
| 95 | "SRC_Transition_to_default", |
| 96 | "SNK_Startup", |
| 97 | "SNK_Discovery", |
| 98 | "SNK_Wait_for_Capabilities", |
| 99 | "SNK_Evaluate_Capability", |
| 100 | "SNK_Select_Capability", |
| 101 | "SNK_Transition_Sink", |
| 102 | "SNK_Ready", |
| 103 | "SNK_Hard_Reset", |
| 104 | "SNK_Soft_Reset", |
| 105 | "SNK_Send_Soft_Reset", |
| 106 | "SNK_Transition_to_default", |
| 107 | "DRS_Send_DR_Swap", |
| 108 | "PRS_SNK_SRC_Send_Swap", |
| 109 | "PRS_SNK_SRC_Transition_to_off", |
| 110 | "PRS_SNK_SRC_Source_on", |
| 111 | "PRS_SRC_SNK_Send_Swap", |
| 112 | "PRS_SRC_SNK_Transition_to_off", |
| 113 | "PRS_SRC_SNK_Wait_Source_on", |
| 114 | "VCS_Wait_for_VCONN", |
| 115 | }; |
| 116 | |
| 117 | enum usbpd_control_msg_type { |
| 118 | MSG_RESERVED = 0, |
| 119 | MSG_GOODCRC, |
| 120 | MSG_GOTOMIN, |
| 121 | MSG_ACCEPT, |
| 122 | MSG_REJECT, |
| 123 | MSG_PING, |
| 124 | MSG_PS_RDY, |
| 125 | MSG_GET_SOURCE_CAP, |
| 126 | MSG_GET_SINK_CAP, |
| 127 | MSG_DR_SWAP, |
| 128 | MSG_PR_SWAP, |
| 129 | MSG_VCONN_SWAP, |
| 130 | MSG_WAIT, |
| 131 | MSG_SOFT_RESET, |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 132 | MSG_NOT_SUPPORTED = 0x10, |
| 133 | MSG_GET_SOURCE_CAP_EXTENDED, |
| 134 | MSG_GET_STATUS, |
| 135 | MSG_FR_SWAP, |
| 136 | MSG_GET_PPS_STATUS, |
| 137 | MSG_GET_COUNTRY_CODES, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | enum usbpd_data_msg_type { |
| 141 | MSG_SOURCE_CAPABILITIES = 1, |
| 142 | MSG_REQUEST, |
| 143 | MSG_BIST, |
| 144 | MSG_SINK_CAPABILITIES, |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 145 | MSG_BATTERY_STATUS, |
| 146 | MSG_ALERT, |
| 147 | MSG_GET_COUNTRY_INFO, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 148 | MSG_VDM = 0xF, |
| 149 | }; |
| 150 | |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 151 | enum usbpd_ext_msg_type { |
| 152 | MSG_SOURCE_CAPABILITIES_EXTENDED = 1, |
| 153 | MSG_STATUS, |
| 154 | MSG_GET_BATTERY_CAP, |
| 155 | MSG_GET_BATTERY_STATUS, |
| 156 | MSG_BATTERY_CAPABILITIES, |
| 157 | MSG_GET_MANUFACTURER_INFO, |
| 158 | MSG_MANUFACTURER_INFO, |
| 159 | MSG_SECURITY_REQUEST, |
| 160 | MSG_SECURITY_RESPONSE, |
| 161 | MSG_FIRMWARE_UPDATE_REQUEST, |
| 162 | MSG_FIRMWARE_UPDATE_RESPONSE, |
| 163 | MSG_PPS_STATUS, |
| 164 | MSG_COUNTRY_INFO, |
| 165 | MSG_COUNTRY_CODES, |
| 166 | }; |
| 167 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 168 | enum vdm_state { |
| 169 | VDM_NONE, |
| 170 | DISCOVERED_ID, |
| 171 | DISCOVERED_SVIDS, |
| 172 | DISCOVERED_MODES, |
| 173 | MODE_ENTERED, |
| 174 | MODE_EXITED, |
| 175 | }; |
| 176 | |
| 177 | static void *usbpd_ipc_log; |
| 178 | #define usbpd_dbg(dev, fmt, ...) do { \ |
| 179 | ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \ |
| 180 | ##__VA_ARGS__); \ |
| 181 | dev_dbg(dev, fmt, ##__VA_ARGS__); \ |
| 182 | } while (0) |
| 183 | |
| 184 | #define usbpd_info(dev, fmt, ...) do { \ |
| 185 | ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \ |
| 186 | ##__VA_ARGS__); \ |
| 187 | dev_info(dev, fmt, ##__VA_ARGS__); \ |
| 188 | } while (0) |
| 189 | |
| 190 | #define usbpd_warn(dev, fmt, ...) do { \ |
| 191 | ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \ |
| 192 | ##__VA_ARGS__); \ |
| 193 | dev_warn(dev, fmt, ##__VA_ARGS__); \ |
| 194 | } while (0) |
| 195 | |
| 196 | #define usbpd_err(dev, fmt, ...) do { \ |
| 197 | ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \ |
| 198 | ##__VA_ARGS__); \ |
| 199 | dev_err(dev, fmt, ##__VA_ARGS__); \ |
| 200 | } while (0) |
| 201 | |
| 202 | #define NUM_LOG_PAGES 10 |
| 203 | |
| 204 | /* Timeouts (in ms) */ |
| 205 | #define ERROR_RECOVERY_TIME 25 |
| 206 | #define SENDER_RESPONSE_TIME 26 |
| 207 | #define SINK_WAIT_CAP_TIME 500 |
| 208 | #define PS_TRANSITION_TIME 450 |
| 209 | #define SRC_CAP_TIME 120 |
| 210 | #define SRC_TRANSITION_TIME 25 |
| 211 | #define SRC_RECOVER_TIME 750 |
| 212 | #define PS_HARD_RESET_TIME 25 |
| 213 | #define PS_SOURCE_ON 400 |
| 214 | #define PS_SOURCE_OFF 750 |
Jack Pham | 3512134 | 2017-07-06 00:12:15 -0700 | [diff] [blame] | 215 | #define FIRST_SOURCE_CAP_TIME 200 |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 216 | #define VDM_BUSY_TIME 50 |
| 217 | #define VCONN_ON_TIME 100 |
| 218 | |
| 219 | /* tPSHardReset + tSafe0V */ |
| 220 | #define SNK_HARD_RESET_VBUS_OFF_TIME (35 + 650) |
| 221 | |
| 222 | /* tSrcRecover + tSrcTurnOn */ |
| 223 | #define SNK_HARD_RESET_VBUS_ON_TIME (1000 + 275) |
| 224 | |
| 225 | #define PD_CAPS_COUNT 50 |
| 226 | |
| 227 | #define PD_MAX_MSG_ID 7 |
| 228 | |
Hemant Kumar | 796534e | 2017-05-30 15:54:55 -0700 | [diff] [blame] | 229 | #define PD_MAX_DATA_OBJ 7 |
| 230 | |
Hemant Kumar | 018b598 | 2017-08-09 14:14:37 -0700 | [diff] [blame] | 231 | #define PD_SRC_CAP_EXT_DB_LEN 24 |
Hemant Kumar | a187594 | 2017-08-09 16:50:14 -0700 | [diff] [blame] | 232 | #define PD_STATUS_DB_LEN 5 |
Hemant Kumar | 51ded97 | 2017-08-09 17:57:24 -0700 | [diff] [blame] | 233 | #define PD_BATTERY_CAP_DB_LEN 9 |
Hemant Kumar | 018b598 | 2017-08-09 14:14:37 -0700 | [diff] [blame] | 234 | |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 235 | #define PD_MAX_EXT_MSG_LEN 260 |
| 236 | #define PD_MAX_EXT_MSG_LEGACY_LEN 26 |
| 237 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 238 | #define PD_MSG_HDR(type, dr, pr, id, cnt, rev) \ |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 239 | (((type) & 0x1F) | ((dr) << 5) | (rev << 6) | \ |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 240 | ((pr) << 8) | ((id) << 9) | ((cnt) << 12)) |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 241 | #define PD_MSG_HDR_COUNT(hdr) (((hdr) >> 12) & 7) |
| 242 | #define PD_MSG_HDR_TYPE(hdr) ((hdr) & 0x1F) |
| 243 | #define PD_MSG_HDR_ID(hdr) (((hdr) >> 9) & 7) |
| 244 | #define PD_MSG_HDR_REV(hdr) (((hdr) >> 6) & 3) |
| 245 | #define PD_MSG_HDR_EXTENDED BIT(15) |
| 246 | #define PD_MSG_HDR_IS_EXTENDED(hdr) ((hdr) & PD_MSG_HDR_EXTENDED) |
| 247 | |
| 248 | #define PD_MSG_EXT_HDR(chunked, num, req, size) \ |
| 249 | (((chunked) << 15) | (((num) & 0xF) << 11) | \ |
| 250 | ((req) << 10) | ((size) & 0x1FF)) |
| 251 | #define PD_MSG_EXT_HDR_IS_CHUNKED(ehdr) ((ehdr) & 0x8000) |
| 252 | #define PD_MSG_EXT_HDR_CHUNK_NUM(ehdr) (((ehdr) >> 11) & 0xF) |
| 253 | #define PD_MSG_EXT_HDR_REQ_CHUNK(ehdr) ((ehdr) & 0x400) |
| 254 | #define PD_MSG_EXT_HDR_DATA_SIZE(ehdr) ((ehdr) & 0x1FF) |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 255 | |
| 256 | #define PD_RDO_FIXED(obj, gb, mismatch, usb_comm, no_usb_susp, curr1, curr2) \ |
| 257 | (((obj) << 28) | ((gb) << 27) | ((mismatch) << 26) | \ |
| 258 | ((usb_comm) << 25) | ((no_usb_susp) << 24) | \ |
| 259 | ((curr1) << 10) | (curr2)) |
| 260 | |
| 261 | #define PD_RDO_AUGMENTED(obj, mismatch, usb_comm, no_usb_susp, volt, curr) \ |
| 262 | (((obj) << 28) | ((mismatch) << 26) | ((usb_comm) << 25) | \ |
| 263 | ((no_usb_susp) << 24) | ((volt) << 9) | (curr)) |
| 264 | |
| 265 | #define PD_RDO_OBJ_POS(rdo) ((rdo) >> 28 & 7) |
| 266 | #define PD_RDO_GIVEBACK(rdo) ((rdo) >> 27 & 1) |
| 267 | #define PD_RDO_MISMATCH(rdo) ((rdo) >> 26 & 1) |
| 268 | #define PD_RDO_USB_COMM(rdo) ((rdo) >> 25 & 1) |
| 269 | #define PD_RDO_NO_USB_SUSP(rdo) ((rdo) >> 24 & 1) |
| 270 | #define PD_RDO_FIXED_CURR(rdo) ((rdo) >> 10 & 0x3FF) |
| 271 | #define PD_RDO_FIXED_CURR_MINMAX(rdo) ((rdo) & 0x3FF) |
| 272 | #define PD_RDO_PROG_VOLTAGE(rdo) ((rdo) >> 9 & 0x7FF) |
| 273 | #define PD_RDO_PROG_CURR(rdo) ((rdo) & 0x7F) |
| 274 | |
| 275 | #define PD_SRC_PDO_TYPE(pdo) (((pdo) >> 30) & 3) |
| 276 | #define PD_SRC_PDO_TYPE_FIXED 0 |
| 277 | #define PD_SRC_PDO_TYPE_BATTERY 1 |
| 278 | #define PD_SRC_PDO_TYPE_VARIABLE 2 |
| 279 | #define PD_SRC_PDO_TYPE_AUGMENTED 3 |
| 280 | |
| 281 | #define PD_SRC_PDO_FIXED_PR_SWAP(pdo) (((pdo) >> 29) & 1) |
| 282 | #define PD_SRC_PDO_FIXED_USB_SUSP(pdo) (((pdo) >> 28) & 1) |
| 283 | #define PD_SRC_PDO_FIXED_EXT_POWERED(pdo) (((pdo) >> 27) & 1) |
| 284 | #define PD_SRC_PDO_FIXED_USB_COMM(pdo) (((pdo) >> 26) & 1) |
| 285 | #define PD_SRC_PDO_FIXED_DR_SWAP(pdo) (((pdo) >> 25) & 1) |
| 286 | #define PD_SRC_PDO_FIXED_PEAK_CURR(pdo) (((pdo) >> 20) & 3) |
| 287 | #define PD_SRC_PDO_FIXED_VOLTAGE(pdo) (((pdo) >> 10) & 0x3FF) |
| 288 | #define PD_SRC_PDO_FIXED_MAX_CURR(pdo) ((pdo) & 0x3FF) |
| 289 | |
| 290 | #define PD_SRC_PDO_VAR_BATT_MAX_VOLT(pdo) (((pdo) >> 20) & 0x3FF) |
| 291 | #define PD_SRC_PDO_VAR_BATT_MIN_VOLT(pdo) (((pdo) >> 10) & 0x3FF) |
| 292 | #define PD_SRC_PDO_VAR_BATT_MAX(pdo) ((pdo) & 0x3FF) |
| 293 | |
| 294 | #define PD_APDO_PPS(pdo) (((pdo) >> 28) & 3) |
| 295 | #define PD_APDO_MAX_VOLT(pdo) (((pdo) >> 17) & 0xFF) |
| 296 | #define PD_APDO_MIN_VOLT(pdo) (((pdo) >> 8) & 0xFF) |
| 297 | #define PD_APDO_MAX_CURR(pdo) ((pdo) & 0x7F) |
| 298 | |
| 299 | /* Vendor Defined Messages */ |
| 300 | #define MAX_CRC_RECEIVE_TIME 9 /* ~(2 * tReceive_max(1.1ms) * # retry 4) */ |
| 301 | #define MAX_VDM_RESPONSE_TIME 60 /* 2 * tVDMSenderResponse_max(30ms) */ |
| 302 | #define MAX_VDM_BUSY_TIME 100 /* 2 * tVDMBusy (50ms) */ |
| 303 | |
Jack Pham | ee1f905 | 2017-01-26 12:27:07 -0800 | [diff] [blame] | 304 | #define PD_SNK_PDO_FIXED(prs, hc, uc, usb_comm, drs, volt, curr) \ |
| 305 | (((prs) << 29) | ((hc) << 28) | ((uc) << 27) | ((usb_comm) << 26) | \ |
| 306 | ((drs) << 25) | ((volt) << 10) | (curr)) |
| 307 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 308 | /* VDM header is the first 32-bit object following the 16-bit PD header */ |
| 309 | #define VDM_HDR_SVID(hdr) ((hdr) >> 16) |
| 310 | #define VDM_IS_SVDM(hdr) ((hdr) & 0x8000) |
| 311 | #define SVDM_HDR_OBJ_POS(hdr) (((hdr) >> 8) & 0x7) |
| 312 | #define SVDM_HDR_CMD_TYPE(hdr) (((hdr) >> 6) & 0x3) |
| 313 | #define SVDM_HDR_CMD(hdr) ((hdr) & 0x1f) |
| 314 | |
| 315 | #define SVDM_HDR(svid, ver, obj, cmd_type, cmd) \ |
| 316 | (((svid) << 16) | (1 << 15) | ((ver) << 13) \ |
| 317 | | ((obj) << 8) | ((cmd_type) << 6) | (cmd)) |
| 318 | |
| 319 | /* discover id response vdo bit fields */ |
| 320 | #define ID_HDR_USB_HOST BIT(31) |
| 321 | #define ID_HDR_USB_DEVICE BIT(30) |
| 322 | #define ID_HDR_MODAL_OPR BIT(26) |
| 323 | #define ID_HDR_PRODUCT_TYPE(n) ((n) >> 27) |
| 324 | #define ID_HDR_PRODUCT_PER_MASK (2 << 27) |
| 325 | #define ID_HDR_PRODUCT_HUB 1 |
| 326 | #define ID_HDR_PRODUCT_PER 2 |
| 327 | #define ID_HDR_PRODUCT_AMA 5 |
| 328 | #define ID_HDR_VID 0x05c6 /* qcom */ |
| 329 | #define PROD_VDO_PID 0x0a00 /* TBD */ |
| 330 | |
| 331 | static bool check_vsafe0v = true; |
| 332 | module_param(check_vsafe0v, bool, 0600); |
| 333 | |
| 334 | static int min_sink_current = 900; |
| 335 | module_param(min_sink_current, int, 0600); |
| 336 | |
| 337 | static const u32 default_src_caps[] = { 0x36019096 }; /* VSafe5V @ 1.5A */ |
Jack Pham | ee1f905 | 2017-01-26 12:27:07 -0800 | [diff] [blame] | 338 | static const u32 default_snk_caps[] = { 0x2601912C }; /* VSafe5V @ 3A */ |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 339 | |
| 340 | struct vdm_tx { |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 341 | u32 data[PD_MAX_DATA_OBJ]; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 342 | int size; |
| 343 | }; |
| 344 | |
| 345 | struct rx_msg { |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 346 | u16 hdr; |
| 347 | u16 data_len; /* size of payload in bytes */ |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 348 | struct list_head entry; |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 349 | u8 payload[]; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 350 | }; |
| 351 | |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 352 | #define IS_DATA(m, t) ((m) && !PD_MSG_HDR_IS_EXTENDED((m)->hdr) && \ |
| 353 | PD_MSG_HDR_COUNT((m)->hdr) && \ |
| 354 | (PD_MSG_HDR_TYPE((m)->hdr) == (t))) |
| 355 | #define IS_CTRL(m, t) ((m) && !PD_MSG_HDR_COUNT((m)->hdr) && \ |
| 356 | (PD_MSG_HDR_TYPE((m)->hdr) == (t))) |
| 357 | #define IS_EXT(m, t) ((m) && PD_MSG_HDR_IS_EXTENDED((m)->hdr) && \ |
| 358 | (PD_MSG_HDR_TYPE((m)->hdr) == (t))) |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 359 | |
| 360 | struct usbpd { |
| 361 | struct device dev; |
| 362 | struct workqueue_struct *wq; |
| 363 | struct work_struct sm_work; |
| 364 | struct hrtimer timer; |
| 365 | bool sm_queued; |
| 366 | |
| 367 | struct extcon_dev *extcon; |
| 368 | |
| 369 | enum usbpd_state current_state; |
| 370 | bool hard_reset_recvd; |
Jack Pham | 4b32328 | 2017-11-03 12:24:59 -0700 | [diff] [blame] | 371 | ktime_t hard_reset_recvd_time; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 372 | struct list_head rx_q; |
| 373 | spinlock_t rx_lock; |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 374 | struct rx_msg *rx_ext_msg; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 375 | |
Hemant Kumar | 796534e | 2017-05-30 15:54:55 -0700 | [diff] [blame] | 376 | u32 received_pdos[PD_MAX_DATA_OBJ]; |
Hemant Kumar | a187594 | 2017-08-09 16:50:14 -0700 | [diff] [blame] | 377 | u32 received_ado; |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 378 | u16 src_cap_id; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 379 | u8 selected_pdo; |
| 380 | u8 requested_pdo; |
| 381 | u32 rdo; /* can be either source or sink */ |
| 382 | int current_voltage; /* uV */ |
| 383 | int requested_voltage; /* uV */ |
| 384 | int requested_current; /* mA */ |
| 385 | bool pd_connected; |
| 386 | bool in_explicit_contract; |
| 387 | bool peer_usb_comm; |
| 388 | bool peer_pr_swap; |
| 389 | bool peer_dr_swap; |
| 390 | |
Jack Pham | ee1f905 | 2017-01-26 12:27:07 -0800 | [diff] [blame] | 391 | u32 sink_caps[7]; |
| 392 | int num_sink_caps; |
| 393 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 394 | struct power_supply *usb_psy; |
| 395 | struct notifier_block psy_nb; |
| 396 | |
| 397 | enum power_supply_typec_mode typec_mode; |
| 398 | enum power_supply_type psy_type; |
| 399 | enum power_supply_typec_power_role forced_pr; |
| 400 | bool vbus_present; |
| 401 | |
| 402 | enum pd_spec_rev spec_rev; |
| 403 | enum data_role current_dr; |
| 404 | enum power_role current_pr; |
| 405 | bool in_pr_swap; |
| 406 | bool pd_phy_opened; |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 407 | bool send_request; |
| 408 | struct completion is_ready; |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 409 | struct completion tx_chunk_request; |
| 410 | u8 next_tx_chunk; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 411 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 412 | struct mutex swap_lock; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 413 | struct dual_role_phy_instance *dual_role; |
| 414 | struct dual_role_phy_desc dr_desc; |
| 415 | bool send_pr_swap; |
| 416 | bool send_dr_swap; |
| 417 | |
| 418 | struct regulator *vbus; |
| 419 | struct regulator *vconn; |
| 420 | bool vbus_enabled; |
| 421 | bool vconn_enabled; |
| 422 | bool vconn_is_external; |
| 423 | |
| 424 | u8 tx_msgid; |
| 425 | u8 rx_msgid; |
| 426 | int caps_count; |
| 427 | int hard_reset_count; |
| 428 | |
| 429 | enum vdm_state vdm_state; |
| 430 | u16 *discovered_svids; |
| 431 | int num_svids; |
| 432 | struct vdm_tx *vdm_tx; |
| 433 | struct vdm_tx *vdm_tx_retry; |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 434 | struct mutex svid_handler_lock; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 435 | struct list_head svid_handlers; |
| 436 | |
| 437 | struct list_head instance; |
Mayank Rana | 6af4342 | 2017-07-18 12:09:02 -0700 | [diff] [blame] | 438 | |
| 439 | u16 ss_lane_svid; |
Hemant Kumar | 018b598 | 2017-08-09 14:14:37 -0700 | [diff] [blame] | 440 | |
| 441 | /* ext msg support */ |
| 442 | bool send_get_src_cap_ext; |
| 443 | u8 src_cap_ext_db[PD_SRC_CAP_EXT_DB_LEN]; |
| 444 | bool send_get_pps_status; |
| 445 | u32 pps_status_db; |
Hemant Kumar | a187594 | 2017-08-09 16:50:14 -0700 | [diff] [blame] | 446 | u8 status_db[PD_STATUS_DB_LEN]; |
Hemant Kumar | 51ded97 | 2017-08-09 17:57:24 -0700 | [diff] [blame] | 447 | bool send_get_battery_cap; |
| 448 | u8 get_battery_cap_db; |
| 449 | u8 battery_cap_db[PD_BATTERY_CAP_DB_LEN]; |
| 450 | u8 get_battery_status_db; |
| 451 | bool send_get_battery_status; |
| 452 | u32 battery_sts_dobj; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | static LIST_HEAD(_usbpd); /* useful for debugging */ |
| 456 | |
| 457 | static const unsigned int usbpd_extcon_cable[] = { |
| 458 | EXTCON_USB, |
| 459 | EXTCON_USB_HOST, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 460 | EXTCON_NONE, |
| 461 | }; |
| 462 | |
| 463 | /* EXTCON_USB and EXTCON_USB_HOST are mutually exclusive */ |
| 464 | static const u32 usbpd_extcon_exclusive[] = {0x3, 0}; |
| 465 | |
| 466 | enum plug_orientation usbpd_get_plug_orientation(struct usbpd *pd) |
| 467 | { |
| 468 | int ret; |
| 469 | union power_supply_propval val; |
| 470 | |
| 471 | ret = power_supply_get_property(pd->usb_psy, |
| 472 | POWER_SUPPLY_PROP_TYPEC_CC_ORIENTATION, &val); |
| 473 | if (ret) |
| 474 | return ORIENTATION_NONE; |
| 475 | |
| 476 | return val.intval; |
| 477 | } |
| 478 | EXPORT_SYMBOL(usbpd_get_plug_orientation); |
| 479 | |
| 480 | static inline void stop_usb_host(struct usbpd *pd) |
| 481 | { |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 482 | extcon_set_state_sync(pd->extcon, EXTCON_USB_HOST, 0); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static inline void start_usb_host(struct usbpd *pd, bool ss) |
| 486 | { |
| 487 | enum plug_orientation cc = usbpd_get_plug_orientation(pd); |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 488 | union extcon_property_value val; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 489 | |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 490 | val.intval = (cc == ORIENTATION_CC2); |
| 491 | extcon_set_property(pd->extcon, EXTCON_USB_HOST, |
| 492 | EXTCON_PROP_USB_TYPEC_POLARITY, val); |
| 493 | |
| 494 | val.intval = ss; |
| 495 | extcon_set_property(pd->extcon, EXTCON_USB_HOST, |
| 496 | EXTCON_PROP_USB_SS, val); |
| 497 | |
| 498 | extcon_set_state_sync(pd->extcon, EXTCON_USB_HOST, 1); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static inline void stop_usb_peripheral(struct usbpd *pd) |
| 502 | { |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 503 | extcon_set_state_sync(pd->extcon, EXTCON_USB, 0); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static inline void start_usb_peripheral(struct usbpd *pd) |
| 507 | { |
| 508 | enum plug_orientation cc = usbpd_get_plug_orientation(pd); |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 509 | union extcon_property_value val; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 510 | |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 511 | val.intval = (cc == ORIENTATION_CC2); |
| 512 | extcon_set_property(pd->extcon, EXTCON_USB, |
| 513 | EXTCON_PROP_USB_TYPEC_POLARITY, val); |
| 514 | |
| 515 | val.intval = 1; |
| 516 | extcon_set_property(pd->extcon, EXTCON_USB, EXTCON_PROP_USB_SS, val); |
| 517 | |
| 518 | extcon_set_state_sync(pd->extcon, EXTCON_USB, 1); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Mayank Rana | 6af4342 | 2017-07-18 12:09:02 -0700 | [diff] [blame] | 521 | /** |
| 522 | * This API allows client driver to request for releasing SS lanes. It should |
| 523 | * not be called from atomic context. |
| 524 | * |
| 525 | * @pd - USBPD handler |
| 526 | * @hdlr - client's handler |
| 527 | * |
| 528 | * @returns int - Success - 0, else negative error code |
| 529 | */ |
| 530 | static int usbpd_release_ss_lane(struct usbpd *pd, |
| 531 | struct usbpd_svid_handler *hdlr) |
| 532 | { |
| 533 | int ret = 0; |
| 534 | |
| 535 | if (!hdlr || !pd) |
| 536 | return -EINVAL; |
| 537 | |
| 538 | usbpd_dbg(&pd->dev, "hdlr:%pK svid:%d", hdlr, hdlr->svid); |
| 539 | /* |
| 540 | * If USB SS lanes are already used by one client, and other client is |
| 541 | * requesting for same or same client requesting again, return -EBUSY. |
| 542 | */ |
| 543 | if (pd->ss_lane_svid) { |
| 544 | usbpd_dbg(&pd->dev, "-EBUSY: ss_lanes are already used by(%d)", |
| 545 | pd->ss_lane_svid); |
| 546 | ret = -EBUSY; |
| 547 | goto err_exit; |
| 548 | } |
| 549 | |
| 550 | ret = extcon_blocking_sync(pd->extcon, EXTCON_USB_HOST, 0); |
| 551 | if (ret) { |
| 552 | usbpd_err(&pd->dev, "err(%d) for releasing ss lane", ret); |
| 553 | goto err_exit; |
| 554 | } |
| 555 | |
| 556 | pd->ss_lane_svid = hdlr->svid; |
| 557 | |
| 558 | err_exit: |
| 559 | return ret; |
| 560 | } |
| 561 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 562 | static int set_power_role(struct usbpd *pd, enum power_role pr) |
| 563 | { |
| 564 | union power_supply_propval val = {0}; |
| 565 | |
| 566 | switch (pr) { |
| 567 | case PR_NONE: |
| 568 | val.intval = POWER_SUPPLY_TYPEC_PR_NONE; |
| 569 | break; |
| 570 | case PR_SINK: |
| 571 | val.intval = POWER_SUPPLY_TYPEC_PR_SINK; |
| 572 | break; |
| 573 | case PR_SRC: |
| 574 | val.intval = POWER_SUPPLY_TYPEC_PR_SOURCE; |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | return power_supply_set_property(pd->usb_psy, |
| 579 | POWER_SUPPLY_PROP_TYPEC_POWER_ROLE, &val); |
| 580 | } |
| 581 | |
| 582 | static struct usbpd_svid_handler *find_svid_handler(struct usbpd *pd, u16 svid) |
| 583 | { |
| 584 | struct usbpd_svid_handler *handler; |
| 585 | |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 586 | mutex_lock(&pd->svid_handler_lock); |
| 587 | list_for_each_entry(handler, &pd->svid_handlers, entry) { |
| 588 | if (svid == handler->svid) { |
| 589 | mutex_unlock(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 590 | return handler; |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 593 | |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 594 | mutex_unlock(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | /* Reset protocol layer */ |
| 599 | static inline void pd_reset_protocol(struct usbpd *pd) |
| 600 | { |
| 601 | /* |
| 602 | * first Rx ID should be 0; set this to a sentinel of -1 so that in |
| 603 | * phy_msg_received() we can check if we had seen it before. |
| 604 | */ |
| 605 | pd->rx_msgid = -1; |
| 606 | pd->tx_msgid = 0; |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 607 | pd->send_request = false; |
| 608 | pd->send_pr_swap = false; |
| 609 | pd->send_dr_swap = false; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Jack Pham | e95cfc7 | 2017-08-01 17:36:50 -0700 | [diff] [blame] | 612 | static int pd_send_msg(struct usbpd *pd, u8 msg_type, const u32 *data, |
| 613 | size_t num_data, enum pd_sop_type sop) |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 614 | { |
| 615 | int ret; |
| 616 | u16 hdr; |
| 617 | |
Jack Pham | 4b32328 | 2017-11-03 12:24:59 -0700 | [diff] [blame] | 618 | if (pd->hard_reset_recvd) |
| 619 | return -EBUSY; |
| 620 | |
Jack Pham | e95cfc7 | 2017-08-01 17:36:50 -0700 | [diff] [blame] | 621 | hdr = PD_MSG_HDR(msg_type, pd->current_dr, pd->current_pr, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 622 | pd->tx_msgid, num_data, pd->spec_rev); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 623 | |
Jack Pham | 7dfd761 | 2017-08-01 18:04:13 -0700 | [diff] [blame] | 624 | ret = pd_phy_write(hdr, (u8 *)data, num_data * sizeof(u32), sop); |
| 625 | if (ret) |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 626 | return ret; |
Jack Pham | cc11975 | 2017-06-07 15:35:57 -0700 | [diff] [blame] | 627 | |
| 628 | pd->tx_msgid = (pd->tx_msgid + 1) & PD_MAX_MSG_ID; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
Hemant Kumar | 51ded97 | 2017-08-09 17:57:24 -0700 | [diff] [blame] | 632 | static int pd_send_ext_msg(struct usbpd *pd, u8 msg_type, |
| 633 | const u8 *data, size_t data_len, enum pd_sop_type sop) |
| 634 | { |
| 635 | int ret; |
| 636 | size_t len_remain, chunk_len; |
| 637 | u8 chunked_payload[PD_MAX_DATA_OBJ * sizeof(u32)] = {0}; |
| 638 | u16 hdr; |
| 639 | u16 ext_hdr; |
| 640 | u8 num_objs; |
| 641 | |
| 642 | if (data_len > PD_MAX_EXT_MSG_LEN) { |
| 643 | usbpd_warn(&pd->dev, "Extended message length exceeds max, truncating...\n"); |
| 644 | data_len = PD_MAX_EXT_MSG_LEN; |
| 645 | } |
| 646 | |
| 647 | pd->next_tx_chunk = 0; |
| 648 | len_remain = data_len; |
| 649 | do { |
| 650 | ext_hdr = PD_MSG_EXT_HDR(1, pd->next_tx_chunk++, 0, data_len); |
| 651 | memcpy(chunked_payload, &ext_hdr, sizeof(ext_hdr)); |
| 652 | |
| 653 | chunk_len = min_t(size_t, len_remain, |
| 654 | PD_MAX_EXT_MSG_LEGACY_LEN); |
| 655 | memcpy(chunked_payload + sizeof(ext_hdr), data, chunk_len); |
| 656 | |
| 657 | num_objs = DIV_ROUND_UP(chunk_len + sizeof(u16), sizeof(u32)); |
| 658 | len_remain -= chunk_len; |
| 659 | |
| 660 | reinit_completion(&pd->tx_chunk_request); |
| 661 | hdr = PD_MSG_HDR(msg_type, pd->current_dr, pd->current_pr, |
| 662 | pd->tx_msgid, num_objs, pd->spec_rev) | |
| 663 | PD_MSG_HDR_EXTENDED; |
| 664 | ret = pd_phy_write(hdr, chunked_payload, |
| 665 | num_objs * sizeof(u32), sop); |
| 666 | if (ret) |
| 667 | return ret; |
| 668 | |
| 669 | pd->tx_msgid = (pd->tx_msgid + 1) & PD_MAX_MSG_ID; |
| 670 | |
| 671 | /* Wait for request chunk */ |
| 672 | if (len_remain && |
| 673 | !wait_for_completion_timeout(&pd->tx_chunk_request, |
| 674 | msecs_to_jiffies(SENDER_RESPONSE_TIME))) { |
| 675 | usbpd_err(&pd->dev, "Timed out waiting for chunk request\n"); |
| 676 | return -EPROTO; |
| 677 | } |
| 678 | } while (len_remain); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 683 | static int pd_select_pdo(struct usbpd *pd, int pdo_pos, int uv, int ua) |
| 684 | { |
| 685 | int curr; |
| 686 | int max_current; |
| 687 | bool mismatch = false; |
| 688 | u8 type; |
| 689 | u32 pdo = pd->received_pdos[pdo_pos - 1]; |
| 690 | |
| 691 | type = PD_SRC_PDO_TYPE(pdo); |
| 692 | if (type == PD_SRC_PDO_TYPE_FIXED) { |
| 693 | curr = max_current = PD_SRC_PDO_FIXED_MAX_CURR(pdo) * 10; |
| 694 | |
| 695 | /* |
| 696 | * Check if the PDO has enough current, otherwise set the |
| 697 | * Capability Mismatch flag |
| 698 | */ |
| 699 | if (curr < min_sink_current) { |
| 700 | mismatch = true; |
| 701 | max_current = min_sink_current; |
| 702 | } |
| 703 | |
| 704 | pd->requested_voltage = |
| 705 | PD_SRC_PDO_FIXED_VOLTAGE(pdo) * 50 * 1000; |
| 706 | pd->rdo = PD_RDO_FIXED(pdo_pos, 0, mismatch, 1, 1, curr / 10, |
| 707 | max_current / 10); |
| 708 | } else if (type == PD_SRC_PDO_TYPE_AUGMENTED) { |
| 709 | if ((uv / 100000) > PD_APDO_MAX_VOLT(pdo) || |
| 710 | (uv / 100000) < PD_APDO_MIN_VOLT(pdo) || |
| 711 | (ua / 50000) > PD_APDO_MAX_CURR(pdo) || (ua < 0)) { |
| 712 | usbpd_err(&pd->dev, "uv (%d) and ua (%d) out of range of APDO\n", |
| 713 | uv, ua); |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | |
| 717 | curr = ua / 1000; |
| 718 | pd->requested_voltage = uv; |
| 719 | pd->rdo = PD_RDO_AUGMENTED(pdo_pos, mismatch, 1, 1, |
| 720 | uv / 20000, ua / 50000); |
| 721 | } else { |
| 722 | usbpd_err(&pd->dev, "Only Fixed or Programmable PDOs supported\n"); |
| 723 | return -ENOTSUPP; |
| 724 | } |
| 725 | |
| 726 | /* Can't sink more than 5V if VCONN is sourced from the VBUS input */ |
| 727 | if (pd->vconn_enabled && !pd->vconn_is_external && |
| 728 | pd->requested_voltage > 5000000) |
| 729 | return -ENOTSUPP; |
| 730 | |
| 731 | pd->requested_current = curr; |
| 732 | pd->requested_pdo = pdo_pos; |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static int pd_eval_src_caps(struct usbpd *pd) |
| 738 | { |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 739 | int i; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 740 | union power_supply_propval val; |
| 741 | u32 first_pdo = pd->received_pdos[0]; |
| 742 | |
| 743 | if (PD_SRC_PDO_TYPE(first_pdo) != PD_SRC_PDO_TYPE_FIXED) { |
| 744 | usbpd_err(&pd->dev, "First src_cap invalid! %08x\n", first_pdo); |
| 745 | return -EINVAL; |
| 746 | } |
| 747 | |
| 748 | pd->peer_usb_comm = PD_SRC_PDO_FIXED_USB_COMM(first_pdo); |
| 749 | pd->peer_pr_swap = PD_SRC_PDO_FIXED_PR_SWAP(first_pdo); |
| 750 | pd->peer_dr_swap = PD_SRC_PDO_FIXED_DR_SWAP(first_pdo); |
| 751 | |
| 752 | val.intval = PD_SRC_PDO_FIXED_USB_SUSP(first_pdo); |
| 753 | power_supply_set_property(pd->usb_psy, |
| 754 | POWER_SUPPLY_PROP_PD_USB_SUSPEND_SUPPORTED, &val); |
| 755 | |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 756 | if (pd->spec_rev == USBPD_REV_30 && !rev3_sink_only) { |
| 757 | bool pps_found = false; |
| 758 | |
| 759 | /* downgrade to 2.0 if no PPS */ |
| 760 | for (i = 1; i < PD_MAX_DATA_OBJ; i++) { |
| 761 | if ((PD_SRC_PDO_TYPE(pd->received_pdos[i]) == |
Hemant Kumar | 796534e | 2017-05-30 15:54:55 -0700 | [diff] [blame] | 762 | PD_SRC_PDO_TYPE_AUGMENTED) && |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 763 | !PD_APDO_PPS(pd->received_pdos[i])) { |
| 764 | pps_found = true; |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | if (!pps_found) |
| 769 | pd->spec_rev = USBPD_REV_20; |
Hemant Kumar | 796534e | 2017-05-30 15:54:55 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 772 | /* Select the first PDO (vSafe5V) immediately. */ |
| 773 | pd_select_pdo(pd, 1, 0, 0); |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static void pd_send_hard_reset(struct usbpd *pd) |
| 779 | { |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 780 | union power_supply_propval val = {0}; |
| 781 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 782 | usbpd_dbg(&pd->dev, "send hard reset"); |
| 783 | |
| 784 | /* Force CC logic to source/sink to keep Rp/Rd unchanged */ |
| 785 | set_power_role(pd, pd->current_pr); |
| 786 | pd->hard_reset_count++; |
Jack Pham | 7dfd761 | 2017-08-01 18:04:13 -0700 | [diff] [blame] | 787 | pd_phy_signal(HARD_RESET_SIG); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 788 | pd->in_pr_swap = false; |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 789 | power_supply_set_property(pd->usb_psy, POWER_SUPPLY_PROP_PR_SWAP, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static void kick_sm(struct usbpd *pd, int ms) |
| 793 | { |
| 794 | pm_stay_awake(&pd->dev); |
| 795 | pd->sm_queued = true; |
| 796 | |
| 797 | if (ms) |
| 798 | hrtimer_start(&pd->timer, ms_to_ktime(ms), HRTIMER_MODE_REL); |
| 799 | else |
| 800 | queue_work(pd->wq, &pd->sm_work); |
| 801 | } |
| 802 | |
Jack Pham | e95cfc7 | 2017-08-01 17:36:50 -0700 | [diff] [blame] | 803 | static void phy_sig_received(struct usbpd *pd, enum pd_sig_type sig) |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 804 | { |
Jack Pham | 9411cbb | 2017-06-06 11:10:03 -0700 | [diff] [blame] | 805 | union power_supply_propval val = {1}; |
| 806 | |
Jack Pham | e95cfc7 | 2017-08-01 17:36:50 -0700 | [diff] [blame] | 807 | if (sig != HARD_RESET_SIG) { |
| 808 | usbpd_err(&pd->dev, "invalid signal (%d) received\n", sig); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 809 | return; |
| 810 | } |
| 811 | |
Jack Pham | 4b32328 | 2017-11-03 12:24:59 -0700 | [diff] [blame] | 812 | pd->hard_reset_recvd = true; |
| 813 | pd->hard_reset_recvd_time = ktime_get(); |
| 814 | |
| 815 | usbpd_err(&pd->dev, "hard reset received\n"); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 816 | |
| 817 | /* Force CC logic to source/sink to keep Rp/Rd unchanged */ |
| 818 | set_power_role(pd, pd->current_pr); |
Jack Pham | 9411cbb | 2017-06-06 11:10:03 -0700 | [diff] [blame] | 819 | power_supply_set_property(pd->usb_psy, |
| 820 | POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val); |
| 821 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 822 | kick_sm(pd, 0); |
| 823 | } |
| 824 | |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 825 | struct pd_request_chunk { |
| 826 | struct work_struct w; |
| 827 | struct usbpd *pd; |
| 828 | u8 msg_type; |
| 829 | u8 chunk_num; |
| 830 | enum pd_sop_type sop; |
| 831 | }; |
| 832 | |
| 833 | static void pd_request_chunk_work(struct work_struct *w) |
| 834 | { |
| 835 | struct pd_request_chunk *req = |
| 836 | container_of(w, struct pd_request_chunk, w); |
| 837 | struct usbpd *pd = req->pd; |
| 838 | unsigned long flags; |
| 839 | int ret; |
| 840 | u8 payload[4] = {0}; /* ext_hdr + padding */ |
| 841 | u16 hdr = PD_MSG_HDR(req->msg_type, pd->current_dr, pd->current_pr, |
| 842 | pd->tx_msgid, 1, pd->spec_rev) | PD_MSG_HDR_EXTENDED; |
| 843 | |
| 844 | *(u16 *)payload = PD_MSG_EXT_HDR(1, req->chunk_num, 1, 0); |
| 845 | |
| 846 | ret = pd_phy_write(hdr, payload, sizeof(payload), req->sop); |
| 847 | if (!ret) { |
| 848 | pd->tx_msgid = (pd->tx_msgid + 1) & PD_MAX_MSG_ID; |
| 849 | } else { |
| 850 | usbpd_err(&pd->dev, "could not send chunk request\n"); |
| 851 | |
| 852 | /* queue what we have anyway */ |
| 853 | spin_lock_irqsave(&pd->rx_lock, flags); |
| 854 | list_add_tail(&pd->rx_ext_msg->entry, &pd->rx_q); |
| 855 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 856 | |
| 857 | pd->rx_ext_msg = NULL; |
| 858 | } |
| 859 | |
| 860 | kfree(req); |
| 861 | } |
| 862 | |
| 863 | static struct rx_msg *pd_ext_msg_received(struct usbpd *pd, u16 header, u8 *buf, |
| 864 | size_t len, enum pd_sop_type sop) |
| 865 | { |
| 866 | struct rx_msg *rx_msg; |
| 867 | u16 bytes_to_copy; |
| 868 | u16 ext_hdr = *(u16 *)buf; |
| 869 | u8 chunk_num; |
| 870 | |
| 871 | if (!PD_MSG_EXT_HDR_IS_CHUNKED(ext_hdr)) { |
| 872 | usbpd_err(&pd->dev, "unchunked extended messages unsupported\n"); |
| 873 | return NULL; |
| 874 | } |
| 875 | |
| 876 | /* request for next Tx chunk */ |
| 877 | if (PD_MSG_EXT_HDR_REQ_CHUNK(ext_hdr)) { |
| 878 | if (PD_MSG_EXT_HDR_DATA_SIZE(ext_hdr) || |
| 879 | PD_MSG_EXT_HDR_CHUNK_NUM(ext_hdr) != |
| 880 | pd->next_tx_chunk) { |
| 881 | usbpd_err(&pd->dev, "invalid request chunk ext header 0x%02x\n", |
| 882 | ext_hdr); |
| 883 | return NULL; |
| 884 | } |
| 885 | |
| 886 | if (!completion_done(&pd->tx_chunk_request)) |
| 887 | complete(&pd->tx_chunk_request); |
| 888 | |
| 889 | return NULL; |
| 890 | } |
| 891 | |
| 892 | chunk_num = PD_MSG_EXT_HDR_CHUNK_NUM(ext_hdr); |
| 893 | if (!chunk_num) { |
| 894 | /* allocate new message if first chunk */ |
| 895 | rx_msg = kzalloc(sizeof(*rx_msg) + |
| 896 | PD_MSG_EXT_HDR_DATA_SIZE(ext_hdr), |
| 897 | GFP_KERNEL); |
| 898 | if (!rx_msg) |
| 899 | return NULL; |
| 900 | |
| 901 | rx_msg->hdr = header; |
| 902 | rx_msg->data_len = PD_MSG_EXT_HDR_DATA_SIZE(ext_hdr); |
| 903 | |
| 904 | if (rx_msg->data_len > PD_MAX_EXT_MSG_LEN) { |
| 905 | usbpd_warn(&pd->dev, "Extended message length exceeds max, truncating...\n"); |
| 906 | rx_msg->data_len = PD_MAX_EXT_MSG_LEN; |
| 907 | } |
| 908 | } else { |
| 909 | if (!pd->rx_ext_msg) { |
| 910 | usbpd_err(&pd->dev, "missing first rx_ext_msg chunk\n"); |
| 911 | return NULL; |
| 912 | } |
| 913 | |
| 914 | rx_msg = pd->rx_ext_msg; |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * The amount to copy is derived as follows: |
| 919 | * |
| 920 | * - if extended data_len < 26, then copy data_len bytes |
| 921 | * - for chunks 0..N-2, copy 26 bytes |
| 922 | * - for the last chunk (N-1), copy the remainder |
| 923 | */ |
| 924 | bytes_to_copy = |
| 925 | min((rx_msg->data_len - chunk_num * PD_MAX_EXT_MSG_LEGACY_LEN), |
| 926 | PD_MAX_EXT_MSG_LEGACY_LEN); |
| 927 | |
| 928 | /* check against received length to avoid overrun */ |
| 929 | if (bytes_to_copy > len - sizeof(ext_hdr)) { |
| 930 | usbpd_warn(&pd->dev, "not enough bytes in chunk, expected:%u received:%lu\n", |
| 931 | bytes_to_copy, len - sizeof(ext_hdr)); |
| 932 | bytes_to_copy = len - sizeof(ext_hdr); |
| 933 | } |
| 934 | |
| 935 | memcpy(rx_msg->payload + chunk_num * PD_MAX_EXT_MSG_LEGACY_LEN, buf + 2, |
| 936 | bytes_to_copy); |
| 937 | |
| 938 | /* request next chunk? */ |
| 939 | if ((rx_msg->data_len - chunk_num * PD_MAX_EXT_MSG_LEGACY_LEN) > |
| 940 | PD_MAX_EXT_MSG_LEGACY_LEN) { |
| 941 | struct pd_request_chunk *req; |
| 942 | |
| 943 | if (pd->rx_ext_msg && pd->rx_ext_msg != rx_msg) { |
| 944 | usbpd_dbg(&pd->dev, "stale previous rx_ext_msg?\n"); |
| 945 | kfree(pd->rx_ext_msg); |
| 946 | } |
| 947 | |
| 948 | pd->rx_ext_msg = rx_msg; |
| 949 | |
| 950 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 951 | if (!req) |
| 952 | goto queue_rx; /* return what we have anyway */ |
| 953 | |
| 954 | INIT_WORK(&req->w, pd_request_chunk_work); |
| 955 | req->pd = pd; |
| 956 | req->msg_type = PD_MSG_HDR_TYPE(header); |
| 957 | req->chunk_num = chunk_num + 1; |
| 958 | req->sop = sop; |
| 959 | queue_work(pd->wq, &req->w); |
| 960 | |
| 961 | return NULL; |
| 962 | } |
| 963 | |
| 964 | queue_rx: |
| 965 | pd->rx_ext_msg = NULL; |
| 966 | return rx_msg; /* queue it for usbpd_sm */ |
| 967 | } |
| 968 | |
Jack Pham | e95cfc7 | 2017-08-01 17:36:50 -0700 | [diff] [blame] | 969 | static void phy_msg_received(struct usbpd *pd, enum pd_sop_type sop, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 970 | u8 *buf, size_t len) |
| 971 | { |
| 972 | struct rx_msg *rx_msg; |
| 973 | unsigned long flags; |
| 974 | u16 header; |
| 975 | |
Jack Pham | e95cfc7 | 2017-08-01 17:36:50 -0700 | [diff] [blame] | 976 | if (sop != SOP_MSG) { |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 977 | usbpd_err(&pd->dev, "invalid msg type (%d) received; only SOP supported\n", |
Jack Pham | e95cfc7 | 2017-08-01 17:36:50 -0700 | [diff] [blame] | 978 | sop); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | |
| 982 | if (len < 2) { |
| 983 | usbpd_err(&pd->dev, "invalid message received, len=%zd\n", len); |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | header = *((u16 *)buf); |
| 988 | buf += sizeof(u16); |
| 989 | len -= sizeof(u16); |
| 990 | |
| 991 | if (len % 4 != 0) { |
| 992 | usbpd_err(&pd->dev, "len=%zd not multiple of 4\n", len); |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | /* if MSGID already seen, discard */ |
| 997 | if (PD_MSG_HDR_ID(header) == pd->rx_msgid && |
| 998 | PD_MSG_HDR_TYPE(header) != MSG_SOFT_RESET) { |
| 999 | usbpd_dbg(&pd->dev, "MessageID already seen, discarding\n"); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | pd->rx_msgid = PD_MSG_HDR_ID(header); |
| 1004 | |
| 1005 | /* discard Pings */ |
| 1006 | if (PD_MSG_HDR_TYPE(header) == MSG_PING && !len) |
| 1007 | return; |
| 1008 | |
| 1009 | /* check header's count field to see if it matches len */ |
| 1010 | if (PD_MSG_HDR_COUNT(header) != (len / 4)) { |
| 1011 | usbpd_err(&pd->dev, "header count (%d) mismatch, len=%zd\n", |
| 1012 | PD_MSG_HDR_COUNT(header), len); |
| 1013 | return; |
| 1014 | } |
| 1015 | |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 1016 | /* if spec rev differs (i.e. is older), update PHY */ |
| 1017 | if (PD_MSG_HDR_REV(header) < pd->spec_rev) |
| 1018 | pd->spec_rev = PD_MSG_HDR_REV(header); |
| 1019 | |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 1020 | usbpd_dbg(&pd->dev, "received message: type(%d) num_objs(%d)\n", |
| 1021 | PD_MSG_HDR_TYPE(header), PD_MSG_HDR_COUNT(header)); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1022 | |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 1023 | if (!PD_MSG_HDR_IS_EXTENDED(header)) { |
| 1024 | rx_msg = kzalloc(sizeof(*rx_msg) + len, GFP_KERNEL); |
| 1025 | if (!rx_msg) |
| 1026 | return; |
| 1027 | |
| 1028 | rx_msg->hdr = header; |
| 1029 | rx_msg->data_len = len; |
| 1030 | memcpy(rx_msg->payload, buf, len); |
| 1031 | } else { |
| 1032 | rx_msg = pd_ext_msg_received(pd, header, buf, len, sop); |
| 1033 | if (!rx_msg) |
| 1034 | return; |
| 1035 | } |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1036 | |
| 1037 | spin_lock_irqsave(&pd->rx_lock, flags); |
| 1038 | list_add_tail(&rx_msg->entry, &pd->rx_q); |
| 1039 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 1040 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1041 | kick_sm(pd, 0); |
| 1042 | } |
| 1043 | |
| 1044 | static void phy_shutdown(struct usbpd *pd) |
| 1045 | { |
| 1046 | usbpd_dbg(&pd->dev, "shutdown"); |
Jack Pham | 0ab0296 | 2017-10-12 15:33:50 -0700 | [diff] [blame] | 1047 | |
| 1048 | if (pd->vconn_enabled) { |
| 1049 | regulator_disable(pd->vconn); |
| 1050 | pd->vconn_enabled = false; |
| 1051 | } |
| 1052 | |
| 1053 | if (pd->vbus_enabled) { |
| 1054 | regulator_disable(pd->vbus); |
| 1055 | pd->vbus_enabled = false; |
| 1056 | } |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static enum hrtimer_restart pd_timeout(struct hrtimer *timer) |
| 1060 | { |
| 1061 | struct usbpd *pd = container_of(timer, struct usbpd, timer); |
| 1062 | |
| 1063 | usbpd_dbg(&pd->dev, "timeout"); |
| 1064 | queue_work(pd->wq, &pd->sm_work); |
| 1065 | |
| 1066 | return HRTIMER_NORESTART; |
| 1067 | } |
| 1068 | |
| 1069 | /* Enters new state and executes actions on entry */ |
| 1070 | static void usbpd_set_state(struct usbpd *pd, enum usbpd_state next_state) |
| 1071 | { |
| 1072 | struct pd_phy_params phy_params = { |
| 1073 | .signal_cb = phy_sig_received, |
| 1074 | .msg_rx_cb = phy_msg_received, |
| 1075 | .shutdown_cb = phy_shutdown, |
| 1076 | .frame_filter_val = FRAME_FILTER_EN_SOP | |
| 1077 | FRAME_FILTER_EN_HARD_RESET, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1078 | }; |
| 1079 | union power_supply_propval val = {0}; |
| 1080 | unsigned long flags; |
| 1081 | int ret; |
| 1082 | |
Jack Pham | 4b32328 | 2017-11-03 12:24:59 -0700 | [diff] [blame] | 1083 | if (pd->hard_reset_recvd) /* let usbpd_sm handle it */ |
| 1084 | return; |
| 1085 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1086 | usbpd_dbg(&pd->dev, "%s -> %s\n", |
| 1087 | usbpd_state_strings[pd->current_state], |
| 1088 | usbpd_state_strings[next_state]); |
| 1089 | |
| 1090 | pd->current_state = next_state; |
| 1091 | |
| 1092 | switch (next_state) { |
| 1093 | case PE_ERROR_RECOVERY: /* perform hard disconnect/reconnect */ |
| 1094 | pd->in_pr_swap = false; |
| 1095 | pd->current_pr = PR_NONE; |
| 1096 | set_power_role(pd, PR_NONE); |
| 1097 | pd->typec_mode = POWER_SUPPLY_TYPEC_NONE; |
| 1098 | kick_sm(pd, 0); |
| 1099 | break; |
| 1100 | |
| 1101 | /* Source states */ |
Jack Pham | b9200bb | 2017-04-19 00:25:32 -0700 | [diff] [blame] | 1102 | case PE_SRC_DISABLED: |
| 1103 | /* are we still connected? */ |
| 1104 | if (pd->typec_mode == POWER_SUPPLY_TYPEC_NONE) { |
| 1105 | pd->current_pr = PR_NONE; |
| 1106 | kick_sm(pd, 0); |
| 1107 | } |
| 1108 | |
| 1109 | break; |
| 1110 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1111 | case PE_SRC_STARTUP: |
| 1112 | if (pd->current_dr == DR_NONE) { |
| 1113 | pd->current_dr = DR_DFP; |
Mayank Rana | 38e9b25 | 2017-03-23 12:35:57 -0700 | [diff] [blame] | 1114 | start_usb_host(pd, true); |
Mayank Rana | 6af4342 | 2017-07-18 12:09:02 -0700 | [diff] [blame] | 1115 | pd->ss_lane_svid = 0x0; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | dual_role_instance_changed(pd->dual_role); |
| 1119 | |
| 1120 | /* Set CC back to DRP toggle for the next disconnect */ |
| 1121 | val.intval = POWER_SUPPLY_TYPEC_PR_DUAL; |
| 1122 | power_supply_set_property(pd->usb_psy, |
| 1123 | POWER_SUPPLY_PROP_TYPEC_POWER_ROLE, &val); |
| 1124 | |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 1125 | /* support only PD 2.0 as a source */ |
| 1126 | pd->spec_rev = USBPD_REV_20; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1127 | pd_reset_protocol(pd); |
| 1128 | |
| 1129 | if (!pd->in_pr_swap) { |
| 1130 | if (pd->pd_phy_opened) { |
| 1131 | pd_phy_close(); |
| 1132 | pd->pd_phy_opened = false; |
| 1133 | } |
| 1134 | |
| 1135 | phy_params.data_role = pd->current_dr; |
| 1136 | phy_params.power_role = pd->current_pr; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1137 | |
| 1138 | ret = pd_phy_open(&phy_params); |
| 1139 | if (ret) { |
| 1140 | WARN_ON_ONCE(1); |
| 1141 | usbpd_err(&pd->dev, "error opening PD PHY %d\n", |
| 1142 | ret); |
| 1143 | pd->current_state = PE_UNKNOWN; |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | pd->pd_phy_opened = true; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1150 | if (pd->in_pr_swap) { |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1151 | pd->in_pr_swap = false; |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 1152 | val.intval = 0; |
| 1153 | power_supply_set_property(pd->usb_psy, |
| 1154 | POWER_SUPPLY_PROP_PR_SWAP, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Jack Pham | 3512134 | 2017-07-06 00:12:15 -0700 | [diff] [blame] | 1157 | /* |
| 1158 | * A sink might remove its terminations (during some Type-C |
| 1159 | * compliance tests or a sink attempting to do Try.SRC) |
| 1160 | * at this point just after we enabled VBUS. Sending PD |
| 1161 | * messages now would delay detecting the detach beyond the |
| 1162 | * required timing. Instead, delay sending out the first |
| 1163 | * source capabilities to allow for the other side to |
| 1164 | * completely settle CC debounce and allow HW to detect detach |
| 1165 | * sooner in the meantime. PD spec allows up to |
| 1166 | * tFirstSourceCap (250ms). |
| 1167 | */ |
| 1168 | pd->current_state = PE_SRC_SEND_CAPABILITIES; |
| 1169 | kick_sm(pd, FIRST_SOURCE_CAP_TIME); |
| 1170 | break; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1171 | |
| 1172 | case PE_SRC_SEND_CAPABILITIES: |
| 1173 | kick_sm(pd, 0); |
| 1174 | break; |
| 1175 | |
| 1176 | case PE_SRC_NEGOTIATE_CAPABILITY: |
| 1177 | if (PD_RDO_OBJ_POS(pd->rdo) != 1 || |
| 1178 | PD_RDO_FIXED_CURR(pd->rdo) > |
| 1179 | PD_SRC_PDO_FIXED_MAX_CURR(*default_src_caps) || |
| 1180 | PD_RDO_FIXED_CURR_MINMAX(pd->rdo) > |
| 1181 | PD_SRC_PDO_FIXED_MAX_CURR(*default_src_caps)) { |
| 1182 | /* send Reject */ |
| 1183 | ret = pd_send_msg(pd, MSG_REJECT, NULL, 0, SOP_MSG); |
| 1184 | if (ret) { |
| 1185 | usbpd_err(&pd->dev, "Error sending Reject\n"); |
| 1186 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 1187 | break; |
| 1188 | } |
| 1189 | |
| 1190 | usbpd_err(&pd->dev, "Invalid request: %08x\n", pd->rdo); |
| 1191 | |
| 1192 | if (pd->in_explicit_contract) |
| 1193 | usbpd_set_state(pd, PE_SRC_READY); |
| 1194 | else |
| 1195 | /* |
| 1196 | * bypass PE_SRC_Capability_Response and |
| 1197 | * PE_SRC_Wait_New_Capabilities in this |
| 1198 | * implementation for simplicity. |
| 1199 | */ |
| 1200 | usbpd_set_state(pd, PE_SRC_SEND_CAPABILITIES); |
| 1201 | break; |
| 1202 | } |
| 1203 | |
| 1204 | /* PE_SRC_TRANSITION_SUPPLY pseudo-state */ |
| 1205 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 1206 | if (ret) { |
| 1207 | usbpd_err(&pd->dev, "Error sending Accept\n"); |
| 1208 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 1209 | break; |
| 1210 | } |
| 1211 | |
| 1212 | /* tSrcTransition required after ACCEPT */ |
| 1213 | usleep_range(SRC_TRANSITION_TIME * USEC_PER_MSEC, |
| 1214 | (SRC_TRANSITION_TIME + 5) * USEC_PER_MSEC); |
| 1215 | |
| 1216 | /* |
| 1217 | * Normally a voltage change should occur within tSrcReady |
| 1218 | * but since we only support VSafe5V there is nothing more to |
| 1219 | * prepare from the power supply so send PS_RDY right away. |
| 1220 | */ |
| 1221 | ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG); |
| 1222 | if (ret) { |
| 1223 | usbpd_err(&pd->dev, "Error sending PS_RDY\n"); |
| 1224 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 1225 | break; |
| 1226 | } |
| 1227 | |
| 1228 | usbpd_set_state(pd, PE_SRC_READY); |
| 1229 | break; |
| 1230 | |
| 1231 | case PE_SRC_READY: |
| 1232 | pd->in_explicit_contract = true; |
Jack Pham | be0f20c | 2017-10-05 12:51:26 -0700 | [diff] [blame] | 1233 | |
| 1234 | if (pd->vdm_tx) |
| 1235 | kick_sm(pd, 0); |
| 1236 | else if (pd->current_dr == DR_DFP && pd->vdm_state == VDM_NONE) |
| 1237 | usbpd_send_svdm(pd, USBPD_SID, |
| 1238 | USBPD_SVDM_DISCOVER_IDENTITY, |
| 1239 | SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1240 | |
| 1241 | kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 1242 | complete(&pd->is_ready); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1243 | dual_role_instance_changed(pd->dual_role); |
| 1244 | break; |
| 1245 | |
| 1246 | case PE_SRC_HARD_RESET: |
| 1247 | case PE_SNK_HARD_RESET: |
Jack Pham | b9200bb | 2017-04-19 00:25:32 -0700 | [diff] [blame] | 1248 | /* are we still connected? */ |
| 1249 | if (pd->typec_mode == POWER_SUPPLY_TYPEC_NONE) |
| 1250 | pd->current_pr = PR_NONE; |
| 1251 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1252 | /* hard reset may sleep; handle it in the workqueue */ |
| 1253 | kick_sm(pd, 0); |
| 1254 | break; |
| 1255 | |
| 1256 | case PE_SRC_SEND_SOFT_RESET: |
| 1257 | case PE_SNK_SEND_SOFT_RESET: |
| 1258 | pd_reset_protocol(pd); |
| 1259 | |
| 1260 | ret = pd_send_msg(pd, MSG_SOFT_RESET, NULL, 0, SOP_MSG); |
| 1261 | if (ret) { |
| 1262 | usbpd_err(&pd->dev, "Error sending Soft Reset, do Hard Reset\n"); |
| 1263 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 1264 | PE_SRC_HARD_RESET : PE_SNK_HARD_RESET); |
| 1265 | break; |
| 1266 | } |
| 1267 | |
| 1268 | /* wait for ACCEPT */ |
| 1269 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 1270 | break; |
| 1271 | |
| 1272 | /* Sink states */ |
| 1273 | case PE_SNK_STARTUP: |
| 1274 | if (pd->current_dr == DR_NONE || pd->current_dr == DR_UFP) { |
| 1275 | pd->current_dr = DR_UFP; |
| 1276 | |
| 1277 | if (pd->psy_type == POWER_SUPPLY_TYPE_USB || |
Vijayavardhan Vennapusa | 34a1f1d | 2017-05-31 12:00:37 +0530 | [diff] [blame] | 1278 | pd->psy_type == POWER_SUPPLY_TYPE_USB_CDP || |
Hemant Kumar | bbf7d05 | 2017-07-13 12:08:49 -0700 | [diff] [blame] | 1279 | pd->psy_type == POWER_SUPPLY_TYPE_USB_FLOAT || |
Vijayavardhan Vennapusa | 34a1f1d | 2017-05-31 12:00:37 +0530 | [diff] [blame] | 1280 | usb_compliance_mode) |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1281 | start_usb_peripheral(pd); |
| 1282 | } |
| 1283 | |
| 1284 | dual_role_instance_changed(pd->dual_role); |
| 1285 | |
| 1286 | ret = power_supply_get_property(pd->usb_psy, |
| 1287 | POWER_SUPPLY_PROP_PD_ALLOWED, &val); |
| 1288 | if (ret) { |
| 1289 | usbpd_err(&pd->dev, "Unable to read USB PROP_PD_ALLOWED: %d\n", |
| 1290 | ret); |
| 1291 | break; |
| 1292 | } |
| 1293 | |
Vijayavardhan Vennapusa | 49f21ce | 2017-06-05 14:47:56 +0530 | [diff] [blame] | 1294 | if (!val.intval || disable_usb_pd) |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1295 | break; |
| 1296 | |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 1297 | /* |
| 1298 | * support up to PD 3.0 as a sink; if source is 2.0 |
| 1299 | * phy_msg_received() will handle the downgrade. |
| 1300 | */ |
| 1301 | pd->spec_rev = USBPD_REV_30; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1302 | pd_reset_protocol(pd); |
| 1303 | |
| 1304 | if (!pd->in_pr_swap) { |
| 1305 | if (pd->pd_phy_opened) { |
| 1306 | pd_phy_close(); |
| 1307 | pd->pd_phy_opened = false; |
| 1308 | } |
| 1309 | |
| 1310 | phy_params.data_role = pd->current_dr; |
| 1311 | phy_params.power_role = pd->current_pr; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1312 | |
| 1313 | ret = pd_phy_open(&phy_params); |
| 1314 | if (ret) { |
| 1315 | WARN_ON_ONCE(1); |
| 1316 | usbpd_err(&pd->dev, "error opening PD PHY %d\n", |
| 1317 | ret); |
| 1318 | pd->current_state = PE_UNKNOWN; |
| 1319 | return; |
| 1320 | } |
| 1321 | |
| 1322 | pd->pd_phy_opened = true; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | pd->current_voltage = pd->requested_voltage = 5000000; |
| 1326 | val.intval = pd->requested_voltage; /* set max range to 5V */ |
| 1327 | power_supply_set_property(pd->usb_psy, |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 1328 | POWER_SUPPLY_PROP_PD_VOLTAGE_MAX, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1329 | |
| 1330 | if (!pd->vbus_present) { |
| 1331 | pd->current_state = PE_SNK_DISCOVERY; |
| 1332 | /* max time for hard reset to turn vbus back on */ |
| 1333 | kick_sm(pd, SNK_HARD_RESET_VBUS_ON_TIME); |
| 1334 | break; |
| 1335 | } |
| 1336 | |
| 1337 | pd->current_state = PE_SNK_WAIT_FOR_CAPABILITIES; |
| 1338 | /* fall-through */ |
| 1339 | |
| 1340 | case PE_SNK_WAIT_FOR_CAPABILITIES: |
| 1341 | spin_lock_irqsave(&pd->rx_lock, flags); |
| 1342 | if (list_empty(&pd->rx_q)) |
| 1343 | kick_sm(pd, SINK_WAIT_CAP_TIME); |
| 1344 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 1345 | break; |
| 1346 | |
| 1347 | case PE_SNK_EVALUATE_CAPABILITY: |
| 1348 | pd->pd_connected = true; /* we know peer is PD capable */ |
| 1349 | pd->hard_reset_count = 0; |
| 1350 | |
| 1351 | /* evaluate PDOs and select one */ |
| 1352 | ret = pd_eval_src_caps(pd); |
| 1353 | if (ret < 0) { |
| 1354 | usbpd_err(&pd->dev, "Invalid src_caps received. Skipping request\n"); |
| 1355 | break; |
| 1356 | } |
| 1357 | pd->current_state = PE_SNK_SELECT_CAPABILITY; |
| 1358 | /* fall-through */ |
| 1359 | |
| 1360 | case PE_SNK_SELECT_CAPABILITY: |
| 1361 | ret = pd_send_msg(pd, MSG_REQUEST, &pd->rdo, 1, SOP_MSG); |
| 1362 | if (ret) { |
| 1363 | usbpd_err(&pd->dev, "Error sending Request\n"); |
| 1364 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 1365 | break; |
| 1366 | } |
| 1367 | |
| 1368 | /* wait for ACCEPT */ |
| 1369 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 1370 | break; |
| 1371 | |
| 1372 | case PE_SNK_TRANSITION_SINK: |
| 1373 | /* wait for PS_RDY */ |
| 1374 | kick_sm(pd, PS_TRANSITION_TIME); |
| 1375 | break; |
| 1376 | |
| 1377 | case PE_SNK_READY: |
| 1378 | pd->in_explicit_contract = true; |
Jack Pham | be0f20c | 2017-10-05 12:51:26 -0700 | [diff] [blame] | 1379 | |
| 1380 | if (pd->vdm_tx) |
| 1381 | kick_sm(pd, 0); |
| 1382 | else if (pd->current_dr == DR_DFP && pd->vdm_state == VDM_NONE) |
| 1383 | usbpd_send_svdm(pd, USBPD_SID, |
| 1384 | USBPD_SVDM_DISCOVER_IDENTITY, |
| 1385 | SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0); |
| 1386 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1387 | kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 1388 | complete(&pd->is_ready); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1389 | dual_role_instance_changed(pd->dual_role); |
| 1390 | break; |
| 1391 | |
| 1392 | case PE_SNK_TRANSITION_TO_DEFAULT: |
| 1393 | if (pd->current_dr != DR_UFP) { |
| 1394 | stop_usb_host(pd); |
| 1395 | start_usb_peripheral(pd); |
| 1396 | pd->current_dr = DR_UFP; |
| 1397 | pd_phy_update_roles(pd->current_dr, pd->current_pr); |
| 1398 | } |
| 1399 | if (pd->vconn_enabled) { |
| 1400 | regulator_disable(pd->vconn); |
| 1401 | pd->vconn_enabled = false; |
| 1402 | } |
| 1403 | |
| 1404 | /* max time for hard reset to turn vbus off */ |
| 1405 | kick_sm(pd, SNK_HARD_RESET_VBUS_OFF_TIME); |
| 1406 | break; |
| 1407 | |
| 1408 | case PE_PRS_SNK_SRC_TRANSITION_TO_OFF: |
| 1409 | val.intval = pd->requested_current = 0; /* suspend charging */ |
| 1410 | power_supply_set_property(pd->usb_psy, |
| 1411 | POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val); |
| 1412 | |
| 1413 | pd->in_explicit_contract = false; |
| 1414 | |
| 1415 | /* |
| 1416 | * need to update PR bit in message header so that |
| 1417 | * proper GoodCRC is sent when receiving next PS_RDY |
| 1418 | */ |
| 1419 | pd_phy_update_roles(pd->current_dr, PR_SRC); |
| 1420 | |
| 1421 | /* wait for PS_RDY */ |
| 1422 | kick_sm(pd, PS_SOURCE_OFF); |
| 1423 | break; |
| 1424 | |
| 1425 | default: |
| 1426 | usbpd_dbg(&pd->dev, "No action for state %s\n", |
| 1427 | usbpd_state_strings[pd->current_state]); |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | int usbpd_register_svid(struct usbpd *pd, struct usbpd_svid_handler *hdlr) |
| 1433 | { |
| 1434 | if (find_svid_handler(pd, hdlr->svid)) { |
| 1435 | usbpd_err(&pd->dev, "SVID 0x%04x already registered\n", |
| 1436 | hdlr->svid); |
| 1437 | return -EINVAL; |
| 1438 | } |
| 1439 | |
| 1440 | /* require connect/disconnect callbacks be implemented */ |
| 1441 | if (!hdlr->connect || !hdlr->disconnect) { |
| 1442 | usbpd_err(&pd->dev, "SVID 0x%04x connect/disconnect must be non-NULL\n", |
| 1443 | hdlr->svid); |
| 1444 | return -EINVAL; |
| 1445 | } |
| 1446 | |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 1447 | usbpd_dbg(&pd->dev, "registered handler(%pK) for SVID 0x%04x\n", |
| 1448 | hdlr, hdlr->svid); |
| 1449 | mutex_lock(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1450 | list_add_tail(&hdlr->entry, &pd->svid_handlers); |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 1451 | mutex_unlock(&pd->svid_handler_lock); |
Mayank Rana | 6af4342 | 2017-07-18 12:09:02 -0700 | [diff] [blame] | 1452 | hdlr->request_usb_ss_lane = usbpd_release_ss_lane; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1453 | |
| 1454 | /* already connected with this SVID discovered? */ |
| 1455 | if (pd->vdm_state >= DISCOVERED_SVIDS) { |
| 1456 | int i; |
| 1457 | |
| 1458 | for (i = 0; i < pd->num_svids; i++) { |
| 1459 | if (pd->discovered_svids[i] == hdlr->svid) { |
| 1460 | hdlr->connect(hdlr); |
| 1461 | hdlr->discovered = true; |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | return 0; |
| 1468 | } |
| 1469 | EXPORT_SYMBOL(usbpd_register_svid); |
| 1470 | |
| 1471 | void usbpd_unregister_svid(struct usbpd *pd, struct usbpd_svid_handler *hdlr) |
| 1472 | { |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 1473 | |
| 1474 | usbpd_dbg(&pd->dev, "unregistered handler(%pK) for SVID 0x%04x\n", |
| 1475 | hdlr, hdlr->svid); |
| 1476 | mutex_lock(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1477 | list_del_init(&hdlr->entry); |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 1478 | mutex_unlock(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1479 | } |
| 1480 | EXPORT_SYMBOL(usbpd_unregister_svid); |
| 1481 | |
| 1482 | int usbpd_send_vdm(struct usbpd *pd, u32 vdm_hdr, const u32 *vdos, int num_vdos) |
| 1483 | { |
| 1484 | struct vdm_tx *vdm_tx; |
| 1485 | |
| 1486 | if (!pd->in_explicit_contract || pd->vdm_tx) |
| 1487 | return -EBUSY; |
| 1488 | |
| 1489 | vdm_tx = kzalloc(sizeof(*vdm_tx), GFP_KERNEL); |
| 1490 | if (!vdm_tx) |
| 1491 | return -ENOMEM; |
| 1492 | |
| 1493 | vdm_tx->data[0] = vdm_hdr; |
| 1494 | if (vdos && num_vdos) |
| 1495 | memcpy(&vdm_tx->data[1], vdos, num_vdos * sizeof(u32)); |
| 1496 | vdm_tx->size = num_vdos + 1; /* include the header */ |
| 1497 | |
| 1498 | /* VDM will get sent in PE_SRC/SNK_READY state handling */ |
| 1499 | pd->vdm_tx = vdm_tx; |
| 1500 | |
| 1501 | /* slight delay before queuing to prioritize handling of incoming VDM */ |
| 1502 | kick_sm(pd, 2); |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | EXPORT_SYMBOL(usbpd_send_vdm); |
| 1507 | |
| 1508 | int usbpd_send_svdm(struct usbpd *pd, u16 svid, u8 cmd, |
| 1509 | enum usbpd_svdm_cmd_type cmd_type, int obj_pos, |
| 1510 | const u32 *vdos, int num_vdos) |
| 1511 | { |
| 1512 | u32 svdm_hdr = SVDM_HDR(svid, 0, obj_pos, cmd_type, cmd); |
| 1513 | |
| 1514 | usbpd_dbg(&pd->dev, "VDM tx: svid:%x cmd:%x cmd_type:%x svdm_hdr:%x\n", |
| 1515 | svid, cmd, cmd_type, svdm_hdr); |
| 1516 | |
| 1517 | return usbpd_send_vdm(pd, svdm_hdr, vdos, num_vdos); |
| 1518 | } |
| 1519 | EXPORT_SYMBOL(usbpd_send_svdm); |
| 1520 | |
| 1521 | static void handle_vdm_rx(struct usbpd *pd, struct rx_msg *rx_msg) |
| 1522 | { |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 1523 | u32 vdm_hdr = |
| 1524 | rx_msg->data_len >= sizeof(u32) ? ((u32 *)rx_msg->payload)[0] : 0; |
| 1525 | |
| 1526 | u32 *vdos = (u32 *)&rx_msg->payload[sizeof(u32)]; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1527 | u16 svid = VDM_HDR_SVID(vdm_hdr); |
| 1528 | u16 *psvid; |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 1529 | u8 i, num_vdos = PD_MSG_HDR_COUNT(rx_msg->hdr) - 1; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1530 | u8 cmd = SVDM_HDR_CMD(vdm_hdr); |
| 1531 | u8 cmd_type = SVDM_HDR_CMD_TYPE(vdm_hdr); |
| 1532 | bool has_dp = false; |
| 1533 | struct usbpd_svid_handler *handler; |
| 1534 | |
| 1535 | usbpd_dbg(&pd->dev, "VDM rx: svid:%x cmd:%x cmd_type:%x vdm_hdr:%x\n", |
| 1536 | svid, cmd, cmd_type, vdm_hdr); |
| 1537 | |
| 1538 | /* if it's a supported SVID, pass the message to the handler */ |
| 1539 | handler = find_svid_handler(pd, svid); |
| 1540 | |
| 1541 | /* Unstructured VDM */ |
| 1542 | if (!VDM_IS_SVDM(vdm_hdr)) { |
| 1543 | if (handler && handler->vdm_received) |
| 1544 | handler->vdm_received(handler, vdm_hdr, vdos, num_vdos); |
| 1545 | return; |
| 1546 | } |
| 1547 | |
| 1548 | /* if this interrupts a previous exchange, abort queued response */ |
| 1549 | if (cmd_type == SVDM_CMD_TYPE_INITIATOR && pd->vdm_tx) { |
| 1550 | usbpd_dbg(&pd->dev, "Discarding previously queued SVDM tx (SVID:0x%04x)\n", |
| 1551 | VDM_HDR_SVID(pd->vdm_tx->data[0])); |
| 1552 | |
| 1553 | kfree(pd->vdm_tx); |
| 1554 | pd->vdm_tx = NULL; |
| 1555 | } |
| 1556 | |
| 1557 | if (handler && handler->svdm_received) { |
| 1558 | handler->svdm_received(handler, cmd, cmd_type, vdos, num_vdos); |
| 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | /* Standard Discovery or unhandled messages go here */ |
| 1563 | switch (cmd_type) { |
| 1564 | case SVDM_CMD_TYPE_INITIATOR: |
| 1565 | if (svid == USBPD_SID && cmd == USBPD_SVDM_DISCOVER_IDENTITY) { |
| 1566 | u32 tx_vdos[3] = { |
| 1567 | ID_HDR_USB_HOST | ID_HDR_USB_DEVICE | |
| 1568 | ID_HDR_PRODUCT_PER_MASK | ID_HDR_VID, |
| 1569 | 0x0, /* TBD: Cert Stat VDO */ |
| 1570 | (PROD_VDO_PID << 16), |
| 1571 | /* TBD: Get these from gadget */ |
| 1572 | }; |
| 1573 | |
| 1574 | usbpd_send_svdm(pd, USBPD_SID, cmd, |
| 1575 | SVDM_CMD_TYPE_RESP_ACK, 0, tx_vdos, 3); |
| 1576 | } else if (cmd != USBPD_SVDM_ATTENTION) { |
| 1577 | usbpd_send_svdm(pd, svid, cmd, SVDM_CMD_TYPE_RESP_NAK, |
| 1578 | SVDM_HDR_OBJ_POS(vdm_hdr), NULL, 0); |
| 1579 | } |
| 1580 | break; |
| 1581 | |
| 1582 | case SVDM_CMD_TYPE_RESP_ACK: |
| 1583 | if (svid != USBPD_SID) { |
| 1584 | usbpd_err(&pd->dev, "unhandled ACK for SVID:0x%x\n", |
| 1585 | svid); |
| 1586 | break; |
| 1587 | } |
| 1588 | |
| 1589 | switch (cmd) { |
| 1590 | case USBPD_SVDM_DISCOVER_IDENTITY: |
| 1591 | kfree(pd->vdm_tx_retry); |
| 1592 | pd->vdm_tx_retry = NULL; |
| 1593 | |
| 1594 | pd->vdm_state = DISCOVERED_ID; |
| 1595 | usbpd_send_svdm(pd, USBPD_SID, |
| 1596 | USBPD_SVDM_DISCOVER_SVIDS, |
| 1597 | SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0); |
| 1598 | break; |
| 1599 | |
| 1600 | case USBPD_SVDM_DISCOVER_SVIDS: |
| 1601 | pd->vdm_state = DISCOVERED_SVIDS; |
| 1602 | |
| 1603 | kfree(pd->vdm_tx_retry); |
| 1604 | pd->vdm_tx_retry = NULL; |
| 1605 | |
| 1606 | if (!pd->discovered_svids) { |
| 1607 | pd->num_svids = 2 * num_vdos; |
| 1608 | pd->discovered_svids = kcalloc(pd->num_svids, |
| 1609 | sizeof(u16), |
| 1610 | GFP_KERNEL); |
| 1611 | if (!pd->discovered_svids) { |
| 1612 | usbpd_err(&pd->dev, "unable to allocate SVIDs\n"); |
| 1613 | break; |
| 1614 | } |
| 1615 | |
| 1616 | psvid = pd->discovered_svids; |
| 1617 | } else { /* handle > 12 SVIDs */ |
| 1618 | void *ptr; |
| 1619 | size_t oldsize = pd->num_svids * sizeof(u16); |
| 1620 | size_t newsize = oldsize + |
| 1621 | (2 * num_vdos * sizeof(u16)); |
| 1622 | |
| 1623 | ptr = krealloc(pd->discovered_svids, newsize, |
| 1624 | GFP_KERNEL); |
| 1625 | if (!ptr) { |
| 1626 | usbpd_err(&pd->dev, "unable to realloc SVIDs\n"); |
| 1627 | break; |
| 1628 | } |
| 1629 | |
| 1630 | pd->discovered_svids = ptr; |
| 1631 | psvid = pd->discovered_svids + pd->num_svids; |
| 1632 | memset(psvid, 0, (2 * num_vdos)); |
| 1633 | pd->num_svids += 2 * num_vdos; |
| 1634 | } |
| 1635 | |
| 1636 | /* convert 32-bit VDOs to list of 16-bit SVIDs */ |
| 1637 | for (i = 0; i < num_vdos * 2; i++) { |
| 1638 | /* |
| 1639 | * Within each 32-bit VDO, |
| 1640 | * SVID[i]: upper 16-bits |
| 1641 | * SVID[i+1]: lower 16-bits |
| 1642 | * where i is even. |
| 1643 | */ |
| 1644 | if (!(i & 1)) |
| 1645 | svid = vdos[i >> 1] >> 16; |
| 1646 | else |
| 1647 | svid = vdos[i >> 1] & 0xFFFF; |
| 1648 | |
| 1649 | /* |
| 1650 | * There are some devices that incorrectly |
| 1651 | * swap the order of SVIDs within a VDO. So in |
| 1652 | * case of an odd-number of SVIDs it could end |
| 1653 | * up with SVID[i] as 0 while SVID[i+1] is |
| 1654 | * non-zero. Just skip over the zero ones. |
| 1655 | */ |
| 1656 | if (svid) { |
| 1657 | usbpd_dbg(&pd->dev, "Discovered SVID: 0x%04x\n", |
| 1658 | svid); |
| 1659 | *psvid++ = svid; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | /* if more than 12 SVIDs, resend the request */ |
| 1664 | if (num_vdos == 6 && vdos[5] != 0) { |
| 1665 | usbpd_send_svdm(pd, USBPD_SID, |
| 1666 | USBPD_SVDM_DISCOVER_SVIDS, |
| 1667 | SVDM_CMD_TYPE_INITIATOR, 0, |
| 1668 | NULL, 0); |
| 1669 | break; |
| 1670 | } |
| 1671 | |
| 1672 | /* now that all SVIDs are discovered, notify handlers */ |
| 1673 | for (i = 0; i < pd->num_svids; i++) { |
| 1674 | svid = pd->discovered_svids[i]; |
| 1675 | if (svid) { |
| 1676 | handler = find_svid_handler(pd, svid); |
| 1677 | if (handler) { |
| 1678 | handler->connect(handler); |
| 1679 | handler->discovered = true; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | if (svid == 0xFF01) |
| 1684 | has_dp = true; |
| 1685 | } |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1686 | break; |
| 1687 | |
| 1688 | default: |
| 1689 | usbpd_dbg(&pd->dev, "unhandled ACK for command:0x%x\n", |
| 1690 | cmd); |
| 1691 | break; |
| 1692 | } |
| 1693 | break; |
| 1694 | |
| 1695 | case SVDM_CMD_TYPE_RESP_NAK: |
| 1696 | usbpd_info(&pd->dev, "VDM NAK received for SVID:0x%04x command:0x%x\n", |
| 1697 | svid, cmd); |
| 1698 | |
| 1699 | switch (cmd) { |
| 1700 | case USBPD_SVDM_DISCOVER_IDENTITY: |
| 1701 | case USBPD_SVDM_DISCOVER_SVIDS: |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1702 | break; |
| 1703 | default: |
| 1704 | break; |
| 1705 | } |
| 1706 | |
| 1707 | break; |
| 1708 | |
| 1709 | case SVDM_CMD_TYPE_RESP_BUSY: |
| 1710 | switch (cmd) { |
| 1711 | case USBPD_SVDM_DISCOVER_IDENTITY: |
| 1712 | case USBPD_SVDM_DISCOVER_SVIDS: |
| 1713 | if (!pd->vdm_tx_retry) { |
| 1714 | usbpd_err(&pd->dev, "Discover command %d VDM was unexpectedly freed\n", |
| 1715 | cmd); |
| 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | /* wait tVDMBusy, then retry */ |
| 1720 | pd->vdm_tx = pd->vdm_tx_retry; |
| 1721 | pd->vdm_tx_retry = NULL; |
| 1722 | kick_sm(pd, VDM_BUSY_TIME); |
| 1723 | break; |
| 1724 | default: |
| 1725 | break; |
| 1726 | } |
| 1727 | break; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | static void handle_vdm_tx(struct usbpd *pd) |
| 1732 | { |
| 1733 | int ret; |
| 1734 | unsigned long flags; |
| 1735 | |
| 1736 | /* only send one VDM at a time */ |
| 1737 | if (pd->vdm_tx) { |
| 1738 | u32 vdm_hdr = pd->vdm_tx->data[0]; |
| 1739 | |
| 1740 | /* bail out and try again later if a message just arrived */ |
| 1741 | spin_lock_irqsave(&pd->rx_lock, flags); |
| 1742 | if (!list_empty(&pd->rx_q)) { |
| 1743 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 1744 | return; |
| 1745 | } |
| 1746 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 1747 | |
| 1748 | ret = pd_send_msg(pd, MSG_VDM, pd->vdm_tx->data, |
| 1749 | pd->vdm_tx->size, SOP_MSG); |
| 1750 | if (ret) { |
| 1751 | usbpd_err(&pd->dev, "Error (%d) sending VDM command %d\n", |
| 1752 | ret, SVDM_HDR_CMD(pd->vdm_tx->data[0])); |
| 1753 | |
| 1754 | /* retry when hitting PE_SRC/SNK_Ready again */ |
| 1755 | if (ret != -EBUSY) |
| 1756 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 1757 | PE_SRC_SEND_SOFT_RESET : |
| 1758 | PE_SNK_SEND_SOFT_RESET); |
| 1759 | |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * special case: keep initiated Discover ID/SVIDs |
| 1765 | * around in case we need to re-try when receiving BUSY |
| 1766 | */ |
| 1767 | if (VDM_IS_SVDM(vdm_hdr) && |
| 1768 | SVDM_HDR_CMD_TYPE(vdm_hdr) == SVDM_CMD_TYPE_INITIATOR && |
| 1769 | SVDM_HDR_CMD(vdm_hdr) <= USBPD_SVDM_DISCOVER_SVIDS) { |
| 1770 | if (pd->vdm_tx_retry) { |
| 1771 | usbpd_dbg(&pd->dev, "Previous Discover VDM command %d not ACKed/NAKed\n", |
| 1772 | SVDM_HDR_CMD( |
| 1773 | pd->vdm_tx_retry->data[0])); |
| 1774 | kfree(pd->vdm_tx_retry); |
| 1775 | } |
| 1776 | pd->vdm_tx_retry = pd->vdm_tx; |
| 1777 | } else { |
| 1778 | kfree(pd->vdm_tx); |
| 1779 | } |
| 1780 | |
| 1781 | pd->vdm_tx = NULL; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | static void reset_vdm_state(struct usbpd *pd) |
| 1786 | { |
| 1787 | struct usbpd_svid_handler *handler; |
| 1788 | |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 1789 | mutex_lock(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1790 | list_for_each_entry(handler, &pd->svid_handlers, entry) { |
| 1791 | if (handler->discovered) { |
| 1792 | handler->disconnect(handler); |
| 1793 | handler->discovered = false; |
| 1794 | } |
| 1795 | } |
| 1796 | |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 1797 | mutex_unlock(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1798 | pd->vdm_state = VDM_NONE; |
| 1799 | kfree(pd->vdm_tx_retry); |
| 1800 | pd->vdm_tx_retry = NULL; |
| 1801 | kfree(pd->discovered_svids); |
| 1802 | pd->discovered_svids = NULL; |
| 1803 | pd->num_svids = 0; |
| 1804 | kfree(pd->vdm_tx); |
| 1805 | pd->vdm_tx = NULL; |
| 1806 | } |
| 1807 | |
| 1808 | static void dr_swap(struct usbpd *pd) |
| 1809 | { |
| 1810 | reset_vdm_state(pd); |
Mayank Rana | 45b0bc4 | 2017-07-25 15:38:05 -0700 | [diff] [blame] | 1811 | usbpd_dbg(&pd->dev, "dr_swap: current_dr(%d)\n", pd->current_dr); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1812 | |
| 1813 | if (pd->current_dr == DR_DFP) { |
| 1814 | stop_usb_host(pd); |
| 1815 | start_usb_peripheral(pd); |
| 1816 | pd->current_dr = DR_UFP; |
| 1817 | } else if (pd->current_dr == DR_UFP) { |
| 1818 | stop_usb_peripheral(pd); |
Mayank Rana | 45b0bc4 | 2017-07-25 15:38:05 -0700 | [diff] [blame] | 1819 | start_usb_host(pd, true); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1820 | pd->current_dr = DR_DFP; |
| 1821 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1822 | usbpd_send_svdm(pd, USBPD_SID, USBPD_SVDM_DISCOVER_IDENTITY, |
| 1823 | SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0); |
| 1824 | } |
| 1825 | |
| 1826 | pd_phy_update_roles(pd->current_dr, pd->current_pr); |
Jack Pham | e232bde | 2017-03-02 11:37:00 -0800 | [diff] [blame] | 1827 | dual_role_instance_changed(pd->dual_role); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | |
| 1831 | static void vconn_swap(struct usbpd *pd) |
| 1832 | { |
| 1833 | int ret; |
| 1834 | |
| 1835 | if (pd->vconn_enabled) { |
| 1836 | pd->current_state = PE_VCS_WAIT_FOR_VCONN; |
| 1837 | kick_sm(pd, VCONN_ON_TIME); |
| 1838 | } else { |
| 1839 | ret = regulator_enable(pd->vconn); |
| 1840 | if (ret) { |
| 1841 | usbpd_err(&pd->dev, "Unable to enable vconn\n"); |
| 1842 | return; |
| 1843 | } |
| 1844 | |
| 1845 | pd->vconn_enabled = true; |
| 1846 | |
| 1847 | /* |
| 1848 | * Small delay to ensure Vconn has ramped up. This is well |
| 1849 | * below tVCONNSourceOn (100ms) so we still send PS_RDY within |
| 1850 | * the allowed time. |
| 1851 | */ |
| 1852 | usleep_range(5000, 10000); |
| 1853 | |
| 1854 | ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG); |
| 1855 | if (ret) { |
| 1856 | usbpd_err(&pd->dev, "Error sending PS_RDY\n"); |
| 1857 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 1858 | PE_SRC_SEND_SOFT_RESET : |
| 1859 | PE_SNK_SEND_SOFT_RESET); |
| 1860 | return; |
| 1861 | } |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | static int enable_vbus(struct usbpd *pd) |
| 1866 | { |
| 1867 | union power_supply_propval val = {0}; |
| 1868 | int count = 100; |
| 1869 | int ret; |
| 1870 | |
| 1871 | if (!check_vsafe0v) |
| 1872 | goto enable_reg; |
| 1873 | |
| 1874 | /* |
| 1875 | * Check to make sure there's no lingering charge on |
| 1876 | * VBUS before enabling it as a source. If so poll here |
| 1877 | * until it goes below VSafe0V (0.8V) before proceeding. |
| 1878 | */ |
| 1879 | while (count--) { |
| 1880 | ret = power_supply_get_property(pd->usb_psy, |
| 1881 | POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); |
| 1882 | if (ret || val.intval <= 800000) |
| 1883 | break; |
| 1884 | usleep_range(20000, 30000); |
| 1885 | } |
| 1886 | |
| 1887 | if (count < 99) |
| 1888 | msleep(100); /* need to wait an additional tCCDebounce */ |
| 1889 | |
| 1890 | enable_reg: |
| 1891 | ret = regulator_enable(pd->vbus); |
| 1892 | if (ret) |
| 1893 | usbpd_err(&pd->dev, "Unable to enable vbus (%d)\n", ret); |
| 1894 | else |
| 1895 | pd->vbus_enabled = true; |
| 1896 | |
| 1897 | return ret; |
| 1898 | } |
| 1899 | |
| 1900 | static inline void rx_msg_cleanup(struct usbpd *pd) |
| 1901 | { |
| 1902 | struct rx_msg *msg, *tmp; |
| 1903 | unsigned long flags; |
| 1904 | |
| 1905 | spin_lock_irqsave(&pd->rx_lock, flags); |
| 1906 | list_for_each_entry_safe(msg, tmp, &pd->rx_q, entry) { |
| 1907 | list_del(&msg->entry); |
| 1908 | kfree(msg); |
| 1909 | } |
| 1910 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 1911 | } |
| 1912 | |
| 1913 | /* For PD 3.0, check SinkTxOk before allowing initiating AMS */ |
| 1914 | static inline bool is_sink_tx_ok(struct usbpd *pd) |
| 1915 | { |
| 1916 | if (pd->spec_rev == USBPD_REV_30) |
| 1917 | return pd->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_HIGH; |
| 1918 | |
| 1919 | return true; |
| 1920 | } |
| 1921 | |
| 1922 | /* Handles current state and determines transitions */ |
| 1923 | static void usbpd_sm(struct work_struct *w) |
| 1924 | { |
| 1925 | struct usbpd *pd = container_of(w, struct usbpd, sm_work); |
| 1926 | union power_supply_propval val = {0}; |
| 1927 | int ret; |
| 1928 | struct rx_msg *rx_msg = NULL; |
| 1929 | unsigned long flags; |
| 1930 | |
| 1931 | usbpd_dbg(&pd->dev, "handle state %s\n", |
| 1932 | usbpd_state_strings[pd->current_state]); |
| 1933 | |
| 1934 | hrtimer_cancel(&pd->timer); |
| 1935 | pd->sm_queued = false; |
| 1936 | |
| 1937 | spin_lock_irqsave(&pd->rx_lock, flags); |
| 1938 | if (!list_empty(&pd->rx_q)) { |
| 1939 | rx_msg = list_first_entry(&pd->rx_q, struct rx_msg, entry); |
| 1940 | list_del(&rx_msg->entry); |
| 1941 | } |
| 1942 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 1943 | |
| 1944 | /* Disconnect? */ |
| 1945 | if (pd->current_pr == PR_NONE) { |
| 1946 | if (pd->current_state == PE_UNKNOWN) |
| 1947 | goto sm_done; |
| 1948 | |
Hemant Kumar | 86bd10f | 2017-05-24 12:25:15 -0700 | [diff] [blame] | 1949 | if (pd->vconn_enabled) { |
| 1950 | regulator_disable(pd->vconn); |
| 1951 | pd->vconn_enabled = false; |
| 1952 | } |
| 1953 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1954 | usbpd_info(&pd->dev, "USB Type-C disconnect\n"); |
| 1955 | |
| 1956 | if (pd->pd_phy_opened) { |
| 1957 | pd_phy_close(); |
| 1958 | pd->pd_phy_opened = false; |
| 1959 | } |
| 1960 | |
| 1961 | pd->in_pr_swap = false; |
| 1962 | pd->pd_connected = false; |
| 1963 | pd->in_explicit_contract = false; |
| 1964 | pd->hard_reset_recvd = false; |
| 1965 | pd->caps_count = 0; |
| 1966 | pd->hard_reset_count = 0; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1967 | pd->requested_voltage = 0; |
| 1968 | pd->requested_current = 0; |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 1969 | pd->selected_pdo = pd->requested_pdo = 0; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1970 | memset(&pd->received_pdos, 0, sizeof(pd->received_pdos)); |
| 1971 | rx_msg_cleanup(pd); |
| 1972 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1973 | power_supply_set_property(pd->usb_psy, |
| 1974 | POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val); |
| 1975 | |
| 1976 | power_supply_set_property(pd->usb_psy, |
| 1977 | POWER_SUPPLY_PROP_PD_USB_SUSPEND_SUPPORTED, |
| 1978 | &val); |
| 1979 | |
| 1980 | power_supply_set_property(pd->usb_psy, |
| 1981 | POWER_SUPPLY_PROP_PD_ACTIVE, &val); |
| 1982 | |
| 1983 | if (pd->vbus_enabled) { |
| 1984 | regulator_disable(pd->vbus); |
| 1985 | pd->vbus_enabled = false; |
| 1986 | } |
| 1987 | |
Mayank Rana | 86cb2029 | 2017-06-01 11:36:07 -0700 | [diff] [blame] | 1988 | reset_vdm_state(pd); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1989 | if (pd->current_dr == DR_UFP) |
| 1990 | stop_usb_peripheral(pd); |
| 1991 | else if (pd->current_dr == DR_DFP) |
| 1992 | stop_usb_host(pd); |
| 1993 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1994 | pd->current_dr = DR_NONE; |
| 1995 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 1996 | if (pd->current_state == PE_ERROR_RECOVERY) |
| 1997 | /* forced disconnect, wait before resetting to DRP */ |
| 1998 | usleep_range(ERROR_RECOVERY_TIME * USEC_PER_MSEC, |
| 1999 | (ERROR_RECOVERY_TIME + 5) * USEC_PER_MSEC); |
| 2000 | |
Jack Pham | 01c5cfd | 2017-06-06 22:22:18 -0700 | [diff] [blame] | 2001 | val.intval = 0; |
| 2002 | power_supply_set_property(pd->usb_psy, |
| 2003 | POWER_SUPPLY_PROP_PR_SWAP, &val); |
| 2004 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2005 | /* set due to dual_role class "mode" change */ |
| 2006 | if (pd->forced_pr != POWER_SUPPLY_TYPEC_PR_NONE) |
| 2007 | val.intval = pd->forced_pr; |
Hemant Kumar | be74622 | 2017-07-20 13:51:49 -0700 | [diff] [blame] | 2008 | else if (rev3_sink_only) |
| 2009 | val.intval = POWER_SUPPLY_TYPEC_PR_SINK; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2010 | else |
| 2011 | /* Set CC back to DRP toggle */ |
| 2012 | val.intval = POWER_SUPPLY_TYPEC_PR_DUAL; |
| 2013 | |
| 2014 | power_supply_set_property(pd->usb_psy, |
| 2015 | POWER_SUPPLY_PROP_TYPEC_POWER_ROLE, &val); |
| 2016 | pd->forced_pr = POWER_SUPPLY_TYPEC_PR_NONE; |
| 2017 | |
| 2018 | pd->current_state = PE_UNKNOWN; |
| 2019 | |
| 2020 | kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE); |
| 2021 | dual_role_instance_changed(pd->dual_role); |
| 2022 | |
| 2023 | goto sm_done; |
| 2024 | } |
| 2025 | |
| 2026 | /* Hard reset? */ |
| 2027 | if (pd->hard_reset_recvd) { |
| 2028 | pd->hard_reset_recvd = false; |
| 2029 | |
Jack Pham | 6de2080 | 2017-05-24 13:44:56 -0700 | [diff] [blame] | 2030 | if (pd->requested_current) { |
| 2031 | val.intval = pd->requested_current = 0; |
| 2032 | power_supply_set_property(pd->usb_psy, |
| 2033 | POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val); |
| 2034 | } |
| 2035 | |
| 2036 | pd->requested_voltage = 5000000; |
| 2037 | val.intval = pd->requested_voltage; |
| 2038 | power_supply_set_property(pd->usb_psy, |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2039 | POWER_SUPPLY_PROP_PD_VOLTAGE_MIN, &val); |
Jack Pham | 6de2080 | 2017-05-24 13:44:56 -0700 | [diff] [blame] | 2040 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2041 | pd->in_pr_swap = false; |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 2042 | val.intval = 0; |
| 2043 | power_supply_set_property(pd->usb_psy, |
| 2044 | POWER_SUPPLY_PROP_PR_SWAP, &val); |
| 2045 | |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 2046 | pd->in_explicit_contract = false; |
| 2047 | pd->selected_pdo = pd->requested_pdo = 0; |
| 2048 | pd->rdo = 0; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2049 | rx_msg_cleanup(pd); |
| 2050 | reset_vdm_state(pd); |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 2051 | kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2052 | |
| 2053 | if (pd->current_pr == PR_SINK) { |
| 2054 | usbpd_set_state(pd, PE_SNK_TRANSITION_TO_DEFAULT); |
| 2055 | } else { |
Jack Pham | 4b32328 | 2017-11-03 12:24:59 -0700 | [diff] [blame] | 2056 | s64 delta = ktime_ms_delta(ktime_get(), |
| 2057 | pd->hard_reset_recvd_time); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2058 | pd->current_state = PE_SRC_TRANSITION_TO_DEFAULT; |
Jack Pham | 4b32328 | 2017-11-03 12:24:59 -0700 | [diff] [blame] | 2059 | if (delta >= PS_HARD_RESET_TIME) |
| 2060 | kick_sm(pd, 0); |
| 2061 | else |
| 2062 | kick_sm(pd, PS_HARD_RESET_TIME - (int)delta); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | goto sm_done; |
| 2066 | } |
| 2067 | |
| 2068 | /* Soft reset? */ |
| 2069 | if (IS_CTRL(rx_msg, MSG_SOFT_RESET)) { |
| 2070 | usbpd_dbg(&pd->dev, "Handle soft reset\n"); |
| 2071 | |
| 2072 | if (pd->current_pr == PR_SRC) |
| 2073 | pd->current_state = PE_SRC_SOFT_RESET; |
| 2074 | else if (pd->current_pr == PR_SINK) |
| 2075 | pd->current_state = PE_SNK_SOFT_RESET; |
| 2076 | } |
| 2077 | |
| 2078 | switch (pd->current_state) { |
| 2079 | case PE_UNKNOWN: |
Jack Pham | c4be61c | 2017-10-10 16:59:32 -0700 | [diff] [blame] | 2080 | val.intval = 0; |
| 2081 | power_supply_set_property(pd->usb_psy, |
| 2082 | POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val); |
| 2083 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2084 | if (pd->current_pr == PR_SINK) { |
| 2085 | usbpd_set_state(pd, PE_SNK_STARTUP); |
| 2086 | } else if (pd->current_pr == PR_SRC) { |
| 2087 | enable_vbus(pd); |
| 2088 | if (!pd->vconn_enabled && |
| 2089 | pd->typec_mode == |
| 2090 | POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE) { |
| 2091 | ret = regulator_enable(pd->vconn); |
| 2092 | if (ret) |
| 2093 | usbpd_err(&pd->dev, "Unable to enable vconn\n"); |
| 2094 | else |
| 2095 | pd->vconn_enabled = true; |
| 2096 | } |
| 2097 | |
| 2098 | usbpd_set_state(pd, PE_SRC_STARTUP); |
| 2099 | } |
| 2100 | break; |
| 2101 | |
| 2102 | case PE_SRC_STARTUP: |
| 2103 | usbpd_set_state(pd, PE_SRC_STARTUP); |
| 2104 | break; |
| 2105 | |
| 2106 | case PE_SRC_SEND_CAPABILITIES: |
| 2107 | ret = pd_send_msg(pd, MSG_SOURCE_CAPABILITIES, default_src_caps, |
| 2108 | ARRAY_SIZE(default_src_caps), SOP_MSG); |
| 2109 | if (ret) { |
| 2110 | pd->caps_count++; |
Mayank Rana | 38e9b25 | 2017-03-23 12:35:57 -0700 | [diff] [blame] | 2111 | if (pd->caps_count >= PD_CAPS_COUNT) { |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2112 | usbpd_dbg(&pd->dev, "Src CapsCounter exceeded, disabling PD\n"); |
| 2113 | usbpd_set_state(pd, PE_SRC_DISABLED); |
| 2114 | |
| 2115 | val.intval = 0; |
| 2116 | power_supply_set_property(pd->usb_psy, |
| 2117 | POWER_SUPPLY_PROP_PD_ACTIVE, |
| 2118 | &val); |
| 2119 | break; |
| 2120 | } |
| 2121 | |
| 2122 | kick_sm(pd, SRC_CAP_TIME); |
| 2123 | break; |
| 2124 | } |
| 2125 | |
| 2126 | /* transmit was successful if GoodCRC was received */ |
| 2127 | pd->caps_count = 0; |
| 2128 | pd->hard_reset_count = 0; |
| 2129 | pd->pd_connected = true; /* we know peer is PD capable */ |
| 2130 | |
| 2131 | /* wait for REQUEST */ |
| 2132 | pd->current_state = PE_SRC_SEND_CAPABILITIES_WAIT; |
| 2133 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2134 | |
| 2135 | val.intval = 1; |
| 2136 | power_supply_set_property(pd->usb_psy, |
| 2137 | POWER_SUPPLY_PROP_PD_ACTIVE, &val); |
| 2138 | break; |
| 2139 | |
| 2140 | case PE_SRC_SEND_CAPABILITIES_WAIT: |
| 2141 | if (IS_DATA(rx_msg, MSG_REQUEST)) { |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 2142 | pd->rdo = *(u32 *)rx_msg->payload; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2143 | usbpd_set_state(pd, PE_SRC_NEGOTIATE_CAPABILITY); |
| 2144 | } else if (rx_msg) { |
| 2145 | usbpd_err(&pd->dev, "Unexpected message received\n"); |
| 2146 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2147 | } else { |
| 2148 | usbpd_set_state(pd, PE_SRC_HARD_RESET); |
| 2149 | } |
| 2150 | break; |
| 2151 | |
| 2152 | case PE_SRC_READY: |
| 2153 | if (IS_CTRL(rx_msg, MSG_GET_SOURCE_CAP)) { |
Hemant Kumar | ee2db9b | 2017-05-30 14:52:18 -0700 | [diff] [blame] | 2154 | pd->current_state = PE_SRC_SEND_CAPABILITIES; |
| 2155 | kick_sm(pd, 0); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2156 | } else if (IS_CTRL(rx_msg, MSG_GET_SINK_CAP)) { |
| 2157 | ret = pd_send_msg(pd, MSG_SINK_CAPABILITIES, |
Jack Pham | ee1f905 | 2017-01-26 12:27:07 -0800 | [diff] [blame] | 2158 | pd->sink_caps, pd->num_sink_caps, |
| 2159 | SOP_MSG); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2160 | if (ret) { |
| 2161 | usbpd_err(&pd->dev, "Error sending Sink Caps\n"); |
| 2162 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2163 | } |
| 2164 | } else if (IS_DATA(rx_msg, MSG_REQUEST)) { |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 2165 | pd->rdo = *(u32 *)rx_msg->payload; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2166 | usbpd_set_state(pd, PE_SRC_NEGOTIATE_CAPABILITY); |
| 2167 | } else if (IS_CTRL(rx_msg, MSG_DR_SWAP)) { |
| 2168 | if (pd->vdm_state == MODE_ENTERED) { |
| 2169 | usbpd_set_state(pd, PE_SRC_HARD_RESET); |
| 2170 | break; |
| 2171 | } |
| 2172 | |
| 2173 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 2174 | if (ret) { |
| 2175 | usbpd_err(&pd->dev, "Error sending Accept\n"); |
| 2176 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2177 | break; |
| 2178 | } |
| 2179 | |
| 2180 | dr_swap(pd); |
| 2181 | } else if (IS_CTRL(rx_msg, MSG_PR_SWAP)) { |
| 2182 | /* lock in current mode */ |
| 2183 | set_power_role(pd, pd->current_pr); |
| 2184 | |
| 2185 | /* we'll happily accept Src->Sink requests anytime */ |
| 2186 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 2187 | if (ret) { |
| 2188 | usbpd_err(&pd->dev, "Error sending Accept\n"); |
| 2189 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2190 | break; |
| 2191 | } |
| 2192 | |
| 2193 | pd->current_state = PE_PRS_SRC_SNK_TRANSITION_TO_OFF; |
| 2194 | kick_sm(pd, SRC_TRANSITION_TIME); |
| 2195 | break; |
| 2196 | } else if (IS_CTRL(rx_msg, MSG_VCONN_SWAP)) { |
| 2197 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 2198 | if (ret) { |
| 2199 | usbpd_err(&pd->dev, "Error sending Accept\n"); |
| 2200 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2201 | break; |
| 2202 | } |
| 2203 | |
| 2204 | vconn_swap(pd); |
| 2205 | } else if (IS_DATA(rx_msg, MSG_VDM)) { |
| 2206 | handle_vdm_rx(pd, rx_msg); |
Jack Pham | 69a427e | 2017-08-04 12:26:51 -0700 | [diff] [blame] | 2207 | } else if (rx_msg && pd->spec_rev == USBPD_REV_30) { |
| 2208 | /* unhandled messages */ |
| 2209 | ret = pd_send_msg(pd, MSG_NOT_SUPPORTED, NULL, 0, |
| 2210 | SOP_MSG); |
| 2211 | if (ret) { |
| 2212 | usbpd_err(&pd->dev, "Error sending Not supported\n"); |
| 2213 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2214 | } |
| 2215 | break; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2216 | } else if (pd->send_pr_swap) { |
| 2217 | pd->send_pr_swap = false; |
| 2218 | ret = pd_send_msg(pd, MSG_PR_SWAP, NULL, 0, SOP_MSG); |
| 2219 | if (ret) { |
| 2220 | dev_err(&pd->dev, "Error sending PR Swap\n"); |
| 2221 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2222 | break; |
| 2223 | } |
| 2224 | |
| 2225 | pd->current_state = PE_PRS_SRC_SNK_SEND_SWAP; |
| 2226 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2227 | } else if (pd->send_dr_swap) { |
| 2228 | pd->send_dr_swap = false; |
| 2229 | ret = pd_send_msg(pd, MSG_DR_SWAP, NULL, 0, SOP_MSG); |
| 2230 | if (ret) { |
| 2231 | dev_err(&pd->dev, "Error sending DR Swap\n"); |
| 2232 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2233 | break; |
| 2234 | } |
| 2235 | |
| 2236 | pd->current_state = PE_DRS_SEND_DR_SWAP; |
| 2237 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2238 | } else { |
| 2239 | handle_vdm_tx(pd); |
| 2240 | } |
| 2241 | break; |
| 2242 | |
| 2243 | case PE_SRC_TRANSITION_TO_DEFAULT: |
| 2244 | if (pd->vconn_enabled) |
| 2245 | regulator_disable(pd->vconn); |
Jack Pham | c4be61c | 2017-10-10 16:59:32 -0700 | [diff] [blame] | 2246 | pd->vconn_enabled = false; |
| 2247 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2248 | if (pd->vbus_enabled) |
| 2249 | regulator_disable(pd->vbus); |
Jack Pham | c4be61c | 2017-10-10 16:59:32 -0700 | [diff] [blame] | 2250 | pd->vbus_enabled = false; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2251 | |
| 2252 | if (pd->current_dr != DR_DFP) { |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 2253 | extcon_set_state_sync(pd->extcon, EXTCON_USB, 0); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2254 | pd->current_dr = DR_DFP; |
| 2255 | pd_phy_update_roles(pd->current_dr, pd->current_pr); |
| 2256 | } |
| 2257 | |
Jack Pham | c4be61c | 2017-10-10 16:59:32 -0700 | [diff] [blame] | 2258 | /* PE_UNKNOWN will turn on VBUS and go back to PE_SRC_STARTUP */ |
| 2259 | pd->current_state = PE_UNKNOWN; |
| 2260 | kick_sm(pd, SRC_RECOVER_TIME); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2261 | break; |
| 2262 | |
| 2263 | case PE_SRC_HARD_RESET: |
| 2264 | val.intval = 1; |
| 2265 | power_supply_set_property(pd->usb_psy, |
| 2266 | POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val); |
| 2267 | |
| 2268 | pd_send_hard_reset(pd); |
| 2269 | pd->in_explicit_contract = false; |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 2270 | pd->rdo = 0; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2271 | rx_msg_cleanup(pd); |
| 2272 | reset_vdm_state(pd); |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 2273 | kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2274 | |
| 2275 | pd->current_state = PE_SRC_TRANSITION_TO_DEFAULT; |
| 2276 | kick_sm(pd, PS_HARD_RESET_TIME); |
| 2277 | break; |
| 2278 | |
| 2279 | case PE_SNK_STARTUP: |
| 2280 | usbpd_set_state(pd, PE_SNK_STARTUP); |
| 2281 | break; |
| 2282 | |
| 2283 | case PE_SNK_DISCOVERY: |
| 2284 | if (!rx_msg) { |
| 2285 | if (pd->vbus_present) |
| 2286 | usbpd_set_state(pd, |
| 2287 | PE_SNK_WAIT_FOR_CAPABILITIES); |
| 2288 | |
| 2289 | /* |
| 2290 | * Handle disconnection in the middle of PR_Swap. |
| 2291 | * Since in psy_changed() if pd->in_pr_swap is true |
| 2292 | * we ignore the typec_mode==NONE change since that is |
| 2293 | * expected to happen. However if the cable really did |
| 2294 | * get disconnected we need to check for it here after |
| 2295 | * waiting for VBUS presence times out. |
| 2296 | */ |
| 2297 | if (!pd->typec_mode) { |
| 2298 | pd->current_pr = PR_NONE; |
| 2299 | kick_sm(pd, 0); |
| 2300 | } |
| 2301 | |
| 2302 | break; |
| 2303 | } |
| 2304 | /* else fall-through */ |
| 2305 | |
| 2306 | case PE_SNK_WAIT_FOR_CAPABILITIES: |
| 2307 | pd->in_pr_swap = false; |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 2308 | val.intval = 0; |
| 2309 | power_supply_set_property(pd->usb_psy, |
| 2310 | POWER_SUPPLY_PROP_PR_SWAP, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2311 | |
| 2312 | if (IS_DATA(rx_msg, MSG_SOURCE_CAPABILITIES)) { |
| 2313 | val.intval = 0; |
| 2314 | power_supply_set_property(pd->usb_psy, |
| 2315 | POWER_SUPPLY_PROP_PD_IN_HARD_RESET, |
| 2316 | &val); |
| 2317 | |
| 2318 | /* save the PDOs so userspace can further evaluate */ |
Jack Pham | e9e1f13 | 2017-10-23 09:47:49 -0700 | [diff] [blame] | 2319 | memset(&pd->received_pdos, 0, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2320 | sizeof(pd->received_pdos)); |
Jack Pham | e9e1f13 | 2017-10-23 09:47:49 -0700 | [diff] [blame] | 2321 | memcpy(&pd->received_pdos, rx_msg->payload, |
| 2322 | min_t(size_t, rx_msg->data_len, |
| 2323 | sizeof(pd->received_pdos))); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2324 | pd->src_cap_id++; |
| 2325 | |
| 2326 | usbpd_set_state(pd, PE_SNK_EVALUATE_CAPABILITY); |
| 2327 | |
| 2328 | val.intval = 1; |
| 2329 | power_supply_set_property(pd->usb_psy, |
| 2330 | POWER_SUPPLY_PROP_PD_ACTIVE, &val); |
| 2331 | } else if (pd->hard_reset_count < 3) { |
| 2332 | usbpd_set_state(pd, PE_SNK_HARD_RESET); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2333 | } else { |
| 2334 | usbpd_dbg(&pd->dev, "Sink hard reset count exceeded, disabling PD\n"); |
| 2335 | |
| 2336 | val.intval = 0; |
| 2337 | power_supply_set_property(pd->usb_psy, |
| 2338 | POWER_SUPPLY_PROP_PD_IN_HARD_RESET, |
| 2339 | &val); |
| 2340 | |
| 2341 | val.intval = 0; |
| 2342 | power_supply_set_property(pd->usb_psy, |
| 2343 | POWER_SUPPLY_PROP_PD_ACTIVE, &val); |
| 2344 | } |
| 2345 | break; |
| 2346 | |
| 2347 | case PE_SNK_SELECT_CAPABILITY: |
| 2348 | if (IS_CTRL(rx_msg, MSG_ACCEPT)) { |
Jack Pham | 78c869a | 2017-02-14 16:10:28 -0800 | [diff] [blame] | 2349 | u32 pdo = pd->received_pdos[pd->requested_pdo - 1]; |
| 2350 | bool same_pps = (pd->selected_pdo == pd->requested_pdo) |
| 2351 | && (PD_SRC_PDO_TYPE(pdo) == |
| 2352 | PD_SRC_PDO_TYPE_AUGMENTED); |
| 2353 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2354 | usbpd_set_state(pd, PE_SNK_TRANSITION_SINK); |
| 2355 | |
| 2356 | /* prepare for voltage increase/decrease */ |
| 2357 | val.intval = pd->requested_voltage; |
| 2358 | power_supply_set_property(pd->usb_psy, |
| 2359 | pd->requested_voltage >= pd->current_voltage ? |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2360 | POWER_SUPPLY_PROP_PD_VOLTAGE_MAX : |
| 2361 | POWER_SUPPLY_PROP_PD_VOLTAGE_MIN, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2362 | &val); |
| 2363 | |
| 2364 | /* |
Jack Pham | 78c869a | 2017-02-14 16:10:28 -0800 | [diff] [blame] | 2365 | * if changing voltages (not within the same PPS PDO), |
| 2366 | * we must lower input current to pSnkStdby (2.5W). |
| 2367 | * Calculate it and set PD_CURRENT_MAX accordingly. |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2368 | */ |
Jack Pham | 78c869a | 2017-02-14 16:10:28 -0800 | [diff] [blame] | 2369 | if (!same_pps && |
| 2370 | pd->requested_voltage != pd->current_voltage) { |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2371 | int mv = max(pd->requested_voltage, |
| 2372 | pd->current_voltage) / 1000; |
| 2373 | val.intval = (2500000 / mv) * 1000; |
| 2374 | power_supply_set_property(pd->usb_psy, |
| 2375 | POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val); |
| 2376 | } else { |
| 2377 | /* decreasing current? */ |
| 2378 | ret = power_supply_get_property(pd->usb_psy, |
| 2379 | POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val); |
| 2380 | if (!ret && |
| 2381 | pd->requested_current < val.intval) { |
| 2382 | val.intval = |
| 2383 | pd->requested_current * 1000; |
| 2384 | power_supply_set_property(pd->usb_psy, |
| 2385 | POWER_SUPPLY_PROP_PD_CURRENT_MAX, |
| 2386 | &val); |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | pd->selected_pdo = pd->requested_pdo; |
| 2391 | } else if (IS_CTRL(rx_msg, MSG_REJECT) || |
| 2392 | IS_CTRL(rx_msg, MSG_WAIT)) { |
| 2393 | if (pd->in_explicit_contract) |
| 2394 | usbpd_set_state(pd, PE_SNK_READY); |
| 2395 | else |
| 2396 | usbpd_set_state(pd, |
| 2397 | PE_SNK_WAIT_FOR_CAPABILITIES); |
| 2398 | } else if (rx_msg) { |
| 2399 | usbpd_err(&pd->dev, "Invalid response to sink request\n"); |
| 2400 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2401 | } else { |
| 2402 | /* timed out; go to hard reset */ |
| 2403 | usbpd_set_state(pd, PE_SNK_HARD_RESET); |
| 2404 | } |
| 2405 | break; |
| 2406 | |
| 2407 | case PE_SNK_TRANSITION_SINK: |
| 2408 | if (IS_CTRL(rx_msg, MSG_PS_RDY)) { |
| 2409 | val.intval = pd->requested_voltage; |
| 2410 | power_supply_set_property(pd->usb_psy, |
| 2411 | pd->requested_voltage >= pd->current_voltage ? |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2412 | POWER_SUPPLY_PROP_PD_VOLTAGE_MIN : |
| 2413 | POWER_SUPPLY_PROP_PD_VOLTAGE_MAX, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2414 | pd->current_voltage = pd->requested_voltage; |
| 2415 | |
| 2416 | /* resume charging */ |
| 2417 | val.intval = pd->requested_current * 1000; /* mA->uA */ |
| 2418 | power_supply_set_property(pd->usb_psy, |
| 2419 | POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val); |
| 2420 | |
| 2421 | usbpd_set_state(pd, PE_SNK_READY); |
| 2422 | } else { |
| 2423 | /* timed out; go to hard reset */ |
| 2424 | usbpd_set_state(pd, PE_SNK_HARD_RESET); |
| 2425 | } |
| 2426 | break; |
| 2427 | |
| 2428 | case PE_SNK_READY: |
| 2429 | if (IS_DATA(rx_msg, MSG_SOURCE_CAPABILITIES)) { |
| 2430 | /* save the PDOs so userspace can further evaluate */ |
Jack Pham | e9e1f13 | 2017-10-23 09:47:49 -0700 | [diff] [blame] | 2431 | memset(&pd->received_pdos, 0, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2432 | sizeof(pd->received_pdos)); |
Jack Pham | e9e1f13 | 2017-10-23 09:47:49 -0700 | [diff] [blame] | 2433 | memcpy(&pd->received_pdos, rx_msg->payload, |
| 2434 | min_t(size_t, rx_msg->data_len, |
| 2435 | sizeof(pd->received_pdos))); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2436 | pd->src_cap_id++; |
| 2437 | |
| 2438 | usbpd_set_state(pd, PE_SNK_EVALUATE_CAPABILITY); |
| 2439 | } else if (IS_CTRL(rx_msg, MSG_GET_SINK_CAP)) { |
| 2440 | ret = pd_send_msg(pd, MSG_SINK_CAPABILITIES, |
Jack Pham | ee1f905 | 2017-01-26 12:27:07 -0800 | [diff] [blame] | 2441 | pd->sink_caps, pd->num_sink_caps, |
| 2442 | SOP_MSG); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2443 | if (ret) { |
| 2444 | usbpd_err(&pd->dev, "Error sending Sink Caps\n"); |
| 2445 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2446 | } |
Jack Pham | 69a427e | 2017-08-04 12:26:51 -0700 | [diff] [blame] | 2447 | } else if (IS_CTRL(rx_msg, MSG_GET_SOURCE_CAP) && |
| 2448 | pd->spec_rev == USBPD_REV_20) { |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2449 | ret = pd_send_msg(pd, MSG_SOURCE_CAPABILITIES, |
| 2450 | default_src_caps, |
| 2451 | ARRAY_SIZE(default_src_caps), SOP_MSG); |
| 2452 | if (ret) { |
| 2453 | usbpd_err(&pd->dev, "Error sending SRC CAPs\n"); |
| 2454 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2455 | break; |
| 2456 | } |
| 2457 | } else if (IS_CTRL(rx_msg, MSG_DR_SWAP)) { |
| 2458 | if (pd->vdm_state == MODE_ENTERED) { |
| 2459 | usbpd_set_state(pd, PE_SNK_HARD_RESET); |
| 2460 | break; |
| 2461 | } |
| 2462 | |
| 2463 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 2464 | if (ret) { |
| 2465 | usbpd_err(&pd->dev, "Error sending Accept\n"); |
| 2466 | usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET); |
| 2467 | break; |
| 2468 | } |
| 2469 | |
| 2470 | dr_swap(pd); |
Jack Pham | 69a427e | 2017-08-04 12:26:51 -0700 | [diff] [blame] | 2471 | } else if (IS_CTRL(rx_msg, MSG_PR_SWAP) && |
| 2472 | pd->spec_rev == USBPD_REV_20) { |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2473 | /* lock in current mode */ |
| 2474 | set_power_role(pd, pd->current_pr); |
| 2475 | |
| 2476 | /* TODO: should we Reject in certain circumstances? */ |
| 2477 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 2478 | if (ret) { |
| 2479 | usbpd_err(&pd->dev, "Error sending Accept\n"); |
| 2480 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2481 | break; |
| 2482 | } |
| 2483 | |
| 2484 | pd->in_pr_swap = true; |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 2485 | val.intval = 1; |
| 2486 | power_supply_set_property(pd->usb_psy, |
| 2487 | POWER_SUPPLY_PROP_PR_SWAP, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2488 | usbpd_set_state(pd, PE_PRS_SNK_SRC_TRANSITION_TO_OFF); |
| 2489 | break; |
Jack Pham | 69a427e | 2017-08-04 12:26:51 -0700 | [diff] [blame] | 2490 | } else if (IS_CTRL(rx_msg, MSG_VCONN_SWAP) && |
| 2491 | pd->spec_rev == USBPD_REV_20) { |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2492 | /* |
| 2493 | * if VCONN is connected to VBUS, make sure we are |
| 2494 | * not in high voltage contract, otherwise reject. |
| 2495 | */ |
| 2496 | if (!pd->vconn_is_external && |
| 2497 | (pd->requested_voltage > 5000000)) { |
| 2498 | ret = pd_send_msg(pd, MSG_REJECT, NULL, 0, |
| 2499 | SOP_MSG); |
| 2500 | if (ret) { |
| 2501 | usbpd_err(&pd->dev, "Error sending Reject\n"); |
| 2502 | usbpd_set_state(pd, |
| 2503 | PE_SNK_SEND_SOFT_RESET); |
| 2504 | } |
| 2505 | |
| 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 2510 | if (ret) { |
| 2511 | usbpd_err(&pd->dev, "Error sending Accept\n"); |
| 2512 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2513 | break; |
| 2514 | } |
| 2515 | |
| 2516 | vconn_swap(pd); |
| 2517 | } else if (IS_DATA(rx_msg, MSG_VDM)) { |
| 2518 | handle_vdm_rx(pd, rx_msg); |
Hemant Kumar | 018b598 | 2017-08-09 14:14:37 -0700 | [diff] [blame] | 2519 | } else if (pd->send_get_src_cap_ext && is_sink_tx_ok(pd)) { |
| 2520 | pd->send_get_src_cap_ext = false; |
| 2521 | ret = pd_send_msg(pd, MSG_GET_SOURCE_CAP_EXTENDED, NULL, |
| 2522 | 0, SOP_MSG); |
| 2523 | if (ret) { |
| 2524 | dev_err(&pd->dev, |
| 2525 | "Error sending get_src_cap_ext\n"); |
| 2526 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2527 | break; |
| 2528 | } |
| 2529 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2530 | } else if (rx_msg && |
| 2531 | IS_EXT(rx_msg, MSG_SOURCE_CAPABILITIES_EXTENDED)) { |
| 2532 | if (rx_msg->data_len != PD_SRC_CAP_EXT_DB_LEN) { |
| 2533 | usbpd_err(&pd->dev, "Invalid src cap ext db\n"); |
| 2534 | break; |
| 2535 | } |
| 2536 | memcpy(&pd->src_cap_ext_db, rx_msg->payload, |
| 2537 | sizeof(pd->src_cap_ext_db)); |
| 2538 | complete(&pd->is_ready); |
| 2539 | } else if (pd->send_get_pps_status && is_sink_tx_ok(pd)) { |
| 2540 | pd->send_get_pps_status = false; |
| 2541 | ret = pd_send_msg(pd, MSG_GET_PPS_STATUS, NULL, |
| 2542 | 0, SOP_MSG); |
| 2543 | if (ret) { |
| 2544 | dev_err(&pd->dev, |
| 2545 | "Error sending get_pps_status\n"); |
| 2546 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2547 | break; |
| 2548 | } |
| 2549 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2550 | } else if (rx_msg && |
| 2551 | IS_EXT(rx_msg, MSG_PPS_STATUS)) { |
| 2552 | if (rx_msg->data_len != sizeof(pd->pps_status_db)) { |
| 2553 | usbpd_err(&pd->dev, "Invalid pps status db\n"); |
| 2554 | break; |
| 2555 | } |
| 2556 | memcpy(&pd->pps_status_db, rx_msg->payload, |
| 2557 | sizeof(pd->pps_status_db)); |
| 2558 | complete(&pd->is_ready); |
Hemant Kumar | a187594 | 2017-08-09 16:50:14 -0700 | [diff] [blame] | 2559 | } else if (IS_DATA(rx_msg, MSG_ALERT)) { |
| 2560 | if (rx_msg->data_len != sizeof(pd->received_ado)) { |
| 2561 | usbpd_err(&pd->dev, "Invalid ado\n"); |
| 2562 | break; |
| 2563 | } |
| 2564 | memcpy(&pd->received_ado, rx_msg->payload, |
| 2565 | sizeof(pd->received_ado)); |
| 2566 | ret = pd_send_msg(pd, MSG_GET_STATUS, NULL, |
| 2567 | 0, SOP_MSG); |
| 2568 | if (ret) { |
| 2569 | dev_err(&pd->dev, |
| 2570 | "Error sending get_status\n"); |
| 2571 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2572 | break; |
| 2573 | } |
| 2574 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2575 | } else if (rx_msg && |
| 2576 | IS_EXT(rx_msg, MSG_STATUS)) { |
| 2577 | if (rx_msg->data_len != PD_STATUS_DB_LEN) { |
| 2578 | usbpd_err(&pd->dev, "Invalid status db\n"); |
| 2579 | break; |
| 2580 | } |
| 2581 | memcpy(&pd->status_db, rx_msg->payload, |
| 2582 | sizeof(pd->status_db)); |
| 2583 | kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE); |
Hemant Kumar | 51ded97 | 2017-08-09 17:57:24 -0700 | [diff] [blame] | 2584 | } else if (pd->send_get_battery_cap && is_sink_tx_ok(pd)) { |
| 2585 | pd->send_get_battery_cap = false; |
| 2586 | ret = pd_send_ext_msg(pd, MSG_GET_BATTERY_CAP, |
| 2587 | &pd->get_battery_cap_db, 1, SOP_MSG); |
| 2588 | if (ret) { |
| 2589 | dev_err(&pd->dev, |
| 2590 | "Error sending get_battery_cap\n"); |
| 2591 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2592 | break; |
| 2593 | } |
| 2594 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2595 | } else if (rx_msg && |
| 2596 | IS_EXT(rx_msg, MSG_BATTERY_CAPABILITIES)) { |
| 2597 | if (rx_msg->data_len != PD_BATTERY_CAP_DB_LEN) { |
| 2598 | usbpd_err(&pd->dev, "Invalid battery cap db\n"); |
| 2599 | break; |
| 2600 | } |
| 2601 | memcpy(&pd->battery_cap_db, rx_msg->payload, |
| 2602 | sizeof(pd->battery_cap_db)); |
| 2603 | complete(&pd->is_ready); |
| 2604 | } else if (pd->send_get_battery_status && is_sink_tx_ok(pd)) { |
| 2605 | pd->send_get_battery_status = false; |
| 2606 | ret = pd_send_ext_msg(pd, MSG_GET_BATTERY_STATUS, |
| 2607 | &pd->get_battery_status_db, 1, SOP_MSG); |
| 2608 | if (ret) { |
| 2609 | dev_err(&pd->dev, |
| 2610 | "Error sending get_battery_status\n"); |
| 2611 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2612 | break; |
| 2613 | } |
| 2614 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2615 | } else if (rx_msg && |
| 2616 | IS_EXT(rx_msg, MSG_BATTERY_STATUS)) { |
| 2617 | if (rx_msg->data_len != sizeof(pd->battery_sts_dobj)) { |
| 2618 | usbpd_err(&pd->dev, "Invalid bat sts dobj\n"); |
| 2619 | break; |
| 2620 | } |
| 2621 | memcpy(&pd->battery_sts_dobj, rx_msg->payload, |
| 2622 | sizeof(pd->battery_sts_dobj)); |
| 2623 | complete(&pd->is_ready); |
Jack Pham | 69a427e | 2017-08-04 12:26:51 -0700 | [diff] [blame] | 2624 | } else if (rx_msg && pd->spec_rev == USBPD_REV_30) { |
| 2625 | /* unhandled messages */ |
| 2626 | ret = pd_send_msg(pd, MSG_NOT_SUPPORTED, NULL, 0, |
| 2627 | SOP_MSG); |
| 2628 | if (ret) { |
| 2629 | usbpd_err(&pd->dev, "Error sending Not supported\n"); |
| 2630 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2631 | } |
| 2632 | break; |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 2633 | } else if (pd->send_request) { |
| 2634 | pd->send_request = false; |
| 2635 | usbpd_set_state(pd, PE_SNK_SELECT_CAPABILITY); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2636 | } else if (pd->send_pr_swap && is_sink_tx_ok(pd)) { |
| 2637 | pd->send_pr_swap = false; |
| 2638 | ret = pd_send_msg(pd, MSG_PR_SWAP, NULL, 0, SOP_MSG); |
| 2639 | if (ret) { |
| 2640 | dev_err(&pd->dev, "Error sending PR Swap\n"); |
| 2641 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2642 | break; |
| 2643 | } |
| 2644 | |
| 2645 | pd->current_state = PE_PRS_SNK_SRC_SEND_SWAP; |
| 2646 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2647 | } else if (pd->send_dr_swap && is_sink_tx_ok(pd)) { |
| 2648 | pd->send_dr_swap = false; |
| 2649 | ret = pd_send_msg(pd, MSG_DR_SWAP, NULL, 0, SOP_MSG); |
| 2650 | if (ret) { |
| 2651 | dev_err(&pd->dev, "Error sending DR Swap\n"); |
| 2652 | usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET); |
| 2653 | break; |
| 2654 | } |
| 2655 | |
| 2656 | pd->current_state = PE_DRS_SEND_DR_SWAP; |
| 2657 | kick_sm(pd, SENDER_RESPONSE_TIME); |
| 2658 | } else if (is_sink_tx_ok(pd)) { |
| 2659 | handle_vdm_tx(pd); |
| 2660 | } |
| 2661 | break; |
| 2662 | |
| 2663 | case PE_SNK_TRANSITION_TO_DEFAULT: |
| 2664 | usbpd_set_state(pd, PE_SNK_STARTUP); |
| 2665 | break; |
| 2666 | |
| 2667 | case PE_SRC_SOFT_RESET: |
| 2668 | case PE_SNK_SOFT_RESET: |
| 2669 | pd_reset_protocol(pd); |
| 2670 | |
| 2671 | ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG); |
| 2672 | if (ret) { |
| 2673 | usbpd_err(&pd->dev, "%s: Error sending Accept, do Hard Reset\n", |
| 2674 | usbpd_state_strings[pd->current_state]); |
| 2675 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 2676 | PE_SRC_HARD_RESET : PE_SNK_HARD_RESET); |
| 2677 | break; |
| 2678 | } |
| 2679 | |
| 2680 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 2681 | PE_SRC_SEND_CAPABILITIES : |
| 2682 | PE_SNK_WAIT_FOR_CAPABILITIES); |
| 2683 | break; |
| 2684 | |
| 2685 | case PE_SRC_SEND_SOFT_RESET: |
| 2686 | case PE_SNK_SEND_SOFT_RESET: |
| 2687 | if (IS_CTRL(rx_msg, MSG_ACCEPT)) { |
| 2688 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 2689 | PE_SRC_SEND_CAPABILITIES : |
| 2690 | PE_SNK_WAIT_FOR_CAPABILITIES); |
| 2691 | } else { |
| 2692 | usbpd_err(&pd->dev, "%s: Did not see Accept, do Hard Reset\n", |
| 2693 | usbpd_state_strings[pd->current_state]); |
| 2694 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 2695 | PE_SRC_HARD_RESET : PE_SNK_HARD_RESET); |
| 2696 | } |
| 2697 | break; |
| 2698 | |
| 2699 | case PE_SNK_HARD_RESET: |
| 2700 | /* prepare charger for VBUS change */ |
| 2701 | val.intval = 1; |
| 2702 | power_supply_set_property(pd->usb_psy, |
| 2703 | POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val); |
| 2704 | |
| 2705 | pd->requested_voltage = 5000000; |
| 2706 | |
| 2707 | if (pd->requested_current) { |
| 2708 | val.intval = pd->requested_current = 0; |
| 2709 | power_supply_set_property(pd->usb_psy, |
| 2710 | POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val); |
| 2711 | } |
| 2712 | |
| 2713 | val.intval = pd->requested_voltage; |
| 2714 | power_supply_set_property(pd->usb_psy, |
Nicholas Troast | 7f55c92 | 2017-07-25 13:18:03 -0700 | [diff] [blame] | 2715 | POWER_SUPPLY_PROP_PD_VOLTAGE_MIN, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2716 | |
| 2717 | pd_send_hard_reset(pd); |
| 2718 | pd->in_explicit_contract = false; |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 2719 | pd->selected_pdo = pd->requested_pdo = 0; |
| 2720 | pd->rdo = 0; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2721 | reset_vdm_state(pd); |
Jack Pham | f6c02da | 2017-01-31 15:23:56 -0800 | [diff] [blame] | 2722 | kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2723 | usbpd_set_state(pd, PE_SNK_TRANSITION_TO_DEFAULT); |
| 2724 | break; |
| 2725 | |
| 2726 | case PE_DRS_SEND_DR_SWAP: |
| 2727 | if (IS_CTRL(rx_msg, MSG_ACCEPT)) |
| 2728 | dr_swap(pd); |
| 2729 | |
| 2730 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 2731 | PE_SRC_READY : PE_SNK_READY); |
| 2732 | break; |
| 2733 | |
| 2734 | case PE_PRS_SRC_SNK_SEND_SWAP: |
| 2735 | if (!IS_CTRL(rx_msg, MSG_ACCEPT)) { |
| 2736 | pd->current_state = PE_SRC_READY; |
| 2737 | break; |
| 2738 | } |
| 2739 | |
| 2740 | pd->current_state = PE_PRS_SRC_SNK_TRANSITION_TO_OFF; |
| 2741 | kick_sm(pd, SRC_TRANSITION_TIME); |
| 2742 | break; |
| 2743 | |
| 2744 | case PE_PRS_SRC_SNK_TRANSITION_TO_OFF: |
| 2745 | pd->in_pr_swap = true; |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 2746 | val.intval = 1; |
| 2747 | power_supply_set_property(pd->usb_psy, |
| 2748 | POWER_SUPPLY_PROP_PR_SWAP, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2749 | pd->in_explicit_contract = false; |
| 2750 | |
| 2751 | if (pd->vbus_enabled) { |
| 2752 | regulator_disable(pd->vbus); |
| 2753 | pd->vbus_enabled = false; |
| 2754 | } |
| 2755 | |
| 2756 | /* PE_PRS_SRC_SNK_Assert_Rd */ |
| 2757 | pd->current_pr = PR_SINK; |
| 2758 | set_power_role(pd, pd->current_pr); |
| 2759 | pd_phy_update_roles(pd->current_dr, pd->current_pr); |
| 2760 | |
| 2761 | /* allow time for Vbus discharge, must be < tSrcSwapStdby */ |
| 2762 | msleep(500); |
| 2763 | |
| 2764 | ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG); |
| 2765 | if (ret) { |
| 2766 | usbpd_err(&pd->dev, "Error sending PS_RDY\n"); |
| 2767 | usbpd_set_state(pd, PE_ERROR_RECOVERY); |
| 2768 | break; |
| 2769 | } |
| 2770 | |
| 2771 | pd->current_state = PE_PRS_SRC_SNK_WAIT_SOURCE_ON; |
| 2772 | kick_sm(pd, PS_SOURCE_ON); |
| 2773 | break; |
| 2774 | |
| 2775 | case PE_PRS_SRC_SNK_WAIT_SOURCE_ON: |
| 2776 | if (IS_CTRL(rx_msg, MSG_PS_RDY)) |
| 2777 | usbpd_set_state(pd, PE_SNK_STARTUP); |
| 2778 | else |
| 2779 | usbpd_set_state(pd, PE_ERROR_RECOVERY); |
| 2780 | break; |
| 2781 | |
| 2782 | case PE_PRS_SNK_SRC_SEND_SWAP: |
| 2783 | if (!IS_CTRL(rx_msg, MSG_ACCEPT)) { |
| 2784 | pd->current_state = PE_SNK_READY; |
| 2785 | break; |
| 2786 | } |
| 2787 | |
| 2788 | pd->in_pr_swap = true; |
Jack Pham | 857ee68 | 2017-05-25 11:53:36 -0700 | [diff] [blame] | 2789 | val.intval = 1; |
| 2790 | power_supply_set_property(pd->usb_psy, |
| 2791 | POWER_SUPPLY_PROP_PR_SWAP, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2792 | usbpd_set_state(pd, PE_PRS_SNK_SRC_TRANSITION_TO_OFF); |
| 2793 | break; |
| 2794 | |
| 2795 | case PE_PRS_SNK_SRC_TRANSITION_TO_OFF: |
| 2796 | if (!IS_CTRL(rx_msg, MSG_PS_RDY)) { |
| 2797 | usbpd_set_state(pd, PE_ERROR_RECOVERY); |
| 2798 | break; |
| 2799 | } |
| 2800 | |
| 2801 | /* PE_PRS_SNK_SRC_Assert_Rp */ |
| 2802 | pd->current_pr = PR_SRC; |
| 2803 | set_power_role(pd, pd->current_pr); |
| 2804 | pd->current_state = PE_PRS_SNK_SRC_SOURCE_ON; |
| 2805 | |
| 2806 | /* fall-through */ |
| 2807 | |
| 2808 | case PE_PRS_SNK_SRC_SOURCE_ON: |
| 2809 | enable_vbus(pd); |
| 2810 | msleep(200); /* allow time VBUS ramp-up, must be < tNewSrc */ |
| 2811 | |
| 2812 | ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG); |
| 2813 | if (ret) { |
| 2814 | usbpd_err(&pd->dev, "Error sending PS_RDY\n"); |
| 2815 | usbpd_set_state(pd, PE_ERROR_RECOVERY); |
| 2816 | break; |
| 2817 | } |
| 2818 | |
| 2819 | usbpd_set_state(pd, PE_SRC_STARTUP); |
| 2820 | break; |
| 2821 | |
| 2822 | case PE_VCS_WAIT_FOR_VCONN: |
| 2823 | if (IS_CTRL(rx_msg, MSG_PS_RDY)) { |
| 2824 | /* |
| 2825 | * hopefully redundant check but in case not enabled |
| 2826 | * avoids unbalanced regulator disable count |
| 2827 | */ |
| 2828 | if (pd->vconn_enabled) |
| 2829 | regulator_disable(pd->vconn); |
| 2830 | pd->vconn_enabled = false; |
| 2831 | |
| 2832 | pd->current_state = pd->current_pr == PR_SRC ? |
| 2833 | PE_SRC_READY : PE_SNK_READY; |
| 2834 | } else { |
| 2835 | /* timed out; go to hard reset */ |
| 2836 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 2837 | PE_SRC_HARD_RESET : PE_SNK_HARD_RESET); |
| 2838 | } |
| 2839 | |
| 2840 | break; |
| 2841 | |
| 2842 | default: |
| 2843 | usbpd_err(&pd->dev, "Unhandled state %s\n", |
| 2844 | usbpd_state_strings[pd->current_state]); |
| 2845 | break; |
| 2846 | } |
| 2847 | |
| 2848 | sm_done: |
| 2849 | kfree(rx_msg); |
| 2850 | |
Jack Pham | 564b417 | 2017-05-24 18:41:53 -0700 | [diff] [blame] | 2851 | spin_lock_irqsave(&pd->rx_lock, flags); |
| 2852 | ret = list_empty(&pd->rx_q); |
| 2853 | spin_unlock_irqrestore(&pd->rx_lock, flags); |
| 2854 | |
| 2855 | /* requeue if there are any new/pending RX messages */ |
| 2856 | if (!ret) |
| 2857 | kick_sm(pd, 0); |
| 2858 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2859 | if (!pd->sm_queued) |
| 2860 | pm_relax(&pd->dev); |
| 2861 | } |
| 2862 | |
| 2863 | static inline const char *src_current(enum power_supply_typec_mode typec_mode) |
| 2864 | { |
| 2865 | switch (typec_mode) { |
| 2866 | case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT: |
| 2867 | return "default"; |
| 2868 | case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM: |
| 2869 | return "medium - 1.5A"; |
| 2870 | case POWER_SUPPLY_TYPEC_SOURCE_HIGH: |
| 2871 | return "high - 3.0A"; |
| 2872 | default: |
| 2873 | return ""; |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | static int psy_changed(struct notifier_block *nb, unsigned long evt, void *ptr) |
| 2878 | { |
| 2879 | struct usbpd *pd = container_of(nb, struct usbpd, psy_nb); |
| 2880 | union power_supply_propval val; |
| 2881 | enum power_supply_typec_mode typec_mode; |
| 2882 | int ret; |
| 2883 | |
| 2884 | if (ptr != pd->usb_psy || evt != PSY_EVENT_PROP_CHANGED) |
| 2885 | return 0; |
| 2886 | |
| 2887 | ret = power_supply_get_property(pd->usb_psy, |
| 2888 | POWER_SUPPLY_PROP_TYPEC_MODE, &val); |
| 2889 | if (ret) { |
| 2890 | usbpd_err(&pd->dev, "Unable to read USB TYPEC_MODE: %d\n", ret); |
| 2891 | return ret; |
| 2892 | } |
| 2893 | |
| 2894 | typec_mode = val.intval; |
| 2895 | |
| 2896 | ret = power_supply_get_property(pd->usb_psy, |
| 2897 | POWER_SUPPLY_PROP_PE_START, &val); |
| 2898 | if (ret) { |
| 2899 | usbpd_err(&pd->dev, "Unable to read USB PROP_PE_START: %d\n", |
| 2900 | ret); |
| 2901 | return ret; |
| 2902 | } |
| 2903 | |
| 2904 | /* Don't proceed if PE_START=0 as other props may still change */ |
| 2905 | if (!val.intval && !pd->pd_connected && |
| 2906 | typec_mode != POWER_SUPPLY_TYPEC_NONE) |
| 2907 | return 0; |
| 2908 | |
| 2909 | ret = power_supply_get_property(pd->usb_psy, |
| 2910 | POWER_SUPPLY_PROP_PRESENT, &val); |
| 2911 | if (ret) { |
| 2912 | usbpd_err(&pd->dev, "Unable to read USB PRESENT: %d\n", ret); |
| 2913 | return ret; |
| 2914 | } |
| 2915 | |
| 2916 | pd->vbus_present = val.intval; |
| 2917 | |
| 2918 | ret = power_supply_get_property(pd->usb_psy, |
Fenglin Wu | 80826e0 | 2017-04-25 21:45:08 +0800 | [diff] [blame] | 2919 | POWER_SUPPLY_PROP_REAL_TYPE, &val); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2920 | if (ret) { |
| 2921 | usbpd_err(&pd->dev, "Unable to read USB TYPE: %d\n", ret); |
| 2922 | return ret; |
| 2923 | } |
| 2924 | |
| 2925 | pd->psy_type = val.intval; |
| 2926 | |
| 2927 | /* |
| 2928 | * For sink hard reset, state machine needs to know when VBUS changes |
| 2929 | * - when in PE_SNK_TRANSITION_TO_DEFAULT, notify when VBUS falls |
| 2930 | * - when in PE_SNK_DISCOVERY, notify when VBUS rises |
| 2931 | */ |
| 2932 | if (typec_mode && ((!pd->vbus_present && |
| 2933 | pd->current_state == PE_SNK_TRANSITION_TO_DEFAULT) || |
| 2934 | (pd->vbus_present && pd->current_state == PE_SNK_DISCOVERY))) { |
| 2935 | usbpd_dbg(&pd->dev, "hard reset: typec mode:%d present:%d\n", |
| 2936 | typec_mode, pd->vbus_present); |
| 2937 | pd->typec_mode = typec_mode; |
| 2938 | kick_sm(pd, 0); |
| 2939 | return 0; |
| 2940 | } |
| 2941 | |
| 2942 | if (pd->typec_mode == typec_mode) |
| 2943 | return 0; |
| 2944 | |
| 2945 | pd->typec_mode = typec_mode; |
| 2946 | |
| 2947 | usbpd_dbg(&pd->dev, "typec mode:%d present:%d type:%d orientation:%d\n", |
| 2948 | typec_mode, pd->vbus_present, pd->psy_type, |
| 2949 | usbpd_get_plug_orientation(pd)); |
| 2950 | |
| 2951 | switch (typec_mode) { |
| 2952 | /* Disconnect */ |
| 2953 | case POWER_SUPPLY_TYPEC_NONE: |
| 2954 | if (pd->in_pr_swap) { |
| 2955 | usbpd_dbg(&pd->dev, "Ignoring disconnect due to PR swap\n"); |
| 2956 | return 0; |
| 2957 | } |
| 2958 | |
| 2959 | pd->current_pr = PR_NONE; |
| 2960 | break; |
| 2961 | |
| 2962 | /* Sink states */ |
| 2963 | case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT: |
| 2964 | case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM: |
| 2965 | case POWER_SUPPLY_TYPEC_SOURCE_HIGH: |
| 2966 | usbpd_info(&pd->dev, "Type-C Source (%s) connected\n", |
| 2967 | src_current(typec_mode)); |
| 2968 | |
| 2969 | /* if waiting for SinkTxOk to start an AMS */ |
| 2970 | if (pd->spec_rev == USBPD_REV_30 && |
| 2971 | typec_mode == POWER_SUPPLY_TYPEC_SOURCE_HIGH && |
| 2972 | (pd->send_pr_swap || pd->send_dr_swap || pd->vdm_tx)) |
| 2973 | break; |
| 2974 | |
| 2975 | if (pd->current_pr == PR_SINK) |
| 2976 | return 0; |
| 2977 | |
Jack Pham | d434a41 | 2017-07-24 10:05:05 -0700 | [diff] [blame] | 2978 | /* |
| 2979 | * Unexpected if not in PR swap; need to force disconnect from |
| 2980 | * source so we can turn off VBUS, Vconn, PD PHY etc. |
| 2981 | */ |
| 2982 | if (pd->current_pr == PR_SRC) { |
| 2983 | usbpd_info(&pd->dev, "Forcing disconnect from source mode\n"); |
| 2984 | pd->current_pr = PR_NONE; |
| 2985 | break; |
| 2986 | } |
| 2987 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 2988 | pd->current_pr = PR_SINK; |
| 2989 | break; |
| 2990 | |
| 2991 | /* Source states */ |
| 2992 | case POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE: |
| 2993 | case POWER_SUPPLY_TYPEC_SINK: |
| 2994 | usbpd_info(&pd->dev, "Type-C Sink%s connected\n", |
| 2995 | typec_mode == POWER_SUPPLY_TYPEC_SINK ? |
| 2996 | "" : " (powered)"); |
| 2997 | |
| 2998 | if (pd->current_pr == PR_SRC) |
| 2999 | return 0; |
| 3000 | |
| 3001 | pd->current_pr = PR_SRC; |
| 3002 | break; |
| 3003 | |
| 3004 | case POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY: |
| 3005 | usbpd_info(&pd->dev, "Type-C Debug Accessory connected\n"); |
| 3006 | break; |
| 3007 | case POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER: |
| 3008 | usbpd_info(&pd->dev, "Type-C Analog Audio Adapter connected\n"); |
| 3009 | break; |
| 3010 | default: |
| 3011 | usbpd_warn(&pd->dev, "Unsupported typec mode:%d\n", |
| 3012 | typec_mode); |
| 3013 | break; |
| 3014 | } |
| 3015 | |
| 3016 | /* queue state machine due to CC state change */ |
| 3017 | kick_sm(pd, 0); |
| 3018 | return 0; |
| 3019 | } |
| 3020 | |
| 3021 | static enum dual_role_property usbpd_dr_properties[] = { |
| 3022 | DUAL_ROLE_PROP_SUPPORTED_MODES, |
| 3023 | DUAL_ROLE_PROP_MODE, |
| 3024 | DUAL_ROLE_PROP_PR, |
| 3025 | DUAL_ROLE_PROP_DR, |
| 3026 | }; |
| 3027 | |
| 3028 | static int usbpd_dr_get_property(struct dual_role_phy_instance *dual_role, |
| 3029 | enum dual_role_property prop, unsigned int *val) |
| 3030 | { |
| 3031 | struct usbpd *pd = dual_role_get_drvdata(dual_role); |
| 3032 | |
| 3033 | if (!pd) |
| 3034 | return -ENODEV; |
| 3035 | |
| 3036 | switch (prop) { |
| 3037 | case DUAL_ROLE_PROP_MODE: |
| 3038 | /* For now associate UFP/DFP with data role only */ |
| 3039 | if (pd->current_dr == DR_UFP) |
| 3040 | *val = DUAL_ROLE_PROP_MODE_UFP; |
| 3041 | else if (pd->current_dr == DR_DFP) |
| 3042 | *val = DUAL_ROLE_PROP_MODE_DFP; |
| 3043 | else |
| 3044 | *val = DUAL_ROLE_PROP_MODE_NONE; |
| 3045 | break; |
| 3046 | case DUAL_ROLE_PROP_PR: |
| 3047 | if (pd->current_pr == PR_SRC) |
| 3048 | *val = DUAL_ROLE_PROP_PR_SRC; |
| 3049 | else if (pd->current_pr == PR_SINK) |
| 3050 | *val = DUAL_ROLE_PROP_PR_SNK; |
| 3051 | else |
| 3052 | *val = DUAL_ROLE_PROP_PR_NONE; |
| 3053 | break; |
| 3054 | case DUAL_ROLE_PROP_DR: |
| 3055 | if (pd->current_dr == DR_UFP) |
| 3056 | *val = DUAL_ROLE_PROP_DR_DEVICE; |
| 3057 | else if (pd->current_dr == DR_DFP) |
| 3058 | *val = DUAL_ROLE_PROP_DR_HOST; |
| 3059 | else |
| 3060 | *val = DUAL_ROLE_PROP_DR_NONE; |
| 3061 | break; |
| 3062 | default: |
| 3063 | usbpd_warn(&pd->dev, "unsupported property %d\n", prop); |
| 3064 | return -ENODATA; |
| 3065 | } |
| 3066 | |
| 3067 | return 0; |
| 3068 | } |
| 3069 | |
| 3070 | static int usbpd_dr_set_property(struct dual_role_phy_instance *dual_role, |
| 3071 | enum dual_role_property prop, const unsigned int *val) |
| 3072 | { |
| 3073 | struct usbpd *pd = dual_role_get_drvdata(dual_role); |
| 3074 | bool do_swap = false; |
| 3075 | |
| 3076 | if (!pd) |
| 3077 | return -ENODEV; |
| 3078 | |
| 3079 | switch (prop) { |
| 3080 | case DUAL_ROLE_PROP_MODE: |
| 3081 | usbpd_dbg(&pd->dev, "Setting mode to %d\n", *val); |
| 3082 | |
| 3083 | /* |
| 3084 | * Forces disconnect on CC and re-establishes connection. |
| 3085 | * This does not use PD-based PR/DR swap |
| 3086 | */ |
| 3087 | if (*val == DUAL_ROLE_PROP_MODE_UFP) |
| 3088 | pd->forced_pr = POWER_SUPPLY_TYPEC_PR_SINK; |
| 3089 | else if (*val == DUAL_ROLE_PROP_MODE_DFP) |
| 3090 | pd->forced_pr = POWER_SUPPLY_TYPEC_PR_SOURCE; |
| 3091 | |
| 3092 | /* new mode will be applied in disconnect handler */ |
| 3093 | set_power_role(pd, PR_NONE); |
| 3094 | |
| 3095 | /* wait until it takes effect */ |
| 3096 | while (pd->forced_pr != POWER_SUPPLY_TYPEC_PR_NONE) |
| 3097 | msleep(20); |
| 3098 | |
| 3099 | break; |
| 3100 | |
| 3101 | case DUAL_ROLE_PROP_DR: |
| 3102 | usbpd_dbg(&pd->dev, "Setting data_role to %d\n", *val); |
| 3103 | |
| 3104 | if (*val == DUAL_ROLE_PROP_DR_HOST) { |
| 3105 | if (pd->current_dr == DR_UFP) |
| 3106 | do_swap = true; |
| 3107 | } else if (*val == DUAL_ROLE_PROP_DR_DEVICE) { |
| 3108 | if (pd->current_dr == DR_DFP) |
| 3109 | do_swap = true; |
| 3110 | } else { |
| 3111 | usbpd_warn(&pd->dev, "setting data_role to 'none' unsupported\n"); |
| 3112 | return -ENOTSUPP; |
| 3113 | } |
| 3114 | |
| 3115 | if (do_swap) { |
| 3116 | if (pd->current_state != PE_SRC_READY && |
| 3117 | pd->current_state != PE_SNK_READY) { |
| 3118 | usbpd_err(&pd->dev, "data_role swap not allowed: PD not in Ready state\n"); |
| 3119 | return -EAGAIN; |
| 3120 | } |
| 3121 | |
| 3122 | if (pd->current_state == PE_SNK_READY && |
| 3123 | !is_sink_tx_ok(pd)) { |
| 3124 | usbpd_err(&pd->dev, "Rp indicates SinkTxNG\n"); |
| 3125 | return -EAGAIN; |
| 3126 | } |
| 3127 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3128 | mutex_lock(&pd->swap_lock); |
| 3129 | reinit_completion(&pd->is_ready); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3130 | pd->send_dr_swap = true; |
| 3131 | kick_sm(pd, 0); |
| 3132 | |
| 3133 | /* wait for operation to complete */ |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3134 | if (!wait_for_completion_timeout(&pd->is_ready, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3135 | msecs_to_jiffies(100))) { |
| 3136 | usbpd_err(&pd->dev, "data_role swap timed out\n"); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3137 | mutex_unlock(&pd->swap_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3138 | return -ETIMEDOUT; |
| 3139 | } |
| 3140 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3141 | mutex_unlock(&pd->swap_lock); |
| 3142 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3143 | if ((*val == DUAL_ROLE_PROP_DR_HOST && |
| 3144 | pd->current_dr != DR_DFP) || |
| 3145 | (*val == DUAL_ROLE_PROP_DR_DEVICE && |
| 3146 | pd->current_dr != DR_UFP)) { |
| 3147 | usbpd_err(&pd->dev, "incorrect state (%s) after data_role swap\n", |
| 3148 | pd->current_dr == DR_DFP ? |
| 3149 | "dfp" : "ufp"); |
| 3150 | return -EPROTO; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | break; |
| 3155 | |
| 3156 | case DUAL_ROLE_PROP_PR: |
| 3157 | usbpd_dbg(&pd->dev, "Setting power_role to %d\n", *val); |
| 3158 | |
| 3159 | if (*val == DUAL_ROLE_PROP_PR_SRC) { |
| 3160 | if (pd->current_pr == PR_SINK) |
| 3161 | do_swap = true; |
| 3162 | } else if (*val == DUAL_ROLE_PROP_PR_SNK) { |
| 3163 | if (pd->current_pr == PR_SRC) |
| 3164 | do_swap = true; |
| 3165 | } else { |
| 3166 | usbpd_warn(&pd->dev, "setting power_role to 'none' unsupported\n"); |
| 3167 | return -ENOTSUPP; |
| 3168 | } |
| 3169 | |
| 3170 | if (do_swap) { |
| 3171 | if (pd->current_state != PE_SRC_READY && |
| 3172 | pd->current_state != PE_SNK_READY) { |
| 3173 | usbpd_err(&pd->dev, "power_role swap not allowed: PD not in Ready state\n"); |
| 3174 | return -EAGAIN; |
| 3175 | } |
| 3176 | |
| 3177 | if (pd->current_state == PE_SNK_READY && |
| 3178 | !is_sink_tx_ok(pd)) { |
| 3179 | usbpd_err(&pd->dev, "Rp indicates SinkTxNG\n"); |
| 3180 | return -EAGAIN; |
| 3181 | } |
| 3182 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3183 | mutex_lock(&pd->swap_lock); |
| 3184 | reinit_completion(&pd->is_ready); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3185 | pd->send_pr_swap = true; |
| 3186 | kick_sm(pd, 0); |
| 3187 | |
| 3188 | /* wait for operation to complete */ |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3189 | if (!wait_for_completion_timeout(&pd->is_ready, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3190 | msecs_to_jiffies(2000))) { |
| 3191 | usbpd_err(&pd->dev, "power_role swap timed out\n"); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3192 | mutex_unlock(&pd->swap_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3193 | return -ETIMEDOUT; |
| 3194 | } |
| 3195 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3196 | mutex_unlock(&pd->swap_lock); |
| 3197 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3198 | if ((*val == DUAL_ROLE_PROP_PR_SRC && |
| 3199 | pd->current_pr != PR_SRC) || |
| 3200 | (*val == DUAL_ROLE_PROP_PR_SNK && |
| 3201 | pd->current_pr != PR_SINK)) { |
| 3202 | usbpd_err(&pd->dev, "incorrect state (%s) after power_role swap\n", |
| 3203 | pd->current_pr == PR_SRC ? |
| 3204 | "source" : "sink"); |
| 3205 | return -EPROTO; |
| 3206 | } |
| 3207 | } |
| 3208 | break; |
| 3209 | |
| 3210 | default: |
| 3211 | usbpd_warn(&pd->dev, "unsupported property %d\n", prop); |
| 3212 | return -ENOTSUPP; |
| 3213 | } |
| 3214 | |
| 3215 | return 0; |
| 3216 | } |
| 3217 | |
| 3218 | static int usbpd_dr_prop_writeable(struct dual_role_phy_instance *dual_role, |
| 3219 | enum dual_role_property prop) |
| 3220 | { |
Jack Pham | e232bde | 2017-03-02 11:37:00 -0800 | [diff] [blame] | 3221 | struct usbpd *pd = dual_role_get_drvdata(dual_role); |
| 3222 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3223 | switch (prop) { |
| 3224 | case DUAL_ROLE_PROP_MODE: |
Jack Pham | e232bde | 2017-03-02 11:37:00 -0800 | [diff] [blame] | 3225 | return 1; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3226 | case DUAL_ROLE_PROP_DR: |
| 3227 | case DUAL_ROLE_PROP_PR: |
Jack Pham | e232bde | 2017-03-02 11:37:00 -0800 | [diff] [blame] | 3228 | if (pd) |
| 3229 | return pd->current_state == PE_SNK_READY || |
| 3230 | pd->current_state == PE_SRC_READY; |
| 3231 | break; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3232 | default: |
| 3233 | break; |
| 3234 | } |
| 3235 | |
| 3236 | return 0; |
| 3237 | } |
| 3238 | |
| 3239 | static int usbpd_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 3240 | { |
| 3241 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3242 | int i; |
| 3243 | |
| 3244 | add_uevent_var(env, "DATA_ROLE=%s", pd->current_dr == DR_DFP ? |
| 3245 | "dfp" : "ufp"); |
| 3246 | |
| 3247 | if (pd->current_pr == PR_SINK) { |
| 3248 | add_uevent_var(env, "POWER_ROLE=sink"); |
| 3249 | add_uevent_var(env, "SRC_CAP_ID=%d", pd->src_cap_id); |
| 3250 | |
| 3251 | for (i = 0; i < ARRAY_SIZE(pd->received_pdos); i++) |
| 3252 | add_uevent_var(env, "PDO%d=%08x", i, |
| 3253 | pd->received_pdos[i]); |
| 3254 | |
| 3255 | add_uevent_var(env, "REQUESTED_PDO=%d", pd->requested_pdo); |
| 3256 | add_uevent_var(env, "SELECTED_PDO=%d", pd->selected_pdo); |
| 3257 | } else { |
| 3258 | add_uevent_var(env, "POWER_ROLE=source"); |
| 3259 | for (i = 0; i < ARRAY_SIZE(default_src_caps); i++) |
| 3260 | add_uevent_var(env, "PDO%d=%08x", i, |
| 3261 | default_src_caps[i]); |
| 3262 | } |
| 3263 | |
| 3264 | add_uevent_var(env, "RDO=%08x", pd->rdo); |
| 3265 | add_uevent_var(env, "CONTRACT=%s", pd->in_explicit_contract ? |
| 3266 | "explicit" : "implicit"); |
| 3267 | add_uevent_var(env, "ALT_MODE=%d", pd->vdm_state == MODE_ENTERED); |
| 3268 | |
Hemant Kumar | a187594 | 2017-08-09 16:50:14 -0700 | [diff] [blame] | 3269 | add_uevent_var(env, "ADO=%08x", pd->received_ado); |
| 3270 | for (i = 0; i < PD_STATUS_DB_LEN; i++) |
| 3271 | add_uevent_var(env, "SDB%d=%08x", i, pd->status_db[i]); |
| 3272 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3273 | return 0; |
| 3274 | } |
| 3275 | |
| 3276 | static ssize_t contract_show(struct device *dev, struct device_attribute *attr, |
| 3277 | char *buf) |
| 3278 | { |
| 3279 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3280 | |
| 3281 | return snprintf(buf, PAGE_SIZE, "%s\n", |
| 3282 | pd->in_explicit_contract ? "explicit" : "implicit"); |
| 3283 | } |
| 3284 | static DEVICE_ATTR_RO(contract); |
| 3285 | |
| 3286 | static ssize_t current_pr_show(struct device *dev, |
| 3287 | struct device_attribute *attr, char *buf) |
| 3288 | { |
| 3289 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3290 | const char *pr = "none"; |
| 3291 | |
| 3292 | if (pd->current_pr == PR_SINK) |
| 3293 | pr = "sink"; |
| 3294 | else if (pd->current_pr == PR_SRC) |
| 3295 | pr = "source"; |
| 3296 | |
| 3297 | return snprintf(buf, PAGE_SIZE, "%s\n", pr); |
| 3298 | } |
| 3299 | static DEVICE_ATTR_RO(current_pr); |
| 3300 | |
| 3301 | static ssize_t initial_pr_show(struct device *dev, |
| 3302 | struct device_attribute *attr, char *buf) |
| 3303 | { |
| 3304 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3305 | const char *pr = "none"; |
| 3306 | |
| 3307 | if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SOURCE_DEFAULT) |
| 3308 | pr = "sink"; |
| 3309 | else if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SINK) |
| 3310 | pr = "source"; |
| 3311 | |
| 3312 | return snprintf(buf, PAGE_SIZE, "%s\n", pr); |
| 3313 | } |
| 3314 | static DEVICE_ATTR_RO(initial_pr); |
| 3315 | |
| 3316 | static ssize_t current_dr_show(struct device *dev, |
| 3317 | struct device_attribute *attr, char *buf) |
| 3318 | { |
| 3319 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3320 | const char *dr = "none"; |
| 3321 | |
| 3322 | if (pd->current_dr == DR_UFP) |
| 3323 | dr = "ufp"; |
| 3324 | else if (pd->current_dr == DR_DFP) |
| 3325 | dr = "dfp"; |
| 3326 | |
| 3327 | return snprintf(buf, PAGE_SIZE, "%s\n", dr); |
| 3328 | } |
| 3329 | static DEVICE_ATTR_RO(current_dr); |
| 3330 | |
| 3331 | static ssize_t initial_dr_show(struct device *dev, |
| 3332 | struct device_attribute *attr, char *buf) |
| 3333 | { |
| 3334 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3335 | const char *dr = "none"; |
| 3336 | |
| 3337 | if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SOURCE_DEFAULT) |
| 3338 | dr = "ufp"; |
| 3339 | else if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SINK) |
| 3340 | dr = "dfp"; |
| 3341 | |
| 3342 | return snprintf(buf, PAGE_SIZE, "%s\n", dr); |
| 3343 | } |
| 3344 | static DEVICE_ATTR_RO(initial_dr); |
| 3345 | |
| 3346 | static ssize_t src_cap_id_show(struct device *dev, |
| 3347 | struct device_attribute *attr, char *buf) |
| 3348 | { |
| 3349 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3350 | |
| 3351 | return snprintf(buf, PAGE_SIZE, "%d\n", pd->src_cap_id); |
| 3352 | } |
| 3353 | static DEVICE_ATTR_RO(src_cap_id); |
| 3354 | |
| 3355 | /* Dump received source PDOs in human-readable format */ |
| 3356 | static ssize_t pdo_h_show(struct device *dev, struct device_attribute *attr, |
| 3357 | char *buf) |
| 3358 | { |
| 3359 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3360 | int i; |
| 3361 | ssize_t cnt = 0; |
| 3362 | |
| 3363 | for (i = 0; i < ARRAY_SIZE(pd->received_pdos); i++) { |
| 3364 | u32 pdo = pd->received_pdos[i]; |
| 3365 | |
| 3366 | if (pdo == 0) |
| 3367 | break; |
| 3368 | |
| 3369 | cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt, "PDO %d\n", i + 1); |
| 3370 | |
| 3371 | if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_FIXED) { |
| 3372 | cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt, |
| 3373 | "\tFixed supply\n" |
| 3374 | "\tDual-Role Power:%d\n" |
| 3375 | "\tUSB Suspend Supported:%d\n" |
| 3376 | "\tExternally Powered:%d\n" |
| 3377 | "\tUSB Communications Capable:%d\n" |
| 3378 | "\tData Role Swap:%d\n" |
| 3379 | "\tPeak Current:%d\n" |
| 3380 | "\tVoltage:%d (mV)\n" |
| 3381 | "\tMax Current:%d (mA)\n", |
| 3382 | PD_SRC_PDO_FIXED_PR_SWAP(pdo), |
| 3383 | PD_SRC_PDO_FIXED_USB_SUSP(pdo), |
| 3384 | PD_SRC_PDO_FIXED_EXT_POWERED(pdo), |
| 3385 | PD_SRC_PDO_FIXED_USB_COMM(pdo), |
| 3386 | PD_SRC_PDO_FIXED_DR_SWAP(pdo), |
| 3387 | PD_SRC_PDO_FIXED_PEAK_CURR(pdo), |
| 3388 | PD_SRC_PDO_FIXED_VOLTAGE(pdo) * 50, |
| 3389 | PD_SRC_PDO_FIXED_MAX_CURR(pdo) * 10); |
| 3390 | } else if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_BATTERY) { |
| 3391 | cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt, |
| 3392 | "\tBattery supply\n" |
| 3393 | "\tMax Voltage:%d (mV)\n" |
| 3394 | "\tMin Voltage:%d (mV)\n" |
| 3395 | "\tMax Power:%d (mW)\n", |
| 3396 | PD_SRC_PDO_VAR_BATT_MAX_VOLT(pdo) * 50, |
| 3397 | PD_SRC_PDO_VAR_BATT_MIN_VOLT(pdo) * 50, |
| 3398 | PD_SRC_PDO_VAR_BATT_MAX(pdo) * 250); |
| 3399 | } else if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_VARIABLE) { |
| 3400 | cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt, |
| 3401 | "\tVariable supply\n" |
| 3402 | "\tMax Voltage:%d (mV)\n" |
| 3403 | "\tMin Voltage:%d (mV)\n" |
| 3404 | "\tMax Current:%d (mA)\n", |
| 3405 | PD_SRC_PDO_VAR_BATT_MAX_VOLT(pdo) * 50, |
| 3406 | PD_SRC_PDO_VAR_BATT_MIN_VOLT(pdo) * 50, |
| 3407 | PD_SRC_PDO_VAR_BATT_MAX(pdo) * 10); |
| 3408 | } else if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_AUGMENTED) { |
| 3409 | cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt, |
| 3410 | "\tProgrammable Power supply\n" |
| 3411 | "\tMax Voltage:%d (mV)\n" |
| 3412 | "\tMin Voltage:%d (mV)\n" |
| 3413 | "\tMax Current:%d (mA)\n", |
| 3414 | PD_APDO_MAX_VOLT(pdo) * 100, |
| 3415 | PD_APDO_MIN_VOLT(pdo) * 100, |
| 3416 | PD_APDO_MAX_CURR(pdo) * 50); |
| 3417 | } else { |
| 3418 | cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt, |
| 3419 | "Invalid PDO\n"); |
| 3420 | } |
| 3421 | |
| 3422 | buf[cnt++] = '\n'; |
| 3423 | } |
| 3424 | |
| 3425 | return cnt; |
| 3426 | } |
| 3427 | static DEVICE_ATTR_RO(pdo_h); |
| 3428 | |
| 3429 | static ssize_t pdo_n_show(struct device *dev, struct device_attribute *attr, |
| 3430 | char *buf); |
| 3431 | |
| 3432 | #define PDO_ATTR(n) { \ |
| 3433 | .attr = { .name = __stringify(pdo##n), .mode = 0444 }, \ |
| 3434 | .show = pdo_n_show, \ |
| 3435 | } |
| 3436 | static struct device_attribute dev_attr_pdos[] = { |
| 3437 | PDO_ATTR(1), |
| 3438 | PDO_ATTR(2), |
| 3439 | PDO_ATTR(3), |
| 3440 | PDO_ATTR(4), |
| 3441 | PDO_ATTR(5), |
| 3442 | PDO_ATTR(6), |
| 3443 | PDO_ATTR(7), |
| 3444 | }; |
| 3445 | |
| 3446 | static ssize_t pdo_n_show(struct device *dev, struct device_attribute *attr, |
| 3447 | char *buf) |
| 3448 | { |
| 3449 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3450 | int i; |
| 3451 | |
| 3452 | for (i = 0; i < ARRAY_SIZE(dev_attr_pdos); i++) |
| 3453 | if (attr == &dev_attr_pdos[i]) |
| 3454 | /* dump the PDO as a hex string */ |
| 3455 | return snprintf(buf, PAGE_SIZE, "%08x\n", |
| 3456 | pd->received_pdos[i]); |
| 3457 | |
| 3458 | usbpd_err(&pd->dev, "Invalid PDO index\n"); |
| 3459 | return -EINVAL; |
| 3460 | } |
| 3461 | |
| 3462 | static ssize_t select_pdo_store(struct device *dev, |
| 3463 | struct device_attribute *attr, const char *buf, size_t size) |
| 3464 | { |
| 3465 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3466 | int src_cap_id; |
| 3467 | int pdo, uv = 0, ua = 0; |
| 3468 | int ret; |
| 3469 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3470 | mutex_lock(&pd->swap_lock); |
| 3471 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3472 | /* Only allowed if we are already in explicit sink contract */ |
| 3473 | if (pd->current_state != PE_SNK_READY || !is_sink_tx_ok(pd)) { |
| 3474 | usbpd_err(&pd->dev, "select_pdo: Cannot select new PDO yet\n"); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3475 | ret = -EBUSY; |
| 3476 | goto out; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | ret = sscanf(buf, "%d %d %d %d", &src_cap_id, &pdo, &uv, &ua); |
| 3480 | if (ret != 2 && ret != 4) { |
| 3481 | usbpd_err(&pd->dev, "select_pdo: Must specify <src cap id> <PDO> [<uV> <uA>]\n"); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3482 | ret = -EINVAL; |
| 3483 | goto out; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | if (src_cap_id != pd->src_cap_id) { |
| 3487 | usbpd_err(&pd->dev, "select_pdo: src_cap_id mismatch. Requested:%d, current:%d\n", |
| 3488 | src_cap_id, pd->src_cap_id); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3489 | ret = -EINVAL; |
| 3490 | goto out; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | if (pdo < 1 || pdo > 7) { |
| 3494 | usbpd_err(&pd->dev, "select_pdo: invalid PDO:%d\n", pdo); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3495 | ret = -EINVAL; |
| 3496 | goto out; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | ret = pd_select_pdo(pd, pdo, uv, ua); |
| 3500 | if (ret) |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3501 | goto out; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3502 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3503 | reinit_completion(&pd->is_ready); |
| 3504 | pd->send_request = true; |
| 3505 | kick_sm(pd, 0); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3506 | |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3507 | /* wait for operation to complete */ |
| 3508 | if (!wait_for_completion_timeout(&pd->is_ready, |
| 3509 | msecs_to_jiffies(1000))) { |
| 3510 | usbpd_err(&pd->dev, "select_pdo: request timed out\n"); |
| 3511 | ret = -ETIMEDOUT; |
| 3512 | goto out; |
| 3513 | } |
| 3514 | |
| 3515 | /* determine if request was accepted/rejected */ |
| 3516 | if (pd->selected_pdo != pd->requested_pdo || |
| 3517 | pd->current_voltage != pd->requested_voltage) { |
| 3518 | usbpd_err(&pd->dev, "select_pdo: request rejected\n"); |
| 3519 | ret = -EINVAL; |
| 3520 | } |
| 3521 | |
| 3522 | out: |
| 3523 | pd->send_request = false; |
| 3524 | mutex_unlock(&pd->swap_lock); |
| 3525 | return ret ? ret : size; |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | static ssize_t select_pdo_show(struct device *dev, |
| 3529 | struct device_attribute *attr, char *buf) |
| 3530 | { |
| 3531 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3532 | |
| 3533 | return snprintf(buf, PAGE_SIZE, "%d\n", pd->selected_pdo); |
| 3534 | } |
| 3535 | static DEVICE_ATTR_RW(select_pdo); |
| 3536 | |
| 3537 | static ssize_t rdo_show(struct device *dev, struct device_attribute *attr, |
| 3538 | char *buf) |
| 3539 | { |
| 3540 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3541 | |
| 3542 | /* dump the RDO as a hex string */ |
| 3543 | return snprintf(buf, PAGE_SIZE, "%08x\n", pd->rdo); |
| 3544 | } |
| 3545 | static DEVICE_ATTR_RO(rdo); |
| 3546 | |
| 3547 | static ssize_t rdo_h_show(struct device *dev, struct device_attribute *attr, |
| 3548 | char *buf) |
| 3549 | { |
| 3550 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3551 | int pos = PD_RDO_OBJ_POS(pd->rdo); |
| 3552 | int type = PD_SRC_PDO_TYPE(pd->received_pdos[pos]); |
| 3553 | int len; |
| 3554 | |
| 3555 | len = scnprintf(buf, PAGE_SIZE, "Request Data Object\n" |
| 3556 | "\tObj Pos:%d\n" |
| 3557 | "\tGiveback:%d\n" |
| 3558 | "\tCapability Mismatch:%d\n" |
| 3559 | "\tUSB Communications Capable:%d\n" |
| 3560 | "\tNo USB Suspend:%d\n", |
| 3561 | PD_RDO_OBJ_POS(pd->rdo), |
| 3562 | PD_RDO_GIVEBACK(pd->rdo), |
| 3563 | PD_RDO_MISMATCH(pd->rdo), |
| 3564 | PD_RDO_USB_COMM(pd->rdo), |
| 3565 | PD_RDO_NO_USB_SUSP(pd->rdo)); |
| 3566 | |
| 3567 | switch (type) { |
| 3568 | case PD_SRC_PDO_TYPE_FIXED: |
| 3569 | case PD_SRC_PDO_TYPE_VARIABLE: |
| 3570 | len += scnprintf(buf + len, PAGE_SIZE - len, |
| 3571 | "(Fixed/Variable)\n" |
| 3572 | "\tOperating Current:%d (mA)\n" |
| 3573 | "\t%s Current:%d (mA)\n", |
| 3574 | PD_RDO_FIXED_CURR(pd->rdo) * 10, |
| 3575 | PD_RDO_GIVEBACK(pd->rdo) ? "Min" : "Max", |
| 3576 | PD_RDO_FIXED_CURR_MINMAX(pd->rdo) * 10); |
| 3577 | break; |
| 3578 | |
| 3579 | case PD_SRC_PDO_TYPE_BATTERY: |
| 3580 | len += scnprintf(buf + len, PAGE_SIZE - len, |
| 3581 | "(Battery)\n" |
| 3582 | "\tOperating Power:%d (mW)\n" |
| 3583 | "\t%s Power:%d (mW)\n", |
| 3584 | PD_RDO_FIXED_CURR(pd->rdo) * 250, |
| 3585 | PD_RDO_GIVEBACK(pd->rdo) ? "Min" : "Max", |
| 3586 | PD_RDO_FIXED_CURR_MINMAX(pd->rdo) * 250); |
| 3587 | break; |
| 3588 | |
| 3589 | case PD_SRC_PDO_TYPE_AUGMENTED: |
| 3590 | len += scnprintf(buf + len, PAGE_SIZE - len, |
| 3591 | "(Programmable)\n" |
| 3592 | "\tOutput Voltage:%d (mV)\n" |
| 3593 | "\tOperating Current:%d (mA)\n", |
| 3594 | PD_RDO_PROG_VOLTAGE(pd->rdo) * 20, |
| 3595 | PD_RDO_PROG_CURR(pd->rdo) * 50); |
| 3596 | break; |
| 3597 | } |
| 3598 | |
| 3599 | return len; |
| 3600 | } |
| 3601 | static DEVICE_ATTR_RO(rdo_h); |
| 3602 | |
| 3603 | static ssize_t hard_reset_store(struct device *dev, |
| 3604 | struct device_attribute *attr, const char *buf, size_t size) |
| 3605 | { |
| 3606 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3607 | int val = 0; |
| 3608 | |
| 3609 | if (sscanf(buf, "%d\n", &val) != 1) |
| 3610 | return -EINVAL; |
| 3611 | |
| 3612 | if (val) |
| 3613 | usbpd_set_state(pd, pd->current_pr == PR_SRC ? |
| 3614 | PE_SRC_HARD_RESET : PE_SNK_HARD_RESET); |
| 3615 | |
| 3616 | return size; |
| 3617 | } |
| 3618 | static DEVICE_ATTR_WO(hard_reset); |
| 3619 | |
Hemant Kumar | 018b598 | 2017-08-09 14:14:37 -0700 | [diff] [blame] | 3620 | static int trigger_tx_msg(struct usbpd *pd, bool *msg_tx_flag) |
| 3621 | { |
| 3622 | int ret = 0; |
| 3623 | |
| 3624 | /* Only allowed if we are already in explicit sink contract */ |
| 3625 | if (pd->current_state != PE_SNK_READY || !is_sink_tx_ok(pd)) { |
| 3626 | usbpd_err(&pd->dev, "%s: Cannot send msg\n", __func__); |
| 3627 | ret = -EBUSY; |
| 3628 | goto out; |
| 3629 | } |
| 3630 | |
| 3631 | reinit_completion(&pd->is_ready); |
| 3632 | *msg_tx_flag = true; |
| 3633 | kick_sm(pd, 0); |
| 3634 | |
| 3635 | /* wait for operation to complete */ |
| 3636 | if (!wait_for_completion_timeout(&pd->is_ready, |
| 3637 | msecs_to_jiffies(1000))) { |
| 3638 | usbpd_err(&pd->dev, "%s: request timed out\n", __func__); |
| 3639 | ret = -ETIMEDOUT; |
| 3640 | } |
| 3641 | |
| 3642 | out: |
| 3643 | *msg_tx_flag = false; |
| 3644 | return ret; |
| 3645 | |
| 3646 | } |
| 3647 | |
| 3648 | static ssize_t get_src_cap_ext_show(struct device *dev, |
| 3649 | struct device_attribute *attr, char *buf) |
| 3650 | { |
| 3651 | int i, ret, len = 0; |
| 3652 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3653 | |
| 3654 | if (pd->spec_rev == USBPD_REV_20) |
| 3655 | return -EINVAL; |
| 3656 | |
| 3657 | ret = trigger_tx_msg(pd, &pd->send_get_src_cap_ext); |
| 3658 | if (ret) |
| 3659 | return ret; |
| 3660 | |
| 3661 | for (i = 0; i < PD_SRC_CAP_EXT_DB_LEN; i++) |
| 3662 | len += snprintf(buf + len, PAGE_SIZE - len, "%d\n", |
| 3663 | pd->src_cap_ext_db[i]); |
| 3664 | return len; |
| 3665 | } |
| 3666 | static DEVICE_ATTR_RO(get_src_cap_ext); |
| 3667 | |
| 3668 | static ssize_t get_pps_status_show(struct device *dev, |
| 3669 | struct device_attribute *attr, char *buf) |
| 3670 | { |
| 3671 | int ret; |
| 3672 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3673 | |
| 3674 | if (pd->spec_rev == USBPD_REV_20) |
| 3675 | return -EINVAL; |
| 3676 | |
| 3677 | ret = trigger_tx_msg(pd, &pd->send_get_pps_status); |
| 3678 | if (ret) |
| 3679 | return ret; |
| 3680 | |
| 3681 | return snprintf(buf, PAGE_SIZE, "%d\n", pd->pps_status_db); |
| 3682 | } |
| 3683 | static DEVICE_ATTR_RO(get_pps_status); |
| 3684 | |
Hemant Kumar | a187594 | 2017-08-09 16:50:14 -0700 | [diff] [blame] | 3685 | static ssize_t rx_ado_show(struct device *dev, struct device_attribute *attr, |
| 3686 | char *buf) |
| 3687 | { |
| 3688 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3689 | |
| 3690 | /* dump the ADO as a hex string */ |
| 3691 | return snprintf(buf, PAGE_SIZE, "%08x\n", pd->received_ado); |
| 3692 | } |
| 3693 | static DEVICE_ATTR_RO(rx_ado); |
| 3694 | |
Hemant Kumar | 51ded97 | 2017-08-09 17:57:24 -0700 | [diff] [blame] | 3695 | static ssize_t get_battery_cap_store(struct device *dev, |
| 3696 | struct device_attribute *attr, const char *buf, size_t size) |
| 3697 | { |
| 3698 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3699 | int val, ret; |
| 3700 | |
| 3701 | if (pd->spec_rev == USBPD_REV_20 || sscanf(buf, "%d\n", &val) != 1) { |
| 3702 | pd->get_battery_cap_db = -EINVAL; |
| 3703 | return -EINVAL; |
| 3704 | } |
| 3705 | |
| 3706 | pd->get_battery_cap_db = val; |
| 3707 | |
| 3708 | ret = trigger_tx_msg(pd, &pd->send_get_battery_cap); |
| 3709 | |
| 3710 | return ret ? ret : size; |
| 3711 | } |
| 3712 | |
| 3713 | static ssize_t get_battery_cap_show(struct device *dev, |
| 3714 | struct device_attribute *attr, char *buf) |
| 3715 | { |
| 3716 | int i, len = 0; |
| 3717 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3718 | |
| 3719 | if (pd->get_battery_cap_db == -EINVAL) |
| 3720 | return -EINVAL; |
| 3721 | |
| 3722 | for (i = 0; i < PD_BATTERY_CAP_DB_LEN; i++) |
| 3723 | len += snprintf(buf + len, PAGE_SIZE - len, "%d\n", |
| 3724 | pd->battery_cap_db[i]); |
| 3725 | return len; |
| 3726 | } |
| 3727 | static DEVICE_ATTR_RW(get_battery_cap); |
| 3728 | |
| 3729 | static ssize_t get_battery_status_store(struct device *dev, |
| 3730 | struct device_attribute *attr, const char *buf, size_t size) |
| 3731 | { |
| 3732 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3733 | int val, ret; |
| 3734 | |
| 3735 | if (pd->spec_rev == USBPD_REV_20 || sscanf(buf, "%d\n", &val) != 1) { |
| 3736 | pd->get_battery_status_db = -EINVAL; |
| 3737 | return -EINVAL; |
| 3738 | } |
| 3739 | |
| 3740 | pd->get_battery_status_db = val; |
| 3741 | |
| 3742 | ret = trigger_tx_msg(pd, &pd->send_get_battery_status); |
| 3743 | |
| 3744 | return ret ? ret : size; |
| 3745 | } |
| 3746 | |
| 3747 | static ssize_t get_battery_status_show(struct device *dev, |
| 3748 | struct device_attribute *attr, char *buf) |
| 3749 | { |
| 3750 | struct usbpd *pd = dev_get_drvdata(dev); |
| 3751 | |
| 3752 | if (pd->get_battery_status_db == -EINVAL) |
| 3753 | return -EINVAL; |
| 3754 | |
| 3755 | return snprintf(buf, PAGE_SIZE, "%d\n", pd->battery_sts_dobj); |
| 3756 | } |
| 3757 | static DEVICE_ATTR_RW(get_battery_status); |
| 3758 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3759 | static struct attribute *usbpd_attrs[] = { |
| 3760 | &dev_attr_contract.attr, |
| 3761 | &dev_attr_initial_pr.attr, |
| 3762 | &dev_attr_current_pr.attr, |
| 3763 | &dev_attr_initial_dr.attr, |
| 3764 | &dev_attr_current_dr.attr, |
| 3765 | &dev_attr_src_cap_id.attr, |
| 3766 | &dev_attr_pdo_h.attr, |
| 3767 | &dev_attr_pdos[0].attr, |
| 3768 | &dev_attr_pdos[1].attr, |
| 3769 | &dev_attr_pdos[2].attr, |
| 3770 | &dev_attr_pdos[3].attr, |
| 3771 | &dev_attr_pdos[4].attr, |
| 3772 | &dev_attr_pdos[5].attr, |
| 3773 | &dev_attr_pdos[6].attr, |
| 3774 | &dev_attr_select_pdo.attr, |
| 3775 | &dev_attr_rdo.attr, |
| 3776 | &dev_attr_rdo_h.attr, |
| 3777 | &dev_attr_hard_reset.attr, |
Hemant Kumar | 018b598 | 2017-08-09 14:14:37 -0700 | [diff] [blame] | 3778 | &dev_attr_get_src_cap_ext.attr, |
| 3779 | &dev_attr_get_pps_status.attr, |
Hemant Kumar | a187594 | 2017-08-09 16:50:14 -0700 | [diff] [blame] | 3780 | &dev_attr_rx_ado.attr, |
Hemant Kumar | 51ded97 | 2017-08-09 17:57:24 -0700 | [diff] [blame] | 3781 | &dev_attr_get_battery_cap.attr, |
| 3782 | &dev_attr_get_battery_status.attr, |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3783 | NULL, |
| 3784 | }; |
| 3785 | ATTRIBUTE_GROUPS(usbpd); |
| 3786 | |
| 3787 | static struct class usbpd_class = { |
| 3788 | .name = "usbpd", |
| 3789 | .owner = THIS_MODULE, |
| 3790 | .dev_uevent = usbpd_uevent, |
| 3791 | .dev_groups = usbpd_groups, |
| 3792 | }; |
| 3793 | |
| 3794 | static int match_usbpd_device(struct device *dev, const void *data) |
| 3795 | { |
| 3796 | return dev->parent == data; |
| 3797 | } |
| 3798 | |
| 3799 | static void devm_usbpd_put(struct device *dev, void *res) |
| 3800 | { |
| 3801 | struct usbpd **ppd = res; |
| 3802 | |
| 3803 | put_device(&(*ppd)->dev); |
| 3804 | } |
| 3805 | |
| 3806 | struct usbpd *devm_usbpd_get_by_phandle(struct device *dev, const char *phandle) |
| 3807 | { |
| 3808 | struct usbpd **ptr, *pd = NULL; |
| 3809 | struct device_node *pd_np; |
| 3810 | struct platform_device *pdev; |
| 3811 | struct device *pd_dev; |
| 3812 | |
| 3813 | if (!usbpd_class.p) /* usbpd_init() not yet called */ |
| 3814 | return ERR_PTR(-EAGAIN); |
| 3815 | |
| 3816 | if (!dev->of_node) |
| 3817 | return ERR_PTR(-EINVAL); |
| 3818 | |
| 3819 | pd_np = of_parse_phandle(dev->of_node, phandle, 0); |
| 3820 | if (!pd_np) |
| 3821 | return ERR_PTR(-ENXIO); |
| 3822 | |
| 3823 | pdev = of_find_device_by_node(pd_np); |
| 3824 | if (!pdev) |
| 3825 | return ERR_PTR(-ENODEV); |
| 3826 | |
| 3827 | pd_dev = class_find_device(&usbpd_class, NULL, &pdev->dev, |
| 3828 | match_usbpd_device); |
| 3829 | if (!pd_dev) { |
| 3830 | platform_device_put(pdev); |
| 3831 | /* device was found but maybe hadn't probed yet, so defer */ |
| 3832 | return ERR_PTR(-EPROBE_DEFER); |
| 3833 | } |
| 3834 | |
| 3835 | ptr = devres_alloc(devm_usbpd_put, sizeof(*ptr), GFP_KERNEL); |
| 3836 | if (!ptr) { |
| 3837 | put_device(pd_dev); |
| 3838 | platform_device_put(pdev); |
| 3839 | return ERR_PTR(-ENOMEM); |
| 3840 | } |
| 3841 | |
| 3842 | pd = dev_get_drvdata(pd_dev); |
| 3843 | if (!pd) |
| 3844 | return ERR_PTR(-EPROBE_DEFER); |
| 3845 | |
| 3846 | *ptr = pd; |
| 3847 | devres_add(dev, ptr); |
| 3848 | |
| 3849 | return pd; |
| 3850 | } |
| 3851 | EXPORT_SYMBOL(devm_usbpd_get_by_phandle); |
| 3852 | |
| 3853 | static int num_pd_instances; |
| 3854 | |
| 3855 | /** |
| 3856 | * usbpd_create - Create a new instance of USB PD protocol/policy engine |
| 3857 | * @parent - parent device to associate with |
| 3858 | * |
| 3859 | * This creates a new usbpd class device which manages the state of a |
| 3860 | * USB PD-capable port. The parent device that is passed in should be |
| 3861 | * associated with the physical device port, e.g. a PD PHY. |
| 3862 | * |
| 3863 | * Return: struct usbpd pointer, or an ERR_PTR value |
| 3864 | */ |
| 3865 | struct usbpd *usbpd_create(struct device *parent) |
| 3866 | { |
| 3867 | int ret; |
| 3868 | struct usbpd *pd; |
| 3869 | |
| 3870 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 3871 | if (!pd) |
| 3872 | return ERR_PTR(-ENOMEM); |
| 3873 | |
| 3874 | device_initialize(&pd->dev); |
| 3875 | pd->dev.class = &usbpd_class; |
| 3876 | pd->dev.parent = parent; |
| 3877 | dev_set_drvdata(&pd->dev, pd); |
| 3878 | |
| 3879 | ret = dev_set_name(&pd->dev, "usbpd%d", num_pd_instances++); |
| 3880 | if (ret) |
| 3881 | goto free_pd; |
| 3882 | |
| 3883 | ret = device_init_wakeup(&pd->dev, true); |
| 3884 | if (ret) |
| 3885 | goto free_pd; |
| 3886 | |
| 3887 | ret = device_add(&pd->dev); |
| 3888 | if (ret) |
| 3889 | goto free_pd; |
| 3890 | |
Hemant Kumar | 86bd10f | 2017-05-24 12:25:15 -0700 | [diff] [blame] | 3891 | pd->wq = alloc_ordered_workqueue("usbpd_wq", WQ_FREEZABLE | WQ_HIGHPRI); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3892 | if (!pd->wq) { |
| 3893 | ret = -ENOMEM; |
| 3894 | goto del_pd; |
| 3895 | } |
| 3896 | INIT_WORK(&pd->sm_work, usbpd_sm); |
| 3897 | hrtimer_init(&pd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 3898 | pd->timer.function = pd_timeout; |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 3899 | mutex_init(&pd->swap_lock); |
Mayank Rana | 8344320 | 2017-08-31 15:38:03 -0700 | [diff] [blame] | 3900 | mutex_init(&pd->svid_handler_lock); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3901 | |
| 3902 | pd->usb_psy = power_supply_get_by_name("usb"); |
| 3903 | if (!pd->usb_psy) { |
| 3904 | usbpd_dbg(&pd->dev, "Could not get USB power_supply, deferring probe\n"); |
| 3905 | ret = -EPROBE_DEFER; |
| 3906 | goto destroy_wq; |
| 3907 | } |
| 3908 | |
| 3909 | /* |
| 3910 | * associate extcon with the parent dev as it could have a DT |
| 3911 | * node which will be useful for extcon_get_edev_by_phandle() |
| 3912 | */ |
| 3913 | pd->extcon = devm_extcon_dev_allocate(parent, usbpd_extcon_cable); |
| 3914 | if (IS_ERR(pd->extcon)) { |
| 3915 | usbpd_err(&pd->dev, "failed to allocate extcon device\n"); |
| 3916 | ret = PTR_ERR(pd->extcon); |
| 3917 | goto put_psy; |
| 3918 | } |
| 3919 | |
| 3920 | pd->extcon->mutually_exclusive = usbpd_extcon_exclusive; |
| 3921 | ret = devm_extcon_dev_register(parent, pd->extcon); |
| 3922 | if (ret) { |
| 3923 | usbpd_err(&pd->dev, "failed to register extcon device\n"); |
| 3924 | goto put_psy; |
| 3925 | } |
| 3926 | |
Jack Pham | 4e9dff7 | 2017-04-04 18:05:53 -0700 | [diff] [blame] | 3927 | /* Support reporting polarity and speed via properties */ |
| 3928 | extcon_set_property_capability(pd->extcon, EXTCON_USB, |
| 3929 | EXTCON_PROP_USB_TYPEC_POLARITY); |
| 3930 | extcon_set_property_capability(pd->extcon, EXTCON_USB, |
| 3931 | EXTCON_PROP_USB_SS); |
| 3932 | extcon_set_property_capability(pd->extcon, EXTCON_USB_HOST, |
| 3933 | EXTCON_PROP_USB_TYPEC_POLARITY); |
| 3934 | extcon_set_property_capability(pd->extcon, EXTCON_USB_HOST, |
| 3935 | EXTCON_PROP_USB_SS); |
| 3936 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3937 | pd->vbus = devm_regulator_get(parent, "vbus"); |
| 3938 | if (IS_ERR(pd->vbus)) { |
| 3939 | ret = PTR_ERR(pd->vbus); |
| 3940 | goto put_psy; |
| 3941 | } |
| 3942 | |
| 3943 | pd->vconn = devm_regulator_get(parent, "vconn"); |
| 3944 | if (IS_ERR(pd->vconn)) { |
| 3945 | ret = PTR_ERR(pd->vconn); |
| 3946 | goto put_psy; |
| 3947 | } |
| 3948 | |
| 3949 | pd->vconn_is_external = device_property_present(parent, |
| 3950 | "qcom,vconn-uses-external-source"); |
| 3951 | |
Jack Pham | ee1f905 | 2017-01-26 12:27:07 -0800 | [diff] [blame] | 3952 | pd->num_sink_caps = device_property_read_u32_array(parent, |
| 3953 | "qcom,default-sink-caps", NULL, 0); |
Vijayavardhan Vennapusa | 248d723 | 2017-02-14 15:32:07 +0530 | [diff] [blame] | 3954 | if (pd->num_sink_caps > 0) { |
Jack Pham | ee1f905 | 2017-01-26 12:27:07 -0800 | [diff] [blame] | 3955 | int i; |
| 3956 | u32 sink_caps[14]; |
| 3957 | |
| 3958 | if (pd->num_sink_caps % 2 || pd->num_sink_caps > 14) { |
| 3959 | ret = -EINVAL; |
| 3960 | usbpd_err(&pd->dev, "default-sink-caps must be be specified as voltage/current, max 7 pairs\n"); |
| 3961 | goto put_psy; |
| 3962 | } |
| 3963 | |
| 3964 | ret = device_property_read_u32_array(parent, |
| 3965 | "qcom,default-sink-caps", sink_caps, |
| 3966 | pd->num_sink_caps); |
| 3967 | if (ret) { |
| 3968 | usbpd_err(&pd->dev, "Error reading default-sink-caps\n"); |
| 3969 | goto put_psy; |
| 3970 | } |
| 3971 | |
| 3972 | pd->num_sink_caps /= 2; |
| 3973 | |
| 3974 | for (i = 0; i < pd->num_sink_caps; i++) { |
| 3975 | int v = sink_caps[i * 2] / 50; |
| 3976 | int c = sink_caps[i * 2 + 1] / 10; |
| 3977 | |
| 3978 | pd->sink_caps[i] = |
| 3979 | PD_SNK_PDO_FIXED(0, 0, 0, 0, 0, v, c); |
| 3980 | } |
| 3981 | |
| 3982 | /* First PDO includes additional capabilities */ |
| 3983 | pd->sink_caps[0] |= PD_SNK_PDO_FIXED(1, 0, 0, 1, 1, 0, 0); |
| 3984 | } else { |
| 3985 | memcpy(pd->sink_caps, default_snk_caps, |
| 3986 | sizeof(default_snk_caps)); |
| 3987 | pd->num_sink_caps = ARRAY_SIZE(default_snk_caps); |
| 3988 | } |
| 3989 | |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 3990 | /* |
| 3991 | * Register the Android dual-role class (/sys/class/dual_role_usb/). |
| 3992 | * The first instance should be named "otg_default" as that's what |
| 3993 | * Android expects. |
| 3994 | * Note this is different than the /sys/class/usbpd/ created above. |
| 3995 | */ |
| 3996 | pd->dr_desc.name = (num_pd_instances == 1) ? |
| 3997 | "otg_default" : dev_name(&pd->dev); |
| 3998 | pd->dr_desc.supported_modes = DUAL_ROLE_SUPPORTED_MODES_DFP_AND_UFP; |
| 3999 | pd->dr_desc.properties = usbpd_dr_properties; |
| 4000 | pd->dr_desc.num_properties = ARRAY_SIZE(usbpd_dr_properties); |
| 4001 | pd->dr_desc.get_property = usbpd_dr_get_property; |
| 4002 | pd->dr_desc.set_property = usbpd_dr_set_property; |
| 4003 | pd->dr_desc.property_is_writeable = usbpd_dr_prop_writeable; |
| 4004 | |
| 4005 | pd->dual_role = devm_dual_role_instance_register(&pd->dev, |
| 4006 | &pd->dr_desc); |
| 4007 | if (IS_ERR(pd->dual_role)) { |
| 4008 | usbpd_err(&pd->dev, "could not register dual_role instance\n"); |
| 4009 | goto put_psy; |
| 4010 | } else { |
| 4011 | pd->dual_role->drv_data = pd; |
| 4012 | } |
| 4013 | |
| 4014 | pd->current_pr = PR_NONE; |
| 4015 | pd->current_dr = DR_NONE; |
| 4016 | list_add_tail(&pd->instance, &_usbpd); |
| 4017 | |
| 4018 | spin_lock_init(&pd->rx_lock); |
| 4019 | INIT_LIST_HEAD(&pd->rx_q); |
| 4020 | INIT_LIST_HEAD(&pd->svid_handlers); |
Jack Pham | af7d384 | 2017-01-26 13:28:19 -0800 | [diff] [blame] | 4021 | init_completion(&pd->is_ready); |
Jack Pham | f3c1bd3 | 2017-08-02 18:32:23 -0700 | [diff] [blame] | 4022 | init_completion(&pd->tx_chunk_request); |
Jack Pham | f4baeb1 | 2017-02-03 19:01:48 -0800 | [diff] [blame] | 4023 | |
| 4024 | pd->psy_nb.notifier_call = psy_changed; |
| 4025 | ret = power_supply_reg_notifier(&pd->psy_nb); |
| 4026 | if (ret) |
| 4027 | goto del_inst; |
| 4028 | |
| 4029 | /* force read initial power_supply values */ |
| 4030 | psy_changed(&pd->psy_nb, PSY_EVENT_PROP_CHANGED, pd->usb_psy); |
| 4031 | |
| 4032 | return pd; |
| 4033 | |
| 4034 | del_inst: |
| 4035 | list_del(&pd->instance); |
| 4036 | put_psy: |
| 4037 | power_supply_put(pd->usb_psy); |
| 4038 | destroy_wq: |
| 4039 | destroy_workqueue(pd->wq); |
| 4040 | del_pd: |
| 4041 | device_del(&pd->dev); |
| 4042 | free_pd: |
| 4043 | num_pd_instances--; |
| 4044 | kfree(pd); |
| 4045 | return ERR_PTR(ret); |
| 4046 | } |
| 4047 | EXPORT_SYMBOL(usbpd_create); |
| 4048 | |
| 4049 | /** |
| 4050 | * usbpd_destroy - Removes and frees a usbpd instance |
| 4051 | * @pd: the instance to destroy |
| 4052 | */ |
| 4053 | void usbpd_destroy(struct usbpd *pd) |
| 4054 | { |
| 4055 | if (!pd) |
| 4056 | return; |
| 4057 | |
| 4058 | list_del(&pd->instance); |
| 4059 | power_supply_unreg_notifier(&pd->psy_nb); |
| 4060 | power_supply_put(pd->usb_psy); |
| 4061 | destroy_workqueue(pd->wq); |
| 4062 | device_del(&pd->dev); |
| 4063 | kfree(pd); |
| 4064 | } |
| 4065 | EXPORT_SYMBOL(usbpd_destroy); |
| 4066 | |
| 4067 | static int __init usbpd_init(void) |
| 4068 | { |
| 4069 | usbpd_ipc_log = ipc_log_context_create(NUM_LOG_PAGES, "usb_pd", 0); |
| 4070 | return class_register(&usbpd_class); |
| 4071 | } |
| 4072 | module_init(usbpd_init); |
| 4073 | |
| 4074 | static void __exit usbpd_exit(void) |
| 4075 | { |
| 4076 | class_unregister(&usbpd_class); |
| 4077 | } |
| 4078 | module_exit(usbpd_exit); |
| 4079 | |
| 4080 | MODULE_DESCRIPTION("USB Power Delivery Policy Engine"); |
| 4081 | MODULE_LICENSE("GPL v2"); |