| .. | .. |
|---|
| 8 | 8 | #include <linux/completion.h> |
|---|
| 9 | 9 | #include <linux/debugfs.h> |
|---|
| 10 | 10 | #include <linux/device.h> |
|---|
| 11 | | -#include <linux/extcon-provider.h> |
|---|
| 12 | 11 | #include <linux/hrtimer.h> |
|---|
| 13 | 12 | #include <linux/jiffies.h> |
|---|
| 14 | 13 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 18 | 17 | #include <linux/power_supply.h> |
|---|
| 19 | 18 | #include <linux/proc_fs.h> |
|---|
| 20 | 19 | #include <linux/property.h> |
|---|
| 21 | | -#include <linux/regulator/consumer.h> |
|---|
| 22 | 20 | #include <linux/sched/clock.h> |
|---|
| 23 | 21 | #include <linux/seq_file.h> |
|---|
| 24 | 22 | #include <linux/slab.h> |
|---|
| 25 | 23 | #include <linux/spinlock.h> |
|---|
| 24 | +#include <linux/usb.h> |
|---|
| 26 | 25 | #include <linux/usb/pd.h> |
|---|
| 27 | 26 | #include <linux/usb/pd_ado.h> |
|---|
| 28 | 27 | #include <linux/usb/pd_bdo.h> |
|---|
| .. | .. |
|---|
| 31 | 30 | #include <linux/usb/role.h> |
|---|
| 32 | 31 | #include <linux/usb/tcpm.h> |
|---|
| 33 | 32 | #include <linux/usb/typec_altmode.h> |
|---|
| 34 | | -#include <linux/usb/typec_dp.h> |
|---|
| 35 | 33 | |
|---|
| 34 | +#include <trace/hooks/typec.h> |
|---|
| 36 | 35 | #include <uapi/linux/sched/types.h> |
|---|
| 37 | 36 | |
|---|
| 38 | 37 | #define FOREACH_STATE(S) \ |
|---|
| .. | .. |
|---|
| 145 | 144 | S(PORT_RESET), \ |
|---|
| 146 | 145 | S(PORT_RESET_WAIT_OFF), \ |
|---|
| 147 | 146 | \ |
|---|
| 148 | | - S(AMS_START) |
|---|
| 147 | + S(AMS_START), \ |
|---|
| 148 | + S(CHUNK_NOT_SUPP) |
|---|
| 149 | 149 | |
|---|
| 150 | 150 | #define FOREACH_AMS(S) \ |
|---|
| 151 | 151 | S(NONE_AMS), \ |
|---|
| .. | .. |
|---|
| 223 | 223 | PD_MSG_DATA_SOURCE_CAP, |
|---|
| 224 | 224 | }; |
|---|
| 225 | 225 | |
|---|
| 226 | +enum adev_actions { |
|---|
| 227 | + ADEV_NONE = 0, |
|---|
| 228 | + ADEV_NOTIFY_USB_AND_QUEUE_VDM, |
|---|
| 229 | + ADEV_QUEUE_VDM, |
|---|
| 230 | + ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL, |
|---|
| 231 | + ADEV_ATTENTION, |
|---|
| 232 | +}; |
|---|
| 233 | + |
|---|
| 226 | 234 | /* |
|---|
| 227 | 235 | * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap. |
|---|
| 228 | 236 | * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0, |
|---|
| .. | .. |
|---|
| 234 | 242 | FRS_5V_1P5A, |
|---|
| 235 | 243 | FRS_5V_3A, |
|---|
| 236 | 244 | }; |
|---|
| 237 | | - |
|---|
| 238 | | -static const unsigned int tcpm_cable[] = { |
|---|
| 239 | | - EXTCON_USB, |
|---|
| 240 | | - EXTCON_USB_HOST, |
|---|
| 241 | | - EXTCON_USB_VBUS_EN, |
|---|
| 242 | | - EXTCON_CHG_USB_SDP, |
|---|
| 243 | | - EXTCON_CHG_USB_CDP, |
|---|
| 244 | | - EXTCON_CHG_USB_DCP, |
|---|
| 245 | | - EXTCON_CHG_USB_SLOW, |
|---|
| 246 | | - EXTCON_CHG_USB_FAST, |
|---|
| 247 | | - EXTCON_DISP_DP, |
|---|
| 248 | | - EXTCON_NONE, |
|---|
| 249 | | -}; |
|---|
| 250 | | - |
|---|
| 251 | | -/* Pin assignments where one channel is for USB */ |
|---|
| 252 | | -#define DP_PIN_ASSIGN_MULTI_FUNCTION_MASK (BIT(DP_PIN_ASSIGN_B) | \ |
|---|
| 253 | | - BIT(DP_PIN_ASSIGN_D) | \ |
|---|
| 254 | | - BIT(DP_PIN_ASSIGN_F)) |
|---|
| 255 | 245 | |
|---|
| 256 | 246 | /* Events from low level driver */ |
|---|
| 257 | 247 | |
|---|
| .. | .. |
|---|
| 270 | 260 | #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX) |
|---|
| 271 | 261 | |
|---|
| 272 | 262 | #define GET_SINK_CAP_RETRY_MS 100 |
|---|
| 273 | | -#define SEND_NEW_MODE_NOTIFY_MS 20 |
|---|
| 263 | +#define SEND_DISCOVER_RETRY_MS 100 |
|---|
| 274 | 264 | |
|---|
| 275 | 265 | struct pd_mode_data { |
|---|
| 276 | 266 | int svid_index; /* current SVID index */ |
|---|
| .. | .. |
|---|
| 280 | 270 | struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX]; |
|---|
| 281 | 271 | }; |
|---|
| 282 | 272 | |
|---|
| 273 | +/* |
|---|
| 274 | + * @min_volt: Actual min voltage at the local port |
|---|
| 275 | + * @req_min_volt: Requested min voltage to the port partner |
|---|
| 276 | + * @max_volt: Actual max voltage at the local port |
|---|
| 277 | + * @req_max_volt: Requested max voltage to the port partner |
|---|
| 278 | + * @max_curr: Actual max current at the local port |
|---|
| 279 | + * @req_max_curr: Requested max current of the port partner |
|---|
| 280 | + * @req_out_volt: Requested output voltage to the port partner |
|---|
| 281 | + * @req_op_curr: Requested operating current to the port partner |
|---|
| 282 | + * @supported: Parter has atleast one APDO hence supports PPS |
|---|
| 283 | + * @active: PPS mode is active |
|---|
| 284 | + */ |
|---|
| 283 | 285 | struct pd_pps_data { |
|---|
| 284 | 286 | u32 min_volt; |
|---|
| 287 | + u32 req_min_volt; |
|---|
| 285 | 288 | u32 max_volt; |
|---|
| 289 | + u32 req_max_volt; |
|---|
| 286 | 290 | u32 max_curr; |
|---|
| 287 | | - u32 out_volt; |
|---|
| 288 | | - u32 op_curr; |
|---|
| 291 | + u32 req_max_curr; |
|---|
| 292 | + u32 req_out_volt; |
|---|
| 293 | + u32 req_op_curr; |
|---|
| 289 | 294 | bool supported; |
|---|
| 290 | 295 | bool active; |
|---|
| 291 | 296 | }; |
|---|
| .. | .. |
|---|
| 311 | 316 | struct typec_partner_desc partner_desc; |
|---|
| 312 | 317 | struct typec_partner *partner; |
|---|
| 313 | 318 | |
|---|
| 314 | | - struct regulator *vbus; |
|---|
| 315 | | - struct extcon_dev *extcon; |
|---|
| 316 | | - |
|---|
| 317 | 319 | enum typec_cc_status cc_req; |
|---|
| 318 | 320 | |
|---|
| 319 | 321 | enum typec_cc_status cc1; |
|---|
| .. | .. |
|---|
| 323 | 325 | bool attached; |
|---|
| 324 | 326 | bool connected; |
|---|
| 325 | 327 | enum typec_port_type port_type; |
|---|
| 328 | + |
|---|
| 329 | + /* |
|---|
| 330 | + * Set to true when vbus is greater than VSAFE5V min. |
|---|
| 331 | + * Set to false when vbus falls below vSinkDisconnect max threshold. |
|---|
| 332 | + */ |
|---|
| 326 | 333 | bool vbus_present; |
|---|
| 334 | + |
|---|
| 335 | + /* |
|---|
| 336 | + * Set to true when vbus is less than VSAFE0V max. |
|---|
| 337 | + * Set to false when vbus is greater than VSAFE0V max. |
|---|
| 338 | + */ |
|---|
| 339 | + bool vbus_vsafe0v; |
|---|
| 340 | + |
|---|
| 327 | 341 | bool vbus_never_low; |
|---|
| 328 | 342 | bool vbus_source; |
|---|
| 329 | 343 | bool vbus_charge; |
|---|
| 330 | 344 | |
|---|
| 345 | + /* Set to true when Discover_Identity Command is expected to be sent in Ready states. */ |
|---|
| 331 | 346 | bool send_discover; |
|---|
| 332 | 347 | bool op_vsafe5v; |
|---|
| 333 | | - bool vbus_on; |
|---|
| 334 | 348 | |
|---|
| 335 | 349 | int try_role; |
|---|
| 336 | 350 | int try_snk_count; |
|---|
| .. | .. |
|---|
| 346 | 360 | unsigned long delay_ms; |
|---|
| 347 | 361 | |
|---|
| 348 | 362 | spinlock_t pd_event_lock; |
|---|
| 363 | +#ifdef CONFIG_NO_GKI |
|---|
| 349 | 364 | struct mutex pd_handler_lock; |
|---|
| 365 | +#endif |
|---|
| 350 | 366 | u32 pd_events; |
|---|
| 351 | 367 | |
|---|
| 352 | 368 | struct kthread_work event_work; |
|---|
| .. | .. |
|---|
| 356 | 372 | struct kthread_work vdm_state_machine; |
|---|
| 357 | 373 | struct hrtimer enable_frs_timer; |
|---|
| 358 | 374 | struct kthread_work enable_frs; |
|---|
| 359 | | - struct kthread_work data_role_swap; |
|---|
| 360 | | - struct hrtimer data_role_swap_timer; |
|---|
| 375 | + struct hrtimer send_discover_timer; |
|---|
| 376 | + struct kthread_work send_discover_work; |
|---|
| 361 | 377 | bool state_machine_running; |
|---|
| 378 | + /* Set to true when VDM State Machine has following actions. */ |
|---|
| 362 | 379 | bool vdm_sm_running; |
|---|
| 363 | 380 | |
|---|
| 364 | 381 | struct completion tx_complete; |
|---|
| .. | .. |
|---|
| 378 | 395 | bool explicit_contract; |
|---|
| 379 | 396 | unsigned int rx_msgid; |
|---|
| 380 | 397 | |
|---|
| 381 | | - u32 dp_pin_assignment; |
|---|
| 382 | | - u32 dp_status; |
|---|
| 383 | | - bool dp_configured; |
|---|
| 384 | | - |
|---|
| 385 | 398 | /* Partner capabilities/requests */ |
|---|
| 386 | 399 | u32 sink_request; |
|---|
| 387 | 400 | u32 source_caps[PDO_MAX_OBJECTS]; |
|---|
| .. | .. |
|---|
| 394 | 407 | unsigned int nr_src_pdo; |
|---|
| 395 | 408 | u32 snk_pdo[PDO_MAX_OBJECTS]; |
|---|
| 396 | 409 | unsigned int nr_snk_pdo; |
|---|
| 410 | + u32 snk_vdo_v1[VDO_MAX_OBJECTS]; |
|---|
| 411 | + unsigned int nr_snk_vdo_v1; |
|---|
| 397 | 412 | u32 snk_vdo[VDO_MAX_OBJECTS]; |
|---|
| 398 | 413 | unsigned int nr_snk_vdo; |
|---|
| 399 | 414 | |
|---|
| 400 | 415 | unsigned int operating_snk_mw; |
|---|
| 401 | 416 | bool update_sink_caps; |
|---|
| 402 | 417 | |
|---|
| 403 | | - /* Requested current / voltage */ |
|---|
| 418 | + /* Requested current / voltage to the port partner */ |
|---|
| 419 | + u32 req_current_limit; |
|---|
| 420 | + u32 req_supply_voltage; |
|---|
| 421 | + /* Actual current / voltage limit of the local port */ |
|---|
| 404 | 422 | u32 current_limit; |
|---|
| 405 | 423 | u32 supply_voltage; |
|---|
| 406 | 424 | |
|---|
| .. | .. |
|---|
| 437 | 455 | /* port belongs to a self powered device */ |
|---|
| 438 | 456 | bool self_powered; |
|---|
| 439 | 457 | |
|---|
| 440 | | - /* FRS */ |
|---|
| 441 | | - enum frs_typec_current frs_current; |
|---|
| 458 | + /* Sink FRS */ |
|---|
| 459 | + enum frs_typec_current new_source_frs_current; |
|---|
| 442 | 460 | |
|---|
| 443 | 461 | /* Sink caps have been queried */ |
|---|
| 444 | 462 | bool sink_cap_done; |
|---|
| 445 | 463 | |
|---|
| 464 | + /* Port is still in tCCDebounce */ |
|---|
| 465 | + bool debouncing; |
|---|
| 466 | + |
|---|
| 446 | 467 | /* Collision Avoidance and Atomic Message Sequence */ |
|---|
| 447 | 468 | enum tcpm_state upcoming_state; |
|---|
| 448 | 469 | enum tcpm_ams ams; |
|---|
| 470 | + enum tcpm_ams next_ams; |
|---|
| 449 | 471 | bool in_ams; |
|---|
| 450 | 472 | |
|---|
| 473 | + /* Auto vbus discharge status */ |
|---|
| 474 | + bool auto_vbus_discharge_enabled; |
|---|
| 475 | + |
|---|
| 476 | + /* |
|---|
| 477 | + * When set, port requests PD_P_SNK_STDBY_MW upon entering SNK_DISCOVERY and |
|---|
| 478 | + * the actual currrent limit after RX of PD_CTRL_PSRDY for PD link, |
|---|
| 479 | + * SNK_READY for non-pd link. |
|---|
| 480 | + */ |
|---|
| 481 | + bool slow_charger_loop; |
|---|
| 451 | 482 | #ifdef CONFIG_DEBUG_FS |
|---|
| 452 | 483 | struct dentry *dentry; |
|---|
| 453 | 484 | struct mutex logbuffer_lock; /* log buffer access lock */ |
|---|
| .. | .. |
|---|
| 505 | 536 | ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \ |
|---|
| 506 | 537 | (port)->port_type == TYPEC_PORT_DRP) |
|---|
| 507 | 538 | |
|---|
| 539 | +#define tcpm_data_role_for_source(port) \ |
|---|
| 540 | + ((port)->typec_caps.data == TYPEC_PORT_UFP ? \ |
|---|
| 541 | + TYPEC_DEVICE : TYPEC_HOST) |
|---|
| 542 | + |
|---|
| 543 | +#define tcpm_data_role_for_sink(port) \ |
|---|
| 544 | + ((port)->typec_caps.data == TYPEC_PORT_DFP ? \ |
|---|
| 545 | + TYPEC_HOST : TYPEC_DEVICE) |
|---|
| 546 | + |
|---|
| 508 | 547 | #define tcpm_sink_tx_ok(port) \ |
|---|
| 509 | 548 | (tcpm_port_is_sink(port) && \ |
|---|
| 510 | 549 | ((port)->cc1 == TYPEC_CC_RP_3_0 || (port)->cc2 == TYPEC_CC_RP_3_0)) |
|---|
| 550 | + |
|---|
| 551 | +#define tcpm_wait_for_discharge(port) \ |
|---|
| 552 | + (((port)->auto_vbus_discharge_enabled && !(port)->vbus_vsafe0v) ? PD_T_SAFE_0V : 0) |
|---|
| 511 | 553 | |
|---|
| 512 | 554 | static enum tcpm_state tcpm_default_state(struct tcpm_port *port) |
|---|
| 513 | 555 | { |
|---|
| .. | .. |
|---|
| 521 | 563 | return SNK_UNATTACHED; |
|---|
| 522 | 564 | } |
|---|
| 523 | 565 | return SRC_UNATTACHED; |
|---|
| 524 | | -} |
|---|
| 525 | | - |
|---|
| 526 | | -static inline |
|---|
| 527 | | -struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap) |
|---|
| 528 | | -{ |
|---|
| 529 | | - return container_of(cap, struct tcpm_port, typec_caps); |
|---|
| 530 | 566 | } |
|---|
| 531 | 567 | |
|---|
| 532 | 568 | static bool tcpm_port_is_disconnected(struct tcpm_port *port) |
|---|
| .. | .. |
|---|
| 557 | 593 | char tmpbuffer[LOG_BUFFER_ENTRY_SIZE]; |
|---|
| 558 | 594 | u64 ts_nsec = local_clock(); |
|---|
| 559 | 595 | unsigned long rem_nsec; |
|---|
| 596 | + bool bypass_log = false; |
|---|
| 560 | 597 | |
|---|
| 561 | 598 | mutex_lock(&port->logbuffer_lock); |
|---|
| 562 | 599 | if (!port->logbuffer[port->logbuffer_head]) { |
|---|
| .. | .. |
|---|
| 569 | 606 | } |
|---|
| 570 | 607 | |
|---|
| 571 | 608 | vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args); |
|---|
| 609 | + trace_android_vh_typec_tcpm_log(tmpbuffer, &bypass_log); |
|---|
| 610 | + if (bypass_log) |
|---|
| 611 | + goto abort; |
|---|
| 572 | 612 | |
|---|
| 573 | 613 | if (tcpm_log_full(port)) { |
|---|
| 574 | 614 | port->logbuffer_head = max(port->logbuffer_head - 1, 0); |
|---|
| .. | .. |
|---|
| 705 | 745 | } |
|---|
| 706 | 746 | DEFINE_SHOW_ATTRIBUTE(tcpm_debug); |
|---|
| 707 | 747 | |
|---|
| 708 | | -static struct dentry *rootdir; |
|---|
| 709 | | - |
|---|
| 710 | 748 | static void tcpm_debugfs_init(struct tcpm_port *port) |
|---|
| 711 | 749 | { |
|---|
| 712 | | - mutex_init(&port->logbuffer_lock); |
|---|
| 713 | | - /* /sys/kernel/debug/tcpm/usbcX */ |
|---|
| 714 | | - if (!rootdir) |
|---|
| 715 | | - rootdir = debugfs_create_dir("tcpm", NULL); |
|---|
| 750 | + char name[NAME_MAX]; |
|---|
| 716 | 751 | |
|---|
| 717 | | - port->dentry = debugfs_create_file(dev_name(port->dev), |
|---|
| 718 | | - S_IFREG | 0444, rootdir, |
|---|
| 752 | + mutex_init(&port->logbuffer_lock); |
|---|
| 753 | + snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev)); |
|---|
| 754 | + port->dentry = debugfs_create_file(name, S_IFREG | 0444, usb_debug_root, |
|---|
| 719 | 755 | port, &tcpm_debug_fops); |
|---|
| 720 | 756 | } |
|---|
| 721 | 757 | |
|---|
| .. | .. |
|---|
| 731 | 767 | mutex_unlock(&port->logbuffer_lock); |
|---|
| 732 | 768 | |
|---|
| 733 | 769 | debugfs_remove(port->dentry); |
|---|
| 734 | | - if (list_empty(&rootdir->d_subdirs)) { |
|---|
| 735 | | - debugfs_remove(rootdir); |
|---|
| 736 | | - rootdir = NULL; |
|---|
| 737 | | - } |
|---|
| 738 | 770 | } |
|---|
| 739 | 771 | |
|---|
| 740 | 772 | #else |
|---|
| .. | .. |
|---|
| 749 | 781 | |
|---|
| 750 | 782 | #endif |
|---|
| 751 | 783 | |
|---|
| 752 | | -static int tcpm_send_vbus_notify(struct tcpm_port *port, bool enable) |
|---|
| 753 | | -{ |
|---|
| 754 | | - int ret = 0; |
|---|
| 755 | | - |
|---|
| 756 | | - if (port->vbus_on == enable) { |
|---|
| 757 | | - tcpm_log(port, "vbus is already %s", enable ? "on" : "Off"); |
|---|
| 758 | | - goto done; |
|---|
| 759 | | - } |
|---|
| 760 | | - |
|---|
| 761 | | - if (port->vbus) { |
|---|
| 762 | | - if (enable) |
|---|
| 763 | | - ret = regulator_enable(port->vbus); |
|---|
| 764 | | - else |
|---|
| 765 | | - ret = regulator_disable(port->vbus); |
|---|
| 766 | | - if (ret < 0) { |
|---|
| 767 | | - dev_err(port->dev, "failed to %s vbus supply(%d)\n", |
|---|
| 768 | | - enable ? "enable" : "disable", ret); |
|---|
| 769 | | - goto done; |
|---|
| 770 | | - } |
|---|
| 771 | | - |
|---|
| 772 | | - /* Only set state here, don't sync notifier to PMIC */ |
|---|
| 773 | | - extcon_set_state(port->extcon, EXTCON_USB_VBUS_EN, enable); |
|---|
| 774 | | - } else { |
|---|
| 775 | | - extcon_set_state(port->extcon, EXTCON_USB_VBUS_EN, enable); |
|---|
| 776 | | - extcon_sync(port->extcon, EXTCON_USB_VBUS_EN); |
|---|
| 777 | | - tcpm_log(port, "tcpm driver send extcon to %s vbus 5v\n", |
|---|
| 778 | | - enable ? "enable" : "disable"); |
|---|
| 779 | | - } |
|---|
| 780 | | - |
|---|
| 781 | | - port->vbus_on = enable; |
|---|
| 782 | | - |
|---|
| 783 | | -done: |
|---|
| 784 | | - return ret; |
|---|
| 785 | | -} |
|---|
| 786 | | - |
|---|
| 787 | | -static void tcpm_send_orientation_notify(struct tcpm_port *port) |
|---|
| 788 | | -{ |
|---|
| 789 | | - union extcon_property_value property; |
|---|
| 790 | | - |
|---|
| 791 | | - property.intval = port->polarity; |
|---|
| 792 | | - extcon_set_property(port->extcon, EXTCON_USB, |
|---|
| 793 | | - EXTCON_PROP_USB_TYPEC_POLARITY, property); |
|---|
| 794 | | - extcon_set_property(port->extcon, EXTCON_USB_HOST, |
|---|
| 795 | | - EXTCON_PROP_USB_TYPEC_POLARITY, property); |
|---|
| 796 | | - extcon_set_property(port->extcon, EXTCON_DISP_DP, |
|---|
| 797 | | - EXTCON_PROP_USB_TYPEC_POLARITY, property); |
|---|
| 798 | | -} |
|---|
| 799 | | - |
|---|
| 800 | | -static void tcpm_send_data_role_notify(struct tcpm_port *port, bool attached, |
|---|
| 801 | | - enum typec_data_role data) |
|---|
| 802 | | -{ |
|---|
| 803 | | - bool dfp = false; |
|---|
| 804 | | - bool ufp = false; |
|---|
| 805 | | - |
|---|
| 806 | | - if (attached) { |
|---|
| 807 | | - if (data == TYPEC_HOST) |
|---|
| 808 | | - dfp = true; |
|---|
| 809 | | - else |
|---|
| 810 | | - ufp = true; |
|---|
| 811 | | - } |
|---|
| 812 | | - extcon_set_state(port->extcon, EXTCON_USB, ufp); |
|---|
| 813 | | - extcon_set_state(port->extcon, EXTCON_USB_HOST, dfp); |
|---|
| 814 | | - extcon_sync(port->extcon, EXTCON_USB); |
|---|
| 815 | | - extcon_sync(port->extcon, EXTCON_USB_HOST); |
|---|
| 816 | | -} |
|---|
| 817 | | - |
|---|
| 818 | | -static void tcpm_send_dp_notify(struct tcpm_port *port) |
|---|
| 819 | | -{ |
|---|
| 820 | | - union extcon_property_value property; |
|---|
| 821 | | - bool usb_ss = false; |
|---|
| 822 | | - bool hpd = false; |
|---|
| 823 | | - |
|---|
| 824 | | - if (port->dp_configured) { |
|---|
| 825 | | - usb_ss = (port->dp_pin_assignment & |
|---|
| 826 | | - DP_PIN_ASSIGN_MULTI_FUNCTION_MASK) ? true : false; |
|---|
| 827 | | - hpd = !!(port->dp_status & DP_STATUS_HPD_STATE); |
|---|
| 828 | | - } |
|---|
| 829 | | - property.intval = usb_ss; |
|---|
| 830 | | - extcon_set_property(port->extcon, EXTCON_USB, |
|---|
| 831 | | - EXTCON_PROP_USB_SS, property); |
|---|
| 832 | | - extcon_set_property(port->extcon, EXTCON_USB_HOST, |
|---|
| 833 | | - EXTCON_PROP_USB_SS, property); |
|---|
| 834 | | - extcon_set_property(port->extcon, EXTCON_DISP_DP, |
|---|
| 835 | | - EXTCON_PROP_USB_SS, property); |
|---|
| 836 | | - extcon_set_state(port->extcon, EXTCON_DISP_DP, port->dp_configured && hpd); |
|---|
| 837 | | - extcon_sync(port->extcon, EXTCON_DISP_DP); |
|---|
| 838 | | -} |
|---|
| 839 | | - |
|---|
| 840 | | -static void tcpm_send_power_change_notify(struct tcpm_port *port) |
|---|
| 841 | | -{ |
|---|
| 842 | | - union extcon_property_value property; |
|---|
| 843 | | - |
|---|
| 844 | | - property.intval = (port->current_limit << 15 | port->supply_voltage); |
|---|
| 845 | | - extcon_set_property(port->extcon, EXTCON_CHG_USB_FAST, |
|---|
| 846 | | - EXTCON_PROP_USB_TYPEC_POLARITY, property); |
|---|
| 847 | | - extcon_set_state(port->extcon, EXTCON_CHG_USB_FAST, true); |
|---|
| 848 | | - extcon_sync(port->extcon, EXTCON_CHG_USB_FAST); |
|---|
| 849 | | -} |
|---|
| 850 | | - |
|---|
| 851 | 784 | static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc) |
|---|
| 852 | 785 | { |
|---|
| 853 | 786 | tcpm_log(port, "cc:=%d", cc); |
|---|
| 854 | 787 | port->cc_req = cc; |
|---|
| 855 | 788 | port->tcpc->set_cc(port->tcpc, cc); |
|---|
| 789 | +} |
|---|
| 790 | + |
|---|
| 791 | +static int tcpm_enable_auto_vbus_discharge(struct tcpm_port *port, bool enable) |
|---|
| 792 | +{ |
|---|
| 793 | + int ret = 0; |
|---|
| 794 | + |
|---|
| 795 | + if (port->tcpc->enable_auto_vbus_discharge) { |
|---|
| 796 | + ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, enable); |
|---|
| 797 | + tcpm_log_force(port, "%s vbus discharge ret:%d", enable ? "enable" : "disable", |
|---|
| 798 | + ret); |
|---|
| 799 | + if (!ret) |
|---|
| 800 | + port->auto_vbus_discharge_enabled = enable; |
|---|
| 801 | + } |
|---|
| 802 | + |
|---|
| 803 | + return ret; |
|---|
| 804 | +} |
|---|
| 805 | + |
|---|
| 806 | +static void tcpm_apply_rc(struct tcpm_port *port) |
|---|
| 807 | +{ |
|---|
| 808 | + /* |
|---|
| 809 | + * TCPCI: Move to APPLY_RC state to prevent disconnect during PR_SWAP |
|---|
| 810 | + * when Vbus auto discharge on disconnect is enabled. |
|---|
| 811 | + */ |
|---|
| 812 | + if (port->tcpc->enable_auto_vbus_discharge && port->tcpc->apply_rc) { |
|---|
| 813 | + tcpm_log(port, "Apply_RC"); |
|---|
| 814 | + port->tcpc->apply_rc(port->tcpc, port->cc_req, port->polarity); |
|---|
| 815 | + tcpm_enable_auto_vbus_discharge(port, false); |
|---|
| 816 | + } |
|---|
| 856 | 817 | } |
|---|
| 857 | 818 | |
|---|
| 858 | 819 | /* |
|---|
| .. | .. |
|---|
| 888 | 849 | return TYPEC_CC_RP_DEF; |
|---|
| 889 | 850 | } |
|---|
| 890 | 851 | |
|---|
| 891 | | -static int tcpm_ams_finish(struct tcpm_port *port) |
|---|
| 852 | +static void tcpm_ams_finish(struct tcpm_port *port) |
|---|
| 892 | 853 | { |
|---|
| 893 | | - int ret = 0; |
|---|
| 894 | | - |
|---|
| 895 | 854 | tcpm_log(port, "AMS %s finished", tcpm_ams_str[port->ams]); |
|---|
| 896 | 855 | |
|---|
| 897 | 856 | if (port->pd_capable && port->pwr_role == TYPEC_SOURCE) { |
|---|
| .. | .. |
|---|
| 905 | 864 | |
|---|
| 906 | 865 | port->in_ams = false; |
|---|
| 907 | 866 | port->ams = NONE_AMS; |
|---|
| 908 | | - |
|---|
| 909 | | - return ret; |
|---|
| 910 | 867 | } |
|---|
| 911 | 868 | |
|---|
| 912 | 869 | static int tcpm_pd_transmit(struct tcpm_port *port, |
|---|
| .. | .. |
|---|
| 922 | 879 | tcpm_log(port, "PD TX, type: %#x", type); |
|---|
| 923 | 880 | |
|---|
| 924 | 881 | reinit_completion(&port->tx_complete); |
|---|
| 925 | | - ret = port->tcpc->pd_transmit(port->tcpc, type, msg); |
|---|
| 882 | + ret = port->tcpc->pd_transmit(port->tcpc, type, msg, port->negotiated_rev); |
|---|
| 926 | 883 | if (ret < 0) |
|---|
| 927 | 884 | return ret; |
|---|
| 928 | 885 | |
|---|
| .. | .. |
|---|
| 1024 | 981 | return ret; |
|---|
| 1025 | 982 | } |
|---|
| 1026 | 983 | |
|---|
| 984 | +bool tcpm_is_debouncing(struct tcpm_port *port) |
|---|
| 985 | +{ |
|---|
| 986 | + bool debounce; |
|---|
| 987 | + |
|---|
| 988 | + if (!port) |
|---|
| 989 | + return false; |
|---|
| 990 | + |
|---|
| 991 | + mutex_lock(&port->lock); |
|---|
| 992 | + debounce = port->debouncing; |
|---|
| 993 | + mutex_unlock(&port->lock); |
|---|
| 994 | + |
|---|
| 995 | + return debounce; |
|---|
| 996 | +} |
|---|
| 997 | +EXPORT_SYMBOL_GPL(tcpm_is_debouncing); |
|---|
| 998 | + |
|---|
| 1027 | 999 | static u32 tcpm_get_current_limit(struct tcpm_port *port) |
|---|
| 1028 | 1000 | { |
|---|
| 1029 | 1001 | enum typec_cc_status cc; |
|---|
| .. | .. |
|---|
| 1057 | 1029 | |
|---|
| 1058 | 1030 | port->supply_voltage = mv; |
|---|
| 1059 | 1031 | port->current_limit = max_ma; |
|---|
| 1032 | + power_supply_changed(port->psy); |
|---|
| 1060 | 1033 | |
|---|
| 1061 | 1034 | if (port->tcpc->set_current_limit) |
|---|
| 1062 | 1035 | ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv); |
|---|
| .. | .. |
|---|
| 1082 | 1055 | else |
|---|
| 1083 | 1056 | orientation = TYPEC_ORIENTATION_REVERSE; |
|---|
| 1084 | 1057 | |
|---|
| 1085 | | - if (data == TYPEC_HOST) |
|---|
| 1086 | | - usb_role = USB_ROLE_HOST; |
|---|
| 1087 | | - else |
|---|
| 1088 | | - usb_role = USB_ROLE_DEVICE; |
|---|
| 1058 | + if (port->typec_caps.data == TYPEC_PORT_DRD) { |
|---|
| 1059 | + if (data == TYPEC_HOST) |
|---|
| 1060 | + usb_role = USB_ROLE_HOST; |
|---|
| 1061 | + else |
|---|
| 1062 | + usb_role = USB_ROLE_DEVICE; |
|---|
| 1063 | + } else if (port->typec_caps.data == TYPEC_PORT_DFP) { |
|---|
| 1064 | + if (data == TYPEC_HOST) { |
|---|
| 1065 | + if (role == TYPEC_SOURCE) |
|---|
| 1066 | + usb_role = USB_ROLE_HOST; |
|---|
| 1067 | + else |
|---|
| 1068 | + usb_role = USB_ROLE_NONE; |
|---|
| 1069 | + } else { |
|---|
| 1070 | + return -ENOTSUPP; |
|---|
| 1071 | + } |
|---|
| 1072 | + } else { |
|---|
| 1073 | + if (data == TYPEC_DEVICE) { |
|---|
| 1074 | + if (role == TYPEC_SINK) |
|---|
| 1075 | + usb_role = USB_ROLE_DEVICE; |
|---|
| 1076 | + else |
|---|
| 1077 | + usb_role = USB_ROLE_NONE; |
|---|
| 1078 | + } else { |
|---|
| 1079 | + return -ENOTSUPP; |
|---|
| 1080 | + } |
|---|
| 1081 | + } |
|---|
| 1089 | 1082 | |
|---|
| 1090 | 1083 | ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation); |
|---|
| 1091 | 1084 | if (ret < 0) |
|---|
| .. | .. |
|---|
| 1118 | 1111 | return 0; |
|---|
| 1119 | 1112 | } |
|---|
| 1120 | 1113 | |
|---|
| 1114 | +/* |
|---|
| 1115 | + * Transform the PDO to be compliant to PD rev2.0. |
|---|
| 1116 | + * Return 0 if the PDO type is not defined in PD rev2.0. |
|---|
| 1117 | + * Otherwise, return the converted PDO. |
|---|
| 1118 | + */ |
|---|
| 1119 | +static u32 tcpm_forge_legacy_pdo(struct tcpm_port *port, u32 pdo, enum typec_role role) |
|---|
| 1120 | +{ |
|---|
| 1121 | + switch (pdo_type(pdo)) { |
|---|
| 1122 | + case PDO_TYPE_FIXED: |
|---|
| 1123 | + if (role == TYPEC_SINK) |
|---|
| 1124 | + return pdo & ~PDO_FIXED_FRS_CURR_MASK; |
|---|
| 1125 | + else |
|---|
| 1126 | + return pdo & ~PDO_FIXED_UNCHUNK_EXT; |
|---|
| 1127 | + case PDO_TYPE_VAR: |
|---|
| 1128 | + case PDO_TYPE_BATT: |
|---|
| 1129 | + return pdo; |
|---|
| 1130 | + case PDO_TYPE_APDO: |
|---|
| 1131 | + default: |
|---|
| 1132 | + return 0; |
|---|
| 1133 | + } |
|---|
| 1134 | +} |
|---|
| 1135 | + |
|---|
| 1121 | 1136 | static int tcpm_pd_send_source_caps(struct tcpm_port *port) |
|---|
| 1122 | 1137 | { |
|---|
| 1123 | 1138 | struct pd_message msg; |
|---|
| 1124 | | - int i; |
|---|
| 1139 | + u32 pdo; |
|---|
| 1140 | + unsigned int i, nr_pdo = 0; |
|---|
| 1125 | 1141 | |
|---|
| 1126 | 1142 | memset(&msg, 0, sizeof(msg)); |
|---|
| 1127 | | - if (!port->nr_src_pdo) { |
|---|
| 1143 | + |
|---|
| 1144 | + for (i = 0; i < port->nr_src_pdo; i++) { |
|---|
| 1145 | + if (port->negotiated_rev >= PD_REV30) { |
|---|
| 1146 | + msg.payload[nr_pdo++] = cpu_to_le32(port->src_pdo[i]); |
|---|
| 1147 | + } else { |
|---|
| 1148 | + pdo = tcpm_forge_legacy_pdo(port, port->src_pdo[i], TYPEC_SOURCE); |
|---|
| 1149 | + if (pdo) |
|---|
| 1150 | + msg.payload[nr_pdo++] = cpu_to_le32(pdo); |
|---|
| 1151 | + } |
|---|
| 1152 | + } |
|---|
| 1153 | + |
|---|
| 1154 | + if (!nr_pdo) { |
|---|
| 1128 | 1155 | /* No source capabilities defined, sink only */ |
|---|
| 1129 | 1156 | msg.header = PD_HEADER_LE(PD_CTRL_REJECT, |
|---|
| 1130 | 1157 | port->pwr_role, |
|---|
| .. | .. |
|---|
| 1137 | 1164 | port->data_role, |
|---|
| 1138 | 1165 | port->negotiated_rev, |
|---|
| 1139 | 1166 | port->message_id, |
|---|
| 1140 | | - port->nr_src_pdo); |
|---|
| 1167 | + nr_pdo); |
|---|
| 1141 | 1168 | } |
|---|
| 1142 | | - for (i = 0; i < port->nr_src_pdo; i++) |
|---|
| 1143 | | - msg.payload[i] = cpu_to_le32(port->src_pdo[i]); |
|---|
| 1144 | 1169 | |
|---|
| 1145 | 1170 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); |
|---|
| 1146 | 1171 | } |
|---|
| .. | .. |
|---|
| 1148 | 1173 | static int tcpm_pd_send_sink_caps(struct tcpm_port *port) |
|---|
| 1149 | 1174 | { |
|---|
| 1150 | 1175 | struct pd_message msg; |
|---|
| 1151 | | - int i; |
|---|
| 1176 | + u32 pdo; |
|---|
| 1177 | + unsigned int i, nr_pdo = 0; |
|---|
| 1152 | 1178 | |
|---|
| 1153 | 1179 | memset(&msg, 0, sizeof(msg)); |
|---|
| 1154 | | - if (!port->nr_snk_pdo) { |
|---|
| 1180 | + |
|---|
| 1181 | + for (i = 0; i < port->nr_snk_pdo; i++) { |
|---|
| 1182 | + if (port->negotiated_rev >= PD_REV30) { |
|---|
| 1183 | + msg.payload[nr_pdo++] = cpu_to_le32(port->snk_pdo[i]); |
|---|
| 1184 | + } else { |
|---|
| 1185 | + pdo = tcpm_forge_legacy_pdo(port, port->snk_pdo[i], TYPEC_SINK); |
|---|
| 1186 | + if (pdo) |
|---|
| 1187 | + msg.payload[nr_pdo++] = cpu_to_le32(pdo); |
|---|
| 1188 | + } |
|---|
| 1189 | + } |
|---|
| 1190 | + |
|---|
| 1191 | + if (!nr_pdo) { |
|---|
| 1155 | 1192 | /* No sink capabilities defined, source only */ |
|---|
| 1156 | 1193 | msg.header = PD_HEADER_LE(PD_CTRL_REJECT, |
|---|
| 1157 | 1194 | port->pwr_role, |
|---|
| .. | .. |
|---|
| 1164 | 1201 | port->data_role, |
|---|
| 1165 | 1202 | port->negotiated_rev, |
|---|
| 1166 | 1203 | port->message_id, |
|---|
| 1167 | | - port->nr_snk_pdo); |
|---|
| 1204 | + nr_pdo); |
|---|
| 1168 | 1205 | } |
|---|
| 1169 | | - for (i = 0; i < port->nr_snk_pdo; i++) |
|---|
| 1170 | | - msg.payload[i] = cpu_to_le32(port->snk_pdo[i]); |
|---|
| 1171 | 1206 | |
|---|
| 1172 | 1207 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); |
|---|
| 1173 | 1208 | } |
|---|
| .. | .. |
|---|
| 1203 | 1238 | } |
|---|
| 1204 | 1239 | } |
|---|
| 1205 | 1240 | |
|---|
| 1206 | | -static void mod_data_role_swap_work(struct tcpm_port *port, unsigned int delay_ms) |
|---|
| 1241 | +static void mod_send_discover_delayed_work(struct tcpm_port *port, unsigned int delay_ms) |
|---|
| 1207 | 1242 | { |
|---|
| 1208 | 1243 | if (delay_ms) { |
|---|
| 1209 | | - hrtimer_start(&port->data_role_swap_timer, |
|---|
| 1210 | | - ms_to_ktime(delay_ms), HRTIMER_MODE_REL); |
|---|
| 1244 | + hrtimer_start(&port->send_discover_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); |
|---|
| 1211 | 1245 | } else { |
|---|
| 1212 | | - hrtimer_cancel(&port->data_role_swap_timer); |
|---|
| 1213 | | - kthread_queue_work(port->wq, &port->data_role_swap); |
|---|
| 1246 | + hrtimer_cancel(&port->send_discover_timer); |
|---|
| 1247 | + kthread_queue_work(port->wq, &port->send_discover_work); |
|---|
| 1214 | 1248 | } |
|---|
| 1215 | 1249 | } |
|---|
| 1216 | 1250 | |
|---|
| .. | .. |
|---|
| 1322 | 1356 | |
|---|
| 1323 | 1357 | tcpm_log(port, "AMS %s start", tcpm_ams_str[ams]); |
|---|
| 1324 | 1358 | |
|---|
| 1325 | | - if (!tcpm_ams_interruptible(port) && ams != HARD_RESET) { |
|---|
| 1359 | + if (!tcpm_ams_interruptible(port) && |
|---|
| 1360 | + !(ams == HARD_RESET || ams == SOFT_RESET_AMS)) { |
|---|
| 1326 | 1361 | port->upcoming_state = INVALID_STATE; |
|---|
| 1327 | 1362 | tcpm_log(port, "AMS %s not interruptible, aborting", |
|---|
| 1328 | 1363 | tcpm_ams_str[port->ams]); |
|---|
| .. | .. |
|---|
| 1340 | 1375 | tcpm_set_state(port, HARD_RESET_START, 0); |
|---|
| 1341 | 1376 | return ret; |
|---|
| 1342 | 1377 | } else if (ams == SOFT_RESET_AMS) { |
|---|
| 1343 | | - if (!port->explicit_contract) { |
|---|
| 1344 | | - port->upcoming_state = INVALID_STATE; |
|---|
| 1378 | + if (!port->explicit_contract) |
|---|
| 1345 | 1379 | tcpm_set_cc(port, tcpm_rp_cc(port)); |
|---|
| 1346 | | - return ret; |
|---|
| 1347 | | - } |
|---|
| 1380 | + tcpm_set_state(port, SOFT_RESET_SEND, 0); |
|---|
| 1381 | + return ret; |
|---|
| 1348 | 1382 | } else if (tcpm_vdm_ams(port)) { |
|---|
| 1349 | 1383 | /* tSinkTx is enforced in vdm_run_state_machine */ |
|---|
| 1350 | 1384 | if (port->negotiated_rev >= PD_REV30) |
|---|
| .. | .. |
|---|
| 1413 | 1447 | static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header, |
|---|
| 1414 | 1448 | const u32 *data, int cnt) |
|---|
| 1415 | 1449 | { |
|---|
| 1450 | + u32 vdo_hdr = port->vdo_data[0]; |
|---|
| 1451 | + |
|---|
| 1452 | + WARN_ON(!mutex_is_locked(&port->lock)); |
|---|
| 1453 | + |
|---|
| 1454 | + /* If is sending discover_identity, handle received message first */ |
|---|
| 1455 | + if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMD(vdo_hdr) == CMD_DISCOVER_IDENT) { |
|---|
| 1456 | + port->send_discover = true; |
|---|
| 1457 | + mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS); |
|---|
| 1458 | + } else { |
|---|
| 1459 | + /* Make sure we are not still processing a previous VDM packet */ |
|---|
| 1460 | + WARN_ON(port->vdm_state > VDM_STATE_DONE); |
|---|
| 1461 | + } |
|---|
| 1462 | + |
|---|
| 1416 | 1463 | port->vdo_count = cnt + 1; |
|---|
| 1417 | 1464 | port->vdo_data[0] = header; |
|---|
| 1418 | 1465 | memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt); |
|---|
| 1419 | 1466 | /* Set ready, vdm state machine will actually send */ |
|---|
| 1420 | 1467 | port->vdm_retries = 0; |
|---|
| 1421 | 1468 | port->vdm_state = VDM_STATE_READY; |
|---|
| 1469 | + port->vdm_sm_running = true; |
|---|
| 1470 | + |
|---|
| 1471 | + mod_vdm_delayed_work(port, 0); |
|---|
| 1422 | 1472 | } |
|---|
| 1423 | 1473 | |
|---|
| 1424 | | -static void svdm_consume_identity(struct tcpm_port *port, const __le32 *payload, |
|---|
| 1425 | | - int cnt) |
|---|
| 1474 | +static void tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header, |
|---|
| 1475 | + const u32 *data, int cnt) |
|---|
| 1426 | 1476 | { |
|---|
| 1427 | | - u32 vdo = le32_to_cpu(payload[VDO_INDEX_IDH]); |
|---|
| 1428 | | - u32 product = le32_to_cpu(payload[VDO_INDEX_PRODUCT]); |
|---|
| 1477 | +#ifdef CONFIG_NO_GKI |
|---|
| 1478 | + mutex_lock(&port->pd_handler_lock); |
|---|
| 1479 | + if (tcpm_port_is_disconnected(port)) |
|---|
| 1480 | + goto unlock; |
|---|
| 1481 | +#endif |
|---|
| 1482 | + mutex_lock(&port->lock); |
|---|
| 1483 | + tcpm_queue_vdm(port, header, data, cnt); |
|---|
| 1484 | + mutex_unlock(&port->lock); |
|---|
| 1485 | +#ifdef CONFIG_NO_GKI |
|---|
| 1486 | +unlock: |
|---|
| 1487 | + mutex_unlock(&port->pd_handler_lock); |
|---|
| 1488 | +#endif |
|---|
| 1489 | +} |
|---|
| 1490 | + |
|---|
| 1491 | +static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) |
|---|
| 1492 | +{ |
|---|
| 1493 | + u32 vdo = p[VDO_INDEX_IDH]; |
|---|
| 1494 | + u32 product = p[VDO_INDEX_PRODUCT]; |
|---|
| 1429 | 1495 | |
|---|
| 1430 | 1496 | memset(&port->mode_data, 0, sizeof(port->mode_data)); |
|---|
| 1431 | 1497 | |
|---|
| 1432 | 1498 | port->partner_ident.id_header = vdo; |
|---|
| 1433 | | - port->partner_ident.cert_stat = le32_to_cpu(payload[VDO_INDEX_CSTAT]); |
|---|
| 1499 | + port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT]; |
|---|
| 1434 | 1500 | port->partner_ident.product = product; |
|---|
| 1435 | 1501 | |
|---|
| 1436 | 1502 | typec_partner_set_identity(port->partner); |
|---|
| .. | .. |
|---|
| 1440 | 1506 | PD_PRODUCT_PID(product), product & 0xffff); |
|---|
| 1441 | 1507 | } |
|---|
| 1442 | 1508 | |
|---|
| 1443 | | -static bool svdm_consume_svids(struct tcpm_port *port, const __le32 *payload, |
|---|
| 1444 | | - int cnt) |
|---|
| 1509 | +static bool svdm_consume_svids(struct tcpm_port *port, const u32 *p, int cnt) |
|---|
| 1445 | 1510 | { |
|---|
| 1446 | 1511 | struct pd_mode_data *pmdata = &port->mode_data; |
|---|
| 1447 | 1512 | int i; |
|---|
| 1448 | 1513 | |
|---|
| 1449 | 1514 | for (i = 1; i < cnt; i++) { |
|---|
| 1450 | | - u32 p = le32_to_cpu(payload[i]); |
|---|
| 1451 | 1515 | u16 svid; |
|---|
| 1452 | 1516 | |
|---|
| 1453 | | - svid = (p >> 16) & 0xffff; |
|---|
| 1517 | + svid = (p[i] >> 16) & 0xffff; |
|---|
| 1454 | 1518 | if (!svid) |
|---|
| 1455 | 1519 | return false; |
|---|
| 1456 | 1520 | |
|---|
| .. | .. |
|---|
| 1460 | 1524 | pmdata->svids[pmdata->nsvids++] = svid; |
|---|
| 1461 | 1525 | tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); |
|---|
| 1462 | 1526 | |
|---|
| 1463 | | - svid = p & 0xffff; |
|---|
| 1527 | + svid = p[i] & 0xffff; |
|---|
| 1464 | 1528 | if (!svid) |
|---|
| 1465 | 1529 | return false; |
|---|
| 1466 | 1530 | |
|---|
| .. | .. |
|---|
| 1470 | 1534 | pmdata->svids[pmdata->nsvids++] = svid; |
|---|
| 1471 | 1535 | tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); |
|---|
| 1472 | 1536 | } |
|---|
| 1473 | | - return true; |
|---|
| 1537 | + |
|---|
| 1538 | + /* |
|---|
| 1539 | + * PD3.0 Spec 6.4.4.3.2: The SVIDs are returned 2 per VDO (see Table |
|---|
| 1540 | + * 6-43), and can be returned maximum 6 VDOs per response (see Figure |
|---|
| 1541 | + * 6-19). If the Respondersupports 12 or more SVID then the Discover |
|---|
| 1542 | + * SVIDs Command Shall be executed multiple times until a Discover |
|---|
| 1543 | + * SVIDs VDO is returned ending either with a SVID value of 0x0000 in |
|---|
| 1544 | + * the last part of the last VDO or with a VDO containing two SVIDs |
|---|
| 1545 | + * with values of 0x0000. |
|---|
| 1546 | + * |
|---|
| 1547 | + * However, some odd dockers support SVIDs less than 12 but without |
|---|
| 1548 | + * 0x0000 in the last VDO, so we need to break the Discover SVIDs |
|---|
| 1549 | + * request and return false here. |
|---|
| 1550 | + */ |
|---|
| 1551 | + return cnt == 7; |
|---|
| 1474 | 1552 | abort: |
|---|
| 1475 | 1553 | tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX); |
|---|
| 1476 | 1554 | return false; |
|---|
| 1477 | 1555 | } |
|---|
| 1478 | 1556 | |
|---|
| 1479 | | -static void svdm_consume_modes(struct tcpm_port *port, const __le32 *payload, |
|---|
| 1480 | | - int cnt) |
|---|
| 1557 | +static void svdm_consume_modes(struct tcpm_port *port, const u32 *p, int cnt) |
|---|
| 1481 | 1558 | { |
|---|
| 1482 | 1559 | struct pd_mode_data *pmdata = &port->mode_data; |
|---|
| 1483 | 1560 | struct typec_altmode_desc *paltmode; |
|---|
| .. | .. |
|---|
| 1494 | 1571 | |
|---|
| 1495 | 1572 | paltmode->svid = pmdata->svids[pmdata->svid_index]; |
|---|
| 1496 | 1573 | paltmode->mode = i; |
|---|
| 1497 | | - paltmode->vdo = le32_to_cpu(payload[i]); |
|---|
| 1574 | + paltmode->vdo = p[i]; |
|---|
| 1498 | 1575 | |
|---|
| 1499 | 1576 | tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x", |
|---|
| 1500 | 1577 | pmdata->altmodes, paltmode->svid, |
|---|
| .. | .. |
|---|
| 1513 | 1590 | for (i = 0; i < modep->altmodes; i++) { |
|---|
| 1514 | 1591 | altmode = typec_partner_register_altmode(port->partner, |
|---|
| 1515 | 1592 | &modep->altmode_desc[i]); |
|---|
| 1516 | | - if (!altmode) |
|---|
| 1593 | + if (IS_ERR(altmode)) { |
|---|
| 1517 | 1594 | tcpm_log(port, "Failed to register partner SVID 0x%04x", |
|---|
| 1518 | 1595 | modep->altmode_desc[i].svid); |
|---|
| 1596 | + altmode = NULL; |
|---|
| 1597 | + } |
|---|
| 1519 | 1598 | port->partner_altmode[i] = altmode; |
|---|
| 1520 | 1599 | } |
|---|
| 1521 | 1600 | } |
|---|
| 1522 | 1601 | |
|---|
| 1523 | 1602 | #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header) |
|---|
| 1524 | 1603 | |
|---|
| 1525 | | -static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt, |
|---|
| 1526 | | - u32 *response) |
|---|
| 1604 | +static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev, |
|---|
| 1605 | + const u32 *p, int cnt, u32 *response, |
|---|
| 1606 | + enum adev_actions *adev_action) |
|---|
| 1527 | 1607 | { |
|---|
| 1528 | | - struct typec_altmode *adev; |
|---|
| 1608 | + struct typec_port *typec = port->typec_port; |
|---|
| 1529 | 1609 | struct typec_altmode *pdev; |
|---|
| 1530 | 1610 | struct pd_mode_data *modep; |
|---|
| 1531 | | - u32 p[PD_MAX_PAYLOAD]; |
|---|
| 1611 | + int svdm_version; |
|---|
| 1532 | 1612 | int rlen = 0; |
|---|
| 1533 | 1613 | int cmd_type; |
|---|
| 1534 | 1614 | int cmd; |
|---|
| 1535 | 1615 | int i; |
|---|
| 1536 | | - |
|---|
| 1537 | | - for (i = 0; i < cnt; i++) |
|---|
| 1538 | | - p[i] = le32_to_cpu(payload[i]); |
|---|
| 1539 | 1616 | |
|---|
| 1540 | 1617 | cmd_type = PD_VDO_CMDT(p[0]); |
|---|
| 1541 | 1618 | cmd = PD_VDO_CMD(p[0]); |
|---|
| .. | .. |
|---|
| 1545 | 1622 | |
|---|
| 1546 | 1623 | modep = &port->mode_data; |
|---|
| 1547 | 1624 | |
|---|
| 1548 | | - adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX, |
|---|
| 1549 | | - PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); |
|---|
| 1550 | | - |
|---|
| 1551 | 1625 | pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX, |
|---|
| 1552 | 1626 | PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); |
|---|
| 1627 | + |
|---|
| 1628 | + svdm_version = typec_get_negotiated_svdm_version(typec); |
|---|
| 1629 | + if (svdm_version < 0) |
|---|
| 1630 | + return 0; |
|---|
| 1553 | 1631 | |
|---|
| 1554 | 1632 | switch (cmd_type) { |
|---|
| 1555 | 1633 | case CMDT_INIT: |
|---|
| 1556 | 1634 | switch (cmd) { |
|---|
| 1557 | 1635 | case CMD_DISCOVER_IDENT: |
|---|
| 1558 | | - /* 6.4.4.3.1: Only respond as UFP (device) */ |
|---|
| 1559 | | - if (port->data_role == TYPEC_DEVICE && |
|---|
| 1636 | + if (PD_VDO_VID(p[0]) != USB_SID_PD) |
|---|
| 1637 | + break; |
|---|
| 1638 | + |
|---|
| 1639 | + if (PD_VDO_SVDM_VER(p[0]) < svdm_version) { |
|---|
| 1640 | + typec_partner_set_svdm_version(port->partner, |
|---|
| 1641 | + PD_VDO_SVDM_VER(p[0])); |
|---|
| 1642 | + svdm_version = PD_VDO_SVDM_VER(p[0]); |
|---|
| 1643 | + } |
|---|
| 1644 | + |
|---|
| 1645 | + port->ams = DISCOVER_IDENTITY; |
|---|
| 1646 | + /* |
|---|
| 1647 | + * PD2.0 Spec 6.10.3: respond with NAK as DFP (data host) |
|---|
| 1648 | + * PD3.1 Spec 6.4.4.2.5.1: respond with NAK if "invalid field" or |
|---|
| 1649 | + * "wrong configuation" or "Unrecognized" |
|---|
| 1650 | + */ |
|---|
| 1651 | + if ((port->data_role == TYPEC_DEVICE || svdm_version >= SVDM_VER_2_0) && |
|---|
| 1560 | 1652 | port->nr_snk_vdo) { |
|---|
| 1561 | | - for (i = 0; i < port->nr_snk_vdo; i++) |
|---|
| 1562 | | - response[i + 1] = port->snk_vdo[i]; |
|---|
| 1563 | | - rlen = port->nr_snk_vdo + 1; |
|---|
| 1653 | + if (svdm_version < SVDM_VER_2_0) { |
|---|
| 1654 | + for (i = 0; i < port->nr_snk_vdo_v1; i++) |
|---|
| 1655 | + response[i + 1] = port->snk_vdo_v1[i]; |
|---|
| 1656 | + rlen = port->nr_snk_vdo_v1 + 1; |
|---|
| 1657 | + |
|---|
| 1658 | + } else { |
|---|
| 1659 | + for (i = 0; i < port->nr_snk_vdo; i++) |
|---|
| 1660 | + response[i + 1] = port->snk_vdo[i]; |
|---|
| 1661 | + rlen = port->nr_snk_vdo + 1; |
|---|
| 1662 | + } |
|---|
| 1564 | 1663 | } |
|---|
| 1565 | 1664 | break; |
|---|
| 1566 | 1665 | case CMD_DISCOVER_SVID: |
|---|
| 1666 | + port->ams = DISCOVER_SVIDS; |
|---|
| 1567 | 1667 | break; |
|---|
| 1568 | 1668 | case CMD_DISCOVER_MODES: |
|---|
| 1669 | + port->ams = DISCOVER_MODES; |
|---|
| 1569 | 1670 | break; |
|---|
| 1570 | 1671 | case CMD_ENTER_MODE: |
|---|
| 1672 | + port->ams = DFP_TO_UFP_ENTER_MODE; |
|---|
| 1571 | 1673 | break; |
|---|
| 1572 | 1674 | case CMD_EXIT_MODE: |
|---|
| 1675 | + port->ams = DFP_TO_UFP_EXIT_MODE; |
|---|
| 1573 | 1676 | break; |
|---|
| 1574 | 1677 | case CMD_ATTENTION: |
|---|
| 1575 | 1678 | /* Attention command does not have response */ |
|---|
| 1576 | | - if (adev) |
|---|
| 1577 | | - typec_altmode_attention(adev, p[1]); |
|---|
| 1679 | + *adev_action = ADEV_ATTENTION; |
|---|
| 1578 | 1680 | return 0; |
|---|
| 1579 | 1681 | default: |
|---|
| 1580 | 1682 | break; |
|---|
| .. | .. |
|---|
| 1588 | 1690 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY); |
|---|
| 1589 | 1691 | rlen = 1; |
|---|
| 1590 | 1692 | } |
|---|
| 1693 | + response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
|---|
| 1694 | + (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec))); |
|---|
| 1591 | 1695 | break; |
|---|
| 1592 | 1696 | case CMDT_RSP_ACK: |
|---|
| 1593 | 1697 | /* silently drop message if we are not connected */ |
|---|
| .. | .. |
|---|
| 1598 | 1702 | |
|---|
| 1599 | 1703 | switch (cmd) { |
|---|
| 1600 | 1704 | case CMD_DISCOVER_IDENT: |
|---|
| 1705 | + if (PD_VDO_SVDM_VER(p[0]) < svdm_version) |
|---|
| 1706 | + typec_partner_set_svdm_version(port->partner, |
|---|
| 1707 | + PD_VDO_SVDM_VER(p[0])); |
|---|
| 1601 | 1708 | /* 6.4.4.3.1 */ |
|---|
| 1602 | | - svdm_consume_identity(port, payload, cnt); |
|---|
| 1603 | | - response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID); |
|---|
| 1709 | + svdm_consume_identity(port, p, cnt); |
|---|
| 1710 | + response[0] = VDO(USB_SID_PD, 1, typec_get_negotiated_svdm_version(typec), |
|---|
| 1711 | + CMD_DISCOVER_SVID); |
|---|
| 1604 | 1712 | rlen = 1; |
|---|
| 1605 | 1713 | break; |
|---|
| 1606 | 1714 | case CMD_DISCOVER_SVID: |
|---|
| 1607 | 1715 | /* 6.4.4.3.2 */ |
|---|
| 1608 | | - if (svdm_consume_svids(port, payload, cnt)) { |
|---|
| 1609 | | - response[0] = VDO(USB_SID_PD, 1, |
|---|
| 1610 | | - CMD_DISCOVER_SVID); |
|---|
| 1716 | + if (svdm_consume_svids(port, p, cnt)) { |
|---|
| 1717 | + response[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID); |
|---|
| 1611 | 1718 | rlen = 1; |
|---|
| 1612 | 1719 | } else if (modep->nsvids && supports_modal(port)) { |
|---|
| 1613 | | - response[0] = VDO(modep->svids[0], 1, |
|---|
| 1720 | + response[0] = VDO(modep->svids[0], 1, svdm_version, |
|---|
| 1614 | 1721 | CMD_DISCOVER_MODES); |
|---|
| 1615 | 1722 | rlen = 1; |
|---|
| 1616 | 1723 | } |
|---|
| 1617 | 1724 | break; |
|---|
| 1618 | 1725 | case CMD_DISCOVER_MODES: |
|---|
| 1619 | 1726 | /* 6.4.4.3.3 */ |
|---|
| 1620 | | - svdm_consume_modes(port, payload, cnt); |
|---|
| 1727 | + svdm_consume_modes(port, p, cnt); |
|---|
| 1621 | 1728 | modep->svid_index++; |
|---|
| 1622 | 1729 | if (modep->svid_index < modep->nsvids) { |
|---|
| 1623 | 1730 | u16 svid = modep->svids[modep->svid_index]; |
|---|
| 1624 | | - response[0] = VDO(svid, 1, CMD_DISCOVER_MODES); |
|---|
| 1731 | + response[0] = VDO(svid, 1, svdm_version, CMD_DISCOVER_MODES); |
|---|
| 1625 | 1732 | rlen = 1; |
|---|
| 1626 | | - } else { |
|---|
| 1733 | + } else if (port->data_role == TYPEC_HOST) { |
|---|
| 1627 | 1734 | tcpm_register_partner_altmodes(port); |
|---|
| 1628 | | - port->vdm_sm_running = false; |
|---|
| 1735 | + } else { |
|---|
| 1736 | + /* Do dr_swap for ufp if the port supports drd */ |
|---|
| 1737 | + if (port->typec_caps.data == TYPEC_PORT_DRD && |
|---|
| 1738 | + !IS_ERR_OR_NULL(port->port_altmode[0])) { |
|---|
| 1739 | + port->vdm_sm_running = false; |
|---|
| 1740 | + port->upcoming_state = DR_SWAP_SEND; |
|---|
| 1741 | + tcpm_ams_start(port, DATA_ROLE_SWAP); |
|---|
| 1742 | + } |
|---|
| 1629 | 1743 | } |
|---|
| 1630 | 1744 | break; |
|---|
| 1631 | 1745 | case CMD_ENTER_MODE: |
|---|
| 1632 | 1746 | if (adev && pdev) { |
|---|
| 1633 | 1747 | typec_altmode_update_active(pdev, true); |
|---|
| 1634 | | - |
|---|
| 1635 | | - if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) { |
|---|
| 1636 | | - response[0] = VDO(adev->svid, 1, |
|---|
| 1637 | | - CMD_EXIT_MODE); |
|---|
| 1638 | | - response[0] |= VDO_OPOS(adev->mode); |
|---|
| 1639 | | - return 1; |
|---|
| 1640 | | - } |
|---|
| 1748 | + *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL; |
|---|
| 1641 | 1749 | } |
|---|
| 1642 | 1750 | return 0; |
|---|
| 1643 | 1751 | case CMD_EXIT_MODE: |
|---|
| 1644 | 1752 | if (adev && pdev) { |
|---|
| 1645 | 1753 | typec_altmode_update_active(pdev, false); |
|---|
| 1646 | | - |
|---|
| 1647 | 1754 | /* Back to USB Operation */ |
|---|
| 1648 | | - WARN_ON(typec_altmode_notify(adev, |
|---|
| 1649 | | - TYPEC_STATE_USB, |
|---|
| 1650 | | - NULL)); |
|---|
| 1755 | + *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; |
|---|
| 1756 | + return 0; |
|---|
| 1651 | 1757 | } |
|---|
| 1652 | 1758 | break; |
|---|
| 1759 | + case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): |
|---|
| 1760 | + break; |
|---|
| 1653 | 1761 | default: |
|---|
| 1762 | + /* Unrecognized SVDM */ |
|---|
| 1763 | + response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); |
|---|
| 1764 | + rlen = 1; |
|---|
| 1765 | + response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
|---|
| 1766 | + (VDO_SVDM_VERS(svdm_version)); |
|---|
| 1654 | 1767 | break; |
|---|
| 1655 | 1768 | } |
|---|
| 1656 | 1769 | break; |
|---|
| 1657 | 1770 | case CMDT_RSP_NAK: |
|---|
| 1658 | 1771 | tcpm_ams_finish(port); |
|---|
| 1659 | 1772 | switch (cmd) { |
|---|
| 1773 | + case CMD_DISCOVER_IDENT: |
|---|
| 1774 | + /* Do dr_swap for ufp if the port supports drd */ |
|---|
| 1775 | + if (port->typec_caps.data == TYPEC_PORT_DRD && |
|---|
| 1776 | + port->data_role == TYPEC_DEVICE && |
|---|
| 1777 | + !IS_ERR_OR_NULL(port->port_altmode[0])) { |
|---|
| 1778 | + port->vdm_sm_running = false; |
|---|
| 1779 | + port->upcoming_state = DR_SWAP_SEND; |
|---|
| 1780 | + tcpm_ams_start(port, DATA_ROLE_SWAP); |
|---|
| 1781 | + break; |
|---|
| 1782 | + } |
|---|
| 1783 | + fallthrough; |
|---|
| 1784 | + case CMD_DISCOVER_SVID: |
|---|
| 1785 | + case CMD_DISCOVER_MODES: |
|---|
| 1786 | + case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): |
|---|
| 1787 | + break; |
|---|
| 1660 | 1788 | case CMD_ENTER_MODE: |
|---|
| 1661 | 1789 | /* Back to USB Operation */ |
|---|
| 1662 | | - if (adev) |
|---|
| 1663 | | - WARN_ON(typec_altmode_notify(adev, |
|---|
| 1664 | | - TYPEC_STATE_USB, |
|---|
| 1665 | | - NULL)); |
|---|
| 1666 | | - break; |
|---|
| 1790 | + *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; |
|---|
| 1791 | + return 0; |
|---|
| 1667 | 1792 | default: |
|---|
| 1793 | + /* Unrecognized SVDM */ |
|---|
| 1794 | + response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); |
|---|
| 1795 | + rlen = 1; |
|---|
| 1796 | + response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
|---|
| 1797 | + (VDO_SVDM_VERS(svdm_version)); |
|---|
| 1668 | 1798 | break; |
|---|
| 1669 | 1799 | } |
|---|
| 1670 | | - port->vdm_sm_running = false; |
|---|
| 1671 | 1800 | break; |
|---|
| 1672 | 1801 | default: |
|---|
| 1673 | | - port->vdm_sm_running = false; |
|---|
| 1802 | + response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); |
|---|
| 1803 | + rlen = 1; |
|---|
| 1804 | + response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
|---|
| 1805 | + (VDO_SVDM_VERS(svdm_version)); |
|---|
| 1674 | 1806 | break; |
|---|
| 1675 | 1807 | } |
|---|
| 1676 | 1808 | |
|---|
| 1677 | 1809 | /* Informing the alternate mode drivers about everything */ |
|---|
| 1678 | | - if (adev) |
|---|
| 1679 | | - typec_altmode_vdm(adev, p[0], &p[1], cnt); |
|---|
| 1680 | | - |
|---|
| 1810 | + *adev_action = ADEV_QUEUE_VDM; |
|---|
| 1681 | 1811 | return rlen; |
|---|
| 1682 | 1812 | } |
|---|
| 1813 | + |
|---|
| 1814 | +static void tcpm_pd_handle_msg(struct tcpm_port *port, |
|---|
| 1815 | + enum pd_msg_request message, |
|---|
| 1816 | + enum tcpm_ams ams); |
|---|
| 1683 | 1817 | |
|---|
| 1684 | 1818 | static void tcpm_handle_vdm_request(struct tcpm_port *port, |
|---|
| 1685 | 1819 | const __le32 *payload, int cnt) |
|---|
| 1686 | 1820 | { |
|---|
| 1687 | | - int rlen = 0; |
|---|
| 1821 | + enum adev_actions adev_action = ADEV_NONE; |
|---|
| 1822 | + struct typec_altmode *adev; |
|---|
| 1823 | + u32 p[PD_MAX_PAYLOAD]; |
|---|
| 1688 | 1824 | u32 response[8] = { }; |
|---|
| 1689 | | - u32 p0 = le32_to_cpu(payload[0]); |
|---|
| 1825 | + int i, rlen = 0; |
|---|
| 1826 | + |
|---|
| 1827 | + for (i = 0; i < cnt; i++) |
|---|
| 1828 | + p[i] = le32_to_cpu(payload[i]); |
|---|
| 1829 | + |
|---|
| 1830 | + adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX, |
|---|
| 1831 | + PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); |
|---|
| 1690 | 1832 | |
|---|
| 1691 | 1833 | if (port->vdm_state == VDM_STATE_BUSY) { |
|---|
| 1692 | 1834 | /* If UFP responded busy retry after timeout */ |
|---|
| 1693 | | - if (PD_VDO_CMDT(p0) == CMDT_RSP_BUSY) { |
|---|
| 1835 | + if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) { |
|---|
| 1694 | 1836 | port->vdm_state = VDM_STATE_WAIT_RSP_BUSY; |
|---|
| 1695 | | - port->vdo_retry = (p0 & ~VDO_CMDT_MASK) | |
|---|
| 1837 | + port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) | |
|---|
| 1696 | 1838 | CMDT_INIT; |
|---|
| 1697 | 1839 | mod_vdm_delayed_work(port, PD_T_VDM_BUSY); |
|---|
| 1698 | 1840 | return; |
|---|
| .. | .. |
|---|
| 1700 | 1842 | port->vdm_state = VDM_STATE_DONE; |
|---|
| 1701 | 1843 | } |
|---|
| 1702 | 1844 | |
|---|
| 1703 | | - if (PD_VDO_SVDM(p0)) |
|---|
| 1704 | | - rlen = tcpm_pd_svdm(port, payload, cnt, response); |
|---|
| 1705 | | - |
|---|
| 1706 | | - if (rlen > 0) { |
|---|
| 1707 | | - tcpm_queue_vdm(port, response[0], &response[1], rlen - 1); |
|---|
| 1708 | | - mod_vdm_delayed_work(port, 0); |
|---|
| 1845 | + if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) { |
|---|
| 1846 | + /* |
|---|
| 1847 | + * Here a SVDM is received (INIT or RSP or unknown). Set the vdm_sm_running in |
|---|
| 1848 | + * advance because we are dropping the lock but may send VDMs soon. |
|---|
| 1849 | + * For the cases of INIT received: |
|---|
| 1850 | + * - If no response to send, it will be cleared later in this function. |
|---|
| 1851 | + * - If there are responses to send, it will be cleared in the state machine. |
|---|
| 1852 | + * For the cases of RSP received: |
|---|
| 1853 | + * - If no further INIT to send, it will be cleared later in this function. |
|---|
| 1854 | + * - Otherwise, it will be cleared in the state machine if timeout or it will go |
|---|
| 1855 | + * back here until no further INIT to send. |
|---|
| 1856 | + * For the cases of unknown type received: |
|---|
| 1857 | + * - We will send NAK and the flag will be cleared in the state machine. |
|---|
| 1858 | + */ |
|---|
| 1859 | + port->vdm_sm_running = true; |
|---|
| 1860 | + rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action); |
|---|
| 1861 | + } else { |
|---|
| 1862 | + if (port->negotiated_rev >= PD_REV30) |
|---|
| 1863 | + tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
|---|
| 1709 | 1864 | } |
|---|
| 1865 | + |
|---|
| 1866 | + /* |
|---|
| 1867 | + * We are done with any state stored in the port struct now, except |
|---|
| 1868 | + * for any port struct changes done by the tcpm_queue_vdm() call |
|---|
| 1869 | + * below, which is a separate operation. |
|---|
| 1870 | + * |
|---|
| 1871 | + * So we can safely release the lock here; and we MUST release the |
|---|
| 1872 | + * lock here to avoid an AB BA lock inversion: |
|---|
| 1873 | + * |
|---|
| 1874 | + * If we keep the lock here then the lock ordering in this path is: |
|---|
| 1875 | + * 1. tcpm_pd_rx_handler take the tcpm port lock |
|---|
| 1876 | + * 2. One of the typec_altmode_* calls below takes the alt-mode's lock |
|---|
| 1877 | + * |
|---|
| 1878 | + * And we also have this ordering: |
|---|
| 1879 | + * 1. alt-mode driver takes the alt-mode's lock |
|---|
| 1880 | + * 2. alt-mode driver calls tcpm_altmode_enter which takes the |
|---|
| 1881 | + * tcpm port lock |
|---|
| 1882 | + * |
|---|
| 1883 | + * Dropping our lock here avoids this. |
|---|
| 1884 | + */ |
|---|
| 1885 | + mutex_unlock(&port->lock); |
|---|
| 1886 | + |
|---|
| 1887 | + if (adev) { |
|---|
| 1888 | + switch (adev_action) { |
|---|
| 1889 | + case ADEV_NONE: |
|---|
| 1890 | + break; |
|---|
| 1891 | + case ADEV_NOTIFY_USB_AND_QUEUE_VDM: |
|---|
| 1892 | + WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL)); |
|---|
| 1893 | + typec_altmode_vdm(adev, p[0], &p[1], cnt); |
|---|
| 1894 | + break; |
|---|
| 1895 | + case ADEV_QUEUE_VDM: |
|---|
| 1896 | + typec_altmode_vdm(adev, p[0], &p[1], cnt); |
|---|
| 1897 | + break; |
|---|
| 1898 | + case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL: |
|---|
| 1899 | + if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) { |
|---|
| 1900 | + int svdm_version = typec_get_negotiated_svdm_version( |
|---|
| 1901 | + port->typec_port); |
|---|
| 1902 | + if (svdm_version < 0) |
|---|
| 1903 | + break; |
|---|
| 1904 | + |
|---|
| 1905 | + response[0] = VDO(adev->svid, 1, svdm_version, |
|---|
| 1906 | + CMD_EXIT_MODE); |
|---|
| 1907 | + response[0] |= VDO_OPOS(adev->mode); |
|---|
| 1908 | + rlen = 1; |
|---|
| 1909 | + } |
|---|
| 1910 | + break; |
|---|
| 1911 | + case ADEV_ATTENTION: |
|---|
| 1912 | + if (typec_altmode_attention(adev, p[1])) |
|---|
| 1913 | + tcpm_log(port, "typec_altmode_attention no port partner altmode"); |
|---|
| 1914 | + break; |
|---|
| 1915 | + } |
|---|
| 1916 | + } |
|---|
| 1917 | + |
|---|
| 1918 | + /* |
|---|
| 1919 | + * We must re-take the lock here to balance the unlock in |
|---|
| 1920 | + * tcpm_pd_rx_handler, note that no changes, other then the |
|---|
| 1921 | + * tcpm_queue_vdm call, are made while the lock is held again. |
|---|
| 1922 | + * All that is done after the call is unwinding the call stack until |
|---|
| 1923 | + * we return to tcpm_pd_rx_handler and do the unlock there. |
|---|
| 1924 | + */ |
|---|
| 1925 | + mutex_lock(&port->lock); |
|---|
| 1926 | + |
|---|
| 1927 | + if (rlen > 0) |
|---|
| 1928 | + tcpm_queue_vdm(port, response[0], &response[1], rlen - 1); |
|---|
| 1929 | + else |
|---|
| 1930 | + port->vdm_sm_running = false; |
|---|
| 1710 | 1931 | } |
|---|
| 1711 | 1932 | |
|---|
| 1712 | 1933 | static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd, |
|---|
| 1713 | 1934 | const u32 *data, int count) |
|---|
| 1714 | 1935 | { |
|---|
| 1936 | + int svdm_version = typec_get_negotiated_svdm_version(port->typec_port); |
|---|
| 1715 | 1937 | u32 header; |
|---|
| 1938 | + |
|---|
| 1939 | + if (svdm_version < 0) |
|---|
| 1940 | + return; |
|---|
| 1716 | 1941 | |
|---|
| 1717 | 1942 | if (WARN_ON(count > VDO_MAX_SIZE - 1)) |
|---|
| 1718 | 1943 | count = VDO_MAX_SIZE - 1; |
|---|
| 1719 | 1944 | |
|---|
| 1720 | 1945 | /* set VDM header with VID & CMD */ |
|---|
| 1721 | 1946 | header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ? |
|---|
| 1722 | | - 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd); |
|---|
| 1947 | + 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), |
|---|
| 1948 | + svdm_version, cmd); |
|---|
| 1723 | 1949 | tcpm_queue_vdm(port, header, data, count); |
|---|
| 1724 | | - |
|---|
| 1725 | | - mod_vdm_delayed_work(port, 0); |
|---|
| 1726 | 1950 | } |
|---|
| 1727 | 1951 | |
|---|
| 1728 | 1952 | static unsigned int vdm_ready_timeout(u32 vdm_hdr) |
|---|
| .. | .. |
|---|
| 1769 | 1993 | * if there's traffic or we're not in PDO ready state don't send |
|---|
| 1770 | 1994 | * a VDM. |
|---|
| 1771 | 1995 | */ |
|---|
| 1772 | | - if (port->state != SRC_READY && port->state != SNK_READY) |
|---|
| 1996 | + if (port->state != SRC_READY && port->state != SNK_READY) { |
|---|
| 1997 | + port->vdm_sm_running = false; |
|---|
| 1773 | 1998 | break; |
|---|
| 1999 | + } |
|---|
| 1774 | 2000 | |
|---|
| 1775 | 2001 | /* TODO: AMS operation for Unstructured VDM */ |
|---|
| 1776 | 2002 | if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) { |
|---|
| 1777 | 2003 | switch (PD_VDO_CMD(vdo_hdr)) { |
|---|
| 1778 | 2004 | case CMD_DISCOVER_IDENT: |
|---|
| 1779 | 2005 | res = tcpm_ams_start(port, DISCOVER_IDENTITY); |
|---|
| 1780 | | - if (res == 0) |
|---|
| 2006 | + if (res == 0) { |
|---|
| 1781 | 2007 | port->send_discover = false; |
|---|
| 2008 | + } else if (res == -EAGAIN) { |
|---|
| 2009 | + port->vdo_data[0] = 0; |
|---|
| 2010 | + mod_send_discover_delayed_work(port, |
|---|
| 2011 | + SEND_DISCOVER_RETRY_MS); |
|---|
| 2012 | + } |
|---|
| 1782 | 2013 | break; |
|---|
| 1783 | 2014 | case CMD_DISCOVER_SVID: |
|---|
| 1784 | 2015 | res = tcpm_ams_start(port, DISCOVER_SVIDS); |
|---|
| .. | .. |
|---|
| 1804 | 2035 | } |
|---|
| 1805 | 2036 | |
|---|
| 1806 | 2037 | if (res < 0) { |
|---|
| 1807 | | - port->vdm_sm_running = false; |
|---|
| 2038 | + port->vdm_state = VDM_STATE_ERR_BUSY; |
|---|
| 1808 | 2039 | return; |
|---|
| 1809 | 2040 | } |
|---|
| 1810 | 2041 | } |
|---|
| .. | .. |
|---|
| 1820 | 2051 | port->vdo_data[0] = port->vdo_retry; |
|---|
| 1821 | 2052 | port->vdo_count = 1; |
|---|
| 1822 | 2053 | port->vdm_state = VDM_STATE_READY; |
|---|
| 2054 | + tcpm_ams_finish(port); |
|---|
| 1823 | 2055 | break; |
|---|
| 1824 | 2056 | case VDM_STATE_BUSY: |
|---|
| 1825 | 2057 | port->vdm_state = VDM_STATE_ERR_TMOUT; |
|---|
| .. | .. |
|---|
| 1860 | 2092 | unsigned long timeout; |
|---|
| 1861 | 2093 | |
|---|
| 1862 | 2094 | port->vdm_retries = 0; |
|---|
| 2095 | + port->vdo_data[0] = 0; |
|---|
| 1863 | 2096 | port->vdm_state = VDM_STATE_BUSY; |
|---|
| 1864 | 2097 | timeout = vdm_ready_timeout(vdo_hdr); |
|---|
| 1865 | 2098 | mod_vdm_delayed_work(port, timeout); |
|---|
| .. | .. |
|---|
| 1888 | 2121 | port->vdm_state != VDM_STATE_BUSY && |
|---|
| 1889 | 2122 | port->vdm_state != VDM_STATE_SEND_MESSAGE); |
|---|
| 1890 | 2123 | |
|---|
| 1891 | | - if (port->vdm_state == VDM_STATE_ERR_TMOUT) |
|---|
| 2124 | + if (port->vdm_state < VDM_STATE_READY) |
|---|
| 1892 | 2125 | port->vdm_sm_running = false; |
|---|
| 1893 | 2126 | |
|---|
| 1894 | 2127 | mutex_unlock(&port->lock); |
|---|
| .. | .. |
|---|
| 2014 | 2247 | return 0; |
|---|
| 2015 | 2248 | } |
|---|
| 2016 | 2249 | |
|---|
| 2017 | | -static int tcpm_altmode_enter(struct typec_altmode *altmode) |
|---|
| 2250 | +static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo) |
|---|
| 2018 | 2251 | { |
|---|
| 2019 | 2252 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); |
|---|
| 2253 | + int svdm_version; |
|---|
| 2020 | 2254 | u32 header; |
|---|
| 2021 | | - int ret = 0; |
|---|
| 2022 | 2255 | |
|---|
| 2023 | | - mutex_lock(&port->pd_handler_lock); |
|---|
| 2024 | | - if (tcpm_port_is_disconnected(port)) { |
|---|
| 2025 | | - ret = -ENODEV; |
|---|
| 2026 | | - goto unlock; |
|---|
| 2027 | | - } |
|---|
| 2256 | + svdm_version = typec_get_negotiated_svdm_version(port->typec_port); |
|---|
| 2257 | + if (svdm_version < 0) |
|---|
| 2258 | + return svdm_version; |
|---|
| 2028 | 2259 | |
|---|
| 2029 | | - mutex_lock(&port->lock); |
|---|
| 2030 | | - header = VDO(altmode->svid, 1, CMD_ENTER_MODE); |
|---|
| 2260 | + header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE); |
|---|
| 2031 | 2261 | header |= VDO_OPOS(altmode->mode); |
|---|
| 2032 | 2262 | |
|---|
| 2033 | | - tcpm_queue_vdm(port, header, NULL, 0); |
|---|
| 2034 | | - mod_vdm_delayed_work(port, 0); |
|---|
| 2035 | | - mutex_unlock(&port->lock); |
|---|
| 2036 | | -unlock: |
|---|
| 2037 | | - mutex_unlock(&port->pd_handler_lock); |
|---|
| 2038 | | - return ret; |
|---|
| 2263 | + tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0); |
|---|
| 2264 | + return 0; |
|---|
| 2039 | 2265 | } |
|---|
| 2040 | 2266 | |
|---|
| 2041 | 2267 | static int tcpm_altmode_exit(struct typec_altmode *altmode) |
|---|
| 2042 | 2268 | { |
|---|
| 2043 | 2269 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); |
|---|
| 2270 | + int svdm_version; |
|---|
| 2044 | 2271 | u32 header; |
|---|
| 2045 | | - int ret = 0; |
|---|
| 2046 | 2272 | |
|---|
| 2047 | | - mutex_lock(&port->pd_handler_lock); |
|---|
| 2048 | | - if (tcpm_port_is_disconnected(port)) { |
|---|
| 2049 | | - ret = -ENODEV; |
|---|
| 2050 | | - goto unlock; |
|---|
| 2051 | | - } |
|---|
| 2273 | + svdm_version = typec_get_negotiated_svdm_version(port->typec_port); |
|---|
| 2274 | + if (svdm_version < 0) |
|---|
| 2275 | + return svdm_version; |
|---|
| 2052 | 2276 | |
|---|
| 2053 | | - mutex_lock(&port->lock); |
|---|
| 2054 | | - header = VDO(altmode->svid, 1, CMD_EXIT_MODE); |
|---|
| 2277 | + header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE); |
|---|
| 2055 | 2278 | header |= VDO_OPOS(altmode->mode); |
|---|
| 2056 | 2279 | |
|---|
| 2057 | | - tcpm_queue_vdm(port, header, NULL, 0); |
|---|
| 2058 | | - mod_vdm_delayed_work(port, 0); |
|---|
| 2059 | | - mutex_unlock(&port->lock); |
|---|
| 2060 | | - |
|---|
| 2061 | | -unlock: |
|---|
| 2062 | | - mutex_unlock(&port->pd_handler_lock); |
|---|
| 2063 | | - return ret; |
|---|
| 2280 | + tcpm_queue_vdm_unlocked(port, header, NULL, 0); |
|---|
| 2281 | + return 0; |
|---|
| 2064 | 2282 | } |
|---|
| 2065 | 2283 | |
|---|
| 2066 | 2284 | static int tcpm_altmode_vdm(struct typec_altmode *altmode, |
|---|
| 2067 | 2285 | u32 header, const u32 *data, int count) |
|---|
| 2068 | 2286 | { |
|---|
| 2069 | 2287 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); |
|---|
| 2070 | | - int ret = 0; |
|---|
| 2071 | 2288 | |
|---|
| 2072 | | - mutex_lock(&port->pd_handler_lock); |
|---|
| 2073 | | - if (tcpm_port_is_disconnected(port)) { |
|---|
| 2074 | | - ret = -ENODEV; |
|---|
| 2075 | | - goto unlock; |
|---|
| 2076 | | - } |
|---|
| 2077 | | - |
|---|
| 2078 | | - mutex_lock(&port->lock); |
|---|
| 2079 | | - tcpm_queue_vdm(port, header, data, count - 1); |
|---|
| 2080 | | - mod_vdm_delayed_work(port, 0); |
|---|
| 2081 | | - mutex_unlock(&port->lock); |
|---|
| 2082 | | - |
|---|
| 2083 | | -unlock: |
|---|
| 2084 | | - mutex_unlock(&port->pd_handler_lock); |
|---|
| 2085 | | - return ret; |
|---|
| 2086 | | -} |
|---|
| 2087 | | - |
|---|
| 2088 | | -static int tcpm_altmode_notify(struct typec_altmode *altmode, |
|---|
| 2089 | | - unsigned long conf, void *data) |
|---|
| 2090 | | -{ |
|---|
| 2091 | | - struct tcpm_port *port = typec_altmode_get_drvdata(altmode); |
|---|
| 2092 | | - struct typec_displayport_data *dp_data = data; |
|---|
| 2093 | | - |
|---|
| 2094 | | - if ((conf >= TYPEC_DP_STATE_A) && (conf <= TYPEC_DP_STATE_F)) { |
|---|
| 2095 | | - if (port->dp_configured) { |
|---|
| 2096 | | - port->dp_status = dp_data->status; |
|---|
| 2097 | | - tcpm_send_dp_notify(port); |
|---|
| 2098 | | - dev_info(port->dev, "dp_status %x\n", dp_data->status); |
|---|
| 2099 | | - } else { |
|---|
| 2100 | | - port->dp_pin_assignment = dp_data->conf; |
|---|
| 2101 | | - port->dp_configured = true; |
|---|
| 2102 | | - dev_info(port->dev, "DP pin assignment 0x%x\n", |
|---|
| 2103 | | - port->dp_pin_assignment); |
|---|
| 2104 | | - } |
|---|
| 2105 | | - } else if ((conf == TYPEC_STATE_USB) && (port->dp_configured)) { |
|---|
| 2106 | | - /* may receive ufp device response Exit Mode Command Ack */ |
|---|
| 2107 | | - port->dp_status = 0; |
|---|
| 2108 | | - port->dp_pin_assignment = 0; |
|---|
| 2109 | | - port->dp_configured = false; |
|---|
| 2110 | | - tcpm_send_dp_notify(port); |
|---|
| 2111 | | - } |
|---|
| 2289 | + tcpm_queue_vdm_unlocked(port, header, data, count - 1); |
|---|
| 2112 | 2290 | |
|---|
| 2113 | 2291 | return 0; |
|---|
| 2114 | 2292 | } |
|---|
| .. | .. |
|---|
| 2117 | 2295 | .enter = tcpm_altmode_enter, |
|---|
| 2118 | 2296 | .exit = tcpm_altmode_exit, |
|---|
| 2119 | 2297 | .vdm = tcpm_altmode_vdm, |
|---|
| 2120 | | - .notify = tcpm_altmode_notify, |
|---|
| 2121 | 2298 | }; |
|---|
| 2122 | 2299 | |
|---|
| 2123 | 2300 | /* |
|---|
| .. | .. |
|---|
| 2142 | 2319 | |
|---|
| 2143 | 2320 | if (!type) { |
|---|
| 2144 | 2321 | tcpm_log(port, "Alert message received with no type"); |
|---|
| 2322 | + tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); |
|---|
| 2145 | 2323 | return; |
|---|
| 2146 | 2324 | } |
|---|
| 2147 | 2325 | |
|---|
| 2148 | 2326 | /* Just handling non-battery alerts for now */ |
|---|
| 2149 | 2327 | if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) { |
|---|
| 2150 | | - switch (port->state) { |
|---|
| 2151 | | - case SRC_READY: |
|---|
| 2152 | | - case SNK_READY: |
|---|
| 2328 | + if (port->pwr_role == TYPEC_SOURCE) { |
|---|
| 2329 | + port->upcoming_state = GET_STATUS_SEND; |
|---|
| 2330 | + tcpm_ams_start(port, GETTING_SOURCE_SINK_STATUS); |
|---|
| 2331 | + } else { |
|---|
| 2332 | + /* |
|---|
| 2333 | + * Do not check SinkTxOk here in case the Source doesn't set its Rp to |
|---|
| 2334 | + * SinkTxOk in time. |
|---|
| 2335 | + */ |
|---|
| 2336 | + port->ams = GETTING_SOURCE_SINK_STATUS; |
|---|
| 2153 | 2337 | tcpm_set_state(port, GET_STATUS_SEND, 0); |
|---|
| 2154 | | - break; |
|---|
| 2155 | | - default: |
|---|
| 2156 | | - tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2157 | | - break; |
|---|
| 2158 | 2338 | } |
|---|
| 2339 | + } else { |
|---|
| 2340 | + tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); |
|---|
| 2341 | + } |
|---|
| 2342 | +} |
|---|
| 2343 | + |
|---|
| 2344 | +static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port, |
|---|
| 2345 | + enum typec_pwr_opmode mode, bool pps_active, |
|---|
| 2346 | + u32 requested_vbus_voltage) |
|---|
| 2347 | +{ |
|---|
| 2348 | + int ret; |
|---|
| 2349 | + |
|---|
| 2350 | + if (!port->tcpc->set_auto_vbus_discharge_threshold) |
|---|
| 2351 | + return 0; |
|---|
| 2352 | + |
|---|
| 2353 | + ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active, |
|---|
| 2354 | + requested_vbus_voltage); |
|---|
| 2355 | + tcpm_log_force(port, |
|---|
| 2356 | + "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u ret:%d", |
|---|
| 2357 | + mode, pps_active ? 'y' : 'n', requested_vbus_voltage, ret); |
|---|
| 2358 | + |
|---|
| 2359 | + return ret; |
|---|
| 2360 | +} |
|---|
| 2361 | + |
|---|
| 2362 | +static void tcpm_pd_handle_state(struct tcpm_port *port, |
|---|
| 2363 | + enum tcpm_state state, |
|---|
| 2364 | + enum tcpm_ams ams, |
|---|
| 2365 | + unsigned int delay_ms) |
|---|
| 2366 | +{ |
|---|
| 2367 | + switch (port->state) { |
|---|
| 2368 | + case SRC_READY: |
|---|
| 2369 | + case SNK_READY: |
|---|
| 2370 | + port->ams = ams; |
|---|
| 2371 | + tcpm_set_state(port, state, delay_ms); |
|---|
| 2372 | + break; |
|---|
| 2373 | + /* 8.3.3.4.1.1 and 6.8.1 power transitioning */ |
|---|
| 2374 | + case SNK_TRANSITION_SINK: |
|---|
| 2375 | + case SNK_TRANSITION_SINK_VBUS: |
|---|
| 2376 | + case SRC_TRANSITION_SUPPLY: |
|---|
| 2377 | + tcpm_set_state(port, HARD_RESET_SEND, 0); |
|---|
| 2378 | + break; |
|---|
| 2379 | + default: |
|---|
| 2380 | + if (!tcpm_ams_interruptible(port)) { |
|---|
| 2381 | + tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? |
|---|
| 2382 | + SRC_SOFT_RESET_WAIT_SNK_TX : |
|---|
| 2383 | + SNK_SOFT_RESET, |
|---|
| 2384 | + 0); |
|---|
| 2385 | + } else { |
|---|
| 2386 | + /* process the Message 6.8.1 */ |
|---|
| 2387 | + port->upcoming_state = state; |
|---|
| 2388 | + port->next_ams = ams; |
|---|
| 2389 | + tcpm_set_state(port, ready_state(port), delay_ms); |
|---|
| 2390 | + } |
|---|
| 2391 | + break; |
|---|
| 2392 | + } |
|---|
| 2393 | +} |
|---|
| 2394 | + |
|---|
| 2395 | +static void tcpm_pd_handle_msg(struct tcpm_port *port, |
|---|
| 2396 | + enum pd_msg_request message, |
|---|
| 2397 | + enum tcpm_ams ams) |
|---|
| 2398 | +{ |
|---|
| 2399 | + switch (port->state) { |
|---|
| 2400 | + case SRC_READY: |
|---|
| 2401 | + case SNK_READY: |
|---|
| 2402 | + port->ams = ams; |
|---|
| 2403 | + tcpm_queue_message(port, message); |
|---|
| 2404 | + break; |
|---|
| 2405 | + /* PD 3.0 Spec 8.3.3.4.1.1 and 6.8.1 */ |
|---|
| 2406 | + case SNK_TRANSITION_SINK: |
|---|
| 2407 | + case SNK_TRANSITION_SINK_VBUS: |
|---|
| 2408 | + case SRC_TRANSITION_SUPPLY: |
|---|
| 2409 | + tcpm_set_state(port, HARD_RESET_SEND, 0); |
|---|
| 2410 | + break; |
|---|
| 2411 | + default: |
|---|
| 2412 | + if (!tcpm_ams_interruptible(port)) { |
|---|
| 2413 | + tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? |
|---|
| 2414 | + SRC_SOFT_RESET_WAIT_SNK_TX : |
|---|
| 2415 | + SNK_SOFT_RESET, |
|---|
| 2416 | + 0); |
|---|
| 2417 | + } else { |
|---|
| 2418 | + port->next_ams = ams; |
|---|
| 2419 | + tcpm_set_state(port, ready_state(port), 0); |
|---|
| 2420 | + /* 6.8.1 process the Message */ |
|---|
| 2421 | + tcpm_queue_message(port, message); |
|---|
| 2422 | + } |
|---|
| 2423 | + break; |
|---|
| 2159 | 2424 | } |
|---|
| 2160 | 2425 | } |
|---|
| 2161 | 2426 | |
|---|
| .. | .. |
|---|
| 2166 | 2431 | unsigned int cnt = pd_header_cnt_le(msg->header); |
|---|
| 2167 | 2432 | unsigned int rev = pd_header_rev_le(msg->header); |
|---|
| 2168 | 2433 | unsigned int i; |
|---|
| 2169 | | - enum frs_typec_current frs_current; |
|---|
| 2434 | + enum frs_typec_current partner_frs_current; |
|---|
| 2170 | 2435 | bool frs_enable; |
|---|
| 2171 | 2436 | int ret; |
|---|
| 2172 | 2437 | |
|---|
| 2438 | + if (tcpm_vdm_ams(port) && type != PD_DATA_VENDOR_DEF) { |
|---|
| 2439 | + port->vdm_state = VDM_STATE_ERR_BUSY; |
|---|
| 2440 | + tcpm_ams_finish(port); |
|---|
| 2441 | + mod_vdm_delayed_work(port, 0); |
|---|
| 2442 | + } |
|---|
| 2443 | + |
|---|
| 2173 | 2444 | switch (type) { |
|---|
| 2174 | 2445 | case PD_DATA_SOURCE_CAP: |
|---|
| 2175 | | - if (port->pwr_role != TYPEC_SINK) |
|---|
| 2176 | | - break; |
|---|
| 2177 | | - |
|---|
| 2178 | 2446 | for (i = 0; i < cnt; i++) |
|---|
| 2179 | 2447 | port->source_caps[i] = le32_to_cpu(msg->payload[i]); |
|---|
| 2180 | 2448 | |
|---|
| .. | .. |
|---|
| 2185 | 2453 | tcpm_validate_caps(port, port->source_caps, |
|---|
| 2186 | 2454 | port->nr_source_caps); |
|---|
| 2187 | 2455 | |
|---|
| 2456 | + trace_android_vh_typec_store_partner_src_caps(port, &port->nr_source_caps, |
|---|
| 2457 | + &port->source_caps); |
|---|
| 2458 | + |
|---|
| 2188 | 2459 | /* |
|---|
| 2189 | 2460 | * Adjust revision in subsequent message headers, as required, |
|---|
| 2190 | 2461 | * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't |
|---|
| 2191 | 2462 | * support Rev 1.0 so just do nothing in that scenario. |
|---|
| 2192 | 2463 | */ |
|---|
| 2193 | | - if (rev == PD_REV10) |
|---|
| 2464 | + if (rev == PD_REV10) { |
|---|
| 2465 | + if (port->ams == GET_SOURCE_CAPABILITIES) |
|---|
| 2466 | + tcpm_ams_finish(port); |
|---|
| 2194 | 2467 | break; |
|---|
| 2468 | + } |
|---|
| 2195 | 2469 | |
|---|
| 2196 | 2470 | if (rev < PD_MAX_REV) |
|---|
| 2197 | | - port->negotiated_rev = rev; |
|---|
| 2471 | + port->negotiated_rev = min_t(u16, rev, port->negotiated_rev); |
|---|
| 2198 | 2472 | |
|---|
| 2473 | + if (port->pwr_role == TYPEC_SOURCE) { |
|---|
| 2474 | + if (port->ams == GET_SOURCE_CAPABILITIES) |
|---|
| 2475 | + tcpm_pd_handle_state(port, SRC_READY, NONE_AMS, 0); |
|---|
| 2476 | + /* Unexpected Source Capabilities */ |
|---|
| 2477 | + else |
|---|
| 2478 | + tcpm_pd_handle_msg(port, |
|---|
| 2479 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2480 | + PD_MSG_CTRL_REJECT : |
|---|
| 2481 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2482 | + NONE_AMS); |
|---|
| 2483 | + } else if (port->state == SNK_WAIT_CAPABILITIES) { |
|---|
| 2199 | 2484 | /* |
|---|
| 2200 | 2485 | * This message may be received even if VBUS is not |
|---|
| 2201 | 2486 | * present. This is quite unexpected; see USB PD |
|---|
| .. | .. |
|---|
| 2209 | 2494 | * but be prepared to keep waiting for VBUS after it was |
|---|
| 2210 | 2495 | * handled. |
|---|
| 2211 | 2496 | */ |
|---|
| 2212 | | - tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); |
|---|
| 2497 | + port->ams = POWER_NEGOTIATION; |
|---|
| 2498 | + port->in_ams = true; |
|---|
| 2499 | + tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); |
|---|
| 2500 | + } else { |
|---|
| 2501 | + if (port->ams == GET_SOURCE_CAPABILITIES) |
|---|
| 2502 | + tcpm_ams_finish(port); |
|---|
| 2503 | + tcpm_pd_handle_state(port, SNK_NEGOTIATE_CAPABILITIES, |
|---|
| 2504 | + POWER_NEGOTIATION, 0); |
|---|
| 2505 | + } |
|---|
| 2213 | 2506 | break; |
|---|
| 2214 | 2507 | case PD_DATA_REQUEST: |
|---|
| 2215 | | - if (port->pwr_role != TYPEC_SOURCE || |
|---|
| 2216 | | - cnt != 1) { |
|---|
| 2217 | | - tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
|---|
| 2218 | | - break; |
|---|
| 2219 | | - } |
|---|
| 2220 | | - |
|---|
| 2221 | 2508 | /* |
|---|
| 2222 | 2509 | * Adjust revision in subsequent message headers, as required, |
|---|
| 2223 | 2510 | * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't |
|---|
| 2224 | 2511 | * support Rev 1.0 so just reject in that scenario. |
|---|
| 2225 | 2512 | */ |
|---|
| 2226 | 2513 | if (rev == PD_REV10) { |
|---|
| 2227 | | - tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
|---|
| 2514 | + tcpm_pd_handle_msg(port, |
|---|
| 2515 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2516 | + PD_MSG_CTRL_REJECT : |
|---|
| 2517 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2518 | + NONE_AMS); |
|---|
| 2228 | 2519 | break; |
|---|
| 2229 | 2520 | } |
|---|
| 2230 | 2521 | |
|---|
| 2231 | 2522 | if (rev < PD_MAX_REV) |
|---|
| 2232 | | - port->negotiated_rev = rev; |
|---|
| 2523 | + port->negotiated_rev = min_t(u16, rev, port->negotiated_rev); |
|---|
| 2524 | + |
|---|
| 2525 | + if (port->pwr_role != TYPEC_SOURCE || cnt != 1) { |
|---|
| 2526 | + tcpm_pd_handle_msg(port, |
|---|
| 2527 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2528 | + PD_MSG_CTRL_REJECT : |
|---|
| 2529 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2530 | + NONE_AMS); |
|---|
| 2531 | + break; |
|---|
| 2532 | + } |
|---|
| 2233 | 2533 | |
|---|
| 2234 | 2534 | port->sink_request = le32_to_cpu(msg->payload[0]); |
|---|
| 2235 | 2535 | |
|---|
| 2236 | 2536 | if (port->vdm_sm_running && port->explicit_contract) { |
|---|
| 2237 | | - tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2537 | + tcpm_pd_handle_msg(port, PD_MSG_CTRL_WAIT, port->ams); |
|---|
| 2238 | 2538 | break; |
|---|
| 2239 | 2539 | } |
|---|
| 2240 | 2540 | |
|---|
| 2241 | | - tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0); |
|---|
| 2541 | + if (port->state == SRC_SEND_CAPABILITIES) |
|---|
| 2542 | + tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0); |
|---|
| 2543 | + else |
|---|
| 2544 | + tcpm_pd_handle_state(port, SRC_NEGOTIATE_CAPABILITIES, |
|---|
| 2545 | + POWER_NEGOTIATION, 0); |
|---|
| 2242 | 2546 | break; |
|---|
| 2243 | 2547 | case PD_DATA_SINK_CAP: |
|---|
| 2244 | 2548 | /* We don't do anything with this at the moment... */ |
|---|
| 2245 | 2549 | for (i = 0; i < cnt; i++) |
|---|
| 2246 | 2550 | port->sink_caps[i] = le32_to_cpu(msg->payload[i]); |
|---|
| 2247 | 2551 | |
|---|
| 2248 | | - frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >> |
|---|
| 2552 | + partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >> |
|---|
| 2249 | 2553 | PDO_FIXED_FRS_CURR_SHIFT; |
|---|
| 2250 | | - frs_enable = frs_current && (frs_current <= port->frs_current); |
|---|
| 2554 | + frs_enable = partner_frs_current && (partner_frs_current <= |
|---|
| 2555 | + port->new_source_frs_current); |
|---|
| 2251 | 2556 | tcpm_log(port, |
|---|
| 2252 | 2557 | "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c", |
|---|
| 2253 | | - frs_current, port->frs_current, frs_enable ? 'y' : 'n'); |
|---|
| 2558 | + partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n'); |
|---|
| 2254 | 2559 | if (frs_enable) { |
|---|
| 2255 | 2560 | ret = port->tcpc->enable_frs(port->tcpc, true); |
|---|
| 2256 | 2561 | tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret); |
|---|
| .. | .. |
|---|
| 2258 | 2563 | |
|---|
| 2259 | 2564 | port->nr_sink_caps = cnt; |
|---|
| 2260 | 2565 | port->sink_cap_done = true; |
|---|
| 2261 | | - tcpm_set_state(port, SNK_READY, 0); |
|---|
| 2566 | + if (port->ams == GET_SINK_CAPABILITIES) |
|---|
| 2567 | + tcpm_set_state(port, ready_state(port), 0); |
|---|
| 2568 | + /* Unexpected Sink Capabilities */ |
|---|
| 2569 | + else |
|---|
| 2570 | + tcpm_pd_handle_msg(port, |
|---|
| 2571 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2572 | + PD_MSG_CTRL_REJECT : |
|---|
| 2573 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2574 | + NONE_AMS); |
|---|
| 2262 | 2575 | break; |
|---|
| 2263 | 2576 | case PD_DATA_VENDOR_DEF: |
|---|
| 2264 | 2577 | tcpm_handle_vdm_request(port, msg->payload, cnt); |
|---|
| 2265 | 2578 | break; |
|---|
| 2266 | 2579 | case PD_DATA_BIST: |
|---|
| 2267 | | - if (port->state == SRC_READY || port->state == SNK_READY) { |
|---|
| 2268 | | - port->bist_request = le32_to_cpu(msg->payload[0]); |
|---|
| 2269 | | - tcpm_set_state(port, BIST_RX, 0); |
|---|
| 2270 | | - } |
|---|
| 2580 | + port->bist_request = le32_to_cpu(msg->payload[0]); |
|---|
| 2581 | + tcpm_pd_handle_state(port, BIST_RX, BIST, 0); |
|---|
| 2271 | 2582 | break; |
|---|
| 2272 | 2583 | case PD_DATA_ALERT: |
|---|
| 2273 | | - tcpm_handle_alert(port, msg->payload, cnt); |
|---|
| 2584 | + if (port->state != SRC_READY && port->state != SNK_READY) |
|---|
| 2585 | + tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ? |
|---|
| 2586 | + SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET, |
|---|
| 2587 | + NONE_AMS, 0); |
|---|
| 2588 | + else |
|---|
| 2589 | + tcpm_handle_alert(port, msg->payload, cnt); |
|---|
| 2274 | 2590 | break; |
|---|
| 2275 | 2591 | case PD_DATA_BATT_STATUS: |
|---|
| 2276 | 2592 | case PD_DATA_GET_COUNTRY_INFO: |
|---|
| 2277 | 2593 | /* Currently unsupported */ |
|---|
| 2278 | | - tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); |
|---|
| 2594 | + tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ? |
|---|
| 2595 | + PD_MSG_CTRL_REJECT : |
|---|
| 2596 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2597 | + NONE_AMS); |
|---|
| 2279 | 2598 | break; |
|---|
| 2280 | 2599 | default: |
|---|
| 2281 | | - tcpm_log(port, "Unhandled data message type %#x", type); |
|---|
| 2600 | + tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ? |
|---|
| 2601 | + PD_MSG_CTRL_REJECT : |
|---|
| 2602 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2603 | + NONE_AMS); |
|---|
| 2604 | + tcpm_log(port, "Unrecognized data message type %#x", type); |
|---|
| 2282 | 2605 | break; |
|---|
| 2283 | 2606 | } |
|---|
| 2284 | 2607 | } |
|---|
| .. | .. |
|---|
| 2298 | 2621 | enum pd_ctrl_msg_type type = pd_header_type_le(msg->header); |
|---|
| 2299 | 2622 | enum tcpm_state next_state; |
|---|
| 2300 | 2623 | |
|---|
| 2624 | + /* |
|---|
| 2625 | + * Stop VDM state machine if interrupted by other Messages while NOT_SUPP is allowed in |
|---|
| 2626 | + * VDM AMS if waiting for VDM responses and will be handled later. |
|---|
| 2627 | + */ |
|---|
| 2628 | + if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) { |
|---|
| 2629 | + port->vdm_state = VDM_STATE_ERR_BUSY; |
|---|
| 2630 | + tcpm_ams_finish(port); |
|---|
| 2631 | + mod_vdm_delayed_work(port, 0); |
|---|
| 2632 | + } |
|---|
| 2633 | + |
|---|
| 2301 | 2634 | switch (type) { |
|---|
| 2302 | 2635 | case PD_CTRL_GOOD_CRC: |
|---|
| 2303 | 2636 | case PD_CTRL_PING: |
|---|
| 2304 | 2637 | break; |
|---|
| 2305 | 2638 | case PD_CTRL_GET_SOURCE_CAP: |
|---|
| 2306 | | - switch (port->state) { |
|---|
| 2307 | | - case SRC_READY: |
|---|
| 2308 | | - case SNK_READY: |
|---|
| 2309 | | - tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP); |
|---|
| 2310 | | - break; |
|---|
| 2311 | | - default: |
|---|
| 2312 | | - tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
|---|
| 2313 | | - break; |
|---|
| 2314 | | - } |
|---|
| 2639 | + tcpm_pd_handle_msg(port, PD_MSG_DATA_SOURCE_CAP, GET_SOURCE_CAPABILITIES); |
|---|
| 2315 | 2640 | break; |
|---|
| 2316 | 2641 | case PD_CTRL_GET_SINK_CAP: |
|---|
| 2317 | | - switch (port->state) { |
|---|
| 2318 | | - case SRC_READY: |
|---|
| 2319 | | - case SNK_READY: |
|---|
| 2320 | | - tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP); |
|---|
| 2321 | | - break; |
|---|
| 2322 | | - default: |
|---|
| 2323 | | - tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
|---|
| 2324 | | - break; |
|---|
| 2325 | | - } |
|---|
| 2642 | + tcpm_pd_handle_msg(port, PD_MSG_DATA_SINK_CAP, GET_SINK_CAPABILITIES); |
|---|
| 2326 | 2643 | break; |
|---|
| 2327 | 2644 | case PD_CTRL_GOTO_MIN: |
|---|
| 2328 | 2645 | break; |
|---|
| .. | .. |
|---|
| 2331 | 2648 | case SNK_TRANSITION_SINK: |
|---|
| 2332 | 2649 | if (port->vbus_present) { |
|---|
| 2333 | 2650 | tcpm_set_current_limit(port, |
|---|
| 2334 | | - port->current_limit, |
|---|
| 2335 | | - port->supply_voltage); |
|---|
| 2651 | + port->req_current_limit, |
|---|
| 2652 | + port->req_supply_voltage); |
|---|
| 2336 | 2653 | port->explicit_contract = true; |
|---|
| 2337 | | - /* Set VDM running flag ASAP */ |
|---|
| 2338 | | - if (port->data_role == TYPEC_HOST && |
|---|
| 2339 | | - port->send_discover) |
|---|
| 2340 | | - port->vdm_sm_running = true; |
|---|
| 2654 | + tcpm_set_auto_vbus_discharge_threshold(port, |
|---|
| 2655 | + TYPEC_PWR_MODE_PD, |
|---|
| 2656 | + port->pps_data.active, |
|---|
| 2657 | + port->supply_voltage); |
|---|
| 2341 | 2658 | tcpm_set_state(port, SNK_READY, 0); |
|---|
| 2342 | 2659 | } else { |
|---|
| 2343 | 2660 | /* |
|---|
| .. | .. |
|---|
| 2361 | 2678 | tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0); |
|---|
| 2362 | 2679 | break; |
|---|
| 2363 | 2680 | default: |
|---|
| 2681 | + tcpm_pd_handle_state(port, |
|---|
| 2682 | + port->pwr_role == TYPEC_SOURCE ? |
|---|
| 2683 | + SRC_SOFT_RESET_WAIT_SNK_TX : |
|---|
| 2684 | + SNK_SOFT_RESET, |
|---|
| 2685 | + NONE_AMS, 0); |
|---|
| 2364 | 2686 | break; |
|---|
| 2365 | 2687 | } |
|---|
| 2366 | 2688 | break; |
|---|
| .. | .. |
|---|
| 2370 | 2692 | switch (port->state) { |
|---|
| 2371 | 2693 | case SNK_NEGOTIATE_CAPABILITIES: |
|---|
| 2372 | 2694 | /* USB PD specification, Figure 8-43 */ |
|---|
| 2373 | | - if (port->explicit_contract) { |
|---|
| 2695 | + if (port->explicit_contract) |
|---|
| 2374 | 2696 | next_state = SNK_READY; |
|---|
| 2375 | | - if (port->data_role == TYPEC_HOST && |
|---|
| 2376 | | - port->send_discover) |
|---|
| 2377 | | - port->vdm_sm_running = true; |
|---|
| 2378 | | - } else { |
|---|
| 2697 | + else |
|---|
| 2379 | 2698 | next_state = SNK_WAIT_CAPABILITIES; |
|---|
| 2380 | | - } |
|---|
| 2699 | + |
|---|
| 2700 | + /* Threshold was relaxed before sending Request. Restore it back. */ |
|---|
| 2701 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, |
|---|
| 2702 | + port->pps_data.active, |
|---|
| 2703 | + port->supply_voltage); |
|---|
| 2381 | 2704 | tcpm_set_state(port, next_state, 0); |
|---|
| 2382 | 2705 | break; |
|---|
| 2383 | 2706 | case SNK_NEGOTIATE_PPS_CAPABILITIES: |
|---|
| 2384 | 2707 | /* Revert data back from any requested PPS updates */ |
|---|
| 2385 | | - port->pps_data.out_volt = port->supply_voltage; |
|---|
| 2386 | | - port->pps_data.op_curr = port->current_limit; |
|---|
| 2708 | + port->pps_data.req_out_volt = port->supply_voltage; |
|---|
| 2709 | + port->pps_data.req_op_curr = port->current_limit; |
|---|
| 2387 | 2710 | port->pps_status = (type == PD_CTRL_WAIT ? |
|---|
| 2388 | 2711 | -EAGAIN : -EOPNOTSUPP); |
|---|
| 2389 | 2712 | |
|---|
| 2390 | | - if (port->data_role == TYPEC_HOST && |
|---|
| 2391 | | - port->send_discover) |
|---|
| 2392 | | - port->vdm_sm_running = true; |
|---|
| 2713 | + /* Threshold was relaxed before sending Request. Restore it back. */ |
|---|
| 2714 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, |
|---|
| 2715 | + port->pps_data.active, |
|---|
| 2716 | + port->supply_voltage); |
|---|
| 2393 | 2717 | |
|---|
| 2394 | 2718 | tcpm_set_state(port, SNK_READY, 0); |
|---|
| 2395 | 2719 | break; |
|---|
| .. | .. |
|---|
| 2415 | 2739 | port->sink_cap_done = true; |
|---|
| 2416 | 2740 | tcpm_set_state(port, ready_state(port), 0); |
|---|
| 2417 | 2741 | break; |
|---|
| 2742 | + case SRC_READY: |
|---|
| 2743 | + case SNK_READY: |
|---|
| 2744 | + if (port->vdm_state > VDM_STATE_READY) { |
|---|
| 2745 | + port->vdm_state = VDM_STATE_DONE; |
|---|
| 2746 | + if (tcpm_vdm_ams(port)) |
|---|
| 2747 | + tcpm_ams_finish(port); |
|---|
| 2748 | + mod_vdm_delayed_work(port, 0); |
|---|
| 2749 | + break; |
|---|
| 2750 | + } |
|---|
| 2751 | + fallthrough; |
|---|
| 2418 | 2752 | default: |
|---|
| 2753 | + tcpm_pd_handle_state(port, |
|---|
| 2754 | + port->pwr_role == TYPEC_SOURCE ? |
|---|
| 2755 | + SRC_SOFT_RESET_WAIT_SNK_TX : |
|---|
| 2756 | + SNK_SOFT_RESET, |
|---|
| 2757 | + NONE_AMS, 0); |
|---|
| 2419 | 2758 | break; |
|---|
| 2420 | 2759 | } |
|---|
| 2421 | 2760 | break; |
|---|
| .. | .. |
|---|
| 2427 | 2766 | break; |
|---|
| 2428 | 2767 | case SNK_NEGOTIATE_PPS_CAPABILITIES: |
|---|
| 2429 | 2768 | port->pps_data.active = true; |
|---|
| 2430 | | - port->supply_voltage = port->pps_data.out_volt; |
|---|
| 2431 | | - port->current_limit = port->pps_data.op_curr; |
|---|
| 2769 | + port->pps_data.min_volt = port->pps_data.req_min_volt; |
|---|
| 2770 | + port->pps_data.max_volt = port->pps_data.req_max_volt; |
|---|
| 2771 | + port->pps_data.max_curr = port->pps_data.req_max_curr; |
|---|
| 2772 | + port->req_supply_voltage = port->pps_data.req_out_volt; |
|---|
| 2773 | + port->req_current_limit = port->pps_data.req_op_curr; |
|---|
| 2774 | + power_supply_changed(port->psy); |
|---|
| 2432 | 2775 | tcpm_set_state(port, SNK_TRANSITION_SINK, 0); |
|---|
| 2433 | 2776 | break; |
|---|
| 2434 | 2777 | case SOFT_RESET_SEND: |
|---|
| 2435 | | - port->message_id = 0; |
|---|
| 2436 | | - port->rx_msgid = -1; |
|---|
| 2437 | 2778 | if (port->ams == SOFT_RESET_AMS) |
|---|
| 2438 | 2779 | tcpm_ams_finish(port); |
|---|
| 2439 | 2780 | if (port->pwr_role == TYPEC_SOURCE) { |
|---|
| .. | .. |
|---|
| 2444 | 2785 | } |
|---|
| 2445 | 2786 | break; |
|---|
| 2446 | 2787 | case DR_SWAP_SEND: |
|---|
| 2447 | | - if (port->data_role == TYPEC_DEVICE && |
|---|
| 2448 | | - port->send_discover) |
|---|
| 2449 | | - port->vdm_sm_running = true; |
|---|
| 2450 | | - |
|---|
| 2451 | 2788 | tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0); |
|---|
| 2452 | 2789 | break; |
|---|
| 2453 | 2790 | case PR_SWAP_SEND: |
|---|
| .. | .. |
|---|
| 2460 | 2797 | tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0); |
|---|
| 2461 | 2798 | break; |
|---|
| 2462 | 2799 | default: |
|---|
| 2800 | + tcpm_pd_handle_state(port, |
|---|
| 2801 | + port->pwr_role == TYPEC_SOURCE ? |
|---|
| 2802 | + SRC_SOFT_RESET_WAIT_SNK_TX : |
|---|
| 2803 | + SNK_SOFT_RESET, |
|---|
| 2804 | + NONE_AMS, 0); |
|---|
| 2463 | 2805 | break; |
|---|
| 2464 | 2806 | } |
|---|
| 2465 | 2807 | break; |
|---|
| 2466 | 2808 | case PD_CTRL_SOFT_RESET: |
|---|
| 2809 | + port->ams = SOFT_RESET_AMS; |
|---|
| 2467 | 2810 | tcpm_set_state(port, SOFT_RESET, 0); |
|---|
| 2468 | 2811 | break; |
|---|
| 2469 | 2812 | case PD_CTRL_DR_SWAP: |
|---|
| 2470 | | - if (port->port_type != TYPEC_PORT_DRP) { |
|---|
| 2471 | | - tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
|---|
| 2472 | | - break; |
|---|
| 2473 | | - } |
|---|
| 2474 | 2813 | /* |
|---|
| 2475 | 2814 | * XXX |
|---|
| 2476 | 2815 | * 6.3.9: If an alternate mode is active, a request to swap |
|---|
| 2477 | 2816 | * alternate modes shall trigger a port reset. |
|---|
| 2478 | 2817 | */ |
|---|
| 2479 | | - switch (port->state) { |
|---|
| 2480 | | - case SRC_READY: |
|---|
| 2481 | | - case SNK_READY: |
|---|
| 2482 | | - if (port->vdm_sm_running) { |
|---|
| 2818 | + if (port->typec_caps.data != TYPEC_PORT_DRD) { |
|---|
| 2819 | + tcpm_pd_handle_msg(port, |
|---|
| 2820 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2821 | + PD_MSG_CTRL_REJECT : |
|---|
| 2822 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2823 | + NONE_AMS); |
|---|
| 2824 | + } else { |
|---|
| 2825 | + if (port->send_discover) { |
|---|
| 2483 | 2826 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2484 | 2827 | break; |
|---|
| 2485 | 2828 | } |
|---|
| 2486 | | - tcpm_set_state(port, DR_SWAP_ACCEPT, 0); |
|---|
| 2487 | | - break; |
|---|
| 2488 | | - default: |
|---|
| 2489 | | - tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2490 | | - break; |
|---|
| 2829 | + |
|---|
| 2830 | + tcpm_pd_handle_state(port, DR_SWAP_ACCEPT, DATA_ROLE_SWAP, 0); |
|---|
| 2491 | 2831 | } |
|---|
| 2492 | 2832 | break; |
|---|
| 2493 | 2833 | case PD_CTRL_PR_SWAP: |
|---|
| 2494 | 2834 | if (port->port_type != TYPEC_PORT_DRP) { |
|---|
| 2495 | | - tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
|---|
| 2496 | | - break; |
|---|
| 2497 | | - } |
|---|
| 2498 | | - switch (port->state) { |
|---|
| 2499 | | - case SRC_READY: |
|---|
| 2500 | | - case SNK_READY: |
|---|
| 2501 | | - if (port->vdm_sm_running) { |
|---|
| 2835 | + tcpm_pd_handle_msg(port, |
|---|
| 2836 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2837 | + PD_MSG_CTRL_REJECT : |
|---|
| 2838 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2839 | + NONE_AMS); |
|---|
| 2840 | + } else { |
|---|
| 2841 | + if (port->send_discover) { |
|---|
| 2502 | 2842 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2503 | 2843 | break; |
|---|
| 2504 | 2844 | } |
|---|
| 2505 | | - tcpm_set_state(port, PR_SWAP_ACCEPT, 0); |
|---|
| 2506 | | - break; |
|---|
| 2507 | | - default: |
|---|
| 2508 | | - tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2509 | | - break; |
|---|
| 2845 | + |
|---|
| 2846 | + tcpm_pd_handle_state(port, PR_SWAP_ACCEPT, POWER_ROLE_SWAP, 0); |
|---|
| 2510 | 2847 | } |
|---|
| 2511 | 2848 | break; |
|---|
| 2512 | 2849 | case PD_CTRL_VCONN_SWAP: |
|---|
| 2513 | | - switch (port->state) { |
|---|
| 2514 | | - case SRC_READY: |
|---|
| 2515 | | - case SNK_READY: |
|---|
| 2516 | | - if (port->vdm_sm_running) { |
|---|
| 2517 | | - tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2518 | | - break; |
|---|
| 2519 | | - } |
|---|
| 2520 | | - tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0); |
|---|
| 2521 | | - break; |
|---|
| 2522 | | - default: |
|---|
| 2850 | + if (port->send_discover) { |
|---|
| 2523 | 2851 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
|---|
| 2524 | 2852 | break; |
|---|
| 2525 | 2853 | } |
|---|
| 2854 | + |
|---|
| 2855 | + tcpm_pd_handle_state(port, VCONN_SWAP_ACCEPT, VCONN_SWAP, 0); |
|---|
| 2526 | 2856 | break; |
|---|
| 2527 | 2857 | case PD_CTRL_GET_SOURCE_CAP_EXT: |
|---|
| 2528 | 2858 | case PD_CTRL_GET_STATUS: |
|---|
| .. | .. |
|---|
| 2530 | 2860 | case PD_CTRL_GET_PPS_STATUS: |
|---|
| 2531 | 2861 | case PD_CTRL_GET_COUNTRY_CODES: |
|---|
| 2532 | 2862 | /* Currently not supported */ |
|---|
| 2533 | | - tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); |
|---|
| 2863 | + tcpm_pd_handle_msg(port, |
|---|
| 2864 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2865 | + PD_MSG_CTRL_REJECT : |
|---|
| 2866 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2867 | + NONE_AMS); |
|---|
| 2534 | 2868 | break; |
|---|
| 2535 | 2869 | default: |
|---|
| 2536 | | - tcpm_log(port, "Unhandled ctrl message type %#x", type); |
|---|
| 2870 | + tcpm_pd_handle_msg(port, |
|---|
| 2871 | + port->negotiated_rev < PD_REV30 ? |
|---|
| 2872 | + PD_MSG_CTRL_REJECT : |
|---|
| 2873 | + PD_MSG_CTRL_NOT_SUPP, |
|---|
| 2874 | + NONE_AMS); |
|---|
| 2875 | + tcpm_log(port, "Unrecognized ctrl message type %#x", type); |
|---|
| 2537 | 2876 | break; |
|---|
| 2538 | 2877 | } |
|---|
| 2539 | 2878 | } |
|---|
| .. | .. |
|---|
| 2544 | 2883 | enum pd_ext_msg_type type = pd_header_type_le(msg->header); |
|---|
| 2545 | 2884 | unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header); |
|---|
| 2546 | 2885 | |
|---|
| 2547 | | - if (!(msg->ext_msg.header & PD_EXT_HDR_CHUNKED)) { |
|---|
| 2886 | + /* stopping VDM state machine if interrupted by other Messages */ |
|---|
| 2887 | + if (tcpm_vdm_ams(port)) { |
|---|
| 2888 | + port->vdm_state = VDM_STATE_ERR_BUSY; |
|---|
| 2889 | + tcpm_ams_finish(port); |
|---|
| 2890 | + mod_vdm_delayed_work(port, 0); |
|---|
| 2891 | + } |
|---|
| 2892 | + |
|---|
| 2893 | + if (!(le16_to_cpu(msg->ext_msg.header) & PD_EXT_HDR_CHUNKED)) { |
|---|
| 2894 | + tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
|---|
| 2548 | 2895 | tcpm_log(port, "Unchunked extended messages unsupported"); |
|---|
| 2549 | 2896 | return; |
|---|
| 2550 | 2897 | } |
|---|
| 2551 | 2898 | |
|---|
| 2552 | 2899 | if (data_size > PD_EXT_MAX_CHUNK_DATA) { |
|---|
| 2900 | + tcpm_pd_handle_state(port, CHUNK_NOT_SUPP, NONE_AMS, PD_T_CHUNK_NOT_SUPP); |
|---|
| 2553 | 2901 | tcpm_log(port, "Chunk handling not yet supported"); |
|---|
| 2554 | 2902 | return; |
|---|
| 2555 | 2903 | } |
|---|
| 2556 | 2904 | |
|---|
| 2557 | 2905 | switch (type) { |
|---|
| 2558 | 2906 | case PD_EXT_STATUS: |
|---|
| 2559 | | - /* |
|---|
| 2560 | | - * If PPS related events raised then get PPS status to clear |
|---|
| 2561 | | - * (see USB PD 3.0 Spec, 6.5.2.4) |
|---|
| 2562 | | - */ |
|---|
| 2563 | | - if (msg->ext_msg.data[USB_PD_EXT_SDB_EVENT_FLAGS] & |
|---|
| 2564 | | - USB_PD_EXT_SDB_PPS_EVENTS) |
|---|
| 2565 | | - tcpm_set_state(port, GET_PPS_STATUS_SEND, 0); |
|---|
| 2566 | | - else |
|---|
| 2567 | | - tcpm_set_state(port, ready_state(port), 0); |
|---|
| 2568 | | - break; |
|---|
| 2569 | 2907 | case PD_EXT_PPS_STATUS: |
|---|
| 2570 | | - /* |
|---|
| 2571 | | - * For now the PPS status message is used to clear events |
|---|
| 2572 | | - * and nothing more. |
|---|
| 2573 | | - */ |
|---|
| 2574 | | - tcpm_set_state(port, ready_state(port), 0); |
|---|
| 2908 | + if (port->ams == GETTING_SOURCE_SINK_STATUS) { |
|---|
| 2909 | + tcpm_ams_finish(port); |
|---|
| 2910 | + tcpm_set_state(port, ready_state(port), 0); |
|---|
| 2911 | + } else { |
|---|
| 2912 | + /* unexpected Status or PPS_Status Message */ |
|---|
| 2913 | + tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ? |
|---|
| 2914 | + SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET, |
|---|
| 2915 | + NONE_AMS, 0); |
|---|
| 2916 | + } |
|---|
| 2575 | 2917 | break; |
|---|
| 2576 | 2918 | case PD_EXT_SOURCE_CAP_EXT: |
|---|
| 2577 | 2919 | case PD_EXT_GET_BATT_CAP: |
|---|
| .. | .. |
|---|
| 2585 | 2927 | case PD_EXT_FW_UPDATE_RESPONSE: |
|---|
| 2586 | 2928 | case PD_EXT_COUNTRY_INFO: |
|---|
| 2587 | 2929 | case PD_EXT_COUNTRY_CODES: |
|---|
| 2588 | | - tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); |
|---|
| 2930 | + tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
|---|
| 2589 | 2931 | break; |
|---|
| 2590 | 2932 | default: |
|---|
| 2591 | | - tcpm_log(port, "Unhandled extended message type %#x", type); |
|---|
| 2933 | + tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
|---|
| 2934 | + tcpm_log(port, "Unrecognized extended message type %#x", type); |
|---|
| 2592 | 2935 | break; |
|---|
| 2593 | 2936 | } |
|---|
| 2594 | 2937 | } |
|---|
| .. | .. |
|---|
| 2633 | 2976 | "Data role mismatch, initiating error recovery"); |
|---|
| 2634 | 2977 | tcpm_set_state(port, ERROR_RECOVERY, 0); |
|---|
| 2635 | 2978 | } else { |
|---|
| 2636 | | - if (msg->header & PD_HEADER_EXT_HDR) |
|---|
| 2979 | + if (le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR) |
|---|
| 2637 | 2980 | tcpm_pd_ext_msg_request(port, msg); |
|---|
| 2638 | 2981 | else if (cnt) |
|---|
| 2639 | 2982 | tcpm_pd_data_request(port, msg); |
|---|
| .. | .. |
|---|
| 2684 | 3027 | static bool tcpm_send_queued_message(struct tcpm_port *port) |
|---|
| 2685 | 3028 | { |
|---|
| 2686 | 3029 | enum pd_msg_request queued_message; |
|---|
| 3030 | + int ret; |
|---|
| 2687 | 3031 | |
|---|
| 2688 | 3032 | do { |
|---|
| 2689 | 3033 | queued_message = port->queued_message; |
|---|
| .. | .. |
|---|
| 2700 | 3044 | tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP); |
|---|
| 2701 | 3045 | break; |
|---|
| 2702 | 3046 | case PD_MSG_DATA_SINK_CAP: |
|---|
| 2703 | | - tcpm_pd_send_sink_caps(port); |
|---|
| 3047 | + ret = tcpm_pd_send_sink_caps(port); |
|---|
| 3048 | + if (ret < 0) { |
|---|
| 3049 | + tcpm_log(port, "Unable to send snk caps, ret=%d", ret); |
|---|
| 3050 | + tcpm_set_state(port, SNK_SOFT_RESET, 0); |
|---|
| 3051 | + } |
|---|
| 3052 | + tcpm_ams_finish(port); |
|---|
| 2704 | 3053 | break; |
|---|
| 2705 | 3054 | case PD_MSG_DATA_SOURCE_CAP: |
|---|
| 2706 | | - tcpm_pd_send_source_caps(port); |
|---|
| 3055 | + ret = tcpm_pd_send_source_caps(port); |
|---|
| 3056 | + if (ret < 0) { |
|---|
| 3057 | + tcpm_log(port, |
|---|
| 3058 | + "Unable to send src caps, ret=%d", |
|---|
| 3059 | + ret); |
|---|
| 3060 | + tcpm_set_state(port, SOFT_RESET_SEND, 0); |
|---|
| 3061 | + } else if (port->pwr_role == TYPEC_SOURCE) { |
|---|
| 3062 | + tcpm_ams_finish(port); |
|---|
| 3063 | + tcpm_set_state(port, HARD_RESET_SEND, |
|---|
| 3064 | + PD_T_SENDER_RESPONSE); |
|---|
| 3065 | + } else { |
|---|
| 3066 | + tcpm_ams_finish(port); |
|---|
| 3067 | + } |
|---|
| 2707 | 3068 | break; |
|---|
| 2708 | 3069 | default: |
|---|
| 2709 | 3070 | break; |
|---|
| .. | .. |
|---|
| 2791 | 3152 | |
|---|
| 2792 | 3153 | port->pps_data.supported = false; |
|---|
| 2793 | 3154 | port->usb_type = POWER_SUPPLY_USB_TYPE_PD; |
|---|
| 3155 | + power_supply_changed(port->psy); |
|---|
| 2794 | 3156 | |
|---|
| 2795 | 3157 | /* |
|---|
| 2796 | 3158 | * Select the source PDO providing the most power which has a |
|---|
| .. | .. |
|---|
| 2815 | 3177 | port->pps_data.supported = true; |
|---|
| 2816 | 3178 | port->usb_type = |
|---|
| 2817 | 3179 | POWER_SUPPLY_USB_TYPE_PD_PPS; |
|---|
| 3180 | + power_supply_changed(port->psy); |
|---|
| 2818 | 3181 | } |
|---|
| 2819 | 3182 | continue; |
|---|
| 2820 | 3183 | default: |
|---|
| .. | .. |
|---|
| 2884 | 3247 | unsigned int i, j, max_mw = 0, max_mv = 0; |
|---|
| 2885 | 3248 | unsigned int min_src_mv, max_src_mv, src_ma, src_mw; |
|---|
| 2886 | 3249 | unsigned int min_snk_mv, max_snk_mv; |
|---|
| 2887 | | - u32 pdo; |
|---|
| 3250 | + unsigned int max_op_mv; |
|---|
| 3251 | + u32 pdo, src, snk; |
|---|
| 2888 | 3252 | unsigned int src_pdo = 0, snk_pdo = 0; |
|---|
| 2889 | 3253 | |
|---|
| 2890 | 3254 | /* |
|---|
| .. | .. |
|---|
| 2934 | 3298 | continue; |
|---|
| 2935 | 3299 | } |
|---|
| 2936 | 3300 | |
|---|
| 2937 | | - if (max_src_mv <= max_snk_mv && |
|---|
| 2938 | | - min_src_mv >= min_snk_mv) { |
|---|
| 3301 | + if (min_src_mv <= max_snk_mv && |
|---|
| 3302 | + max_src_mv >= min_snk_mv) { |
|---|
| 3303 | + max_op_mv = min(max_src_mv, max_snk_mv); |
|---|
| 3304 | + src_mw = (max_op_mv * src_ma) / 1000; |
|---|
| 2939 | 3305 | /* Prefer higher voltages if available */ |
|---|
| 2940 | 3306 | if ((src_mw == max_mw && |
|---|
| 2941 | | - min_src_mv > max_mv) || |
|---|
| 3307 | + max_op_mv > max_mv) || |
|---|
| 2942 | 3308 | src_mw > max_mw) { |
|---|
| 2943 | 3309 | src_pdo = i; |
|---|
| 2944 | 3310 | snk_pdo = j; |
|---|
| 2945 | 3311 | max_mw = src_mw; |
|---|
| 2946 | | - max_mv = max_src_mv; |
|---|
| 3312 | + max_mv = max_op_mv; |
|---|
| 2947 | 3313 | } |
|---|
| 2948 | 3314 | } |
|---|
| 2949 | 3315 | } |
|---|
| .. | .. |
|---|
| 2956 | 3322 | } |
|---|
| 2957 | 3323 | |
|---|
| 2958 | 3324 | if (src_pdo) { |
|---|
| 2959 | | - pdo = port->source_caps[src_pdo]; |
|---|
| 3325 | + src = port->source_caps[src_pdo]; |
|---|
| 3326 | + snk = port->snk_pdo[snk_pdo]; |
|---|
| 2960 | 3327 | |
|---|
| 2961 | | - port->pps_data.min_volt = pdo_pps_apdo_min_voltage(pdo); |
|---|
| 2962 | | - port->pps_data.max_volt = pdo_pps_apdo_max_voltage(pdo); |
|---|
| 2963 | | - port->pps_data.max_curr = |
|---|
| 2964 | | - min_pps_apdo_current(pdo, port->snk_pdo[snk_pdo]); |
|---|
| 2965 | | - port->pps_data.out_volt = |
|---|
| 2966 | | - min(pdo_pps_apdo_max_voltage(pdo), port->pps_data.out_volt); |
|---|
| 2967 | | - port->pps_data.op_curr = |
|---|
| 2968 | | - min(port->pps_data.max_curr, port->pps_data.op_curr); |
|---|
| 3328 | + port->pps_data.req_min_volt = max(pdo_pps_apdo_min_voltage(src), |
|---|
| 3329 | + pdo_pps_apdo_min_voltage(snk)); |
|---|
| 3330 | + port->pps_data.req_max_volt = min(pdo_pps_apdo_max_voltage(src), |
|---|
| 3331 | + pdo_pps_apdo_max_voltage(snk)); |
|---|
| 3332 | + port->pps_data.req_max_curr = min_pps_apdo_current(src, snk); |
|---|
| 3333 | + port->pps_data.req_out_volt = min(port->pps_data.req_max_volt, |
|---|
| 3334 | + max(port->pps_data.req_min_volt, |
|---|
| 3335 | + port->pps_data.req_out_volt)); |
|---|
| 3336 | + port->pps_data.req_op_curr = min(port->pps_data.req_max_curr, |
|---|
| 3337 | + port->pps_data.req_op_curr); |
|---|
| 2969 | 3338 | } |
|---|
| 2970 | 3339 | |
|---|
| 2971 | 3340 | return src_pdo; |
|---|
| .. | .. |
|---|
| 3045 | 3414 | flags & RDO_CAP_MISMATCH ? " [mismatch]" : ""); |
|---|
| 3046 | 3415 | } |
|---|
| 3047 | 3416 | |
|---|
| 3048 | | - port->current_limit = ma; |
|---|
| 3049 | | - port->supply_voltage = mv; |
|---|
| 3417 | + port->req_current_limit = ma; |
|---|
| 3418 | + port->req_supply_voltage = mv; |
|---|
| 3050 | 3419 | |
|---|
| 3051 | 3420 | return 0; |
|---|
| 3052 | 3421 | } |
|---|
| .. | .. |
|---|
| 3060 | 3429 | ret = tcpm_pd_build_request(port, &rdo); |
|---|
| 3061 | 3430 | if (ret < 0) |
|---|
| 3062 | 3431 | return ret; |
|---|
| 3432 | + |
|---|
| 3433 | + /* |
|---|
| 3434 | + * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition. |
|---|
| 3435 | + * It is safer to modify the threshold here. |
|---|
| 3436 | + */ |
|---|
| 3437 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); |
|---|
| 3063 | 3438 | |
|---|
| 3064 | 3439 | memset(&msg, 0, sizeof(msg)); |
|---|
| 3065 | 3440 | msg.header = PD_HEADER_LE(PD_DATA_REQUEST, |
|---|
| .. | .. |
|---|
| 3092 | 3467 | tcpm_log(port, "Invalid APDO selected!"); |
|---|
| 3093 | 3468 | return -EINVAL; |
|---|
| 3094 | 3469 | } |
|---|
| 3095 | | - max_mv = port->pps_data.max_volt; |
|---|
| 3096 | | - max_ma = port->pps_data.max_curr; |
|---|
| 3097 | | - out_mv = port->pps_data.out_volt; |
|---|
| 3098 | | - op_ma = port->pps_data.op_curr; |
|---|
| 3470 | + max_mv = port->pps_data.req_max_volt; |
|---|
| 3471 | + max_ma = port->pps_data.req_max_curr; |
|---|
| 3472 | + out_mv = port->pps_data.req_out_volt; |
|---|
| 3473 | + op_ma = port->pps_data.req_op_curr; |
|---|
| 3099 | 3474 | break; |
|---|
| 3100 | 3475 | default: |
|---|
| 3101 | 3476 | tcpm_log(port, "Invalid PDO selected!"); |
|---|
| .. | .. |
|---|
| 3142 | 3517 | tcpm_log(port, "Requesting APDO %d: %u mV, %u mA", |
|---|
| 3143 | 3518 | src_pdo_index, out_mv, op_ma); |
|---|
| 3144 | 3519 | |
|---|
| 3145 | | - port->pps_data.op_curr = op_ma; |
|---|
| 3146 | | - port->pps_data.out_volt = out_mv; |
|---|
| 3520 | + port->pps_data.req_op_curr = op_ma; |
|---|
| 3521 | + port->pps_data.req_out_volt = out_mv; |
|---|
| 3147 | 3522 | |
|---|
| 3148 | 3523 | return 0; |
|---|
| 3149 | 3524 | } |
|---|
| .. | .. |
|---|
| 3157 | 3532 | ret = tcpm_pd_build_pps_request(port, &rdo); |
|---|
| 3158 | 3533 | if (ret < 0) |
|---|
| 3159 | 3534 | return ret; |
|---|
| 3535 | + |
|---|
| 3536 | + /* Relax the threshold as voltage will be adjusted right after Accept Message. */ |
|---|
| 3537 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); |
|---|
| 3160 | 3538 | |
|---|
| 3161 | 3539 | memset(&msg, 0, sizeof(msg)); |
|---|
| 3162 | 3540 | msg.header = PD_HEADER_LE(PD_DATA_REQUEST, |
|---|
| .. | .. |
|---|
| 3178 | 3556 | |
|---|
| 3179 | 3557 | tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge); |
|---|
| 3180 | 3558 | |
|---|
| 3181 | | - ret = tcpm_send_vbus_notify(port, enable); |
|---|
| 3182 | | - if (ret) |
|---|
| 3183 | | - return ret; |
|---|
| 3184 | | - |
|---|
| 3185 | 3559 | ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge); |
|---|
| 3186 | 3560 | if (ret < 0) |
|---|
| 3187 | 3561 | return ret; |
|---|
| .. | .. |
|---|
| 3199 | 3573 | |
|---|
| 3200 | 3574 | if (charge != port->vbus_charge) { |
|---|
| 3201 | 3575 | tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge); |
|---|
| 3202 | | - |
|---|
| 3203 | | - ret = tcpm_send_vbus_notify(port, port->vbus_source); |
|---|
| 3204 | | - if (ret < 0) |
|---|
| 3205 | | - return ret; |
|---|
| 3206 | | - |
|---|
| 3207 | 3576 | ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source, |
|---|
| 3208 | 3577 | charge); |
|---|
| 3209 | 3578 | if (ret < 0) |
|---|
| 3210 | 3579 | return ret; |
|---|
| 3211 | 3580 | } |
|---|
| 3212 | 3581 | port->vbus_charge = charge; |
|---|
| 3582 | + power_supply_changed(port->psy); |
|---|
| 3213 | 3583 | return 0; |
|---|
| 3214 | 3584 | } |
|---|
| 3215 | 3585 | |
|---|
| .. | .. |
|---|
| 3228 | 3598 | static int tcpm_init_vbus(struct tcpm_port *port) |
|---|
| 3229 | 3599 | { |
|---|
| 3230 | 3600 | int ret; |
|---|
| 3231 | | - |
|---|
| 3232 | | - ret = tcpm_send_vbus_notify(port, false); |
|---|
| 3233 | | - if (ret) |
|---|
| 3234 | | - return ret; |
|---|
| 3235 | 3601 | |
|---|
| 3236 | 3602 | ret = port->tcpc->set_vbus(port->tcpc, false, false); |
|---|
| 3237 | 3603 | port->vbus_source = false; |
|---|
| .. | .. |
|---|
| 3280 | 3646 | if (ret < 0) |
|---|
| 3281 | 3647 | return ret; |
|---|
| 3282 | 3648 | |
|---|
| 3283 | | - ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST); |
|---|
| 3649 | + tcpm_enable_auto_vbus_discharge(port, true); |
|---|
| 3650 | + |
|---|
| 3651 | + ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port)); |
|---|
| 3284 | 3652 | if (ret < 0) |
|---|
| 3285 | 3653 | return ret; |
|---|
| 3286 | 3654 | |
|---|
| .. | .. |
|---|
| 3309 | 3677 | port->partner = NULL; |
|---|
| 3310 | 3678 | |
|---|
| 3311 | 3679 | port->attached = true; |
|---|
| 3680 | + port->debouncing = false; |
|---|
| 3312 | 3681 | port->send_discover = true; |
|---|
| 3313 | | - |
|---|
| 3314 | | - dev_info(port->dev, "CC connected in %s as DFP\n", |
|---|
| 3315 | | - polarity ? "CC2" : "CC1"); |
|---|
| 3316 | | - tcpm_send_orientation_notify(port); |
|---|
| 3317 | | - tcpm_send_data_role_notify(port, port->attached, port->data_role); |
|---|
| 3318 | 3682 | |
|---|
| 3319 | 3683 | return 0; |
|---|
| 3320 | 3684 | |
|---|
| .. | .. |
|---|
| 3350 | 3714 | memset(modep, 0, sizeof(*modep)); |
|---|
| 3351 | 3715 | } |
|---|
| 3352 | 3716 | |
|---|
| 3717 | +static void tcpm_set_partner_usb_comm_capable(struct tcpm_port *port, bool capable) |
|---|
| 3718 | +{ |
|---|
| 3719 | + tcpm_log(port, "Setting usb_comm capable %s", capable ? "true" : "false"); |
|---|
| 3720 | + |
|---|
| 3721 | + if (port->tcpc->set_partner_usb_comm_capable) |
|---|
| 3722 | + port->tcpc->set_partner_usb_comm_capable(port->tcpc, capable); |
|---|
| 3723 | +} |
|---|
| 3724 | + |
|---|
| 3353 | 3725 | static void tcpm_reset_port(struct tcpm_port *port) |
|---|
| 3354 | 3726 | { |
|---|
| 3727 | + tcpm_enable_auto_vbus_discharge(port, false); |
|---|
| 3355 | 3728 | port->in_ams = false; |
|---|
| 3356 | 3729 | port->ams = NONE_AMS; |
|---|
| 3357 | 3730 | port->vdm_sm_running = false; |
|---|
| 3358 | 3731 | tcpm_unregister_altmodes(port); |
|---|
| 3359 | 3732 | tcpm_typec_disconnect(port); |
|---|
| 3360 | | - if (port->attached) { |
|---|
| 3361 | | - port->attached = false; |
|---|
| 3362 | | - if (port->dp_configured) { |
|---|
| 3363 | | - port->dp_configured = false; |
|---|
| 3364 | | - port->dp_pin_assignment = 0; |
|---|
| 3365 | | - port->dp_status = 0; |
|---|
| 3366 | | - tcpm_send_dp_notify(port); |
|---|
| 3367 | | - } |
|---|
| 3368 | | - tcpm_send_data_role_notify(port, port->attached, |
|---|
| 3369 | | - port->data_role); |
|---|
| 3370 | | - } |
|---|
| 3733 | + port->attached = false; |
|---|
| 3371 | 3734 | port->pd_capable = false; |
|---|
| 3372 | 3735 | port->pps_data.supported = false; |
|---|
| 3736 | + tcpm_set_partner_usb_comm_capable(port, false); |
|---|
| 3373 | 3737 | |
|---|
| 3374 | 3738 | /* |
|---|
| 3375 | 3739 | * First Rx ID should be 0; set this to a sentinel of -1 so that |
|---|
| .. | .. |
|---|
| 3388 | 3752 | port->try_src_count = 0; |
|---|
| 3389 | 3753 | port->try_snk_count = 0; |
|---|
| 3390 | 3754 | port->usb_type = POWER_SUPPLY_USB_TYPE_C; |
|---|
| 3755 | + power_supply_changed(port->psy); |
|---|
| 3391 | 3756 | port->nr_sink_caps = 0; |
|---|
| 3392 | 3757 | port->sink_cap_done = false; |
|---|
| 3393 | 3758 | if (port->tcpc->enable_frs) |
|---|
| 3394 | 3759 | port->tcpc->enable_frs(port->tcpc, false); |
|---|
| 3395 | | - |
|---|
| 3396 | | - power_supply_changed(port->psy); |
|---|
| 3397 | 3760 | } |
|---|
| 3398 | 3761 | |
|---|
| 3399 | 3762 | static void tcpm_detach(struct tcpm_port *port) |
|---|
| .. | .. |
|---|
| 3403 | 3766 | |
|---|
| 3404 | 3767 | if (!port->attached) |
|---|
| 3405 | 3768 | return; |
|---|
| 3769 | + |
|---|
| 3770 | + if (port->tcpc->set_bist_data) { |
|---|
| 3771 | + tcpm_log(port, "disable BIST MODE TESTDATA"); |
|---|
| 3772 | + port->tcpc->set_bist_data(port->tcpc, false); |
|---|
| 3773 | + } |
|---|
| 3406 | 3774 | |
|---|
| 3407 | 3775 | tcpm_reset_port(port); |
|---|
| 3408 | 3776 | } |
|---|
| .. | .. |
|---|
| 3424 | 3792 | if (ret < 0) |
|---|
| 3425 | 3793 | return ret; |
|---|
| 3426 | 3794 | |
|---|
| 3427 | | - ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE); |
|---|
| 3795 | + tcpm_enable_auto_vbus_discharge(port, true); |
|---|
| 3796 | + |
|---|
| 3797 | + ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port)); |
|---|
| 3428 | 3798 | if (ret < 0) |
|---|
| 3429 | 3799 | return ret; |
|---|
| 3430 | 3800 | |
|---|
| .. | .. |
|---|
| 3433 | 3803 | port->partner = NULL; |
|---|
| 3434 | 3804 | |
|---|
| 3435 | 3805 | port->attached = true; |
|---|
| 3806 | + port->debouncing = false; |
|---|
| 3436 | 3807 | port->send_discover = true; |
|---|
| 3437 | | - |
|---|
| 3438 | | - dev_info(port->dev, "CC connected in %s as UFP\n", |
|---|
| 3439 | | - port->cc1 != TYPEC_CC_OPEN ? "CC1" : "CC2"); |
|---|
| 3440 | | - tcpm_send_orientation_notify(port); |
|---|
| 3441 | | - tcpm_send_data_role_notify(port, port->attached, port->data_role); |
|---|
| 3442 | 3808 | |
|---|
| 3443 | 3809 | return 0; |
|---|
| 3444 | 3810 | } |
|---|
| .. | .. |
|---|
| 3455 | 3821 | if (port->attached) |
|---|
| 3456 | 3822 | return 0; |
|---|
| 3457 | 3823 | |
|---|
| 3458 | | - ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST); |
|---|
| 3824 | + ret = tcpm_set_roles(port, true, TYPEC_SOURCE, |
|---|
| 3825 | + tcpm_data_role_for_source(port)); |
|---|
| 3459 | 3826 | if (ret < 0) |
|---|
| 3460 | 3827 | return ret; |
|---|
| 3461 | 3828 | |
|---|
| .. | .. |
|---|
| 3464 | 3831 | tcpm_typec_connect(port); |
|---|
| 3465 | 3832 | |
|---|
| 3466 | 3833 | port->attached = true; |
|---|
| 3467 | | - |
|---|
| 3468 | | - dev_info(port->dev, "CC connected as Audio Accessory\n"); |
|---|
| 3469 | | - tcpm_send_orientation_notify(port); |
|---|
| 3470 | | - tcpm_send_data_role_notify(port, port->attached, port->data_role); |
|---|
| 3834 | + port->debouncing = false; |
|---|
| 3471 | 3835 | |
|---|
| 3472 | 3836 | return 0; |
|---|
| 3473 | 3837 | } |
|---|
| .. | .. |
|---|
| 3504 | 3868 | return SNK_UNATTACHED; |
|---|
| 3505 | 3869 | } |
|---|
| 3506 | 3870 | |
|---|
| 3507 | | -static void tcpm_check_send_discover(struct tcpm_port *port) |
|---|
| 3871 | +bool tcpm_is_toggling(struct tcpm_port *port) |
|---|
| 3508 | 3872 | { |
|---|
| 3509 | | - if (port->data_role == TYPEC_HOST && port->send_discover && |
|---|
| 3510 | | - port->pd_capable) { |
|---|
| 3511 | | - tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0); |
|---|
| 3512 | | - port->send_discover = false; |
|---|
| 3513 | | - } |
|---|
| 3873 | + if (port->port_type == TYPEC_PORT_DRP) |
|---|
| 3874 | + return port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED || |
|---|
| 3875 | + port->state == TOGGLING; |
|---|
| 3876 | + |
|---|
| 3877 | + return false; |
|---|
| 3514 | 3878 | } |
|---|
| 3879 | +EXPORT_SYMBOL_GPL(tcpm_is_toggling); |
|---|
| 3515 | 3880 | |
|---|
| 3516 | 3881 | static void tcpm_swap_complete(struct tcpm_port *port, int result) |
|---|
| 3517 | 3882 | { |
|---|
| .. | .. |
|---|
| 3540 | 3905 | { |
|---|
| 3541 | 3906 | int ret; |
|---|
| 3542 | 3907 | enum typec_pwr_opmode opmode; |
|---|
| 3543 | | - unsigned int msecs; |
|---|
| 3908 | + unsigned int msecs, timer_val_msecs; |
|---|
| 3544 | 3909 | enum tcpm_state upcoming_state; |
|---|
| 3910 | + const char *state_name; |
|---|
| 3911 | + u32 current_limit; |
|---|
| 3912 | + bool adjust; |
|---|
| 3545 | 3913 | |
|---|
| 3546 | 3914 | port->enter_state = port->state; |
|---|
| 3547 | 3915 | switch (port->state) { |
|---|
| .. | .. |
|---|
| 3552 | 3920 | if (!port->non_pd_role_swap) |
|---|
| 3553 | 3921 | tcpm_swap_complete(port, -ENOTCONN); |
|---|
| 3554 | 3922 | tcpm_src_detach(port); |
|---|
| 3923 | + if (port->debouncing) { |
|---|
| 3924 | + port->debouncing = false; |
|---|
| 3925 | + if (port->tcpc->check_contaminant && |
|---|
| 3926 | + port->tcpc->check_contaminant(port->tcpc)) { |
|---|
| 3927 | + /* Contaminant detection would handle toggling */ |
|---|
| 3928 | + tcpm_set_state(port, TOGGLING, 0); |
|---|
| 3929 | + break; |
|---|
| 3930 | + } |
|---|
| 3931 | + } |
|---|
| 3555 | 3932 | if (tcpm_start_toggling(port, tcpm_rp_cc(port))) { |
|---|
| 3556 | 3933 | tcpm_set_state(port, TOGGLING, 0); |
|---|
| 3557 | 3934 | break; |
|---|
| .. | .. |
|---|
| 3561 | 3938 | tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK); |
|---|
| 3562 | 3939 | break; |
|---|
| 3563 | 3940 | case SRC_ATTACH_WAIT: |
|---|
| 3941 | + port->debouncing = true; |
|---|
| 3942 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 3943 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SRC_ATTACH_WAIT], |
|---|
| 3944 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 3564 | 3945 | if (tcpm_port_is_debug(port)) |
|---|
| 3565 | 3946 | tcpm_set_state(port, DEBUG_ACC_ATTACHED, |
|---|
| 3566 | | - PD_T_CC_DEBOUNCE); |
|---|
| 3947 | + timer_val_msecs); |
|---|
| 3567 | 3948 | else if (tcpm_port_is_audio(port)) |
|---|
| 3568 | 3949 | tcpm_set_state(port, AUDIO_ACC_ATTACHED, |
|---|
| 3569 | | - PD_T_CC_DEBOUNCE); |
|---|
| 3570 | | - else if (tcpm_port_is_source(port)) |
|---|
| 3950 | + timer_val_msecs); |
|---|
| 3951 | + else if (tcpm_port_is_source(port) && port->vbus_vsafe0v) |
|---|
| 3571 | 3952 | tcpm_set_state(port, |
|---|
| 3572 | 3953 | tcpm_try_snk(port) ? SNK_TRY |
|---|
| 3573 | 3954 | : SRC_ATTACHED, |
|---|
| 3574 | | - PD_T_CC_DEBOUNCE); |
|---|
| 3955 | + timer_val_msecs); |
|---|
| 3575 | 3956 | break; |
|---|
| 3576 | 3957 | |
|---|
| 3577 | 3958 | case SNK_TRY: |
|---|
| 3959 | + port->debouncing = false; |
|---|
| 3578 | 3960 | port->try_snk_count++; |
|---|
| 3579 | 3961 | /* |
|---|
| 3580 | 3962 | * Requirements: |
|---|
| .. | .. |
|---|
| 3597 | 3979 | break; |
|---|
| 3598 | 3980 | case SNK_TRY_WAIT_DEBOUNCE: |
|---|
| 3599 | 3981 | tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, |
|---|
| 3600 | | - PD_T_PD_DEBOUNCE); |
|---|
| 3982 | + PD_T_TRY_CC_DEBOUNCE); |
|---|
| 3601 | 3983 | break; |
|---|
| 3602 | 3984 | case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: |
|---|
| 3603 | | - if (port->vbus_present && tcpm_port_is_sink(port)) { |
|---|
| 3985 | + if (port->vbus_present && tcpm_port_is_sink(port)) |
|---|
| 3604 | 3986 | tcpm_set_state(port, SNK_ATTACHED, 0); |
|---|
| 3605 | | - } else { |
|---|
| 3606 | | - tcpm_set_state(port, SRC_TRYWAIT, 0); |
|---|
| 3987 | + else |
|---|
| 3607 | 3988 | port->max_wait = 0; |
|---|
| 3608 | | - } |
|---|
| 3609 | 3989 | break; |
|---|
| 3610 | 3990 | case SRC_TRYWAIT: |
|---|
| 3611 | 3991 | tcpm_set_cc(port, tcpm_rp_cc(port)); |
|---|
| .. | .. |
|---|
| 3624 | 4004 | } |
|---|
| 3625 | 4005 | break; |
|---|
| 3626 | 4006 | case SRC_TRYWAIT_DEBOUNCE: |
|---|
| 3627 | | - tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE); |
|---|
| 4007 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 4008 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SRC_TRYWAIT_DEBOUNCE], |
|---|
| 4009 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 4010 | + tcpm_set_state(port, SRC_ATTACHED, timer_val_msecs); |
|---|
| 3628 | 4011 | break; |
|---|
| 3629 | 4012 | case SRC_TRYWAIT_UNATTACHED: |
|---|
| 3630 | 4013 | tcpm_set_state(port, SNK_UNATTACHED, 0); |
|---|
| .. | .. |
|---|
| 3640 | 4023 | typec_set_pwr_opmode(port->typec_port, opmode); |
|---|
| 3641 | 4024 | port->pwr_opmode = TYPEC_PWR_MODE_USB; |
|---|
| 3642 | 4025 | port->caps_count = 0; |
|---|
| 3643 | | - port->negotiated_rev = PD_MAX_REV; |
|---|
| 4026 | + port->negotiated_rev = (((port->typec_caps.pd_revision >> 8) & 0xff) - 1); |
|---|
| 3644 | 4027 | port->message_id = 0; |
|---|
| 3645 | 4028 | port->rx_msgid = -1; |
|---|
| 3646 | 4029 | port->explicit_contract = false; |
|---|
| .. | .. |
|---|
| 3710 | 4093 | } |
|---|
| 3711 | 4094 | } else { |
|---|
| 3712 | 4095 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); |
|---|
| 4096 | + tcpm_set_partner_usb_comm_capable(port, |
|---|
| 4097 | + !!(port->sink_request & RDO_USB_COMM)); |
|---|
| 3713 | 4098 | tcpm_set_state(port, SRC_TRANSITION_SUPPLY, |
|---|
| 3714 | 4099 | PD_T_SRC_TRANSITION); |
|---|
| 3715 | 4100 | } |
|---|
| .. | .. |
|---|
| 3733 | 4118 | |
|---|
| 3734 | 4119 | if (port->ams != NONE_AMS) |
|---|
| 3735 | 4120 | tcpm_ams_finish(port); |
|---|
| 4121 | + if (port->next_ams != NONE_AMS) { |
|---|
| 4122 | + port->ams = port->next_ams; |
|---|
| 4123 | + port->next_ams = NONE_AMS; |
|---|
| 4124 | + } |
|---|
| 4125 | + |
|---|
| 3736 | 4126 | /* |
|---|
| 3737 | 4127 | * If previous AMS is interrupted, switch to the upcoming |
|---|
| 3738 | 4128 | * state. |
|---|
| .. | .. |
|---|
| 3744 | 4134 | break; |
|---|
| 3745 | 4135 | } |
|---|
| 3746 | 4136 | |
|---|
| 3747 | | - tcpm_check_send_discover(port); |
|---|
| 4137 | + /* |
|---|
| 4138 | + * 6.4.4.3.1 Discover Identity |
|---|
| 4139 | + * "The Discover Identity Command Shall only be sent to SOP when there is an |
|---|
| 4140 | + * Explicit Contract." |
|---|
| 4141 | + * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using |
|---|
| 4142 | + * port->explicit_contract to decide whether to send the command. |
|---|
| 4143 | + */ |
|---|
| 4144 | + if (port->explicit_contract) |
|---|
| 4145 | + mod_send_discover_delayed_work(port, 0); |
|---|
| 4146 | + else |
|---|
| 4147 | + port->send_discover = false; |
|---|
| 4148 | + |
|---|
| 3748 | 4149 | /* |
|---|
| 3749 | 4150 | * 6.3.5 |
|---|
| 3750 | 4151 | * Sending ping messages is not necessary if |
|---|
| .. | .. |
|---|
| 3769 | 4170 | tcpm_swap_complete(port, -ENOTCONN); |
|---|
| 3770 | 4171 | tcpm_pps_complete(port, -ENOTCONN); |
|---|
| 3771 | 4172 | tcpm_snk_detach(port); |
|---|
| 4173 | + if (port->debouncing) { |
|---|
| 4174 | + port->debouncing = false; |
|---|
| 4175 | + if (port->tcpc->check_contaminant && |
|---|
| 4176 | + port->tcpc->check_contaminant(port->tcpc)) { |
|---|
| 4177 | + /* Contaminant detection would handle toggling */ |
|---|
| 4178 | + tcpm_set_state(port, TOGGLING, 0); |
|---|
| 4179 | + break; |
|---|
| 4180 | + } |
|---|
| 4181 | + } |
|---|
| 3772 | 4182 | if (tcpm_start_toggling(port, TYPEC_CC_RD)) { |
|---|
| 3773 | 4183 | tcpm_set_state(port, TOGGLING, 0); |
|---|
| 3774 | 4184 | break; |
|---|
| .. | .. |
|---|
| 3778 | 4188 | tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC); |
|---|
| 3779 | 4189 | break; |
|---|
| 3780 | 4190 | case SNK_ATTACH_WAIT: |
|---|
| 4191 | + port->debouncing = true; |
|---|
| 4192 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 4193 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SNK_ATTACH_WAIT], |
|---|
| 4194 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 3781 | 4195 | if ((port->cc1 == TYPEC_CC_OPEN && |
|---|
| 3782 | 4196 | port->cc2 != TYPEC_CC_OPEN) || |
|---|
| 3783 | 4197 | (port->cc1 != TYPEC_CC_OPEN && |
|---|
| 3784 | 4198 | port->cc2 == TYPEC_CC_OPEN)) |
|---|
| 3785 | 4199 | tcpm_set_state(port, SNK_DEBOUNCED, |
|---|
| 3786 | | - PD_T_CC_DEBOUNCE); |
|---|
| 4200 | + timer_val_msecs); |
|---|
| 3787 | 4201 | else if (tcpm_port_is_disconnected(port)) |
|---|
| 3788 | 4202 | tcpm_set_state(port, SNK_UNATTACHED, |
|---|
| 3789 | | - PD_T_PD_DEBOUNCE); |
|---|
| 4203 | + timer_val_msecs); |
|---|
| 3790 | 4204 | break; |
|---|
| 3791 | 4205 | case SNK_DEBOUNCED: |
|---|
| 3792 | | - if (tcpm_port_is_disconnected(port)) |
|---|
| 4206 | + if (tcpm_port_is_disconnected(port)) { |
|---|
| 3793 | 4207 | tcpm_set_state(port, SNK_UNATTACHED, |
|---|
| 3794 | 4208 | PD_T_PD_DEBOUNCE); |
|---|
| 3795 | | - else if (port->vbus_present) |
|---|
| 4209 | + } else if (port->vbus_present) { |
|---|
| 3796 | 4210 | tcpm_set_state(port, |
|---|
| 3797 | 4211 | tcpm_try_src(port) ? SRC_TRY |
|---|
| 3798 | 4212 | : SNK_ATTACHED, |
|---|
| 3799 | 4213 | 0); |
|---|
| 4214 | + port->debouncing = false; |
|---|
| 4215 | + } else { |
|---|
| 4216 | + port->debouncing = false; |
|---|
| 4217 | + } |
|---|
| 3800 | 4218 | break; |
|---|
| 3801 | 4219 | case SRC_TRY: |
|---|
| 3802 | 4220 | port->try_src_count++; |
|---|
| .. | .. |
|---|
| 3822 | 4240 | tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE); |
|---|
| 3823 | 4241 | break; |
|---|
| 3824 | 4242 | case SNK_TRYWAIT: |
|---|
| 4243 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 4244 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SNK_TRYWAIT], |
|---|
| 4245 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 3825 | 4246 | tcpm_set_cc(port, TYPEC_CC_RD); |
|---|
| 3826 | | - tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE); |
|---|
| 4247 | + tcpm_set_state(port, SNK_TRYWAIT_VBUS, timer_val_msecs); |
|---|
| 3827 | 4248 | break; |
|---|
| 3828 | 4249 | case SNK_TRYWAIT_VBUS: |
|---|
| 3829 | 4250 | /* |
|---|
| .. | .. |
|---|
| 3853 | 4274 | port->cc2 : port->cc1); |
|---|
| 3854 | 4275 | typec_set_pwr_opmode(port->typec_port, opmode); |
|---|
| 3855 | 4276 | port->pwr_opmode = TYPEC_PWR_MODE_USB; |
|---|
| 3856 | | - port->negotiated_rev = PD_MAX_REV; |
|---|
| 4277 | + port->negotiated_rev = (((port->typec_caps.pd_revision >> 8) & 0xff) - 1); |
|---|
| 3857 | 4278 | port->message_id = 0; |
|---|
| 3858 | 4279 | port->rx_msgid = -1; |
|---|
| 3859 | 4280 | port->explicit_contract = false; |
|---|
| .. | .. |
|---|
| 3863 | 4284 | /* SRC -> SNK POWER/FAST_ROLE_SWAP finished */ |
|---|
| 3864 | 4285 | tcpm_ams_finish(port); |
|---|
| 3865 | 4286 | |
|---|
| 3866 | | - tcpm_set_state(port, SNK_DISCOVERY, 0); |
|---|
| 4287 | + timer_val_msecs = 0; |
|---|
| 4288 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SNK_STARTUP], |
|---|
| 4289 | + SINK_DISCOVERY_BC12, &timer_val_msecs); |
|---|
| 4290 | + tcpm_set_state(port, SNK_DISCOVERY, timer_val_msecs); |
|---|
| 3867 | 4291 | break; |
|---|
| 3868 | 4292 | case SNK_DISCOVERY: |
|---|
| 3869 | 4293 | if (port->vbus_present) { |
|---|
| 3870 | | - tcpm_set_current_limit(port, |
|---|
| 3871 | | - tcpm_get_current_limit(port), |
|---|
| 3872 | | - 5000); |
|---|
| 4294 | + current_limit = tcpm_get_current_limit(port); |
|---|
| 4295 | + trace_android_vh_typec_tcpm_adj_current_limit(tcpm_states[SNK_DISCOVERY], |
|---|
| 4296 | + port->current_limit, |
|---|
| 4297 | + port->supply_voltage, |
|---|
| 4298 | + port->pd_capable, |
|---|
| 4299 | + ¤t_limit, &adjust); |
|---|
| 4300 | + if (port->slow_charger_loop && (current_limit > PD_P_SNK_STDBY_MW / 5)) |
|---|
| 4301 | + current_limit = PD_P_SNK_STDBY_MW / 5; |
|---|
| 4302 | + tcpm_set_current_limit(port, current_limit, 5000); |
|---|
| 3873 | 4303 | tcpm_set_charge(port, true); |
|---|
| 3874 | 4304 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); |
|---|
| 3875 | 4305 | break; |
|---|
| .. | .. |
|---|
| 3884 | 4314 | PD_T_DB_DETECT : PD_T_NO_RESPONSE); |
|---|
| 3885 | 4315 | break; |
|---|
| 3886 | 4316 | case SNK_DISCOVERY_DEBOUNCE: |
|---|
| 3887 | | - tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE, |
|---|
| 3888 | | - PD_T_CC_DEBOUNCE); |
|---|
| 4317 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 4318 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SNK_DISCOVERY_DEBOUNCE], |
|---|
| 4319 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 4320 | + tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE, timer_val_msecs); |
|---|
| 3889 | 4321 | break; |
|---|
| 3890 | 4322 | case SNK_DISCOVERY_DEBOUNCE_DONE: |
|---|
| 3891 | 4323 | if (!tcpm_port_is_disconnected(port) && |
|---|
| .. | .. |
|---|
| 3898 | 4330 | tcpm_set_state(port, unattached_state(port), 0); |
|---|
| 3899 | 4331 | break; |
|---|
| 3900 | 4332 | case SNK_WAIT_CAPABILITIES: |
|---|
| 3901 | | - ret = port->tcpc->set_pd_rx(port->tcpc, true); |
|---|
| 3902 | | - if (ret < 0) { |
|---|
| 3903 | | - tcpm_set_state(port, SNK_READY, 0); |
|---|
| 3904 | | - break; |
|---|
| 4333 | + if (port->prev_state != SOFT_RESET_SEND) { |
|---|
| 4334 | + ret = port->tcpc->set_pd_rx(port->tcpc, true); |
|---|
| 4335 | + if (ret < 0) { |
|---|
| 4336 | + tcpm_set_state(port, SNK_READY, 0); |
|---|
| 4337 | + break; |
|---|
| 4338 | + } |
|---|
| 3905 | 4339 | } |
|---|
| 4340 | + timer_val_msecs = PD_T_SINK_WAIT_CAP; |
|---|
| 4341 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SNK_WAIT_CAPABILITIES], |
|---|
| 4342 | + SINK_WAIT_CAP, &timer_val_msecs); |
|---|
| 3906 | 4343 | /* |
|---|
| 3907 | 4344 | * If VBUS has never been low, and we time out waiting |
|---|
| 3908 | 4345 | * for source cap, try a soft reset first, in case we |
|---|
| .. | .. |
|---|
| 3912 | 4349 | if (port->vbus_never_low) { |
|---|
| 3913 | 4350 | port->vbus_never_low = false; |
|---|
| 3914 | 4351 | tcpm_set_state(port, SNK_SOFT_RESET, |
|---|
| 3915 | | - PD_T_SINK_WAIT_CAP); |
|---|
| 4352 | + timer_val_msecs); |
|---|
| 3916 | 4353 | } else { |
|---|
| 3917 | 4354 | tcpm_set_state(port, hard_reset_state(port), |
|---|
| 3918 | | - PD_T_SINK_WAIT_CAP); |
|---|
| 4355 | + timer_val_msecs); |
|---|
| 3919 | 4356 | } |
|---|
| 3920 | 4357 | break; |
|---|
| 3921 | 4358 | case SNK_NEGOTIATE_CAPABILITIES: |
|---|
| 3922 | 4359 | port->pd_capable = true; |
|---|
| 4360 | + tcpm_set_partner_usb_comm_capable(port, |
|---|
| 4361 | + !!(port->source_caps[0] & PDO_FIXED_USB_COMM)); |
|---|
| 3923 | 4362 | port->hard_reset_count = 0; |
|---|
| 3924 | 4363 | ret = tcpm_pd_send_request(port); |
|---|
| 3925 | 4364 | if (ret < 0) { |
|---|
| 4365 | + /* Restore back to the original state */ |
|---|
| 4366 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, |
|---|
| 4367 | + port->pps_data.active, |
|---|
| 4368 | + port->supply_voltage); |
|---|
| 3926 | 4369 | /* Let the Source send capabilities again. */ |
|---|
| 3927 | 4370 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); |
|---|
| 3928 | 4371 | } else { |
|---|
| .. | .. |
|---|
| 3933 | 4376 | case SNK_NEGOTIATE_PPS_CAPABILITIES: |
|---|
| 3934 | 4377 | ret = tcpm_pd_send_pps_request(port); |
|---|
| 3935 | 4378 | if (ret < 0) { |
|---|
| 4379 | + /* Restore back to the original state */ |
|---|
| 4380 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, |
|---|
| 4381 | + port->pps_data.active, |
|---|
| 4382 | + port->supply_voltage); |
|---|
| 3936 | 4383 | port->pps_status = ret; |
|---|
| 3937 | 4384 | /* |
|---|
| 3938 | 4385 | * If this was called due to updates to sink |
|---|
| .. | .. |
|---|
| 3949 | 4396 | } |
|---|
| 3950 | 4397 | break; |
|---|
| 3951 | 4398 | case SNK_TRANSITION_SINK: |
|---|
| 4399 | + /* From the USB PD spec: |
|---|
| 4400 | + * "The Sink Shall transition to Sink Standby before a positive or |
|---|
| 4401 | + * negative voltage transition of VBUS. During Sink Standby |
|---|
| 4402 | + * the Sink Shall reduce its power draw to pSnkStdby." |
|---|
| 4403 | + * |
|---|
| 4404 | + * This is not applicable to PPS though as the port can continue |
|---|
| 4405 | + * to draw negotiated power without switching to standby. |
|---|
| 4406 | + */ |
|---|
| 4407 | + if (port->supply_voltage != port->req_supply_voltage && !port->pps_data.active && |
|---|
| 4408 | + port->current_limit * port->supply_voltage / 1000 > PD_P_SNK_STDBY_MW) { |
|---|
| 4409 | + u32 stdby_ma = PD_P_SNK_STDBY_MW * 1000 / port->supply_voltage; |
|---|
| 4410 | + |
|---|
| 4411 | + tcpm_log(port, "Setting standby current %u mV @ %u mA", |
|---|
| 4412 | + port->supply_voltage, stdby_ma); |
|---|
| 4413 | + tcpm_set_current_limit(port, stdby_ma, port->supply_voltage); |
|---|
| 4414 | + } |
|---|
| 4415 | + fallthrough; |
|---|
| 3952 | 4416 | case SNK_TRANSITION_SINK_VBUS: |
|---|
| 3953 | 4417 | tcpm_set_state(port, hard_reset_state(port), |
|---|
| 3954 | 4418 | PD_T_PS_TRANSITION); |
|---|
| .. | .. |
|---|
| 3962 | 4426 | port->pwr_opmode = TYPEC_PWR_MODE_PD; |
|---|
| 3963 | 4427 | } |
|---|
| 3964 | 4428 | |
|---|
| 4429 | + current_limit = tcpm_get_current_limit(port); |
|---|
| 4430 | + adjust = false; |
|---|
| 4431 | + trace_android_vh_typec_tcpm_adj_current_limit(tcpm_states[SNK_READY], |
|---|
| 4432 | + port->current_limit, |
|---|
| 4433 | + port->supply_voltage, |
|---|
| 4434 | + port->pd_capable, |
|---|
| 4435 | + ¤t_limit, |
|---|
| 4436 | + &adjust); |
|---|
| 4437 | + if (adjust) |
|---|
| 4438 | + tcpm_set_current_limit(port, current_limit, 5000); |
|---|
| 4439 | + |
|---|
| 4440 | + if (!port->pd_capable && port->slow_charger_loop) |
|---|
| 4441 | + tcpm_set_current_limit(port, tcpm_get_current_limit(port), 5000); |
|---|
| 3965 | 4442 | tcpm_swap_complete(port, 0); |
|---|
| 3966 | 4443 | tcpm_typec_connect(port); |
|---|
| 3967 | 4444 | mod_enable_frs_delayed_work(port, 0); |
|---|
| .. | .. |
|---|
| 3969 | 4446 | |
|---|
| 3970 | 4447 | if (port->ams != NONE_AMS) |
|---|
| 3971 | 4448 | tcpm_ams_finish(port); |
|---|
| 4449 | + if (port->next_ams != NONE_AMS) { |
|---|
| 4450 | + port->ams = port->next_ams; |
|---|
| 4451 | + port->next_ams = NONE_AMS; |
|---|
| 4452 | + } |
|---|
| 4453 | + |
|---|
| 3972 | 4454 | /* |
|---|
| 3973 | 4455 | * If previous AMS is interrupted, switch to the upcoming |
|---|
| 3974 | 4456 | * state. |
|---|
| .. | .. |
|---|
| 3980 | 4462 | break; |
|---|
| 3981 | 4463 | } |
|---|
| 3982 | 4464 | |
|---|
| 3983 | | - tcpm_check_send_discover(port); |
|---|
| 4465 | + /* |
|---|
| 4466 | + * 6.4.4.3.1 Discover Identity |
|---|
| 4467 | + * "The Discover Identity Command Shall only be sent to SOP when there is an |
|---|
| 4468 | + * Explicit Contract." |
|---|
| 4469 | + * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using |
|---|
| 4470 | + * port->explicit_contract. |
|---|
| 4471 | + */ |
|---|
| 4472 | + if (port->explicit_contract) |
|---|
| 4473 | + mod_send_discover_delayed_work(port, 0); |
|---|
| 4474 | + else |
|---|
| 4475 | + port->send_discover = false; |
|---|
| 4476 | + |
|---|
| 3984 | 4477 | power_supply_changed(port->psy); |
|---|
| 3985 | | - if (port->usb_type == POWER_SUPPLY_USB_TYPE_PD || |
|---|
| 3986 | | - port->usb_type == POWER_SUPPLY_USB_TYPE_PD_PPS) |
|---|
| 3987 | | - tcpm_send_power_change_notify(port); |
|---|
| 3988 | 4478 | break; |
|---|
| 3989 | 4479 | |
|---|
| 3990 | 4480 | /* Accessory states */ |
|---|
| .. | .. |
|---|
| 3999 | 4489 | tcpm_set_state(port, ACC_UNATTACHED, 0); |
|---|
| 4000 | 4490 | break; |
|---|
| 4001 | 4491 | case AUDIO_ACC_DEBOUNCE: |
|---|
| 4002 | | - tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE); |
|---|
| 4492 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 4493 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[AUDIO_ACC_DEBOUNCE], |
|---|
| 4494 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 4495 | + tcpm_set_state(port, ACC_UNATTACHED, timer_val_msecs); |
|---|
| 4003 | 4496 | break; |
|---|
| 4004 | 4497 | |
|---|
| 4005 | 4498 | /* Hard_Reset states */ |
|---|
| .. | .. |
|---|
| 4029 | 4522 | tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0); |
|---|
| 4030 | 4523 | break; |
|---|
| 4031 | 4524 | case SRC_HARD_RESET_VBUS_OFF: |
|---|
| 4032 | | - tcpm_set_vconn(port, true); |
|---|
| 4525 | + /* |
|---|
| 4526 | + * 7.1.5 Response to Hard Resets |
|---|
| 4527 | + * Hard Reset Signaling indicates a communication failure has occurred and the |
|---|
| 4528 | + * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall |
|---|
| 4529 | + * drive VBUS to vSafe0V as shown in Figure 7-9. |
|---|
| 4530 | + */ |
|---|
| 4531 | + tcpm_set_vconn(port, false); |
|---|
| 4033 | 4532 | tcpm_set_vbus(port, false); |
|---|
| 4034 | 4533 | tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE, |
|---|
| 4035 | | - TYPEC_HOST); |
|---|
| 4036 | | - tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER); |
|---|
| 4534 | + tcpm_data_role_for_source(port)); |
|---|
| 4535 | + /* |
|---|
| 4536 | + * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V + |
|---|
| 4537 | + * PD_T_SRC_RECOVER before turning vbus back on. |
|---|
| 4538 | + * From Table 7-12 Sequence Description for a Source Initiated Hard Reset: |
|---|
| 4539 | + * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then |
|---|
| 4540 | + * tells the Device Policy Manager to instruct the power supply to perform a |
|---|
| 4541 | + * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2). |
|---|
| 4542 | + * 5. After tSrcRecover the Source applies power to VBUS in an attempt to |
|---|
| 4543 | + * re-establish communication with the Sink and resume USB Default Operation. |
|---|
| 4544 | + * The transition to vSafe5V Shall occur within tSrcTurnOn(t4). |
|---|
| 4545 | + */ |
|---|
| 4546 | + tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER); |
|---|
| 4037 | 4547 | break; |
|---|
| 4038 | 4548 | case SRC_HARD_RESET_VBUS_ON: |
|---|
| 4549 | + tcpm_set_vconn(port, true); |
|---|
| 4039 | 4550 | tcpm_set_vbus(port, true); |
|---|
| 4040 | 4551 | if (port->ams == HARD_RESET) |
|---|
| 4041 | 4552 | tcpm_ams_finish(port); |
|---|
| .. | .. |
|---|
| 4044 | 4555 | tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON); |
|---|
| 4045 | 4556 | break; |
|---|
| 4046 | 4557 | case SNK_HARD_RESET_SINK_OFF: |
|---|
| 4558 | + /* Do not discharge/disconnect during hard reseet */ |
|---|
| 4559 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); |
|---|
| 4047 | 4560 | memset(&port->pps_data, 0, sizeof(port->pps_data)); |
|---|
| 4048 | 4561 | tcpm_set_vconn(port, false); |
|---|
| 4049 | 4562 | if (port->pd_capable) |
|---|
| 4050 | 4563 | tcpm_set_charge(port, false); |
|---|
| 4051 | 4564 | tcpm_set_roles(port, port->self_powered, TYPEC_SINK, |
|---|
| 4052 | | - TYPEC_DEVICE); |
|---|
| 4565 | + tcpm_data_role_for_sink(port)); |
|---|
| 4053 | 4566 | /* |
|---|
| 4054 | 4567 | * VBUS may or may not toggle, depending on the adapter. |
|---|
| 4055 | 4568 | * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON |
|---|
| .. | .. |
|---|
| 4090 | 4603 | if (port->ams == HARD_RESET) |
|---|
| 4091 | 4604 | tcpm_ams_finish(port); |
|---|
| 4092 | 4605 | tcpm_set_attached_state(port, true); |
|---|
| 4606 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); |
|---|
| 4093 | 4607 | tcpm_set_state(port, SNK_STARTUP, 0); |
|---|
| 4094 | 4608 | break; |
|---|
| 4095 | 4609 | |
|---|
| .. | .. |
|---|
| 4098 | 4612 | port->message_id = 0; |
|---|
| 4099 | 4613 | port->rx_msgid = -1; |
|---|
| 4100 | 4614 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); |
|---|
| 4615 | + tcpm_ams_finish(port); |
|---|
| 4101 | 4616 | if (port->pwr_role == TYPEC_SOURCE) { |
|---|
| 4102 | 4617 | port->upcoming_state = SRC_SEND_CAPABILITIES; |
|---|
| 4103 | 4618 | tcpm_ams_start(port, POWER_NEGOTIATION); |
|---|
| .. | .. |
|---|
| 4115 | 4630 | case SOFT_RESET_SEND: |
|---|
| 4116 | 4631 | port->message_id = 0; |
|---|
| 4117 | 4632 | port->rx_msgid = -1; |
|---|
| 4633 | + port->tcpc->set_pd_rx(port->tcpc, true); |
|---|
| 4118 | 4634 | if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET)) |
|---|
| 4119 | 4635 | tcpm_set_state_cond(port, hard_reset_state(port), 0); |
|---|
| 4120 | 4636 | else |
|---|
| .. | .. |
|---|
| 4125 | 4641 | /* DR_Swap states */ |
|---|
| 4126 | 4642 | case DR_SWAP_SEND: |
|---|
| 4127 | 4643 | tcpm_pd_send_control(port, PD_CTRL_DR_SWAP); |
|---|
| 4644 | + if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20) |
|---|
| 4645 | + port->send_discover = true; |
|---|
| 4128 | 4646 | tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT, |
|---|
| 4129 | 4647 | PD_T_SENDER_RESPONSE); |
|---|
| 4130 | 4648 | break; |
|---|
| 4131 | 4649 | case DR_SWAP_ACCEPT: |
|---|
| 4132 | 4650 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); |
|---|
| 4133 | | - /* Set VDM state machine running flag ASAP */ |
|---|
| 4134 | | - if (port->data_role == TYPEC_DEVICE && port->send_discover) |
|---|
| 4135 | | - port->vdm_sm_running = true; |
|---|
| 4651 | + if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20) |
|---|
| 4652 | + port->send_discover = true; |
|---|
| 4136 | 4653 | tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0); |
|---|
| 4137 | 4654 | break; |
|---|
| 4138 | 4655 | case DR_SWAP_SEND_TIMEOUT: |
|---|
| 4139 | 4656 | tcpm_swap_complete(port, -ETIMEDOUT); |
|---|
| 4657 | + port->send_discover = false; |
|---|
| 4658 | + tcpm_ams_finish(port); |
|---|
| 4140 | 4659 | tcpm_set_state(port, ready_state(port), 0); |
|---|
| 4141 | 4660 | break; |
|---|
| 4142 | 4661 | case DR_SWAP_CHANGE_DR: |
|---|
| 4143 | | - /* |
|---|
| 4144 | | - * After the DR_swap process is executed, for the |
|---|
| 4145 | | - * Rockchip platform, the tcpm framework needs to |
|---|
| 4146 | | - * send two notifications to the dwc3 driver, the |
|---|
| 4147 | | - * two notifications require a time interval. |
|---|
| 4148 | | - * |
|---|
| 4149 | | - * (a) For Type-C devices with DP function, |
|---|
| 4150 | | - * a notification will be sent here, another |
|---|
| 4151 | | - * notification can be sent after receiving |
|---|
| 4152 | | - * Attention cmd. |
|---|
| 4153 | | - * (b) But for Type-C devices without DP function, |
|---|
| 4154 | | - * the tcpm framework will only send a notification once. |
|---|
| 4155 | | - * |
|---|
| 4156 | | - * Based on the above reasons, it is necessary to start |
|---|
| 4157 | | - * the timer here, wait for 20 ms to start work and send |
|---|
| 4158 | | - * the notification again. |
|---|
| 4159 | | - */ |
|---|
| 4160 | | - |
|---|
| 4161 | 4662 | if (port->data_role == TYPEC_HOST) { |
|---|
| 4162 | 4663 | tcpm_unregister_altmodes(port); |
|---|
| 4163 | 4664 | tcpm_set_roles(port, true, port->pwr_role, |
|---|
| 4164 | 4665 | TYPEC_DEVICE); |
|---|
| 4165 | | - tcpm_send_data_role_notify(port, false, TYPEC_HOST); |
|---|
| 4166 | | - mod_data_role_swap_work(port, SEND_NEW_MODE_NOTIFY_MS); |
|---|
| 4167 | 4666 | } else { |
|---|
| 4168 | 4667 | tcpm_set_roles(port, true, port->pwr_role, |
|---|
| 4169 | 4668 | TYPEC_HOST); |
|---|
| 4170 | | - port->send_discover = true; |
|---|
| 4171 | | - tcpm_send_data_role_notify(port, false, TYPEC_DEVICE); |
|---|
| 4172 | | - mod_data_role_swap_work(port, SEND_NEW_MODE_NOTIFY_MS); |
|---|
| 4173 | 4669 | } |
|---|
| 4670 | + tcpm_ams_finish(port); |
|---|
| 4174 | 4671 | tcpm_set_state(port, ready_state(port), 0); |
|---|
| 4175 | 4672 | break; |
|---|
| 4176 | 4673 | |
|---|
| .. | .. |
|---|
| 4185 | 4682 | tcpm_set_state(port, ERROR_RECOVERY, 0); |
|---|
| 4186 | 4683 | break; |
|---|
| 4187 | 4684 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: |
|---|
| 4188 | | - tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_OFF); |
|---|
| 4685 | + timer_val_msecs = PD_T_PS_SOURCE_OFF; |
|---|
| 4686 | + state_name = tcpm_states[FR_SWAP_SNK_SRC_TRANSITION_TO_OFF]; |
|---|
| 4687 | + trace_android_vh_typec_tcpm_get_timer(state_name, SOURCE_OFF, &timer_val_msecs); |
|---|
| 4688 | + tcpm_set_state(port, ERROR_RECOVERY, timer_val_msecs); |
|---|
| 4189 | 4689 | break; |
|---|
| 4190 | 4690 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: |
|---|
| 4191 | 4691 | if (port->vbus_source) |
|---|
| .. | .. |
|---|
| 4218 | 4718 | tcpm_set_state(port, ready_state(port), 0); |
|---|
| 4219 | 4719 | break; |
|---|
| 4220 | 4720 | case PR_SWAP_START: |
|---|
| 4721 | + tcpm_apply_rc(port); |
|---|
| 4221 | 4722 | if (port->pwr_role == TYPEC_SOURCE) |
|---|
| 4222 | 4723 | tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF, |
|---|
| 4223 | 4724 | PD_T_SRC_TRANSITION); |
|---|
| .. | .. |
|---|
| 4225 | 4726 | tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0); |
|---|
| 4226 | 4727 | break; |
|---|
| 4227 | 4728 | case PR_SWAP_SRC_SNK_TRANSITION_OFF: |
|---|
| 4729 | + /* |
|---|
| 4730 | + * Prevent vbus discharge circuit from turning on during PR_SWAP |
|---|
| 4731 | + * as this is not a disconnect. |
|---|
| 4732 | + */ |
|---|
| 4228 | 4733 | tcpm_set_vbus(port, false); |
|---|
| 4229 | 4734 | port->explicit_contract = false; |
|---|
| 4230 | 4735 | /* allow time for Vbus discharge, must be < tSrcSwapStdby */ |
|---|
| .. | .. |
|---|
| 4232 | 4737 | PD_T_SRCSWAPSTDBY); |
|---|
| 4233 | 4738 | break; |
|---|
| 4234 | 4739 | case PR_SWAP_SRC_SNK_SOURCE_OFF: |
|---|
| 4740 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 4741 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[PR_SWAP_SRC_SNK_SOURCE_OFF], |
|---|
| 4742 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 4235 | 4743 | tcpm_set_cc(port, TYPEC_CC_RD); |
|---|
| 4236 | 4744 | /* allow CC debounce */ |
|---|
| 4237 | 4745 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED, |
|---|
| 4238 | | - PD_T_CC_DEBOUNCE); |
|---|
| 4746 | + timer_val_msecs); |
|---|
| 4239 | 4747 | break; |
|---|
| 4240 | 4748 | case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: |
|---|
| 4241 | 4749 | /* |
|---|
| .. | .. |
|---|
| 4250 | 4758 | tcpm_set_state(port, ERROR_RECOVERY, 0); |
|---|
| 4251 | 4759 | break; |
|---|
| 4252 | 4760 | } |
|---|
| 4253 | | - tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON); |
|---|
| 4761 | + tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_ON_PRS); |
|---|
| 4254 | 4762 | break; |
|---|
| 4255 | 4763 | case PR_SWAP_SRC_SNK_SINK_ON: |
|---|
| 4764 | + tcpm_enable_auto_vbus_discharge(port, true); |
|---|
| 4765 | + /* Set the vbus disconnect threshold for implicit contract */ |
|---|
| 4766 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); |
|---|
| 4256 | 4767 | tcpm_set_state(port, SNK_STARTUP, 0); |
|---|
| 4257 | 4768 | break; |
|---|
| 4258 | 4769 | case PR_SWAP_SNK_SRC_SINK_OFF: |
|---|
| 4770 | + timer_val_msecs = PD_T_PS_SOURCE_OFF; |
|---|
| 4771 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[PR_SWAP_SNK_SRC_SINK_OFF], |
|---|
| 4772 | + SOURCE_OFF, &timer_val_msecs); |
|---|
| 4773 | + /* |
|---|
| 4774 | + * Prevent vbus discharge circuit from turning on during PR_SWAP |
|---|
| 4775 | + * as this is not a disconnect. |
|---|
| 4776 | + */ |
|---|
| 4777 | + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, |
|---|
| 4778 | + port->pps_data.active, 0); |
|---|
| 4259 | 4779 | tcpm_set_charge(port, false); |
|---|
| 4260 | | - tcpm_set_state(port, hard_reset_state(port), |
|---|
| 4261 | | - PD_T_PS_SOURCE_OFF); |
|---|
| 4780 | + tcpm_set_state(port, hard_reset_state(port), timer_val_msecs); |
|---|
| 4262 | 4781 | break; |
|---|
| 4263 | 4782 | case PR_SWAP_SNK_SRC_SOURCE_ON: |
|---|
| 4783 | + tcpm_enable_auto_vbus_discharge(port, true); |
|---|
| 4264 | 4784 | tcpm_set_cc(port, tcpm_rp_cc(port)); |
|---|
| 4265 | 4785 | tcpm_set_vbus(port, true); |
|---|
| 4266 | 4786 | /* |
|---|
| .. | .. |
|---|
| 4286 | 4806 | |
|---|
| 4287 | 4807 | case VCONN_SWAP_ACCEPT: |
|---|
| 4288 | 4808 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); |
|---|
| 4809 | + tcpm_ams_finish(port); |
|---|
| 4289 | 4810 | tcpm_set_state(port, VCONN_SWAP_START, 0); |
|---|
| 4290 | 4811 | break; |
|---|
| 4291 | 4812 | case VCONN_SWAP_SEND: |
|---|
| .. | .. |
|---|
| 4295 | 4816 | break; |
|---|
| 4296 | 4817 | case VCONN_SWAP_SEND_TIMEOUT: |
|---|
| 4297 | 4818 | tcpm_swap_complete(port, -ETIMEDOUT); |
|---|
| 4298 | | - if (port->data_role == TYPEC_HOST && port->send_discover) |
|---|
| 4299 | | - port->vdm_sm_running = true; |
|---|
| 4300 | 4819 | tcpm_set_state(port, ready_state(port), 0); |
|---|
| 4301 | 4820 | break; |
|---|
| 4302 | 4821 | case VCONN_SWAP_START: |
|---|
| .. | .. |
|---|
| 4312 | 4831 | case VCONN_SWAP_TURN_ON_VCONN: |
|---|
| 4313 | 4832 | tcpm_set_vconn(port, true); |
|---|
| 4314 | 4833 | tcpm_pd_send_control(port, PD_CTRL_PS_RDY); |
|---|
| 4315 | | - if (port->data_role == TYPEC_HOST && port->send_discover) |
|---|
| 4316 | | - port->vdm_sm_running = true; |
|---|
| 4317 | 4834 | tcpm_set_state(port, ready_state(port), 0); |
|---|
| 4318 | 4835 | break; |
|---|
| 4319 | 4836 | case VCONN_SWAP_TURN_OFF_VCONN: |
|---|
| 4320 | 4837 | tcpm_set_vconn(port, false); |
|---|
| 4321 | | - if (port->data_role == TYPEC_HOST && port->send_discover) |
|---|
| 4322 | | - port->vdm_sm_running = true; |
|---|
| 4323 | 4838 | tcpm_set_state(port, ready_state(port), 0); |
|---|
| 4324 | 4839 | break; |
|---|
| 4325 | 4840 | |
|---|
| .. | .. |
|---|
| 4327 | 4842 | case PR_SWAP_CANCEL: |
|---|
| 4328 | 4843 | case VCONN_SWAP_CANCEL: |
|---|
| 4329 | 4844 | tcpm_swap_complete(port, port->swap_status); |
|---|
| 4330 | | - if (port->data_role == TYPEC_HOST && port->send_discover) |
|---|
| 4331 | | - port->vdm_sm_running = true; |
|---|
| 4332 | 4845 | if (port->pwr_role == TYPEC_SOURCE) |
|---|
| 4333 | 4846 | tcpm_set_state(port, SRC_READY, 0); |
|---|
| 4334 | 4847 | else |
|---|
| .. | .. |
|---|
| 4345 | 4858 | switch (BDO_MODE_MASK(port->bist_request)) { |
|---|
| 4346 | 4859 | case BDO_MODE_CARRIER2: |
|---|
| 4347 | 4860 | tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL); |
|---|
| 4861 | + tcpm_set_state(port, unattached_state(port), |
|---|
| 4862 | + PD_T_BIST_CONT_MODE); |
|---|
| 4863 | + break; |
|---|
| 4864 | + case BDO_MODE_TESTDATA: |
|---|
| 4865 | + if (port->tcpc->set_bist_data) { |
|---|
| 4866 | + tcpm_log(port, "Enable BIST MODE TESTDATA"); |
|---|
| 4867 | + port->tcpc->set_bist_data(port->tcpc, true); |
|---|
| 4868 | + } |
|---|
| 4348 | 4869 | break; |
|---|
| 4349 | 4870 | default: |
|---|
| 4350 | 4871 | break; |
|---|
| 4351 | 4872 | } |
|---|
| 4352 | | - /* Always switch to unattached state */ |
|---|
| 4353 | | - tcpm_set_state(port, unattached_state(port), 0); |
|---|
| 4354 | 4873 | break; |
|---|
| 4355 | 4874 | case GET_STATUS_SEND: |
|---|
| 4356 | 4875 | tcpm_pd_send_control(port, PD_CTRL_GET_STATUS); |
|---|
| .. | .. |
|---|
| 4388 | 4907 | PD_T_ERROR_RECOVERY); |
|---|
| 4389 | 4908 | break; |
|---|
| 4390 | 4909 | case PORT_RESET_WAIT_OFF: |
|---|
| 4910 | + timer_val_msecs = PD_T_PS_SOURCE_OFF; |
|---|
| 4911 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[PORT_RESET_WAIT_OFF], |
|---|
| 4912 | + SOURCE_OFF, &timer_val_msecs); |
|---|
| 4391 | 4913 | tcpm_set_state(port, |
|---|
| 4392 | 4914 | tcpm_default_state(port), |
|---|
| 4393 | | - port->vbus_present ? PD_T_PS_SOURCE_OFF : 0); |
|---|
| 4915 | + port->vbus_present ? timer_val_msecs : 0); |
|---|
| 4394 | 4916 | break; |
|---|
| 4395 | 4917 | |
|---|
| 4396 | 4918 | /* AMS intermediate state */ |
|---|
| .. | .. |
|---|
| 4404 | 4926 | upcoming_state = port->upcoming_state; |
|---|
| 4405 | 4927 | port->upcoming_state = INVALID_STATE; |
|---|
| 4406 | 4928 | tcpm_set_state(port, upcoming_state, 0); |
|---|
| 4929 | + break; |
|---|
| 4930 | + |
|---|
| 4931 | + /* Chunk state */ |
|---|
| 4932 | + case CHUNK_NOT_SUPP: |
|---|
| 4933 | + tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP); |
|---|
| 4934 | + tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? SRC_READY : SNK_READY, 0); |
|---|
| 4407 | 4935 | break; |
|---|
| 4408 | 4936 | default: |
|---|
| 4409 | 4937 | WARN(1, "Unexpected port state %d\n", port->state); |
|---|
| .. | .. |
|---|
| 4488 | 5016 | tcpm_set_state(port, SRC_ATTACH_WAIT, 0); |
|---|
| 4489 | 5017 | break; |
|---|
| 4490 | 5018 | case SRC_ATTACHED: |
|---|
| 5019 | + case SRC_STARTUP: |
|---|
| 4491 | 5020 | case SRC_SEND_CAPABILITIES: |
|---|
| 4492 | 5021 | case SRC_READY: |
|---|
| 4493 | 5022 | if (tcpm_port_is_disconnected(port) || |
|---|
| 4494 | | - !tcpm_port_is_source(port)) |
|---|
| 4495 | | - tcpm_set_state(port, SRC_UNATTACHED, 0); |
|---|
| 5023 | + !tcpm_port_is_source(port)) { |
|---|
| 5024 | + if (port->port_type == TYPEC_PORT_SRC) |
|---|
| 5025 | + tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port)); |
|---|
| 5026 | + else |
|---|
| 5027 | + tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); |
|---|
| 5028 | + } |
|---|
| 4496 | 5029 | break; |
|---|
| 4497 | 5030 | case SNK_UNATTACHED: |
|---|
| 4498 | 5031 | if (tcpm_port_is_sink(port)) |
|---|
| .. | .. |
|---|
| 4522 | 5055 | tcpm_set_state(port, SNK_DEBOUNCED, 0); |
|---|
| 4523 | 5056 | break; |
|---|
| 4524 | 5057 | case SNK_READY: |
|---|
| 4525 | | - if (tcpm_port_is_disconnected(port)) |
|---|
| 5058 | + /* |
|---|
| 5059 | + * EXIT condition is based primarily on vbus disconnect and CC is secondary. |
|---|
| 5060 | + * "A port that has entered into USB PD communications with the Source and |
|---|
| 5061 | + * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect |
|---|
| 5062 | + * cable disconnect in addition to monitoring VBUS. |
|---|
| 5063 | + * |
|---|
| 5064 | + * A port that is monitoring the CC voltage for disconnect (but is not in |
|---|
| 5065 | + * the process of a USB PD PR_Swap or USB PD FR_Swap) shall transition to |
|---|
| 5066 | + * Unattached.SNK within tSinkDisconnect after the CC voltage remains below |
|---|
| 5067 | + * vRd-USB for tPDDebounce." |
|---|
| 5068 | + * |
|---|
| 5069 | + * When set_auto_vbus_discharge_threshold is enabled, CC pins go |
|---|
| 5070 | + * away before vbus decays to disconnect threshold. Allow |
|---|
| 5071 | + * disconnect to be driven by vbus disconnect when auto vbus |
|---|
| 5072 | + * discharge is enabled. |
|---|
| 5073 | + */ |
|---|
| 5074 | + if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port)) |
|---|
| 4526 | 5075 | tcpm_set_state(port, unattached_state(port), 0); |
|---|
| 4527 | 5076 | else if (!port->pd_capable && |
|---|
| 4528 | 5077 | (cc1 != old_cc1 || cc2 != old_cc2)) |
|---|
| .. | .. |
|---|
| 4587 | 5136 | if (!tcpm_port_is_sink(port)) |
|---|
| 4588 | 5137 | tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0); |
|---|
| 4589 | 5138 | break; |
|---|
| 5139 | + case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: |
|---|
| 5140 | + if (!tcpm_port_is_sink(port)) |
|---|
| 5141 | + tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE); |
|---|
| 5142 | + else |
|---|
| 5143 | + tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0); |
|---|
| 5144 | + break; |
|---|
| 4590 | 5145 | case SNK_TRYWAIT: |
|---|
| 4591 | 5146 | /* Do nothing, waiting for tCCDebounce */ |
|---|
| 4592 | 5147 | break; |
|---|
| .. | .. |
|---|
| 4615 | 5170 | * Ignore CC changes here. |
|---|
| 4616 | 5171 | */ |
|---|
| 4617 | 5172 | break; |
|---|
| 4618 | | - |
|---|
| 4619 | 5173 | default: |
|---|
| 4620 | | - if (tcpm_port_is_disconnected(port)) |
|---|
| 5174 | + /* |
|---|
| 5175 | + * While acting as sink and auto vbus discharge is enabled, Allow disconnect |
|---|
| 5176 | + * to be driven by vbus disconnect. |
|---|
| 5177 | + */ |
|---|
| 5178 | + if (tcpm_port_is_disconnected(port) && !(port->pwr_role == TYPEC_SINK && |
|---|
| 5179 | + port->auto_vbus_discharge_enabled)) |
|---|
| 4621 | 5180 | tcpm_set_state(port, unattached_state(port), 0); |
|---|
| 4622 | 5181 | break; |
|---|
| 4623 | 5182 | } |
|---|
| .. | .. |
|---|
| 4627 | 5186 | { |
|---|
| 4628 | 5187 | tcpm_log_force(port, "VBUS on"); |
|---|
| 4629 | 5188 | port->vbus_present = true; |
|---|
| 5189 | + /* |
|---|
| 5190 | + * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly |
|---|
| 5191 | + * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here. |
|---|
| 5192 | + */ |
|---|
| 5193 | + port->vbus_vsafe0v = false; |
|---|
| 5194 | + |
|---|
| 4630 | 5195 | switch (port->state) { |
|---|
| 4631 | 5196 | case SNK_TRANSITION_SINK_VBUS: |
|---|
| 4632 | 5197 | port->explicit_contract = true; |
|---|
| 4633 | | - /* Set the VDM flag ASAP */ |
|---|
| 4634 | | - if (port->data_role == TYPEC_HOST && port->send_discover) |
|---|
| 4635 | | - port->vdm_sm_running = true; |
|---|
| 4636 | 5198 | tcpm_set_state(port, SNK_READY, 0); |
|---|
| 4637 | 5199 | break; |
|---|
| 4638 | 5200 | case SNK_DISCOVERY: |
|---|
| .. | .. |
|---|
| 4676 | 5238 | case SNK_TRYWAIT_DEBOUNCE: |
|---|
| 4677 | 5239 | /* Do nothing, waiting for Rp */ |
|---|
| 4678 | 5240 | break; |
|---|
| 5241 | + case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: |
|---|
| 5242 | + if (port->vbus_present && tcpm_port_is_sink(port)) |
|---|
| 5243 | + tcpm_set_state(port, SNK_ATTACHED, 0); |
|---|
| 5244 | + break; |
|---|
| 4679 | 5245 | case SRC_TRY_WAIT: |
|---|
| 4680 | 5246 | case SRC_TRY_DEBOUNCE: |
|---|
| 4681 | 5247 | /* Do nothing, waiting for sink detection */ |
|---|
| 4682 | 5248 | break; |
|---|
| 5249 | + case FR_SWAP_SEND: |
|---|
| 5250 | + case FR_SWAP_SEND_TIMEOUT: |
|---|
| 5251 | + case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: |
|---|
| 5252 | + case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: |
|---|
| 5253 | + if (port->tcpc->frs_sourcing_vbus) |
|---|
| 5254 | + port->tcpc->frs_sourcing_vbus(port->tcpc); |
|---|
| 5255 | + break; |
|---|
| 4683 | 5256 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: |
|---|
| 5257 | + if (port->tcpc->frs_sourcing_vbus) |
|---|
| 5258 | + port->tcpc->frs_sourcing_vbus(port->tcpc); |
|---|
| 4684 | 5259 | tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0); |
|---|
| 4685 | 5260 | break; |
|---|
| 4686 | 5261 | |
|---|
| .. | .. |
|---|
| 4706 | 5281 | case SNK_HARD_RESET_SINK_OFF: |
|---|
| 4707 | 5282 | tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0); |
|---|
| 4708 | 5283 | break; |
|---|
| 4709 | | - case SRC_HARD_RESET_VBUS_OFF: |
|---|
| 4710 | | - tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, 0); |
|---|
| 4711 | | - break; |
|---|
| 4712 | 5284 | case HARD_RESET_SEND: |
|---|
| 4713 | 5285 | break; |
|---|
| 4714 | | - |
|---|
| 4715 | 5286 | case SNK_TRY: |
|---|
| 4716 | 5287 | /* Do nothing, waiting for timeout */ |
|---|
| 4717 | 5288 | break; |
|---|
| .. | .. |
|---|
| 4729 | 5300 | break; |
|---|
| 4730 | 5301 | case SNK_ATTACH_WAIT: |
|---|
| 4731 | 5302 | case SNK_DEBOUNCED: |
|---|
| 5303 | + port->debouncing = false; |
|---|
| 4732 | 5304 | /* Do nothing, as TCPM is still waiting for vbus to reaach VSAFE5V to connect */ |
|---|
| 4733 | 5305 | break; |
|---|
| 4734 | 5306 | |
|---|
| .. | .. |
|---|
| 4743 | 5315 | /* Do nothing, expected */ |
|---|
| 4744 | 5316 | break; |
|---|
| 4745 | 5317 | |
|---|
| 5318 | + case PR_SWAP_SNK_SRC_SOURCE_ON: |
|---|
| 5319 | + /* |
|---|
| 5320 | + * Do nothing when vbus off notification is received. |
|---|
| 5321 | + * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON |
|---|
| 5322 | + * for the vbus source to ramp up. |
|---|
| 5323 | + */ |
|---|
| 5324 | + break; |
|---|
| 5325 | + |
|---|
| 4746 | 5326 | case PORT_RESET_WAIT_OFF: |
|---|
| 4747 | 5327 | tcpm_set_state(port, tcpm_default_state(port), 0); |
|---|
| 4748 | 5328 | break; |
|---|
| .. | .. |
|---|
| 4750 | 5330 | case SRC_TRY_WAIT: |
|---|
| 4751 | 5331 | case SRC_TRY_DEBOUNCE: |
|---|
| 4752 | 5332 | /* Do nothing, waiting for sink detection */ |
|---|
| 5333 | + break; |
|---|
| 5334 | + |
|---|
| 5335 | + case SRC_STARTUP: |
|---|
| 5336 | + case SRC_SEND_CAPABILITIES: |
|---|
| 5337 | + case SRC_SEND_CAPABILITIES_TIMEOUT: |
|---|
| 5338 | + case SRC_NEGOTIATE_CAPABILITIES: |
|---|
| 5339 | + case SRC_TRANSITION_SUPPLY: |
|---|
| 5340 | + case SRC_READY: |
|---|
| 5341 | + case SRC_WAIT_NEW_CAPABILITIES: |
|---|
| 5342 | + /* |
|---|
| 5343 | + * Force to unattached state to re-initiate connection. |
|---|
| 5344 | + * DRP port should move to Unattached.SNK instead of Unattached.SRC if |
|---|
| 5345 | + * sink removed. Although sink removal here is due to source's vbus collapse, |
|---|
| 5346 | + * treat it the same way for consistency. |
|---|
| 5347 | + */ |
|---|
| 5348 | + if (port->port_type == TYPEC_PORT_SRC) |
|---|
| 5349 | + tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port)); |
|---|
| 5350 | + else |
|---|
| 5351 | + tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); |
|---|
| 4753 | 5352 | break; |
|---|
| 4754 | 5353 | |
|---|
| 4755 | 5354 | case PORT_RESET: |
|---|
| .. | .. |
|---|
| 4768 | 5367 | break; |
|---|
| 4769 | 5368 | |
|---|
| 4770 | 5369 | default: |
|---|
| 4771 | | - if (port->pwr_role == TYPEC_SINK && |
|---|
| 4772 | | - port->attached) |
|---|
| 5370 | + if (port->pwr_role == TYPEC_SINK && port->attached) |
|---|
| 5371 | + tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); |
|---|
| 5372 | + break; |
|---|
| 5373 | + } |
|---|
| 5374 | +} |
|---|
| 5375 | + |
|---|
| 5376 | +static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port) |
|---|
| 5377 | +{ |
|---|
| 5378 | + unsigned int timer_val_msecs; |
|---|
| 5379 | + |
|---|
| 5380 | + tcpm_log_force(port, "VBUS VSAFE0V"); |
|---|
| 5381 | + port->vbus_vsafe0v = true; |
|---|
| 5382 | + switch (port->state) { |
|---|
| 5383 | + case SRC_HARD_RESET_VBUS_OFF: |
|---|
| 5384 | + /* |
|---|
| 5385 | + * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait |
|---|
| 5386 | + * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V. |
|---|
| 5387 | + */ |
|---|
| 5388 | + tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER); |
|---|
| 5389 | + break; |
|---|
| 5390 | + case SRC_ATTACH_WAIT: |
|---|
| 5391 | + timer_val_msecs = PD_T_CC_DEBOUNCE; |
|---|
| 5392 | + trace_android_vh_typec_tcpm_get_timer(tcpm_states[SRC_ATTACH_WAIT], |
|---|
| 5393 | + CC_DEBOUNCE, &timer_val_msecs); |
|---|
| 5394 | + if (tcpm_port_is_source(port)) |
|---|
| 5395 | + tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED, |
|---|
| 5396 | + timer_val_msecs); |
|---|
| 5397 | + break; |
|---|
| 5398 | + case SRC_STARTUP: |
|---|
| 5399 | + case SRC_SEND_CAPABILITIES: |
|---|
| 5400 | + case SRC_SEND_CAPABILITIES_TIMEOUT: |
|---|
| 5401 | + case SRC_NEGOTIATE_CAPABILITIES: |
|---|
| 5402 | + case SRC_TRANSITION_SUPPLY: |
|---|
| 5403 | + case SRC_READY: |
|---|
| 5404 | + case SRC_WAIT_NEW_CAPABILITIES: |
|---|
| 5405 | + if (port->auto_vbus_discharge_enabled) { |
|---|
| 5406 | + if (port->port_type == TYPEC_PORT_SRC) |
|---|
| 5407 | + tcpm_set_state(port, SRC_UNATTACHED, 0); |
|---|
| 5408 | + else |
|---|
| 5409 | + tcpm_set_state(port, SNK_UNATTACHED, 0); |
|---|
| 5410 | + } |
|---|
| 5411 | + break; |
|---|
| 5412 | + case PR_SWAP_SNK_SRC_SINK_OFF: |
|---|
| 5413 | + case PR_SWAP_SNK_SRC_SOURCE_ON: |
|---|
| 5414 | + /* Do nothing, vsafe0v is expected during transition */ |
|---|
| 5415 | + break; |
|---|
| 5416 | + case SNK_ATTACH_WAIT: |
|---|
| 5417 | + case SNK_DEBOUNCED: |
|---|
| 5418 | + /*Do nothing, still waiting for VSAFE5V for connect */ |
|---|
| 5419 | + break; |
|---|
| 5420 | + default: |
|---|
| 5421 | + if (port->pwr_role == TYPEC_SINK && port->auto_vbus_discharge_enabled) |
|---|
| 4773 | 5422 | tcpm_set_state(port, SNK_UNATTACHED, 0); |
|---|
| 4774 | 5423 | break; |
|---|
| 4775 | 5424 | } |
|---|
| .. | .. |
|---|
| 4778 | 5427 | static void _tcpm_pd_hard_reset(struct tcpm_port *port) |
|---|
| 4779 | 5428 | { |
|---|
| 4780 | 5429 | tcpm_log_force(port, "Received hard reset"); |
|---|
| 5430 | + if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data) |
|---|
| 5431 | + port->tcpc->set_bist_data(port->tcpc, false); |
|---|
| 4781 | 5432 | |
|---|
| 4782 | 5433 | if (port->ams != NONE_AMS) |
|---|
| 4783 | 5434 | port->ams = NONE_AMS; |
|---|
| 5435 | + if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) |
|---|
| 5436 | + port->ams = HARD_RESET; |
|---|
| 4784 | 5437 | /* |
|---|
| 4785 | 5438 | * If we keep receiving hard reset requests, executing the hard reset |
|---|
| 4786 | 5439 | * must have failed. Revert to error recovery if that happens. |
|---|
| .. | .. |
|---|
| 4797 | 5450 | event_work); |
|---|
| 4798 | 5451 | u32 events; |
|---|
| 4799 | 5452 | |
|---|
| 5453 | +#ifdef CONFIG_NO_GKI |
|---|
| 4800 | 5454 | mutex_lock(&port->pd_handler_lock); |
|---|
| 5455 | +#endif |
|---|
| 4801 | 5456 | mutex_lock(&port->lock); |
|---|
| 4802 | 5457 | |
|---|
| 4803 | 5458 | spin_lock(&port->pd_event_lock); |
|---|
| .. | .. |
|---|
| 4811 | 5466 | bool vbus; |
|---|
| 4812 | 5467 | |
|---|
| 4813 | 5468 | vbus = port->tcpc->get_vbus(port->tcpc); |
|---|
| 4814 | | - if (vbus) |
|---|
| 5469 | + if (vbus) { |
|---|
| 4815 | 5470 | _tcpm_pd_vbus_on(port); |
|---|
| 4816 | | - else |
|---|
| 5471 | + } else { |
|---|
| 4817 | 5472 | _tcpm_pd_vbus_off(port); |
|---|
| 5473 | + /* |
|---|
| 5474 | + * When TCPC does not support detecting vsafe0v voltage level, |
|---|
| 5475 | + * treat vbus absent as vsafe0v. Else invoke is_vbus_vsafe0v |
|---|
| 5476 | + * to see if vbus has discharge to VSAFE0V. |
|---|
| 5477 | + */ |
|---|
| 5478 | + if (!port->tcpc->is_vbus_vsafe0v || |
|---|
| 5479 | + port->tcpc->is_vbus_vsafe0v(port->tcpc)) |
|---|
| 5480 | + _tcpm_pd_vbus_vsafe0v(port); |
|---|
| 5481 | + } |
|---|
| 4818 | 5482 | } |
|---|
| 4819 | 5483 | if (events & TCPM_CC_EVENT) { |
|---|
| 4820 | 5484 | enum typec_cc_status cc1, cc2; |
|---|
| .. | .. |
|---|
| 4851 | 5515 | } |
|---|
| 4852 | 5516 | spin_unlock(&port->pd_event_lock); |
|---|
| 4853 | 5517 | mutex_unlock(&port->lock); |
|---|
| 5518 | +#ifdef CONFIG_NO_GKI |
|---|
| 4854 | 5519 | mutex_unlock(&port->pd_handler_lock); |
|---|
| 5520 | +#endif |
|---|
| 4855 | 5521 | } |
|---|
| 4856 | 5522 | |
|---|
| 4857 | 5523 | void tcpm_cc_change(struct tcpm_port *port) |
|---|
| .. | .. |
|---|
| 4931 | 5597 | mutex_unlock(&port->lock); |
|---|
| 4932 | 5598 | } |
|---|
| 4933 | 5599 | |
|---|
| 4934 | | -static void tcpm_data_role_swap_work(struct kthread_work *work) |
|---|
| 5600 | +static void tcpm_send_discover_work(struct kthread_work *work) |
|---|
| 4935 | 5601 | { |
|---|
| 4936 | | - struct tcpm_port *port = |
|---|
| 4937 | | - container_of(work, struct tcpm_port, data_role_swap); |
|---|
| 5602 | + struct tcpm_port *port = container_of(work, struct tcpm_port, send_discover_work); |
|---|
| 4938 | 5603 | |
|---|
| 4939 | | - if (tcpm_port_is_disconnected(port)) |
|---|
| 4940 | | - return; |
|---|
| 5604 | + mutex_lock(&port->lock); |
|---|
| 5605 | + /* No need to send DISCOVER_IDENTITY anymore */ |
|---|
| 5606 | + if (!port->send_discover) |
|---|
| 5607 | + goto unlock; |
|---|
| 4941 | 5608 | |
|---|
| 4942 | | - tcpm_send_data_role_notify(port, true, port->data_role); |
|---|
| 5609 | + if (port->data_role == TYPEC_DEVICE && port->negotiated_rev < PD_REV30) { |
|---|
| 5610 | + port->send_discover = false; |
|---|
| 5611 | + goto unlock; |
|---|
| 5612 | + } |
|---|
| 5613 | + |
|---|
| 5614 | + /* Retry if the port is not idle */ |
|---|
| 5615 | + if ((port->state != SRC_READY && port->state != SNK_READY) || port->vdm_sm_running) { |
|---|
| 5616 | + mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS); |
|---|
| 5617 | + goto unlock; |
|---|
| 5618 | + } |
|---|
| 5619 | + |
|---|
| 5620 | + tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0); |
|---|
| 5621 | + |
|---|
| 5622 | +unlock: |
|---|
| 5623 | + mutex_unlock(&port->lock); |
|---|
| 4943 | 5624 | } |
|---|
| 4944 | 5625 | |
|---|
| 4945 | | -static int tcpm_dr_set(const struct typec_capability *cap, |
|---|
| 4946 | | - enum typec_data_role data) |
|---|
| 5626 | +static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data) |
|---|
| 4947 | 5627 | { |
|---|
| 4948 | | - struct tcpm_port *port = typec_cap_to_tcpm(cap); |
|---|
| 5628 | + struct tcpm_port *port = typec_get_drvdata(p); |
|---|
| 4949 | 5629 | int ret; |
|---|
| 4950 | 5630 | |
|---|
| 4951 | 5631 | mutex_lock(&port->swap_lock); |
|---|
| 4952 | 5632 | mutex_lock(&port->lock); |
|---|
| 4953 | 5633 | |
|---|
| 4954 | | - if (port->port_type != TYPEC_PORT_DRP) { |
|---|
| 5634 | + if (port->typec_caps.data != TYPEC_PORT_DRD) { |
|---|
| 4955 | 5635 | ret = -EINVAL; |
|---|
| 4956 | 5636 | goto port_unlock; |
|---|
| 4957 | 5637 | } |
|---|
| .. | .. |
|---|
| 5015 | 5695 | return ret; |
|---|
| 5016 | 5696 | } |
|---|
| 5017 | 5697 | |
|---|
| 5018 | | -static int tcpm_pr_set(const struct typec_capability *cap, |
|---|
| 5019 | | - enum typec_role role) |
|---|
| 5698 | +static int tcpm_pr_set(struct typec_port *p, enum typec_role role) |
|---|
| 5020 | 5699 | { |
|---|
| 5021 | | - struct tcpm_port *port = typec_cap_to_tcpm(cap); |
|---|
| 5700 | + struct tcpm_port *port = typec_get_drvdata(p); |
|---|
| 5022 | 5701 | int ret; |
|---|
| 5023 | 5702 | |
|---|
| 5024 | 5703 | mutex_lock(&port->swap_lock); |
|---|
| .. | .. |
|---|
| 5065 | 5744 | return ret; |
|---|
| 5066 | 5745 | } |
|---|
| 5067 | 5746 | |
|---|
| 5068 | | -static int tcpm_vconn_set(const struct typec_capability *cap, |
|---|
| 5069 | | - enum typec_role role) |
|---|
| 5747 | +static int tcpm_vconn_set(struct typec_port *p, enum typec_role role) |
|---|
| 5070 | 5748 | { |
|---|
| 5071 | | - struct tcpm_port *port = typec_cap_to_tcpm(cap); |
|---|
| 5749 | + struct tcpm_port *port = typec_get_drvdata(p); |
|---|
| 5072 | 5750 | int ret; |
|---|
| 5073 | 5751 | |
|---|
| 5074 | 5752 | mutex_lock(&port->swap_lock); |
|---|
| .. | .. |
|---|
| 5111 | 5789 | return ret; |
|---|
| 5112 | 5790 | } |
|---|
| 5113 | 5791 | |
|---|
| 5114 | | -static int tcpm_try_role(const struct typec_capability *cap, int role) |
|---|
| 5792 | +static int tcpm_try_role(struct typec_port *p, int role) |
|---|
| 5115 | 5793 | { |
|---|
| 5116 | | - struct tcpm_port *port = typec_cap_to_tcpm(cap); |
|---|
| 5794 | + struct tcpm_port *port = typec_get_drvdata(p); |
|---|
| 5117 | 5795 | struct tcpc_dev *tcpc = port->tcpc; |
|---|
| 5118 | 5796 | int ret = 0; |
|---|
| 5119 | 5797 | |
|---|
| .. | .. |
|---|
| 5129 | 5807 | return ret; |
|---|
| 5130 | 5808 | } |
|---|
| 5131 | 5809 | |
|---|
| 5132 | | -static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 op_curr) |
|---|
| 5810 | +static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 req_op_curr) |
|---|
| 5133 | 5811 | { |
|---|
| 5134 | 5812 | unsigned int target_mw; |
|---|
| 5135 | 5813 | int ret; |
|---|
| .. | .. |
|---|
| 5147 | 5825 | goto port_unlock; |
|---|
| 5148 | 5826 | } |
|---|
| 5149 | 5827 | |
|---|
| 5150 | | - if (op_curr > port->pps_data.max_curr) { |
|---|
| 5828 | + if (req_op_curr > port->pps_data.max_curr) { |
|---|
| 5151 | 5829 | ret = -EINVAL; |
|---|
| 5152 | 5830 | goto port_unlock; |
|---|
| 5153 | 5831 | } |
|---|
| 5154 | 5832 | |
|---|
| 5155 | | - target_mw = (op_curr * port->pps_data.out_volt) / 1000; |
|---|
| 5833 | + target_mw = (req_op_curr * port->supply_voltage) / 1000; |
|---|
| 5156 | 5834 | if (target_mw < port->operating_snk_mw) { |
|---|
| 5157 | 5835 | ret = -EINVAL; |
|---|
| 5158 | 5836 | goto port_unlock; |
|---|
| .. | .. |
|---|
| 5166 | 5844 | } |
|---|
| 5167 | 5845 | |
|---|
| 5168 | 5846 | /* Round down operating current to align with PPS valid steps */ |
|---|
| 5169 | | - op_curr = op_curr - (op_curr % RDO_PROG_CURR_MA_STEP); |
|---|
| 5847 | + req_op_curr = req_op_curr - (req_op_curr % RDO_PROG_CURR_MA_STEP); |
|---|
| 5170 | 5848 | |
|---|
| 5171 | 5849 | reinit_completion(&port->pps_complete); |
|---|
| 5172 | | - port->pps_data.op_curr = op_curr; |
|---|
| 5850 | + port->pps_data.req_op_curr = req_op_curr; |
|---|
| 5173 | 5851 | port->pps_status = 0; |
|---|
| 5174 | 5852 | port->pps_pending = true; |
|---|
| 5175 | 5853 | mutex_unlock(&port->lock); |
|---|
| .. | .. |
|---|
| 5190 | 5868 | return ret; |
|---|
| 5191 | 5869 | } |
|---|
| 5192 | 5870 | |
|---|
| 5193 | | -static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 out_volt) |
|---|
| 5871 | +static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 req_out_volt) |
|---|
| 5194 | 5872 | { |
|---|
| 5195 | 5873 | unsigned int target_mw; |
|---|
| 5196 | 5874 | int ret; |
|---|
| .. | .. |
|---|
| 5208 | 5886 | goto port_unlock; |
|---|
| 5209 | 5887 | } |
|---|
| 5210 | 5888 | |
|---|
| 5211 | | - if (out_volt < port->pps_data.min_volt || |
|---|
| 5212 | | - out_volt > port->pps_data.max_volt) { |
|---|
| 5889 | + if (req_out_volt < port->pps_data.min_volt || |
|---|
| 5890 | + req_out_volt > port->pps_data.max_volt) { |
|---|
| 5213 | 5891 | ret = -EINVAL; |
|---|
| 5214 | 5892 | goto port_unlock; |
|---|
| 5215 | 5893 | } |
|---|
| 5216 | 5894 | |
|---|
| 5217 | | - target_mw = (port->pps_data.op_curr * out_volt) / 1000; |
|---|
| 5895 | + target_mw = (port->current_limit * req_out_volt) / 1000; |
|---|
| 5218 | 5896 | if (target_mw < port->operating_snk_mw) { |
|---|
| 5219 | 5897 | ret = -EINVAL; |
|---|
| 5220 | 5898 | goto port_unlock; |
|---|
| .. | .. |
|---|
| 5228 | 5906 | } |
|---|
| 5229 | 5907 | |
|---|
| 5230 | 5908 | /* Round down output voltage to align with PPS valid steps */ |
|---|
| 5231 | | - out_volt = out_volt - (out_volt % RDO_PROG_VOLT_MV_STEP); |
|---|
| 5909 | + req_out_volt = req_out_volt - (req_out_volt % RDO_PROG_VOLT_MV_STEP); |
|---|
| 5232 | 5910 | |
|---|
| 5233 | 5911 | reinit_completion(&port->pps_complete); |
|---|
| 5234 | | - port->pps_data.out_volt = out_volt; |
|---|
| 5912 | + port->pps_data.req_out_volt = req_out_volt; |
|---|
| 5235 | 5913 | port->pps_status = 0; |
|---|
| 5236 | 5914 | port->pps_pending = true; |
|---|
| 5237 | 5915 | mutex_unlock(&port->lock); |
|---|
| .. | .. |
|---|
| 5289 | 5967 | |
|---|
| 5290 | 5968 | /* Trigger PPS request or move back to standard PDO contract */ |
|---|
| 5291 | 5969 | if (activate) { |
|---|
| 5292 | | - port->pps_data.out_volt = port->supply_voltage; |
|---|
| 5293 | | - port->pps_data.op_curr = port->current_limit; |
|---|
| 5970 | + port->pps_data.req_out_volt = port->supply_voltage; |
|---|
| 5971 | + port->pps_data.req_op_curr = port->current_limit; |
|---|
| 5294 | 5972 | } |
|---|
| 5295 | 5973 | mutex_unlock(&port->lock); |
|---|
| 5296 | 5974 | |
|---|
| .. | .. |
|---|
| 5327 | 6005 | if (port->vbus_present) |
|---|
| 5328 | 6006 | port->vbus_never_low = true; |
|---|
| 5329 | 6007 | |
|---|
| 6008 | + /* |
|---|
| 6009 | + * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V. |
|---|
| 6010 | + * So implicitly vbus_vsafe0v = false. |
|---|
| 6011 | + * |
|---|
| 6012 | + * 2. When vbus_present is false and TCPC does NOT support querying |
|---|
| 6013 | + * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e. |
|---|
| 6014 | + * vbus_vsafe0v is true. |
|---|
| 6015 | + * |
|---|
| 6016 | + * 3. When vbus_present is false and TCPC does support querying vsafe0v, |
|---|
| 6017 | + * then, query tcpc for vsafe0v status. |
|---|
| 6018 | + */ |
|---|
| 6019 | + if (port->vbus_present) |
|---|
| 6020 | + port->vbus_vsafe0v = false; |
|---|
| 6021 | + else if (!port->tcpc->is_vbus_vsafe0v) |
|---|
| 6022 | + port->vbus_vsafe0v = true; |
|---|
| 6023 | + else |
|---|
| 6024 | + port->vbus_vsafe0v = port->tcpc->is_vbus_vsafe0v(port->tcpc); |
|---|
| 6025 | + |
|---|
| 5330 | 6026 | tcpm_set_state(port, tcpm_default_state(port), 0); |
|---|
| 5331 | 6027 | |
|---|
| 5332 | 6028 | if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0) |
|---|
| .. | .. |
|---|
| 5339 | 6035 | tcpm_set_state(port, PORT_RESET, 0); |
|---|
| 5340 | 6036 | } |
|---|
| 5341 | 6037 | |
|---|
| 5342 | | -static int tcpm_port_type_set(const struct typec_capability *cap, |
|---|
| 5343 | | - enum typec_port_type type) |
|---|
| 6038 | +static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type) |
|---|
| 5344 | 6039 | { |
|---|
| 5345 | | - struct tcpm_port *port = typec_cap_to_tcpm(cap); |
|---|
| 6040 | + struct tcpm_port *port = typec_get_drvdata(p); |
|---|
| 5346 | 6041 | |
|---|
| 5347 | 6042 | mutex_lock(&port->lock); |
|---|
| 5348 | 6043 | if (type == port->port_type) |
|---|
| .. | .. |
|---|
| 5367 | 6062 | return 0; |
|---|
| 5368 | 6063 | } |
|---|
| 5369 | 6064 | |
|---|
| 6065 | +static const struct typec_operations tcpm_ops = { |
|---|
| 6066 | + .try_role = tcpm_try_role, |
|---|
| 6067 | + .dr_set = tcpm_dr_set, |
|---|
| 6068 | + .pr_set = tcpm_pr_set, |
|---|
| 6069 | + .vconn_set = tcpm_vconn_set, |
|---|
| 6070 | + .port_type_set = tcpm_port_type_set |
|---|
| 6071 | +}; |
|---|
| 6072 | + |
|---|
| 5370 | 6073 | void tcpm_tcpc_reset(struct tcpm_port *port) |
|---|
| 5371 | 6074 | { |
|---|
| 5372 | 6075 | mutex_lock(&port->lock); |
|---|
| .. | .. |
|---|
| 5376 | 6079 | } |
|---|
| 5377 | 6080 | EXPORT_SYMBOL_GPL(tcpm_tcpc_reset); |
|---|
| 5378 | 6081 | |
|---|
| 5379 | | -static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo, |
|---|
| 5380 | | - unsigned int nr_pdo) |
|---|
| 5381 | | -{ |
|---|
| 5382 | | - unsigned int i; |
|---|
| 5383 | | - |
|---|
| 5384 | | - if (nr_pdo > PDO_MAX_OBJECTS) |
|---|
| 5385 | | - nr_pdo = PDO_MAX_OBJECTS; |
|---|
| 5386 | | - |
|---|
| 5387 | | - for (i = 0; i < nr_pdo; i++) |
|---|
| 5388 | | - dest_pdo[i] = src_pdo[i]; |
|---|
| 5389 | | - |
|---|
| 5390 | | - return nr_pdo; |
|---|
| 5391 | | -} |
|---|
| 5392 | | - |
|---|
| 5393 | 6082 | static int tcpm_fw_get_caps(struct tcpm_port *port, |
|---|
| 5394 | 6083 | struct fwnode_handle *fwnode) |
|---|
| 5395 | 6084 | { |
|---|
| 5396 | 6085 | const char *cap_str; |
|---|
| 5397 | 6086 | int ret; |
|---|
| 5398 | | - u32 mw, frs_current; |
|---|
| 6087 | + u32 mw, frs_current, pd_revision; |
|---|
| 5399 | 6088 | |
|---|
| 5400 | 6089 | if (!fwnode) |
|---|
| 5401 | 6090 | return -EINVAL; |
|---|
| 5402 | 6091 | |
|---|
| 5403 | | - /* get vbus regulator */ |
|---|
| 5404 | | - port->vbus = devm_regulator_get_optional(port->dev, "vbus"); |
|---|
| 5405 | | - if (IS_ERR(port->vbus)) { |
|---|
| 5406 | | - if (PTR_ERR(port->vbus) == -EPROBE_DEFER) |
|---|
| 5407 | | - return PTR_ERR(port->vbus); |
|---|
| 5408 | | - |
|---|
| 5409 | | - dev_info(port->dev, "may no need vbus regulator\n"); |
|---|
| 5410 | | - port->vbus = NULL; |
|---|
| 5411 | | - } |
|---|
| 6092 | + ret = fwnode_property_read_u32(fwnode, "pd-revision", |
|---|
| 6093 | + &pd_revision); |
|---|
| 6094 | + if (ret < 0) |
|---|
| 6095 | + port->typec_caps.pd_revision = 0x0300; |
|---|
| 6096 | + else |
|---|
| 6097 | + port->typec_caps.pd_revision = pd_revision & 0xffff; |
|---|
| 5412 | 6098 | |
|---|
| 5413 | 6099 | /* USB data support is optional */ |
|---|
| 5414 | 6100 | ret = fwnode_property_read_string(fwnode, "data-role", &cap_str); |
|---|
| 5415 | 6101 | if (ret == 0) { |
|---|
| 5416 | | - port->typec_caps.data = typec_find_port_data_role(cap_str); |
|---|
| 5417 | | - if (port->typec_caps.data < 0) |
|---|
| 5418 | | - return -EINVAL; |
|---|
| 6102 | + ret = typec_find_port_data_role(cap_str); |
|---|
| 6103 | + if (ret < 0) |
|---|
| 6104 | + return ret; |
|---|
| 6105 | + port->typec_caps.data = ret; |
|---|
| 5419 | 6106 | } |
|---|
| 5420 | 6107 | |
|---|
| 5421 | 6108 | ret = fwnode_property_read_string(fwnode, "power-role", &cap_str); |
|---|
| 5422 | 6109 | if (ret < 0) |
|---|
| 5423 | 6110 | return ret; |
|---|
| 5424 | 6111 | |
|---|
| 5425 | | - port->typec_caps.type = typec_find_port_power_role(cap_str); |
|---|
| 5426 | | - if (port->typec_caps.type < 0) |
|---|
| 5427 | | - return -EINVAL; |
|---|
| 6112 | + ret = typec_find_port_power_role(cap_str); |
|---|
| 6113 | + if (ret < 0) |
|---|
| 6114 | + return ret; |
|---|
| 6115 | + port->typec_caps.type = ret; |
|---|
| 5428 | 6116 | port->port_type = port->typec_caps.type; |
|---|
| 5429 | 6117 | |
|---|
| 6118 | + port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop"); |
|---|
| 5430 | 6119 | if (port->port_type == TYPEC_PORT_SNK) |
|---|
| 5431 | 6120 | goto sink; |
|---|
| 5432 | 6121 | |
|---|
| 5433 | 6122 | /* Get source pdos */ |
|---|
| 5434 | | - ret = fwnode_property_read_u32_array(fwnode, "source-pdos", |
|---|
| 5435 | | - NULL, 0); |
|---|
| 6123 | + ret = fwnode_property_count_u32(fwnode, "source-pdos"); |
|---|
| 5436 | 6124 | if (ret <= 0) |
|---|
| 5437 | 6125 | return -EINVAL; |
|---|
| 5438 | 6126 | |
|---|
| .. | .. |
|---|
| 5456 | 6144 | return -EINVAL; |
|---|
| 5457 | 6145 | sink: |
|---|
| 5458 | 6146 | /* Get sink pdos */ |
|---|
| 5459 | | - ret = fwnode_property_read_u32_array(fwnode, "sink-pdos", |
|---|
| 5460 | | - NULL, 0); |
|---|
| 6147 | + ret = fwnode_property_count_u32(fwnode, "sink-pdos"); |
|---|
| 5461 | 6148 | if (ret <= 0) |
|---|
| 5462 | 6149 | return -EINVAL; |
|---|
| 5463 | 6150 | |
|---|
| .. | .. |
|---|
| 5476 | 6163 | |
|---|
| 5477 | 6164 | /* FRS can only be supported byb DRP ports */ |
|---|
| 5478 | 6165 | if (port->port_type == TYPEC_PORT_DRP) { |
|---|
| 5479 | | - ret = fwnode_property_read_u32(fwnode, "frs-typec-current", &frs_current); |
|---|
| 6166 | + ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current", |
|---|
| 6167 | + &frs_current); |
|---|
| 5480 | 6168 | if (ret >= 0 && frs_current <= FRS_5V_3A) |
|---|
| 5481 | | - port->frs_current = frs_current; |
|---|
| 6169 | + port->new_source_frs_current = frs_current; |
|---|
| 5482 | 6170 | } |
|---|
| 5483 | 6171 | |
|---|
| 5484 | 6172 | /* sink-vdos is optional */ |
|---|
| .. | .. |
|---|
| 5495 | 6183 | return ret; |
|---|
| 5496 | 6184 | } |
|---|
| 5497 | 6185 | |
|---|
| 5498 | | - return 0; |
|---|
| 5499 | | -} |
|---|
| 6186 | + /* If sink-vdos is found, sink-vdos-v1 is expected for backward compatibility. */ |
|---|
| 6187 | + if (port->nr_snk_vdo) { |
|---|
| 6188 | + ret = fwnode_property_count_u32(fwnode, "sink-vdos-v1"); |
|---|
| 6189 | + if (ret < 0) |
|---|
| 6190 | + return ret; |
|---|
| 6191 | + else if (ret == 0) |
|---|
| 6192 | + return -ENODATA; |
|---|
| 5500 | 6193 | |
|---|
| 5501 | | -int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo, |
|---|
| 5502 | | - unsigned int nr_pdo) |
|---|
| 5503 | | -{ |
|---|
| 5504 | | - if (tcpm_validate_caps(port, pdo, nr_pdo)) |
|---|
| 5505 | | - return -EINVAL; |
|---|
| 5506 | | - |
|---|
| 5507 | | - mutex_lock(&port->lock); |
|---|
| 5508 | | - port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, pdo, nr_pdo); |
|---|
| 5509 | | - switch (port->state) { |
|---|
| 5510 | | - case SRC_UNATTACHED: |
|---|
| 5511 | | - case SRC_ATTACH_WAIT: |
|---|
| 5512 | | - case SRC_TRYWAIT: |
|---|
| 5513 | | - tcpm_set_cc(port, tcpm_rp_cc(port)); |
|---|
| 5514 | | - break; |
|---|
| 5515 | | - case SRC_SEND_CAPABILITIES: |
|---|
| 5516 | | - case SRC_NEGOTIATE_CAPABILITIES: |
|---|
| 5517 | | - case SRC_READY: |
|---|
| 5518 | | - case SRC_WAIT_NEW_CAPABILITIES: |
|---|
| 5519 | | - tcpm_set_cc(port, tcpm_rp_cc(port)); |
|---|
| 5520 | | - tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); |
|---|
| 5521 | | - break; |
|---|
| 5522 | | - default: |
|---|
| 5523 | | - break; |
|---|
| 6194 | + port->nr_snk_vdo_v1 = min(ret, VDO_MAX_OBJECTS); |
|---|
| 6195 | + ret = fwnode_property_read_u32_array(fwnode, "sink-vdos-v1", |
|---|
| 6196 | + port->snk_vdo_v1, |
|---|
| 6197 | + port->nr_snk_vdo_v1); |
|---|
| 6198 | + if (ret < 0) |
|---|
| 6199 | + return ret; |
|---|
| 5524 | 6200 | } |
|---|
| 5525 | | - mutex_unlock(&port->lock); |
|---|
| 6201 | + |
|---|
| 5526 | 6202 | return 0; |
|---|
| 5527 | 6203 | } |
|---|
| 5528 | | -EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities); |
|---|
| 5529 | 6204 | |
|---|
| 5530 | | -int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, |
|---|
| 5531 | | - unsigned int nr_pdo, |
|---|
| 6205 | +static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo, unsigned int nr_pdo) |
|---|
| 6206 | +{ |
|---|
| 6207 | + unsigned int i; |
|---|
| 6208 | + |
|---|
| 6209 | + if (nr_pdo > PDO_MAX_OBJECTS) |
|---|
| 6210 | + nr_pdo = PDO_MAX_OBJECTS; |
|---|
| 6211 | + |
|---|
| 6212 | + for (i = 0; i < nr_pdo; i++) |
|---|
| 6213 | + dest_pdo[i] = src_pdo[i]; |
|---|
| 6214 | + |
|---|
| 6215 | + return nr_pdo; |
|---|
| 6216 | +} |
|---|
| 6217 | + |
|---|
| 6218 | +int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, unsigned int nr_pdo, |
|---|
| 5532 | 6219 | unsigned int operating_snk_mw) |
|---|
| 5533 | 6220 | { |
|---|
| 6221 | + int ret = 0; |
|---|
| 6222 | + |
|---|
| 5534 | 6223 | if (tcpm_validate_caps(port, pdo, nr_pdo)) |
|---|
| 5535 | 6224 | return -EINVAL; |
|---|
| 5536 | 6225 | |
|---|
| 5537 | 6226 | mutex_lock(&port->lock); |
|---|
| 5538 | 6227 | port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo); |
|---|
| 5539 | 6228 | port->operating_snk_mw = operating_snk_mw; |
|---|
| 5540 | | - port->update_sink_caps = true; |
|---|
| 5541 | 6229 | |
|---|
| 5542 | 6230 | switch (port->state) { |
|---|
| 5543 | 6231 | case SNK_NEGOTIATE_CAPABILITIES: |
|---|
| .. | .. |
|---|
| 5546 | 6234 | case SNK_TRANSITION_SINK: |
|---|
| 5547 | 6235 | case SNK_TRANSITION_SINK_VBUS: |
|---|
| 5548 | 6236 | if (port->pps_data.active) |
|---|
| 5549 | | - tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0); |
|---|
| 6237 | + port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; |
|---|
| 6238 | + else if (port->pd_capable) |
|---|
| 6239 | + port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES; |
|---|
| 5550 | 6240 | else |
|---|
| 5551 | | - tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); |
|---|
| 6241 | + break; |
|---|
| 6242 | + |
|---|
| 6243 | + port->update_sink_caps = true; |
|---|
| 6244 | + |
|---|
| 6245 | + ret = tcpm_ams_start(port, POWER_NEGOTIATION); |
|---|
| 6246 | + if (ret == -EAGAIN) { |
|---|
| 6247 | + port->upcoming_state = INVALID_STATE; |
|---|
| 6248 | + break; |
|---|
| 6249 | + } |
|---|
| 5552 | 6250 | break; |
|---|
| 5553 | 6251 | default: |
|---|
| 5554 | 6252 | break; |
|---|
| 5555 | 6253 | } |
|---|
| 5556 | 6254 | mutex_unlock(&port->lock); |
|---|
| 5557 | | - return 0; |
|---|
| 6255 | + return ret; |
|---|
| 5558 | 6256 | } |
|---|
| 5559 | 6257 | EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities); |
|---|
| 5560 | 6258 | |
|---|
| .. | .. |
|---|
| 5639 | 6337 | return 0; |
|---|
| 5640 | 6338 | } |
|---|
| 5641 | 6339 | |
|---|
| 6340 | +static int tcpm_psy_get_input_power_limit(struct tcpm_port *port, |
|---|
| 6341 | + union power_supply_propval *val) |
|---|
| 6342 | +{ |
|---|
| 6343 | + unsigned int src_mv, src_ma, max_src_mw = 0; |
|---|
| 6344 | + unsigned int i, tmp; |
|---|
| 6345 | + |
|---|
| 6346 | + for (i = 0; i < port->nr_source_caps; i++) { |
|---|
| 6347 | + u32 pdo = port->source_caps[i]; |
|---|
| 6348 | + |
|---|
| 6349 | + if (pdo_type(pdo) == PDO_TYPE_FIXED) { |
|---|
| 6350 | + src_mv = pdo_fixed_voltage(pdo); |
|---|
| 6351 | + src_ma = pdo_max_current(pdo); |
|---|
| 6352 | + tmp = src_mv * src_ma / 1000; |
|---|
| 6353 | + max_src_mw = tmp > max_src_mw ? tmp : max_src_mw; |
|---|
| 6354 | + } |
|---|
| 6355 | + } |
|---|
| 6356 | + |
|---|
| 6357 | + val->intval = max_src_mw; |
|---|
| 6358 | + return 0; |
|---|
| 6359 | +} |
|---|
| 6360 | + |
|---|
| 5642 | 6361 | static int tcpm_psy_get_prop(struct power_supply *psy, |
|---|
| 5643 | 6362 | enum power_supply_property psp, |
|---|
| 5644 | 6363 | union power_supply_propval *val) |
|---|
| .. | .. |
|---|
| 5668 | 6387 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
|---|
| 5669 | 6388 | ret = tcpm_psy_get_current_now(port, val); |
|---|
| 5670 | 6389 | break; |
|---|
| 6390 | + case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT: |
|---|
| 6391 | + tcpm_psy_get_input_power_limit(port, val); |
|---|
| 6392 | + break; |
|---|
| 5671 | 6393 | default: |
|---|
| 5672 | 6394 | ret = -EINVAL; |
|---|
| 5673 | 6395 | break; |
|---|
| 5674 | 6396 | } |
|---|
| 5675 | | - |
|---|
| 5676 | 6397 | return ret; |
|---|
| 5677 | 6398 | } |
|---|
| 5678 | 6399 | |
|---|
| .. | .. |
|---|
| 5724 | 6445 | ret = -EINVAL; |
|---|
| 5725 | 6446 | break; |
|---|
| 5726 | 6447 | } |
|---|
| 5727 | | - |
|---|
| 6448 | + power_supply_changed(port->psy); |
|---|
| 5728 | 6449 | return ret; |
|---|
| 5729 | 6450 | } |
|---|
| 5730 | 6451 | |
|---|
| .. | .. |
|---|
| 5807 | 6528 | return HRTIMER_NORESTART; |
|---|
| 5808 | 6529 | } |
|---|
| 5809 | 6530 | |
|---|
| 5810 | | -static enum hrtimer_restart data_role_swap_timer_handler(struct hrtimer *timer) |
|---|
| 6531 | +static enum hrtimer_restart send_discover_timer_handler(struct hrtimer *timer) |
|---|
| 5811 | 6532 | { |
|---|
| 5812 | | - struct tcpm_port *port = |
|---|
| 5813 | | - container_of(timer, struct tcpm_port, data_role_swap_timer); |
|---|
| 6533 | + struct tcpm_port *port = container_of(timer, struct tcpm_port, send_discover_timer); |
|---|
| 5814 | 6534 | |
|---|
| 5815 | | - kthread_queue_work(port->wq, &port->data_role_swap); |
|---|
| 6535 | + kthread_queue_work(port->wq, &port->send_discover_work); |
|---|
| 5816 | 6536 | return HRTIMER_NORESTART; |
|---|
| 5817 | | -} |
|---|
| 5818 | | - |
|---|
| 5819 | | -static int tcpm_extcon_register(struct tcpm_port *port) |
|---|
| 5820 | | -{ |
|---|
| 5821 | | - struct extcon_dev *extcon; |
|---|
| 5822 | | - int ret; |
|---|
| 5823 | | - |
|---|
| 5824 | | - extcon = devm_extcon_dev_allocate(port->dev, tcpm_cable); |
|---|
| 5825 | | - if (IS_ERR(extcon)) { |
|---|
| 5826 | | - dev_err(port->dev, "allocate extcon failed\n"); |
|---|
| 5827 | | - return -ENOMEM; |
|---|
| 5828 | | - } |
|---|
| 5829 | | - |
|---|
| 5830 | | - ret = devm_extcon_dev_register(port->dev, extcon); |
|---|
| 5831 | | - if (ret) { |
|---|
| 5832 | | - dev_err(port->dev, "failed to register extcon: %d\n", ret); |
|---|
| 5833 | | - goto init_err; |
|---|
| 5834 | | - } |
|---|
| 5835 | | - |
|---|
| 5836 | | - ret = extcon_set_property_capability(extcon, EXTCON_USB, |
|---|
| 5837 | | - EXTCON_PROP_USB_TYPEC_POLARITY); |
|---|
| 5838 | | - if (ret) { |
|---|
| 5839 | | - dev_err(port->dev, "failed to set USB property capability: %d\n", ret); |
|---|
| 5840 | | - goto init_err; |
|---|
| 5841 | | - } |
|---|
| 5842 | | - |
|---|
| 5843 | | - ret = extcon_set_property_capability(extcon, EXTCON_USB_HOST, |
|---|
| 5844 | | - EXTCON_PROP_USB_TYPEC_POLARITY); |
|---|
| 5845 | | - if (ret) { |
|---|
| 5846 | | - dev_err(port->dev, "failed to set USB_HOST property capability: %d\n", ret); |
|---|
| 5847 | | - goto init_err; |
|---|
| 5848 | | - } |
|---|
| 5849 | | - |
|---|
| 5850 | | - ret = extcon_set_property_capability(extcon, EXTCON_DISP_DP, |
|---|
| 5851 | | - EXTCON_PROP_USB_TYPEC_POLARITY); |
|---|
| 5852 | | - if (ret) { |
|---|
| 5853 | | - dev_err(port->dev, "failed to set DISP_DP property capability: %d\n", ret); |
|---|
| 5854 | | - goto init_err; |
|---|
| 5855 | | - } |
|---|
| 5856 | | - |
|---|
| 5857 | | - ret = extcon_set_property_capability(extcon, EXTCON_USB, EXTCON_PROP_USB_SS); |
|---|
| 5858 | | - if (ret) { |
|---|
| 5859 | | - dev_err(port->dev, "failed to set USB USB_SS property capability: %d\n", ret); |
|---|
| 5860 | | - goto init_err; |
|---|
| 5861 | | - } |
|---|
| 5862 | | - |
|---|
| 5863 | | - ret = extcon_set_property_capability(extcon, EXTCON_USB_HOST, EXTCON_PROP_USB_SS); |
|---|
| 5864 | | - if (ret) { |
|---|
| 5865 | | - dev_err(port->dev, "failed to set USB_HOST USB_SS property capability: %d", ret); |
|---|
| 5866 | | - goto init_err; |
|---|
| 5867 | | - } |
|---|
| 5868 | | - |
|---|
| 5869 | | - ret = extcon_set_property_capability(extcon, EXTCON_DISP_DP, EXTCON_PROP_USB_SS); |
|---|
| 5870 | | - if (ret) { |
|---|
| 5871 | | - dev_err(port->dev, "failed to set DISP_DP USB_SS property capability: %d", ret); |
|---|
| 5872 | | - goto init_err; |
|---|
| 5873 | | - } |
|---|
| 5874 | | - |
|---|
| 5875 | | - ret = extcon_set_property_capability(extcon, EXTCON_CHG_USB_FAST, |
|---|
| 5876 | | - EXTCON_PROP_USB_TYPEC_POLARITY); |
|---|
| 5877 | | - if (ret) { |
|---|
| 5878 | | - dev_err(port->dev, "failed to set USB_PD property capability: %d\n", ret); |
|---|
| 5879 | | - goto init_err; |
|---|
| 5880 | | - } |
|---|
| 5881 | | - |
|---|
| 5882 | | - port->extcon = extcon; |
|---|
| 5883 | | - tcpm_log(port, "init extcon finished\n"); |
|---|
| 5884 | | - |
|---|
| 5885 | | - return 0; |
|---|
| 5886 | | - |
|---|
| 5887 | | -init_err: |
|---|
| 5888 | | - return ret; |
|---|
| 5889 | 6537 | } |
|---|
| 5890 | 6538 | |
|---|
| 5891 | 6539 | struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) |
|---|
| 5892 | 6540 | { |
|---|
| 5893 | 6541 | struct tcpm_port *port; |
|---|
| 5894 | 6542 | int err; |
|---|
| 5895 | | - struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
|---|
| 5896 | 6543 | |
|---|
| 5897 | 6544 | if (!dev || !tcpc || |
|---|
| 5898 | 6545 | !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || |
|---|
| .. | .. |
|---|
| 5909 | 6556 | |
|---|
| 5910 | 6557 | mutex_init(&port->lock); |
|---|
| 5911 | 6558 | mutex_init(&port->swap_lock); |
|---|
| 6559 | +#ifdef CONFIG_NO_GKI |
|---|
| 5912 | 6560 | mutex_init(&port->pd_handler_lock); |
|---|
| 6561 | +#endif |
|---|
| 5913 | 6562 | |
|---|
| 5914 | 6563 | port->wq = kthread_create_worker(0, dev_name(dev)); |
|---|
| 5915 | 6564 | if (IS_ERR(port->wq)) |
|---|
| 5916 | 6565 | return ERR_CAST(port->wq); |
|---|
| 5917 | | - sched_setscheduler_nocheck(port->wq->task, SCHED_FIFO, ¶m); |
|---|
| 6566 | + sched_set_fifo(port->wq->task); |
|---|
| 5918 | 6567 | |
|---|
| 5919 | 6568 | kthread_init_work(&port->state_machine, tcpm_state_machine_work); |
|---|
| 5920 | 6569 | kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work); |
|---|
| 5921 | 6570 | kthread_init_work(&port->event_work, tcpm_pd_event_handler); |
|---|
| 5922 | 6571 | kthread_init_work(&port->enable_frs, tcpm_enable_frs_work); |
|---|
| 5923 | | - kthread_init_work(&port->data_role_swap, tcpm_data_role_swap_work); |
|---|
| 6572 | + kthread_init_work(&port->send_discover_work, tcpm_send_discover_work); |
|---|
| 5924 | 6573 | hrtimer_init(&port->state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
|---|
| 5925 | 6574 | port->state_machine_timer.function = state_machine_timer_handler; |
|---|
| 5926 | 6575 | hrtimer_init(&port->vdm_state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
|---|
| 5927 | 6576 | port->vdm_state_machine_timer.function = vdm_state_machine_timer_handler; |
|---|
| 5928 | 6577 | hrtimer_init(&port->enable_frs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
|---|
| 5929 | 6578 | port->enable_frs_timer.function = enable_frs_timer_handler; |
|---|
| 5930 | | - hrtimer_init(&port->data_role_swap_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
|---|
| 5931 | | - port->data_role_swap_timer.function = data_role_swap_timer_handler; |
|---|
| 6579 | + hrtimer_init(&port->send_discover_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
|---|
| 6580 | + port->send_discover_timer.function = send_discover_timer_handler; |
|---|
| 5932 | 6581 | |
|---|
| 5933 | 6582 | spin_lock_init(&port->pd_event_lock); |
|---|
| 5934 | 6583 | |
|---|
| .. | .. |
|---|
| 5936 | 6585 | init_completion(&port->swap_complete); |
|---|
| 5937 | 6586 | init_completion(&port->pps_complete); |
|---|
| 5938 | 6587 | tcpm_debugfs_init(port); |
|---|
| 5939 | | - |
|---|
| 5940 | | - /* init extcon */ |
|---|
| 5941 | | - err = tcpm_extcon_register(port); |
|---|
| 5942 | | - if (err) |
|---|
| 5943 | | - goto out_destroy_wq; |
|---|
| 5944 | 6588 | |
|---|
| 5945 | 6589 | err = tcpm_fw_get_caps(port, tcpc->fwnode); |
|---|
| 5946 | 6590 | if (err < 0) |
|---|
| .. | .. |
|---|
| 5950 | 6594 | |
|---|
| 5951 | 6595 | port->typec_caps.fwnode = tcpc->fwnode; |
|---|
| 5952 | 6596 | port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */ |
|---|
| 5953 | | - port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */ |
|---|
| 5954 | | - port->typec_caps.dr_set = tcpm_dr_set; |
|---|
| 5955 | | - port->typec_caps.pr_set = tcpm_pr_set; |
|---|
| 5956 | | - port->typec_caps.vconn_set = tcpm_vconn_set; |
|---|
| 5957 | | - port->typec_caps.try_role = tcpm_try_role; |
|---|
| 5958 | | - port->typec_caps.port_type_set = tcpm_port_type_set; |
|---|
| 6597 | + port->typec_caps.svdm_version = SVDM_VER_2_0; |
|---|
| 6598 | + port->typec_caps.driver_data = port; |
|---|
| 6599 | + port->typec_caps.ops = &tcpm_ops; |
|---|
| 6600 | + port->typec_caps.orientation_aware = 1; |
|---|
| 5959 | 6601 | |
|---|
| 5960 | 6602 | port->partner_desc.identity = &port->partner_ident; |
|---|
| 5961 | 6603 | port->port_type = port->typec_caps.type; |
|---|
| .. | .. |
|---|
| 5968 | 6610 | |
|---|
| 5969 | 6611 | err = devm_tcpm_psy_register(port); |
|---|
| 5970 | 6612 | if (err) |
|---|
| 5971 | | - goto out_destroy_wq; |
|---|
| 6613 | + goto out_role_sw_put; |
|---|
| 6614 | + power_supply_changed(port->psy); |
|---|
| 5972 | 6615 | |
|---|
| 5973 | 6616 | port->typec_port = typec_register_port(port->dev, &port->typec_caps); |
|---|
| 5974 | 6617 | if (IS_ERR(port->typec_port)) { |
|---|
| 5975 | 6618 | err = PTR_ERR(port->typec_port); |
|---|
| 5976 | | - goto out_destroy_wq; |
|---|
| 6619 | + goto out_role_sw_put; |
|---|
| 5977 | 6620 | } |
|---|
| 5978 | 6621 | |
|---|
| 5979 | 6622 | typec_port_register_altmodes(port->typec_port, |
|---|
| .. | .. |
|---|
| 5987 | 6630 | tcpm_log(port, "%s: registered", dev_name(dev)); |
|---|
| 5988 | 6631 | return port; |
|---|
| 5989 | 6632 | |
|---|
| 5990 | | -out_destroy_wq: |
|---|
| 6633 | +out_role_sw_put: |
|---|
| 5991 | 6634 | usb_role_switch_put(port->role_sw); |
|---|
| 6635 | +out_destroy_wq: |
|---|
| 6636 | + tcpm_debugfs_exit(port); |
|---|
| 5992 | 6637 | kthread_destroy_worker(port->wq); |
|---|
| 5993 | 6638 | return ERR_PTR(err); |
|---|
| 5994 | 6639 | } |
|---|
| .. | .. |
|---|
| 5998 | 6643 | { |
|---|
| 5999 | 6644 | int i; |
|---|
| 6000 | 6645 | |
|---|
| 6646 | + hrtimer_cancel(&port->send_discover_timer); |
|---|
| 6001 | 6647 | hrtimer_cancel(&port->enable_frs_timer); |
|---|
| 6002 | 6648 | hrtimer_cancel(&port->vdm_state_machine_timer); |
|---|
| 6003 | 6649 | hrtimer_cancel(&port->state_machine_timer); |
|---|
| 6004 | | - hrtimer_cancel(&port->data_role_swap_timer); |
|---|
| 6005 | 6650 | |
|---|
| 6006 | 6651 | tcpm_reset_port(port); |
|---|
| 6007 | 6652 | for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++) |
|---|