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