1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright 2015-2017 Google, Inc 4 * 5 * USB Power Delivery protocol stack. 6 */ 7 8 #include <linux/completion.h> 9 #include <linux/debugfs.h> 10 #include <linux/device.h> 11 #include <linux/hrtimer.h> 12 #include <linux/jiffies.h> 13 #include <linux/kernel.h> 14 #include <linux/kthread.h> 15 #include <linux/math64.h> 16 #include <linux/minmax.h> 17 #include <linux/module.h> 18 #include <linux/mutex.h> 19 #include <linux/power_supply.h> 20 #include <linux/proc_fs.h> 21 #include <linux/property.h> 22 #include <linux/sched/clock.h> 23 #include <linux/seq_file.h> 24 #include <linux/slab.h> 25 #include <linux/spinlock.h> 26 #include <linux/string_choices.h> 27 #include <linux/usb.h> 28 #include <linux/usb/pd.h> 29 #include <linux/usb/pd_ado.h> 30 #include <linux/usb/pd_bdo.h> 31 #include <linux/usb/pd_ext_sdb.h> 32 #include <linux/usb/pd_vdo.h> 33 #include <linux/usb/role.h> 34 #include <linux/usb/tcpm.h> 35 #include <linux/usb/typec_altmode.h> 36 37 #include <uapi/linux/sched/types.h> 38 39 #define FOREACH_STATE(S) \ 40 S(INVALID_STATE), \ 41 S(TOGGLING), \ 42 S(CHECK_CONTAMINANT), \ 43 S(SRC_UNATTACHED), \ 44 S(SRC_ATTACH_WAIT), \ 45 S(SRC_ATTACHED), \ 46 S(SRC_STARTUP), \ 47 S(SRC_SEND_CAPABILITIES), \ 48 S(SRC_SEND_CAPABILITIES_TIMEOUT), \ 49 S(SRC_NEGOTIATE_CAPABILITIES), \ 50 S(SRC_TRANSITION_SUPPLY), \ 51 S(SRC_READY), \ 52 S(SRC_WAIT_NEW_CAPABILITIES), \ 53 \ 54 S(SNK_UNATTACHED), \ 55 S(SNK_ATTACH_WAIT), \ 56 S(SNK_DEBOUNCED), \ 57 S(SNK_ATTACHED), \ 58 S(SNK_STARTUP), \ 59 S(SNK_DISCOVERY), \ 60 S(SNK_DISCOVERY_DEBOUNCE), \ 61 S(SNK_DISCOVERY_DEBOUNCE_DONE), \ 62 S(SNK_WAIT_CAPABILITIES), \ 63 S(SNK_WAIT_CAPABILITIES_TIMEOUT), \ 64 S(SNK_NEGOTIATE_CAPABILITIES), \ 65 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \ 66 S(SNK_NEGOTIATE_SPR_AVS_CAPABILITIES), \ 67 S(SNK_TRANSITION_SINK), \ 68 S(SNK_TRANSITION_SINK_VBUS), \ 69 S(SNK_READY), \ 70 \ 71 S(ACC_UNATTACHED), \ 72 S(DEBUG_ACC_ATTACHED), \ 73 S(DEBUG_ACC_DEBOUNCE), \ 74 S(AUDIO_ACC_ATTACHED), \ 75 S(AUDIO_ACC_DEBOUNCE), \ 76 \ 77 S(HARD_RESET_SEND), \ 78 S(HARD_RESET_START), \ 79 S(SRC_HARD_RESET_VBUS_OFF), \ 80 S(SRC_HARD_RESET_VBUS_ON), \ 81 S(SNK_HARD_RESET_SINK_OFF), \ 82 S(SNK_HARD_RESET_WAIT_VBUS), \ 83 S(SNK_HARD_RESET_SINK_ON), \ 84 \ 85 S(SOFT_RESET), \ 86 S(SRC_SOFT_RESET_WAIT_SNK_TX), \ 87 S(SNK_SOFT_RESET), \ 88 S(SOFT_RESET_SEND), \ 89 \ 90 S(DR_SWAP_ACCEPT), \ 91 S(DR_SWAP_SEND), \ 92 S(DR_SWAP_SEND_TIMEOUT), \ 93 S(DR_SWAP_CANCEL), \ 94 S(DR_SWAP_CHANGE_DR), \ 95 \ 96 S(PR_SWAP_ACCEPT), \ 97 S(PR_SWAP_SEND), \ 98 S(PR_SWAP_SEND_TIMEOUT), \ 99 S(PR_SWAP_CANCEL), \ 100 S(PR_SWAP_START), \ 101 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \ 102 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \ 103 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \ 104 S(PR_SWAP_SRC_SNK_SINK_ON), \ 105 S(PR_SWAP_SNK_SRC_SINK_OFF), \ 106 S(PR_SWAP_SNK_SRC_SOURCE_ON), \ 107 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \ 108 \ 109 S(VCONN_SWAP_ACCEPT), \ 110 S(VCONN_SWAP_SEND), \ 111 S(VCONN_SWAP_SEND_TIMEOUT), \ 112 S(VCONN_SWAP_CANCEL), \ 113 S(VCONN_SWAP_START), \ 114 S(VCONN_SWAP_WAIT_FOR_VCONN), \ 115 S(VCONN_SWAP_TURN_ON_VCONN), \ 116 S(VCONN_SWAP_TURN_OFF_VCONN), \ 117 S(VCONN_SWAP_SEND_SOFT_RESET), \ 118 \ 119 S(FR_SWAP_SEND), \ 120 S(FR_SWAP_SEND_TIMEOUT), \ 121 S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF), \ 122 S(FR_SWAP_SNK_SRC_NEW_SINK_READY), \ 123 S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED), \ 124 S(FR_SWAP_CANCEL), \ 125 \ 126 S(SNK_TRY), \ 127 S(SNK_TRY_WAIT), \ 128 S(SNK_TRY_WAIT_DEBOUNCE), \ 129 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \ 130 S(SRC_TRYWAIT), \ 131 S(SRC_TRYWAIT_DEBOUNCE), \ 132 S(SRC_TRYWAIT_UNATTACHED), \ 133 \ 134 S(SRC_TRY), \ 135 S(SRC_TRY_WAIT), \ 136 S(SRC_TRY_DEBOUNCE), \ 137 S(SNK_TRYWAIT), \ 138 S(SNK_TRYWAIT_DEBOUNCE), \ 139 S(SNK_TRYWAIT_VBUS), \ 140 S(BIST_RX), \ 141 \ 142 S(GET_STATUS_SEND), \ 143 S(GET_STATUS_SEND_TIMEOUT), \ 144 S(GET_PPS_STATUS_SEND), \ 145 S(GET_PPS_STATUS_SEND_TIMEOUT), \ 146 \ 147 S(GET_SINK_CAP), \ 148 S(GET_SINK_CAP_TIMEOUT), \ 149 \ 150 S(ERROR_RECOVERY), \ 151 S(PORT_RESET), \ 152 S(PORT_RESET_WAIT_OFF), \ 153 \ 154 S(AMS_START), \ 155 S(CHUNK_NOT_SUPP), \ 156 \ 157 S(SRC_VDM_IDENTITY_REQUEST) 158 159 #define FOREACH_AMS(S) \ 160 S(NONE_AMS), \ 161 S(POWER_NEGOTIATION), \ 162 S(GOTOMIN), \ 163 S(SOFT_RESET_AMS), \ 164 S(HARD_RESET), \ 165 S(CABLE_RESET), \ 166 S(GET_SOURCE_CAPABILITIES), \ 167 S(GET_SINK_CAPABILITIES), \ 168 S(POWER_ROLE_SWAP), \ 169 S(FAST_ROLE_SWAP), \ 170 S(DATA_ROLE_SWAP), \ 171 S(VCONN_SWAP), \ 172 S(SOURCE_ALERT), \ 173 S(GETTING_SOURCE_EXTENDED_CAPABILITIES),\ 174 S(GETTING_SOURCE_SINK_STATUS), \ 175 S(GETTING_BATTERY_CAPABILITIES), \ 176 S(GETTING_BATTERY_STATUS), \ 177 S(GETTING_MANUFACTURER_INFORMATION), \ 178 S(SECURITY), \ 179 S(FIRMWARE_UPDATE), \ 180 S(DISCOVER_IDENTITY), \ 181 S(SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY), \ 182 S(DISCOVER_SVIDS), \ 183 S(DISCOVER_MODES), \ 184 S(DFP_TO_UFP_ENTER_MODE), \ 185 S(DFP_TO_UFP_EXIT_MODE), \ 186 S(DFP_TO_CABLE_PLUG_ENTER_MODE), \ 187 S(DFP_TO_CABLE_PLUG_EXIT_MODE), \ 188 S(ATTENTION), \ 189 S(BIST), \ 190 S(UNSTRUCTURED_VDMS), \ 191 S(STRUCTURED_VDMS), \ 192 S(COUNTRY_INFO), \ 193 S(COUNTRY_CODES), \ 194 S(REVISION_INFORMATION), \ 195 S(GETTING_SINK_EXTENDED_CAPABILITIES) 196 197 #define GENERATE_ENUM(e) e 198 #define GENERATE_STRING(s) #s 199 200 enum tcpm_state { 201 FOREACH_STATE(GENERATE_ENUM) 202 }; 203 204 static const char * const tcpm_states[] = { 205 FOREACH_STATE(GENERATE_STRING) 206 }; 207 208 enum tcpm_ams { 209 FOREACH_AMS(GENERATE_ENUM) 210 }; 211 212 static const char * const tcpm_ams_str[] = { 213 FOREACH_AMS(GENERATE_STRING) 214 }; 215 216 #define FOREACH_VDM_DISCOVERY(S) \ 217 S(VDM_DISCOVERY_UNKNOWN), \ 218 S(VDM_DISCOVERY_PARTNER_IDENT), \ 219 S(VDM_DISCOVERY_CABLE_IDENT), \ 220 S(VDM_DISCOVERY_PARTNER_SVIDS), \ 221 S(VDM_DISCOVERY_PARTNER_MODES), \ 222 S(VDM_DISCOVERY_CABLE_SVIDS), \ 223 S(VDM_DISCOVERY_CABLE_MODES), \ 224 S(VDM_DISCOVERY_COMPLETE) 225 226 enum vdm_discovery_states { 227 FOREACH_VDM_DISCOVERY(GENERATE_ENUM) 228 }; 229 230 static const char * const vdm_discovery_state_strings[] = { 231 FOREACH_VDM_DISCOVERY(GENERATE_STRING) 232 }; 233 234 enum vdm_states { 235 VDM_STATE_ERR_BUSY = -3, 236 VDM_STATE_ERR_SEND = -2, 237 VDM_STATE_ERR_TMOUT = -1, 238 VDM_STATE_DONE = 0, 239 /* Anything >0 represents an active state */ 240 VDM_STATE_READY = 1, 241 VDM_STATE_BUSY = 2, 242 VDM_STATE_WAIT_RSP_BUSY = 3, 243 VDM_STATE_SEND_MESSAGE = 4, 244 }; 245 246 enum pd_msg_request { 247 PD_MSG_NONE = 0, 248 PD_MSG_CTRL_REJECT, 249 PD_MSG_CTRL_WAIT, 250 PD_MSG_CTRL_NOT_SUPP, 251 PD_MSG_DATA_SINK_CAP, 252 PD_MSG_DATA_SOURCE_CAP, 253 PD_MSG_DATA_REV, 254 PD_MSG_EXT_SINK_CAP_EXT, 255 PD_MSG_DATA_BATT_STATUS, 256 PD_MSG_EXT_BATT_CAP, 257 }; 258 259 enum adev_actions { 260 ADEV_NONE = 0, 261 ADEV_NOTIFY_USB_AND_QUEUE_VDM, 262 ADEV_QUEUE_VDM, 263 ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL, 264 ADEV_ATTENTION, 265 }; 266 267 /* 268 * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap. 269 * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0, 270 * Version 1.2" 271 */ 272 enum frs_typec_current { 273 FRS_NOT_SUPPORTED, 274 FRS_DEFAULT_POWER, 275 FRS_5V_1P5A, 276 FRS_5V_3A, 277 }; 278 279 /* Events from low level driver */ 280 281 #define TCPM_CC_EVENT BIT(0) 282 #define TCPM_VBUS_EVENT BIT(1) 283 #define TCPM_RESET_EVENT BIT(2) 284 #define TCPM_FRS_EVENT BIT(3) 285 #define TCPM_SOURCING_VBUS BIT(4) 286 #define TCPM_PORT_CLEAN BIT(5) 287 #define TCPM_PORT_ERROR BIT(6) 288 289 #define LOG_BUFFER_ENTRIES 1024 290 #define LOG_BUFFER_ENTRY_SIZE 128 291 292 /* Alternate mode support */ 293 294 #define SVID_DISCOVERY_MAX 16 295 #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX) 296 297 #define GET_SINK_CAP_RETRY_MS 100 298 #define SEND_DISCOVERY_VDM_RETRY_MS 100 299 300 struct pd_mode_data { 301 int svid_index; /* current SVID index */ 302 int nsvids; 303 u16 svids[SVID_DISCOVERY_MAX]; 304 int altmodes; /* number of alternate modes */ 305 struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX]; 306 }; 307 308 /* 309 * @min_volt: Actual min voltage at the local port 310 * @req_min_volt: Requested min voltage to the port partner 311 * @max_volt: Actual max voltage at the local port 312 * @req_max_volt: Requested max voltage to the port partner 313 * @max_curr: Actual max current at the local port 314 * @req_max_curr: Requested max current of the port partner 315 * @req_out_volt: Requested output voltage to the port partner 316 * @req_op_curr: Requested operating current to the port partner 317 * @supported: Parter has at least one APDO hence supports PPS 318 * @active: PPS mode is active 319 */ 320 struct pd_pps_data { 321 u32 min_volt; 322 u32 req_min_volt; 323 u32 max_volt; 324 u32 req_max_volt; 325 u32 max_curr; 326 u32 req_max_curr; 327 u32 req_out_volt; 328 u32 req_op_curr; 329 bool supported; 330 bool active; 331 }; 332 333 enum spr_avs_status { 334 SPR_AVS_UNKNOWN, 335 SPR_AVS_NOT_SUPPORTED, 336 SPR_AVS_SUPPORTED 337 }; 338 339 static const char * const spr_avs_status_strings[] = { 340 [SPR_AVS_UNKNOWN] = "Unknown", 341 [SPR_AVS_SUPPORTED] = "Supported", 342 [SPR_AVS_NOT_SUPPORTED] = "Not Supported", 343 }; 344 345 /* 346 * Standard Power Range Adjustable Voltage Supply (SPR - AVS) data 347 * @max_current_ma_9v_to_15v: Max current for 9V to 15V range derived from 348 * source cap & sink cap 349 * @max_current_ma_15v_to_20v: Max current for 15V to 20V range derived from 350 * source cap & sink cap 351 * @req_op_curr_ma: Requested operating current to the port partner acting as source 352 * @req_out_volt_mv: Requested output voltage to the port partner acting as source 353 * @max_out_volt_mv: Max SPR voltage supported by the port and the port partner 354 * @max_current_ma; MAX SPR current supported by the port and the port partner 355 * @port_partner_src_status: SPR AVS status of port partner acting as source 356 * @port_partner_src_pdo_index: PDO index of SPR AVS cap of the port partner 357 * acting as source. Valid only when 358 * port_partner_src_status is SPR_AVS_SUPPORTED. 359 * @port_snk_status: SPR AVS status of the local port acting as sink. 360 * @port_snk_pdo_index: PDO index of SPR AVS cap of local port acting as sink 361 * @active: True when the local port acting as the sink has negotiated SPR AVS 362 * with the partner acting as source. 363 */ 364 struct pd_spr_avs_data { 365 u32 max_current_ma_9v_to_15v; 366 u32 max_current_ma_15v_to_20v; 367 u32 req_op_curr_ma; 368 u32 req_out_volt_mv; 369 u32 max_out_volt_mv; 370 u32 max_current_ma; 371 enum spr_avs_status port_partner_src_status; 372 unsigned int port_partner_src_pdo_index; 373 enum spr_avs_status port_snk_status; 374 unsigned int port_snk_pdo_index; 375 bool active; 376 }; 377 378 struct pd_data { 379 struct usb_power_delivery *pd; 380 struct usb_power_delivery_capabilities *source_cap; 381 struct usb_power_delivery_capabilities_desc source_desc; 382 struct usb_power_delivery_capabilities *sink_cap; 383 struct usb_power_delivery_capabilities_desc sink_desc; 384 unsigned int operating_snk_mw; 385 }; 386 387 #define PD_CAP_REV10 0x1 388 #define PD_CAP_REV20 0x2 389 #define PD_CAP_REV30 0x3 390 391 struct pd_revision_info { 392 u8 rev_major; 393 u8 rev_minor; 394 u8 ver_major; 395 u8 ver_minor; 396 }; 397 398 /* 399 * @sink_wait_cap_time: Deadline (in ms) for tTypeCSinkWaitCap timer 400 * @ps_src_wait_off_time: Deadline (in ms) for tPSSourceOff timer 401 * @cc_debounce_time: Deadline (in ms) for tCCDebounce timer 402 */ 403 struct pd_timings { 404 u32 sink_wait_cap_time; 405 u32 ps_src_off_time; 406 u32 cc_debounce_time; 407 u32 snk_bc12_cmpletion_time; 408 }; 409 410 /* Convert microwatt to watt */ 411 #define UW_TO_W(pow) (div_u64((pow), 1000000)) 412 413 /* Convert micro-Watt-hours to USB PD energy (increments of 0.1Wh) */ 414 #define UWH_TO_PD_ENERGY(energy) (UW_TO_W((u64)(energy) * 10)) 415 416 /* 417 * As per USB PD Spec Rev 3.18 (Sec. 6.5.13.11), the number of fixed batteries 418 * that a port can be queried is restricted to 4. 419 */ 420 #define MAX_NUM_FIXED_BATT 4 421 422 #define BATTERY_PROPERTY_UNKNOWN 0xffff 423 424 /* 425 * struct pd_identifier - Contains info about PD identifiers 426 * @vid: Vendor ID (assigned by USB-IF) 427 * @pid: Product ID (assigned by manufacturer) 428 * @xid: Value assigned by USB-IF for product 429 */ 430 struct pd_identifier { 431 u16 vid; 432 u16 pid; 433 u32 xid; 434 }; 435 436 /* 437 * struct sink_caps_ext_data - Sink extended capability data 438 * @load_step: Indicates the load step slew rate. Value of 0 indicates 150mA/us 439 * & 1 indicates 500 mA/us 440 * @load_char: Snk overload characteristics 441 * @compliance: Types of sources the sink has been tested & certified on 442 * @modes: Charging caps & power sources supported 443 * @spr_min_pdp: Sink Minimum PDP for SPR mode (in Watts) 444 * @spr_op_pdp: Sink Operational PDP for SPR mode (in Watts) 445 * @spr_max_pdp: Sink Maximum PDP for SPR mode (in Watts) 446 */ 447 struct sink_caps_ext_data { 448 u8 load_step; 449 u16 load_char; 450 u8 compliance; 451 u8 modes; 452 u8 spr_min_pdp; 453 u8 spr_op_pdp; 454 u8 spr_max_pdp; 455 }; 456 457 enum aug_req_type { 458 PD_PPS, 459 PD_SPR_AVS, 460 }; 461 462 struct tcpm_port { 463 struct device *dev; 464 465 struct mutex lock; /* tcpm state machine lock */ 466 struct kthread_worker *wq; 467 468 struct typec_capability typec_caps; 469 struct typec_port *typec_port; 470 471 struct tcpc_dev *tcpc; 472 struct usb_role_switch *role_sw; 473 474 enum typec_role vconn_role; 475 enum typec_role pwr_role; 476 enum typec_data_role data_role; 477 enum typec_pwr_opmode pwr_opmode; 478 479 struct usb_pd_identity partner_ident; 480 struct typec_partner_desc partner_desc; 481 struct typec_partner *partner; 482 483 struct usb_pd_identity cable_ident; 484 struct typec_cable_desc cable_desc; 485 struct typec_cable *cable; 486 struct typec_plug_desc plug_prime_desc; 487 struct typec_plug *plug_prime; 488 489 enum typec_cc_status cc_req; 490 enum typec_cc_status src_rp; /* work only if pd_supported == false */ 491 492 enum typec_cc_status cc1; 493 enum typec_cc_status cc2; 494 enum typec_cc_polarity polarity; 495 496 bool attached; 497 bool connected; 498 bool registered; 499 bool pd_supported; 500 enum typec_port_type port_type; 501 502 /* 503 * Set to true when vbus is greater than VSAFE5V min. 504 * Set to false when vbus falls below vSinkDisconnect max threshold. 505 */ 506 bool vbus_present; 507 508 /* 509 * Set to true when vbus is less than VSAFE0V max. 510 * Set to false when vbus is greater than VSAFE0V max. 511 */ 512 bool vbus_vsafe0v; 513 514 bool vbus_never_low; 515 bool vbus_source; 516 bool vbus_charge; 517 518 bool op_vsafe5v; 519 520 int try_role; 521 int try_snk_count; 522 int try_src_count; 523 524 enum pd_msg_request queued_message; 525 526 enum tcpm_state enter_state; 527 enum tcpm_state prev_state; 528 enum tcpm_state state; 529 enum tcpm_state delayed_state; 530 ktime_t delayed_runtime; 531 unsigned long delay_ms; 532 533 spinlock_t pd_event_lock; 534 u32 pd_events; 535 536 struct kthread_work event_work; 537 struct hrtimer state_machine_timer; 538 struct kthread_work state_machine; 539 struct hrtimer vdm_state_machine_timer; 540 struct kthread_work vdm_state_machine; 541 struct hrtimer enable_frs_timer; 542 struct kthread_work enable_frs; 543 struct hrtimer vdm_discovery_timer; 544 struct kthread_work vdm_discovery_work; 545 bool vdm_discovery_armed; 546 bool state_machine_running; 547 /* Set to true when VDM State Machine has following actions. */ 548 bool vdm_sm_running; 549 550 struct completion tx_complete; 551 enum tcpm_transmit_status tx_status; 552 553 struct mutex swap_lock; /* swap command lock */ 554 bool swap_pending; 555 bool non_pd_role_swap; 556 struct completion swap_complete; 557 int swap_status; 558 559 unsigned int negotiated_rev; 560 unsigned int message_id; 561 unsigned int caps_count; 562 unsigned int hard_reset_count; 563 bool pd_capable; 564 bool explicit_contract; 565 unsigned int rx_msgid; 566 567 /* USB PD objects */ 568 struct usb_power_delivery **pds; 569 struct pd_data **pd_list; 570 struct usb_power_delivery_capabilities *port_source_caps; 571 struct usb_power_delivery_capabilities *port_sink_caps; 572 struct usb_power_delivery *partner_pd; 573 struct usb_power_delivery_capabilities *partner_source_caps; 574 struct usb_power_delivery_capabilities *partner_sink_caps; 575 struct usb_power_delivery *selected_pd; 576 577 /* Partner capabilities/requests */ 578 u32 sink_request; 579 u32 source_caps[PDO_MAX_OBJECTS]; 580 unsigned int nr_source_caps; 581 u32 sink_caps[PDO_MAX_OBJECTS]; 582 unsigned int nr_sink_caps; 583 584 /* Local capabilities */ 585 unsigned int pd_count; 586 u32 src_pdo[PDO_MAX_OBJECTS]; 587 unsigned int nr_src_pdo; 588 u32 snk_pdo[PDO_MAX_OBJECTS]; 589 unsigned int nr_snk_pdo; 590 u32 snk_vdo_v1[VDO_MAX_OBJECTS]; 591 unsigned int nr_snk_vdo_v1; 592 u32 snk_vdo[VDO_MAX_OBJECTS]; 593 unsigned int nr_snk_vdo; 594 595 unsigned int operating_snk_mw; 596 bool update_sink_caps; 597 598 /* Requested current / voltage to the port partner */ 599 u32 req_current_limit; 600 u32 req_supply_voltage; 601 /* Actual current / voltage limit of the local port */ 602 u32 current_limit; 603 u32 supply_voltage; 604 605 /* Used to export TA voltage and current */ 606 struct power_supply *psy; 607 struct power_supply_desc psy_desc; 608 enum power_supply_usb_type usb_type; 609 610 u32 bist_request; 611 612 /* VDM Discovery State to determine message sent */ 613 enum vdm_discovery_states vdm_discovery_state; 614 615 /* PD state for Vendor Defined Messages */ 616 enum vdm_states vdm_state; 617 u32 vdm_retries; 618 /* next Vendor Defined Message to send */ 619 u32 vdo_data[VDO_MAX_SIZE]; 620 u8 vdo_count; 621 /* VDO to retry if UFP responder replied busy */ 622 u32 vdo_retry; 623 624 /* PPS */ 625 struct pd_pps_data pps_data; 626 627 /* SPR AVS */ 628 struct pd_spr_avs_data spr_avs_data; 629 630 /* Augmented supply request - PPS; SPR_AVS */ 631 struct completion aug_supply_req_complete; 632 bool aug_supply_req_pending; 633 int aug_supply_req_status; 634 635 /* Alternate mode data */ 636 struct pd_mode_data mode_data; 637 struct pd_mode_data mode_data_prime; 638 struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX]; 639 struct typec_altmode *plug_prime_altmode[ALTMODE_DISCOVERY_MAX]; 640 struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX]; 641 642 /* Deadline in jiffies to exit src_try_wait state */ 643 unsigned long max_wait; 644 645 /* port belongs to a self powered device */ 646 bool self_powered; 647 648 /* Sink FRS */ 649 enum frs_typec_current new_source_frs_current; 650 651 /* Sink caps have been queried */ 652 bool sink_cap_done; 653 654 /* Collision Avoidance and Atomic Message Sequence */ 655 enum tcpm_state upcoming_state; 656 enum tcpm_ams ams; 657 enum tcpm_ams next_ams; 658 bool in_ams; 659 660 /* Auto vbus discharge status */ 661 bool auto_vbus_discharge_enabled; 662 663 /* 664 * When set, port requests PD_P_SNK_STDBY_MW upon entering SNK_DISCOVERY and 665 * the actual current limit after RX of PD_CTRL_PSRDY for PD link, 666 * SNK_READY for non-pd link. 667 */ 668 bool slow_charger_loop; 669 670 /* 671 * When true indicates that the lower level drivers indicate potential presence 672 * of contaminant in the connector pins based on the tcpm state machine 673 * transitions. 674 */ 675 bool potential_contaminant; 676 677 /* SOP* Related Fields */ 678 /* 679 * tx_sop_type determines which SOP* a message is being sent on. 680 * For messages that are queued and not sent immediately such as in 681 * tcpm_queue_message or messages that send after state changes, 682 * the tx_sop_type is set accordingly. 683 */ 684 enum tcpm_transmit_type tx_sop_type; 685 /* 686 * Prior to discovering the port partner's Specification Revision, the 687 * Vconn source and cable plug will use the lower of their two revisions. 688 * 689 * When the port partner's Specification Revision is discovered, the following 690 * rules are put in place. 691 * 1. If the cable revision (1) is lower than the revision negotiated 692 * between the port and partner (2), the port and partner will communicate 693 * on revision (2), but the port and cable will communicate on revision (1). 694 * 2. If the cable revision (1) is higher than the revision negotiated 695 * between the port and partner (2), the port and partner will communicate 696 * on revision (2), and the port and cable will communicate on revision (2) 697 * as well. 698 */ 699 unsigned int negotiated_rev_prime; 700 /* 701 * Each SOP* type must maintain their own tx and rx message IDs 702 */ 703 unsigned int message_id_prime; 704 unsigned int rx_msgid_prime; 705 706 /* Timer deadline values configured at runtime */ 707 struct pd_timings timings; 708 709 /* Indicates maximum (revision, version) supported */ 710 struct pd_revision_info pd_rev; 711 712 struct pd_identifier pd_ident; 713 struct sink_caps_ext_data sink_caps_ext; 714 struct power_supply **fixed_batt; 715 u32 fixed_batt_cnt; 716 u32 batt_request_id; 717 #ifdef CONFIG_DEBUG_FS 718 struct dentry *dentry; 719 struct mutex logbuffer_lock; /* log buffer access lock */ 720 int logbuffer_head; 721 int logbuffer_tail; 722 u8 *logbuffer[LOG_BUFFER_ENTRIES]; 723 #endif 724 }; 725 726 struct pd_rx_event { 727 struct kthread_work work; 728 struct tcpm_port *port; 729 struct pd_message msg; 730 enum tcpm_transmit_type rx_sop_type; 731 }; 732 733 struct altmode_vdm_event { 734 struct kthread_work work; 735 struct tcpm_port *port; 736 u32 header; 737 int cnt; 738 enum tcpm_transmit_type tx_sop_type; 739 u32 data[] __counted_by(cnt); 740 }; 741 742 static const char * const pd_rev[] = { 743 [PD_REV10] = "rev1", 744 [PD_REV20] = "rev2", 745 [PD_REV30] = "rev3", 746 }; 747 748 #define tcpm_cc_is_sink(cc) \ 749 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \ 750 (cc) == TYPEC_CC_RP_3_0) 751 752 /* As long as cc is pulled up, we can consider it as sink. */ 753 #define tcpm_port_is_sink(port) \ 754 (tcpm_cc_is_sink((port)->cc1) || tcpm_cc_is_sink((port)->cc2)) 755 756 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD) 757 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA) 758 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN) 759 760 #define tcpm_port_is_source(port) \ 761 ((tcpm_cc_is_source((port)->cc1) && \ 762 !tcpm_cc_is_source((port)->cc2)) || \ 763 (tcpm_cc_is_source((port)->cc2) && \ 764 !tcpm_cc_is_source((port)->cc1))) 765 766 #define tcpm_port_is_debug_source(port) \ 767 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2)) 768 769 #define tcpm_port_is_debug_sink(port) \ 770 (tcpm_cc_is_sink((port)->cc1) && tcpm_cc_is_sink((port)->cc2)) 771 772 #define tcpm_port_is_debug(port) \ 773 (tcpm_port_is_debug_source(port) || tcpm_port_is_debug_sink(port)) 774 775 #define tcpm_port_is_audio(port) \ 776 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2)) 777 778 #define tcpm_port_is_audio_detached(port) \ 779 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \ 780 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1))) 781 782 #define tcpm_try_snk(port) \ 783 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \ 784 (port)->port_type == TYPEC_PORT_DRP) 785 786 #define tcpm_try_src(port) \ 787 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \ 788 (port)->port_type == TYPEC_PORT_DRP) 789 790 #define tcpm_data_role_for_source(port) \ 791 ((port)->typec_caps.data == TYPEC_PORT_UFP ? \ 792 TYPEC_DEVICE : TYPEC_HOST) 793 794 #define tcpm_data_role_for_sink(port) \ 795 ((port)->typec_caps.data == TYPEC_PORT_DFP ? \ 796 TYPEC_HOST : TYPEC_DEVICE) 797 798 #define tcpm_sink_tx_ok(port) \ 799 (tcpm_port_is_sink(port) && \ 800 ((port)->cc1 == TYPEC_CC_RP_3_0 || (port)->cc2 == TYPEC_CC_RP_3_0)) 801 802 #define tcpm_wait_for_discharge(port) \ 803 (((port)->auto_vbus_discharge_enabled && !(port)->vbus_vsafe0v) ? PD_T_SAFE_0V : 0) 804 805 #define tcpm_can_send_vdm(state) \ 806 ((state == SRC_READY || state == SNK_READY || state == SRC_VDM_IDENTITY_REQUEST)) 807 808 static enum tcpm_state tcpm_default_state(struct tcpm_port *port) 809 { 810 if (port->port_type == TYPEC_PORT_DRP) { 811 if (port->try_role == TYPEC_SINK) 812 return SNK_UNATTACHED; 813 else if (port->try_role == TYPEC_SOURCE) 814 return SRC_UNATTACHED; 815 /* Fall through to return SRC_UNATTACHED */ 816 } else if (port->port_type == TYPEC_PORT_SNK) { 817 return SNK_UNATTACHED; 818 } 819 return SRC_UNATTACHED; 820 } 821 822 static bool tcpm_port_is_disconnected(struct tcpm_port *port) 823 { 824 return (!port->attached && port->cc1 == TYPEC_CC_OPEN && 825 port->cc2 == TYPEC_CC_OPEN) || 826 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 && 827 port->cc1 == TYPEC_CC_OPEN) || 828 (port->polarity == TYPEC_POLARITY_CC2 && 829 port->cc2 == TYPEC_CC_OPEN))); 830 } 831 832 /* 833 * Logging 834 */ 835 836 #ifdef CONFIG_DEBUG_FS 837 838 static bool tcpm_log_full(struct tcpm_port *port) 839 { 840 return port->logbuffer_tail == 841 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES; 842 } 843 844 __printf(2, 0) 845 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args) 846 { 847 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE]; 848 u64 ts_nsec = local_clock(); 849 unsigned long rem_nsec; 850 851 mutex_lock(&port->logbuffer_lock); 852 if (!port->logbuffer[port->logbuffer_head]) { 853 port->logbuffer[port->logbuffer_head] = 854 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL); 855 if (!port->logbuffer[port->logbuffer_head]) { 856 mutex_unlock(&port->logbuffer_lock); 857 return; 858 } 859 } 860 861 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args); 862 863 if (tcpm_log_full(port)) { 864 port->logbuffer_head = max(port->logbuffer_head - 1, 0); 865 strscpy(tmpbuffer, "overflow"); 866 } 867 868 if (port->logbuffer_head < 0 || 869 port->logbuffer_head >= LOG_BUFFER_ENTRIES) { 870 dev_warn(port->dev, 871 "Bad log buffer index %d\n", port->logbuffer_head); 872 goto abort; 873 } 874 875 if (!port->logbuffer[port->logbuffer_head]) { 876 dev_warn(port->dev, 877 "Log buffer index %d is NULL\n", port->logbuffer_head); 878 goto abort; 879 } 880 881 rem_nsec = do_div(ts_nsec, 1000000000); 882 scnprintf(port->logbuffer[port->logbuffer_head], 883 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s", 884 (unsigned long)ts_nsec, rem_nsec / 1000, 885 tmpbuffer); 886 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES; 887 888 abort: 889 mutex_unlock(&port->logbuffer_lock); 890 } 891 892 __printf(2, 3) 893 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...) 894 { 895 va_list args; 896 897 /* Do not log while disconnected and unattached */ 898 if (tcpm_port_is_disconnected(port) && 899 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED || 900 port->state == TOGGLING || port->state == CHECK_CONTAMINANT)) 901 return; 902 903 va_start(args, fmt); 904 _tcpm_log(port, fmt, args); 905 va_end(args); 906 } 907 908 __printf(2, 3) 909 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) 910 { 911 va_list args; 912 913 va_start(args, fmt); 914 _tcpm_log(port, fmt, args); 915 va_end(args); 916 } 917 918 static void tcpm_log_source_caps(struct tcpm_port *port) 919 { 920 int i; 921 922 for (i = 0; i < port->nr_source_caps; i++) { 923 u32 pdo = port->source_caps[i]; 924 enum pd_pdo_type type = pdo_type(pdo); 925 char msg[64]; 926 927 switch (type) { 928 case PDO_TYPE_FIXED: 929 scnprintf(msg, sizeof(msg), 930 "%u mV, %u mA [%s%s%s%s%s%s]", 931 pdo_fixed_voltage(pdo), 932 pdo_max_current(pdo), 933 (pdo & PDO_FIXED_DUAL_ROLE) ? 934 "R" : "", 935 (pdo & PDO_FIXED_SUSPEND) ? 936 "S" : "", 937 (pdo & PDO_FIXED_HIGHER_CAP) ? 938 "H" : "", 939 (pdo & PDO_FIXED_USB_COMM) ? 940 "U" : "", 941 (pdo & PDO_FIXED_DATA_SWAP) ? 942 "D" : "", 943 (pdo & PDO_FIXED_EXTPOWER) ? 944 "E" : ""); 945 break; 946 case PDO_TYPE_VAR: 947 scnprintf(msg, sizeof(msg), 948 "%u-%u mV, %u mA", 949 pdo_min_voltage(pdo), 950 pdo_max_voltage(pdo), 951 pdo_max_current(pdo)); 952 break; 953 case PDO_TYPE_BATT: 954 scnprintf(msg, sizeof(msg), 955 "%u-%u mV, %u mW", 956 pdo_min_voltage(pdo), 957 pdo_max_voltage(pdo), 958 pdo_max_power(pdo)); 959 break; 960 case PDO_TYPE_APDO: 961 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) 962 scnprintf(msg, sizeof(msg), 963 "PPS %u-%u mV, %u mA", 964 pdo_pps_apdo_min_voltage(pdo), 965 pdo_pps_apdo_max_voltage(pdo), 966 pdo_pps_apdo_max_current(pdo)); 967 else if (pdo_apdo_type(pdo) == APDO_TYPE_EPR_AVS) 968 scnprintf(msg, sizeof(msg), 969 "EPR AVS %u-%u mV %u W peak_current: %u", 970 pdo_epr_avs_apdo_min_voltage_mv(pdo), 971 pdo_epr_avs_apdo_max_voltage_mv(pdo), 972 pdo_epr_avs_apdo_pdp_w(pdo), 973 pdo_epr_avs_apdo_src_peak_current(pdo)); 974 else if (pdo_apdo_type(pdo) == APDO_TYPE_SPR_AVS) 975 scnprintf(msg, sizeof(msg), 976 "SPR AVS 9-15 V: %u mA 15-20 V: %u mA peak_current: %u", 977 pdo_spr_avs_apdo_9v_to_15v_max_current_ma(pdo), 978 pdo_spr_avs_apdo_15v_to_20v_max_current_ma(pdo), 979 pdo_spr_avs_apdo_src_peak_current(pdo)); 980 else 981 strscpy(msg, "undefined APDO"); 982 break; 983 default: 984 strscpy(msg, "undefined"); 985 break; 986 } 987 tcpm_log(port, " PDO %d: type %d, %s", 988 i, type, msg); 989 } 990 } 991 992 static int tcpm_debug_show(struct seq_file *s, void *v) 993 { 994 struct tcpm_port *port = s->private; 995 int tail; 996 997 mutex_lock(&port->logbuffer_lock); 998 tail = port->logbuffer_tail; 999 while (tail != port->logbuffer_head) { 1000 seq_printf(s, "%s\n", port->logbuffer[tail]); 1001 tail = (tail + 1) % LOG_BUFFER_ENTRIES; 1002 } 1003 if (!seq_has_overflowed(s)) 1004 port->logbuffer_tail = tail; 1005 mutex_unlock(&port->logbuffer_lock); 1006 1007 return 0; 1008 } 1009 DEFINE_SHOW_ATTRIBUTE(tcpm_debug); 1010 1011 static void tcpm_debugfs_init(struct tcpm_port *port) 1012 { 1013 char name[NAME_MAX]; 1014 1015 mutex_init(&port->logbuffer_lock); 1016 snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev)); 1017 port->dentry = debugfs_create_dir(name, usb_debug_root); 1018 debugfs_create_file("log", S_IFREG | 0444, port->dentry, port, 1019 &tcpm_debug_fops); 1020 } 1021 1022 static void tcpm_debugfs_exit(struct tcpm_port *port) 1023 { 1024 int i; 1025 1026 mutex_lock(&port->logbuffer_lock); 1027 for (i = 0; i < LOG_BUFFER_ENTRIES; i++) { 1028 kfree(port->logbuffer[i]); 1029 port->logbuffer[i] = NULL; 1030 } 1031 mutex_unlock(&port->logbuffer_lock); 1032 1033 debugfs_remove(port->dentry); 1034 } 1035 1036 #else 1037 1038 __printf(2, 3) 1039 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { } 1040 __printf(2, 3) 1041 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { } 1042 static void tcpm_log_source_caps(struct tcpm_port *port) { } 1043 static void tcpm_debugfs_init(const struct tcpm_port *port) { } 1044 static void tcpm_debugfs_exit(const struct tcpm_port *port) { } 1045 1046 #endif 1047 1048 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc) 1049 { 1050 tcpm_log(port, "cc:=%d", cc); 1051 port->cc_req = cc; 1052 port->tcpc->set_cc(port->tcpc, cc); 1053 } 1054 1055 static int tcpm_enable_auto_vbus_discharge(struct tcpm_port *port, bool enable) 1056 { 1057 int ret = 0; 1058 1059 if (port->tcpc->enable_auto_vbus_discharge) { 1060 ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, enable); 1061 tcpm_log_force(port, "%s vbus discharge ret:%d", 1062 str_enable_disable(enable), ret); 1063 if (!ret) 1064 port->auto_vbus_discharge_enabled = enable; 1065 } 1066 1067 return ret; 1068 } 1069 1070 static void tcpm_apply_rc(struct tcpm_port *port) 1071 { 1072 /* 1073 * TCPCI: Move to APPLY_RC state to prevent disconnect during PR_SWAP 1074 * when Vbus auto discharge on disconnect is enabled. 1075 */ 1076 if (port->tcpc->enable_auto_vbus_discharge && port->tcpc->apply_rc) { 1077 tcpm_log(port, "Apply_RC"); 1078 port->tcpc->apply_rc(port->tcpc, port->cc_req, port->polarity); 1079 tcpm_enable_auto_vbus_discharge(port, false); 1080 } 1081 } 1082 1083 /* 1084 * Determine RP value to set based on maximum current supported 1085 * by a port if configured as source. 1086 * Returns CC value to report to link partner. 1087 */ 1088 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port) 1089 { 1090 const u32 *src_pdo = port->src_pdo; 1091 int nr_pdo = port->nr_src_pdo; 1092 int i; 1093 1094 if (!port->pd_supported) 1095 return port->src_rp; 1096 1097 /* 1098 * Search for first entry with matching voltage. 1099 * It should report the maximum supported current. 1100 */ 1101 for (i = 0; i < nr_pdo; i++) { 1102 const u32 pdo = src_pdo[i]; 1103 1104 if (pdo_type(pdo) == PDO_TYPE_FIXED && 1105 pdo_fixed_voltage(pdo) == 5000) { 1106 unsigned int curr = pdo_max_current(pdo); 1107 1108 if (curr >= 3000) 1109 return TYPEC_CC_RP_3_0; 1110 else if (curr >= 1500) 1111 return TYPEC_CC_RP_1_5; 1112 return TYPEC_CC_RP_DEF; 1113 } 1114 } 1115 1116 return TYPEC_CC_RP_DEF; 1117 } 1118 1119 static void tcpm_ams_finish(struct tcpm_port *port) 1120 { 1121 tcpm_log(port, "AMS %s finished", tcpm_ams_str[port->ams]); 1122 1123 if (port->pd_capable && port->pwr_role == TYPEC_SOURCE) { 1124 if (port->negotiated_rev >= PD_REV30) 1125 tcpm_set_cc(port, SINK_TX_OK); 1126 else 1127 tcpm_set_cc(port, SINK_TX_NG); 1128 } else if (port->pwr_role == TYPEC_SOURCE) { 1129 tcpm_set_cc(port, tcpm_rp_cc(port)); 1130 } 1131 1132 port->in_ams = false; 1133 port->ams = NONE_AMS; 1134 } 1135 1136 static int tcpm_pd_transmit(struct tcpm_port *port, 1137 enum tcpm_transmit_type tx_sop_type, 1138 const struct pd_message *msg) 1139 { 1140 unsigned long time_left; 1141 int ret; 1142 unsigned int negotiated_rev; 1143 1144 switch (tx_sop_type) { 1145 case TCPC_TX_SOP_PRIME: 1146 negotiated_rev = port->negotiated_rev_prime; 1147 break; 1148 case TCPC_TX_SOP: 1149 default: 1150 negotiated_rev = port->negotiated_rev; 1151 break; 1152 } 1153 1154 if (msg) 1155 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header)); 1156 else 1157 tcpm_log(port, "PD TX, type: %#x", tx_sop_type); 1158 1159 reinit_completion(&port->tx_complete); 1160 ret = port->tcpc->pd_transmit(port->tcpc, tx_sop_type, msg, negotiated_rev); 1161 if (ret < 0) 1162 return ret; 1163 1164 mutex_unlock(&port->lock); 1165 time_left = wait_for_completion_timeout(&port->tx_complete, 1166 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT)); 1167 mutex_lock(&port->lock); 1168 if (!time_left) 1169 return -ETIMEDOUT; 1170 1171 switch (port->tx_status) { 1172 case TCPC_TX_SUCCESS: 1173 switch (tx_sop_type) { 1174 case TCPC_TX_SOP_PRIME: 1175 port->message_id_prime = (port->message_id_prime + 1) & 1176 PD_HEADER_ID_MASK; 1177 break; 1178 case TCPC_TX_SOP: 1179 default: 1180 port->message_id = (port->message_id + 1) & 1181 PD_HEADER_ID_MASK; 1182 break; 1183 } 1184 /* 1185 * USB PD rev 2.0, 8.3.2.2.1: 1186 * USB PD rev 3.0, 8.3.2.1.3: 1187 * "... Note that every AMS is Interruptible until the first 1188 * Message in the sequence has been successfully sent (GoodCRC 1189 * Message received)." 1190 */ 1191 if (port->ams != NONE_AMS) 1192 port->in_ams = true; 1193 break; 1194 case TCPC_TX_DISCARDED: 1195 ret = -EAGAIN; 1196 break; 1197 case TCPC_TX_FAILED: 1198 default: 1199 ret = -EIO; 1200 break; 1201 } 1202 1203 /* Some AMS don't expect responses. Finish them here. */ 1204 if (port->ams == ATTENTION || port->ams == SOURCE_ALERT) 1205 tcpm_ams_finish(port); 1206 1207 return ret; 1208 } 1209 1210 void tcpm_pd_transmit_complete(struct tcpm_port *port, 1211 enum tcpm_transmit_status status) 1212 { 1213 tcpm_log(port, "PD TX complete, status: %u", status); 1214 port->tx_status = status; 1215 complete(&port->tx_complete); 1216 } 1217 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete); 1218 1219 static int tcpm_mux_set(struct tcpm_port *port, int state, 1220 enum usb_role usb_role, 1221 enum typec_orientation orientation) 1222 { 1223 int ret; 1224 1225 tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d", 1226 state, usb_role, orientation); 1227 1228 ret = typec_set_orientation(port->typec_port, orientation); 1229 if (ret) 1230 return ret; 1231 1232 if (port->role_sw) { 1233 ret = usb_role_switch_set_role(port->role_sw, usb_role); 1234 if (ret) 1235 return ret; 1236 } 1237 1238 return typec_set_mode(port->typec_port, state); 1239 } 1240 1241 static int tcpm_set_polarity(struct tcpm_port *port, 1242 enum typec_cc_polarity polarity) 1243 { 1244 int ret; 1245 1246 tcpm_log(port, "polarity %d", polarity); 1247 1248 ret = port->tcpc->set_polarity(port->tcpc, polarity); 1249 if (ret < 0) 1250 return ret; 1251 1252 port->polarity = polarity; 1253 1254 return 0; 1255 } 1256 1257 static int tcpm_set_vconn(struct tcpm_port *port, bool enable) 1258 { 1259 int ret; 1260 1261 tcpm_log(port, "vconn:=%d", enable); 1262 1263 ret = port->tcpc->set_vconn(port->tcpc, enable); 1264 if (!ret) { 1265 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK; 1266 typec_set_vconn_role(port->typec_port, port->vconn_role); 1267 } 1268 1269 return ret; 1270 } 1271 1272 static u32 tcpm_get_current_limit(struct tcpm_port *port) 1273 { 1274 enum typec_cc_status cc; 1275 u32 limit; 1276 1277 cc = port->polarity ? port->cc2 : port->cc1; 1278 switch (cc) { 1279 case TYPEC_CC_RP_1_5: 1280 limit = 1500; 1281 break; 1282 case TYPEC_CC_RP_3_0: 1283 limit = 3000; 1284 break; 1285 case TYPEC_CC_RP_DEF: 1286 default: 1287 if (port->tcpc->get_current_limit) 1288 limit = port->tcpc->get_current_limit(port->tcpc); 1289 else 1290 limit = 0; 1291 break; 1292 } 1293 1294 return limit; 1295 } 1296 1297 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv) 1298 { 1299 int ret = -EOPNOTSUPP; 1300 1301 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma); 1302 1303 port->supply_voltage = mv; 1304 port->current_limit = max_ma; 1305 power_supply_changed(port->psy); 1306 1307 if (port->tcpc->set_current_limit) 1308 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv); 1309 1310 return ret; 1311 } 1312 1313 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached) 1314 { 1315 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role, 1316 port->data_role); 1317 } 1318 1319 static int tcpm_set_roles(struct tcpm_port *port, bool attached, int state, 1320 enum typec_role role, enum typec_data_role data) 1321 { 1322 enum typec_orientation orientation; 1323 enum usb_role usb_role; 1324 int ret; 1325 1326 if (port->polarity == TYPEC_POLARITY_CC1) 1327 orientation = TYPEC_ORIENTATION_NORMAL; 1328 else 1329 orientation = TYPEC_ORIENTATION_REVERSE; 1330 1331 if (port->typec_caps.data == TYPEC_PORT_DRD) { 1332 if (data == TYPEC_HOST) 1333 usb_role = USB_ROLE_HOST; 1334 else 1335 usb_role = USB_ROLE_DEVICE; 1336 } else if (port->typec_caps.data == TYPEC_PORT_DFP) { 1337 if (data == TYPEC_HOST) { 1338 if (role == TYPEC_SOURCE) 1339 usb_role = USB_ROLE_HOST; 1340 else 1341 usb_role = USB_ROLE_NONE; 1342 } else { 1343 return -ENOTSUPP; 1344 } 1345 } else { 1346 if (data == TYPEC_DEVICE) { 1347 if (role == TYPEC_SINK) 1348 usb_role = USB_ROLE_DEVICE; 1349 else 1350 usb_role = USB_ROLE_NONE; 1351 } else { 1352 return -ENOTSUPP; 1353 } 1354 } 1355 1356 ret = tcpm_mux_set(port, state, usb_role, orientation); 1357 if (ret < 0) 1358 return ret; 1359 1360 ret = port->tcpc->set_roles(port->tcpc, attached, role, data); 1361 if (ret < 0) 1362 return ret; 1363 1364 if (port->tcpc->set_orientation) { 1365 ret = port->tcpc->set_orientation(port->tcpc, orientation); 1366 if (ret < 0) 1367 return ret; 1368 } 1369 1370 port->pwr_role = role; 1371 port->data_role = data; 1372 typec_set_data_role(port->typec_port, data); 1373 typec_set_pwr_role(port->typec_port, role); 1374 1375 return 0; 1376 } 1377 1378 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role) 1379 { 1380 int ret; 1381 1382 ret = port->tcpc->set_roles(port->tcpc, true, role, 1383 port->data_role); 1384 if (ret < 0) 1385 return ret; 1386 1387 port->pwr_role = role; 1388 typec_set_pwr_role(port->typec_port, role); 1389 1390 return 0; 1391 } 1392 1393 /* 1394 * Transform the PDO to be compliant to PD rev2.0. 1395 * Return 0 if the PDO type is not defined in PD rev2.0. 1396 * Otherwise, return the converted PDO. 1397 */ 1398 static u32 tcpm_forge_legacy_pdo(struct tcpm_port *port, u32 pdo, enum typec_role role) 1399 { 1400 switch (pdo_type(pdo)) { 1401 case PDO_TYPE_FIXED: 1402 if (role == TYPEC_SINK) 1403 return pdo & ~PDO_FIXED_FRS_CURR_MASK; 1404 else 1405 return pdo & ~PDO_FIXED_UNCHUNK_EXT; 1406 case PDO_TYPE_VAR: 1407 case PDO_TYPE_BATT: 1408 return pdo; 1409 case PDO_TYPE_APDO: 1410 default: 1411 return 0; 1412 } 1413 } 1414 1415 static int tcpm_pd_send_revision(struct tcpm_port *port) 1416 { 1417 struct pd_message msg; 1418 u32 rmdo; 1419 1420 memset(&msg, 0, sizeof(msg)); 1421 rmdo = RMDO(port->pd_rev.rev_major, port->pd_rev.rev_minor, 1422 port->pd_rev.ver_major, port->pd_rev.ver_minor); 1423 msg.payload[0] = cpu_to_le32(rmdo); 1424 msg.header = PD_HEADER_LE(PD_DATA_REVISION, 1425 port->pwr_role, 1426 port->data_role, 1427 port->negotiated_rev, 1428 port->message_id, 1429 1); 1430 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 1431 } 1432 1433 static int tcpm_pd_send_source_caps(struct tcpm_port *port) 1434 { 1435 struct pd_message msg; 1436 u32 pdo; 1437 unsigned int i, nr_pdo = 0; 1438 1439 memset(&msg, 0, sizeof(msg)); 1440 1441 for (i = 0; i < port->nr_src_pdo; i++) { 1442 if (port->negotiated_rev >= PD_REV30) { 1443 msg.payload[nr_pdo++] = cpu_to_le32(port->src_pdo[i]); 1444 } else { 1445 pdo = tcpm_forge_legacy_pdo(port, port->src_pdo[i], TYPEC_SOURCE); 1446 if (pdo) 1447 msg.payload[nr_pdo++] = cpu_to_le32(pdo); 1448 } 1449 } 1450 1451 if (!nr_pdo) { 1452 /* No source capabilities defined, sink only */ 1453 msg.header = PD_HEADER_LE(PD_CTRL_REJECT, 1454 port->pwr_role, 1455 port->data_role, 1456 port->negotiated_rev, 1457 port->message_id, 0); 1458 } else { 1459 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP, 1460 port->pwr_role, 1461 port->data_role, 1462 port->negotiated_rev, 1463 port->message_id, 1464 nr_pdo); 1465 } 1466 1467 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 1468 } 1469 1470 static int tcpm_pd_send_sink_caps(struct tcpm_port *port) 1471 { 1472 struct pd_message msg; 1473 u32 pdo; 1474 unsigned int i, nr_pdo = 0; 1475 1476 memset(&msg, 0, sizeof(msg)); 1477 1478 for (i = 0; i < port->nr_snk_pdo; i++) { 1479 if (port->negotiated_rev >= PD_REV30) { 1480 msg.payload[nr_pdo++] = cpu_to_le32(port->snk_pdo[i]); 1481 } else { 1482 pdo = tcpm_forge_legacy_pdo(port, port->snk_pdo[i], TYPEC_SINK); 1483 if (pdo) 1484 msg.payload[nr_pdo++] = cpu_to_le32(pdo); 1485 } 1486 } 1487 1488 if (!nr_pdo) { 1489 /* No sink capabilities defined, source only */ 1490 msg.header = PD_HEADER_LE(PD_CTRL_REJECT, 1491 port->pwr_role, 1492 port->data_role, 1493 port->negotiated_rev, 1494 port->message_id, 0); 1495 } else { 1496 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP, 1497 port->pwr_role, 1498 port->data_role, 1499 port->negotiated_rev, 1500 port->message_id, 1501 nr_pdo); 1502 } 1503 1504 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 1505 } 1506 1507 static void tcpm_get_fixed_batt(struct tcpm_port *port) 1508 { 1509 int ret; 1510 1511 if (!port->self_powered || port->fixed_batt_cnt > 0) 1512 return; 1513 1514 ret = power_supply_get_system_batteries(port->dev, &port->fixed_batt); 1515 if (ret < 0) 1516 tcpm_log(port, "Failed to get battery array, ret=%d", ret); 1517 else 1518 port->fixed_batt_cnt = ret; 1519 } 1520 1521 static int tcpm_pd_send_sink_cap_ext(struct tcpm_port *port) 1522 { 1523 u16 operating_snk_watt = port->operating_snk_mw / 1000; 1524 struct sink_caps_ext_data *data = &port->sink_caps_ext; 1525 struct pd_identifier *pd_ident = &port->pd_ident; 1526 struct sink_caps_ext_msg skedb = {0}; 1527 struct pd_message msg; 1528 u8 data_obj_cnt; 1529 1530 if (!port->self_powered) 1531 data->spr_op_pdp = operating_snk_watt; 1532 1533 tcpm_get_fixed_batt(port); 1534 1535 /* 1536 * SPR Sink Minimum PDP indicates the minimum power required to operate 1537 * a sink device in its lowest level of functionality without requiring 1538 * power from the battery. We can use the operating_snk_watt value to 1539 * populate it, as operating_snk_watt indicates device's min operating 1540 * power. 1541 */ 1542 data->spr_min_pdp = operating_snk_watt; 1543 1544 if (data->spr_op_pdp < data->spr_min_pdp || 1545 data->spr_max_pdp < data->spr_op_pdp) { 1546 tcpm_log(port, 1547 "Invalid PDP values, Min PDP:%u, Op PDP:%u, Max PDP:%u", 1548 data->spr_min_pdp, data->spr_op_pdp, data->spr_max_pdp); 1549 return -EOPNOTSUPP; 1550 } 1551 1552 memset(&msg, 0, sizeof(msg)); 1553 skedb.vid = cpu_to_le16(pd_ident->vid); 1554 skedb.pid = cpu_to_le16(pd_ident->pid); 1555 skedb.xid = cpu_to_le32(pd_ident->xid); 1556 skedb.skedb_ver = SKEDB_VER_1_0; 1557 skedb.load_step = data->load_step; 1558 skedb.load_char = cpu_to_le16(data->load_char); 1559 skedb.compliance = data->compliance; 1560 skedb.batt_info = min(port->fixed_batt_cnt, MAX_NUM_FIXED_BATT); 1561 skedb.modes = data->modes; 1562 skedb.spr_min_pdp = data->spr_min_pdp; 1563 skedb.spr_op_pdp = data->spr_op_pdp; 1564 skedb.spr_max_pdp = data->spr_max_pdp; 1565 memcpy(msg.ext_msg.data, &skedb, sizeof(skedb)); 1566 msg.ext_msg.header = PD_EXT_HDR_LE(sizeof(skedb), 1567 0, /* Denotes if request chunk */ 1568 0, /* Chunk Number */ 1569 1 /* Chunked */); 1570 1571 data_obj_cnt = count_chunked_data_objs(sizeof(skedb)); 1572 msg.header = cpu_to_le16(PD_HEADER(PD_EXT_SINK_CAP_EXT, 1573 port->pwr_role, 1574 port->data_role, 1575 port->negotiated_rev, 1576 port->message_id, 1577 data_obj_cnt, 1578 1 /* Denotes if ext header */)); 1579 1580 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 1581 } 1582 1583 static u16 tcpm_charge_to_energy(int charge, int voltage) 1584 { 1585 u64 energy = div_u64((u64)charge * voltage, 1000000); 1586 1587 return UWH_TO_PD_ENERGY(energy); 1588 } 1589 1590 static int tcpm_pd_send_batt_status(struct tcpm_port *port) 1591 { 1592 u16 present_charge = BATTERY_PROPERTY_UNKNOWN; 1593 bool batt_present = false, invalid_ref = true; 1594 u32 batt_id = port->batt_request_id; 1595 union power_supply_propval val; 1596 struct power_supply *batt; 1597 u8 charging_status = 0; 1598 struct pd_message msg; 1599 int ret, charge_now; 1600 u32 bsdo; 1601 1602 tcpm_get_fixed_batt(port); 1603 memset(&msg, 0, sizeof(msg)); 1604 1605 if (batt_id >= port->fixed_batt_cnt || batt_id >= MAX_NUM_FIXED_BATT) 1606 goto send_status; 1607 1608 invalid_ref = false; 1609 batt = port->fixed_batt[batt_id]; 1610 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_PRESENT, &val); 1611 if (ret) 1612 tcpm_log(port, 1613 "Failed to fetch power_supply_prop_present ret %d", 1614 ret); 1615 else 1616 batt_present = val.intval > 0; 1617 1618 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_ENERGY_NOW, &val); 1619 if (!ret) { 1620 present_charge = UWH_TO_PD_ENERGY(val.intval); 1621 } else { 1622 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_CHARGE_NOW, &val); 1623 if (!ret) { 1624 charge_now = val.intval; 1625 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_VOLTAGE_AVG, &val); 1626 if (!ret) 1627 present_charge = tcpm_charge_to_energy(charge_now, val.intval); 1628 } 1629 } 1630 1631 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_STATUS, &val); 1632 if (!ret) { 1633 switch (val.intval) { 1634 case POWER_SUPPLY_STATUS_CHARGING: 1635 charging_status = BSDO_BATTERY_INFO_CHARGING; 1636 break; 1637 case POWER_SUPPLY_STATUS_DISCHARGING: 1638 charging_status = BSDO_BATTERY_INFO_DISCHARGING; 1639 break; 1640 case POWER_SUPPLY_STATUS_NOT_CHARGING: 1641 case POWER_SUPPLY_STATUS_FULL: 1642 charging_status = BSDO_BATTERY_INFO_IDLE; 1643 break; 1644 default: 1645 charging_status = BSDO_BATTERY_INFO_RSVD; 1646 break; 1647 } 1648 } 1649 1650 send_status: 1651 1652 bsdo = BSDO(present_charge, charging_status, batt_present, invalid_ref); 1653 msg.payload[0] = cpu_to_le32(bsdo); 1654 msg.header = PD_HEADER_LE(PD_DATA_BATT_STATUS, 1655 port->pwr_role, 1656 port->data_role, 1657 port->negotiated_rev, 1658 port->message_id, 1659 1); 1660 1661 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 1662 } 1663 1664 static int tcpm_pd_send_batt_cap(struct tcpm_port *port) 1665 { 1666 u16 design_cap = BATTERY_PROPERTY_UNKNOWN; 1667 u16 charge_cap = BATTERY_PROPERTY_UNKNOWN; 1668 u32 batt_id = port->batt_request_id; 1669 union power_supply_propval val; 1670 struct batt_cap_ext_msg bcdb; 1671 struct power_supply *batt; 1672 bool invalid_ref = true; 1673 struct pd_message msg; 1674 u8 data_obj_cnt; 1675 int ret, vol; 1676 1677 tcpm_get_fixed_batt(port); 1678 memset(&msg, 0, sizeof(msg)); 1679 1680 if (batt_id >= port->fixed_batt_cnt || batt_id >= MAX_NUM_FIXED_BATT) 1681 goto send_cap; 1682 1683 invalid_ref = false; 1684 batt = port->fixed_batt[batt_id]; 1685 1686 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, &val); 1687 if (!ret) 1688 design_cap = UWH_TO_PD_ENERGY(val.intval); 1689 1690 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_ENERGY_FULL, &val); 1691 if (!ret) 1692 charge_cap = UWH_TO_PD_ENERGY(val.intval); 1693 1694 if (design_cap != BATTERY_PROPERTY_UNKNOWN && charge_cap != BATTERY_PROPERTY_UNKNOWN) 1695 goto send_cap; 1696 1697 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_VOLTAGE_AVG, &val); 1698 if (ret) 1699 goto send_cap; 1700 1701 vol = val.intval; 1702 if (design_cap == BATTERY_PROPERTY_UNKNOWN) { 1703 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, &val); 1704 if (!ret) 1705 design_cap = tcpm_charge_to_energy(val.intval, vol); 1706 } 1707 1708 if (charge_cap == BATTERY_PROPERTY_UNKNOWN) { 1709 ret = power_supply_get_property(batt, POWER_SUPPLY_PROP_CHARGE_FULL, &val); 1710 if (!ret) 1711 charge_cap = tcpm_charge_to_energy(val.intval, vol); 1712 } 1713 1714 send_cap: 1715 1716 /* 1717 * As per the USB PD Rev3.1 v1.8 spec, if a battery VID (assigned by the 1718 * USB-IF) does not exist or an invalid battery reference is made by the 1719 * requestor, then set the VID field to 0xffff. If the VID field is 1720 * 0xffff, set the PID field to 0. 1721 */ 1722 bcdb.vid = BATTERY_PROPERTY_UNKNOWN; 1723 bcdb.pid = 0; 1724 bcdb.batt_design_cap = cpu_to_le16(design_cap); 1725 bcdb.batt_last_chg_cap = cpu_to_le16(charge_cap); 1726 bcdb.batt_type = invalid_ref ? BATT_CAP_BATT_TYPE_INVALID_REF : 0; 1727 memcpy(msg.ext_msg.data, &bcdb, sizeof(bcdb)); 1728 msg.ext_msg.header = PD_EXT_HDR_LE(sizeof(bcdb), 1729 0, /* Denotes if request chunk */ 1730 0, /* Chunk number */ 1731 1 /* Chunked */); 1732 1733 data_obj_cnt = count_chunked_data_objs(sizeof(bcdb)); 1734 msg.header = PD_HEADER_EXT_LE(PD_EXT_BATT_CAP, port->pwr_role, 1735 port->data_role, port->negotiated_rev, 1736 port->message_id, data_obj_cnt); 1737 1738 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 1739 } 1740 1741 static void mod_tcpm_delayed_work(struct tcpm_port *port, unsigned int delay_ms) 1742 { 1743 if (delay_ms) { 1744 hrtimer_start(&port->state_machine_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); 1745 } else { 1746 hrtimer_cancel(&port->state_machine_timer); 1747 kthread_queue_work(port->wq, &port->state_machine); 1748 } 1749 } 1750 1751 static void mod_vdm_delayed_work(struct tcpm_port *port, unsigned int delay_ms) 1752 { 1753 if (delay_ms) { 1754 hrtimer_start(&port->vdm_state_machine_timer, ms_to_ktime(delay_ms), 1755 HRTIMER_MODE_REL); 1756 } else { 1757 hrtimer_cancel(&port->vdm_state_machine_timer); 1758 kthread_queue_work(port->wq, &port->vdm_state_machine); 1759 } 1760 } 1761 1762 static void mod_enable_frs_delayed_work(struct tcpm_port *port, unsigned int delay_ms) 1763 { 1764 if (delay_ms) { 1765 hrtimer_start(&port->enable_frs_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); 1766 } else { 1767 hrtimer_cancel(&port->enable_frs_timer); 1768 kthread_queue_work(port->wq, &port->enable_frs); 1769 } 1770 } 1771 1772 /* 1773 * Called with port->lock held, which the work takes, and not only from the 1774 * worker: the work cannot be waited for. It does nothing unless it is armed. 1775 */ 1776 static void mod_vdm_discovery_cancel_delayed_work(struct tcpm_port *port) 1777 { 1778 hrtimer_cancel(&port->vdm_discovery_timer); 1779 port->vdm_discovery_armed = false; 1780 } 1781 1782 static void mod_vdm_discovery_delayed_work(struct tcpm_port *port, unsigned int delay_ms) 1783 { 1784 port->vdm_discovery_armed = true; 1785 1786 if (delay_ms) { 1787 hrtimer_start(&port->vdm_discovery_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); 1788 } else { 1789 hrtimer_cancel(&port->vdm_discovery_timer); 1790 kthread_queue_work(port->wq, &port->vdm_discovery_work); 1791 } 1792 } 1793 1794 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state, 1795 unsigned int delay_ms) 1796 { 1797 if (delay_ms) { 1798 tcpm_log(port, "pending state change %s -> %s @ %u ms [%s %s]", 1799 tcpm_states[port->state], tcpm_states[state], delay_ms, 1800 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]); 1801 port->delayed_state = state; 1802 mod_tcpm_delayed_work(port, delay_ms); 1803 port->delayed_runtime = ktime_add(ktime_get(), ms_to_ktime(delay_ms)); 1804 port->delay_ms = delay_ms; 1805 } else { 1806 tcpm_log(port, "state change %s -> %s [%s %s]", 1807 tcpm_states[port->state], tcpm_states[state], 1808 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]); 1809 port->delayed_state = INVALID_STATE; 1810 port->prev_state = port->state; 1811 port->state = state; 1812 /* 1813 * Don't re-queue the state machine work item if we're currently 1814 * in the state machine and we're immediately changing states. 1815 * tcpm_state_machine_work() will continue running the state 1816 * machine. 1817 */ 1818 if (!port->state_machine_running) 1819 mod_tcpm_delayed_work(port, 0); 1820 } 1821 } 1822 1823 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state, 1824 unsigned int delay_ms) 1825 { 1826 if (port->enter_state == port->state) 1827 tcpm_set_state(port, state, delay_ms); 1828 else 1829 tcpm_log(port, 1830 "skipped %sstate change %s -> %s [%u ms], context state %s [%s %s]", 1831 delay_ms ? "delayed " : "", 1832 tcpm_states[port->state], tcpm_states[state], 1833 delay_ms, tcpm_states[port->enter_state], 1834 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]); 1835 } 1836 1837 static void tcpm_queue_message(struct tcpm_port *port, 1838 enum pd_msg_request message) 1839 { 1840 port->queued_message = message; 1841 mod_tcpm_delayed_work(port, 0); 1842 } 1843 1844 static bool tcpm_vdm_ams(struct tcpm_port *port) 1845 { 1846 switch (port->ams) { 1847 case DISCOVER_IDENTITY: 1848 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY: 1849 case DISCOVER_SVIDS: 1850 case DISCOVER_MODES: 1851 case DFP_TO_UFP_ENTER_MODE: 1852 case DFP_TO_UFP_EXIT_MODE: 1853 case DFP_TO_CABLE_PLUG_ENTER_MODE: 1854 case DFP_TO_CABLE_PLUG_EXIT_MODE: 1855 case ATTENTION: 1856 case UNSTRUCTURED_VDMS: 1857 case STRUCTURED_VDMS: 1858 break; 1859 default: 1860 return false; 1861 } 1862 1863 return true; 1864 } 1865 1866 static bool tcpm_ams_interruptible(struct tcpm_port *port) 1867 { 1868 switch (port->ams) { 1869 /* Interruptible AMS */ 1870 case NONE_AMS: 1871 case SECURITY: 1872 case FIRMWARE_UPDATE: 1873 case DISCOVER_IDENTITY: 1874 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY: 1875 case DISCOVER_SVIDS: 1876 case DISCOVER_MODES: 1877 case DFP_TO_UFP_ENTER_MODE: 1878 case DFP_TO_UFP_EXIT_MODE: 1879 case DFP_TO_CABLE_PLUG_ENTER_MODE: 1880 case DFP_TO_CABLE_PLUG_EXIT_MODE: 1881 case UNSTRUCTURED_VDMS: 1882 case STRUCTURED_VDMS: 1883 case COUNTRY_INFO: 1884 case COUNTRY_CODES: 1885 break; 1886 /* Non-Interruptible AMS */ 1887 default: 1888 if (port->in_ams) 1889 return false; 1890 break; 1891 } 1892 1893 return true; 1894 } 1895 1896 static int tcpm_ams_start(struct tcpm_port *port, enum tcpm_ams ams) 1897 { 1898 int ret = 0; 1899 1900 tcpm_log(port, "AMS %s start", tcpm_ams_str[ams]); 1901 1902 if (!tcpm_ams_interruptible(port) && 1903 !(ams == HARD_RESET || ams == SOFT_RESET_AMS)) { 1904 port->upcoming_state = INVALID_STATE; 1905 tcpm_log(port, "AMS %s not interruptible, aborting", 1906 tcpm_ams_str[port->ams]); 1907 return -EAGAIN; 1908 } 1909 1910 if (port->pwr_role == TYPEC_SOURCE) { 1911 enum typec_cc_status cc_req = port->cc_req; 1912 1913 port->ams = ams; 1914 1915 if (ams == HARD_RESET) { 1916 tcpm_set_cc(port, tcpm_rp_cc(port)); 1917 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL); 1918 tcpm_set_state(port, HARD_RESET_START, 0); 1919 return ret; 1920 } else if (ams == SOFT_RESET_AMS) { 1921 if (!port->explicit_contract) 1922 tcpm_set_cc(port, tcpm_rp_cc(port)); 1923 tcpm_set_state(port, SOFT_RESET_SEND, 0); 1924 return ret; 1925 } else if (tcpm_vdm_ams(port)) { 1926 /* tSinkTx is enforced in vdm_run_state_machine */ 1927 if (port->negotiated_rev >= PD_REV30) 1928 tcpm_set_cc(port, SINK_TX_NG); 1929 return ret; 1930 } 1931 1932 if (port->negotiated_rev >= PD_REV30) 1933 tcpm_set_cc(port, SINK_TX_NG); 1934 1935 switch (port->state) { 1936 case SRC_READY: 1937 case SRC_STARTUP: 1938 case SRC_SOFT_RESET_WAIT_SNK_TX: 1939 case SOFT_RESET: 1940 case SOFT_RESET_SEND: 1941 if (port->negotiated_rev >= PD_REV30) 1942 tcpm_set_state(port, AMS_START, 1943 cc_req == SINK_TX_OK ? 1944 PD_T_SINK_TX : 0); 1945 else 1946 tcpm_set_state(port, AMS_START, 0); 1947 break; 1948 default: 1949 if (port->negotiated_rev >= PD_REV30) 1950 tcpm_set_state(port, SRC_READY, 1951 cc_req == SINK_TX_OK ? 1952 PD_T_SINK_TX : 0); 1953 else 1954 tcpm_set_state(port, SRC_READY, 0); 1955 break; 1956 } 1957 } else { 1958 if (port->negotiated_rev >= PD_REV30 && 1959 !tcpm_sink_tx_ok(port) && 1960 ams != SOFT_RESET_AMS && 1961 ams != HARD_RESET) { 1962 port->upcoming_state = INVALID_STATE; 1963 tcpm_log(port, "Sink TX No Go"); 1964 return -EAGAIN; 1965 } 1966 1967 port->ams = ams; 1968 1969 if (ams == HARD_RESET) { 1970 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL); 1971 tcpm_set_state(port, HARD_RESET_START, 0); 1972 return ret; 1973 } else if (tcpm_vdm_ams(port)) { 1974 return ret; 1975 } 1976 1977 if (port->state == SNK_READY || 1978 port->state == SNK_SOFT_RESET) 1979 tcpm_set_state(port, AMS_START, 0); 1980 else 1981 tcpm_set_state(port, SNK_READY, 0); 1982 } 1983 1984 return ret; 1985 } 1986 1987 /* 1988 * VDM/VDO handling functions 1989 */ 1990 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header, 1991 const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type) 1992 { 1993 u32 vdo_hdr = port->vdo_data[0]; 1994 1995 WARN_ON(!mutex_is_locked(&port->lock)); 1996 1997 /* If is sending discover_identity, handle received message first */ 1998 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMD(vdo_hdr) == CMD_DISCOVER_IDENT) 1999 mod_vdm_discovery_delayed_work(port, SEND_DISCOVERY_VDM_RETRY_MS); 2000 else 2001 /* Make sure we are not still processing a previous VDM packet */ 2002 WARN_ON(port->vdm_state > VDM_STATE_DONE); 2003 2004 port->vdo_count = cnt + 1; 2005 port->vdo_data[0] = header; 2006 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt); 2007 /* Set ready, vdm state machine will actually send */ 2008 port->vdm_retries = 0; 2009 port->vdm_state = VDM_STATE_READY; 2010 port->vdm_sm_running = true; 2011 2012 port->tx_sop_type = tx_sop_type; 2013 2014 mod_vdm_delayed_work(port, 0); 2015 } 2016 2017 static void tcpm_queue_vdm_work(struct kthread_work *work) 2018 { 2019 struct altmode_vdm_event *event = container_of(work, 2020 struct altmode_vdm_event, 2021 work); 2022 struct tcpm_port *port = event->port; 2023 2024 mutex_lock(&port->lock); 2025 if (!tcpm_can_send_vdm(port->state)) { 2026 tcpm_log_force(port, "dropping altmode_vdm_event"); 2027 goto port_unlock; 2028 } 2029 2030 tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type); 2031 2032 port_unlock: 2033 kfree(event); 2034 mutex_unlock(&port->lock); 2035 } 2036 2037 static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header, 2038 const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type) 2039 { 2040 struct altmode_vdm_event *event; 2041 int ret = -ENOMEM; 2042 2043 event = kzalloc_flex(*event, data, cnt); 2044 if (!event) 2045 goto err_event; 2046 2047 kthread_init_work(&event->work, tcpm_queue_vdm_work); 2048 event->cnt = cnt; 2049 event->port = port; 2050 event->header = header; 2051 memcpy(event->data, data, sizeof(u32) * cnt); 2052 event->tx_sop_type = tx_sop_type; 2053 2054 ret = kthread_queue_work(port->wq, &event->work); 2055 if (!ret) { 2056 ret = -EBUSY; 2057 goto err_data; 2058 } 2059 2060 return 0; 2061 2062 err_data: 2063 kfree(event); 2064 err_event: 2065 tcpm_log_force(port, "failed to queue altmode vdm, err:%d", ret); 2066 return ret; 2067 } 2068 2069 static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) 2070 { 2071 u32 vdo = p[VDO_INDEX_IDH]; 2072 u32 product = p[VDO_INDEX_PRODUCT]; 2073 2074 if (cnt <= VDO_INDEX_PRODUCT) 2075 return; 2076 2077 memset(&port->mode_data, 0, sizeof(port->mode_data)); 2078 2079 port->partner_ident.id_header = vdo; 2080 port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT]; 2081 port->partner_ident.product = product; 2082 2083 if (port->partner) 2084 typec_partner_set_identity(port->partner); 2085 2086 tcpm_log(port, "Identity: %04x:%04x.%04x", 2087 PD_IDH_VID(vdo), 2088 PD_PRODUCT_PID(product), product & 0xffff); 2089 } 2090 2091 static void svdm_consume_identity_sop_prime(struct tcpm_port *port, const u32 *p, int cnt) 2092 { 2093 u32 idh = p[VDO_INDEX_IDH]; 2094 u32 product = p[VDO_INDEX_PRODUCT]; 2095 int svdm_version; 2096 2097 if (cnt <= VDO_INDEX_CABLE_1) 2098 return; 2099 2100 /* 2101 * Attempt to consume identity only if cable currently is not set 2102 */ 2103 if (!IS_ERR_OR_NULL(port->cable)) 2104 goto register_plug; 2105 2106 /* Reset cable identity */ 2107 memset(&port->cable_ident, 0, sizeof(port->cable_ident)); 2108 2109 /* Fill out id header, cert, product, cable VDO 1 */ 2110 port->cable_ident.id_header = idh; 2111 port->cable_ident.cert_stat = p[VDO_INDEX_CSTAT]; 2112 port->cable_ident.product = product; 2113 port->cable_ident.vdo[0] = p[VDO_INDEX_CABLE_1]; 2114 2115 /* Fill out cable desc, infer svdm_version from pd revision */ 2116 port->cable_desc.type = (enum typec_plug_type) (VDO_TYPEC_CABLE_TYPE(p[VDO_INDEX_CABLE_1]) + 2117 USB_PLUG_TYPE_A); 2118 port->cable_desc.active = PD_IDH_PTYPE(idh) == IDH_PTYPE_ACABLE ? 1 : 0; 2119 /* Log PD Revision and additional cable VDO from negotiated revision */ 2120 switch (port->negotiated_rev_prime) { 2121 case PD_REV30: 2122 port->cable_desc.pd_revision = 0x0300; 2123 if (port->cable_desc.active && cnt > VDO_INDEX_CABLE_2) 2124 port->cable_ident.vdo[1] = p[VDO_INDEX_CABLE_2]; 2125 break; 2126 case PD_REV20: 2127 port->cable_desc.pd_revision = 0x0200; 2128 break; 2129 default: 2130 port->cable_desc.pd_revision = 0x0200; 2131 break; 2132 } 2133 port->cable_desc.identity = &port->cable_ident; 2134 /* Register Cable, set identity and svdm_version */ 2135 port->cable = typec_register_cable(port->typec_port, &port->cable_desc); 2136 if (IS_ERR_OR_NULL(port->cable)) 2137 return; 2138 typec_cable_set_identity(port->cable); 2139 /* Get SVDM version */ 2140 svdm_version = PD_VDO_SVDM_VER(p[VDO_INDEX_HDR]); 2141 typec_cable_set_svdm_version(port->cable, svdm_version); 2142 2143 register_plug: 2144 if (IS_ERR_OR_NULL(port->plug_prime)) { 2145 port->plug_prime_desc.index = TYPEC_PLUG_SOP_P; 2146 port->plug_prime = typec_register_plug(port->cable, 2147 &port->plug_prime_desc); 2148 } 2149 } 2150 2151 static bool svdm_consume_svids(struct tcpm_port *port, const u32 *p, int cnt, 2152 enum tcpm_transmit_type rx_sop_type) 2153 { 2154 struct pd_mode_data *pmdata = rx_sop_type == TCPC_TX_SOP_PRIME ? 2155 &port->mode_data_prime : &port->mode_data; 2156 int i; 2157 2158 for (i = 1; i < cnt; i++) { 2159 u16 svid; 2160 2161 svid = (p[i] >> 16) & 0xffff; 2162 if (!svid) 2163 return false; 2164 2165 if (pmdata->nsvids >= SVID_DISCOVERY_MAX) 2166 goto abort; 2167 2168 pmdata->svids[pmdata->nsvids++] = svid; 2169 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); 2170 2171 svid = p[i] & 0xffff; 2172 if (!svid) 2173 return false; 2174 2175 if (pmdata->nsvids >= SVID_DISCOVERY_MAX) 2176 goto abort; 2177 2178 pmdata->svids[pmdata->nsvids++] = svid; 2179 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); 2180 } 2181 2182 /* 2183 * PD3.0 Spec 6.4.4.3.2: The SVIDs are returned 2 per VDO (see Table 2184 * 6-43), and can be returned maximum 6 VDOs per response (see Figure 2185 * 6-19). If the Responder supports 12 or more SVID then the Discover 2186 * SVIDs Command Shall be executed multiple times until a Discover 2187 * SVIDs VDO is returned ending either with a SVID value of 0x0000 in 2188 * the last part of the last VDO or with a VDO containing two SVIDs 2189 * with values of 0x0000. 2190 * 2191 * However, some odd dockers support SVIDs less than 12 but without 2192 * 0x0000 in the last VDO, so we need to break the Discover SVIDs 2193 * request and return false here. 2194 */ 2195 return cnt == 7; 2196 abort: 2197 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX); 2198 return false; 2199 } 2200 2201 static void svdm_consume_modes(struct tcpm_port *port, const u32 *p, int cnt, 2202 enum tcpm_transmit_type rx_sop_type) 2203 { 2204 struct pd_mode_data *pmdata = &port->mode_data; 2205 struct typec_altmode_desc *paltmode; 2206 int i; 2207 2208 switch (rx_sop_type) { 2209 case TCPC_TX_SOP_PRIME: 2210 pmdata = &port->mode_data_prime; 2211 break; 2212 case TCPC_TX_SOP: 2213 pmdata = &port->mode_data; 2214 break; 2215 default: 2216 return; 2217 } 2218 2219 if (pmdata->svid_index < 0 || pmdata->svid_index >= pmdata->nsvids) { 2220 tcpm_log(port, "Invalid SVID index %d", pmdata->svid_index); 2221 return; 2222 } 2223 2224 for (i = 1; i < cnt; i++) { 2225 if (pmdata->altmodes >= ALTMODE_DISCOVERY_MAX) { 2226 /* Already logged in svdm_consume_svids() */ 2227 return; 2228 } 2229 paltmode = &pmdata->altmode_desc[pmdata->altmodes]; 2230 memset(paltmode, 0, sizeof(*paltmode)); 2231 2232 paltmode->svid = pmdata->svids[pmdata->svid_index]; 2233 paltmode->mode = i; 2234 paltmode->vdo = p[i]; 2235 2236 tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x", 2237 pmdata->altmodes, paltmode->svid, 2238 paltmode->mode, paltmode->vdo); 2239 2240 pmdata->altmodes++; 2241 } 2242 } 2243 2244 static void tcpm_register_partner_altmodes(struct tcpm_port *port) 2245 { 2246 struct pd_mode_data *modep = &port->mode_data; 2247 struct typec_altmode *altmode; 2248 int i; 2249 2250 if (!port->partner) 2251 return; 2252 2253 for (i = 0; i < modep->altmodes && i < ALTMODE_DISCOVERY_MAX; i++) { 2254 altmode = typec_partner_register_altmode(port->partner, 2255 &modep->altmode_desc[i]); 2256 if (IS_ERR(altmode)) { 2257 tcpm_log(port, "Failed to register partner SVID 0x%04x", 2258 modep->altmode_desc[i].svid); 2259 altmode = NULL; 2260 } 2261 port->partner_altmode[i] = altmode; 2262 } 2263 } 2264 2265 static void tcpm_register_plug_altmodes(struct tcpm_port *port) 2266 { 2267 struct pd_mode_data *modep = &port->mode_data_prime; 2268 struct typec_altmode *altmode; 2269 int i; 2270 2271 typec_plug_set_num_altmodes(port->plug_prime, 2272 min(modep->altmodes, ALTMODE_DISCOVERY_MAX)); 2273 2274 for (i = 0; i < modep->altmodes && i < ALTMODE_DISCOVERY_MAX; i++) { 2275 altmode = typec_plug_register_altmode(port->plug_prime, 2276 &modep->altmode_desc[i]); 2277 if (IS_ERR(altmode)) { 2278 tcpm_log(port, "Failed to register plug SVID 0x%04x", 2279 modep->altmode_desc[i].svid); 2280 altmode = NULL; 2281 } 2282 port->plug_prime_altmode[i] = altmode; 2283 } 2284 } 2285 2286 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header) 2287 #define supports_modal_cable(port) PD_IDH_MODAL_SUPP((port)->cable_ident.id_header) 2288 #define supports_host(port) PD_IDH_HOST_SUPP((port->partner_ident.id_header)) 2289 2290 /* 2291 * Helper to determine whether the port is capable of SOP' communication at the 2292 * current point in time. 2293 */ 2294 static bool tcpm_can_communicate_sop_prime(struct tcpm_port *port) 2295 { 2296 /* Check to see if tcpc supports SOP' communication */ 2297 if (!port->tcpc->cable_comm_capable || !port->tcpc->cable_comm_capable(port->tcpc)) 2298 return false; 2299 /* 2300 * Power Delivery 2.0 Section 6.3.11 2301 * Before communicating with a Cable Plug a Port Should ensure that it 2302 * is the Vconn Source and that the Cable Plugs are powered by 2303 * performing a Vconn swap if necessary. Since it cannot be guaranteed 2304 * that the present Vconn Source is supplying Vconn, the only means to 2305 * ensure that the Cable Plugs are powered is for a Port wishing to 2306 * communicate with a Cable Plug is to become the Vconn Source. 2307 * 2308 * Power Delivery 3.0 Section 6.3.11 2309 * Before communicating with a Cable Plug a Port Shall ensure that it 2310 * is the Vconn source. 2311 */ 2312 if (port->vconn_role != TYPEC_SOURCE) 2313 return false; 2314 /* 2315 * Power Delivery 2.0 Section 2.4.4 2316 * When no Contract or an Implicit Contract is in place the Source can 2317 * communicate with a Cable Plug using SOP' packets in order to discover 2318 * its characteristics. 2319 * 2320 * Power Delivery 3.0 Section 2.4.4 2321 * When no Contract or an Implicit Contract is in place only the Source 2322 * port that is supplying Vconn is allowed to send packets to a Cable 2323 * Plug and is allowed to respond to packets from the Cable Plug. 2324 */ 2325 if (!port->explicit_contract) 2326 return port->pwr_role == TYPEC_SOURCE; 2327 if (port->negotiated_rev == PD_REV30) 2328 return true; 2329 /* 2330 * Power Delivery 2.0 Section 2.4.4 2331 * 2332 * When an Explicit Contract is in place the DFP (either the Source or 2333 * the Sink) can communicate with the Cable Plug(s) using SOP’/SOP” 2334 * Packets (see Figure 2-3). 2335 */ 2336 if (port->negotiated_rev == PD_REV20) 2337 return port->data_role == TYPEC_HOST; 2338 return false; 2339 } 2340 2341 static bool tcpm_attempt_vconn_swap_discovery(struct tcpm_port *port) 2342 { 2343 if (!port->tcpc->attempt_vconn_swap_discovery) 2344 return false; 2345 2346 /* Port is already source, no need to perform swap */ 2347 if (port->vconn_role == TYPEC_SOURCE) 2348 return false; 2349 2350 /* 2351 * Partner needs to support Alternate Modes with modal support. If 2352 * partner is also capable of being a USB Host, it could be a device 2353 * that supports Alternate Modes as the DFP. 2354 */ 2355 if (!supports_modal(port) || supports_host(port)) 2356 return false; 2357 2358 if ((port->negotiated_rev == PD_REV20 && port->data_role == TYPEC_HOST) || 2359 port->negotiated_rev == PD_REV30) 2360 return port->tcpc->attempt_vconn_swap_discovery(port->tcpc); 2361 2362 return false; 2363 } 2364 2365 2366 static bool tcpm_cable_vdm_supported(struct tcpm_port *port) 2367 { 2368 return !IS_ERR_OR_NULL(port->cable) && 2369 typec_cable_is_active(port->cable) && 2370 supports_modal_cable(port) && 2371 tcpm_can_communicate_sop_prime(port); 2372 } 2373 2374 static void tcpm_update_vdm_discovery_state(struct tcpm_port *port, 2375 enum vdm_discovery_states new_state) 2376 { 2377 enum vdm_discovery_states old_state = port->vdm_discovery_state; 2378 2379 if (old_state != new_state) 2380 tcpm_log_force(port, "vdm discovery state changed: %s -> %s", 2381 vdm_discovery_state_strings[old_state], 2382 vdm_discovery_state_strings[new_state]); 2383 2384 port->vdm_discovery_state = new_state; 2385 } 2386 2387 static int tcpm_handle_discover_mode(struct tcpm_port *port, u32 *response, 2388 enum tcpm_transmit_type rx_sop_type, 2389 enum tcpm_transmit_type *response_tx_sop_type) 2390 { 2391 struct typec_port *typec = port->typec_port; 2392 struct pd_mode_data *modep; 2393 2394 if (rx_sop_type == TCPC_TX_SOP) { 2395 modep = &port->mode_data; 2396 modep->svid_index++; 2397 2398 if (modep->svid_index < modep->nsvids) { 2399 u16 svid = modep->svids[modep->svid_index]; 2400 *response_tx_sop_type = TCPC_TX_SOP; 2401 response[0] = VDO(svid, 1, 2402 typec_get_negotiated_svdm_version(typec), 2403 CMD_DISCOVER_MODES); 2404 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_PARTNER_MODES); 2405 return 1; 2406 } 2407 2408 if (tcpm_cable_vdm_supported(port)) { 2409 *response_tx_sop_type = TCPC_TX_SOP_PRIME; 2410 response[0] = VDO(USB_SID_PD, 1, 2411 typec_get_cable_svdm_version(typec), 2412 CMD_DISCOVER_SVID); 2413 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_PARTNER_MODES); 2414 return 1; 2415 } 2416 2417 tcpm_register_partner_altmodes(port); 2418 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 2419 } else if (rx_sop_type == TCPC_TX_SOP_PRIME) { 2420 modep = &port->mode_data_prime; 2421 modep->svid_index++; 2422 2423 if (modep->svid_index < modep->nsvids) { 2424 u16 svid = modep->svids[modep->svid_index]; 2425 *response_tx_sop_type = TCPC_TX_SOP_PRIME; 2426 response[0] = VDO(svid, 1, 2427 typec_get_cable_svdm_version(typec), 2428 CMD_DISCOVER_MODES); 2429 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_CABLE_MODES); 2430 return 1; 2431 } 2432 2433 tcpm_register_plug_altmodes(port); 2434 tcpm_register_partner_altmodes(port); 2435 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 2436 } 2437 2438 return 0; 2439 } 2440 2441 static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev, 2442 const u32 *p, int cnt, u32 *response, 2443 enum adev_actions *adev_action, 2444 enum tcpm_transmit_type rx_sop_type, 2445 enum tcpm_transmit_type *response_tx_sop_type) 2446 { 2447 struct typec_port *typec = port->typec_port; 2448 struct typec_altmode *pdev, *pdev_prime; 2449 struct pd_mode_data *modep, *modep_prime; 2450 int svdm_version; 2451 int rlen = 0; 2452 int cmd_type; 2453 int cmd; 2454 int i; 2455 int ret; 2456 2457 cmd_type = PD_VDO_CMDT(p[0]); 2458 cmd = PD_VDO_CMD(p[0]); 2459 2460 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d", 2461 p[0], cmd_type, cmd, cnt); 2462 2463 switch (rx_sop_type) { 2464 case TCPC_TX_SOP_PRIME: 2465 modep_prime = &port->mode_data_prime; 2466 pdev_prime = typec_match_altmode(port->plug_prime_altmode, 2467 ALTMODE_DISCOVERY_MAX, 2468 PD_VDO_VID(p[0]), 2469 PD_VDO_OPOS(p[0])); 2470 svdm_version = typec_get_cable_svdm_version(typec); 2471 /* 2472 * Update SVDM version if cable was discovered before port partner. 2473 */ 2474 if (!IS_ERR_OR_NULL(port->cable) && 2475 PD_VDO_SVDM_VER(p[0]) < svdm_version) 2476 typec_cable_set_svdm_version(port->cable, svdm_version); 2477 break; 2478 case TCPC_TX_SOP: 2479 modep = &port->mode_data; 2480 pdev = typec_match_altmode(port->partner_altmode, 2481 ALTMODE_DISCOVERY_MAX, 2482 PD_VDO_VID(p[0]), 2483 PD_VDO_OPOS(p[0])); 2484 svdm_version = typec_get_negotiated_svdm_version(typec); 2485 if (svdm_version < 0) 2486 return 0; 2487 break; 2488 default: 2489 modep = &port->mode_data; 2490 pdev = typec_match_altmode(port->partner_altmode, 2491 ALTMODE_DISCOVERY_MAX, 2492 PD_VDO_VID(p[0]), 2493 PD_VDO_OPOS(p[0])); 2494 svdm_version = typec_get_negotiated_svdm_version(typec); 2495 if (svdm_version < 0) 2496 return 0; 2497 break; 2498 } 2499 2500 switch (cmd_type) { 2501 case CMDT_INIT: 2502 /* 2503 * Only the port or port partner is allowed to initialize SVDM 2504 * commands over SOP'. In case the port partner initializes a 2505 * sequence when it is not allowed to send SOP' messages, drop 2506 * the message should the TCPM port try to process it. 2507 */ 2508 if (rx_sop_type == TCPC_TX_SOP_PRIME) 2509 return 0; 2510 2511 switch (cmd) { 2512 case CMD_DISCOVER_IDENT: 2513 if (PD_VDO_VID(p[0]) != USB_SID_PD) 2514 break; 2515 2516 if (IS_ERR_OR_NULL(port->partner)) 2517 break; 2518 2519 if (PD_VDO_SVDM_VER(p[0]) < svdm_version) { 2520 typec_partner_set_svdm_version(port->partner, 2521 PD_VDO_SVDM_VER(p[0])); 2522 svdm_version = PD_VDO_SVDM_VER(p[0]); 2523 } 2524 2525 port->ams = DISCOVER_IDENTITY; 2526 /* 2527 * PD2.0 Spec 6.10.3: respond with NAK as DFP (data host) 2528 * PD3.1 Spec 6.4.4.2.5.1: respond with NAK if "invalid field" or 2529 * "wrong configuation" or "Unrecognized" 2530 */ 2531 if ((port->data_role == TYPEC_DEVICE || svdm_version >= SVDM_VER_2_0) && 2532 port->nr_snk_vdo) { 2533 if (svdm_version < SVDM_VER_2_0) { 2534 for (i = 0; i < port->nr_snk_vdo_v1; i++) 2535 response[i + 1] = port->snk_vdo_v1[i]; 2536 rlen = port->nr_snk_vdo_v1 + 1; 2537 2538 } else { 2539 for (i = 0; i < port->nr_snk_vdo; i++) 2540 response[i + 1] = port->snk_vdo[i]; 2541 rlen = port->nr_snk_vdo + 1; 2542 } 2543 } 2544 break; 2545 case CMD_DISCOVER_SVID: 2546 port->ams = DISCOVER_SVIDS; 2547 break; 2548 case CMD_DISCOVER_MODES: 2549 port->ams = DISCOVER_MODES; 2550 break; 2551 case CMD_ENTER_MODE: 2552 port->ams = DFP_TO_UFP_ENTER_MODE; 2553 break; 2554 case CMD_EXIT_MODE: 2555 port->ams = DFP_TO_UFP_EXIT_MODE; 2556 break; 2557 case CMD_ATTENTION: 2558 /* Attention command does not have response */ 2559 *adev_action = ADEV_ATTENTION; 2560 return 0; 2561 default: 2562 break; 2563 } 2564 if (rlen >= 1) { 2565 response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK); 2566 } else if (rlen == 0) { 2567 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); 2568 rlen = 1; 2569 } else { 2570 response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY); 2571 rlen = 1; 2572 } 2573 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | 2574 (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec))); 2575 break; 2576 case CMDT_RSP_ACK: 2577 /* 2578 * Silently drop message if we are not connected, but can process 2579 * if SOP' Discover Identity prior to explicit contract. 2580 */ 2581 if (IS_ERR_OR_NULL(port->partner) && 2582 !(rx_sop_type == TCPC_TX_SOP_PRIME && cmd == CMD_DISCOVER_IDENT)) 2583 break; 2584 2585 tcpm_ams_finish(port); 2586 2587 switch (cmd) { 2588 /* 2589 * SVDM Command Flow for SOP and SOP': 2590 * SOP Discover Identity 2591 * SOP' Discover Identity 2592 * SOP Discover SVIDs 2593 * Discover Modes 2594 * (Active Cables) 2595 * SOP' Discover SVIDs 2596 * Discover Modes 2597 * 2598 * Perform Discover SOP' if the port can communicate with cable 2599 * plug. 2600 */ 2601 case CMD_DISCOVER_IDENT: 2602 switch (rx_sop_type) { 2603 case TCPC_TX_SOP: 2604 if (PD_VDO_SVDM_VER(p[0]) < svdm_version) { 2605 typec_partner_set_svdm_version(port->partner, 2606 PD_VDO_SVDM_VER(p[0])); 2607 /* If cable is discovered before partner, downgrade svdm */ 2608 if (!IS_ERR_OR_NULL(port->cable) && 2609 (typec_get_cable_svdm_version(port->typec_port) > 2610 svdm_version)) 2611 typec_cable_set_svdm_version(port->cable, 2612 svdm_version); 2613 } 2614 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_PARTNER_IDENT); 2615 2616 /* 6.4.4.3.1 */ 2617 svdm_consume_identity(port, p, cnt); 2618 /* Attempt Vconn swap, delay SOP' discovery if necessary */ 2619 if (tcpm_attempt_vconn_swap_discovery(port)) { 2620 port->upcoming_state = VCONN_SWAP_SEND; 2621 ret = tcpm_ams_start(port, VCONN_SWAP); 2622 if (!ret) 2623 return 0; 2624 /* Cannot perform Vconn swap */ 2625 port->upcoming_state = INVALID_STATE; 2626 } 2627 2628 /* 2629 * Attempt Discover Identity on SOP' if the 2630 * cable was not discovered previously, and use 2631 * the SVDM version of the partner to probe. 2632 */ 2633 if (IS_ERR_OR_NULL(port->cable) && 2634 tcpm_can_communicate_sop_prime(port)) { 2635 *response_tx_sop_type = TCPC_TX_SOP_PRIME; 2636 response[0] = VDO(USB_SID_PD, 1, 2637 typec_get_negotiated_svdm_version(typec), 2638 CMD_DISCOVER_IDENT); 2639 rlen = 1; 2640 } else { 2641 *response_tx_sop_type = TCPC_TX_SOP; 2642 response[0] = VDO(USB_SID_PD, 1, 2643 typec_get_negotiated_svdm_version(typec), 2644 CMD_DISCOVER_SVID); 2645 rlen = 1; 2646 } 2647 break; 2648 case TCPC_TX_SOP_PRIME: 2649 /* 2650 * svdm_consume_identity_sop_prime will determine 2651 * the svdm_version for the cable moving forward. 2652 */ 2653 svdm_consume_identity_sop_prime(port, p, cnt); 2654 2655 /* 2656 * If received in SRC_VDM_IDENTITY_REQUEST, continue 2657 * to SRC_SEND_CAPABILITIES 2658 */ 2659 if (port->state == SRC_VDM_IDENTITY_REQUEST) { 2660 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); 2661 return 0; 2662 } 2663 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_CABLE_IDENT); 2664 2665 *response_tx_sop_type = TCPC_TX_SOP; 2666 response[0] = VDO(USB_SID_PD, 1, 2667 typec_get_negotiated_svdm_version(typec), 2668 CMD_DISCOVER_SVID); 2669 rlen = 1; 2670 break; 2671 default: 2672 return 0; 2673 } 2674 break; 2675 case CMD_DISCOVER_SVID: 2676 *response_tx_sop_type = rx_sop_type; 2677 /* 6.4.4.3.2 */ 2678 if (svdm_consume_svids(port, p, cnt, rx_sop_type)) { 2679 response[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID); 2680 rlen = 1; 2681 } else { 2682 if (rx_sop_type == TCPC_TX_SOP) { 2683 tcpm_update_vdm_discovery_state(port, 2684 VDM_DISCOVERY_PARTNER_SVIDS); 2685 if (modep->nsvids && supports_modal(port)) { 2686 response[0] = VDO(modep->svids[0], 1, svdm_version, 2687 CMD_DISCOVER_MODES); 2688 rlen = 1; 2689 } else { 2690 tcpm_update_vdm_discovery_state(port, 2691 VDM_DISCOVERY_COMPLETE); 2692 } 2693 } else if (rx_sop_type == TCPC_TX_SOP_PRIME) { 2694 tcpm_update_vdm_discovery_state(port, 2695 VDM_DISCOVERY_CABLE_SVIDS); 2696 if (modep_prime->nsvids) { 2697 response[0] = VDO(modep_prime->svids[0], 1, 2698 svdm_version, CMD_DISCOVER_MODES); 2699 rlen = 1; 2700 } else { 2701 tcpm_register_partner_altmodes(port); 2702 tcpm_update_vdm_discovery_state(port, 2703 VDM_DISCOVERY_COMPLETE); 2704 } 2705 } 2706 } 2707 break; 2708 case CMD_DISCOVER_MODES: 2709 /* 6.4.4.3.3 */ 2710 svdm_consume_modes(port, p, cnt, rx_sop_type); 2711 rlen = tcpm_handle_discover_mode(port, response, 2712 rx_sop_type, 2713 response_tx_sop_type); 2714 break; 2715 case CMD_ENTER_MODE: 2716 *response_tx_sop_type = rx_sop_type; 2717 if (rx_sop_type == TCPC_TX_SOP) { 2718 if (adev && pdev) { 2719 typec_altmode_update_active(pdev, true); 2720 *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL; 2721 } 2722 } else if (rx_sop_type == TCPC_TX_SOP_PRIME) { 2723 if (adev && pdev_prime) { 2724 typec_altmode_update_active(pdev_prime, true); 2725 *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL; 2726 } 2727 } 2728 return 0; 2729 case CMD_EXIT_MODE: 2730 *response_tx_sop_type = rx_sop_type; 2731 if (rx_sop_type == TCPC_TX_SOP) { 2732 if (adev && pdev) { 2733 typec_altmode_update_active(pdev, false); 2734 /* Back to USB Operation */ 2735 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; 2736 return 0; 2737 } 2738 } 2739 break; 2740 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): 2741 break; 2742 default: 2743 /* Unrecognized SVDM */ 2744 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); 2745 rlen = 1; 2746 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | 2747 (VDO_SVDM_VERS(svdm_version)); 2748 break; 2749 } 2750 break; 2751 case CMDT_RSP_NAK: 2752 tcpm_ams_finish(port); 2753 switch (cmd) { 2754 /* 2755 * The cable is not allowed to respond with NAK so this must've happened over SOP 2756 */ 2757 case CMD_DISCOVER_IDENT: 2758 case CMD_DISCOVER_SVID: 2759 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 2760 break; 2761 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): 2762 break; 2763 case CMD_DISCOVER_MODES: 2764 tcpm_log(port, "Skip SVID 0x%04x (failed to discover mode)", 2765 PD_VDO_SVID_SVID0(p[0])); 2766 rlen = tcpm_handle_discover_mode(port, response, 2767 rx_sop_type, 2768 response_tx_sop_type); 2769 break; 2770 case CMD_ENTER_MODE: 2771 /* Back to USB Operation */ 2772 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; 2773 return 0; 2774 default: 2775 /* Unrecognized SVDM */ 2776 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); 2777 rlen = 1; 2778 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | 2779 (VDO_SVDM_VERS(svdm_version)); 2780 break; 2781 } 2782 break; 2783 default: 2784 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); 2785 rlen = 1; 2786 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | 2787 (VDO_SVDM_VERS(svdm_version)); 2788 break; 2789 } 2790 2791 /* Informing the alternate mode drivers about everything */ 2792 *adev_action = ADEV_QUEUE_VDM; 2793 return rlen; 2794 } 2795 2796 static void tcpm_pd_handle_msg(struct tcpm_port *port, 2797 enum pd_msg_request message, 2798 enum tcpm_ams ams); 2799 2800 static void tcpm_handle_vdm_request(struct tcpm_port *port, 2801 const __le32 *payload, int cnt, 2802 enum tcpm_transmit_type rx_sop_type) 2803 { 2804 enum adev_actions adev_action = ADEV_NONE; 2805 struct typec_altmode *adev; 2806 u32 p[PD_MAX_PAYLOAD]; 2807 u32 response[8] = { }; 2808 int i, rlen = 0; 2809 enum tcpm_transmit_type response_tx_sop_type = TCPC_TX_SOP; 2810 2811 for (i = 0; i < cnt; i++) 2812 p[i] = le32_to_cpu(payload[i]); 2813 2814 adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX, 2815 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); 2816 2817 if (port->vdm_state == VDM_STATE_BUSY) { 2818 /* If UFP responded busy retry after timeout */ 2819 if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) { 2820 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY; 2821 port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) | 2822 CMDT_INIT; 2823 mod_vdm_delayed_work(port, PD_T_VDM_BUSY); 2824 return; 2825 } 2826 port->vdm_state = VDM_STATE_DONE; 2827 } 2828 2829 if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) { 2830 /* 2831 * Here a SVDM is received (INIT or RSP or unknown). Set the vdm_sm_running in 2832 * advance because we are dropping the lock but may send VDMs soon. 2833 * For the cases of INIT received: 2834 * - If no response to send, it will be cleared later in this function. 2835 * - If there are responses to send, it will be cleared in the state machine. 2836 * For the cases of RSP received: 2837 * - If no further INIT to send, it will be cleared later in this function. 2838 * - Otherwise, it will be cleared in the state machine if timeout or it will go 2839 * back here until no further INIT to send. 2840 * For the cases of unknown type received: 2841 * - We will send NAK and the flag will be cleared in the state machine. 2842 */ 2843 port->vdm_sm_running = true; 2844 rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action, 2845 rx_sop_type, &response_tx_sop_type); 2846 } else { 2847 if (port->negotiated_rev >= PD_REV30) 2848 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); 2849 } 2850 2851 /* 2852 * We are done with any state stored in the port struct now, except 2853 * for any port struct changes done by the tcpm_queue_vdm() call 2854 * below, which is a separate operation. 2855 * 2856 * So we can safely release the lock here; and we MUST release the 2857 * lock here to avoid an AB BA lock inversion: 2858 * 2859 * If we keep the lock here then the lock ordering in this path is: 2860 * 1. tcpm_pd_rx_handler take the tcpm port lock 2861 * 2. One of the typec_altmode_* calls below takes the alt-mode's lock 2862 * 2863 * And we also have this ordering: 2864 * 1. alt-mode driver takes the alt-mode's lock 2865 * 2. alt-mode driver calls tcpm_altmode_enter which takes the 2866 * tcpm port lock 2867 * 2868 * Dropping our lock here avoids this. 2869 */ 2870 mutex_unlock(&port->lock); 2871 2872 if (adev) { 2873 switch (adev_action) { 2874 case ADEV_NONE: 2875 break; 2876 case ADEV_NOTIFY_USB_AND_QUEUE_VDM: 2877 if (rx_sop_type == TCPC_TX_SOP_PRIME) { 2878 typec_cable_altmode_vdm(adev, TYPEC_PLUG_SOP_P, p[0], &p[1], cnt); 2879 } else { 2880 WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL)); 2881 typec_altmode_vdm(adev, p[0], &p[1], cnt); 2882 } 2883 break; 2884 case ADEV_QUEUE_VDM: 2885 if (rx_sop_type == TCPC_TX_SOP_PRIME) 2886 typec_cable_altmode_vdm(adev, TYPEC_PLUG_SOP_P, p[0], &p[1], cnt); 2887 else 2888 typec_altmode_vdm(adev, p[0], &p[1], cnt); 2889 break; 2890 case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL: 2891 if (rx_sop_type == TCPC_TX_SOP_PRIME) { 2892 if (typec_cable_altmode_vdm(adev, TYPEC_PLUG_SOP_P, 2893 p[0], &p[1], cnt)) { 2894 int svdm_version = typec_get_cable_svdm_version( 2895 port->typec_port); 2896 if (svdm_version < 0) 2897 break; 2898 2899 response[0] = VDO(adev->svid, 1, svdm_version, 2900 CMD_EXIT_MODE); 2901 response[0] |= VDO_OPOS(adev->mode); 2902 rlen = 1; 2903 } 2904 } else { 2905 if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) { 2906 int svdm_version = typec_get_negotiated_svdm_version( 2907 port->typec_port); 2908 if (svdm_version < 0) 2909 break; 2910 2911 response[0] = VDO(adev->svid, 1, svdm_version, 2912 CMD_EXIT_MODE); 2913 response[0] |= VDO_OPOS(adev->mode); 2914 rlen = 1; 2915 } 2916 } 2917 break; 2918 case ADEV_ATTENTION: 2919 if (typec_altmode_attention(adev, p[1])) 2920 tcpm_log(port, "typec_altmode_attention no port partner altmode"); 2921 break; 2922 } 2923 } 2924 2925 /* 2926 * We must re-take the lock here to balance the unlock in 2927 * tcpm_pd_rx_handler, note that no changes, other then the 2928 * tcpm_queue_vdm call, are made while the lock is held again. 2929 * All that is done after the call is unwinding the call stack until 2930 * we return to tcpm_pd_rx_handler and do the unlock there. 2931 */ 2932 mutex_lock(&port->lock); 2933 2934 if (rlen > 0) 2935 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1, response_tx_sop_type); 2936 else 2937 port->vdm_sm_running = false; 2938 } 2939 2940 static unsigned int vdm_ready_timeout(u32 vdm_hdr) 2941 { 2942 unsigned int timeout; 2943 int cmd = PD_VDO_CMD(vdm_hdr); 2944 2945 /* its not a structured VDM command */ 2946 if (!PD_VDO_SVDM(vdm_hdr)) 2947 return PD_T_VDM_UNSTRUCTURED; 2948 2949 switch (PD_VDO_CMDT(vdm_hdr)) { 2950 case CMDT_INIT: 2951 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE) 2952 timeout = PD_T_VDM_WAIT_MODE_E; 2953 else 2954 timeout = PD_T_VDM_SNDR_RSP; 2955 break; 2956 default: 2957 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE) 2958 timeout = PD_T_VDM_E_MODE; 2959 else 2960 timeout = PD_T_VDM_RCVR_RSP; 2961 break; 2962 } 2963 return timeout; 2964 } 2965 2966 static void vdm_run_state_machine(struct tcpm_port *port) 2967 { 2968 struct pd_message msg; 2969 int i, res = 0; 2970 u32 vdo_hdr = port->vdo_data[0]; 2971 u32 response[8] = { }; 2972 2973 switch (port->vdm_state) { 2974 case VDM_STATE_READY: 2975 /* Only transmit VDM if attached */ 2976 if (!port->attached) { 2977 port->vdm_state = VDM_STATE_ERR_BUSY; 2978 break; 2979 } 2980 2981 /* 2982 * if there's traffic or we're not in PDO ready state don't send 2983 * a VDM. 2984 */ 2985 if (!tcpm_can_send_vdm(port->state)) { 2986 port->vdm_sm_running = false; 2987 break; 2988 } 2989 2990 /* TODO: AMS operation for Unstructured VDM */ 2991 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) { 2992 switch (PD_VDO_CMD(vdo_hdr)) { 2993 case CMD_DISCOVER_IDENT: 2994 res = tcpm_ams_start(port, DISCOVER_IDENTITY); 2995 if (res == -EAGAIN) { 2996 port->vdo_data[0] = 0; 2997 mod_vdm_discovery_delayed_work(port, 2998 SEND_DISCOVERY_VDM_RETRY_MS); 2999 } 3000 break; 3001 case CMD_DISCOVER_SVID: 3002 res = tcpm_ams_start(port, DISCOVER_SVIDS); 3003 break; 3004 case CMD_DISCOVER_MODES: 3005 res = tcpm_ams_start(port, DISCOVER_MODES); 3006 break; 3007 case CMD_ENTER_MODE: 3008 res = tcpm_ams_start(port, DFP_TO_UFP_ENTER_MODE); 3009 break; 3010 case CMD_EXIT_MODE: 3011 res = tcpm_ams_start(port, DFP_TO_UFP_EXIT_MODE); 3012 break; 3013 case CMD_ATTENTION: 3014 res = tcpm_ams_start(port, ATTENTION); 3015 break; 3016 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): 3017 res = tcpm_ams_start(port, STRUCTURED_VDMS); 3018 break; 3019 default: 3020 res = -EOPNOTSUPP; 3021 break; 3022 } 3023 3024 if (res < 0) { 3025 port->vdm_state = VDM_STATE_ERR_BUSY; 3026 return; 3027 } 3028 } 3029 3030 port->vdm_state = VDM_STATE_SEND_MESSAGE; 3031 mod_vdm_delayed_work(port, (port->negotiated_rev >= PD_REV30 && 3032 port->pwr_role == TYPEC_SOURCE && 3033 PD_VDO_SVDM(vdo_hdr) && 3034 PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) ? 3035 PD_T_SINK_TX : 0); 3036 break; 3037 case VDM_STATE_WAIT_RSP_BUSY: 3038 port->vdo_data[0] = port->vdo_retry; 3039 port->vdo_count = 1; 3040 port->vdm_state = VDM_STATE_READY; 3041 tcpm_ams_finish(port); 3042 break; 3043 case VDM_STATE_BUSY: 3044 port->vdm_state = VDM_STATE_ERR_TMOUT; 3045 if (port->ams != NONE_AMS) 3046 tcpm_ams_finish(port); 3047 break; 3048 case VDM_STATE_ERR_SEND: 3049 /* 3050 * When sending Discover Identity to SOP' before establishing an 3051 * explicit contract, do not retry. Instead, weave sending 3052 * Source_Capabilities over SOP and Discover Identity over SOP'. 3053 */ 3054 if (port->state == SRC_VDM_IDENTITY_REQUEST) { 3055 tcpm_ams_finish(port); 3056 port->vdo_data[0] = 0; 3057 port->vdm_state = VDM_STATE_DONE; 3058 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); 3059 /* 3060 * A partner which does not support USB PD will not reply, 3061 * so this is not a fatal error. At the same time, some 3062 * devices may not return GoodCRC under some circumstances, 3063 * so we need to retry. 3064 */ 3065 } else if (port->vdm_retries < 3) { 3066 tcpm_log(port, "VDM Tx error, retry"); 3067 port->vdm_retries++; 3068 port->vdm_state = VDM_STATE_READY; 3069 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) 3070 tcpm_ams_finish(port); 3071 } else { 3072 tcpm_ams_finish(port); 3073 port->vdo_data[0] = 0; 3074 if (port->tx_sop_type == TCPC_TX_SOP) 3075 break; 3076 /* Handle SOP' Transmission Errors */ 3077 switch (PD_VDO_CMD(vdo_hdr)) { 3078 /* 3079 * If Discover Identity fails on SOP', then resume 3080 * discovery process on SOP only. 3081 */ 3082 case CMD_DISCOVER_IDENT: 3083 response[0] = VDO(USB_SID_PD, 1, 3084 typec_get_negotiated_svdm_version( 3085 port->typec_port), 3086 CMD_DISCOVER_SVID); 3087 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_CABLE_IDENT); 3088 tcpm_queue_vdm(port, response[0], &response[1], 3089 0, TCPC_TX_SOP); 3090 break; 3091 /* 3092 * If Discover SVIDs or Discover Modes fail, then 3093 * proceed with Alt Mode discovery process on SOP. 3094 */ 3095 case CMD_DISCOVER_SVID: 3096 tcpm_register_partner_altmodes(port); 3097 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 3098 break; 3099 case CMD_DISCOVER_MODES: 3100 tcpm_register_partner_altmodes(port); 3101 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 3102 break; 3103 default: 3104 break; 3105 } 3106 } 3107 break; 3108 case VDM_STATE_SEND_MESSAGE: 3109 /* Prepare and send VDM */ 3110 memset(&msg, 0, sizeof(msg)); 3111 if (port->tx_sop_type == TCPC_TX_SOP_PRIME) { 3112 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF, 3113 0, /* Cable Plug Indicator for DFP/UFP */ 3114 0, /* Reserved */ 3115 port->negotiated_rev_prime, 3116 port->message_id_prime, 3117 port->vdo_count); 3118 } else { 3119 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF, 3120 port->pwr_role, 3121 port->data_role, 3122 port->negotiated_rev, 3123 port->message_id, 3124 port->vdo_count); 3125 } 3126 for (i = 0; i < port->vdo_count; i++) 3127 msg.payload[i] = cpu_to_le32(port->vdo_data[i]); 3128 res = tcpm_pd_transmit(port, port->tx_sop_type, &msg); 3129 if (res < 0) { 3130 port->vdm_state = VDM_STATE_ERR_SEND; 3131 } else { 3132 unsigned long timeout; 3133 3134 port->vdm_retries = 0; 3135 port->vdo_data[0] = 0; 3136 port->vdm_state = VDM_STATE_BUSY; 3137 timeout = vdm_ready_timeout(vdo_hdr); 3138 mod_vdm_delayed_work(port, timeout); 3139 } 3140 break; 3141 default: 3142 break; 3143 } 3144 } 3145 3146 static void vdm_state_machine_work(struct kthread_work *work) 3147 { 3148 struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_state_machine); 3149 enum vdm_states prev_state; 3150 3151 mutex_lock(&port->lock); 3152 3153 /* 3154 * Continue running as long as the port is not busy and there was 3155 * a state change. 3156 */ 3157 do { 3158 prev_state = port->vdm_state; 3159 vdm_run_state_machine(port); 3160 } while (port->vdm_state != prev_state && 3161 port->vdm_state != VDM_STATE_BUSY && 3162 port->vdm_state != VDM_STATE_SEND_MESSAGE); 3163 3164 if (port->vdm_state < VDM_STATE_READY) 3165 port->vdm_sm_running = false; 3166 3167 mutex_unlock(&port->lock); 3168 } 3169 3170 enum pdo_err { 3171 PDO_NO_ERR, 3172 PDO_ERR_NO_VSAFE5V, 3173 PDO_ERR_VSAFE5V_NOT_FIRST, 3174 PDO_ERR_PDO_TYPE_NOT_IN_ORDER, 3175 PDO_ERR_FIXED_NOT_SORTED, 3176 PDO_ERR_VARIABLE_BATT_NOT_SORTED, 3177 PDO_ERR_DUPE_PDO, 3178 PDO_ERR_PPS_APDO_NOT_SORTED, 3179 PDO_ERR_DUPE_PPS_APDO, 3180 }; 3181 3182 static const char * const pdo_err_msg[] = { 3183 [PDO_ERR_NO_VSAFE5V] = 3184 " err: source/sink caps should at least have vSafe5V", 3185 [PDO_ERR_VSAFE5V_NOT_FIRST] = 3186 " err: vSafe5V Fixed Supply Object Shall always be the first object", 3187 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] = 3188 " err: PDOs should be in the following order: Fixed; Battery; Variable", 3189 [PDO_ERR_FIXED_NOT_SORTED] = 3190 " err: Fixed supply pdos should be in increasing order of their fixed voltage", 3191 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] = 3192 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage", 3193 [PDO_ERR_DUPE_PDO] = 3194 " err: Variable/Batt supply pdos cannot have same min/max voltage", 3195 [PDO_ERR_PPS_APDO_NOT_SORTED] = 3196 " err: Programmable power supply apdos should be in increasing order of their maximum voltage", 3197 [PDO_ERR_DUPE_PPS_APDO] = 3198 " err: Programmable power supply apdos cannot have same min/max voltage and max current", 3199 }; 3200 3201 static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo, 3202 unsigned int nr_pdo) 3203 { 3204 unsigned int i; 3205 3206 /* Should at least contain vSafe5v */ 3207 if (nr_pdo < 1) 3208 return PDO_ERR_NO_VSAFE5V; 3209 3210 /* The vSafe5V Fixed Supply Object Shall always be the first object */ 3211 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED || 3212 pdo_fixed_voltage(pdo[0]) != VSAFE5V) 3213 return PDO_ERR_VSAFE5V_NOT_FIRST; 3214 3215 for (i = 1; i < nr_pdo; i++) { 3216 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) { 3217 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER; 3218 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) { 3219 enum pd_pdo_type type = pdo_type(pdo[i]); 3220 3221 switch (type) { 3222 /* 3223 * The remaining Fixed Supply Objects, if 3224 * present, shall be sent in voltage order; 3225 * lowest to highest. 3226 */ 3227 case PDO_TYPE_FIXED: 3228 if (pdo_fixed_voltage(pdo[i]) <= 3229 pdo_fixed_voltage(pdo[i - 1])) 3230 return PDO_ERR_FIXED_NOT_SORTED; 3231 break; 3232 /* 3233 * The Battery Supply Objects and Variable 3234 * supply, if present shall be sent in Minimum 3235 * Voltage order; lowest to highest. 3236 */ 3237 case PDO_TYPE_VAR: 3238 case PDO_TYPE_BATT: 3239 if (pdo_min_voltage(pdo[i]) < 3240 pdo_min_voltage(pdo[i - 1])) 3241 return PDO_ERR_VARIABLE_BATT_NOT_SORTED; 3242 else if ((pdo_min_voltage(pdo[i]) == 3243 pdo_min_voltage(pdo[i - 1])) && 3244 (pdo_max_voltage(pdo[i]) == 3245 pdo_max_voltage(pdo[i - 1]))) 3246 return PDO_ERR_DUPE_PDO; 3247 break; 3248 /* 3249 * The Programmable Power Supply APDOs, if present, 3250 * shall be sent in Maximum Voltage order; 3251 * lowest to highest. 3252 */ 3253 case PDO_TYPE_APDO: 3254 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS) 3255 break; 3256 3257 if (pdo_pps_apdo_max_voltage(pdo[i]) < 3258 pdo_pps_apdo_max_voltage(pdo[i - 1])) 3259 return PDO_ERR_PPS_APDO_NOT_SORTED; 3260 else if (pdo_pps_apdo_min_voltage(pdo[i]) == 3261 pdo_pps_apdo_min_voltage(pdo[i - 1]) && 3262 pdo_pps_apdo_max_voltage(pdo[i]) == 3263 pdo_pps_apdo_max_voltage(pdo[i - 1]) && 3264 pdo_pps_apdo_max_current(pdo[i]) == 3265 pdo_pps_apdo_max_current(pdo[i - 1])) 3266 return PDO_ERR_DUPE_PPS_APDO; 3267 break; 3268 default: 3269 tcpm_log_force(port, " Unknown pdo type"); 3270 } 3271 } 3272 } 3273 3274 return PDO_NO_ERR; 3275 } 3276 3277 static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo, 3278 unsigned int nr_pdo) 3279 { 3280 enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo); 3281 3282 if (err_index != PDO_NO_ERR) { 3283 tcpm_log_force(port, " %s", pdo_err_msg[err_index]); 3284 return -EINVAL; 3285 } 3286 3287 return 0; 3288 } 3289 3290 static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo) 3291 { 3292 struct tcpm_port *port = typec_altmode_get_drvdata(altmode); 3293 int svdm_version; 3294 u32 header; 3295 3296 svdm_version = typec_get_negotiated_svdm_version(port->typec_port); 3297 if (svdm_version < 0) 3298 return svdm_version; 3299 3300 header = VDO(altmode->svid, 1, svdm_version, CMD_ENTER_MODE); 3301 header |= VDO_OPOS(altmode->mode); 3302 3303 return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP); 3304 } 3305 3306 static int tcpm_altmode_exit(struct typec_altmode *altmode) 3307 { 3308 struct tcpm_port *port = typec_altmode_get_drvdata(altmode); 3309 int svdm_version; 3310 u32 header; 3311 3312 svdm_version = typec_get_negotiated_svdm_version(port->typec_port); 3313 if (svdm_version < 0) 3314 return svdm_version; 3315 3316 header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE); 3317 header |= VDO_OPOS(altmode->mode); 3318 3319 return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP); 3320 } 3321 3322 static int tcpm_altmode_vdm(struct typec_altmode *altmode, 3323 u32 header, const u32 *data, int count) 3324 { 3325 struct tcpm_port *port = typec_altmode_get_drvdata(altmode); 3326 3327 return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP); 3328 } 3329 3330 static const struct typec_altmode_ops tcpm_altmode_ops = { 3331 .enter = tcpm_altmode_enter, 3332 .exit = tcpm_altmode_exit, 3333 .vdm = tcpm_altmode_vdm, 3334 }; 3335 3336 3337 static int tcpm_cable_altmode_enter(struct typec_altmode *altmode, enum typec_plug_index sop, 3338 u32 *vdo) 3339 { 3340 struct tcpm_port *port = typec_altmode_get_drvdata(altmode); 3341 int svdm_version; 3342 u32 header; 3343 3344 svdm_version = typec_get_cable_svdm_version(port->typec_port); 3345 if (svdm_version < 0) 3346 return svdm_version; 3347 3348 header = VDO(altmode->svid, 1, svdm_version, CMD_ENTER_MODE); 3349 header |= VDO_OPOS(altmode->mode); 3350 3351 return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP_PRIME); 3352 } 3353 3354 static int tcpm_cable_altmode_exit(struct typec_altmode *altmode, enum typec_plug_index sop) 3355 { 3356 struct tcpm_port *port = typec_altmode_get_drvdata(altmode); 3357 int svdm_version; 3358 u32 header; 3359 3360 svdm_version = typec_get_cable_svdm_version(port->typec_port); 3361 if (svdm_version < 0) 3362 return svdm_version; 3363 3364 header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE); 3365 header |= VDO_OPOS(altmode->mode); 3366 3367 return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP_PRIME); 3368 } 3369 3370 static int tcpm_cable_altmode_vdm(struct typec_altmode *altmode, enum typec_plug_index sop, 3371 u32 header, const u32 *data, int count) 3372 { 3373 struct tcpm_port *port = typec_altmode_get_drvdata(altmode); 3374 3375 return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP_PRIME); 3376 } 3377 3378 static const struct typec_cable_ops tcpm_cable_ops = { 3379 .enter = tcpm_cable_altmode_enter, 3380 .exit = tcpm_cable_altmode_exit, 3381 .vdm = tcpm_cable_altmode_vdm, 3382 }; 3383 3384 /* 3385 * PD (data, control) command handling functions 3386 */ 3387 static inline enum tcpm_state ready_state(struct tcpm_port *port) 3388 { 3389 if (port->pwr_role == TYPEC_SOURCE) 3390 return SRC_READY; 3391 else 3392 return SNK_READY; 3393 } 3394 3395 static int tcpm_pd_send_control(struct tcpm_port *port, 3396 enum pd_ctrl_msg_type type, 3397 enum tcpm_transmit_type tx_sop_type); 3398 3399 static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload, 3400 int cnt) 3401 { 3402 u32 p0 = le32_to_cpu(payload[0]); 3403 unsigned int type = usb_pd_ado_type(p0); 3404 3405 if (!type) { 3406 tcpm_log(port, "Alert message received with no type"); 3407 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); 3408 return; 3409 } 3410 3411 /* Just handling non-battery alerts for now */ 3412 if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) { 3413 if (port->pwr_role == TYPEC_SOURCE) { 3414 port->upcoming_state = GET_STATUS_SEND; 3415 tcpm_ams_start(port, GETTING_SOURCE_SINK_STATUS); 3416 } else { 3417 /* 3418 * Do not check SinkTxOk here in case the Source doesn't set its Rp to 3419 * SinkTxOk in time. 3420 */ 3421 port->ams = GETTING_SOURCE_SINK_STATUS; 3422 tcpm_set_state(port, GET_STATUS_SEND, 0); 3423 } 3424 } else { 3425 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); 3426 } 3427 } 3428 3429 static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port, 3430 enum typec_pwr_opmode mode, bool pps_active, 3431 u32 requested_vbus_voltage) 3432 { 3433 int ret; 3434 3435 if (!port->tcpc->set_auto_vbus_discharge_threshold) 3436 return 0; 3437 3438 ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active, 3439 requested_vbus_voltage, 3440 port->pps_data.min_volt); 3441 tcpm_log_force(port, 3442 "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u pps_apdo_min_volt:%u ret:%d", 3443 mode, pps_active ? 'y' : 'n', requested_vbus_voltage, 3444 port->pps_data.min_volt, ret); 3445 3446 return ret; 3447 } 3448 3449 static void tcpm_pd_handle_state(struct tcpm_port *port, 3450 enum tcpm_state state, 3451 enum tcpm_ams ams, 3452 unsigned int delay_ms) 3453 { 3454 switch (port->state) { 3455 case SRC_READY: 3456 case SNK_READY: 3457 port->ams = ams; 3458 tcpm_set_state(port, state, delay_ms); 3459 break; 3460 /* 8.3.3.4.1.1 and 6.8.1 power transitioning */ 3461 case SNK_TRANSITION_SINK: 3462 case SNK_TRANSITION_SINK_VBUS: 3463 case SRC_TRANSITION_SUPPLY: 3464 tcpm_set_state(port, HARD_RESET_SEND, 0); 3465 break; 3466 default: 3467 if (!tcpm_ams_interruptible(port)) { 3468 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? 3469 SRC_SOFT_RESET_WAIT_SNK_TX : 3470 SNK_SOFT_RESET, 3471 0); 3472 } else { 3473 /* process the Message 6.8.1 */ 3474 port->upcoming_state = state; 3475 port->next_ams = ams; 3476 tcpm_set_state(port, ready_state(port), delay_ms); 3477 } 3478 break; 3479 } 3480 } 3481 3482 static void tcpm_pd_handle_msg(struct tcpm_port *port, 3483 enum pd_msg_request message, 3484 enum tcpm_ams ams) 3485 { 3486 switch (port->state) { 3487 case SRC_READY: 3488 case SNK_READY: 3489 port->ams = ams; 3490 tcpm_queue_message(port, message); 3491 break; 3492 /* PD 3.0 Spec 8.3.3.4.1.1 and 6.8.1 */ 3493 case SNK_TRANSITION_SINK: 3494 case SNK_TRANSITION_SINK_VBUS: 3495 case SRC_TRANSITION_SUPPLY: 3496 tcpm_set_state(port, HARD_RESET_SEND, 0); 3497 break; 3498 default: 3499 if (!tcpm_ams_interruptible(port)) { 3500 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? 3501 SRC_SOFT_RESET_WAIT_SNK_TX : 3502 SNK_SOFT_RESET, 3503 0); 3504 } else { 3505 port->next_ams = ams; 3506 tcpm_set_state(port, ready_state(port), 0); 3507 /* 6.8.1 process the Message */ 3508 tcpm_queue_message(port, message); 3509 } 3510 break; 3511 } 3512 } 3513 3514 static int tcpm_register_source_caps(struct tcpm_port *port) 3515 { 3516 struct usb_power_delivery_desc desc = { port->negotiated_rev }; 3517 struct usb_power_delivery_capabilities_desc caps = { }; 3518 struct usb_power_delivery_capabilities *cap = port->partner_source_caps; 3519 3520 if (!port->partner_pd) 3521 port->partner_pd = usb_power_delivery_register(NULL, &desc); 3522 if (IS_ERR(port->partner_pd)) 3523 return PTR_ERR(port->partner_pd); 3524 3525 memcpy(caps.pdo, port->source_caps, sizeof(u32) * port->nr_source_caps); 3526 caps.role = TYPEC_SOURCE; 3527 3528 if (cap) { 3529 usb_power_delivery_unregister_capabilities(cap); 3530 port->partner_source_caps = NULL; 3531 } 3532 3533 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps); 3534 if (IS_ERR(cap)) 3535 return PTR_ERR(cap); 3536 3537 port->partner_source_caps = cap; 3538 3539 return 0; 3540 } 3541 3542 static int tcpm_register_sink_caps(struct tcpm_port *port) 3543 { 3544 struct usb_power_delivery_desc desc = { port->negotiated_rev }; 3545 struct usb_power_delivery_capabilities_desc caps = { }; 3546 struct usb_power_delivery_capabilities *cap; 3547 3548 if (!port->partner_pd) 3549 port->partner_pd = usb_power_delivery_register(NULL, &desc); 3550 if (IS_ERR(port->partner_pd)) 3551 return PTR_ERR(port->partner_pd); 3552 3553 memcpy(caps.pdo, port->sink_caps, sizeof(u32) * port->nr_sink_caps); 3554 caps.role = TYPEC_SINK; 3555 3556 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps); 3557 if (IS_ERR(cap)) 3558 return PTR_ERR(cap); 3559 3560 port->partner_sink_caps = cap; 3561 3562 return 0; 3563 } 3564 3565 static void tcpm_pd_data_request(struct tcpm_port *port, 3566 const struct pd_message *msg, 3567 enum tcpm_transmit_type rx_sop_type) 3568 { 3569 enum pd_data_msg_type type = pd_header_type_le(msg->header); 3570 unsigned int cnt = pd_header_cnt_le(msg->header); 3571 unsigned int rev = pd_header_rev_le(msg->header); 3572 unsigned int i; 3573 enum frs_typec_current partner_frs_current; 3574 bool frs_enable; 3575 int ret; 3576 3577 if (tcpm_vdm_ams(port) && type != PD_DATA_VENDOR_DEF) { 3578 port->vdm_state = VDM_STATE_ERR_BUSY; 3579 tcpm_ams_finish(port); 3580 mod_vdm_delayed_work(port, 0); 3581 } 3582 3583 switch (type) { 3584 case PD_DATA_SOURCE_CAP: 3585 port->spr_avs_data.port_partner_src_status = SPR_AVS_UNKNOWN; 3586 for (i = 0; i < cnt; i++) 3587 port->source_caps[i] = le32_to_cpu(msg->payload[i]); 3588 3589 port->nr_source_caps = cnt; 3590 3591 tcpm_log_source_caps(port); 3592 3593 tcpm_validate_caps(port, port->source_caps, 3594 port->nr_source_caps); 3595 3596 tcpm_register_source_caps(port); 3597 3598 /* 3599 * Adjust revision in subsequent message headers, as required, 3600 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't 3601 * support Rev 1.0 so just do nothing in that scenario. 3602 */ 3603 if (rev == PD_REV10) { 3604 if (port->ams == GET_SOURCE_CAPABILITIES) 3605 tcpm_ams_finish(port); 3606 break; 3607 } 3608 3609 if (rev < PD_MAX_REV) { 3610 port->negotiated_rev = rev; 3611 if (port->negotiated_rev_prime > port->negotiated_rev) 3612 port->negotiated_rev_prime = port->negotiated_rev; 3613 } 3614 3615 if (port->pwr_role == TYPEC_SOURCE) { 3616 if (port->ams == GET_SOURCE_CAPABILITIES) 3617 tcpm_pd_handle_state(port, SRC_READY, NONE_AMS, 0); 3618 /* Unexpected Source Capabilities */ 3619 else 3620 tcpm_pd_handle_msg(port, 3621 port->negotiated_rev < PD_REV30 ? 3622 PD_MSG_CTRL_REJECT : 3623 PD_MSG_CTRL_NOT_SUPP, 3624 NONE_AMS); 3625 } else if (port->state == SNK_WAIT_CAPABILITIES || 3626 port->state == SNK_WAIT_CAPABILITIES_TIMEOUT) { 3627 /* 3628 * This message may be received even if VBUS is not 3629 * present. This is quite unexpected; see USB PD 3630 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2. 3631 * However, at the same time, we must be ready to 3632 * receive this message and respond to it 15ms after 3633 * receiving PS_RDY during power swap operations, no matter 3634 * if VBUS is available or not (USB PD specification, 3635 * section 6.5.9.2). 3636 * So we need to accept the message either way, 3637 * but be prepared to keep waiting for VBUS after it was 3638 * handled. 3639 */ 3640 port->ams = POWER_NEGOTIATION; 3641 port->in_ams = true; 3642 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); 3643 } else { 3644 if (port->ams == GET_SOURCE_CAPABILITIES) 3645 tcpm_ams_finish(port); 3646 tcpm_pd_handle_state(port, SNK_NEGOTIATE_CAPABILITIES, 3647 POWER_NEGOTIATION, 0); 3648 } 3649 break; 3650 case PD_DATA_REQUEST: 3651 /* 3652 * Adjust revision in subsequent message headers, as required, 3653 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't 3654 * support Rev 1.0 so just reject in that scenario. 3655 */ 3656 if (rev == PD_REV10) { 3657 tcpm_pd_handle_msg(port, 3658 port->negotiated_rev < PD_REV30 ? 3659 PD_MSG_CTRL_REJECT : 3660 PD_MSG_CTRL_NOT_SUPP, 3661 NONE_AMS); 3662 break; 3663 } 3664 3665 if (rev < PD_MAX_REV) { 3666 port->negotiated_rev = rev; 3667 if (port->negotiated_rev_prime > port->negotiated_rev) 3668 port->negotiated_rev_prime = port->negotiated_rev; 3669 } 3670 3671 if (port->pwr_role != TYPEC_SOURCE || cnt != 1) { 3672 tcpm_pd_handle_msg(port, 3673 port->negotiated_rev < PD_REV30 ? 3674 PD_MSG_CTRL_REJECT : 3675 PD_MSG_CTRL_NOT_SUPP, 3676 NONE_AMS); 3677 break; 3678 } 3679 3680 port->sink_request = le32_to_cpu(msg->payload[0]); 3681 3682 if (port->vdm_sm_running && port->explicit_contract) { 3683 tcpm_pd_handle_msg(port, PD_MSG_CTRL_WAIT, port->ams); 3684 break; 3685 } 3686 3687 if (port->state == SRC_SEND_CAPABILITIES) 3688 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0); 3689 else 3690 tcpm_pd_handle_state(port, SRC_NEGOTIATE_CAPABILITIES, 3691 POWER_NEGOTIATION, 0); 3692 break; 3693 case PD_DATA_SINK_CAP: 3694 /* We don't do anything with this at the moment... */ 3695 for (i = 0; i < cnt; i++) 3696 port->sink_caps[i] = le32_to_cpu(msg->payload[i]); 3697 3698 partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >> 3699 PDO_FIXED_FRS_CURR_SHIFT; 3700 frs_enable = partner_frs_current && (partner_frs_current <= 3701 port->new_source_frs_current); 3702 tcpm_log(port, 3703 "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c", 3704 partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n'); 3705 if (frs_enable) { 3706 ret = port->tcpc->enable_frs(port->tcpc, true); 3707 tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret); 3708 } 3709 3710 port->nr_sink_caps = cnt; 3711 port->sink_cap_done = true; 3712 tcpm_register_sink_caps(port); 3713 3714 if (port->ams == GET_SINK_CAPABILITIES) 3715 tcpm_set_state(port, ready_state(port), 0); 3716 /* Unexpected Sink Capabilities */ 3717 else 3718 tcpm_pd_handle_msg(port, 3719 port->negotiated_rev < PD_REV30 ? 3720 PD_MSG_CTRL_REJECT : 3721 PD_MSG_CTRL_NOT_SUPP, 3722 NONE_AMS); 3723 break; 3724 case PD_DATA_VENDOR_DEF: 3725 tcpm_handle_vdm_request(port, msg->payload, cnt, rx_sop_type); 3726 break; 3727 case PD_DATA_BIST: 3728 port->bist_request = le32_to_cpu(msg->payload[0]); 3729 tcpm_pd_handle_state(port, BIST_RX, BIST, 0); 3730 break; 3731 case PD_DATA_ALERT: 3732 if (port->state != SRC_READY && port->state != SNK_READY) 3733 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ? 3734 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET, 3735 NONE_AMS, 0); 3736 else 3737 tcpm_handle_alert(port, msg->payload, cnt); 3738 break; 3739 case PD_DATA_BATT_STATUS: 3740 case PD_DATA_GET_COUNTRY_INFO: 3741 /* Currently unsupported */ 3742 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ? 3743 PD_MSG_CTRL_REJECT : 3744 PD_MSG_CTRL_NOT_SUPP, 3745 NONE_AMS); 3746 break; 3747 default: 3748 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ? 3749 PD_MSG_CTRL_REJECT : 3750 PD_MSG_CTRL_NOT_SUPP, 3751 NONE_AMS); 3752 tcpm_log(port, "Unrecognized data message type %#x", type); 3753 break; 3754 } 3755 } 3756 3757 static void tcpm_aug_supply_req_complete(struct tcpm_port *port, int result) 3758 { 3759 if (port->aug_supply_req_pending) { 3760 port->aug_supply_req_status = result; 3761 port->aug_supply_req_pending = false; 3762 complete(&port->aug_supply_req_complete); 3763 } 3764 } 3765 3766 static void tcpm_pd_ctrl_request(struct tcpm_port *port, 3767 const struct pd_message *msg, 3768 enum tcpm_transmit_type rx_sop_type) 3769 { 3770 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header); 3771 enum tcpm_state next_state; 3772 unsigned int rev = pd_header_rev_le(msg->header); 3773 3774 /* 3775 * Stop VDM state machine if interrupted by other Messages while NOT_SUPP is allowed in 3776 * VDM AMS if waiting for VDM responses and will be handled later. 3777 */ 3778 if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) { 3779 port->vdm_state = VDM_STATE_ERR_BUSY; 3780 tcpm_ams_finish(port); 3781 mod_vdm_delayed_work(port, 0); 3782 } 3783 3784 switch (type) { 3785 case PD_CTRL_GOOD_CRC: 3786 case PD_CTRL_PING: 3787 break; 3788 case PD_CTRL_GET_SOURCE_CAP: 3789 tcpm_pd_handle_msg(port, PD_MSG_DATA_SOURCE_CAP, GET_SOURCE_CAPABILITIES); 3790 break; 3791 case PD_CTRL_GET_SINK_CAP: 3792 tcpm_pd_handle_msg(port, PD_MSG_DATA_SINK_CAP, GET_SINK_CAPABILITIES); 3793 break; 3794 case PD_CTRL_GOTO_MIN: 3795 break; 3796 case PD_CTRL_PS_RDY: 3797 switch (port->state) { 3798 case SNK_TRANSITION_SINK: 3799 if (port->vbus_present) { 3800 tcpm_set_current_limit(port, 3801 port->req_current_limit, 3802 port->req_supply_voltage); 3803 port->explicit_contract = true; 3804 tcpm_set_auto_vbus_discharge_threshold(port, 3805 TYPEC_PWR_MODE_PD, 3806 port->pps_data.active, 3807 port->supply_voltage); 3808 tcpm_set_state(port, SNK_READY, 0); 3809 } else { 3810 /* 3811 * Seen after power swap. Keep waiting for VBUS 3812 * in a transitional state. 3813 */ 3814 tcpm_set_state(port, 3815 SNK_TRANSITION_SINK_VBUS, 0); 3816 } 3817 break; 3818 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: 3819 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0); 3820 break; 3821 case PR_SWAP_SNK_SRC_SINK_OFF: 3822 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0); 3823 break; 3824 case VCONN_SWAP_WAIT_FOR_VCONN: 3825 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0); 3826 break; 3827 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: 3828 tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0); 3829 break; 3830 default: 3831 tcpm_pd_handle_state(port, 3832 port->pwr_role == TYPEC_SOURCE ? 3833 SRC_SOFT_RESET_WAIT_SNK_TX : 3834 SNK_SOFT_RESET, 3835 NONE_AMS, 0); 3836 break; 3837 } 3838 break; 3839 case PD_CTRL_REJECT: 3840 case PD_CTRL_WAIT: 3841 case PD_CTRL_NOT_SUPP: 3842 switch (port->state) { 3843 case SNK_NEGOTIATE_CAPABILITIES: 3844 /* USB PD specification, Figure 8-43 */ 3845 if (port->explicit_contract) 3846 next_state = SNK_READY; 3847 else 3848 next_state = SNK_WAIT_CAPABILITIES; 3849 3850 /* Threshold was relaxed before sending Request. Restore it back. */ 3851 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, 3852 port->pps_data.active, 3853 port->supply_voltage); 3854 tcpm_set_state(port, next_state, 0); 3855 break; 3856 case SNK_NEGOTIATE_PPS_CAPABILITIES: 3857 /* Revert data back from any requested PPS updates */ 3858 port->pps_data.req_out_volt = port->supply_voltage; 3859 port->pps_data.req_op_curr = port->current_limit; 3860 port->aug_supply_req_status = (type == PD_CTRL_WAIT ? 3861 -EAGAIN : -EOPNOTSUPP); 3862 3863 /* Threshold was relaxed before sending Request. Restore it back. */ 3864 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, 3865 port->pps_data.active, 3866 port->supply_voltage); 3867 3868 tcpm_set_state(port, SNK_READY, 0); 3869 break; 3870 case SNK_NEGOTIATE_SPR_AVS_CAPABILITIES: 3871 /* Revert data back from any requested SPR AVS updates */ 3872 port->spr_avs_data.req_out_volt_mv = port->supply_voltage; 3873 port->spr_avs_data.req_op_curr_ma = port->current_limit; 3874 port->aug_supply_req_status = (type == PD_CTRL_WAIT ? 3875 -EAGAIN : -EOPNOTSUPP); 3876 3877 /* Threshold was relaxed before sending Request. Restore it back. */ 3878 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, 3879 port->spr_avs_data.active, 3880 port->supply_voltage); 3881 3882 tcpm_set_state(port, SNK_READY, 0); 3883 break; 3884 case DR_SWAP_SEND: 3885 port->swap_status = (type == PD_CTRL_WAIT ? 3886 -EAGAIN : -EOPNOTSUPP); 3887 tcpm_set_state(port, DR_SWAP_CANCEL, 0); 3888 break; 3889 case PR_SWAP_SEND: 3890 port->swap_status = (type == PD_CTRL_WAIT ? 3891 -EAGAIN : -EOPNOTSUPP); 3892 tcpm_set_state(port, PR_SWAP_CANCEL, 0); 3893 break; 3894 case VCONN_SWAP_SEND: 3895 port->swap_status = (type == PD_CTRL_WAIT ? 3896 -EAGAIN : -EOPNOTSUPP); 3897 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0); 3898 break; 3899 case FR_SWAP_SEND: 3900 tcpm_set_state(port, FR_SWAP_CANCEL, 0); 3901 break; 3902 case GET_SINK_CAP: 3903 port->sink_cap_done = true; 3904 tcpm_set_state(port, ready_state(port), 0); 3905 break; 3906 /* 3907 * Some port partners do not support GET_STATUS, avoid soft reset the link to 3908 * prevent redundant power re-negotiation 3909 */ 3910 case GET_STATUS_SEND: 3911 tcpm_set_state(port, ready_state(port), 0); 3912 break; 3913 case SRC_READY: 3914 case SNK_READY: 3915 if (port->vdm_state > VDM_STATE_READY) { 3916 port->vdm_state = VDM_STATE_DONE; 3917 if (tcpm_vdm_ams(port)) 3918 tcpm_ams_finish(port); 3919 mod_vdm_delayed_work(port, 0); 3920 break; 3921 } 3922 fallthrough; 3923 default: 3924 tcpm_pd_handle_state(port, 3925 port->pwr_role == TYPEC_SOURCE ? 3926 SRC_SOFT_RESET_WAIT_SNK_TX : 3927 SNK_SOFT_RESET, 3928 NONE_AMS, 0); 3929 break; 3930 } 3931 break; 3932 case PD_CTRL_ACCEPT: 3933 switch (port->state) { 3934 case SNK_NEGOTIATE_CAPABILITIES: 3935 port->pps_data.active = false; 3936 port->spr_avs_data.active = false; 3937 tcpm_set_state(port, SNK_TRANSITION_SINK, 0); 3938 break; 3939 case SNK_NEGOTIATE_PPS_CAPABILITIES: 3940 port->pps_data.active = true; 3941 port->pps_data.min_volt = port->pps_data.req_min_volt; 3942 port->pps_data.max_volt = port->pps_data.req_max_volt; 3943 port->pps_data.max_curr = port->pps_data.req_max_curr; 3944 port->req_supply_voltage = port->pps_data.req_out_volt; 3945 port->req_current_limit = port->pps_data.req_op_curr; 3946 power_supply_changed(port->psy); 3947 tcpm_set_state(port, SNK_TRANSITION_SINK, 0); 3948 break; 3949 case SNK_NEGOTIATE_SPR_AVS_CAPABILITIES: 3950 port->spr_avs_data.active = true; 3951 port->req_supply_voltage = port->spr_avs_data.req_out_volt_mv; 3952 port->req_current_limit = port->spr_avs_data.req_op_curr_ma; 3953 power_supply_changed(port->psy); 3954 tcpm_set_state(port, SNK_TRANSITION_SINK, 0); 3955 break; 3956 case SOFT_RESET_SEND: 3957 if (port->ams == SOFT_RESET_AMS) 3958 tcpm_ams_finish(port); 3959 /* 3960 * SOP' Soft Reset is done after Vconn Swap, 3961 * which returns to ready state 3962 */ 3963 if (rx_sop_type == TCPC_TX_SOP_PRIME) { 3964 if (rev < port->negotiated_rev_prime) 3965 port->negotiated_rev_prime = rev; 3966 tcpm_set_state(port, ready_state(port), 0); 3967 break; 3968 } 3969 if (port->pwr_role == TYPEC_SOURCE) { 3970 port->upcoming_state = SRC_SEND_CAPABILITIES; 3971 tcpm_ams_start(port, POWER_NEGOTIATION); 3972 } else { 3973 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); 3974 } 3975 break; 3976 case DR_SWAP_SEND: 3977 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0); 3978 break; 3979 case PR_SWAP_SEND: 3980 tcpm_set_state(port, PR_SWAP_START, 0); 3981 break; 3982 case VCONN_SWAP_SEND: 3983 tcpm_set_state(port, VCONN_SWAP_START, 0); 3984 break; 3985 case FR_SWAP_SEND: 3986 tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0); 3987 break; 3988 default: 3989 tcpm_pd_handle_state(port, 3990 port->pwr_role == TYPEC_SOURCE ? 3991 SRC_SOFT_RESET_WAIT_SNK_TX : 3992 SNK_SOFT_RESET, 3993 NONE_AMS, 0); 3994 break; 3995 } 3996 break; 3997 case PD_CTRL_SOFT_RESET: 3998 port->ams = SOFT_RESET_AMS; 3999 tcpm_set_state(port, SOFT_RESET, 0); 4000 break; 4001 case PD_CTRL_DR_SWAP: 4002 /* 4003 * XXX 4004 * 6.3.9: If an alternate mode is active, a request to swap 4005 * alternate modes shall trigger a port reset. 4006 */ 4007 if (port->typec_caps.data != TYPEC_PORT_DRD) { 4008 tcpm_pd_handle_msg(port, 4009 port->negotiated_rev < PD_REV30 ? 4010 PD_MSG_CTRL_REJECT : 4011 PD_MSG_CTRL_NOT_SUPP, 4012 NONE_AMS); 4013 } else { 4014 if (port->vdm_discovery_state == VDM_DISCOVERY_UNKNOWN && 4015 port->negotiated_rev < PD_REV30) { 4016 tcpm_queue_message(port, PD_MSG_CTRL_WAIT); 4017 break; 4018 } 4019 4020 tcpm_pd_handle_state(port, DR_SWAP_ACCEPT, DATA_ROLE_SWAP, 0); 4021 } 4022 break; 4023 case PD_CTRL_PR_SWAP: 4024 if (port->port_type != TYPEC_PORT_DRP) { 4025 tcpm_pd_handle_msg(port, 4026 port->negotiated_rev < PD_REV30 ? 4027 PD_MSG_CTRL_REJECT : 4028 PD_MSG_CTRL_NOT_SUPP, 4029 NONE_AMS); 4030 } else { 4031 if (port->vdm_discovery_state == VDM_DISCOVERY_UNKNOWN && 4032 port->negotiated_rev < PD_REV30) { 4033 tcpm_queue_message(port, PD_MSG_CTRL_WAIT); 4034 break; 4035 } 4036 4037 tcpm_pd_handle_state(port, PR_SWAP_ACCEPT, POWER_ROLE_SWAP, 0); 4038 } 4039 break; 4040 case PD_CTRL_VCONN_SWAP: 4041 if (port->vdm_discovery_state == VDM_DISCOVERY_UNKNOWN && 4042 port->negotiated_rev < PD_REV30) { 4043 tcpm_queue_message(port, PD_MSG_CTRL_WAIT); 4044 break; 4045 } 4046 4047 tcpm_pd_handle_state(port, VCONN_SWAP_ACCEPT, VCONN_SWAP, 0); 4048 break; 4049 case PD_CTRL_GET_SOURCE_CAP_EXT: 4050 case PD_CTRL_GET_STATUS: 4051 case PD_CTRL_FR_SWAP: 4052 case PD_CTRL_GET_PPS_STATUS: 4053 case PD_CTRL_GET_COUNTRY_CODES: 4054 /* Currently not supported */ 4055 tcpm_pd_handle_msg(port, 4056 port->negotiated_rev < PD_REV30 ? 4057 PD_MSG_CTRL_REJECT : 4058 PD_MSG_CTRL_NOT_SUPP, 4059 NONE_AMS); 4060 break; 4061 case PD_CTRL_GET_REVISION: 4062 if (port->negotiated_rev >= PD_REV30 && port->pd_rev.rev_major) 4063 tcpm_pd_handle_msg(port, PD_MSG_DATA_REV, 4064 REVISION_INFORMATION); 4065 else 4066 tcpm_pd_handle_msg(port, 4067 port->negotiated_rev < PD_REV30 ? 4068 PD_MSG_CTRL_REJECT : 4069 PD_MSG_CTRL_NOT_SUPP, 4070 NONE_AMS); 4071 break; 4072 case PD_CTRL_GET_SINK_CAP_EXT: 4073 /* This is an unsupported message if port type is SRC */ 4074 if (port->negotiated_rev >= PD_REV30 && 4075 port->port_type != TYPEC_PORT_SRC) 4076 tcpm_pd_handle_msg(port, PD_MSG_EXT_SINK_CAP_EXT, 4077 GETTING_SINK_EXTENDED_CAPABILITIES); 4078 else 4079 tcpm_pd_handle_msg(port, 4080 port->negotiated_rev < PD_REV30 ? 4081 PD_MSG_CTRL_REJECT : 4082 PD_MSG_CTRL_NOT_SUPP, 4083 NONE_AMS); 4084 break; 4085 default: 4086 tcpm_pd_handle_msg(port, 4087 port->negotiated_rev < PD_REV30 ? 4088 PD_MSG_CTRL_REJECT : 4089 PD_MSG_CTRL_NOT_SUPP, 4090 NONE_AMS); 4091 tcpm_log(port, "Unrecognized ctrl message type %#x", type); 4092 break; 4093 } 4094 } 4095 4096 static void tcpm_pd_ext_msg_request(struct tcpm_port *port, 4097 const struct pd_message *msg) 4098 { 4099 enum pd_ext_msg_type type = pd_header_type_le(msg->header); 4100 unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header); 4101 const struct pd_chunked_ext_message_data *ext_msg = &msg->ext_msg; 4102 4103 /* stopping VDM state machine if interrupted by other Messages */ 4104 if (tcpm_vdm_ams(port)) { 4105 port->vdm_state = VDM_STATE_ERR_BUSY; 4106 tcpm_ams_finish(port); 4107 mod_vdm_delayed_work(port, 0); 4108 } 4109 4110 if (!(le16_to_cpu(ext_msg->header) & PD_EXT_HDR_CHUNKED)) { 4111 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); 4112 tcpm_log(port, "Unchunked extended messages unsupported"); 4113 return; 4114 } 4115 4116 if (data_size > PD_EXT_MAX_CHUNK_DATA) { 4117 tcpm_pd_handle_state(port, CHUNK_NOT_SUPP, NONE_AMS, PD_T_CHUNK_NOT_SUPP); 4118 tcpm_log(port, "Chunk handling not yet supported"); 4119 return; 4120 } 4121 4122 switch (type) { 4123 case PD_EXT_STATUS: 4124 case PD_EXT_PPS_STATUS: 4125 if (port->ams == GETTING_SOURCE_SINK_STATUS) { 4126 tcpm_ams_finish(port); 4127 tcpm_set_state(port, ready_state(port), 0); 4128 } else { 4129 /* unexpected Status or PPS_Status Message */ 4130 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ? 4131 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET, 4132 NONE_AMS, 0); 4133 } 4134 break; 4135 case PD_EXT_GET_BATT_STATUS: 4136 if (data_size >= 1) { 4137 port->batt_request_id = ext_msg->data[0]; 4138 tcpm_pd_handle_msg(port, PD_MSG_DATA_BATT_STATUS, 4139 GETTING_BATTERY_STATUS); 4140 } else { 4141 tcpm_set_state(port, SOFT_RESET_SEND, 0); 4142 } 4143 break; 4144 case PD_EXT_GET_BATT_CAP: 4145 if (data_size >= 1) { 4146 port->batt_request_id = ext_msg->data[0]; 4147 tcpm_pd_handle_msg(port, PD_MSG_EXT_BATT_CAP, 4148 GETTING_BATTERY_CAPABILITIES); 4149 } else { 4150 tcpm_set_state(port, SOFT_RESET_SEND, 0); 4151 } 4152 break; 4153 case PD_EXT_SOURCE_CAP_EXT: 4154 case PD_EXT_BATT_CAP: 4155 case PD_EXT_GET_MANUFACTURER_INFO: 4156 case PD_EXT_MANUFACTURER_INFO: 4157 case PD_EXT_SECURITY_REQUEST: 4158 case PD_EXT_SECURITY_RESPONSE: 4159 case PD_EXT_FW_UPDATE_REQUEST: 4160 case PD_EXT_FW_UPDATE_RESPONSE: 4161 case PD_EXT_COUNTRY_INFO: 4162 case PD_EXT_COUNTRY_CODES: 4163 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); 4164 break; 4165 default: 4166 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); 4167 tcpm_log(port, "Unrecognized extended message type %#x", type); 4168 break; 4169 } 4170 } 4171 4172 static void tcpm_pd_rx_handler(struct kthread_work *work) 4173 { 4174 struct pd_rx_event *event = container_of(work, 4175 struct pd_rx_event, work); 4176 const struct pd_message *msg = &event->msg; 4177 unsigned int cnt = pd_header_cnt_le(msg->header); 4178 struct tcpm_port *port = event->port; 4179 enum tcpm_transmit_type rx_sop_type = event->rx_sop_type; 4180 4181 mutex_lock(&port->lock); 4182 4183 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header), 4184 port->attached); 4185 4186 if (port->attached) { 4187 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header); 4188 unsigned int msgid = pd_header_msgid_le(msg->header); 4189 4190 /* 4191 * Drop SOP' messages if cannot receive via 4192 * tcpm_can_communicate_sop_prime 4193 */ 4194 if (rx_sop_type == TCPC_TX_SOP_PRIME && 4195 !tcpm_can_communicate_sop_prime(port)) 4196 goto done; 4197 4198 /* 4199 * USB PD standard, 6.6.1.2: 4200 * "... if MessageID value in a received Message is the 4201 * same as the stored value, the receiver shall return a 4202 * GoodCRC Message with that MessageID value and drop 4203 * the Message (this is a retry of an already received 4204 * Message). Note: this shall not apply to the Soft_Reset 4205 * Message which always has a MessageID value of zero." 4206 */ 4207 switch (rx_sop_type) { 4208 case TCPC_TX_SOP_PRIME: 4209 if (msgid == port->rx_msgid_prime) 4210 goto done; 4211 port->rx_msgid_prime = msgid; 4212 break; 4213 case TCPC_TX_SOP: 4214 default: 4215 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET) 4216 goto done; 4217 port->rx_msgid = msgid; 4218 break; 4219 } 4220 4221 /* 4222 * If both ends believe to be DFP/host, we have a data role 4223 * mismatch. 4224 */ 4225 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) == 4226 (port->data_role == TYPEC_HOST) && rx_sop_type == TCPC_TX_SOP) { 4227 tcpm_log(port, 4228 "Data role mismatch, initiating error recovery"); 4229 tcpm_set_state(port, ERROR_RECOVERY, 0); 4230 } else { 4231 if (le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR) 4232 tcpm_pd_ext_msg_request(port, msg); 4233 else if (cnt) 4234 tcpm_pd_data_request(port, msg, rx_sop_type); 4235 else 4236 tcpm_pd_ctrl_request(port, msg, rx_sop_type); 4237 } 4238 } 4239 4240 done: 4241 mutex_unlock(&port->lock); 4242 kfree(event); 4243 } 4244 4245 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg, 4246 enum tcpm_transmit_type rx_sop_type) 4247 { 4248 struct pd_rx_event *event; 4249 4250 event = kzalloc_obj(*event, GFP_ATOMIC); 4251 if (!event) 4252 return; 4253 4254 kthread_init_work(&event->work, tcpm_pd_rx_handler); 4255 event->port = port; 4256 event->rx_sop_type = rx_sop_type; 4257 memcpy(&event->msg, msg, sizeof(*msg)); 4258 kthread_queue_work(port->wq, &event->work); 4259 } 4260 EXPORT_SYMBOL_GPL(tcpm_pd_receive); 4261 4262 static int tcpm_pd_send_control(struct tcpm_port *port, 4263 enum pd_ctrl_msg_type type, 4264 enum tcpm_transmit_type tx_sop_type) 4265 { 4266 struct pd_message msg; 4267 4268 memset(&msg, 0, sizeof(msg)); 4269 switch (tx_sop_type) { 4270 case TCPC_TX_SOP_PRIME: 4271 msg.header = PD_HEADER_LE(type, 4272 0, /* Cable Plug Indicator for DFP/UFP */ 4273 0, /* Reserved */ 4274 port->negotiated_rev, 4275 port->message_id_prime, 4276 0); 4277 break; 4278 case TCPC_TX_SOP: 4279 msg.header = PD_HEADER_LE(type, 4280 port->pwr_role, 4281 port->data_role, 4282 port->negotiated_rev, 4283 port->message_id, 4284 0); 4285 break; 4286 default: 4287 msg.header = PD_HEADER_LE(type, 4288 port->pwr_role, 4289 port->data_role, 4290 port->negotiated_rev, 4291 port->message_id, 4292 0); 4293 break; 4294 } 4295 4296 return tcpm_pd_transmit(port, tx_sop_type, &msg); 4297 } 4298 4299 /* 4300 * Send queued message without affecting state. 4301 * Return true if state machine should go back to sleep, 4302 * false otherwise. 4303 */ 4304 static bool tcpm_send_queued_message(struct tcpm_port *port) 4305 { 4306 enum pd_msg_request queued_message; 4307 int ret; 4308 4309 do { 4310 queued_message = port->queued_message; 4311 port->queued_message = PD_MSG_NONE; 4312 4313 switch (queued_message) { 4314 case PD_MSG_CTRL_WAIT: 4315 tcpm_pd_send_control(port, PD_CTRL_WAIT, TCPC_TX_SOP); 4316 break; 4317 case PD_MSG_CTRL_REJECT: 4318 tcpm_pd_send_control(port, PD_CTRL_REJECT, TCPC_TX_SOP); 4319 break; 4320 case PD_MSG_CTRL_NOT_SUPP: 4321 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP, TCPC_TX_SOP); 4322 break; 4323 case PD_MSG_DATA_SINK_CAP: 4324 ret = tcpm_pd_send_sink_caps(port); 4325 if (ret < 0) { 4326 tcpm_log(port, "Unable to send snk caps, ret=%d", ret); 4327 tcpm_set_state(port, SNK_SOFT_RESET, 0); 4328 } 4329 tcpm_ams_finish(port); 4330 break; 4331 case PD_MSG_DATA_SOURCE_CAP: 4332 ret = tcpm_pd_send_source_caps(port); 4333 if (ret < 0) { 4334 tcpm_log(port, 4335 "Unable to send src caps, ret=%d", 4336 ret); 4337 tcpm_set_state(port, SOFT_RESET_SEND, 0); 4338 } else if (port->pwr_role == TYPEC_SOURCE) { 4339 tcpm_ams_finish(port); 4340 tcpm_set_state(port, HARD_RESET_SEND, 4341 PD_T_SENDER_RESPONSE); 4342 } else { 4343 tcpm_ams_finish(port); 4344 } 4345 break; 4346 case PD_MSG_DATA_REV: 4347 ret = tcpm_pd_send_revision(port); 4348 if (ret) 4349 tcpm_log(port, 4350 "Unable to send revision msg, ret=%d", 4351 ret); 4352 tcpm_ams_finish(port); 4353 break; 4354 case PD_MSG_EXT_SINK_CAP_EXT: 4355 ret = tcpm_pd_send_sink_cap_ext(port); 4356 if (ret == -EOPNOTSUPP) 4357 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP, TCPC_TX_SOP); 4358 else if (ret < 0) 4359 tcpm_log(port, 4360 "Unable to transmit sink cap extended, ret=%d", 4361 ret); 4362 tcpm_ams_finish(port); 4363 break; 4364 case PD_MSG_DATA_BATT_STATUS: 4365 ret = tcpm_pd_send_batt_status(port); 4366 if (ret) 4367 tcpm_log(port, 4368 "Failed to send battery status ret=%d", 4369 ret); 4370 tcpm_ams_finish(port); 4371 break; 4372 case PD_MSG_EXT_BATT_CAP: 4373 ret = tcpm_pd_send_batt_cap(port); 4374 if (ret) 4375 tcpm_log(port, 4376 "Failed to send battery cap ret=%d", 4377 ret); 4378 tcpm_ams_finish(port); 4379 break; 4380 default: 4381 break; 4382 } 4383 } while (port->queued_message != PD_MSG_NONE); 4384 4385 if (port->delayed_state != INVALID_STATE) { 4386 if (ktime_after(port->delayed_runtime, ktime_get())) { 4387 mod_tcpm_delayed_work(port, ktime_to_ms(ktime_sub(port->delayed_runtime, 4388 ktime_get()))); 4389 return true; 4390 } 4391 port->delayed_state = INVALID_STATE; 4392 } 4393 return false; 4394 } 4395 4396 static int tcpm_pd_check_request(struct tcpm_port *port) 4397 { 4398 u32 pdo, rdo = port->sink_request; 4399 unsigned int max, op, pdo_max, index; 4400 enum pd_pdo_type type; 4401 4402 index = rdo_index(rdo); 4403 if (!index || index > port->nr_src_pdo) 4404 return -EINVAL; 4405 4406 pdo = port->src_pdo[index - 1]; 4407 type = pdo_type(pdo); 4408 switch (type) { 4409 case PDO_TYPE_FIXED: 4410 case PDO_TYPE_VAR: 4411 max = rdo_max_current(rdo); 4412 op = rdo_op_current(rdo); 4413 pdo_max = pdo_max_current(pdo); 4414 4415 if (op > pdo_max) 4416 return -EINVAL; 4417 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH)) 4418 return -EINVAL; 4419 4420 if (type == PDO_TYPE_FIXED) 4421 tcpm_log(port, 4422 "Requested %u mV, %u mA for %u / %u mA", 4423 pdo_fixed_voltage(pdo), pdo_max, op, max); 4424 else 4425 tcpm_log(port, 4426 "Requested %u -> %u mV, %u mA for %u / %u mA", 4427 pdo_min_voltage(pdo), pdo_max_voltage(pdo), 4428 pdo_max, op, max); 4429 break; 4430 case PDO_TYPE_BATT: 4431 max = rdo_max_power(rdo); 4432 op = rdo_op_power(rdo); 4433 pdo_max = pdo_max_power(pdo); 4434 4435 if (op > pdo_max) 4436 return -EINVAL; 4437 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH)) 4438 return -EINVAL; 4439 tcpm_log(port, 4440 "Requested %u -> %u mV, %u mW for %u / %u mW", 4441 pdo_min_voltage(pdo), pdo_max_voltage(pdo), 4442 pdo_max, op, max); 4443 break; 4444 default: 4445 return -EINVAL; 4446 } 4447 4448 port->op_vsafe5v = index == 1; 4449 4450 return 0; 4451 } 4452 4453 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y)) 4454 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y)) 4455 4456 static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo, 4457 int *src_pdo) 4458 { 4459 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0, 4460 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0, 4461 min_snk_mv = 0; 4462 int ret = -EINVAL; 4463 4464 port->pps_data.supported = false; 4465 port->usb_type = POWER_SUPPLY_USB_TYPE_PD; 4466 power_supply_changed(port->psy); 4467 4468 /* 4469 * Select the source PDO providing the most power which has a 4470 * matchig sink cap. 4471 */ 4472 for (i = 0; i < port->nr_source_caps; i++) { 4473 u32 pdo = port->source_caps[i]; 4474 enum pd_pdo_type type = pdo_type(pdo); 4475 4476 switch (type) { 4477 case PDO_TYPE_FIXED: 4478 max_src_mv = pdo_fixed_voltage(pdo); 4479 min_src_mv = max_src_mv; 4480 break; 4481 case PDO_TYPE_BATT: 4482 case PDO_TYPE_VAR: 4483 max_src_mv = pdo_max_voltage(pdo); 4484 min_src_mv = pdo_min_voltage(pdo); 4485 break; 4486 case PDO_TYPE_APDO: 4487 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) { 4488 port->pps_data.supported = true; 4489 } else if (pdo_apdo_type(pdo) == APDO_TYPE_SPR_AVS) { 4490 port->spr_avs_data.port_partner_src_status = SPR_AVS_SUPPORTED; 4491 port->spr_avs_data.port_partner_src_pdo_index = i; 4492 } 4493 continue; 4494 default: 4495 tcpm_log(port, "Invalid source PDO type, ignoring"); 4496 continue; 4497 } 4498 4499 switch (type) { 4500 case PDO_TYPE_FIXED: 4501 case PDO_TYPE_VAR: 4502 src_ma = pdo_max_current(pdo); 4503 src_mw = src_ma * min_src_mv / 1000; 4504 break; 4505 case PDO_TYPE_BATT: 4506 src_mw = pdo_max_power(pdo); 4507 break; 4508 case PDO_TYPE_APDO: 4509 continue; 4510 default: 4511 tcpm_log(port, "Invalid source PDO type, ignoring"); 4512 continue; 4513 } 4514 4515 for (j = 0; j < port->nr_snk_pdo; j++) { 4516 pdo = port->snk_pdo[j]; 4517 4518 switch (pdo_type(pdo)) { 4519 case PDO_TYPE_FIXED: 4520 max_snk_mv = pdo_fixed_voltage(pdo); 4521 min_snk_mv = max_snk_mv; 4522 break; 4523 case PDO_TYPE_BATT: 4524 case PDO_TYPE_VAR: 4525 max_snk_mv = pdo_max_voltage(pdo); 4526 min_snk_mv = pdo_min_voltage(pdo); 4527 break; 4528 case PDO_TYPE_APDO: 4529 if (pdo_apdo_type(pdo) == APDO_TYPE_SPR_AVS) { 4530 port->spr_avs_data.port_snk_status = SPR_AVS_SUPPORTED; 4531 port->spr_avs_data.port_snk_pdo_index = j; 4532 } 4533 continue; 4534 default: 4535 tcpm_log(port, "Invalid sink PDO type, ignoring"); 4536 continue; 4537 } 4538 4539 if (max_src_mv <= max_snk_mv && 4540 min_src_mv >= min_snk_mv) { 4541 /* Prefer higher voltages if available */ 4542 if ((src_mw == max_mw && min_src_mv > max_mv) || 4543 src_mw > max_mw) { 4544 *src_pdo = i; 4545 *sink_pdo = j; 4546 max_mw = src_mw; 4547 max_mv = min_src_mv; 4548 ret = 0; 4549 } 4550 } 4551 } 4552 } 4553 4554 if (port->spr_avs_data.port_snk_status == SPR_AVS_UNKNOWN) 4555 port->spr_avs_data.port_snk_status = SPR_AVS_NOT_SUPPORTED; 4556 4557 if (port->spr_avs_data.port_partner_src_status == SPR_AVS_UNKNOWN) 4558 port->spr_avs_data.port_partner_src_status = SPR_AVS_NOT_SUPPORTED; 4559 4560 if (port->pps_data.supported && 4561 port->spr_avs_data.port_partner_src_status == SPR_AVS_SUPPORTED) 4562 port->usb_type = POWER_SUPPLY_USB_TYPE_PD_PPS_SPR_AVS; 4563 else if (port->pps_data.supported) 4564 port->usb_type = POWER_SUPPLY_USB_TYPE_PD_PPS; 4565 else if (port->spr_avs_data.port_partner_src_status == SPR_AVS_SUPPORTED) 4566 port->usb_type = POWER_SUPPLY_USB_TYPE_PD_SPR_AVS; 4567 4568 if (port->usb_type != POWER_SUPPLY_USB_TYPE_PD) 4569 power_supply_changed(port->psy); 4570 4571 return ret; 4572 } 4573 4574 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port) 4575 { 4576 unsigned int i, src_ma, max_temp_mw = 0, max_op_ma, op_mw; 4577 unsigned int src_pdo = 0; 4578 u32 pdo, src; 4579 4580 for (i = 1; i < port->nr_source_caps; ++i) { 4581 pdo = port->source_caps[i]; 4582 4583 switch (pdo_type(pdo)) { 4584 case PDO_TYPE_APDO: 4585 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) { 4586 tcpm_log(port, "Not PPS APDO (source), ignoring"); 4587 continue; 4588 } 4589 4590 if (port->pps_data.req_out_volt > pdo_pps_apdo_max_voltage(pdo) || 4591 port->pps_data.req_out_volt < pdo_pps_apdo_min_voltage(pdo)) 4592 continue; 4593 4594 src_ma = pdo_pps_apdo_max_current(pdo); 4595 max_op_ma = min(src_ma, port->pps_data.req_op_curr); 4596 op_mw = max_op_ma * port->pps_data.req_out_volt / 1000; 4597 if (op_mw > max_temp_mw) { 4598 src_pdo = i; 4599 max_temp_mw = op_mw; 4600 } 4601 break; 4602 default: 4603 tcpm_log(port, "Not APDO type (source), ignoring"); 4604 continue; 4605 } 4606 } 4607 4608 if (src_pdo) { 4609 src = port->source_caps[src_pdo]; 4610 4611 port->pps_data.req_min_volt = pdo_pps_apdo_min_voltage(src); 4612 port->pps_data.req_max_volt = pdo_pps_apdo_max_voltage(src); 4613 port->pps_data.req_max_curr = pdo_pps_apdo_max_current(src); 4614 port->pps_data.req_op_curr = min(port->pps_data.req_max_curr, 4615 port->pps_data.req_op_curr); 4616 } 4617 4618 return src_pdo; 4619 } 4620 4621 static int tcpm_pd_select_spr_avs_apdo(struct tcpm_port *port) 4622 { 4623 u32 req_out_volt_mv, req_op_curr_ma, src_max_curr_ma = 0, source_cap; 4624 u32 snk_max_curr_ma = 0, src_pdo_index, snk_pdo_index, snk_pdo; 4625 4626 if (port->spr_avs_data.port_snk_status != SPR_AVS_SUPPORTED || 4627 port->spr_avs_data.port_partner_src_status != 4628 SPR_AVS_SUPPORTED) { 4629 tcpm_log(port, "SPR AVS not supported. port:%s partner:%s", 4630 spr_avs_status_strings[port->spr_avs_data.port_snk_status], 4631 spr_avs_status_strings[port->spr_avs_data.port_partner_src_status]); 4632 return -EOPNOTSUPP; 4633 } 4634 4635 /* Round up to SPR_AVS_VOLT_MV_STEP */ 4636 req_out_volt_mv = port->spr_avs_data.req_out_volt_mv; 4637 if (req_out_volt_mv % SPR_AVS_VOLT_MV_STEP) { 4638 req_out_volt_mv += SPR_AVS_VOLT_MV_STEP - 4639 (req_out_volt_mv % SPR_AVS_VOLT_MV_STEP); 4640 port->spr_avs_data.req_out_volt_mv = req_out_volt_mv; 4641 } 4642 4643 /* Round up to RDO_SPR_AVS_CURR_MA_STEP */ 4644 req_op_curr_ma = port->spr_avs_data.req_op_curr_ma; 4645 if (req_op_curr_ma % RDO_SPR_AVS_CURR_MA_STEP) { 4646 req_op_curr_ma += RDO_SPR_AVS_CURR_MA_STEP - 4647 (req_op_curr_ma % RDO_SPR_AVS_CURR_MA_STEP); 4648 port->spr_avs_data.req_op_curr_ma = req_op_curr_ma; 4649 } 4650 4651 src_pdo_index = port->spr_avs_data.port_partner_src_pdo_index; 4652 snk_pdo_index = port->spr_avs_data.port_snk_pdo_index; 4653 source_cap = port->source_caps[src_pdo_index]; 4654 snk_pdo = port->snk_pdo[snk_pdo_index]; 4655 tcpm_log(port, 4656 "SPR AVS src_pdo_index:%d snk_pdo_index:%d req_op_curr_ma roundup:%u req_out_volt_mv roundup:%u", 4657 src_pdo_index, snk_pdo_index, req_op_curr_ma, req_out_volt_mv); 4658 4659 if (req_out_volt_mv >= SPR_AVS_TIER1_MIN_VOLT_MV && 4660 req_out_volt_mv <= SPR_AVS_TIER1_MAX_VOLT_MV) { 4661 src_max_curr_ma = 4662 pdo_spr_avs_apdo_9v_to_15v_max_current_ma(source_cap); 4663 snk_max_curr_ma = 4664 pdo_spr_avs_apdo_9v_to_15v_max_current_ma(snk_pdo); 4665 } else if (req_out_volt_mv > SPR_AVS_TIER1_MAX_VOLT_MV && 4666 req_out_volt_mv <= SPR_AVS_TIER2_MAX_VOLT_MV) { 4667 src_max_curr_ma = 4668 pdo_spr_avs_apdo_15v_to_20v_max_current_ma(source_cap); 4669 snk_max_curr_ma = 4670 pdo_spr_avs_apdo_15v_to_20v_max_current_ma(snk_pdo); 4671 } else { 4672 tcpm_log(port, "Invalid SPR AVS req_volt:%umV", req_out_volt_mv); 4673 return -EINVAL; 4674 } 4675 4676 if (req_op_curr_ma > src_max_curr_ma || 4677 req_op_curr_ma > snk_max_curr_ma) { 4678 tcpm_log(port, 4679 "Invalid SPR AVS request. req_volt:%umV req_curr:%umA src_max_cur:%umA snk_max_cur:%umA", 4680 req_out_volt_mv, req_op_curr_ma, src_max_curr_ma, 4681 snk_max_curr_ma); 4682 return -EINVAL; 4683 } 4684 4685 /* Max SPR voltage based on both the port and the partner caps */ 4686 if (pdo_spr_avs_apdo_15v_to_20v_max_current_ma(snk_pdo) && 4687 pdo_spr_avs_apdo_15v_to_20v_max_current_ma(source_cap)) 4688 port->spr_avs_data.max_out_volt_mv = SPR_AVS_TIER2_MAX_VOLT_MV; 4689 else 4690 port->spr_avs_data.max_out_volt_mv = SPR_AVS_TIER1_MAX_VOLT_MV; 4691 4692 /* 4693 * Max SPR AVS curr based on 9V to 15V. This should be higher than or 4694 * equal to 15V to 20V range. 4695 */ 4696 port->spr_avs_data.max_current_ma = 4697 min(pdo_spr_avs_apdo_9v_to_15v_max_current_ma(source_cap), 4698 pdo_spr_avs_apdo_9v_to_15v_max_current_ma(snk_pdo)); 4699 4700 return src_pdo_index; 4701 } 4702 4703 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo) 4704 { 4705 unsigned int mv, ma, mw, flags; 4706 unsigned int max_ma, max_mw; 4707 enum pd_pdo_type type; 4708 u32 pdo, matching_snk_pdo; 4709 int src_pdo_index = 0; 4710 int snk_pdo_index = 0; 4711 int ret; 4712 4713 ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index); 4714 if (ret < 0) 4715 return ret; 4716 4717 pdo = port->source_caps[src_pdo_index]; 4718 matching_snk_pdo = port->snk_pdo[snk_pdo_index]; 4719 type = pdo_type(pdo); 4720 4721 switch (type) { 4722 case PDO_TYPE_FIXED: 4723 mv = pdo_fixed_voltage(pdo); 4724 break; 4725 case PDO_TYPE_BATT: 4726 case PDO_TYPE_VAR: 4727 mv = pdo_min_voltage(pdo); 4728 break; 4729 default: 4730 tcpm_log(port, "Invalid PDO selected!"); 4731 return -EINVAL; 4732 } 4733 4734 /* Select maximum available current within the sink pdo's limit */ 4735 if (type == PDO_TYPE_BATT) { 4736 mw = min_power(pdo, matching_snk_pdo); 4737 ma = 1000 * mw / mv; 4738 } else { 4739 ma = min_current(pdo, matching_snk_pdo); 4740 mw = ma * mv / 1000; 4741 } 4742 4743 flags = RDO_USB_COMM | RDO_NO_SUSPEND; 4744 4745 /* Set mismatch bit if offered power is less than operating power */ 4746 max_ma = ma; 4747 max_mw = mw; 4748 if (mw < port->operating_snk_mw) { 4749 flags |= RDO_CAP_MISMATCH; 4750 if (type == PDO_TYPE_BATT && 4751 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo))) 4752 max_mw = pdo_max_power(matching_snk_pdo); 4753 else if (pdo_max_current(matching_snk_pdo) > 4754 pdo_max_current(pdo)) 4755 max_ma = pdo_max_current(matching_snk_pdo); 4756 } 4757 4758 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d", 4759 port->cc_req, port->cc1, port->cc2, port->vbus_source, 4760 port->vconn_role == TYPEC_SOURCE ? "source" : "sink", 4761 port->polarity); 4762 4763 if (type == PDO_TYPE_BATT) { 4764 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags); 4765 4766 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s", 4767 src_pdo_index, mv, mw, 4768 flags & RDO_CAP_MISMATCH ? " [mismatch]" : ""); 4769 } else { 4770 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags); 4771 4772 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s", 4773 src_pdo_index, mv, ma, 4774 flags & RDO_CAP_MISMATCH ? " [mismatch]" : ""); 4775 } 4776 4777 port->req_current_limit = ma; 4778 port->req_supply_voltage = mv; 4779 4780 return 0; 4781 } 4782 4783 static int tcpm_pd_send_request(struct tcpm_port *port) 4784 { 4785 struct pd_message msg; 4786 int ret; 4787 u32 rdo; 4788 4789 ret = tcpm_pd_build_request(port, &rdo); 4790 if (ret < 0) 4791 return ret; 4792 4793 /* 4794 * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition. 4795 * It is safer to modify the threshold here. 4796 */ 4797 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); 4798 4799 memset(&msg, 0, sizeof(msg)); 4800 msg.header = PD_HEADER_LE(PD_DATA_REQUEST, 4801 port->pwr_role, 4802 port->data_role, 4803 port->negotiated_rev, 4804 port->message_id, 1); 4805 msg.payload[0] = cpu_to_le32(rdo); 4806 4807 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 4808 } 4809 4810 static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo) 4811 { 4812 unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags; 4813 unsigned int src_pdo_index; 4814 4815 src_pdo_index = tcpm_pd_select_pps_apdo(port); 4816 if (!src_pdo_index) 4817 return -EOPNOTSUPP; 4818 4819 max_mv = port->pps_data.req_max_volt; 4820 max_ma = port->pps_data.req_max_curr; 4821 out_mv = port->pps_data.req_out_volt; 4822 op_ma = port->pps_data.req_op_curr; 4823 4824 flags = RDO_USB_COMM | RDO_NO_SUSPEND; 4825 4826 op_mw = (op_ma * out_mv) / 1000; 4827 if (op_mw < port->operating_snk_mw) { 4828 /* 4829 * Try raising current to meet power needs. If that's not enough 4830 * then try upping the voltage. If that's still not enough 4831 * then we've obviously chosen a PPS APDO which really isn't 4832 * suitable so abandon ship. 4833 */ 4834 op_ma = (port->operating_snk_mw * 1000) / out_mv; 4835 if ((port->operating_snk_mw * 1000) % out_mv) 4836 ++op_ma; 4837 op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP); 4838 4839 if (op_ma > max_ma) { 4840 op_ma = max_ma; 4841 out_mv = (port->operating_snk_mw * 1000) / op_ma; 4842 if ((port->operating_snk_mw * 1000) % op_ma) 4843 ++out_mv; 4844 out_mv += RDO_PROG_VOLT_MV_STEP - 4845 (out_mv % RDO_PROG_VOLT_MV_STEP); 4846 4847 if (out_mv > max_mv) { 4848 tcpm_log(port, "Invalid PPS APDO selected!"); 4849 return -EINVAL; 4850 } 4851 } 4852 } 4853 4854 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d", 4855 port->cc_req, port->cc1, port->cc2, port->vbus_source, 4856 port->vconn_role == TYPEC_SOURCE ? "source" : "sink", 4857 port->polarity); 4858 4859 *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags); 4860 4861 tcpm_log(port, "Requesting APDO %d: %u mV, %u mA", 4862 src_pdo_index, out_mv, op_ma); 4863 4864 port->pps_data.req_op_curr = op_ma; 4865 port->pps_data.req_out_volt = out_mv; 4866 4867 return 0; 4868 } 4869 4870 static int tcpm_pd_build_spr_avs_request(struct tcpm_port *port, u32 *rdo) 4871 { 4872 u32 out_mv, op_ma, flags, snk_pdo_index, source_cap; 4873 unsigned int src_power_mw, snk_power_mw; 4874 int src_pdo_index; 4875 u32 snk_pdo; 4876 4877 src_pdo_index = tcpm_pd_select_spr_avs_apdo(port); 4878 if (src_pdo_index < 0) 4879 return src_pdo_index; 4880 snk_pdo_index = port->spr_avs_data.port_snk_pdo_index; 4881 source_cap = port->source_caps[src_pdo_index]; 4882 snk_pdo = port->snk_pdo[snk_pdo_index]; 4883 out_mv = port->spr_avs_data.req_out_volt_mv; 4884 op_ma = port->spr_avs_data.req_op_curr_ma; 4885 4886 flags = RDO_USB_COMM | RDO_NO_SUSPEND; 4887 4888 /* 4889 * Set capability mismatch when the maximum power needs in the current 4890 * requested AVS voltage tier range is greater than 4891 * port->operating_snk_mw, however, the maximum power offered by the 4892 * source at the current requested AVS voltage tier is less than 4893 * port->operating_sink_mw. 4894 */ 4895 if (out_mv > SPR_AVS_TIER1_MAX_VOLT_MV) { 4896 src_power_mw = 4897 pdo_spr_avs_apdo_15v_to_20v_max_current_ma(source_cap) * 4898 SPR_AVS_TIER2_MAX_VOLT_MV / 1000; 4899 snk_power_mw = 4900 pdo_spr_avs_apdo_15v_to_20v_max_current_ma(snk_pdo) * 4901 SPR_AVS_TIER2_MAX_VOLT_MV / 1000; 4902 } else { 4903 src_power_mw = 4904 pdo_spr_avs_apdo_9v_to_15v_max_current_ma(source_cap) * 4905 SPR_AVS_TIER1_MAX_VOLT_MV / 1000; 4906 snk_power_mw = 4907 pdo_spr_avs_apdo_9v_to_15v_max_current_ma(snk_pdo) * 4908 SPR_AVS_TIER1_MAX_VOLT_MV / 1000; 4909 } 4910 4911 if (snk_power_mw >= port->operating_snk_mw && 4912 src_power_mw < port->operating_snk_mw) 4913 flags |= RDO_CAP_MISMATCH; 4914 4915 *rdo = RDO_AVS(src_pdo_index + 1, out_mv, op_ma, flags); 4916 4917 tcpm_log(port, "Requesting APDO SPR AVS %d: %u mV, %u mA", 4918 src_pdo_index, out_mv, op_ma); 4919 4920 return 0; 4921 } 4922 4923 static int tcpm_pd_send_aug_supply_request(struct tcpm_port *port, 4924 enum aug_req_type type) 4925 { 4926 struct pd_message msg; 4927 int ret; 4928 u32 rdo; 4929 4930 if (type == PD_PPS) { 4931 ret = tcpm_pd_build_pps_request(port, &rdo); 4932 } else if (type == PD_SPR_AVS) { 4933 ret = tcpm_pd_build_spr_avs_request(port, &rdo); 4934 } else { 4935 tcpm_log(port, "Invalid aug_req_type %d", type); 4936 ret = -EOPNOTSUPP; 4937 } 4938 if (ret < 0) 4939 return ret; 4940 4941 /* Relax the threshold as voltage will be adjusted right after Accept Message. */ 4942 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); 4943 4944 memset(&msg, 0, sizeof(msg)); 4945 msg.header = PD_HEADER_LE(PD_DATA_REQUEST, 4946 port->pwr_role, 4947 port->data_role, 4948 port->negotiated_rev, 4949 port->message_id, 1); 4950 msg.payload[0] = cpu_to_le32(rdo); 4951 4952 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); 4953 } 4954 4955 static int tcpm_set_vbus(struct tcpm_port *port, bool enable) 4956 { 4957 int ret; 4958 4959 if (enable && port->vbus_charge) 4960 return -EINVAL; 4961 4962 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge); 4963 4964 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge); 4965 if (ret < 0) 4966 return ret; 4967 4968 port->vbus_source = enable; 4969 return 0; 4970 } 4971 4972 static int tcpm_set_charge(struct tcpm_port *port, bool charge) 4973 { 4974 int ret; 4975 4976 if (charge && port->vbus_source) 4977 return -EINVAL; 4978 4979 if (charge != port->vbus_charge) { 4980 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge); 4981 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source, 4982 charge); 4983 if (ret < 0) 4984 return ret; 4985 } 4986 port->vbus_charge = charge; 4987 power_supply_changed(port->psy); 4988 return 0; 4989 } 4990 4991 static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc) 4992 { 4993 int ret; 4994 4995 if (!port->tcpc->start_toggling) 4996 return false; 4997 4998 tcpm_log_force(port, "Start toggling"); 4999 ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc); 5000 return ret == 0; 5001 } 5002 5003 static int tcpm_init_vbus(struct tcpm_port *port) 5004 { 5005 int ret; 5006 5007 ret = port->tcpc->set_vbus(port->tcpc, false, false); 5008 port->vbus_source = false; 5009 port->vbus_charge = false; 5010 return ret; 5011 } 5012 5013 static int tcpm_init_vconn(struct tcpm_port *port) 5014 { 5015 int ret; 5016 5017 ret = port->tcpc->set_vconn(port->tcpc, false); 5018 port->vconn_role = TYPEC_SINK; 5019 return ret; 5020 } 5021 5022 static void tcpm_typec_connect(struct tcpm_port *port) 5023 { 5024 struct typec_partner *partner; 5025 5026 if (!port->connected) { 5027 port->connected = true; 5028 /* Make sure we don't report stale identity information */ 5029 memset(&port->partner_ident, 0, sizeof(port->partner_ident)); 5030 port->partner_desc.usb_pd = port->pd_capable; 5031 if (tcpm_port_is_debug(port)) 5032 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG; 5033 else if (tcpm_port_is_audio(port)) 5034 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO; 5035 else 5036 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE; 5037 partner = typec_register_partner(port->typec_port, &port->partner_desc); 5038 if (IS_ERR(partner)) { 5039 dev_err(port->dev, "Failed to register partner (%ld)\n", PTR_ERR(partner)); 5040 return; 5041 } 5042 5043 port->partner = partner; 5044 typec_partner_set_usb_power_delivery(port->partner, port->partner_pd); 5045 } 5046 } 5047 5048 static int tcpm_src_attach(struct tcpm_port *port) 5049 { 5050 enum typec_cc_polarity polarity = 5051 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2 5052 : TYPEC_POLARITY_CC1; 5053 int ret; 5054 5055 if (port->attached) 5056 return 0; 5057 5058 ret = tcpm_set_polarity(port, polarity); 5059 if (ret < 0) 5060 return ret; 5061 5062 tcpm_enable_auto_vbus_discharge(port, true); 5063 5064 /* 5065 * USB Type-C specification, version 1.2, 5066 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements) 5067 * Enable VCONN only if the non-RD port is set to RA. 5068 */ 5069 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) || 5070 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) { 5071 ret = tcpm_set_vconn(port, true); 5072 if (ret < 0) 5073 return ret; 5074 } 5075 5076 ret = tcpm_set_vbus(port, true); 5077 if (ret < 0) 5078 goto out_disable_vconn; 5079 5080 ret = tcpm_set_roles(port, true, TYPEC_STATE_USB, TYPEC_SOURCE, 5081 tcpm_data_role_for_source(port)); 5082 if (ret < 0) 5083 goto out_disable_vbus; 5084 5085 if (port->pd_supported) { 5086 ret = port->tcpc->set_pd_rx(port->tcpc, true); 5087 if (ret < 0) 5088 goto out_disable_mux; 5089 } 5090 5091 port->pd_capable = false; 5092 5093 port->partner = NULL; 5094 5095 port->attached = true; 5096 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_UNKNOWN); 5097 5098 return 0; 5099 5100 out_disable_mux: 5101 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE, 5102 TYPEC_ORIENTATION_NONE); 5103 out_disable_vbus: 5104 tcpm_set_vbus(port, false); 5105 out_disable_vconn: 5106 tcpm_set_vconn(port, false); 5107 5108 return ret; 5109 } 5110 5111 static void tcpm_typec_disconnect(struct tcpm_port *port) 5112 { 5113 /* 5114 * Unregister plug/cable outside of port->connected because cable can 5115 * be discovered before SRC_READY/SNK_READY states where port->connected 5116 * is set. 5117 */ 5118 typec_unregister_plug(port->plug_prime); 5119 typec_unregister_cable(port->cable); 5120 port->plug_prime = NULL; 5121 port->cable = NULL; 5122 if (port->connected) { 5123 if (port->partner) { 5124 typec_partner_set_usb_power_delivery(port->partner, NULL); 5125 typec_unregister_partner(port->partner); 5126 port->partner = NULL; 5127 } 5128 port->connected = false; 5129 } 5130 } 5131 5132 static void tcpm_unregister_altmodes(struct tcpm_port *port) 5133 { 5134 struct pd_mode_data *modep = &port->mode_data; 5135 struct pd_mode_data *modep_prime = &port->mode_data_prime; 5136 int i; 5137 5138 for (i = 0; i < modep->altmodes && i < ALTMODE_DISCOVERY_MAX; i++) { 5139 typec_unregister_altmode(port->partner_altmode[i]); 5140 port->partner_altmode[i] = NULL; 5141 } 5142 for (i = 0; i < modep_prime->altmodes && i < ALTMODE_DISCOVERY_MAX; i++) { 5143 typec_unregister_altmode(port->plug_prime_altmode[i]); 5144 port->plug_prime_altmode[i] = NULL; 5145 } 5146 5147 memset(modep, 0, sizeof(*modep)); 5148 memset(modep_prime, 0, sizeof(*modep_prime)); 5149 } 5150 5151 static void tcpm_set_partner_usb_comm_capable(struct tcpm_port *port, bool capable) 5152 { 5153 tcpm_log(port, "Setting usb_comm capable %s", str_true_false(capable)); 5154 5155 if (port->tcpc->set_partner_usb_comm_capable) 5156 port->tcpc->set_partner_usb_comm_capable(port->tcpc, capable); 5157 } 5158 5159 static void tcpm_partner_source_caps_reset(struct tcpm_port *port) 5160 { 5161 usb_power_delivery_unregister_capabilities(port->partner_source_caps); 5162 port->partner_source_caps = NULL; 5163 port->spr_avs_data.port_partner_src_status = SPR_AVS_UNKNOWN; 5164 port->spr_avs_data.active = false; 5165 } 5166 5167 static void tcpm_reset_port(struct tcpm_port *port) 5168 { 5169 tcpm_enable_auto_vbus_discharge(port, false); 5170 port->in_ams = false; 5171 port->ams = NONE_AMS; 5172 port->vdm_sm_running = false; 5173 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_UNKNOWN); 5174 mod_vdm_discovery_cancel_delayed_work(port); 5175 tcpm_unregister_altmodes(port); 5176 tcpm_typec_disconnect(port); 5177 port->attached = false; 5178 port->pd_capable = false; 5179 port->pps_data.supported = false; 5180 tcpm_set_partner_usb_comm_capable(port, false); 5181 5182 /* 5183 * First Rx ID should be 0; set this to a sentinel of -1 so that 5184 * we can check tcpm_pd_rx_handler() if we had seen it before. 5185 */ 5186 port->rx_msgid = -1; 5187 port->rx_msgid_prime = -1; 5188 5189 port->tcpc->set_pd_rx(port->tcpc, false); 5190 tcpm_init_vbus(port); /* also disables charging */ 5191 tcpm_init_vconn(port); 5192 tcpm_set_current_limit(port, 0, 0); 5193 tcpm_set_polarity(port, TYPEC_POLARITY_CC1); 5194 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE, 5195 TYPEC_ORIENTATION_NONE); 5196 tcpm_set_attached_state(port, false); 5197 port->try_src_count = 0; 5198 port->try_snk_count = 0; 5199 port->usb_type = POWER_SUPPLY_USB_TYPE_C; 5200 power_supply_changed(port->psy); 5201 port->nr_sink_caps = 0; 5202 port->sink_cap_done = false; 5203 if (port->tcpc->enable_frs) 5204 port->tcpc->enable_frs(port->tcpc, false); 5205 5206 usb_power_delivery_unregister_capabilities(port->partner_sink_caps); 5207 port->partner_sink_caps = NULL; 5208 tcpm_partner_source_caps_reset(port); 5209 usb_power_delivery_unregister(port->partner_pd); 5210 port->partner_pd = NULL; 5211 } 5212 5213 static void tcpm_detach(struct tcpm_port *port) 5214 { 5215 if (tcpm_port_is_disconnected(port)) 5216 port->hard_reset_count = 0; 5217 5218 if (!port->attached) 5219 return; 5220 5221 if (port->tcpc->set_bist_data) { 5222 tcpm_log(port, "disable BIST MODE TESTDATA"); 5223 port->tcpc->set_bist_data(port->tcpc, false); 5224 } 5225 5226 tcpm_reset_port(port); 5227 } 5228 5229 static void tcpm_src_detach(struct tcpm_port *port) 5230 { 5231 tcpm_detach(port); 5232 } 5233 5234 static int tcpm_snk_attach(struct tcpm_port *port) 5235 { 5236 int ret; 5237 5238 if (port->attached) 5239 return 0; 5240 5241 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ? 5242 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1); 5243 if (ret < 0) 5244 return ret; 5245 5246 tcpm_enable_auto_vbus_discharge(port, true); 5247 5248 ret = tcpm_set_roles(port, true, TYPEC_STATE_USB, 5249 TYPEC_SINK, tcpm_data_role_for_sink(port)); 5250 if (ret < 0) 5251 return ret; 5252 5253 port->pd_capable = false; 5254 5255 port->partner = NULL; 5256 5257 port->attached = true; 5258 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_UNKNOWN); 5259 5260 return 0; 5261 } 5262 5263 static void tcpm_snk_detach(struct tcpm_port *port) 5264 { 5265 tcpm_detach(port); 5266 } 5267 5268 static int tcpm_acc_attach(struct tcpm_port *port) 5269 { 5270 int ret; 5271 enum typec_role role; 5272 enum typec_data_role data; 5273 int state = TYPEC_STATE_USB; 5274 5275 if (port->attached) 5276 return 0; 5277 5278 role = tcpm_port_is_sink(port) ? TYPEC_SINK : TYPEC_SOURCE; 5279 data = tcpm_port_is_sink(port) ? tcpm_data_role_for_sink(port) 5280 : tcpm_data_role_for_source(port); 5281 5282 if (tcpm_port_is_audio(port)) 5283 state = TYPEC_MODE_AUDIO; 5284 5285 if (tcpm_port_is_debug(port)) 5286 state = TYPEC_MODE_DEBUG; 5287 5288 ret = tcpm_set_roles(port, true, state, role, data); 5289 if (ret < 0) 5290 return ret; 5291 5292 port->partner = NULL; 5293 5294 tcpm_typec_connect(port); 5295 5296 port->attached = true; 5297 5298 return 0; 5299 } 5300 5301 static void tcpm_acc_detach(struct tcpm_port *port) 5302 { 5303 tcpm_detach(port); 5304 } 5305 5306 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port) 5307 { 5308 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) 5309 return HARD_RESET_SEND; 5310 if (port->pd_capable) 5311 return ERROR_RECOVERY; 5312 if (port->pwr_role == TYPEC_SOURCE) 5313 return SRC_UNATTACHED; 5314 if (port->state == SNK_WAIT_CAPABILITIES || 5315 port->state == SNK_WAIT_CAPABILITIES_TIMEOUT) 5316 return SNK_READY; 5317 return SNK_UNATTACHED; 5318 } 5319 5320 static inline enum tcpm_state unattached_state(struct tcpm_port *port) 5321 { 5322 if (port->port_type == TYPEC_PORT_DRP) { 5323 if (port->pwr_role == TYPEC_SOURCE) 5324 return SRC_UNATTACHED; 5325 else 5326 return SNK_UNATTACHED; 5327 } else if (port->port_type == TYPEC_PORT_SRC) { 5328 return SRC_UNATTACHED; 5329 } 5330 5331 return SNK_UNATTACHED; 5332 } 5333 5334 static void tcpm_swap_complete(struct tcpm_port *port, int result) 5335 { 5336 if (port->swap_pending) { 5337 port->swap_status = result; 5338 port->swap_pending = false; 5339 port->non_pd_role_swap = false; 5340 complete(&port->swap_complete); 5341 } 5342 } 5343 5344 static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc) 5345 { 5346 switch (cc) { 5347 case TYPEC_CC_RP_1_5: 5348 return TYPEC_PWR_MODE_1_5A; 5349 case TYPEC_CC_RP_3_0: 5350 return TYPEC_PWR_MODE_3_0A; 5351 case TYPEC_CC_RP_DEF: 5352 default: 5353 return TYPEC_PWR_MODE_USB; 5354 } 5355 } 5356 5357 static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode) 5358 { 5359 switch (opmode) { 5360 case TYPEC_PWR_MODE_USB: 5361 return TYPEC_CC_RP_DEF; 5362 case TYPEC_PWR_MODE_1_5A: 5363 return TYPEC_CC_RP_1_5; 5364 case TYPEC_PWR_MODE_3_0A: 5365 case TYPEC_PWR_MODE_PD: 5366 default: 5367 return TYPEC_CC_RP_3_0; 5368 } 5369 } 5370 5371 static void tcpm_set_initial_svdm_version(struct tcpm_port *port) 5372 { 5373 if (!port->partner) 5374 return; 5375 5376 switch (port->negotiated_rev) { 5377 case PD_REV30: 5378 break; 5379 /* 5380 * 6.4.4.2.3 Structured VDM Version 5381 * 2.0 states "At this time, there is only one version (1.0) defined. 5382 * This field Shall be set to zero to indicate Version 1.0." 5383 * 3.0 states "This field Shall be set to 01b to indicate Version 2.0." 5384 * To ensure that we follow the Power Delivery revision we are currently 5385 * operating on, downgrade the SVDM version to the highest one supported 5386 * by the Power Delivery revision. 5387 */ 5388 case PD_REV20: 5389 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0); 5390 break; 5391 default: 5392 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0); 5393 break; 5394 } 5395 } 5396 5397 static void tcpm_set_initial_negotiated_rev(struct tcpm_port *port) 5398 { 5399 switch (port->pd_rev.rev_major) { 5400 case PD_CAP_REV10: 5401 port->negotiated_rev = PD_REV10; 5402 break; 5403 case PD_CAP_REV20: 5404 port->negotiated_rev = PD_REV20; 5405 break; 5406 case PD_CAP_REV30: 5407 port->negotiated_rev = PD_REV30; 5408 break; 5409 default: 5410 port->negotiated_rev = PD_MAX_REV; 5411 break; 5412 } 5413 port->negotiated_rev_prime = port->negotiated_rev; 5414 } 5415 5416 static void run_state_machine(struct tcpm_port *port) 5417 { 5418 int ret; 5419 enum typec_pwr_opmode opmode; 5420 unsigned int msecs; 5421 enum tcpm_state upcoming_state; 5422 5423 if (port->tcpc->check_contaminant && port->state != CHECK_CONTAMINANT) 5424 port->potential_contaminant = ((port->enter_state == SRC_ATTACH_WAIT && 5425 port->state == SRC_UNATTACHED) || 5426 (port->enter_state == SNK_ATTACH_WAIT && 5427 port->state == SNK_UNATTACHED) || 5428 (port->enter_state == SNK_DEBOUNCED && 5429 port->state == SNK_UNATTACHED)); 5430 5431 port->enter_state = port->state; 5432 switch (port->state) { 5433 case TOGGLING: 5434 break; 5435 case CHECK_CONTAMINANT: 5436 port->tcpc->check_contaminant(port->tcpc); 5437 break; 5438 /* SRC states */ 5439 case SRC_UNATTACHED: 5440 if (!port->non_pd_role_swap) 5441 tcpm_swap_complete(port, -ENOTCONN); 5442 tcpm_src_detach(port); 5443 if (port->potential_contaminant) { 5444 tcpm_set_state(port, CHECK_CONTAMINANT, 0); 5445 break; 5446 } 5447 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) { 5448 tcpm_set_state(port, TOGGLING, 0); 5449 break; 5450 } 5451 tcpm_set_cc(port, tcpm_rp_cc(port)); 5452 if (port->port_type == TYPEC_PORT_DRP) 5453 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK); 5454 break; 5455 case SRC_ATTACH_WAIT: 5456 if (tcpm_port_is_debug_source(port)) 5457 tcpm_set_state(port, DEBUG_ACC_ATTACHED, 5458 port->timings.cc_debounce_time); 5459 else if (tcpm_port_is_audio(port)) 5460 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 5461 port->timings.cc_debounce_time); 5462 else if (tcpm_port_is_source(port) && port->vbus_vsafe0v) 5463 tcpm_set_state(port, 5464 tcpm_try_snk(port) ? SNK_TRY 5465 : SRC_ATTACHED, 5466 port->timings.cc_debounce_time); 5467 break; 5468 5469 case SNK_TRY: 5470 port->try_snk_count++; 5471 /* 5472 * Requirements: 5473 * - Do not drive vconn or vbus 5474 * - Terminate CC pins (both) to Rd 5475 * Action: 5476 * - Wait for tDRPTry (PD_T_DRP_TRY). 5477 * Until then, ignore any state changes. 5478 */ 5479 tcpm_set_cc(port, TYPEC_CC_RD); 5480 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY); 5481 break; 5482 case SNK_TRY_WAIT: 5483 if (tcpm_port_is_sink(port)) { 5484 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0); 5485 } else { 5486 tcpm_set_state(port, SRC_TRYWAIT, 0); 5487 port->max_wait = 0; 5488 } 5489 break; 5490 case SNK_TRY_WAIT_DEBOUNCE: 5491 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 5492 PD_T_TRY_CC_DEBOUNCE); 5493 break; 5494 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: 5495 if (port->vbus_present && tcpm_port_is_sink(port)) 5496 tcpm_set_state(port, SNK_ATTACHED, 0); 5497 else 5498 port->max_wait = 0; 5499 break; 5500 case SRC_TRYWAIT: 5501 tcpm_set_cc(port, tcpm_rp_cc(port)); 5502 if (port->max_wait == 0) { 5503 port->max_wait = jiffies + 5504 msecs_to_jiffies(PD_T_DRP_TRY); 5505 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, 5506 PD_T_DRP_TRY); 5507 } else { 5508 if (time_is_after_jiffies(port->max_wait)) 5509 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, 5510 jiffies_to_msecs(port->max_wait - 5511 jiffies)); 5512 else 5513 tcpm_set_state(port, SNK_UNATTACHED, 0); 5514 } 5515 break; 5516 case SRC_TRYWAIT_DEBOUNCE: 5517 tcpm_set_state(port, SRC_ATTACHED, port->timings.cc_debounce_time); 5518 break; 5519 case SRC_TRYWAIT_UNATTACHED: 5520 tcpm_set_state(port, SNK_UNATTACHED, 0); 5521 break; 5522 5523 case SRC_ATTACHED: 5524 ret = tcpm_src_attach(port); 5525 tcpm_set_state(port, SRC_UNATTACHED, 5526 ret < 0 ? 0 : PD_T_PS_SOURCE_ON); 5527 break; 5528 case SRC_STARTUP: 5529 opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port)); 5530 typec_set_pwr_opmode(port->typec_port, opmode); 5531 port->pwr_opmode = TYPEC_PWR_MODE_USB; 5532 port->caps_count = 0; 5533 tcpm_set_initial_negotiated_rev(port); 5534 port->message_id = 0; 5535 port->message_id_prime = 0; 5536 port->rx_msgid = -1; 5537 port->rx_msgid_prime = -1; 5538 port->explicit_contract = false; 5539 /* SNK -> SRC POWER/FAST_ROLE_SWAP finished */ 5540 if (port->ams == POWER_ROLE_SWAP || 5541 port->ams == FAST_ROLE_SWAP) 5542 tcpm_ams_finish(port); 5543 if (!port->pd_supported) { 5544 tcpm_set_state(port, SRC_READY, 0); 5545 break; 5546 } 5547 port->upcoming_state = SRC_SEND_CAPABILITIES; 5548 tcpm_ams_start(port, POWER_NEGOTIATION); 5549 break; 5550 case SRC_SEND_CAPABILITIES: 5551 port->caps_count++; 5552 if (port->caps_count > PD_N_CAPS_COUNT) { 5553 tcpm_set_state(port, SRC_READY, 0); 5554 break; 5555 } 5556 ret = tcpm_pd_send_source_caps(port); 5557 if (ret < 0) { 5558 if (tcpm_can_communicate_sop_prime(port) && 5559 IS_ERR_OR_NULL(port->cable)) 5560 tcpm_set_state(port, SRC_VDM_IDENTITY_REQUEST, 0); 5561 else 5562 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 5563 PD_T_SEND_SOURCE_CAP); 5564 } else { 5565 /* 5566 * Per standard, we should clear the reset counter here. 5567 * However, that can result in state machine hang-ups. 5568 * Reset it only in READY state to improve stability. 5569 */ 5570 /* port->hard_reset_count = 0; */ 5571 port->caps_count = 0; 5572 port->pd_capable = true; 5573 tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT, 5574 PD_T_SENDER_RESPONSE); 5575 } 5576 break; 5577 case SRC_SEND_CAPABILITIES_TIMEOUT: 5578 /* 5579 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout. 5580 * 5581 * PD 2.0 sinks are supposed to accept src-capabilities with a 5582 * 3.0 header and simply ignore any src PDOs which the sink does 5583 * not understand such as PPS but some 2.0 sinks instead ignore 5584 * the entire PD_DATA_SOURCE_CAP message, causing contract 5585 * negotiation to fail. 5586 * 5587 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try 5588 * sending src-capabilities with a lower PD revision to 5589 * make these broken sinks work. 5590 */ 5591 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) { 5592 tcpm_set_state(port, HARD_RESET_SEND, 0); 5593 } else if (port->negotiated_rev > PD_REV20) { 5594 port->negotiated_rev--; 5595 port->hard_reset_count = 0; 5596 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); 5597 } else { 5598 tcpm_set_state(port, hard_reset_state(port), 0); 5599 } 5600 break; 5601 case SRC_NEGOTIATE_CAPABILITIES: 5602 ret = tcpm_pd_check_request(port); 5603 if (ret < 0) { 5604 tcpm_pd_send_control(port, PD_CTRL_REJECT, TCPC_TX_SOP); 5605 if (!port->explicit_contract) { 5606 tcpm_set_state(port, 5607 SRC_WAIT_NEW_CAPABILITIES, 0); 5608 } else { 5609 tcpm_set_state(port, SRC_READY, 0); 5610 } 5611 } else { 5612 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP); 5613 tcpm_set_partner_usb_comm_capable(port, 5614 !!(port->sink_request & RDO_USB_COMM)); 5615 tcpm_set_state(port, SRC_TRANSITION_SUPPLY, 5616 PD_T_SRC_TRANSITION); 5617 } 5618 break; 5619 case SRC_TRANSITION_SUPPLY: 5620 /* XXX: regulator_set_voltage(vbus, ...) */ 5621 tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP); 5622 port->explicit_contract = true; 5623 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD); 5624 port->pwr_opmode = TYPEC_PWR_MODE_PD; 5625 tcpm_set_state_cond(port, SRC_READY, 0); 5626 break; 5627 case SRC_READY: 5628 #if 1 5629 port->hard_reset_count = 0; 5630 #endif 5631 port->try_src_count = 0; 5632 5633 tcpm_swap_complete(port, 0); 5634 tcpm_typec_connect(port); 5635 5636 if (port->ams != NONE_AMS) 5637 tcpm_ams_finish(port); 5638 if (port->next_ams != NONE_AMS) { 5639 port->ams = port->next_ams; 5640 port->next_ams = NONE_AMS; 5641 } 5642 5643 /* 5644 * If previous AMS is interrupted, switch to the upcoming 5645 * state. 5646 */ 5647 if (port->upcoming_state != INVALID_STATE) { 5648 upcoming_state = port->upcoming_state; 5649 port->upcoming_state = INVALID_STATE; 5650 tcpm_set_state(port, upcoming_state, 0); 5651 break; 5652 } 5653 5654 /* 5655 * 6.4.4.3.1 Discover Identity 5656 * "The Discover Identity Command Shall only be sent to SOP when there is an 5657 * Explicit Contract." 5658 * 5659 * Discover Identity on SOP' should be discovered prior to the 5660 * ready state, but if done after a Vconn Swap following Discover 5661 * Identity on SOP then the discovery process can be run here 5662 * as well. 5663 */ 5664 if (port->explicit_contract) { 5665 if (port->vdm_discovery_state == VDM_DISCOVERY_UNKNOWN) 5666 tcpm_set_initial_svdm_version(port); 5667 mod_vdm_discovery_delayed_work(port, 0); 5668 } else { 5669 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 5670 } 5671 5672 /* 5673 * 6.3.5 5674 * Sending ping messages is not necessary if 5675 * - the source operates at vSafe5V 5676 * or 5677 * - The system is not operating in PD mode 5678 * or 5679 * - Both partners are connected using a Type-C connector 5680 * 5681 * There is no actual need to send PD messages since the local 5682 * port type-c and the spec does not clearly say whether PD is 5683 * possible when type-c is connected to Type-A/B 5684 */ 5685 break; 5686 case SRC_WAIT_NEW_CAPABILITIES: 5687 /* Nothing to do... */ 5688 break; 5689 5690 /* SNK states */ 5691 case SNK_UNATTACHED: 5692 if (!port->non_pd_role_swap) 5693 tcpm_swap_complete(port, -ENOTCONN); 5694 tcpm_aug_supply_req_complete(port, -ENOTCONN); 5695 tcpm_snk_detach(port); 5696 if (port->potential_contaminant) { 5697 tcpm_set_state(port, CHECK_CONTAMINANT, 0); 5698 break; 5699 } 5700 if (tcpm_start_toggling(port, TYPEC_CC_RD)) { 5701 tcpm_set_state(port, TOGGLING, 0); 5702 break; 5703 } 5704 tcpm_set_cc(port, TYPEC_CC_RD); 5705 if (port->port_type == TYPEC_PORT_DRP) 5706 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC); 5707 break; 5708 case SNK_ATTACH_WAIT: 5709 if (tcpm_port_is_debug_sink(port)) 5710 tcpm_set_state(port, DEBUG_ACC_ATTACHED, 5711 PD_T_CC_DEBOUNCE); 5712 else if (tcpm_port_is_audio(port)) 5713 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 5714 PD_T_CC_DEBOUNCE); 5715 else if ((port->cc1 == TYPEC_CC_OPEN && 5716 port->cc2 != TYPEC_CC_OPEN) || 5717 (port->cc1 != TYPEC_CC_OPEN && 5718 port->cc2 == TYPEC_CC_OPEN)) 5719 tcpm_set_state(port, SNK_DEBOUNCED, 5720 port->timings.cc_debounce_time); 5721 else if (tcpm_port_is_disconnected(port)) 5722 tcpm_set_state(port, SNK_UNATTACHED, 5723 PD_T_PD_DEBOUNCE); 5724 break; 5725 case SNK_DEBOUNCED: 5726 if (tcpm_port_is_disconnected(port)) 5727 tcpm_set_state(port, SNK_UNATTACHED, 5728 PD_T_PD_DEBOUNCE); 5729 else if (tcpm_port_is_debug_sink(port)) 5730 tcpm_set_state(port, DEBUG_ACC_ATTACHED, 5731 PD_T_CC_DEBOUNCE); 5732 else if (tcpm_port_is_audio(port)) 5733 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 5734 PD_T_CC_DEBOUNCE); 5735 else if (port->vbus_present) 5736 tcpm_set_state(port, 5737 tcpm_try_src(port) ? SRC_TRY 5738 : SNK_ATTACHED, 5739 0); 5740 break; 5741 case SRC_TRY: 5742 port->try_src_count++; 5743 tcpm_set_cc(port, tcpm_rp_cc(port)); 5744 port->max_wait = 0; 5745 tcpm_set_state(port, SRC_TRY_WAIT, 0); 5746 break; 5747 case SRC_TRY_WAIT: 5748 if (port->max_wait == 0) { 5749 port->max_wait = jiffies + 5750 msecs_to_jiffies(PD_T_DRP_TRY); 5751 msecs = PD_T_DRP_TRY; 5752 } else { 5753 if (time_is_after_jiffies(port->max_wait)) 5754 msecs = jiffies_to_msecs(port->max_wait - 5755 jiffies); 5756 else 5757 msecs = 0; 5758 } 5759 tcpm_set_state(port, SNK_TRYWAIT, msecs); 5760 break; 5761 case SRC_TRY_DEBOUNCE: 5762 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE); 5763 break; 5764 case SNK_TRYWAIT: 5765 tcpm_set_cc(port, TYPEC_CC_RD); 5766 tcpm_set_state(port, SNK_TRYWAIT_VBUS, port->timings.cc_debounce_time); 5767 break; 5768 case SNK_TRYWAIT_VBUS: 5769 /* 5770 * TCPM stays in this state indefinitely until VBUS 5771 * is detected as long as Rp is not detected for 5772 * more than a time period of tPDDebounce. 5773 */ 5774 if (port->vbus_present && tcpm_port_is_sink(port)) { 5775 tcpm_set_state(port, SNK_ATTACHED, 0); 5776 break; 5777 } 5778 if (!tcpm_port_is_sink(port)) 5779 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0); 5780 break; 5781 case SNK_TRYWAIT_DEBOUNCE: 5782 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE); 5783 break; 5784 case SNK_ATTACHED: 5785 ret = tcpm_snk_attach(port); 5786 if (ret < 0) 5787 tcpm_set_state(port, SNK_UNATTACHED, 0); 5788 else 5789 /* 5790 * For Type C port controllers that use Battery Charging 5791 * Detection (based on BCv1.2 spec) to detect USB 5792 * charger type, add a delay of "snk_bc12_cmpletion_time" 5793 * before transitioning to SNK_STARTUP to allow BC1.2 5794 * detection to complete before PD is eventually enabled 5795 * in later states. 5796 */ 5797 tcpm_set_state(port, SNK_STARTUP, 5798 port->timings.snk_bc12_cmpletion_time); 5799 break; 5800 case SNK_STARTUP: 5801 opmode = tcpm_get_pwr_opmode(port->polarity ? 5802 port->cc2 : port->cc1); 5803 typec_set_pwr_opmode(port->typec_port, opmode); 5804 port->pwr_opmode = TYPEC_PWR_MODE_USB; 5805 tcpm_set_initial_negotiated_rev(port); 5806 port->message_id = 0; 5807 port->message_id_prime = 0; 5808 port->rx_msgid = -1; 5809 port->rx_msgid_prime = -1; 5810 port->explicit_contract = false; 5811 5812 if (port->ams == POWER_ROLE_SWAP || 5813 port->ams == FAST_ROLE_SWAP) 5814 /* SRC -> SNK POWER/FAST_ROLE_SWAP finished */ 5815 tcpm_ams_finish(port); 5816 5817 tcpm_set_state(port, SNK_DISCOVERY, 0); 5818 break; 5819 case SNK_DISCOVERY: 5820 if (port->vbus_present) { 5821 u32 current_lim = tcpm_get_current_limit(port); 5822 5823 if (port->slow_charger_loop && (current_lim > PD_P_SNK_STDBY_MW / 5)) 5824 current_lim = PD_P_SNK_STDBY_MW / 5; 5825 tcpm_set_current_limit(port, current_lim, 5000); 5826 /* Not sink vbus if operational current is 0mA */ 5827 tcpm_set_charge(port, !port->pd_supported || 5828 pdo_max_current(port->snk_pdo[0])); 5829 5830 if (!port->pd_supported) 5831 tcpm_set_state(port, SNK_READY, 0); 5832 else 5833 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); 5834 break; 5835 } 5836 /* 5837 * For DRP, timeouts differ. Also, handling is supposed to be 5838 * different and much more complex (dead battery detection; 5839 * see USB power delivery specification, section 8.3.3.6.1.5.1). 5840 */ 5841 tcpm_set_state(port, hard_reset_state(port), 5842 port->port_type == TYPEC_PORT_DRP ? 5843 PD_T_DB_DETECT : PD_T_NO_RESPONSE); 5844 break; 5845 case SNK_DISCOVERY_DEBOUNCE: 5846 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE, 5847 port->timings.cc_debounce_time); 5848 break; 5849 case SNK_DISCOVERY_DEBOUNCE_DONE: 5850 if (!tcpm_port_is_disconnected(port) && 5851 tcpm_port_is_sink(port) && 5852 ktime_after(port->delayed_runtime, ktime_get())) { 5853 tcpm_set_state(port, SNK_DISCOVERY, 5854 ktime_to_ms(ktime_sub(port->delayed_runtime, ktime_get()))); 5855 break; 5856 } 5857 tcpm_set_state(port, unattached_state(port), 0); 5858 break; 5859 case SNK_WAIT_CAPABILITIES: 5860 ret = port->tcpc->set_pd_rx(port->tcpc, true); 5861 if (ret < 0) { 5862 tcpm_set_state(port, SNK_READY, 0); 5863 break; 5864 } 5865 /* 5866 * If VBUS has never been low, and we time out waiting 5867 * for source cap, try a soft reset first, in case we 5868 * were already in a stable contract before this boot. 5869 * Do this only once. 5870 */ 5871 if (port->vbus_never_low) { 5872 port->vbus_never_low = false; 5873 upcoming_state = SNK_SOFT_RESET; 5874 } else { 5875 if (!port->self_powered) 5876 upcoming_state = SNK_WAIT_CAPABILITIES_TIMEOUT; 5877 else 5878 upcoming_state = hard_reset_state(port); 5879 } 5880 5881 tcpm_set_state(port, upcoming_state, 5882 port->timings.sink_wait_cap_time); 5883 break; 5884 case SNK_WAIT_CAPABILITIES_TIMEOUT: 5885 /* 5886 * There are some USB PD sources in the field, which do not 5887 * properly implement the specification and fail to start 5888 * sending Source Capability messages after a soft reset. The 5889 * specification suggests to do a hard reset when no Source 5890 * capability message is received within PD_T_SINK_WAIT_CAP, 5891 * but that might effectively kil the machine's power source. 5892 * 5893 * This slightly diverges from the specification and tries to 5894 * recover from this by explicitly asking for the capabilities 5895 * using the Get_Source_Cap control message before falling back 5896 * to a hard reset. The control message should also be supported 5897 * and handled by all USB PD source and dual role devices 5898 * according to the specification. 5899 */ 5900 if (tcpm_pd_send_control(port, PD_CTRL_GET_SOURCE_CAP, TCPC_TX_SOP)) 5901 tcpm_set_state_cond(port, hard_reset_state(port), 0); 5902 else 5903 tcpm_set_state(port, hard_reset_state(port), 5904 port->timings.sink_wait_cap_time); 5905 break; 5906 case SNK_NEGOTIATE_CAPABILITIES: 5907 port->pd_capable = true; 5908 tcpm_set_partner_usb_comm_capable(port, 5909 !!(port->source_caps[0] & PDO_FIXED_USB_COMM)); 5910 port->hard_reset_count = 0; 5911 ret = tcpm_pd_send_request(port); 5912 if (ret < 0) { 5913 /* Restore back to the original state */ 5914 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, 5915 port->pps_data.active, 5916 port->supply_voltage); 5917 /* Let the Source send capabilities again. */ 5918 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); 5919 } else { 5920 tcpm_set_state_cond(port, hard_reset_state(port), 5921 PD_T_SENDER_RESPONSE); 5922 } 5923 break; 5924 case SNK_NEGOTIATE_PPS_CAPABILITIES: 5925 case SNK_NEGOTIATE_SPR_AVS_CAPABILITIES: 5926 ret = tcpm_pd_send_aug_supply_request(port, port->state == 5927 SNK_NEGOTIATE_PPS_CAPABILITIES ? 5928 PD_PPS : PD_SPR_AVS); 5929 if (ret < 0) { 5930 /* Restore back to the original state */ 5931 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, 5932 port->pps_data.active, 5933 port->supply_voltage); 5934 port->aug_supply_req_status = ret; 5935 /* 5936 * If this was called due to updates to sink 5937 * capabilities, and pps is no longer valid, we should 5938 * safely fall back to a standard PDO. 5939 */ 5940 if (port->update_sink_caps) 5941 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); 5942 else 5943 tcpm_set_state(port, SNK_READY, 0); 5944 } else { 5945 tcpm_set_state_cond(port, hard_reset_state(port), 5946 PD_T_SENDER_RESPONSE); 5947 } 5948 break; 5949 case SNK_TRANSITION_SINK: 5950 if (port->spr_avs_data.active) { 5951 if (abs(port->req_supply_voltage - port->supply_voltage) > 5952 SPR_AVS_AVS_SMALL_STEP_V * 1000) { 5953 /* 5954 * The Sink Shall reduce its current draw to 5955 * iSnkStdby within tSnkStdby. The reduction to 5956 * iSnkStdby is not required if the voltage 5957 * increase is less than or equal to 5958 * vAvsSmallStep. 5959 */ 5960 tcpm_log(port, 5961 "SPR AVS Setting iSnkstandby. Req vol: %u mV Curr vol: %u mV", 5962 port->req_supply_voltage, 5963 port->supply_voltage); 5964 tcpm_set_current_limit(port, PD_I_SNK_STBY_MA, 5965 port->supply_voltage); 5966 } 5967 /* 5968 * Although tAvsSrcTransSmall is expected to be used 5969 * for voltage transistions smaller than 1V, using 5970 * tAvsSrcTransLarge to be resilient against chargers 5971 * which strictly cannot honor tAvsSrcTransSmall to 5972 * improve interoperability. 5973 */ 5974 tcpm_set_state(port, hard_reset_state(port), 5975 PD_T_AVS_SRC_TRANS_LARGE); 5976 /* 5977 * From the USB PD spec: 5978 * "The Sink Shall transition to Sink Standby before a 5979 * positive ornegative voltage transition of VBUS. 5980 * During Sink Standby the Sink Shall reduce its power 5981 * draw to pSnkStdby." 5982 * 5983 * This is not applicable to PPS though as the port can 5984 * continue to draw negotiated power without switching 5985 * to standby. 5986 */ 5987 } else if (port->supply_voltage != port->req_supply_voltage && 5988 !port->pps_data.active && 5989 (port->current_limit * port->supply_voltage / 1000 > 5990 PD_P_SNK_STDBY_MW)) { 5991 u32 stdby_ma = PD_P_SNK_STDBY_MW * 1000 / 5992 port->supply_voltage; 5993 5994 tcpm_log(port, "Setting standby current %u mV @ %u mA", 5995 port->supply_voltage, stdby_ma); 5996 tcpm_set_current_limit(port, stdby_ma, 5997 port->supply_voltage); 5998 tcpm_set_state(port, hard_reset_state(port), 5999 PD_T_PS_TRANSITION); 6000 } 6001 break; 6002 case SNK_TRANSITION_SINK_VBUS: 6003 tcpm_set_state(port, hard_reset_state(port), 6004 PD_T_PS_TRANSITION); 6005 break; 6006 case SNK_READY: 6007 port->try_snk_count = 0; 6008 port->update_sink_caps = false; 6009 if (port->explicit_contract) { 6010 typec_set_pwr_opmode(port->typec_port, 6011 TYPEC_PWR_MODE_PD); 6012 port->pwr_opmode = TYPEC_PWR_MODE_PD; 6013 } 6014 6015 if (!port->pd_capable && port->slow_charger_loop) 6016 tcpm_set_current_limit(port, tcpm_get_current_limit(port), 5000); 6017 tcpm_swap_complete(port, 0); 6018 tcpm_typec_connect(port); 6019 if (port->pd_capable && port->source_caps[0] & PDO_FIXED_DUAL_ROLE) 6020 mod_enable_frs_delayed_work(port, 0); 6021 tcpm_aug_supply_req_complete(port, port->aug_supply_req_status); 6022 6023 if (port->ams != NONE_AMS) 6024 tcpm_ams_finish(port); 6025 if (port->next_ams != NONE_AMS) { 6026 port->ams = port->next_ams; 6027 port->next_ams = NONE_AMS; 6028 } 6029 6030 /* 6031 * If previous AMS is interrupted, switch to the upcoming 6032 * state. 6033 */ 6034 if (port->upcoming_state != INVALID_STATE) { 6035 upcoming_state = port->upcoming_state; 6036 port->upcoming_state = INVALID_STATE; 6037 tcpm_set_state(port, upcoming_state, 0); 6038 break; 6039 } 6040 6041 /* 6042 * 6.4.4.3.1 Discover Identity 6043 * "The Discover Identity Command Shall only be sent to SOP when there is an 6044 * Explicit Contract." 6045 * 6046 * Discover Identity on SOP' should be discovered prior to the 6047 * ready state, but if done after a Vconn Swap following Discover 6048 * Identity on SOP then the discovery process can be run here 6049 * as well. 6050 */ 6051 if (port->explicit_contract) { 6052 if (port->vdm_discovery_state == VDM_DISCOVERY_UNKNOWN) 6053 tcpm_set_initial_svdm_version(port); 6054 mod_vdm_discovery_delayed_work(port, 0); 6055 } else { 6056 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 6057 } 6058 6059 power_supply_changed(port->psy); 6060 break; 6061 6062 /* Accessory states */ 6063 case ACC_UNATTACHED: 6064 tcpm_acc_detach(port); 6065 if (port->port_type == TYPEC_PORT_SRC) 6066 tcpm_set_state(port, SRC_UNATTACHED, 0); 6067 else 6068 tcpm_set_state(port, SNK_UNATTACHED, 0); 6069 break; 6070 case DEBUG_ACC_ATTACHED: 6071 case AUDIO_ACC_ATTACHED: 6072 ret = tcpm_acc_attach(port); 6073 if (ret < 0) 6074 tcpm_set_state(port, ACC_UNATTACHED, 0); 6075 break; 6076 case DEBUG_ACC_DEBOUNCE: 6077 case AUDIO_ACC_DEBOUNCE: 6078 tcpm_set_state(port, ACC_UNATTACHED, port->timings.cc_debounce_time); 6079 break; 6080 6081 /* Hard_Reset states */ 6082 case HARD_RESET_SEND: 6083 if (port->ams != NONE_AMS) 6084 tcpm_ams_finish(port); 6085 if (!port->self_powered && port->port_type == TYPEC_PORT_SNK) 6086 dev_err(port->dev, "Initiating hard-reset, which might result in machine power-loss.\n"); 6087 /* 6088 * State machine will be directed to HARD_RESET_START, 6089 * thus set upcoming_state to INVALID_STATE. 6090 */ 6091 port->upcoming_state = INVALID_STATE; 6092 tcpm_ams_start(port, HARD_RESET); 6093 break; 6094 case HARD_RESET_START: 6095 port->sink_cap_done = false; 6096 if (port->tcpc->enable_frs) 6097 port->tcpc->enable_frs(port->tcpc, false); 6098 port->hard_reset_count++; 6099 port->tcpc->set_pd_rx(port->tcpc, false); 6100 tcpm_unregister_altmodes(port); 6101 port->nr_sink_caps = 0; 6102 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_UNKNOWN); 6103 mod_vdm_discovery_cancel_delayed_work(port); 6104 if (port->pwr_role == TYPEC_SOURCE) 6105 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF, 6106 PD_T_PS_HARD_RESET); 6107 else 6108 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0); 6109 break; 6110 case SRC_HARD_RESET_VBUS_OFF: 6111 /* 6112 * 7.1.5 Response to Hard Resets 6113 * Hard Reset Signaling indicates a communication failure has occurred and the 6114 * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall 6115 * drive VBUS to vSafe0V as shown in Figure 7-9. 6116 */ 6117 tcpm_set_vconn(port, false); 6118 tcpm_set_vbus(port, false); 6119 tcpm_set_roles(port, port->self_powered, TYPEC_STATE_USB, TYPEC_SOURCE, 6120 tcpm_data_role_for_source(port)); 6121 /* 6122 * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V + 6123 * PD_T_SRC_RECOVER before turning vbus back on. 6124 * From Table 7-12 Sequence Description for a Source Initiated Hard Reset: 6125 * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then 6126 * tells the Device Policy Manager to instruct the power supply to perform a 6127 * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2). 6128 * 5. After tSrcRecover the Source applies power to VBUS in an attempt to 6129 * re-establish communication with the Sink and resume USB Default Operation. 6130 * The transition to vSafe5V Shall occur within tSrcTurnOn(t4). 6131 */ 6132 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER); 6133 break; 6134 case SRC_HARD_RESET_VBUS_ON: 6135 tcpm_set_vconn(port, true); 6136 tcpm_set_vbus(port, true); 6137 if (port->ams == HARD_RESET) 6138 tcpm_ams_finish(port); 6139 if (port->pd_supported) 6140 port->tcpc->set_pd_rx(port->tcpc, true); 6141 tcpm_set_attached_state(port, true); 6142 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON); 6143 break; 6144 case SNK_HARD_RESET_SINK_OFF: 6145 /* Do not discharge/disconnect during hard reset */ 6146 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); 6147 memset(&port->pps_data, 0, sizeof(port->pps_data)); 6148 tcpm_set_vconn(port, false); 6149 if (port->pd_capable) 6150 tcpm_set_charge(port, false); 6151 tcpm_set_roles(port, port->self_powered, TYPEC_STATE_USB, TYPEC_SINK, 6152 tcpm_data_role_for_sink(port)); 6153 /* 6154 * VBUS may or may not toggle, depending on the adapter. 6155 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON 6156 * directly after timeout. 6157 */ 6158 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V); 6159 break; 6160 case SNK_HARD_RESET_WAIT_VBUS: 6161 if (port->ams == HARD_RESET) 6162 tcpm_ams_finish(port); 6163 /* Assume we're disconnected if VBUS doesn't come back. */ 6164 tcpm_set_state(port, SNK_UNATTACHED, 6165 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON); 6166 break; 6167 case SNK_HARD_RESET_SINK_ON: 6168 /* Note: There is no guarantee that VBUS is on in this state */ 6169 /* 6170 * XXX: 6171 * The specification suggests that dual mode ports in sink 6172 * mode should transition to state PE_SRC_Transition_to_default. 6173 * See USB power delivery specification chapter 8.3.3.6.1.3. 6174 * This would mean to 6175 * - turn off VCONN, reset power supply 6176 * - request hardware reset 6177 * - turn on VCONN 6178 * - Transition to state PE_Src_Startup 6179 * SNK only ports shall transition to state Snk_Startup 6180 * (see chapter 8.3.3.3.8). 6181 * Similar, dual-mode ports in source mode should transition 6182 * to PE_SNK_Transition_to_default. 6183 */ 6184 if (port->pd_capable) { 6185 tcpm_set_current_limit(port, 6186 tcpm_get_current_limit(port), 6187 5000); 6188 /* Not sink vbus if operational current is 0mA */ 6189 tcpm_set_charge(port, !!pdo_max_current(port->snk_pdo[0])); 6190 } 6191 if (port->ams == HARD_RESET) 6192 tcpm_ams_finish(port); 6193 tcpm_set_attached_state(port, true); 6194 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); 6195 tcpm_set_state(port, SNK_STARTUP, 0); 6196 break; 6197 6198 /* Soft_Reset states */ 6199 case SOFT_RESET: 6200 port->message_id = 0; 6201 port->rx_msgid = -1; 6202 /* remove existing capabilities */ 6203 tcpm_partner_source_caps_reset(port); 6204 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP); 6205 port->vdm_sm_running = false; 6206 port->explicit_contract = false; 6207 tcpm_ams_finish(port); 6208 if (port->pwr_role == TYPEC_SOURCE) { 6209 port->upcoming_state = SRC_SEND_CAPABILITIES; 6210 tcpm_ams_start(port, POWER_NEGOTIATION); 6211 } else { 6212 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); 6213 } 6214 break; 6215 case SRC_SOFT_RESET_WAIT_SNK_TX: 6216 case SNK_SOFT_RESET: 6217 if (port->ams != NONE_AMS) 6218 tcpm_ams_finish(port); 6219 port->upcoming_state = SOFT_RESET_SEND; 6220 tcpm_ams_start(port, SOFT_RESET_AMS); 6221 break; 6222 case SOFT_RESET_SEND: 6223 /* 6224 * Power Delivery 3.0 Section 6.3.13 6225 * 6226 * A Soft_Reset Message Shall be targeted at a specific entity 6227 * depending on the type of SOP* packet used. 6228 */ 6229 if (port->tx_sop_type == TCPC_TX_SOP_PRIME) { 6230 port->message_id_prime = 0; 6231 port->rx_msgid_prime = -1; 6232 tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET, TCPC_TX_SOP_PRIME); 6233 tcpm_set_state_cond(port, ready_state(port), PD_T_SENDER_RESPONSE); 6234 } else { 6235 port->message_id = 0; 6236 port->rx_msgid = -1; 6237 /* remove existing capabilities */ 6238 tcpm_partner_source_caps_reset(port); 6239 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET, TCPC_TX_SOP)) 6240 tcpm_set_state_cond(port, hard_reset_state(port), 0); 6241 else 6242 tcpm_set_state_cond(port, hard_reset_state(port), 6243 PD_T_SENDER_RESPONSE); 6244 } 6245 break; 6246 6247 /* DR_Swap states */ 6248 case DR_SWAP_SEND: 6249 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP, TCPC_TX_SOP); 6250 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT, 6251 PD_T_SENDER_RESPONSE); 6252 break; 6253 case DR_SWAP_ACCEPT: 6254 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP); 6255 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0); 6256 break; 6257 case DR_SWAP_SEND_TIMEOUT: 6258 tcpm_swap_complete(port, -ETIMEDOUT); 6259 tcpm_ams_finish(port); 6260 tcpm_set_state(port, ready_state(port), 0); 6261 break; 6262 case DR_SWAP_CHANGE_DR: 6263 tcpm_unregister_altmodes(port); 6264 if (port->data_role == TYPEC_HOST) 6265 tcpm_set_roles(port, true, TYPEC_STATE_USB, port->pwr_role, 6266 TYPEC_DEVICE); 6267 else 6268 tcpm_set_roles(port, true, TYPEC_STATE_USB, port->pwr_role, 6269 TYPEC_HOST); 6270 if (port->data_role == TYPEC_HOST || port->negotiated_rev > PD_REV20) 6271 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_UNKNOWN); 6272 tcpm_ams_finish(port); 6273 tcpm_set_state(port, ready_state(port), 0); 6274 break; 6275 6276 case FR_SWAP_SEND: 6277 if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP, TCPC_TX_SOP)) { 6278 tcpm_set_state(port, ERROR_RECOVERY, 0); 6279 break; 6280 } 6281 tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE); 6282 break; 6283 case FR_SWAP_SEND_TIMEOUT: 6284 tcpm_set_state(port, ERROR_RECOVERY, 0); 6285 break; 6286 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: 6287 tcpm_set_state(port, ERROR_RECOVERY, port->timings.ps_src_off_time); 6288 break; 6289 case FR_SWAP_SNK_SRC_NEW_SINK_READY: 6290 if (port->vbus_source) 6291 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0); 6292 else 6293 tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE); 6294 break; 6295 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: 6296 tcpm_set_pwr_role(port, TYPEC_SOURCE); 6297 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP)) { 6298 tcpm_set_state(port, ERROR_RECOVERY, 0); 6299 break; 6300 } 6301 tcpm_set_cc(port, tcpm_rp_cc(port)); 6302 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START); 6303 break; 6304 6305 /* PR_Swap states */ 6306 case PR_SWAP_ACCEPT: 6307 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP); 6308 tcpm_set_state(port, PR_SWAP_START, 0); 6309 break; 6310 case PR_SWAP_SEND: 6311 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP, TCPC_TX_SOP); 6312 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT, 6313 PD_T_SENDER_RESPONSE); 6314 break; 6315 case PR_SWAP_SEND_TIMEOUT: 6316 tcpm_swap_complete(port, -ETIMEDOUT); 6317 tcpm_set_state(port, ready_state(port), 0); 6318 break; 6319 case PR_SWAP_START: 6320 tcpm_apply_rc(port); 6321 if (port->pwr_role == TYPEC_SOURCE) 6322 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF, 6323 PD_T_SRC_TRANSITION); 6324 else 6325 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0); 6326 break; 6327 case PR_SWAP_SRC_SNK_TRANSITION_OFF: 6328 /* 6329 * Prevent vbus discharge circuit from turning on during PR_SWAP 6330 * as this is not a disconnect. 6331 */ 6332 tcpm_set_vbus(port, false); 6333 port->explicit_contract = false; 6334 /* allow time for Vbus discharge, must be < tSrcSwapStdby */ 6335 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 6336 PD_T_SRCSWAPSTDBY); 6337 break; 6338 case PR_SWAP_SRC_SNK_SOURCE_OFF: 6339 tcpm_set_cc(port, TYPEC_CC_RD); 6340 /* allow CC debounce */ 6341 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED, 6342 port->timings.cc_debounce_time); 6343 break; 6344 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: 6345 /* 6346 * USB-PD standard, 6.2.1.4, Port Power Role: 6347 * "During the Power Role Swap Sequence, for the initial Source 6348 * Port, the Port Power Role field shall be set to Sink in the 6349 * PS_RDY Message indicating that the initial Source’s power 6350 * supply is turned off" 6351 */ 6352 tcpm_set_pwr_role(port, TYPEC_SINK); 6353 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP)) { 6354 tcpm_set_state(port, ERROR_RECOVERY, 0); 6355 break; 6356 } 6357 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_ON_PRS); 6358 break; 6359 case PR_SWAP_SRC_SNK_SINK_ON: 6360 tcpm_enable_auto_vbus_discharge(port, true); 6361 /* Set the vbus disconnect threshold for implicit contract */ 6362 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); 6363 tcpm_set_state(port, SNK_STARTUP, 0); 6364 break; 6365 case PR_SWAP_SNK_SRC_SINK_OFF: 6366 /* will be source, remove existing capabilities */ 6367 tcpm_partner_source_caps_reset(port); 6368 /* 6369 * Prevent vbus discharge circuit from turning on during PR_SWAP 6370 * as this is not a disconnect. 6371 */ 6372 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, 6373 port->pps_data.active, 0); 6374 tcpm_set_charge(port, false); 6375 tcpm_set_state(port, ERROR_RECOVERY, port->timings.ps_src_off_time); 6376 break; 6377 case PR_SWAP_SNK_SRC_SOURCE_ON: 6378 tcpm_enable_auto_vbus_discharge(port, true); 6379 tcpm_set_cc(port, tcpm_rp_cc(port)); 6380 tcpm_set_vbus(port, true); 6381 /* 6382 * allow time VBUS ramp-up, must be < tNewSrc 6383 * Also, this window overlaps with CC debounce as well. 6384 * So, Wait for the max of two which is PD_T_NEWSRC 6385 */ 6386 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP, 6387 PD_T_NEWSRC); 6388 break; 6389 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP: 6390 /* 6391 * USB PD standard, 6.2.1.4: 6392 * "Subsequent Messages initiated by the Policy Engine, 6393 * such as the PS_RDY Message sent to indicate that Vbus 6394 * is ready, will have the Port Power Role field set to 6395 * Source." 6396 */ 6397 tcpm_set_pwr_role(port, TYPEC_SOURCE); 6398 tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP); 6399 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START); 6400 break; 6401 6402 case VCONN_SWAP_ACCEPT: 6403 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP); 6404 tcpm_ams_finish(port); 6405 tcpm_set_state(port, VCONN_SWAP_START, 0); 6406 break; 6407 case VCONN_SWAP_SEND: 6408 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP, TCPC_TX_SOP); 6409 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT, 6410 PD_T_SENDER_RESPONSE); 6411 break; 6412 case VCONN_SWAP_SEND_TIMEOUT: 6413 tcpm_swap_complete(port, -ETIMEDOUT); 6414 tcpm_set_state(port, ready_state(port), 0); 6415 break; 6416 case VCONN_SWAP_START: 6417 if (port->vconn_role == TYPEC_SOURCE) 6418 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0); 6419 else 6420 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0); 6421 break; 6422 case VCONN_SWAP_WAIT_FOR_VCONN: 6423 tcpm_set_state(port, hard_reset_state(port), 6424 PD_T_VCONN_SOURCE_ON); 6425 break; 6426 case VCONN_SWAP_TURN_ON_VCONN: 6427 ret = tcpm_set_vconn(port, true); 6428 tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP); 6429 /* 6430 * USB PD 3.0 Section 6.4.4.3.1 6431 * 6432 * Note that a Cable Plug or VPD will not be ready for PD 6433 * Communication until tVCONNStable after VCONN has been applied 6434 */ 6435 if (!ret) 6436 tcpm_set_state(port, VCONN_SWAP_SEND_SOFT_RESET, 6437 PD_T_VCONN_STABLE); 6438 else 6439 tcpm_set_state(port, ready_state(port), 0); 6440 break; 6441 case VCONN_SWAP_TURN_OFF_VCONN: 6442 tcpm_set_vconn(port, false); 6443 tcpm_set_state(port, ready_state(port), 0); 6444 break; 6445 case VCONN_SWAP_SEND_SOFT_RESET: 6446 tcpm_swap_complete(port, port->swap_status); 6447 if (tcpm_can_communicate_sop_prime(port)) { 6448 port->tx_sop_type = TCPC_TX_SOP_PRIME; 6449 port->upcoming_state = SOFT_RESET_SEND; 6450 tcpm_ams_start(port, SOFT_RESET_AMS); 6451 } else { 6452 tcpm_set_state(port, ready_state(port), 0); 6453 } 6454 break; 6455 6456 case DR_SWAP_CANCEL: 6457 case PR_SWAP_CANCEL: 6458 case VCONN_SWAP_CANCEL: 6459 tcpm_swap_complete(port, port->swap_status); 6460 if (port->pwr_role == TYPEC_SOURCE) 6461 tcpm_set_state(port, SRC_READY, 0); 6462 else 6463 tcpm_set_state(port, SNK_READY, 0); 6464 break; 6465 case FR_SWAP_CANCEL: 6466 if (port->pwr_role == TYPEC_SOURCE) 6467 tcpm_set_state(port, SRC_READY, 0); 6468 else 6469 tcpm_set_state(port, SNK_READY, 0); 6470 break; 6471 6472 case BIST_RX: 6473 switch (BDO_MODE_MASK(port->bist_request)) { 6474 case BDO_MODE_CARRIER2: 6475 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL); 6476 tcpm_set_state(port, unattached_state(port), 6477 PD_T_BIST_CONT_MODE); 6478 break; 6479 case BDO_MODE_TESTDATA: 6480 if (port->tcpc->set_bist_data) { 6481 tcpm_log(port, "Enable BIST MODE TESTDATA"); 6482 port->tcpc->set_bist_data(port->tcpc, true); 6483 } 6484 break; 6485 default: 6486 break; 6487 } 6488 break; 6489 case GET_STATUS_SEND: 6490 tcpm_pd_send_control(port, PD_CTRL_GET_STATUS, TCPC_TX_SOP); 6491 tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT, 6492 PD_T_SENDER_RESPONSE); 6493 break; 6494 case GET_STATUS_SEND_TIMEOUT: 6495 tcpm_set_state(port, ready_state(port), 0); 6496 break; 6497 case GET_PPS_STATUS_SEND: 6498 tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS, TCPC_TX_SOP); 6499 tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT, 6500 PD_T_SENDER_RESPONSE); 6501 break; 6502 case GET_PPS_STATUS_SEND_TIMEOUT: 6503 tcpm_set_state(port, ready_state(port), 0); 6504 break; 6505 case GET_SINK_CAP: 6506 tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP, TCPC_TX_SOP); 6507 tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE); 6508 break; 6509 case GET_SINK_CAP_TIMEOUT: 6510 port->sink_cap_done = true; 6511 tcpm_set_state(port, ready_state(port), 0); 6512 break; 6513 case ERROR_RECOVERY: 6514 tcpm_swap_complete(port, -EPROTO); 6515 tcpm_aug_supply_req_complete(port, -EPROTO); 6516 tcpm_set_state(port, PORT_RESET, 0); 6517 break; 6518 case PORT_RESET: 6519 tcpm_reset_port(port); 6520 if (port->self_powered) 6521 tcpm_set_cc(port, TYPEC_CC_OPEN); 6522 else 6523 tcpm_set_cc(port, tcpm_default_state(port) == SNK_UNATTACHED ? 6524 TYPEC_CC_RD : tcpm_rp_cc(port)); 6525 tcpm_set_state(port, PORT_RESET_WAIT_OFF, 6526 PD_T_ERROR_RECOVERY); 6527 break; 6528 case PORT_RESET_WAIT_OFF: 6529 tcpm_set_state(port, 6530 tcpm_default_state(port), 6531 port->vbus_present ? port->timings.ps_src_off_time : 0); 6532 break; 6533 6534 /* AMS intermediate state */ 6535 case AMS_START: 6536 if (port->upcoming_state == INVALID_STATE) { 6537 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? 6538 SRC_READY : SNK_READY, 0); 6539 break; 6540 } 6541 6542 upcoming_state = port->upcoming_state; 6543 port->upcoming_state = INVALID_STATE; 6544 tcpm_set_state(port, upcoming_state, 0); 6545 break; 6546 6547 /* Chunk state */ 6548 case CHUNK_NOT_SUPP: 6549 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP, TCPC_TX_SOP); 6550 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? SRC_READY : SNK_READY, 0); 6551 break; 6552 6553 /* Cable states */ 6554 case SRC_VDM_IDENTITY_REQUEST: 6555 mod_vdm_discovery_delayed_work(port, 0); 6556 port->upcoming_state = SRC_SEND_CAPABILITIES; 6557 break; 6558 6559 default: 6560 WARN(1, "Unexpected port state %d\n", port->state); 6561 break; 6562 } 6563 } 6564 6565 static void tcpm_state_machine_work(struct kthread_work *work) 6566 { 6567 struct tcpm_port *port = container_of(work, struct tcpm_port, state_machine); 6568 enum tcpm_state prev_state; 6569 6570 mutex_lock(&port->lock); 6571 port->state_machine_running = true; 6572 6573 if (port->queued_message && tcpm_send_queued_message(port)) 6574 goto done; 6575 6576 /* If we were queued due to a delayed state change, update it now */ 6577 if (port->delayed_state) { 6578 tcpm_log(port, "state change %s -> %s [delayed %ld ms]", 6579 tcpm_states[port->state], 6580 tcpm_states[port->delayed_state], port->delay_ms); 6581 port->prev_state = port->state; 6582 port->state = port->delayed_state; 6583 port->delayed_state = INVALID_STATE; 6584 } 6585 6586 /* 6587 * Continue running as long as we have (non-delayed) state changes 6588 * to make. 6589 */ 6590 do { 6591 prev_state = port->state; 6592 run_state_machine(port); 6593 if (port->queued_message) 6594 tcpm_send_queued_message(port); 6595 } while (port->state != prev_state && !port->delayed_state); 6596 6597 done: 6598 port->state_machine_running = false; 6599 mutex_unlock(&port->lock); 6600 } 6601 6602 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1, 6603 enum typec_cc_status cc2) 6604 { 6605 enum typec_cc_status old_cc1, old_cc2; 6606 enum tcpm_state new_state; 6607 6608 old_cc1 = port->cc1; 6609 old_cc2 = port->cc2; 6610 port->cc1 = cc1; 6611 port->cc2 = cc2; 6612 6613 tcpm_log_force(port, 6614 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]", 6615 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state], 6616 port->polarity, 6617 tcpm_port_is_disconnected(port) ? "disconnected" 6618 : "connected"); 6619 6620 switch (port->state) { 6621 case TOGGLING: 6622 if (tcpm_port_is_debug_source(port) || tcpm_port_is_audio(port) || 6623 tcpm_port_is_source(port)) 6624 tcpm_set_state(port, SRC_ATTACH_WAIT, 0); 6625 else if (tcpm_port_is_debug_sink(port) || tcpm_port_is_sink(port)) 6626 tcpm_set_state(port, SNK_ATTACH_WAIT, 0); 6627 break; 6628 case CHECK_CONTAMINANT: 6629 /* Wait for Toggling to be resumed */ 6630 break; 6631 case SRC_UNATTACHED: 6632 case ACC_UNATTACHED: 6633 if (tcpm_port_is_debug_source(port) || tcpm_port_is_audio(port) || 6634 tcpm_port_is_source(port)) 6635 tcpm_set_state(port, SRC_ATTACH_WAIT, 0); 6636 else if (tcpm_port_is_debug_sink(port)) 6637 tcpm_set_state(port, SNK_ATTACH_WAIT, 0); 6638 break; 6639 case SRC_ATTACH_WAIT: 6640 if (tcpm_port_is_disconnected(port) || 6641 tcpm_port_is_audio_detached(port)) 6642 tcpm_set_state(port, SRC_UNATTACHED, 0); 6643 else if (cc1 != old_cc1 || cc2 != old_cc2) 6644 tcpm_set_state(port, SRC_ATTACH_WAIT, 0); 6645 break; 6646 case SRC_ATTACHED: 6647 case SRC_STARTUP: 6648 case SRC_SEND_CAPABILITIES: 6649 case SRC_READY: 6650 if (tcpm_port_is_disconnected(port) || 6651 !tcpm_port_is_source(port)) { 6652 if (port->port_type == TYPEC_PORT_SRC) 6653 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port)); 6654 else 6655 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); 6656 } 6657 break; 6658 case SNK_UNATTACHED: 6659 if (tcpm_port_is_debug_sink(port) || tcpm_port_is_audio(port) || 6660 tcpm_port_is_sink(port)) 6661 tcpm_set_state(port, SNK_ATTACH_WAIT, 0); 6662 break; 6663 case SNK_ATTACH_WAIT: 6664 if ((port->cc1 == TYPEC_CC_OPEN && 6665 port->cc2 != TYPEC_CC_OPEN) || 6666 (port->cc1 != TYPEC_CC_OPEN && 6667 port->cc2 == TYPEC_CC_OPEN)) 6668 new_state = SNK_DEBOUNCED; 6669 else if (tcpm_port_is_disconnected(port)) 6670 new_state = SNK_UNATTACHED; 6671 else 6672 break; 6673 if (new_state != port->delayed_state) 6674 tcpm_set_state(port, SNK_ATTACH_WAIT, 0); 6675 break; 6676 case SNK_DEBOUNCED: 6677 if (tcpm_port_is_disconnected(port)) 6678 new_state = SNK_UNATTACHED; 6679 else if (port->vbus_present) 6680 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED; 6681 else 6682 new_state = SNK_UNATTACHED; 6683 if (new_state != port->delayed_state) 6684 tcpm_set_state(port, SNK_DEBOUNCED, 0); 6685 break; 6686 case SNK_READY: 6687 /* 6688 * EXIT condition is based primarily on vbus disconnect and CC is secondary. 6689 * "A port that has entered into USB PD communications with the Source and 6690 * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect 6691 * cable disconnect in addition to monitoring VBUS. 6692 * 6693 * A port that is monitoring the CC voltage for disconnect (but is not in 6694 * the process of a USB PD PR_Swap or USB PD FR_Swap) shall transition to 6695 * Unattached.SNK within tSinkDisconnect after the CC voltage remains below 6696 * vRd-USB for tPDDebounce." 6697 * 6698 * When set_auto_vbus_discharge_threshold is enabled, CC pins go 6699 * away before vbus decays to disconnect threshold. Allow 6700 * disconnect to be driven by vbus disconnect when auto vbus 6701 * discharge is enabled. 6702 */ 6703 if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port)) 6704 tcpm_set_state(port, unattached_state(port), 0); 6705 else if (!port->pd_capable && 6706 (cc1 != old_cc1 || cc2 != old_cc2)) 6707 tcpm_set_current_limit(port, 6708 tcpm_get_current_limit(port), 6709 5000); 6710 break; 6711 6712 case AUDIO_ACC_ATTACHED: 6713 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN) 6714 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0); 6715 break; 6716 case AUDIO_ACC_DEBOUNCE: 6717 if (tcpm_port_is_audio(port)) 6718 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0); 6719 break; 6720 6721 case DEBUG_ACC_ATTACHED: 6722 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN) 6723 tcpm_set_state(port, DEBUG_ACC_DEBOUNCE, 0); 6724 break; 6725 6726 case DEBUG_ACC_DEBOUNCE: 6727 if (tcpm_port_is_debug(port)) 6728 tcpm_set_state(port, DEBUG_ACC_ATTACHED, 0); 6729 break; 6730 6731 case SNK_TRY: 6732 /* Do nothing, waiting for timeout */ 6733 break; 6734 6735 case SNK_DISCOVERY: 6736 /* CC line is unstable, wait for debounce */ 6737 if (tcpm_port_is_disconnected(port)) 6738 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0); 6739 break; 6740 case SNK_DISCOVERY_DEBOUNCE: 6741 break; 6742 6743 case SRC_TRYWAIT: 6744 /* Hand over to state machine if needed */ 6745 if (!port->vbus_present && tcpm_port_is_source(port)) 6746 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0); 6747 break; 6748 case SRC_TRYWAIT_DEBOUNCE: 6749 if (port->vbus_present || !tcpm_port_is_source(port)) 6750 tcpm_set_state(port, SRC_TRYWAIT, 0); 6751 break; 6752 case SNK_TRY_WAIT_DEBOUNCE: 6753 if (!tcpm_port_is_sink(port)) { 6754 port->max_wait = 0; 6755 tcpm_set_state(port, SRC_TRYWAIT, PD_T_PD_DEBOUNCE); 6756 } 6757 break; 6758 case SRC_TRY_WAIT: 6759 if (tcpm_port_is_source(port)) 6760 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0); 6761 break; 6762 case SRC_TRY_DEBOUNCE: 6763 tcpm_set_state(port, SRC_TRY_WAIT, 0); 6764 break; 6765 case SNK_TRYWAIT_DEBOUNCE: 6766 if (tcpm_port_is_sink(port)) 6767 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0); 6768 break; 6769 case SNK_TRYWAIT_VBUS: 6770 if (!tcpm_port_is_sink(port)) 6771 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0); 6772 break; 6773 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: 6774 if (!tcpm_port_is_sink(port)) 6775 tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE); 6776 else 6777 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0); 6778 break; 6779 case SNK_TRYWAIT: 6780 /* Do nothing, waiting for tCCDebounce */ 6781 break; 6782 case PR_SWAP_SNK_SRC_SINK_OFF: 6783 case PR_SWAP_SRC_SNK_TRANSITION_OFF: 6784 case PR_SWAP_SRC_SNK_SOURCE_OFF: 6785 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: 6786 case PR_SWAP_SNK_SRC_SOURCE_ON: 6787 /* 6788 * CC state change is expected in PR_SWAP 6789 * Ignore it. 6790 */ 6791 break; 6792 case FR_SWAP_SEND: 6793 case FR_SWAP_SEND_TIMEOUT: 6794 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: 6795 case FR_SWAP_SNK_SRC_NEW_SINK_READY: 6796 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: 6797 /* Do nothing, CC change expected */ 6798 break; 6799 6800 case PORT_RESET: 6801 case PORT_RESET_WAIT_OFF: 6802 /* 6803 * State set back to default mode once the timer completes. 6804 * Ignore CC changes here. 6805 */ 6806 break; 6807 default: 6808 /* 6809 * While acting as sink and auto vbus discharge is enabled, Allow disconnect 6810 * to be driven by vbus disconnect. 6811 */ 6812 if (tcpm_port_is_disconnected(port) && !(port->pwr_role == TYPEC_SINK && 6813 port->auto_vbus_discharge_enabled)) 6814 tcpm_set_state(port, unattached_state(port), 0); 6815 break; 6816 } 6817 } 6818 6819 static void _tcpm_pd_vbus_on(struct tcpm_port *port) 6820 { 6821 tcpm_log_force(port, "VBUS on"); 6822 port->vbus_present = true; 6823 /* 6824 * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly 6825 * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here. 6826 */ 6827 port->vbus_vsafe0v = false; 6828 6829 switch (port->state) { 6830 case SNK_TRANSITION_SINK_VBUS: 6831 port->explicit_contract = true; 6832 tcpm_set_state(port, SNK_READY, 0); 6833 break; 6834 case SNK_DISCOVERY: 6835 tcpm_set_state(port, SNK_DISCOVERY, 0); 6836 break; 6837 6838 case SNK_DEBOUNCED: 6839 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY 6840 : SNK_ATTACHED, 6841 0); 6842 break; 6843 case SNK_HARD_RESET_WAIT_VBUS: 6844 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0); 6845 break; 6846 case SRC_ATTACHED: 6847 tcpm_set_state(port, SRC_STARTUP, 0); 6848 break; 6849 case SRC_HARD_RESET_VBUS_ON: 6850 tcpm_set_state(port, SRC_STARTUP, 0); 6851 break; 6852 6853 case SNK_TRY: 6854 /* Do nothing, waiting for timeout */ 6855 break; 6856 case SRC_TRYWAIT: 6857 /* Do nothing, Waiting for Rd to be detected */ 6858 break; 6859 case SRC_TRYWAIT_DEBOUNCE: 6860 tcpm_set_state(port, SRC_TRYWAIT, 0); 6861 break; 6862 case SNK_TRY_WAIT_DEBOUNCE: 6863 /* Do nothing, waiting for PD_DEBOUNCE to do be done */ 6864 break; 6865 case SNK_TRYWAIT: 6866 /* Do nothing, waiting for tCCDebounce */ 6867 break; 6868 case SNK_TRYWAIT_VBUS: 6869 if (tcpm_port_is_sink(port)) 6870 tcpm_set_state(port, SNK_ATTACHED, 0); 6871 break; 6872 case SNK_TRYWAIT_DEBOUNCE: 6873 /* Do nothing, waiting for Rp */ 6874 break; 6875 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: 6876 if (port->vbus_present && tcpm_port_is_sink(port)) 6877 tcpm_set_state(port, SNK_ATTACHED, 0); 6878 break; 6879 case SRC_TRY_WAIT: 6880 case SRC_TRY_DEBOUNCE: 6881 /* Do nothing, waiting for sink detection */ 6882 break; 6883 case FR_SWAP_SEND: 6884 case FR_SWAP_SEND_TIMEOUT: 6885 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: 6886 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: 6887 if (port->tcpc->frs_sourcing_vbus) 6888 port->tcpc->frs_sourcing_vbus(port->tcpc); 6889 break; 6890 case FR_SWAP_SNK_SRC_NEW_SINK_READY: 6891 if (port->tcpc->frs_sourcing_vbus) 6892 port->tcpc->frs_sourcing_vbus(port->tcpc); 6893 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0); 6894 break; 6895 6896 case PORT_RESET: 6897 case PORT_RESET_WAIT_OFF: 6898 /* 6899 * State set back to default mode once the timer completes. 6900 * Ignore vbus changes here. 6901 */ 6902 break; 6903 6904 default: 6905 break; 6906 } 6907 } 6908 6909 static void _tcpm_pd_vbus_off(struct tcpm_port *port) 6910 { 6911 tcpm_log_force(port, "VBUS off"); 6912 port->vbus_present = false; 6913 port->vbus_never_low = false; 6914 switch (port->state) { 6915 case SNK_HARD_RESET_SINK_OFF: 6916 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0); 6917 break; 6918 case HARD_RESET_SEND: 6919 break; 6920 case SNK_TRY: 6921 /* Do nothing, waiting for timeout */ 6922 break; 6923 case SRC_TRYWAIT: 6924 /* Hand over to state machine if needed */ 6925 if (tcpm_port_is_source(port)) 6926 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0); 6927 break; 6928 case SNK_TRY_WAIT_DEBOUNCE: 6929 /* Do nothing, waiting for PD_DEBOUNCE to do be done */ 6930 break; 6931 case SNK_TRYWAIT: 6932 case SNK_TRYWAIT_VBUS: 6933 case SNK_TRYWAIT_DEBOUNCE: 6934 break; 6935 case SNK_ATTACH_WAIT: 6936 case SNK_DEBOUNCED: 6937 /* Do nothing, as TCPM is still waiting for vbus to reach VSAFE5V to connect */ 6938 break; 6939 6940 case SNK_NEGOTIATE_CAPABILITIES: 6941 break; 6942 6943 case PR_SWAP_SRC_SNK_TRANSITION_OFF: 6944 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0); 6945 break; 6946 6947 case PR_SWAP_SNK_SRC_SINK_OFF: 6948 /* Do nothing, expected */ 6949 break; 6950 6951 case PR_SWAP_SNK_SRC_SOURCE_ON: 6952 /* 6953 * Do nothing when vbus off notification is received. 6954 * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON 6955 * for the vbus source to ramp up. 6956 */ 6957 break; 6958 6959 case PORT_RESET_WAIT_OFF: 6960 tcpm_set_state(port, tcpm_default_state(port), 0); 6961 break; 6962 6963 case SRC_TRY_WAIT: 6964 case SRC_TRY_DEBOUNCE: 6965 /* Do nothing, waiting for sink detection */ 6966 break; 6967 6968 case SRC_STARTUP: 6969 case SRC_SEND_CAPABILITIES: 6970 case SRC_SEND_CAPABILITIES_TIMEOUT: 6971 case SRC_NEGOTIATE_CAPABILITIES: 6972 case SRC_TRANSITION_SUPPLY: 6973 case SRC_READY: 6974 case SRC_WAIT_NEW_CAPABILITIES: 6975 /* 6976 * Force to unattached state to re-initiate connection. 6977 * DRP port should move to Unattached.SNK instead of Unattached.SRC if 6978 * sink removed. Although sink removal here is due to source's vbus collapse, 6979 * treat it the same way for consistency. 6980 */ 6981 if (port->port_type == TYPEC_PORT_SRC) 6982 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port)); 6983 else 6984 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); 6985 break; 6986 6987 case PORT_RESET: 6988 /* 6989 * State set back to default mode once the timer completes. 6990 * Ignore vbus changes here. 6991 */ 6992 break; 6993 6994 case FR_SWAP_SEND: 6995 case FR_SWAP_SEND_TIMEOUT: 6996 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: 6997 case FR_SWAP_SNK_SRC_NEW_SINK_READY: 6998 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: 6999 /* Do nothing, vbus drop expected */ 7000 break; 7001 7002 case SNK_HARD_RESET_WAIT_VBUS: 7003 /* Do nothing, its OK to receive vbus off events */ 7004 break; 7005 7006 default: 7007 if (port->pwr_role == TYPEC_SINK && port->attached) 7008 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); 7009 break; 7010 } 7011 } 7012 7013 static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port) 7014 { 7015 tcpm_log_force(port, "VBUS VSAFE0V"); 7016 port->vbus_vsafe0v = true; 7017 switch (port->state) { 7018 case SRC_HARD_RESET_VBUS_OFF: 7019 /* 7020 * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait 7021 * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V. 7022 */ 7023 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER); 7024 break; 7025 case SRC_ATTACH_WAIT: 7026 if (tcpm_port_is_source(port)) 7027 tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED, 7028 port->timings.cc_debounce_time); 7029 break; 7030 case SRC_STARTUP: 7031 case SRC_SEND_CAPABILITIES: 7032 case SRC_SEND_CAPABILITIES_TIMEOUT: 7033 case SRC_NEGOTIATE_CAPABILITIES: 7034 case SRC_TRANSITION_SUPPLY: 7035 case SRC_READY: 7036 case SRC_WAIT_NEW_CAPABILITIES: 7037 if (port->auto_vbus_discharge_enabled) { 7038 if (port->port_type == TYPEC_PORT_SRC) 7039 tcpm_set_state(port, SRC_UNATTACHED, 0); 7040 else 7041 tcpm_set_state(port, SNK_UNATTACHED, 0); 7042 } 7043 break; 7044 case PR_SWAP_SNK_SRC_SINK_OFF: 7045 case PR_SWAP_SNK_SRC_SOURCE_ON: 7046 /* Do nothing, vsafe0v is expected during transition */ 7047 break; 7048 case SNK_ATTACH_WAIT: 7049 case SNK_DEBOUNCED: 7050 /*Do nothing, still waiting for VSAFE5V for connect */ 7051 break; 7052 case SNK_HARD_RESET_WAIT_VBUS: 7053 /* Do nothing, its OK to receive vbus off events */ 7054 break; 7055 default: 7056 if (port->pwr_role == TYPEC_SINK && port->auto_vbus_discharge_enabled) 7057 tcpm_set_state(port, SNK_UNATTACHED, 0); 7058 break; 7059 } 7060 } 7061 7062 static void _tcpm_pd_hard_reset(struct tcpm_port *port) 7063 { 7064 tcpm_log_force(port, "Received hard reset"); 7065 if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data) 7066 port->tcpc->set_bist_data(port->tcpc, false); 7067 7068 switch (port->state) { 7069 case TOGGLING: 7070 case ERROR_RECOVERY: 7071 case PORT_RESET: 7072 case PORT_RESET_WAIT_OFF: 7073 return; 7074 default: 7075 break; 7076 } 7077 7078 if (port->ams != NONE_AMS) 7079 port->ams = NONE_AMS; 7080 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) 7081 port->ams = HARD_RESET; 7082 /* 7083 * If we keep receiving hard reset requests, executing the hard reset 7084 * must have failed. Revert to error recovery if that happens. 7085 */ 7086 tcpm_set_state(port, 7087 port->hard_reset_count < PD_N_HARD_RESET_COUNT ? 7088 HARD_RESET_START : ERROR_RECOVERY, 7089 0); 7090 } 7091 7092 static void tcpm_pd_event_handler(struct kthread_work *work) 7093 { 7094 struct tcpm_port *port = container_of(work, struct tcpm_port, 7095 event_work); 7096 u32 events; 7097 7098 mutex_lock(&port->lock); 7099 7100 spin_lock(&port->pd_event_lock); 7101 while (port->pd_events) { 7102 events = port->pd_events; 7103 port->pd_events = 0; 7104 spin_unlock(&port->pd_event_lock); 7105 if (events & TCPM_RESET_EVENT) 7106 _tcpm_pd_hard_reset(port); 7107 if (events & TCPM_VBUS_EVENT) { 7108 bool vbus; 7109 7110 vbus = port->tcpc->get_vbus(port->tcpc); 7111 if (vbus) { 7112 _tcpm_pd_vbus_on(port); 7113 } else { 7114 _tcpm_pd_vbus_off(port); 7115 /* 7116 * When TCPC does not support detecting vsafe0v voltage level, 7117 * treat vbus absent as vsafe0v. Else invoke is_vbus_vsafe0v 7118 * to see if vbus has discharge to VSAFE0V. 7119 */ 7120 if (!port->tcpc->is_vbus_vsafe0v || 7121 port->tcpc->is_vbus_vsafe0v(port->tcpc)) 7122 _tcpm_pd_vbus_vsafe0v(port); 7123 } 7124 } 7125 if (events & TCPM_CC_EVENT) { 7126 enum typec_cc_status cc1, cc2; 7127 7128 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0) 7129 _tcpm_cc_change(port, cc1, cc2); 7130 } 7131 if (events & TCPM_FRS_EVENT) { 7132 if (port->state == SNK_READY) { 7133 int ret; 7134 7135 port->upcoming_state = FR_SWAP_SEND; 7136 ret = tcpm_ams_start(port, FAST_ROLE_SWAP); 7137 if (ret == -EAGAIN) 7138 tcpm_set_state(port, ERROR_RECOVERY, 0); 7139 } else { 7140 tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready"); 7141 } 7142 } 7143 if (events & TCPM_SOURCING_VBUS) { 7144 /* 7145 * In fast role swap case TCPC autonomously sources vbus. Set vbus_source 7146 * true conditionally as TCPM wouldn't have called tcpm_set_vbus. 7147 * If TCPM calls tcpm_set_vbus to source vbus, vbus_source would already 7148 * be true. 7149 * 7150 * When TCPM_FRS_EVENT and TCPM_SOURCING_VBUS arrive simultaneously, 7151 * handling TCPM_FRS_EVENT above transitions the state to AMS_START 7152 * with upcoming_state FR_SWAP_SEND. 7153 */ 7154 7155 if (tcpm_port_is_source(port) || 7156 tcpm_port_is_debug_source(port) || 7157 (port->state == AMS_START && port->upcoming_state == FR_SWAP_SEND) || 7158 port->state == FR_SWAP_SEND || 7159 port->state == FR_SWAP_SEND_TIMEOUT || 7160 port->state == FR_SWAP_SNK_SRC_TRANSITION_TO_OFF || 7161 port->state == FR_SWAP_SNK_SRC_NEW_SINK_READY || 7162 port->state == FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED) { 7163 tcpm_log(port, "sourcing vbus"); 7164 port->vbus_source = true; 7165 _tcpm_pd_vbus_on(port); 7166 } else { 7167 tcpm_log(port, "Discarding sourcing vbus! Invalid state %s", 7168 tcpm_states[port->state]); 7169 } 7170 } 7171 if (events & TCPM_PORT_CLEAN) { 7172 tcpm_log(port, "port clean"); 7173 if (port->state == CHECK_CONTAMINANT) { 7174 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) 7175 tcpm_set_state(port, TOGGLING, 0); 7176 else 7177 tcpm_set_state(port, tcpm_default_state(port), 0); 7178 } 7179 } 7180 if (events & TCPM_PORT_ERROR) { 7181 tcpm_log(port, "port triggering error recovery"); 7182 tcpm_set_state(port, ERROR_RECOVERY, 0); 7183 } 7184 7185 spin_lock(&port->pd_event_lock); 7186 } 7187 spin_unlock(&port->pd_event_lock); 7188 mutex_unlock(&port->lock); 7189 } 7190 7191 void tcpm_cc_change(struct tcpm_port *port) 7192 { 7193 spin_lock(&port->pd_event_lock); 7194 port->pd_events |= TCPM_CC_EVENT; 7195 spin_unlock(&port->pd_event_lock); 7196 kthread_queue_work(port->wq, &port->event_work); 7197 } 7198 EXPORT_SYMBOL_GPL(tcpm_cc_change); 7199 7200 void tcpm_vbus_change(struct tcpm_port *port) 7201 { 7202 spin_lock(&port->pd_event_lock); 7203 port->pd_events |= TCPM_VBUS_EVENT; 7204 spin_unlock(&port->pd_event_lock); 7205 kthread_queue_work(port->wq, &port->event_work); 7206 } 7207 EXPORT_SYMBOL_GPL(tcpm_vbus_change); 7208 7209 void tcpm_pd_hard_reset(struct tcpm_port *port) 7210 { 7211 spin_lock(&port->pd_event_lock); 7212 port->pd_events = TCPM_RESET_EVENT; 7213 spin_unlock(&port->pd_event_lock); 7214 kthread_queue_work(port->wq, &port->event_work); 7215 } 7216 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset); 7217 7218 void tcpm_sink_frs(struct tcpm_port *port) 7219 { 7220 spin_lock(&port->pd_event_lock); 7221 port->pd_events |= TCPM_FRS_EVENT; 7222 spin_unlock(&port->pd_event_lock); 7223 kthread_queue_work(port->wq, &port->event_work); 7224 } 7225 EXPORT_SYMBOL_GPL(tcpm_sink_frs); 7226 7227 void tcpm_sourcing_vbus(struct tcpm_port *port) 7228 { 7229 spin_lock(&port->pd_event_lock); 7230 port->pd_events |= TCPM_SOURCING_VBUS; 7231 spin_unlock(&port->pd_event_lock); 7232 kthread_queue_work(port->wq, &port->event_work); 7233 } 7234 EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus); 7235 7236 void tcpm_port_clean(struct tcpm_port *port) 7237 { 7238 spin_lock(&port->pd_event_lock); 7239 port->pd_events |= TCPM_PORT_CLEAN; 7240 spin_unlock(&port->pd_event_lock); 7241 kthread_queue_work(port->wq, &port->event_work); 7242 } 7243 EXPORT_SYMBOL_GPL(tcpm_port_clean); 7244 7245 bool tcpm_port_is_toggling(struct tcpm_port *port) 7246 { 7247 return port->port_type == TYPEC_PORT_DRP && port->state == TOGGLING; 7248 } 7249 EXPORT_SYMBOL_GPL(tcpm_port_is_toggling); 7250 7251 void tcpm_port_error_recovery(struct tcpm_port *port) 7252 { 7253 spin_lock(&port->pd_event_lock); 7254 port->pd_events |= TCPM_PORT_ERROR; 7255 spin_unlock(&port->pd_event_lock); 7256 kthread_queue_work(port->wq, &port->event_work); 7257 } 7258 EXPORT_SYMBOL_GPL(tcpm_port_error_recovery); 7259 7260 static void tcpm_enable_frs_work(struct kthread_work *work) 7261 { 7262 struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs); 7263 int ret; 7264 7265 mutex_lock(&port->lock); 7266 /* Not FRS capable */ 7267 if (!port->connected || port->port_type != TYPEC_PORT_DRP || 7268 port->pwr_opmode != TYPEC_PWR_MODE_PD || 7269 !port->tcpc->enable_frs || 7270 /* Sink caps queried */ 7271 port->sink_cap_done || port->negotiated_rev < PD_REV30) 7272 goto unlock; 7273 7274 /* Send when the state machine is idle */ 7275 if (port->state != SNK_READY || port->vdm_sm_running || 7276 port->vdm_discovery_state == VDM_DISCOVERY_UNKNOWN) 7277 goto resched; 7278 7279 port->upcoming_state = GET_SINK_CAP; 7280 ret = tcpm_ams_start(port, GET_SINK_CAPABILITIES); 7281 if (ret == -EAGAIN) { 7282 port->upcoming_state = INVALID_STATE; 7283 } else { 7284 port->sink_cap_done = true; 7285 goto unlock; 7286 } 7287 resched: 7288 mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS); 7289 unlock: 7290 mutex_unlock(&port->lock); 7291 } 7292 7293 static void tcpm_vdm_discovery_work(struct kthread_work *work) 7294 { 7295 struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_discovery_work); 7296 enum tcpm_transmit_type tx_sop_type = TCPC_TX_SOP; 7297 struct typec_port *typec = port->typec_port; 7298 struct pd_mode_data *modep, *modep_prime; 7299 u32 msg[2] = { }; 7300 int svdm_version; 7301 7302 mutex_lock(&port->lock); 7303 7304 if (!port->vdm_discovery_armed) 7305 goto unlock; 7306 port->vdm_discovery_armed = false; 7307 7308 tcpm_log_force(port, "%s state [%s]", __func__, 7309 vdm_discovery_state_strings[port->vdm_discovery_state]); 7310 7311 /* No need to perform work if Discovery process is complete */ 7312 if (port->vdm_discovery_state == VDM_DISCOVERY_COMPLETE) 7313 goto unlock; 7314 7315 /* Retry if the port is not idle */ 7316 if (!tcpm_can_send_vdm(port->state) || port->vdm_sm_running) { 7317 mod_vdm_discovery_delayed_work(port, SEND_DISCOVERY_VDM_RETRY_MS); 7318 goto unlock; 7319 } 7320 7321 modep = &port->mode_data; 7322 modep_prime = &port->mode_data_prime; 7323 7324 svdm_version = typec_get_negotiated_svdm_version(typec); 7325 7326 switch (port->vdm_discovery_state) { 7327 /* 7328 * The port has not received a Discover Identity response from the port partner. 7329 * 7330 * 1. The port will send Discover Identity to the partner over SOP in the SRC_READY and 7331 * SNK_READY states if there is an explicit contract 7332 * 2. The port will send Discover Identity to the cable over SOP' in the 7333 * SRC_VDM_IDENTITY_REQUEST state if capable of doing so. 7334 */ 7335 case VDM_DISCOVERY_UNKNOWN: 7336 /* Can't send Discover Identity, VDM discovery is complete */ 7337 if (port->data_role == TYPEC_DEVICE && port->negotiated_rev < PD_REV30) { 7338 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 7339 goto unlock; 7340 } 7341 7342 if (port->state == SRC_VDM_IDENTITY_REQUEST) { 7343 tx_sop_type = TCPC_TX_SOP_PRIME; 7344 svdm_version = SVDM_VER_MAX; 7345 } 7346 7347 msg[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_IDENT); 7348 break; 7349 /* 7350 * The port has received a Discover Identity ACK from the port partner. 7351 * 7352 * 1. The port will send Discover Identity to the cable over SOP' in the SRC_READY and 7353 * SNK_READY states if it did not previously discover the cable but is capable of doing 7354 * so. 7355 * 2. The port will send Discover SVIDs to the partner over SOP in the SRC_READY and 7356 * SNK_READY states otherwise. 7357 */ 7358 case VDM_DISCOVERY_PARTNER_IDENT: 7359 if (tcpm_can_communicate_sop_prime(port) && !port->cable) { 7360 tx_sop_type = TCPC_TX_SOP_PRIME; 7361 msg[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_IDENT); 7362 } else { 7363 if (tcpm_can_communicate_sop_prime(port)) 7364 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_CABLE_IDENT); 7365 msg[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID); 7366 } 7367 break; 7368 /* 7369 * The port has received a Discover Identity ACK from the cable. 7370 * 7371 * 1. The port will send Discover SVIDs to the partner over SOP. 7372 */ 7373 case VDM_DISCOVERY_CABLE_IDENT: 7374 msg[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID); 7375 break; 7376 /* 7377 * The port has received a Discover SVIDs ACK from the partner or the last SVIDs supported 7378 * by the partner. 7379 * 7380 * 1. The port will send Discover Modes for the first SVID over SOP if the partner supports 7381 * modal operation and valid SVIDs were registered. 7382 * 2. The vdm_discovery_state will move to VDM_DISCOVERY_COMPLETE otherwise. 7383 */ 7384 case VDM_DISCOVERY_PARTNER_SVIDS: 7385 if (modep->nsvids && supports_modal(port)) { 7386 msg[0] = VDO(modep->svids[0], 1, svdm_version, CMD_DISCOVER_MODES); 7387 } else { 7388 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 7389 goto unlock; 7390 } 7391 break; 7392 /* 7393 * The port has received a Discover Modes ACK from the partner for any mode. 7394 * 7395 * 1. The port will send Discover Modes for the next SVID that has not been discovered to 7396 * the port partner over SOP. 7397 * 2. The port will send Discover SVIDs over SOP' if the port can communicate over SOP' 7398 * and the cable supports VDMs. 7399 * 3. The vdm_discovery_state will move to VDM_DISCOVERY_COMPLETE otherwise. 7400 */ 7401 case VDM_DISCOVERY_PARTNER_MODES: 7402 /* Not all modes have been discovered yet */ 7403 if (modep->svid_index < modep->nsvids) { 7404 msg[0] = VDO(modep_prime->svids[modep->svid_index], 1, svdm_version, 7405 CMD_DISCOVER_MODES); 7406 } else if (tcpm_can_communicate_sop_prime(port) && tcpm_cable_vdm_supported(port)) { 7407 tx_sop_type = TCPC_TX_SOP_PRIME; 7408 svdm_version = typec_get_cable_svdm_version(typec); 7409 msg[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID); 7410 } else { 7411 tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE); 7412 goto unlock; 7413 } 7414 break; 7415 /* 7416 * The port has received a Discover SVIDs ACK from the cable over SOP'. 7417 * 7418 * 1. The port will send Discover Modes for the first SVID over SOP'. 7419 */ 7420 case VDM_DISCOVERY_CABLE_SVIDS: 7421 if (modep_prime->nsvids) { 7422 tx_sop_type = TCPC_TX_SOP_PRIME; 7423 svdm_version = typec_get_cable_svdm_version(typec); 7424 msg[0] = VDO(modep_prime->svids[0], 1, svdm_version, CMD_DISCOVER_MODES); 7425 } else { 7426 goto unlock; 7427 } 7428 break; 7429 /* 7430 * The port has received a Discover Modes ACK from the cable for any mode. 7431 * 7432 * 1. The port will send Discover Modes for the next SVID that has not been discovered to 7433 * the cable over SOP'. 7434 */ 7435 case VDM_DISCOVERY_CABLE_MODES: 7436 if (modep_prime->svid_index < modep_prime->nsvids) { 7437 tx_sop_type = TCPC_TX_SOP_PRIME; 7438 svdm_version = typec_get_cable_svdm_version(typec); 7439 msg[0] = VDO(modep_prime->svids[modep->svid_index], 1, svdm_version, 7440 CMD_DISCOVER_MODES); 7441 } else { 7442 goto unlock; 7443 } 7444 break; 7445 default: 7446 goto unlock; 7447 } 7448 7449 if (svdm_version >= 0) 7450 tcpm_queue_vdm(port, msg[0], &msg[1], 0, tx_sop_type); 7451 7452 unlock: 7453 mutex_unlock(&port->lock); 7454 } 7455 7456 static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data) 7457 { 7458 struct tcpm_port *port = typec_get_drvdata(p); 7459 int ret; 7460 7461 mutex_lock(&port->swap_lock); 7462 mutex_lock(&port->lock); 7463 7464 if (port->typec_caps.data != TYPEC_PORT_DRD) { 7465 ret = -EINVAL; 7466 goto port_unlock; 7467 } 7468 if (port->state != SRC_READY && port->state != SNK_READY) { 7469 ret = -EAGAIN; 7470 goto port_unlock; 7471 } 7472 7473 if (port->data_role == data) { 7474 ret = 0; 7475 goto port_unlock; 7476 } 7477 7478 /* 7479 * XXX 7480 * 6.3.9: If an alternate mode is active, a request to swap 7481 * alternate modes shall trigger a port reset. 7482 * Reject data role swap request in this case. 7483 */ 7484 7485 if (!port->pd_capable) { 7486 /* 7487 * If the partner is not PD capable, reset the port to 7488 * trigger a role change. This can only work if a preferred 7489 * role is configured, and if it matches the requested role. 7490 */ 7491 if (port->try_role == TYPEC_NO_PREFERRED_ROLE || 7492 port->try_role == port->pwr_role) { 7493 ret = -EINVAL; 7494 goto port_unlock; 7495 } 7496 port->non_pd_role_swap = true; 7497 tcpm_set_state(port, PORT_RESET, 0); 7498 } else { 7499 port->upcoming_state = DR_SWAP_SEND; 7500 ret = tcpm_ams_start(port, DATA_ROLE_SWAP); 7501 if (ret == -EAGAIN) { 7502 port->upcoming_state = INVALID_STATE; 7503 goto port_unlock; 7504 } 7505 } 7506 7507 port->swap_status = 0; 7508 port->swap_pending = true; 7509 reinit_completion(&port->swap_complete); 7510 mutex_unlock(&port->lock); 7511 7512 if (!wait_for_completion_timeout(&port->swap_complete, 7513 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) 7514 ret = -ETIMEDOUT; 7515 else 7516 ret = port->swap_status; 7517 7518 port->non_pd_role_swap = false; 7519 goto swap_unlock; 7520 7521 port_unlock: 7522 mutex_unlock(&port->lock); 7523 swap_unlock: 7524 mutex_unlock(&port->swap_lock); 7525 return ret; 7526 } 7527 7528 static int tcpm_pr_set(struct typec_port *p, enum typec_role role) 7529 { 7530 struct tcpm_port *port = typec_get_drvdata(p); 7531 int ret; 7532 7533 mutex_lock(&port->swap_lock); 7534 mutex_lock(&port->lock); 7535 7536 if (port->port_type != TYPEC_PORT_DRP) { 7537 ret = -EINVAL; 7538 goto port_unlock; 7539 } 7540 if (port->state != SRC_READY && port->state != SNK_READY) { 7541 ret = -EAGAIN; 7542 goto port_unlock; 7543 } 7544 7545 if (role == port->pwr_role) { 7546 ret = 0; 7547 goto port_unlock; 7548 } 7549 7550 port->upcoming_state = PR_SWAP_SEND; 7551 ret = tcpm_ams_start(port, POWER_ROLE_SWAP); 7552 if (ret == -EAGAIN) { 7553 port->upcoming_state = INVALID_STATE; 7554 goto port_unlock; 7555 } 7556 7557 port->swap_status = 0; 7558 port->swap_pending = true; 7559 reinit_completion(&port->swap_complete); 7560 mutex_unlock(&port->lock); 7561 7562 if (!wait_for_completion_timeout(&port->swap_complete, 7563 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) 7564 ret = -ETIMEDOUT; 7565 else 7566 ret = port->swap_status; 7567 7568 goto swap_unlock; 7569 7570 port_unlock: 7571 mutex_unlock(&port->lock); 7572 swap_unlock: 7573 mutex_unlock(&port->swap_lock); 7574 return ret; 7575 } 7576 7577 static int tcpm_vconn_set(struct typec_port *p, enum typec_role role) 7578 { 7579 struct tcpm_port *port = typec_get_drvdata(p); 7580 int ret; 7581 7582 mutex_lock(&port->swap_lock); 7583 mutex_lock(&port->lock); 7584 7585 if (port->state != SRC_READY && port->state != SNK_READY) { 7586 ret = -EAGAIN; 7587 goto port_unlock; 7588 } 7589 7590 if (role == port->vconn_role) { 7591 ret = 0; 7592 goto port_unlock; 7593 } 7594 7595 port->upcoming_state = VCONN_SWAP_SEND; 7596 ret = tcpm_ams_start(port, VCONN_SWAP); 7597 if (ret == -EAGAIN) { 7598 port->upcoming_state = INVALID_STATE; 7599 goto port_unlock; 7600 } 7601 7602 port->swap_status = 0; 7603 port->swap_pending = true; 7604 reinit_completion(&port->swap_complete); 7605 mutex_unlock(&port->lock); 7606 7607 if (!wait_for_completion_timeout(&port->swap_complete, 7608 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) 7609 ret = -ETIMEDOUT; 7610 else 7611 ret = port->swap_status; 7612 7613 goto swap_unlock; 7614 7615 port_unlock: 7616 mutex_unlock(&port->lock); 7617 swap_unlock: 7618 mutex_unlock(&port->swap_lock); 7619 return ret; 7620 } 7621 7622 static int tcpm_try_role(struct typec_port *p, int role) 7623 { 7624 struct tcpm_port *port = typec_get_drvdata(p); 7625 struct tcpc_dev *tcpc = port->tcpc; 7626 int ret = 0; 7627 7628 mutex_lock(&port->lock); 7629 if (tcpc->try_role) 7630 ret = tcpc->try_role(tcpc, role); 7631 if (!ret) 7632 port->try_role = role; 7633 port->try_src_count = 0; 7634 port->try_snk_count = 0; 7635 mutex_unlock(&port->lock); 7636 7637 return ret; 7638 } 7639 7640 static int tcpm_aug_set_op_curr(struct tcpm_port *port, u16 req_op_curr_ma) 7641 { 7642 unsigned int target_mw; 7643 int ret; 7644 7645 mutex_lock(&port->swap_lock); 7646 mutex_lock(&port->lock); 7647 7648 if (port->pps_data.active) { 7649 req_op_curr_ma = req_op_curr_ma - 7650 (req_op_curr_ma % RDO_PROG_CURR_MA_STEP); 7651 if (req_op_curr_ma > port->pps_data.max_curr) { 7652 ret = -EINVAL; 7653 goto port_unlock; 7654 } 7655 target_mw = (req_op_curr_ma * port->supply_voltage) / 1000; 7656 if (target_mw < port->operating_snk_mw) { 7657 ret = -EINVAL; 7658 goto port_unlock; 7659 } 7660 } else if (!port->spr_avs_data.active) { 7661 ret = -EOPNOTSUPP; 7662 goto port_unlock; 7663 } 7664 7665 if (port->state != SNK_READY) { 7666 ret = -EAGAIN; 7667 goto port_unlock; 7668 } 7669 7670 if (port->pps_data.active) 7671 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; 7672 else 7673 port->upcoming_state = SNK_NEGOTIATE_SPR_AVS_CAPABILITIES; 7674 7675 ret = tcpm_ams_start(port, POWER_NEGOTIATION); 7676 if (ret == -EAGAIN) { 7677 port->upcoming_state = INVALID_STATE; 7678 goto port_unlock; 7679 } 7680 7681 reinit_completion(&port->aug_supply_req_complete); 7682 if (port->pps_data.active) 7683 port->pps_data.req_op_curr = req_op_curr_ma; 7684 else 7685 port->spr_avs_data.req_op_curr_ma = req_op_curr_ma; 7686 port->aug_supply_req_status = 0; 7687 port->aug_supply_req_pending = true; 7688 mutex_unlock(&port->lock); 7689 7690 if (!wait_for_completion_timeout(&port->aug_supply_req_complete, 7691 msecs_to_jiffies(PD_AUG_PSY_CTRL_TIMEOUT))) 7692 ret = -ETIMEDOUT; 7693 else 7694 ret = port->aug_supply_req_status; 7695 7696 goto swap_unlock; 7697 7698 port_unlock: 7699 mutex_unlock(&port->lock); 7700 swap_unlock: 7701 mutex_unlock(&port->swap_lock); 7702 7703 return ret; 7704 } 7705 7706 static int tcpm_aug_set_out_volt(struct tcpm_port *port, u16 req_out_volt_mv) 7707 { 7708 unsigned int target_mw; 7709 int ret; 7710 7711 mutex_lock(&port->swap_lock); 7712 mutex_lock(&port->lock); 7713 7714 if (port->pps_data.active) { 7715 req_out_volt_mv = req_out_volt_mv - (req_out_volt_mv % 7716 RDO_PROG_VOLT_MV_STEP); 7717 /* Round down output voltage to align with PPS valid steps */ 7718 target_mw = (port->current_limit * req_out_volt_mv) / 1000; 7719 if (target_mw < port->operating_snk_mw) { 7720 ret = -EINVAL; 7721 goto port_unlock; 7722 } 7723 } else if (!port->spr_avs_data.active) { 7724 ret = -EOPNOTSUPP; 7725 goto port_unlock; 7726 } 7727 7728 if (port->state != SNK_READY) { 7729 ret = -EAGAIN; 7730 goto port_unlock; 7731 } 7732 7733 if (port->pps_data.active) 7734 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; 7735 else 7736 port->upcoming_state = SNK_NEGOTIATE_SPR_AVS_CAPABILITIES; 7737 7738 ret = tcpm_ams_start(port, POWER_NEGOTIATION); 7739 if (ret == -EAGAIN) { 7740 port->upcoming_state = INVALID_STATE; 7741 goto port_unlock; 7742 } 7743 7744 reinit_completion(&port->aug_supply_req_complete); 7745 if (port->pps_data.active) 7746 port->pps_data.req_out_volt = req_out_volt_mv; 7747 else 7748 port->spr_avs_data.req_out_volt_mv = req_out_volt_mv; 7749 port->aug_supply_req_status = 0; 7750 port->aug_supply_req_pending = true; 7751 mutex_unlock(&port->lock); 7752 7753 if (!wait_for_completion_timeout(&port->aug_supply_req_complete, 7754 msecs_to_jiffies(PD_AUG_PSY_CTRL_TIMEOUT))) 7755 ret = -ETIMEDOUT; 7756 else 7757 ret = port->aug_supply_req_status; 7758 7759 goto swap_unlock; 7760 7761 port_unlock: 7762 mutex_unlock(&port->lock); 7763 swap_unlock: 7764 mutex_unlock(&port->swap_lock); 7765 7766 return ret; 7767 } 7768 7769 static int tcpm_pps_activate(struct tcpm_port *port, bool activate) 7770 { 7771 int ret = 0; 7772 7773 mutex_lock(&port->swap_lock); 7774 mutex_lock(&port->lock); 7775 7776 if (!port->pps_data.supported) { 7777 ret = -EOPNOTSUPP; 7778 goto port_unlock; 7779 } 7780 7781 /* Trying to deactivate PPS when already deactivated so just bail */ 7782 if (!port->pps_data.active && !activate) 7783 goto port_unlock; 7784 7785 if (port->state != SNK_READY) { 7786 ret = -EAGAIN; 7787 goto port_unlock; 7788 } 7789 7790 if (activate) 7791 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; 7792 else 7793 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES; 7794 ret = tcpm_ams_start(port, POWER_NEGOTIATION); 7795 if (ret == -EAGAIN) { 7796 port->upcoming_state = INVALID_STATE; 7797 goto port_unlock; 7798 } 7799 7800 reinit_completion(&port->aug_supply_req_complete); 7801 port->aug_supply_req_status = 0; 7802 port->aug_supply_req_pending = true; 7803 7804 /* Trigger PPS request or move back to standard PDO contract */ 7805 if (activate) { 7806 port->pps_data.req_out_volt = port->supply_voltage; 7807 port->pps_data.req_op_curr = port->current_limit; 7808 } 7809 mutex_unlock(&port->lock); 7810 7811 if (!wait_for_completion_timeout(&port->aug_supply_req_complete, 7812 msecs_to_jiffies(PD_AUG_PSY_CTRL_TIMEOUT))) 7813 ret = -ETIMEDOUT; 7814 else 7815 ret = port->aug_supply_req_status; 7816 7817 goto swap_unlock; 7818 7819 port_unlock: 7820 mutex_unlock(&port->lock); 7821 swap_unlock: 7822 mutex_unlock(&port->swap_lock); 7823 7824 return ret; 7825 } 7826 7827 static int tcpm_spr_avs_activate(struct tcpm_port *port, bool activate) 7828 { 7829 int ret = 0; 7830 7831 mutex_lock(&port->swap_lock); 7832 mutex_lock(&port->lock); 7833 7834 if (port->spr_avs_data.port_snk_status == SPR_AVS_NOT_SUPPORTED || 7835 port->spr_avs_data.port_partner_src_status == SPR_AVS_NOT_SUPPORTED) { 7836 tcpm_log(port, "SPR_AVS not supported"); 7837 ret = -EOPNOTSUPP; 7838 goto port_unlock; 7839 } 7840 7841 /* Trying to deactivate SPR AVS when already deactivated so just bail */ 7842 if (!port->spr_avs_data.active && !activate) 7843 goto port_unlock; 7844 7845 if (port->state != SNK_READY) { 7846 tcpm_log(port, 7847 "SPR_AVS cannot be activated. Port not in SNK_READY"); 7848 ret = -EAGAIN; 7849 goto port_unlock; 7850 } 7851 7852 if (activate) 7853 port->upcoming_state = SNK_NEGOTIATE_SPR_AVS_CAPABILITIES; 7854 else 7855 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES; 7856 ret = tcpm_ams_start(port, POWER_NEGOTIATION); 7857 if (ret == -EAGAIN) { 7858 tcpm_log(port, "SPR_AVS cannot be %s. AMS start failed", 7859 activate ? "activated" : "deactivated"); 7860 port->upcoming_state = INVALID_STATE; 7861 goto port_unlock; 7862 } 7863 7864 reinit_completion(&port->aug_supply_req_complete); 7865 port->aug_supply_req_status = 0; 7866 port->aug_supply_req_pending = true; 7867 7868 /* Trigger AVS request or move back to standard PDO contract */ 7869 if (activate) { 7870 port->spr_avs_data.req_out_volt_mv = port->supply_voltage; 7871 port->spr_avs_data.req_op_curr_ma = port->current_limit; 7872 } 7873 mutex_unlock(&port->lock); 7874 7875 if (!wait_for_completion_timeout(&port->aug_supply_req_complete, 7876 msecs_to_jiffies(PD_AUG_PSY_CTRL_TIMEOUT))) 7877 ret = -ETIMEDOUT; 7878 else 7879 ret = port->aug_supply_req_status; 7880 7881 goto swap_unlock; 7882 7883 port_unlock: 7884 mutex_unlock(&port->lock); 7885 swap_unlock: 7886 mutex_unlock(&port->swap_lock); 7887 7888 return ret; 7889 } 7890 7891 static void tcpm_init(struct tcpm_port *port) 7892 { 7893 enum typec_cc_status cc1, cc2; 7894 7895 port->tcpc->init(port->tcpc); 7896 7897 tcpm_reset_port(port); 7898 7899 /* 7900 * XXX 7901 * Should possibly wait for VBUS to settle if it was enabled locally 7902 * since tcpm_reset_port() will disable VBUS. 7903 */ 7904 port->vbus_present = port->tcpc->get_vbus(port->tcpc); 7905 if (port->vbus_present) 7906 port->vbus_never_low = true; 7907 7908 /* 7909 * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V. 7910 * So implicitly vbus_vsafe0v = false. 7911 * 7912 * 2. When vbus_present is false and TCPC does NOT support querying 7913 * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e. 7914 * vbus_vsafe0v is true. 7915 * 7916 * 3. When vbus_present is false and TCPC does support querying vsafe0v, 7917 * then, query tcpc for vsafe0v status. 7918 */ 7919 if (port->vbus_present) 7920 port->vbus_vsafe0v = false; 7921 else if (!port->tcpc->is_vbus_vsafe0v) 7922 port->vbus_vsafe0v = true; 7923 else 7924 port->vbus_vsafe0v = port->tcpc->is_vbus_vsafe0v(port->tcpc); 7925 7926 tcpm_set_state(port, tcpm_default_state(port), 0); 7927 7928 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0) 7929 _tcpm_cc_change(port, cc1, cc2); 7930 7931 /* 7932 * Some adapters need a clean slate at startup, and won't recover 7933 * otherwise. So do not try to be fancy and force a clean disconnect. 7934 */ 7935 tcpm_set_state(port, PORT_RESET, 0); 7936 } 7937 7938 static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type) 7939 { 7940 struct tcpm_port *port = typec_get_drvdata(p); 7941 7942 mutex_lock(&port->lock); 7943 if (type == port->port_type) 7944 goto port_unlock; 7945 7946 port->port_type = type; 7947 7948 if (!port->connected) { 7949 tcpm_set_state(port, PORT_RESET, 0); 7950 } else if (type == TYPEC_PORT_SNK) { 7951 if (!(port->pwr_role == TYPEC_SINK && 7952 port->data_role == TYPEC_DEVICE)) 7953 tcpm_set_state(port, PORT_RESET, 0); 7954 } else if (type == TYPEC_PORT_SRC) { 7955 if (!(port->pwr_role == TYPEC_SOURCE && 7956 port->data_role == TYPEC_HOST)) 7957 tcpm_set_state(port, PORT_RESET, 0); 7958 } 7959 7960 port_unlock: 7961 mutex_unlock(&port->lock); 7962 return 0; 7963 } 7964 7965 static struct pd_data *tcpm_find_pd_data(struct tcpm_port *port, struct usb_power_delivery *pd) 7966 { 7967 int i; 7968 7969 for (i = 0; port->pd_list[i]; i++) { 7970 if (port->pd_list[i]->pd == pd) 7971 return port->pd_list[i]; 7972 } 7973 7974 return ERR_PTR(-ENODATA); 7975 } 7976 7977 static struct usb_power_delivery **tcpm_pd_get(struct typec_port *p) 7978 { 7979 struct tcpm_port *port = typec_get_drvdata(p); 7980 7981 return port->pds; 7982 } 7983 7984 static int tcpm_pd_set(struct typec_port *p, struct usb_power_delivery *pd) 7985 { 7986 struct tcpm_port *port = typec_get_drvdata(p); 7987 struct pd_data *data; 7988 int i, ret = 0; 7989 7990 mutex_lock(&port->lock); 7991 7992 if (port->selected_pd == pd) 7993 goto unlock; 7994 7995 data = tcpm_find_pd_data(port, pd); 7996 if (IS_ERR(data)) { 7997 ret = PTR_ERR(data); 7998 goto unlock; 7999 } 8000 8001 if (data->sink_desc.pdo[0]) { 8002 for (i = 0; i < PDO_MAX_OBJECTS && data->sink_desc.pdo[i]; i++) 8003 port->snk_pdo[i] = data->sink_desc.pdo[i]; 8004 port->nr_snk_pdo = i; 8005 port->operating_snk_mw = data->operating_snk_mw; 8006 } 8007 8008 if (data->source_desc.pdo[0]) { 8009 for (i = 0; i < PDO_MAX_OBJECTS && data->source_desc.pdo[i]; i++) 8010 port->src_pdo[i] = data->source_desc.pdo[i]; 8011 port->nr_src_pdo = i; 8012 } 8013 8014 switch (port->state) { 8015 case SRC_UNATTACHED: 8016 case SRC_ATTACH_WAIT: 8017 case SRC_TRYWAIT: 8018 tcpm_set_cc(port, tcpm_rp_cc(port)); 8019 break; 8020 case SRC_SEND_CAPABILITIES: 8021 case SRC_SEND_CAPABILITIES_TIMEOUT: 8022 case SRC_NEGOTIATE_CAPABILITIES: 8023 case SRC_READY: 8024 case SRC_WAIT_NEW_CAPABILITIES: 8025 port->caps_count = 0; 8026 port->upcoming_state = SRC_SEND_CAPABILITIES; 8027 ret = tcpm_ams_start(port, POWER_NEGOTIATION); 8028 if (ret == -EAGAIN) { 8029 port->upcoming_state = INVALID_STATE; 8030 goto unlock; 8031 } 8032 break; 8033 case SNK_NEGOTIATE_CAPABILITIES: 8034 case SNK_NEGOTIATE_PPS_CAPABILITIES: 8035 case SNK_NEGOTIATE_SPR_AVS_CAPABILITIES: 8036 case SNK_READY: 8037 case SNK_TRANSITION_SINK: 8038 case SNK_TRANSITION_SINK_VBUS: 8039 if (port->pps_data.active) { 8040 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; 8041 } else if (port->pd_capable) { 8042 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES; 8043 if (port->spr_avs_data.active) { 8044 /* 8045 * De-activate AVS and fallback to PD to 8046 * re-evaluate whether AVS is supported in the 8047 * current sink cap set. 8048 */ 8049 port->spr_avs_data.active = false; 8050 port->spr_avs_data.port_snk_status = SPR_AVS_UNKNOWN; 8051 } 8052 } else { 8053 break; 8054 } 8055 port->update_sink_caps = true; 8056 8057 ret = tcpm_ams_start(port, POWER_NEGOTIATION); 8058 if (ret == -EAGAIN) { 8059 port->upcoming_state = INVALID_STATE; 8060 goto unlock; 8061 } 8062 break; 8063 default: 8064 break; 8065 } 8066 8067 port->port_source_caps = data->source_cap; 8068 port->port_sink_caps = data->sink_cap; 8069 typec_port_set_usb_power_delivery(p, NULL); 8070 port->selected_pd = pd; 8071 typec_port_set_usb_power_delivery(p, port->selected_pd); 8072 unlock: 8073 mutex_unlock(&port->lock); 8074 return ret; 8075 } 8076 8077 static const struct typec_operations tcpm_ops = { 8078 .try_role = tcpm_try_role, 8079 .dr_set = tcpm_dr_set, 8080 .pr_set = tcpm_pr_set, 8081 .vconn_set = tcpm_vconn_set, 8082 .port_type_set = tcpm_port_type_set, 8083 .pd_get = tcpm_pd_get, 8084 .pd_set = tcpm_pd_set 8085 }; 8086 8087 void tcpm_tcpc_reset(struct tcpm_port *port) 8088 { 8089 mutex_lock(&port->lock); 8090 /* XXX: Maintain PD connection if possible? */ 8091 tcpm_init(port); 8092 mutex_unlock(&port->lock); 8093 } 8094 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset); 8095 8096 static void tcpm_port_unregister_pd(struct tcpm_port *port) 8097 { 8098 int i; 8099 8100 port->port_sink_caps = NULL; 8101 port->port_source_caps = NULL; 8102 for (i = 0; i < port->pd_count; i++) { 8103 usb_power_delivery_unregister_capabilities(port->pd_list[i]->sink_cap); 8104 usb_power_delivery_unregister_capabilities(port->pd_list[i]->source_cap); 8105 devm_kfree(port->dev, port->pd_list[i]); 8106 port->pd_list[i] = NULL; 8107 usb_power_delivery_unregister(port->pds[i]); 8108 port->pds[i] = NULL; 8109 } 8110 } 8111 8112 static int tcpm_port_register_pd(struct tcpm_port *port) 8113 { 8114 u16 pd_revision = port->typec_caps.pd_revision; 8115 u16 pd_version = port->pd_rev.ver_major << 8 | port->pd_rev.ver_minor; 8116 struct usb_power_delivery_desc desc = { pd_revision, pd_version }; 8117 struct usb_power_delivery_capabilities *cap; 8118 int ret, i; 8119 8120 if (!port->nr_src_pdo && !port->nr_snk_pdo) 8121 return 0; 8122 8123 for (i = 0; i < port->pd_count; i++) { 8124 port->pds[i] = usb_power_delivery_register(port->dev, &desc); 8125 if (IS_ERR(port->pds[i])) { 8126 ret = PTR_ERR(port->pds[i]); 8127 goto err_unregister; 8128 } 8129 port->pd_list[i]->pd = port->pds[i]; 8130 8131 if (port->pd_list[i]->source_desc.pdo[0]) { 8132 cap = usb_power_delivery_register_capabilities(port->pds[i], 8133 &port->pd_list[i]->source_desc); 8134 if (IS_ERR(cap)) { 8135 ret = PTR_ERR(cap); 8136 goto err_unregister; 8137 } 8138 port->pd_list[i]->source_cap = cap; 8139 } 8140 8141 if (port->pd_list[i]->sink_desc.pdo[0]) { 8142 cap = usb_power_delivery_register_capabilities(port->pds[i], 8143 &port->pd_list[i]->sink_desc); 8144 if (IS_ERR(cap)) { 8145 ret = PTR_ERR(cap); 8146 goto err_unregister; 8147 } 8148 port->pd_list[i]->sink_cap = cap; 8149 } 8150 } 8151 8152 port->port_source_caps = port->pd_list[0]->source_cap; 8153 port->port_sink_caps = port->pd_list[0]->sink_cap; 8154 port->selected_pd = port->pds[0]; 8155 return 0; 8156 8157 err_unregister: 8158 tcpm_port_unregister_pd(port); 8159 8160 return ret; 8161 } 8162 8163 static void tcpm_fw_get_timings(struct tcpm_port *port, struct fwnode_handle *fwnode) 8164 { 8165 int ret; 8166 u32 val; 8167 8168 ret = fwnode_property_read_u32(fwnode, "sink-wait-cap-time-ms", &val); 8169 if (!ret) 8170 port->timings.sink_wait_cap_time = val; 8171 else 8172 port->timings.sink_wait_cap_time = PD_T_SINK_WAIT_CAP; 8173 8174 ret = fwnode_property_read_u32(fwnode, "ps-source-off-time-ms", &val); 8175 if (!ret) 8176 port->timings.ps_src_off_time = val; 8177 else 8178 port->timings.ps_src_off_time = PD_T_PS_SOURCE_OFF; 8179 8180 ret = fwnode_property_read_u32(fwnode, "cc-debounce-time-ms", &val); 8181 if (!ret) 8182 port->timings.cc_debounce_time = val; 8183 else 8184 port->timings.cc_debounce_time = PD_T_CC_DEBOUNCE; 8185 8186 ret = fwnode_property_read_u32(fwnode, "sink-bc12-completion-time-ms", &val); 8187 if (!ret) 8188 port->timings.snk_bc12_cmpletion_time = val; 8189 } 8190 8191 static void tcpm_fw_get_pd_ident(struct tcpm_port *port) 8192 { 8193 struct pd_identifier *pd_ident = &port->pd_ident; 8194 u32 *vdo; 8195 8196 /* First 3 vdo values contain info regarding USB PID, VID & XID */ 8197 if (port->nr_snk_vdo >= 3) 8198 vdo = port->snk_vdo; 8199 else if (port->nr_snk_vdo_v1 >= 3) 8200 vdo = port->snk_vdo_v1; 8201 else 8202 return; 8203 8204 pd_ident->vid = PD_IDH_VID(vdo[0]); 8205 pd_ident->pid = PD_PRODUCT_PID(vdo[2]); 8206 pd_ident->xid = PD_CSTAT_XID(vdo[1]); 8207 tcpm_log(port, "vid:%#x pid:%#x xid:%#x", 8208 pd_ident->vid, pd_ident->pid, pd_ident->xid); 8209 } 8210 8211 static void tcpm_parse_snk_pdos(struct tcpm_port *port) 8212 { 8213 struct sink_caps_ext_data *caps = &port->sink_caps_ext; 8214 u32 max_mv, max_ma; 8215 u8 avs_tier1_pdp, avs_tier2_pdp; 8216 int i, pdo_itr; 8217 u32 *snk_pdos; 8218 8219 for (i = 0; i < port->pd_count; ++i) { 8220 snk_pdos = port->pd_list[i]->sink_desc.pdo; 8221 for (pdo_itr = 0; pdo_itr < PDO_MAX_OBJECTS && snk_pdos[pdo_itr]; 8222 ++pdo_itr) { 8223 u32 pdo = snk_pdos[pdo_itr]; 8224 u8 curr_snk_pdp = 0; 8225 8226 switch (pdo_type(pdo)) { 8227 case PDO_TYPE_FIXED: 8228 max_mv = pdo_fixed_voltage(pdo); 8229 max_ma = pdo_fixed_current(pdo); 8230 curr_snk_pdp = UW_TO_W(max_mv * max_ma); 8231 break; 8232 case PDO_TYPE_BATT: 8233 curr_snk_pdp = UW_TO_W(pdo_max_power(pdo)); 8234 break; 8235 case PDO_TYPE_VAR: 8236 max_mv = pdo_max_voltage(pdo); 8237 max_ma = pdo_max_current(pdo); 8238 curr_snk_pdp = UW_TO_W(max_mv * max_ma); 8239 break; 8240 case PDO_TYPE_APDO: 8241 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) { 8242 max_mv = pdo_pps_apdo_max_voltage(pdo); 8243 max_ma = pdo_pps_apdo_max_current(pdo); 8244 curr_snk_pdp = UW_TO_W(max_mv * max_ma); 8245 caps->modes |= SINK_MODE_PPS; 8246 } else if (pdo_apdo_type(pdo) == 8247 APDO_TYPE_SPR_AVS) { 8248 avs_tier1_pdp = UW_TO_W(SPR_AVS_TIER1_MAX_VOLT_MV 8249 * pdo_spr_avs_apdo_9v_to_15v_max_current_ma(pdo)); 8250 avs_tier2_pdp = UW_TO_W(SPR_AVS_TIER2_MAX_VOLT_MV 8251 * pdo_spr_avs_apdo_15v_to_20v_max_current_ma(pdo)); 8252 curr_snk_pdp = max(avs_tier1_pdp, avs_tier2_pdp); 8253 caps->modes |= SINK_MODE_AVS; 8254 } 8255 break; 8256 default: 8257 tcpm_log(port, "Invalid source PDO type, ignoring"); 8258 continue; 8259 } 8260 8261 caps->spr_max_pdp = max(caps->spr_max_pdp, 8262 curr_snk_pdp); 8263 } 8264 } 8265 } 8266 8267 static void tcpm_fw_get_sink_caps_ext(struct tcpm_port *port, 8268 struct fwnode_handle *fwnode) 8269 { 8270 struct sink_caps_ext_data *caps = &port->sink_caps_ext; 8271 int ret; 8272 u32 val; 8273 8274 /* 8275 * Load step represents the change in current per usec that a given 8276 * source can tolerate while maintaining Vbus within the vSrcValid 8277 * range. For a sink this represents the "preferred" load-step value. It 8278 * can only have 2 values (150 mA/usec or 500 mA/usec) with 150 mA/usec 8279 * being the default. 8280 */ 8281 ret = fwnode_property_read_u32(fwnode, "sink-load-step", &val); 8282 if (!ret) 8283 caps->load_step = val == 500 ? 1 : 0; 8284 8285 fwnode_property_read_u16(fwnode, "sink-load-characteristics", 8286 &caps->load_char); 8287 fwnode_property_read_u8(fwnode, "sink-compliance", &caps->compliance); 8288 caps->modes = SINK_MODE_VBUS; 8289 8290 /* 8291 * As per "6.5.13.14" SPR Sink Operational PDP definition, for battery 8292 * powered devices, this value will correspond to the PDP of the 8293 * charging adapter either shipped or recommended for use with it. For 8294 * batteryless sink devices SPR Operational PDP indicates the power 8295 * required to operate all the device's functional modes. Hence, this 8296 * value may be considered equal to port's operating_snk_mw. As 8297 * operating_sink_mw can change as per the pd set used thus, OP PDP 8298 * is determined when populating Sink Caps Extended Data Block. 8299 */ 8300 if (port->self_powered) { 8301 fwnode_property_read_u32(fwnode, "charging-adapter-pdp-milliwatt", 8302 &val); 8303 caps->spr_op_pdp = (u8)(val / 1000); 8304 caps->modes |= SINK_MODE_BATT; 8305 } 8306 8307 tcpm_parse_snk_pdos(port); 8308 tcpm_log(port, 8309 "load-step:%#x load-char:%#x compl:%#x op-pdp:%#x max-pdp:%#x", 8310 caps->load_step, caps->load_char, caps->compliance, 8311 caps->spr_op_pdp, caps->spr_max_pdp); 8312 } 8313 8314 static int tcpm_fw_get_caps(struct tcpm_port *port, struct fwnode_handle *fwnode) 8315 { 8316 struct fwnode_handle *capabilities, *caps = NULL; 8317 unsigned int nr_src_pdo, nr_snk_pdo; 8318 const char *opmode_str; 8319 u32 *src_pdo, *snk_pdo; 8320 u32 uw, frs_current; 8321 int ret = 0, i; 8322 int mode; 8323 8324 if (!fwnode) 8325 return -EINVAL; 8326 8327 /* 8328 * This fwnode has a "compatible" property, but is never populated as a 8329 * struct device. Instead we simply parse it to read the properties. 8330 * This it breaks fw_devlink=on. To maintain backward compatibility 8331 * with existing DT files, we work around this by deleting any 8332 * fwnode_links to/from this fwnode. 8333 */ 8334 fw_devlink_purge_absent_suppliers(fwnode); 8335 8336 ret = typec_get_fw_cap(&port->typec_caps, fwnode); 8337 if (ret < 0) 8338 return ret; 8339 8340 mode = 0; 8341 8342 if (fwnode_property_read_bool(fwnode, "accessory-mode-audio")) 8343 port->typec_caps.accessory[mode++] = TYPEC_ACCESSORY_AUDIO; 8344 8345 if (fwnode_property_read_bool(fwnode, "accessory-mode-debug")) 8346 port->typec_caps.accessory[mode++] = TYPEC_ACCESSORY_DEBUG; 8347 8348 port->port_type = port->typec_caps.type; 8349 port->pd_supported = !fwnode_property_read_bool(fwnode, "pd-disable"); 8350 port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop"); 8351 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered"); 8352 8353 if (!port->pd_supported) { 8354 ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str); 8355 if (ret) 8356 return ret; 8357 ret = typec_find_pwr_opmode(opmode_str); 8358 if (ret < 0) 8359 return ret; 8360 port->src_rp = tcpm_pwr_opmode_to_rp(ret); 8361 return 0; 8362 } 8363 8364 /* The following code are applicable to pd-capable ports, i.e. pd_supported is true. */ 8365 8366 /* FRS can only be supported by DRP ports */ 8367 if (port->port_type == TYPEC_PORT_DRP) { 8368 ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current", 8369 &frs_current); 8370 if (!ret && frs_current <= FRS_5V_3A) 8371 port->new_source_frs_current = frs_current; 8372 8373 if (ret) 8374 ret = 0; 8375 } 8376 8377 /* For the backward compatibility, "capabilities" node is optional. */ 8378 capabilities = fwnode_get_named_child_node(fwnode, "capabilities"); 8379 if (!capabilities) { 8380 port->pd_count = 1; 8381 } else { 8382 port->pd_count = fwnode_get_child_node_count(capabilities); 8383 if (!port->pd_count) { 8384 ret = -ENODATA; 8385 goto put_capabilities; 8386 } 8387 } 8388 8389 port->pds = devm_kzalloc_objs(port->dev, struct usb_power_delivery *, 8390 port->pd_count); 8391 if (!port->pds) { 8392 ret = -ENOMEM; 8393 goto put_capabilities; 8394 } 8395 8396 port->pd_list = devm_kzalloc_objs(port->dev, struct pd_data *, 8397 port->pd_count); 8398 if (!port->pd_list) { 8399 ret = -ENOMEM; 8400 goto put_capabilities; 8401 } 8402 8403 for (i = 0; i < port->pd_count; i++) { 8404 port->pd_list[i] = devm_kzalloc_obj(port->dev, struct pd_data); 8405 if (!port->pd_list[i]) { 8406 ret = -ENOMEM; 8407 goto put_capabilities; 8408 } 8409 8410 src_pdo = port->pd_list[i]->source_desc.pdo; 8411 port->pd_list[i]->source_desc.role = TYPEC_SOURCE; 8412 snk_pdo = port->pd_list[i]->sink_desc.pdo; 8413 port->pd_list[i]->sink_desc.role = TYPEC_SINK; 8414 8415 /* If "capabilities" is NULL, fall back to single pd cap population. */ 8416 if (!capabilities) 8417 caps = fwnode; 8418 else 8419 caps = fwnode_get_next_child_node(capabilities, caps); 8420 8421 if (port->port_type != TYPEC_PORT_SNK) { 8422 ret = fwnode_property_count_u32(caps, "source-pdos"); 8423 if (ret == 0) { 8424 ret = -EINVAL; 8425 goto put_caps; 8426 } 8427 if (ret < 0) 8428 goto put_caps; 8429 8430 nr_src_pdo = min(ret, PDO_MAX_OBJECTS); 8431 ret = fwnode_property_read_u32_array(caps, "source-pdos", src_pdo, 8432 nr_src_pdo); 8433 if (ret) 8434 goto put_caps; 8435 8436 ret = tcpm_validate_caps(port, src_pdo, nr_src_pdo); 8437 if (ret) 8438 goto put_caps; 8439 8440 if (i == 0) { 8441 port->nr_src_pdo = nr_src_pdo; 8442 memcpy_and_pad(port->src_pdo, sizeof(u32) * PDO_MAX_OBJECTS, 8443 port->pd_list[0]->source_desc.pdo, 8444 sizeof(u32) * nr_src_pdo, 8445 0); 8446 } 8447 } 8448 8449 if (port->port_type != TYPEC_PORT_SRC) { 8450 ret = fwnode_property_count_u32(caps, "sink-pdos"); 8451 if (ret == 0) { 8452 ret = -EINVAL; 8453 goto put_caps; 8454 } 8455 8456 if (ret < 0) 8457 goto put_caps; 8458 8459 nr_snk_pdo = min(ret, PDO_MAX_OBJECTS); 8460 ret = fwnode_property_read_u32_array(caps, "sink-pdos", snk_pdo, 8461 nr_snk_pdo); 8462 if (ret) 8463 goto put_caps; 8464 8465 ret = tcpm_validate_caps(port, snk_pdo, nr_snk_pdo); 8466 if (ret) 8467 goto put_caps; 8468 8469 if (fwnode_property_read_u32(caps, "op-sink-microwatt", &uw) < 0) { 8470 ret = -EINVAL; 8471 goto put_caps; 8472 } 8473 8474 port->pd_list[i]->operating_snk_mw = uw / 1000; 8475 8476 if (i == 0) { 8477 port->nr_snk_pdo = nr_snk_pdo; 8478 memcpy_and_pad(port->snk_pdo, sizeof(u32) * PDO_MAX_OBJECTS, 8479 port->pd_list[0]->sink_desc.pdo, 8480 sizeof(u32) * nr_snk_pdo, 8481 0); 8482 port->operating_snk_mw = port->pd_list[0]->operating_snk_mw; 8483 } 8484 } 8485 } 8486 8487 if (port->port_type != TYPEC_PORT_SRC) 8488 tcpm_fw_get_sink_caps_ext(port, fwnode); 8489 8490 put_caps: 8491 if (caps != fwnode) 8492 fwnode_handle_put(caps); 8493 put_capabilities: 8494 fwnode_handle_put(capabilities); 8495 return ret; 8496 } 8497 8498 static int tcpm_fw_get_snk_vdos(struct tcpm_port *port, struct fwnode_handle *fwnode) 8499 { 8500 int ret; 8501 8502 /* sink-vdos is optional */ 8503 ret = fwnode_property_count_u32(fwnode, "sink-vdos"); 8504 if (ret < 0) 8505 return 0; 8506 8507 port->nr_snk_vdo = min(ret, VDO_MAX_OBJECTS); 8508 if (port->nr_snk_vdo) { 8509 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos", 8510 port->snk_vdo, 8511 port->nr_snk_vdo); 8512 if (ret < 0) 8513 return ret; 8514 } 8515 8516 /* If sink-vdos is found, sink-vdos-v1 is expected for backward compatibility. */ 8517 if (port->nr_snk_vdo) { 8518 ret = fwnode_property_count_u32(fwnode, "sink-vdos-v1"); 8519 if (ret < 0) 8520 return ret; 8521 else if (ret == 0) 8522 return -ENODATA; 8523 8524 port->nr_snk_vdo_v1 = min(ret, VDO_MAX_OBJECTS); 8525 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos-v1", 8526 port->snk_vdo_v1, 8527 port->nr_snk_vdo_v1); 8528 if (ret < 0) 8529 return ret; 8530 } 8531 8532 tcpm_fw_get_pd_ident(port); 8533 8534 return 0; 8535 } 8536 8537 static void tcpm_fw_get_pd_revision(struct tcpm_port *port, struct fwnode_handle *fwnode) 8538 { 8539 int ret; 8540 u8 val[4]; 8541 8542 ret = fwnode_property_count_u8(fwnode, "pd-revision"); 8543 if (!ret || ret != 4) { 8544 tcpm_log(port, "Unable to find pd-revision property or incorrect array size"); 8545 return; 8546 } 8547 8548 ret = fwnode_property_read_u8_array(fwnode, "pd-revision", val, 4); 8549 if (ret) { 8550 tcpm_log(port, "Failed to parse pd-revision, ret:(%d)", ret); 8551 return; 8552 } 8553 8554 port->pd_rev.rev_major = val[0]; 8555 port->pd_rev.rev_minor = val[1]; 8556 port->pd_rev.ver_major = val[2]; 8557 port->pd_rev.ver_minor = val[3]; 8558 } 8559 8560 /* Power Supply access to expose source power information */ 8561 enum tcpm_psy_online_states { 8562 TCPM_PSY_OFFLINE = 0, 8563 TCPM_PSY_FIXED_ONLINE, 8564 TCPM_PSY_PPS_ONLINE, 8565 TCPM_PSY_SPR_AVS_ONLINE, 8566 }; 8567 8568 static enum power_supply_property tcpm_psy_props[] = { 8569 POWER_SUPPLY_PROP_USB_TYPE, 8570 POWER_SUPPLY_PROP_ONLINE, 8571 POWER_SUPPLY_PROP_VOLTAGE_MIN, 8572 POWER_SUPPLY_PROP_VOLTAGE_MAX, 8573 POWER_SUPPLY_PROP_VOLTAGE_NOW, 8574 POWER_SUPPLY_PROP_CURRENT_MAX, 8575 POWER_SUPPLY_PROP_CURRENT_NOW, 8576 }; 8577 8578 static int tcpm_psy_get_online(struct tcpm_port *port, 8579 union power_supply_propval *val) 8580 { 8581 if (port->vbus_charge) { 8582 if (port->pps_data.active) 8583 val->intval = TCPM_PSY_PPS_ONLINE; 8584 else if (port->spr_avs_data.active) 8585 val->intval = TCPM_PSY_SPR_AVS_ONLINE; 8586 else 8587 val->intval = TCPM_PSY_FIXED_ONLINE; 8588 } else { 8589 val->intval = TCPM_PSY_OFFLINE; 8590 } 8591 8592 return 0; 8593 } 8594 8595 static int tcpm_psy_get_voltage_min(struct tcpm_port *port, 8596 union power_supply_propval *val) 8597 { 8598 if (port->pps_data.active) 8599 val->intval = port->pps_data.min_volt * 1000; 8600 else if (port->spr_avs_data.active) 8601 val->intval = SPR_AVS_TIER1_MIN_VOLT_MV * 1000; 8602 else 8603 val->intval = port->supply_voltage * 1000; 8604 8605 return 0; 8606 } 8607 8608 static int tcpm_psy_get_voltage_max(struct tcpm_port *port, 8609 union power_supply_propval *val) 8610 { 8611 if (port->pps_data.active) 8612 val->intval = port->pps_data.max_volt * 1000; 8613 else if (port->spr_avs_data.active) 8614 val->intval = port->spr_avs_data.max_out_volt_mv * 1000; 8615 else 8616 val->intval = port->supply_voltage * 1000; 8617 8618 return 0; 8619 } 8620 8621 static int tcpm_psy_get_voltage_now(struct tcpm_port *port, 8622 union power_supply_propval *val) 8623 { 8624 val->intval = port->supply_voltage * 1000; 8625 8626 return 0; 8627 } 8628 8629 static int tcpm_psy_get_current_max(struct tcpm_port *port, 8630 union power_supply_propval *val) 8631 { 8632 if (port->pps_data.active) 8633 val->intval = port->pps_data.max_curr * 1000; 8634 else if (port->spr_avs_data.active) 8635 val->intval = port->spr_avs_data.max_current_ma * 1000; 8636 else 8637 val->intval = port->current_limit * 1000; 8638 8639 return 0; 8640 } 8641 8642 static int tcpm_psy_get_current_now(struct tcpm_port *port, 8643 union power_supply_propval *val) 8644 { 8645 val->intval = port->current_limit * 1000; 8646 8647 return 0; 8648 } 8649 8650 static int tcpm_psy_get_input_power_limit(struct tcpm_port *port, 8651 union power_supply_propval *val) 8652 { 8653 unsigned int src_mv, src_ma, max_src_uw = 0; 8654 unsigned int i, tmp; 8655 8656 for (i = 0; i < port->nr_source_caps; i++) { 8657 u32 pdo = port->source_caps[i]; 8658 8659 if (pdo_type(pdo) == PDO_TYPE_FIXED) { 8660 src_mv = pdo_fixed_voltage(pdo); 8661 src_ma = pdo_max_current(pdo); 8662 tmp = src_mv * src_ma; 8663 max_src_uw = max(tmp, max_src_uw); 8664 } 8665 } 8666 8667 val->intval = max_src_uw; 8668 return 0; 8669 } 8670 8671 static int tcpm_psy_get_prop(struct power_supply *psy, 8672 enum power_supply_property psp, 8673 union power_supply_propval *val) 8674 { 8675 struct tcpm_port *port = power_supply_get_drvdata(psy); 8676 int ret = 0; 8677 8678 switch (psp) { 8679 case POWER_SUPPLY_PROP_USB_TYPE: 8680 val->intval = port->usb_type; 8681 break; 8682 case POWER_SUPPLY_PROP_ONLINE: 8683 ret = tcpm_psy_get_online(port, val); 8684 break; 8685 case POWER_SUPPLY_PROP_VOLTAGE_MIN: 8686 ret = tcpm_psy_get_voltage_min(port, val); 8687 break; 8688 case POWER_SUPPLY_PROP_VOLTAGE_MAX: 8689 ret = tcpm_psy_get_voltage_max(port, val); 8690 break; 8691 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 8692 ret = tcpm_psy_get_voltage_now(port, val); 8693 break; 8694 case POWER_SUPPLY_PROP_CURRENT_MAX: 8695 ret = tcpm_psy_get_current_max(port, val); 8696 break; 8697 case POWER_SUPPLY_PROP_CURRENT_NOW: 8698 ret = tcpm_psy_get_current_now(port, val); 8699 break; 8700 case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT: 8701 tcpm_psy_get_input_power_limit(port, val); 8702 break; 8703 default: 8704 ret = -EINVAL; 8705 break; 8706 } 8707 8708 return ret; 8709 } 8710 8711 static int tcpm_disable_pps_avs(struct tcpm_port *port) 8712 { 8713 int ret = 0; 8714 8715 if (port->pps_data.active) 8716 ret = tcpm_pps_activate(port, false); 8717 else if (port->spr_avs_data.active) 8718 ret = tcpm_spr_avs_activate(port, false); 8719 8720 return ret; 8721 } 8722 8723 static int tcpm_psy_set_online(struct tcpm_port *port, 8724 const union power_supply_propval *val) 8725 { 8726 int ret = 0; 8727 8728 switch (val->intval) { 8729 case TCPM_PSY_FIXED_ONLINE: 8730 ret = tcpm_disable_pps_avs(port); 8731 break; 8732 case TCPM_PSY_PPS_ONLINE: 8733 if (port->spr_avs_data.active) 8734 ret = tcpm_spr_avs_activate(port, false); 8735 if (!ret) 8736 ret = tcpm_pps_activate(port, true); 8737 break; 8738 case TCPM_PSY_SPR_AVS_ONLINE: 8739 tcpm_log(port, "request to set AVS online"); 8740 if (port->spr_avs_data.active) 8741 return 0; 8742 ret = tcpm_disable_pps_avs(port); 8743 if (ret) 8744 break; 8745 ret = tcpm_spr_avs_activate(port, true); 8746 break; 8747 default: 8748 ret = -EINVAL; 8749 break; 8750 } 8751 8752 return ret; 8753 } 8754 8755 static int tcpm_psy_set_prop(struct power_supply *psy, 8756 enum power_supply_property psp, 8757 const union power_supply_propval *val) 8758 { 8759 struct tcpm_port *port = power_supply_get_drvdata(psy); 8760 int ret; 8761 8762 /* 8763 * All the properties below are related to USB PD. The check needs to be 8764 * property specific when a non-pd related property is added. 8765 */ 8766 if (!port->pd_supported) 8767 return -EOPNOTSUPP; 8768 8769 switch (psp) { 8770 case POWER_SUPPLY_PROP_ONLINE: 8771 ret = tcpm_psy_set_online(port, val); 8772 break; 8773 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 8774 ret = tcpm_aug_set_out_volt(port, val->intval / 1000); 8775 break; 8776 case POWER_SUPPLY_PROP_CURRENT_NOW: 8777 ret = tcpm_aug_set_op_curr(port, val->intval / 1000); 8778 break; 8779 default: 8780 ret = -EINVAL; 8781 break; 8782 } 8783 power_supply_changed(port->psy); 8784 return ret; 8785 } 8786 8787 static int tcpm_psy_prop_writeable(struct power_supply *psy, 8788 enum power_supply_property psp) 8789 { 8790 switch (psp) { 8791 case POWER_SUPPLY_PROP_ONLINE: 8792 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 8793 case POWER_SUPPLY_PROP_CURRENT_NOW: 8794 return 1; 8795 default: 8796 return 0; 8797 } 8798 } 8799 8800 static const char *tcpm_psy_name_prefix = "tcpm-source-psy-"; 8801 8802 static int devm_tcpm_psy_register(struct tcpm_port *port) 8803 { 8804 struct power_supply_config psy_cfg = {}; 8805 const char *port_dev_name = dev_name(port->dev); 8806 size_t psy_name_len = strlen(tcpm_psy_name_prefix) + 8807 strlen(port_dev_name) + 1; 8808 char *psy_name; 8809 8810 psy_cfg.drv_data = port; 8811 psy_cfg.fwnode = dev_fwnode(port->dev); 8812 psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL); 8813 if (!psy_name) 8814 return -ENOMEM; 8815 8816 snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix, 8817 port_dev_name); 8818 port->psy_desc.name = psy_name; 8819 port->psy_desc.type = POWER_SUPPLY_TYPE_USB; 8820 port->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_C) | 8821 BIT(POWER_SUPPLY_USB_TYPE_PD) | 8822 BIT(POWER_SUPPLY_USB_TYPE_PD_PPS) | 8823 BIT(POWER_SUPPLY_USB_TYPE_PD_PPS_SPR_AVS) | 8824 BIT(POWER_SUPPLY_USB_TYPE_PD_SPR_AVS); 8825 port->psy_desc.properties = tcpm_psy_props; 8826 port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props); 8827 port->psy_desc.get_property = tcpm_psy_get_prop; 8828 port->psy_desc.set_property = tcpm_psy_set_prop; 8829 port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable; 8830 8831 port->usb_type = POWER_SUPPLY_USB_TYPE_C; 8832 8833 port->psy = devm_power_supply_register(port->dev, &port->psy_desc, 8834 &psy_cfg); 8835 8836 return PTR_ERR_OR_ZERO(port->psy); 8837 } 8838 8839 static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer) 8840 { 8841 struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer); 8842 8843 if (port->registered) 8844 kthread_queue_work(port->wq, &port->state_machine); 8845 return HRTIMER_NORESTART; 8846 } 8847 8848 static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *timer) 8849 { 8850 struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer); 8851 8852 if (port->registered) 8853 kthread_queue_work(port->wq, &port->vdm_state_machine); 8854 return HRTIMER_NORESTART; 8855 } 8856 8857 static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer) 8858 { 8859 struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer); 8860 8861 if (port->registered) 8862 kthread_queue_work(port->wq, &port->enable_frs); 8863 return HRTIMER_NORESTART; 8864 } 8865 8866 static enum hrtimer_restart vdm_discovery_timer_handler(struct hrtimer *timer) 8867 { 8868 struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_discovery_timer); 8869 8870 if (port->registered) 8871 kthread_queue_work(port->wq, &port->vdm_discovery_work); 8872 return HRTIMER_NORESTART; 8873 } 8874 8875 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) 8876 { 8877 struct tcpm_port *port; 8878 int err; 8879 8880 if (!dev || !tcpc || 8881 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || 8882 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus || 8883 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit) 8884 return ERR_PTR(-EINVAL); 8885 8886 port = devm_kzalloc_obj(dev, *port); 8887 if (!port) 8888 return ERR_PTR(-ENOMEM); 8889 8890 port->dev = dev; 8891 port->tcpc = tcpc; 8892 8893 mutex_init(&port->lock); 8894 mutex_init(&port->swap_lock); 8895 8896 port->wq = kthread_run_worker(0, dev_name(dev)); 8897 if (IS_ERR(port->wq)) 8898 return ERR_CAST(port->wq); 8899 sched_set_fifo(port->wq->task); 8900 8901 kthread_init_work(&port->state_machine, tcpm_state_machine_work); 8902 kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work); 8903 kthread_init_work(&port->event_work, tcpm_pd_event_handler); 8904 kthread_init_work(&port->enable_frs, tcpm_enable_frs_work); 8905 kthread_init_work(&port->vdm_discovery_work, tcpm_vdm_discovery_work); 8906 hrtimer_setup(&port->state_machine_timer, state_machine_timer_handler, CLOCK_MONOTONIC, 8907 HRTIMER_MODE_REL); 8908 hrtimer_setup(&port->vdm_state_machine_timer, vdm_state_machine_timer_handler, 8909 CLOCK_MONOTONIC, HRTIMER_MODE_REL); 8910 hrtimer_setup(&port->enable_frs_timer, enable_frs_timer_handler, CLOCK_MONOTONIC, 8911 HRTIMER_MODE_REL); 8912 hrtimer_setup(&port->vdm_discovery_timer, vdm_discovery_timer_handler, CLOCK_MONOTONIC, 8913 HRTIMER_MODE_REL); 8914 8915 spin_lock_init(&port->pd_event_lock); 8916 8917 init_completion(&port->tx_complete); 8918 init_completion(&port->swap_complete); 8919 init_completion(&port->aug_supply_req_complete); 8920 tcpm_debugfs_init(port); 8921 8922 err = tcpm_fw_get_caps(port, tcpc->fwnode); 8923 if (err < 0) 8924 goto out_destroy_wq; 8925 err = tcpm_fw_get_snk_vdos(port, tcpc->fwnode); 8926 if (err < 0) 8927 goto out_destroy_wq; 8928 8929 tcpm_fw_get_timings(port, tcpc->fwnode); 8930 tcpm_fw_get_pd_revision(port, tcpc->fwnode); 8931 8932 port->try_role = port->typec_caps.prefer_role; 8933 8934 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */ 8935 8936 if (port->pd_rev.rev_major) 8937 port->typec_caps.pd_revision = port->pd_rev.rev_major << 8 | 8938 port->pd_rev.rev_minor; 8939 else 8940 port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */ 8941 8942 port->typec_caps.svdm_version = SVDM_VER_2_0; 8943 port->typec_caps.driver_data = port; 8944 port->typec_caps.ops = &tcpm_ops; 8945 port->typec_caps.orientation_aware = 1; 8946 8947 port->partner_desc.identity = &port->partner_ident; 8948 8949 port->role_sw = fwnode_usb_role_switch_get(tcpc->fwnode); 8950 if (!port->role_sw) 8951 port->role_sw = usb_role_switch_get(port->dev); 8952 if (IS_ERR(port->role_sw)) { 8953 err = PTR_ERR(port->role_sw); 8954 goto out_destroy_wq; 8955 } 8956 8957 err = devm_tcpm_psy_register(port); 8958 if (err) 8959 goto out_role_sw_put; 8960 power_supply_changed(port->psy); 8961 8962 err = tcpm_port_register_pd(port); 8963 if (err) 8964 goto out_role_sw_put; 8965 8966 if (port->pds) 8967 port->typec_caps.pd = port->pds[0]; 8968 8969 port->typec_port = typec_register_port(port->dev, &port->typec_caps); 8970 if (IS_ERR(port->typec_port)) { 8971 err = PTR_ERR(port->typec_port); 8972 goto out_unregister_pd; 8973 } 8974 8975 typec_port_register_altmodes(port->typec_port, 8976 &tcpm_altmode_ops, port, 8977 port->port_altmode, ALTMODE_DISCOVERY_MAX); 8978 typec_port_register_cable_ops(port->port_altmode, ARRAY_SIZE(port->port_altmode), 8979 &tcpm_cable_ops); 8980 port->registered = true; 8981 8982 mutex_lock(&port->lock); 8983 tcpm_init(port); 8984 mutex_unlock(&port->lock); 8985 8986 tcpm_log(port, "%s: registered", dev_name(dev)); 8987 return port; 8988 8989 out_unregister_pd: 8990 tcpm_port_unregister_pd(port); 8991 out_role_sw_put: 8992 usb_role_switch_put(port->role_sw); 8993 out_destroy_wq: 8994 tcpm_debugfs_exit(port); 8995 kthread_destroy_worker(port->wq); 8996 return ERR_PTR(err); 8997 } 8998 EXPORT_SYMBOL_GPL(tcpm_register_port); 8999 9000 void tcpm_unregister_port(struct tcpm_port *port) 9001 { 9002 int i; 9003 9004 port->registered = false; 9005 kthread_destroy_worker(port->wq); 9006 port->wq = NULL; 9007 9008 hrtimer_cancel(&port->vdm_discovery_timer); 9009 hrtimer_cancel(&port->enable_frs_timer); 9010 hrtimer_cancel(&port->vdm_state_machine_timer); 9011 hrtimer_cancel(&port->state_machine_timer); 9012 9013 power_supply_put_system_batteries(port->fixed_batt, port->fixed_batt_cnt); 9014 tcpm_reset_port(port); 9015 9016 tcpm_port_unregister_pd(port); 9017 9018 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++) 9019 typec_unregister_altmode(port->port_altmode[i]); 9020 typec_unregister_port(port->typec_port); 9021 usb_role_switch_put(port->role_sw); 9022 tcpm_debugfs_exit(port); 9023 } 9024 EXPORT_SYMBOL_GPL(tcpm_unregister_port); 9025 9026 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>"); 9027 MODULE_DESCRIPTION("USB Type-C Port Manager"); 9028 MODULE_LICENSE("GPL"); 9029