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