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