1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB Type-C Connector System Software Interface driver 4 * 5 * Copyright (C) 2017, Intel Corporation 6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 */ 8 9 #include <linux/completion.h> 10 #include <linux/property.h> 11 #include <linux/device.h> 12 #include <linux/module.h> 13 #include <linux/delay.h> 14 #include <linux/slab.h> 15 #include <linux/usb/typec_dp.h> 16 17 #include "ucsi.h" 18 #include "trace.h" 19 20 /* 21 * UCSI_TIMEOUT_MS - PPM communication timeout 22 * 23 * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI 24 * specification) here as reference, but unfortunately we can't. It is very 25 * difficult to estimate the time it takes for the system to process the command 26 * before it is actually passed to the PPM. 27 */ 28 #define UCSI_TIMEOUT_MS 5000 29 30 /* 31 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests 32 * 33 * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even 34 * if the PPM does not generate Connector Change events before that with 35 * partners that do not support USB Power Delivery, this should still work. 36 */ 37 #define UCSI_SWAP_TIMEOUT_MS 5000 38 39 static int ucsi_acknowledge_command(struct ucsi *ucsi) 40 { 41 u64 ctrl; 42 43 ctrl = UCSI_ACK_CC_CI; 44 ctrl |= UCSI_ACK_COMMAND_COMPLETE; 45 46 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl)); 47 } 48 49 static int ucsi_acknowledge_connector_change(struct ucsi *ucsi) 50 { 51 u64 ctrl; 52 53 ctrl = UCSI_ACK_CC_CI; 54 ctrl |= UCSI_ACK_CONNECTOR_CHANGE; 55 56 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl)); 57 } 58 59 static int ucsi_exec_command(struct ucsi *ucsi, u64 command); 60 61 static int ucsi_read_error(struct ucsi *ucsi) 62 { 63 u16 error; 64 int ret; 65 66 /* Acknowledge the command that failed */ 67 ret = ucsi_acknowledge_command(ucsi); 68 if (ret) 69 return ret; 70 71 ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS); 72 if (ret < 0) 73 return ret; 74 75 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error)); 76 if (ret) 77 return ret; 78 79 ret = ucsi_acknowledge_command(ucsi); 80 if (ret) 81 return ret; 82 83 switch (error) { 84 case UCSI_ERROR_INCOMPATIBLE_PARTNER: 85 return -EOPNOTSUPP; 86 case UCSI_ERROR_CC_COMMUNICATION_ERR: 87 return -ECOMM; 88 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL: 89 return -EPROTO; 90 case UCSI_ERROR_DEAD_BATTERY: 91 dev_warn(ucsi->dev, "Dead battery condition!\n"); 92 return -EPERM; 93 case UCSI_ERROR_INVALID_CON_NUM: 94 case UCSI_ERROR_UNREGONIZED_CMD: 95 case UCSI_ERROR_INVALID_CMD_ARGUMENT: 96 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error); 97 return -EINVAL; 98 case UCSI_ERROR_OVERCURRENT: 99 dev_warn(ucsi->dev, "Overcurrent condition\n"); 100 break; 101 case UCSI_ERROR_PARTNER_REJECTED_SWAP: 102 dev_warn(ucsi->dev, "Partner rejected swap\n"); 103 break; 104 case UCSI_ERROR_HARD_RESET: 105 dev_warn(ucsi->dev, "Hard reset occurred\n"); 106 break; 107 case UCSI_ERROR_PPM_POLICY_CONFLICT: 108 dev_warn(ucsi->dev, "PPM Policy conflict\n"); 109 break; 110 case UCSI_ERROR_SWAP_REJECTED: 111 dev_warn(ucsi->dev, "Swap rejected\n"); 112 break; 113 case UCSI_ERROR_UNDEFINED: 114 default: 115 dev_err(ucsi->dev, "unknown error %u\n", error); 116 break; 117 } 118 119 return -EIO; 120 } 121 122 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) 123 { 124 u32 cci; 125 int ret; 126 127 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); 128 if (ret) 129 return ret; 130 131 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); 132 if (ret) 133 return ret; 134 135 if (cci & UCSI_CCI_BUSY) { 136 ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0); 137 return -EBUSY; 138 } 139 140 if (!(cci & UCSI_CCI_COMMAND_COMPLETE)) 141 return -EIO; 142 143 if (cci & UCSI_CCI_NOT_SUPPORTED) 144 return -EOPNOTSUPP; 145 146 if (cci & UCSI_CCI_ERROR) { 147 if (cmd == UCSI_GET_ERROR_STATUS) 148 return -EIO; 149 return ucsi_read_error(ucsi); 150 } 151 152 return UCSI_CCI_LENGTH(cci); 153 } 154 155 int ucsi_send_command(struct ucsi *ucsi, u64 command, 156 void *data, size_t size) 157 { 158 u8 length; 159 int ret; 160 161 mutex_lock(&ucsi->ppm_lock); 162 163 ret = ucsi_exec_command(ucsi, command); 164 if (ret < 0) 165 goto out; 166 167 length = ret; 168 169 if (data) { 170 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size); 171 if (ret) 172 goto out; 173 } 174 175 ret = ucsi_acknowledge_command(ucsi); 176 if (ret) 177 goto out; 178 179 ret = length; 180 out: 181 mutex_unlock(&ucsi->ppm_lock); 182 return ret; 183 } 184 EXPORT_SYMBOL_GPL(ucsi_send_command); 185 186 /* -------------------------------------------------------------------------- */ 187 188 struct ucsi_work { 189 struct delayed_work work; 190 struct list_head node; 191 unsigned long delay; 192 unsigned int count; 193 struct ucsi_connector *con; 194 int (*cb)(struct ucsi_connector *); 195 }; 196 197 static void ucsi_poll_worker(struct work_struct *work) 198 { 199 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work); 200 struct ucsi_connector *con = uwork->con; 201 int ret; 202 203 mutex_lock(&con->lock); 204 205 if (!con->partner) { 206 list_del(&uwork->node); 207 mutex_unlock(&con->lock); 208 kfree(uwork); 209 return; 210 } 211 212 ret = uwork->cb(con); 213 214 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) { 215 queue_delayed_work(con->wq, &uwork->work, uwork->delay); 216 } else { 217 list_del(&uwork->node); 218 kfree(uwork); 219 } 220 221 mutex_unlock(&con->lock); 222 } 223 224 static int ucsi_partner_task(struct ucsi_connector *con, 225 int (*cb)(struct ucsi_connector *), 226 int retries, unsigned long delay) 227 { 228 struct ucsi_work *uwork; 229 230 if (!con->partner) 231 return 0; 232 233 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL); 234 if (!uwork) 235 return -ENOMEM; 236 237 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker); 238 uwork->count = retries; 239 uwork->delay = delay; 240 uwork->con = con; 241 uwork->cb = cb; 242 243 list_add_tail(&uwork->node, &con->partner_tasks); 244 queue_delayed_work(con->wq, &uwork->work, delay); 245 246 return 0; 247 } 248 249 /* -------------------------------------------------------------------------- */ 250 251 void ucsi_altmode_update_active(struct ucsi_connector *con) 252 { 253 const struct typec_altmode *altmode = NULL; 254 u64 command; 255 int ret; 256 u8 cur; 257 int i; 258 259 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num); 260 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur)); 261 if (ret < 0) { 262 if (con->ucsi->version > 0x0100) { 263 dev_err(con->ucsi->dev, 264 "GET_CURRENT_CAM command failed\n"); 265 return; 266 } 267 cur = 0xff; 268 } 269 270 if (cur < UCSI_MAX_ALTMODES) 271 altmode = typec_altmode_get_partner(con->port_altmode[cur]); 272 273 for (i = 0; con->partner_altmode[i]; i++) 274 typec_altmode_update_active(con->partner_altmode[i], 275 con->partner_altmode[i] == altmode); 276 } 277 278 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid) 279 { 280 u8 mode = 1; 281 int i; 282 283 for (i = 0; alt[i]; i++) { 284 if (i > MODE_DISCOVERY_MAX) 285 return -ERANGE; 286 287 if (alt[i]->svid == svid) 288 mode++; 289 } 290 291 return mode; 292 } 293 294 static int ucsi_next_altmode(struct typec_altmode **alt) 295 { 296 int i = 0; 297 298 for (i = 0; i < UCSI_MAX_ALTMODES; i++) 299 if (!alt[i]) 300 return i; 301 302 return -ENOENT; 303 } 304 305 static int ucsi_get_num_altmode(struct typec_altmode **alt) 306 { 307 int i; 308 309 for (i = 0; i < UCSI_MAX_ALTMODES; i++) 310 if (!alt[i]) 311 break; 312 313 return i; 314 } 315 316 static int ucsi_register_altmode(struct ucsi_connector *con, 317 struct typec_altmode_desc *desc, 318 u8 recipient) 319 { 320 struct typec_altmode *alt; 321 bool override; 322 int ret; 323 int i; 324 325 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE); 326 327 switch (recipient) { 328 case UCSI_RECIPIENT_CON: 329 i = ucsi_next_altmode(con->port_altmode); 330 if (i < 0) { 331 ret = i; 332 goto err; 333 } 334 335 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid); 336 if (ret < 0) 337 return ret; 338 339 desc->mode = ret; 340 341 switch (desc->svid) { 342 case USB_TYPEC_DP_SID: 343 alt = ucsi_register_displayport(con, override, i, desc); 344 break; 345 case USB_TYPEC_NVIDIA_VLINK_SID: 346 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO) 347 alt = typec_port_register_altmode(con->port, 348 desc); 349 else 350 alt = ucsi_register_displayport(con, override, 351 i, desc); 352 break; 353 default: 354 alt = typec_port_register_altmode(con->port, desc); 355 break; 356 } 357 358 if (IS_ERR(alt)) { 359 ret = PTR_ERR(alt); 360 goto err; 361 } 362 363 con->port_altmode[i] = alt; 364 break; 365 case UCSI_RECIPIENT_SOP: 366 i = ucsi_next_altmode(con->partner_altmode); 367 if (i < 0) { 368 ret = i; 369 goto err; 370 } 371 372 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid); 373 if (ret < 0) 374 return ret; 375 376 desc->mode = ret; 377 378 alt = typec_partner_register_altmode(con->partner, desc); 379 if (IS_ERR(alt)) { 380 ret = PTR_ERR(alt); 381 goto err; 382 } 383 384 con->partner_altmode[i] = alt; 385 break; 386 default: 387 return -EINVAL; 388 } 389 390 trace_ucsi_register_altmode(recipient, alt); 391 392 return 0; 393 394 err: 395 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n", 396 desc->svid, desc->mode); 397 398 return ret; 399 } 400 401 static int 402 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient) 403 { 404 int max_altmodes = UCSI_MAX_ALTMODES; 405 struct typec_altmode_desc desc; 406 struct ucsi_altmode alt; 407 struct ucsi_altmode orig[UCSI_MAX_ALTMODES]; 408 struct ucsi_altmode updated[UCSI_MAX_ALTMODES]; 409 struct ucsi *ucsi = con->ucsi; 410 bool multi_dp = false; 411 u64 command; 412 int ret; 413 int len; 414 int i; 415 int k = 0; 416 417 if (recipient == UCSI_RECIPIENT_CON) 418 max_altmodes = con->ucsi->cap.num_alt_modes; 419 420 memset(orig, 0, sizeof(orig)); 421 memset(updated, 0, sizeof(updated)); 422 423 /* First get all the alternate modes */ 424 for (i = 0; i < max_altmodes; i++) { 425 memset(&alt, 0, sizeof(alt)); 426 command = UCSI_GET_ALTERNATE_MODES; 427 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); 428 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); 429 command |= UCSI_GET_ALTMODE_OFFSET(i); 430 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt)); 431 /* 432 * We are collecting all altmodes first and then registering. 433 * Some type-C device will return zero length data beyond last 434 * alternate modes. We should not return if length is zero. 435 */ 436 if (len < 0) 437 return len; 438 439 /* We got all altmodes, now break out and register them */ 440 if (!len || !alt.svid) 441 break; 442 443 orig[k].mid = alt.mid; 444 orig[k].svid = alt.svid; 445 k++; 446 } 447 /* 448 * Update the original altmode table as some ppms may report 449 * multiple DP altmodes. 450 */ 451 if (recipient == UCSI_RECIPIENT_CON) 452 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated); 453 454 /* now register altmodes */ 455 for (i = 0; i < max_altmodes; i++) { 456 memset(&desc, 0, sizeof(desc)); 457 if (multi_dp && recipient == UCSI_RECIPIENT_CON) { 458 desc.svid = updated[i].svid; 459 desc.vdo = updated[i].mid; 460 } else { 461 desc.svid = orig[i].svid; 462 desc.vdo = orig[i].mid; 463 } 464 desc.roles = TYPEC_PORT_DRD; 465 466 if (!desc.svid) 467 return 0; 468 469 ret = ucsi_register_altmode(con, &desc, recipient); 470 if (ret) 471 return ret; 472 } 473 474 return 0; 475 } 476 477 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient) 478 { 479 int max_altmodes = UCSI_MAX_ALTMODES; 480 struct typec_altmode_desc desc; 481 struct ucsi_altmode alt[2]; 482 u64 command; 483 int num; 484 int ret; 485 int len; 486 int j; 487 int i; 488 489 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS)) 490 return 0; 491 492 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0]) 493 return 0; 494 495 if (con->ucsi->ops->update_altmodes) 496 return ucsi_register_altmodes_nvidia(con, recipient); 497 498 if (recipient == UCSI_RECIPIENT_CON) 499 max_altmodes = con->ucsi->cap.num_alt_modes; 500 501 for (i = 0; i < max_altmodes;) { 502 memset(alt, 0, sizeof(alt)); 503 command = UCSI_GET_ALTERNATE_MODES; 504 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); 505 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); 506 command |= UCSI_GET_ALTMODE_OFFSET(i); 507 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt)); 508 if (len == -EBUSY) 509 continue; 510 if (len <= 0) 511 return len; 512 513 /* 514 * This code is requesting one alt mode at a time, but some PPMs 515 * may still return two. If that happens both alt modes need be 516 * registered and the offset for the next alt mode has to be 517 * incremented. 518 */ 519 num = len / sizeof(alt[0]); 520 i += num; 521 522 for (j = 0; j < num; j++) { 523 if (!alt[j].svid) 524 return 0; 525 526 memset(&desc, 0, sizeof(desc)); 527 desc.vdo = alt[j].mid; 528 desc.svid = alt[j].svid; 529 desc.roles = TYPEC_PORT_DRD; 530 531 ret = ucsi_register_altmode(con, &desc, recipient); 532 if (ret) 533 return ret; 534 } 535 } 536 537 return 0; 538 } 539 540 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient) 541 { 542 const struct typec_altmode *pdev; 543 struct typec_altmode **adev; 544 int i = 0; 545 546 switch (recipient) { 547 case UCSI_RECIPIENT_CON: 548 adev = con->port_altmode; 549 break; 550 case UCSI_RECIPIENT_SOP: 551 adev = con->partner_altmode; 552 break; 553 default: 554 return; 555 } 556 557 while (adev[i]) { 558 if (recipient == UCSI_RECIPIENT_SOP && 559 (adev[i]->svid == USB_TYPEC_DP_SID || 560 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID && 561 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) { 562 pdev = typec_altmode_get_partner(adev[i]); 563 ucsi_displayport_remove_partner((void *)pdev); 564 } 565 typec_unregister_altmode(adev[i]); 566 adev[i++] = NULL; 567 } 568 } 569 570 static int ucsi_read_pdos(struct ucsi_connector *con, 571 enum typec_role role, int is_partner, 572 u32 *pdos, int offset, int num_pdos) 573 { 574 struct ucsi *ucsi = con->ucsi; 575 u64 command; 576 int ret; 577 578 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num); 579 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner); 580 command |= UCSI_GET_PDOS_PDO_OFFSET(offset); 581 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1); 582 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0; 583 ret = ucsi_send_command(ucsi, command, pdos + offset, 584 num_pdos * sizeof(u32)); 585 if (ret < 0 && ret != -ETIMEDOUT) 586 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret); 587 588 return ret; 589 } 590 591 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role, 592 int is_partner, u32 *pdos) 593 { 594 u8 num_pdos; 595 int ret; 596 597 /* UCSI max payload means only getting at most 4 PDOs at a time */ 598 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS); 599 if (ret < 0) 600 return ret; 601 602 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */ 603 if (num_pdos < UCSI_MAX_PDOS) 604 return num_pdos; 605 606 /* get the remaining PDOs, if any */ 607 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS, 608 PDO_MAX_OBJECTS - UCSI_MAX_PDOS); 609 if (ret < 0) 610 return ret; 611 612 return ret / sizeof(u32) + num_pdos; 613 } 614 615 static int ucsi_get_src_pdos(struct ucsi_connector *con) 616 { 617 int ret; 618 619 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos); 620 if (ret < 0) 621 return ret; 622 623 con->num_pdos = ret; 624 625 ucsi_port_psy_changed(con); 626 627 return ret; 628 } 629 630 static int ucsi_check_altmodes(struct ucsi_connector *con) 631 { 632 int ret, num_partner_am; 633 634 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP); 635 if (ret && ret != -ETIMEDOUT) 636 dev_err(con->ucsi->dev, 637 "con%d: failed to register partner alt modes (%d)\n", 638 con->num, ret); 639 640 /* Ignoring the errors in this case. */ 641 if (con->partner_altmode[0]) { 642 num_partner_am = ucsi_get_num_altmode(con->partner_altmode); 643 if (num_partner_am > 0) 644 typec_partner_set_num_altmodes(con->partner, num_partner_am); 645 ucsi_altmode_update_active(con); 646 return 0; 647 } 648 649 return ret; 650 } 651 652 static int ucsi_register_partner_pdos(struct ucsi_connector *con) 653 { 654 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version }; 655 struct usb_power_delivery_capabilities_desc caps; 656 struct usb_power_delivery_capabilities *cap; 657 int ret; 658 659 if (con->partner_pd) 660 return 0; 661 662 con->partner_pd = usb_power_delivery_register(NULL, &desc); 663 if (IS_ERR(con->partner_pd)) 664 return PTR_ERR(con->partner_pd); 665 666 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, caps.pdo); 667 if (ret > 0) { 668 if (ret < PDO_MAX_OBJECTS) 669 caps.pdo[ret] = 0; 670 671 caps.role = TYPEC_SOURCE; 672 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps); 673 if (IS_ERR(cap)) 674 return PTR_ERR(cap); 675 676 con->partner_source_caps = cap; 677 678 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd); 679 if (ret) { 680 usb_power_delivery_unregister_capabilities(con->partner_source_caps); 681 return ret; 682 } 683 } 684 685 ret = ucsi_get_pdos(con, TYPEC_SINK, 1, caps.pdo); 686 if (ret > 0) { 687 if (ret < PDO_MAX_OBJECTS) 688 caps.pdo[ret] = 0; 689 690 caps.role = TYPEC_SINK; 691 692 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps); 693 if (IS_ERR(cap)) 694 return PTR_ERR(cap); 695 696 con->partner_sink_caps = cap; 697 698 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd); 699 if (ret) { 700 usb_power_delivery_unregister_capabilities(con->partner_sink_caps); 701 return ret; 702 } 703 } 704 705 return 0; 706 } 707 708 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con) 709 { 710 usb_power_delivery_unregister_capabilities(con->partner_sink_caps); 711 con->partner_sink_caps = NULL; 712 usb_power_delivery_unregister_capabilities(con->partner_source_caps); 713 con->partner_source_caps = NULL; 714 usb_power_delivery_unregister(con->partner_pd); 715 con->partner_pd = NULL; 716 } 717 718 static void ucsi_pwr_opmode_change(struct ucsi_connector *con) 719 { 720 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) { 721 case UCSI_CONSTAT_PWR_OPMODE_PD: 722 con->rdo = con->status.request_data_obj; 723 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD); 724 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0); 725 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0); 726 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ); 727 break; 728 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: 729 con->rdo = 0; 730 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A); 731 break; 732 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: 733 con->rdo = 0; 734 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A); 735 break; 736 default: 737 con->rdo = 0; 738 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB); 739 break; 740 } 741 } 742 743 static int ucsi_register_partner(struct ucsi_connector *con) 744 { 745 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags); 746 struct typec_partner_desc desc; 747 struct typec_partner *partner; 748 749 if (con->partner) 750 return 0; 751 752 memset(&desc, 0, sizeof(desc)); 753 754 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) { 755 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG: 756 desc.accessory = TYPEC_ACCESSORY_DEBUG; 757 break; 758 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO: 759 desc.accessory = TYPEC_ACCESSORY_AUDIO; 760 break; 761 default: 762 break; 763 } 764 765 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD; 766 767 partner = typec_register_partner(con->port, &desc); 768 if (IS_ERR(partner)) { 769 dev_err(con->ucsi->dev, 770 "con%d: failed to register partner (%ld)\n", con->num, 771 PTR_ERR(partner)); 772 return PTR_ERR(partner); 773 } 774 775 con->partner = partner; 776 777 return 0; 778 } 779 780 static void ucsi_unregister_partner(struct ucsi_connector *con) 781 { 782 if (!con->partner) 783 return; 784 785 ucsi_unregister_partner_pdos(con); 786 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP); 787 typec_unregister_partner(con->partner); 788 con->partner = NULL; 789 } 790 791 static void ucsi_partner_change(struct ucsi_connector *con) 792 { 793 enum usb_role u_role = USB_ROLE_NONE; 794 int ret; 795 796 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) { 797 case UCSI_CONSTAT_PARTNER_TYPE_UFP: 798 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: 799 u_role = USB_ROLE_HOST; 800 fallthrough; 801 case UCSI_CONSTAT_PARTNER_TYPE_CABLE: 802 typec_set_data_role(con->port, TYPEC_HOST); 803 break; 804 case UCSI_CONSTAT_PARTNER_TYPE_DFP: 805 u_role = USB_ROLE_DEVICE; 806 typec_set_data_role(con->port, TYPEC_DEVICE); 807 break; 808 default: 809 break; 810 } 811 812 /* Only notify USB controller if partner supports USB data */ 813 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB)) 814 u_role = USB_ROLE_NONE; 815 816 ret = usb_role_switch_set_role(con->usb_role_sw, u_role); 817 if (ret) 818 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n", 819 con->num, u_role); 820 } 821 822 static int ucsi_check_connection(struct ucsi_connector *con) 823 { 824 u8 prev_flags = con->status.flags; 825 u64 command; 826 int ret; 827 828 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); 829 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status)); 830 if (ret < 0) { 831 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret); 832 return ret; 833 } 834 835 if (con->status.flags == prev_flags) 836 return 0; 837 838 if (con->status.flags & UCSI_CONSTAT_CONNECTED) { 839 ucsi_register_partner(con); 840 ucsi_pwr_opmode_change(con); 841 ucsi_partner_change(con); 842 } else { 843 ucsi_partner_change(con); 844 ucsi_port_psy_changed(con); 845 ucsi_unregister_partner(con); 846 } 847 848 return 0; 849 } 850 851 static void ucsi_handle_connector_change(struct work_struct *work) 852 { 853 struct ucsi_connector *con = container_of(work, struct ucsi_connector, 854 work); 855 struct ucsi *ucsi = con->ucsi; 856 enum typec_role role; 857 u64 command; 858 int ret; 859 860 mutex_lock(&con->lock); 861 862 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); 863 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status)); 864 if (ret < 0) { 865 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n", 866 __func__, ret); 867 goto out_unlock; 868 } 869 870 trace_ucsi_connector_change(con->num, &con->status); 871 872 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR); 873 874 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) { 875 typec_set_pwr_role(con->port, role); 876 877 /* Complete pending power role swap */ 878 if (!completion_done(&con->complete)) 879 complete(&con->complete); 880 } 881 882 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) { 883 typec_set_pwr_role(con->port, role); 884 ucsi_port_psy_changed(con); 885 ucsi_partner_change(con); 886 887 if (con->status.flags & UCSI_CONSTAT_CONNECTED) { 888 ucsi_register_partner(con); 889 ucsi_partner_task(con, ucsi_check_connection, 1, HZ); 890 891 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) == 892 UCSI_CONSTAT_PWR_OPMODE_PD) 893 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ); 894 } else { 895 ucsi_unregister_partner(con); 896 } 897 } 898 899 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE || 900 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE) 901 ucsi_pwr_opmode_change(con); 902 903 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) { 904 ucsi_partner_change(con); 905 906 /* Complete pending data role swap */ 907 if (!completion_done(&con->complete)) 908 complete(&con->complete); 909 } 910 911 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE) 912 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0); 913 914 clear_bit(EVENT_PENDING, &con->ucsi->flags); 915 916 ret = ucsi_acknowledge_connector_change(ucsi); 917 if (ret) 918 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret); 919 920 out_unlock: 921 mutex_unlock(&con->lock); 922 } 923 924 /** 925 * ucsi_connector_change - Process Connector Change Event 926 * @ucsi: UCSI Interface 927 * @num: Connector number 928 */ 929 void ucsi_connector_change(struct ucsi *ucsi, u8 num) 930 { 931 struct ucsi_connector *con = &ucsi->connector[num - 1]; 932 933 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) { 934 dev_dbg(ucsi->dev, "Bogus connector change event\n"); 935 return; 936 } 937 938 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags)) 939 schedule_work(&con->work); 940 } 941 EXPORT_SYMBOL_GPL(ucsi_connector_change); 942 943 /* -------------------------------------------------------------------------- */ 944 945 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard) 946 { 947 u64 command; 948 949 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num); 950 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0; 951 952 return ucsi_send_command(con->ucsi, command, NULL, 0); 953 } 954 955 static int ucsi_reset_ppm(struct ucsi *ucsi) 956 { 957 u64 command = UCSI_PPM_RESET; 958 unsigned long tmo; 959 u32 cci; 960 int ret; 961 962 mutex_lock(&ucsi->ppm_lock); 963 964 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command, 965 sizeof(command)); 966 if (ret < 0) 967 goto out; 968 969 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); 970 971 do { 972 if (time_is_before_jiffies(tmo)) { 973 ret = -ETIMEDOUT; 974 goto out; 975 } 976 977 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); 978 if (ret) 979 goto out; 980 981 /* If the PPM is still doing something else, reset it again. */ 982 if (cci & ~UCSI_CCI_RESET_COMPLETE) { 983 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, 984 &command, 985 sizeof(command)); 986 if (ret < 0) 987 goto out; 988 } 989 990 msleep(20); 991 } while (!(cci & UCSI_CCI_RESET_COMPLETE)); 992 993 out: 994 mutex_unlock(&ucsi->ppm_lock); 995 return ret; 996 } 997 998 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command) 999 { 1000 int ret; 1001 1002 ret = ucsi_send_command(con->ucsi, command, NULL, 0); 1003 if (ret == -ETIMEDOUT) { 1004 u64 c; 1005 1006 /* PPM most likely stopped responding. Resetting everything. */ 1007 ucsi_reset_ppm(con->ucsi); 1008 1009 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy; 1010 ucsi_send_command(con->ucsi, c, NULL, 0); 1011 1012 ucsi_reset_connector(con, true); 1013 } 1014 1015 return ret; 1016 } 1017 1018 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role) 1019 { 1020 struct ucsi_connector *con = typec_get_drvdata(port); 1021 u8 partner_type; 1022 u64 command; 1023 int ret = 0; 1024 1025 mutex_lock(&con->lock); 1026 1027 if (!con->partner) { 1028 ret = -ENOTCONN; 1029 goto out_unlock; 1030 } 1031 1032 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags); 1033 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP && 1034 role == TYPEC_DEVICE) || 1035 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP && 1036 role == TYPEC_HOST)) 1037 goto out_unlock; 1038 1039 reinit_completion(&con->complete); 1040 1041 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num); 1042 command |= UCSI_SET_UOR_ROLE(role); 1043 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS; 1044 ret = ucsi_role_cmd(con, command); 1045 if (ret < 0) 1046 goto out_unlock; 1047 1048 mutex_unlock(&con->lock); 1049 1050 if (!wait_for_completion_timeout(&con->complete, 1051 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) 1052 return -ETIMEDOUT; 1053 1054 return 0; 1055 1056 out_unlock: 1057 mutex_unlock(&con->lock); 1058 1059 return ret; 1060 } 1061 1062 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) 1063 { 1064 struct ucsi_connector *con = typec_get_drvdata(port); 1065 enum typec_role cur_role; 1066 u64 command; 1067 int ret = 0; 1068 1069 mutex_lock(&con->lock); 1070 1071 if (!con->partner) { 1072 ret = -ENOTCONN; 1073 goto out_unlock; 1074 } 1075 1076 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR); 1077 1078 if (cur_role == role) 1079 goto out_unlock; 1080 1081 reinit_completion(&con->complete); 1082 1083 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num); 1084 command |= UCSI_SET_PDR_ROLE(role); 1085 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS; 1086 ret = ucsi_role_cmd(con, command); 1087 if (ret < 0) 1088 goto out_unlock; 1089 1090 mutex_unlock(&con->lock); 1091 1092 if (!wait_for_completion_timeout(&con->complete, 1093 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) 1094 return -ETIMEDOUT; 1095 1096 mutex_lock(&con->lock); 1097 1098 /* Something has gone wrong while swapping the role */ 1099 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) != 1100 UCSI_CONSTAT_PWR_OPMODE_PD) { 1101 ucsi_reset_connector(con, true); 1102 ret = -EPROTO; 1103 } 1104 1105 out_unlock: 1106 mutex_unlock(&con->lock); 1107 1108 return ret; 1109 } 1110 1111 static const struct typec_operations ucsi_ops = { 1112 .dr_set = ucsi_dr_swap, 1113 .pr_set = ucsi_pr_swap 1114 }; 1115 1116 /* Caller must call fwnode_handle_put() after use */ 1117 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con) 1118 { 1119 struct fwnode_handle *fwnode; 1120 int i = 1; 1121 1122 device_for_each_child_node(con->ucsi->dev, fwnode) 1123 if (i++ == con->num) 1124 return fwnode; 1125 return NULL; 1126 } 1127 1128 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) 1129 { 1130 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version}; 1131 struct usb_power_delivery_capabilities_desc pd_caps; 1132 struct usb_power_delivery_capabilities *pd_cap; 1133 struct typec_capability *cap = &con->typec_cap; 1134 enum typec_accessory *accessory = cap->accessory; 1135 enum usb_role u_role = USB_ROLE_NONE; 1136 u64 command; 1137 char *name; 1138 int ret; 1139 1140 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num); 1141 if (!name) 1142 return -ENOMEM; 1143 1144 con->wq = create_singlethread_workqueue(name); 1145 kfree(name); 1146 if (!con->wq) 1147 return -ENOMEM; 1148 1149 INIT_WORK(&con->work, ucsi_handle_connector_change); 1150 init_completion(&con->complete); 1151 mutex_init(&con->lock); 1152 INIT_LIST_HEAD(&con->partner_tasks); 1153 con->ucsi = ucsi; 1154 1155 cap->fwnode = ucsi_find_fwnode(con); 1156 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode); 1157 if (IS_ERR(con->usb_role_sw)) 1158 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw), 1159 "con%d: failed to get usb role switch\n", con->num); 1160 1161 /* Delay other interactions with the con until registration is complete */ 1162 mutex_lock(&con->lock); 1163 1164 /* Get connector capability */ 1165 command = UCSI_GET_CONNECTOR_CAPABILITY; 1166 command |= UCSI_CONNECTOR_NUMBER(con->num); 1167 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap)); 1168 if (ret < 0) 1169 goto out_unlock; 1170 1171 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP) 1172 cap->data = TYPEC_PORT_DRD; 1173 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP) 1174 cap->data = TYPEC_PORT_DFP; 1175 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP) 1176 cap->data = TYPEC_PORT_UFP; 1177 1178 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) && 1179 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)) 1180 cap->type = TYPEC_PORT_DRP; 1181 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) 1182 cap->type = TYPEC_PORT_SRC; 1183 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER) 1184 cap->type = TYPEC_PORT_SNK; 1185 1186 cap->revision = ucsi->cap.typec_version; 1187 cap->pd_revision = ucsi->cap.pd_version; 1188 cap->svdm_version = SVDM_VER_2_0; 1189 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE; 1190 1191 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY) 1192 *accessory++ = TYPEC_ACCESSORY_AUDIO; 1193 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY) 1194 *accessory = TYPEC_ACCESSORY_DEBUG; 1195 1196 cap->driver_data = con; 1197 cap->ops = &ucsi_ops; 1198 1199 ret = ucsi_register_port_psy(con); 1200 if (ret) 1201 goto out; 1202 1203 /* Register the connector */ 1204 con->port = typec_register_port(ucsi->dev, cap); 1205 if (IS_ERR(con->port)) { 1206 ret = PTR_ERR(con->port); 1207 goto out; 1208 } 1209 1210 con->pd = usb_power_delivery_register(ucsi->dev, &desc); 1211 1212 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 0, pd_caps.pdo); 1213 if (ret > 0) { 1214 if (ret < PDO_MAX_OBJECTS) 1215 pd_caps.pdo[ret] = 0; 1216 1217 pd_caps.role = TYPEC_SOURCE; 1218 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps); 1219 if (IS_ERR(pd_cap)) { 1220 ret = PTR_ERR(pd_cap); 1221 goto out; 1222 } 1223 1224 con->port_source_caps = pd_cap; 1225 typec_port_set_usb_power_delivery(con->port, con->pd); 1226 } 1227 1228 memset(&pd_caps, 0, sizeof(pd_caps)); 1229 ret = ucsi_get_pdos(con, TYPEC_SINK, 0, pd_caps.pdo); 1230 if (ret > 0) { 1231 if (ret < PDO_MAX_OBJECTS) 1232 pd_caps.pdo[ret] = 0; 1233 1234 pd_caps.role = TYPEC_SINK; 1235 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps); 1236 if (IS_ERR(pd_cap)) { 1237 ret = PTR_ERR(pd_cap); 1238 goto out; 1239 } 1240 1241 con->port_sink_caps = pd_cap; 1242 typec_port_set_usb_power_delivery(con->port, con->pd); 1243 } 1244 1245 /* Alternate modes */ 1246 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON); 1247 if (ret) { 1248 dev_err(ucsi->dev, "con%d: failed to register alt modes\n", 1249 con->num); 1250 goto out; 1251 } 1252 1253 /* Get the status */ 1254 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); 1255 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status)); 1256 if (ret < 0) { 1257 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num); 1258 ret = 0; 1259 goto out; 1260 } 1261 ret = 0; /* ucsi_send_command() returns length on success */ 1262 1263 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) { 1264 case UCSI_CONSTAT_PARTNER_TYPE_UFP: 1265 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: 1266 u_role = USB_ROLE_HOST; 1267 fallthrough; 1268 case UCSI_CONSTAT_PARTNER_TYPE_CABLE: 1269 typec_set_data_role(con->port, TYPEC_HOST); 1270 break; 1271 case UCSI_CONSTAT_PARTNER_TYPE_DFP: 1272 u_role = USB_ROLE_DEVICE; 1273 typec_set_data_role(con->port, TYPEC_DEVICE); 1274 break; 1275 default: 1276 break; 1277 } 1278 1279 /* Check if there is already something connected */ 1280 if (con->status.flags & UCSI_CONSTAT_CONNECTED) { 1281 typec_set_pwr_role(con->port, 1282 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR)); 1283 ucsi_register_partner(con); 1284 ucsi_pwr_opmode_change(con); 1285 ucsi_port_psy_changed(con); 1286 } 1287 1288 /* Only notify USB controller if partner supports USB data */ 1289 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB)) 1290 u_role = USB_ROLE_NONE; 1291 1292 ret = usb_role_switch_set_role(con->usb_role_sw, u_role); 1293 if (ret) { 1294 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n", 1295 con->num, u_role); 1296 ret = 0; 1297 } 1298 1299 if (con->partner && 1300 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) == 1301 UCSI_CONSTAT_PWR_OPMODE_PD) { 1302 ucsi_get_src_pdos(con); 1303 ucsi_check_altmodes(con); 1304 } 1305 1306 trace_ucsi_register_port(con->num, &con->status); 1307 1308 out: 1309 fwnode_handle_put(cap->fwnode); 1310 out_unlock: 1311 mutex_unlock(&con->lock); 1312 1313 if (ret && con->wq) { 1314 destroy_workqueue(con->wq); 1315 con->wq = NULL; 1316 } 1317 1318 return ret; 1319 } 1320 1321 /** 1322 * ucsi_init - Initialize UCSI interface 1323 * @ucsi: UCSI to be initialized 1324 * 1325 * Registers all ports @ucsi has and enables all notification events. 1326 */ 1327 static int ucsi_init(struct ucsi *ucsi) 1328 { 1329 struct ucsi_connector *con, *connector; 1330 u64 command, ntfy; 1331 int ret; 1332 int i; 1333 1334 /* Reset the PPM */ 1335 ret = ucsi_reset_ppm(ucsi); 1336 if (ret) { 1337 dev_err(ucsi->dev, "failed to reset PPM!\n"); 1338 goto err; 1339 } 1340 1341 /* Enable basic notifications */ 1342 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR; 1343 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy; 1344 ret = ucsi_send_command(ucsi, command, NULL, 0); 1345 if (ret < 0) 1346 goto err_reset; 1347 1348 /* Get PPM capabilities */ 1349 command = UCSI_GET_CAPABILITY; 1350 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap)); 1351 if (ret < 0) 1352 goto err_reset; 1353 1354 if (!ucsi->cap.num_connectors) { 1355 ret = -ENODEV; 1356 goto err_reset; 1357 } 1358 1359 /* Allocate the connectors. Released in ucsi_unregister() */ 1360 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL); 1361 if (!connector) { 1362 ret = -ENOMEM; 1363 goto err_reset; 1364 } 1365 1366 /* Register all connectors */ 1367 for (i = 0; i < ucsi->cap.num_connectors; i++) { 1368 connector[i].num = i + 1; 1369 ret = ucsi_register_port(ucsi, &connector[i]); 1370 if (ret) 1371 goto err_unregister; 1372 } 1373 1374 /* Enable all notifications */ 1375 ntfy = UCSI_ENABLE_NTFY_ALL; 1376 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy; 1377 ret = ucsi_send_command(ucsi, command, NULL, 0); 1378 if (ret < 0) 1379 goto err_unregister; 1380 1381 ucsi->connector = connector; 1382 ucsi->ntfy = ntfy; 1383 return 0; 1384 1385 err_unregister: 1386 for (con = connector; con->port; con++) { 1387 ucsi_unregister_partner(con); 1388 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON); 1389 ucsi_unregister_port_psy(con); 1390 if (con->wq) 1391 destroy_workqueue(con->wq); 1392 1393 usb_power_delivery_unregister_capabilities(con->port_sink_caps); 1394 con->port_sink_caps = NULL; 1395 usb_power_delivery_unregister_capabilities(con->port_source_caps); 1396 con->port_source_caps = NULL; 1397 usb_power_delivery_unregister(con->pd); 1398 con->pd = NULL; 1399 typec_unregister_port(con->port); 1400 con->port = NULL; 1401 } 1402 kfree(connector); 1403 err_reset: 1404 memset(&ucsi->cap, 0, sizeof(ucsi->cap)); 1405 ucsi_reset_ppm(ucsi); 1406 err: 1407 return ret; 1408 } 1409 1410 static void ucsi_resume_work(struct work_struct *work) 1411 { 1412 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work); 1413 struct ucsi_connector *con; 1414 u64 command; 1415 int ret; 1416 1417 /* Restore UCSI notification enable mask after system resume */ 1418 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; 1419 ret = ucsi_send_command(ucsi, command, NULL, 0); 1420 if (ret < 0) { 1421 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret); 1422 return; 1423 } 1424 1425 for (con = ucsi->connector; con->port; con++) { 1426 mutex_lock(&con->lock); 1427 ucsi_partner_task(con, ucsi_check_connection, 1, 0); 1428 mutex_unlock(&con->lock); 1429 } 1430 } 1431 1432 int ucsi_resume(struct ucsi *ucsi) 1433 { 1434 if (ucsi->connector) 1435 queue_work(system_long_wq, &ucsi->resume_work); 1436 return 0; 1437 } 1438 EXPORT_SYMBOL_GPL(ucsi_resume); 1439 1440 static void ucsi_init_work(struct work_struct *work) 1441 { 1442 struct ucsi *ucsi = container_of(work, struct ucsi, work.work); 1443 int ret; 1444 1445 ret = ucsi_init(ucsi); 1446 if (ret) 1447 dev_err_probe(ucsi->dev, ret, "PPM init failed\n"); 1448 1449 if (ret == -EPROBE_DEFER) { 1450 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) { 1451 dev_err(ucsi->dev, "PPM init failed, stop trying\n"); 1452 return; 1453 } 1454 1455 queue_delayed_work(system_long_wq, &ucsi->work, 1456 UCSI_ROLE_SWITCH_INTERVAL); 1457 } 1458 } 1459 1460 /** 1461 * ucsi_get_drvdata - Return private driver data pointer 1462 * @ucsi: UCSI interface 1463 */ 1464 void *ucsi_get_drvdata(struct ucsi *ucsi) 1465 { 1466 return ucsi->driver_data; 1467 } 1468 EXPORT_SYMBOL_GPL(ucsi_get_drvdata); 1469 1470 /** 1471 * ucsi_set_drvdata - Assign private driver data pointer 1472 * @ucsi: UCSI interface 1473 * @data: Private data pointer 1474 */ 1475 void ucsi_set_drvdata(struct ucsi *ucsi, void *data) 1476 { 1477 ucsi->driver_data = data; 1478 } 1479 EXPORT_SYMBOL_GPL(ucsi_set_drvdata); 1480 1481 /** 1482 * ucsi_create - Allocate UCSI instance 1483 * @dev: Device interface to the PPM (Platform Policy Manager) 1484 * @ops: I/O routines 1485 */ 1486 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) 1487 { 1488 struct ucsi *ucsi; 1489 1490 if (!ops || !ops->read || !ops->sync_write || !ops->async_write) 1491 return ERR_PTR(-EINVAL); 1492 1493 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); 1494 if (!ucsi) 1495 return ERR_PTR(-ENOMEM); 1496 1497 INIT_WORK(&ucsi->resume_work, ucsi_resume_work); 1498 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work); 1499 mutex_init(&ucsi->ppm_lock); 1500 ucsi->dev = dev; 1501 ucsi->ops = ops; 1502 1503 return ucsi; 1504 } 1505 EXPORT_SYMBOL_GPL(ucsi_create); 1506 1507 /** 1508 * ucsi_destroy - Free UCSI instance 1509 * @ucsi: UCSI instance to be freed 1510 */ 1511 void ucsi_destroy(struct ucsi *ucsi) 1512 { 1513 kfree(ucsi); 1514 } 1515 EXPORT_SYMBOL_GPL(ucsi_destroy); 1516 1517 /** 1518 * ucsi_register - Register UCSI interface 1519 * @ucsi: UCSI instance 1520 */ 1521 int ucsi_register(struct ucsi *ucsi) 1522 { 1523 int ret; 1524 1525 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version, 1526 sizeof(ucsi->version)); 1527 if (ret) 1528 return ret; 1529 1530 if (!ucsi->version) 1531 return -ENODEV; 1532 1533 queue_delayed_work(system_long_wq, &ucsi->work, 0); 1534 1535 return 0; 1536 } 1537 EXPORT_SYMBOL_GPL(ucsi_register); 1538 1539 /** 1540 * ucsi_unregister - Unregister UCSI interface 1541 * @ucsi: UCSI interface to be unregistered 1542 * 1543 * Unregister UCSI interface that was created with ucsi_register(). 1544 */ 1545 void ucsi_unregister(struct ucsi *ucsi) 1546 { 1547 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE; 1548 int i; 1549 1550 /* Make sure that we are not in the middle of driver initialization */ 1551 cancel_delayed_work_sync(&ucsi->work); 1552 cancel_work_sync(&ucsi->resume_work); 1553 1554 /* Disable notifications */ 1555 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); 1556 1557 if (!ucsi->connector) 1558 return; 1559 1560 for (i = 0; i < ucsi->cap.num_connectors; i++) { 1561 cancel_work_sync(&ucsi->connector[i].work); 1562 ucsi_unregister_partner(&ucsi->connector[i]); 1563 ucsi_unregister_altmodes(&ucsi->connector[i], 1564 UCSI_RECIPIENT_CON); 1565 ucsi_unregister_port_psy(&ucsi->connector[i]); 1566 1567 if (ucsi->connector[i].wq) { 1568 struct ucsi_work *uwork; 1569 1570 mutex_lock(&ucsi->connector[i].lock); 1571 /* 1572 * queue delayed items immediately so they can execute 1573 * and free themselves before the wq is destroyed 1574 */ 1575 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node) 1576 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0); 1577 mutex_unlock(&ucsi->connector[i].lock); 1578 destroy_workqueue(ucsi->connector[i].wq); 1579 } 1580 1581 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps); 1582 ucsi->connector[i].port_sink_caps = NULL; 1583 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps); 1584 ucsi->connector[i].port_source_caps = NULL; 1585 usb_power_delivery_unregister(ucsi->connector[i].pd); 1586 ucsi->connector[i].pd = NULL; 1587 typec_unregister_port(ucsi->connector[i].port); 1588 } 1589 1590 kfree(ucsi->connector); 1591 } 1592 EXPORT_SYMBOL_GPL(ucsi_unregister); 1593 1594 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); 1595 MODULE_LICENSE("GPL v2"); 1596 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver"); 1597