| 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 <[email protected]> |
| 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 |
#include <linux/usb/typec_tbt.h> |
| 17 |
|
| 18 |
#include "ucsi.h" |
| 19 |
#include "trace.h" |
| 20 |
|
| 21 |
/* |
| 22 |
* UCSI_TIMEOUT_MS - PPM communication timeout |
| 23 |
* |
| 24 |
* Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI |
| 25 |
* specification) here as reference, but unfortunately we can't. It is very |
| 26 |
* difficult to estimate the time it takes for the system to process the command |
| 27 |
* before it is actually passed to the PPM. |
| 28 |
*/ |
| 29 |
#define UCSI_TIMEOUT_MS 10000 |
| 30 |
|
| 31 |
/* |
| 32 |
* UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests |
| 33 |
* |
| 34 |
* 5 seconds is close to the time it takes for CapsCounter to reach 0, so even |
| 35 |
* if the PPM does not generate Connector Change events before that with |
| 36 |
* partners that do not support USB Power Delivery, this should still work. |
| 37 |
*/ |
| 38 |
#define UCSI_SWAP_TIMEOUT_MS 5000 |
| 39 |
|
| 40 |
void ucsi_notify_common(struct ucsi *ucsi, u32 cci) |
| 41 |
{ |
| 42 |
/* Ignore bogus data in CCI if busy indicator is set. */ |
| 43 |
if (cci & UCSI_CCI_BUSY) |
| 44 |
return; |
| 45 |
|
| 46 |
if (UCSI_CCI_CONNECTOR(cci)) { |
| 47 |
if (!ucsi->cap.num_connectors || |
| 48 |
UCSI_CCI_CONNECTOR(cci) <= ucsi->cap.num_connectors) |
| 49 |
ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci)); |
| 50 |
else |
| 51 |
dev_err(ucsi->dev, "bogus connector number in CCI: %lu\n", |
| 52 |
UCSI_CCI_CONNECTOR(cci)); |
| 53 |
} |
| 54 |
|
| 55 |
if (cci & UCSI_CCI_ACK_COMPLETE && |
| 56 |
test_and_clear_bit(ACK_PENDING, &ucsi->flags)) |
| 57 |
complete(&ucsi->complete); |
| 58 |
|
| 59 |
if (cci & UCSI_CCI_COMMAND_COMPLETE && |
| 60 |
test_and_clear_bit(COMMAND_PENDING, &ucsi->flags)) |
| 61 |
complete(&ucsi->complete); |
| 62 |
} |
| 63 |
EXPORT_SYMBOL_GPL(ucsi_notify_common); |
| 64 |
|
| 65 |
int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci, |
| 66 |
void *data, size_t size) |
| 67 |
{ |
| 68 |
bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI; |
| 69 |
int ret; |
| 70 |
|
| 71 |
if (ack) |
| 72 |
set_bit(ACK_PENDING, &ucsi->flags); |
| 73 |
else |
| 74 |
set_bit(COMMAND_PENDING, &ucsi->flags); |
| 75 |
|
| 76 |
reinit_completion(&ucsi->complete); |
| 77 |
|
| 78 |
ret = ucsi->ops->async_control(ucsi, command); |
| 79 |
if (ret) |
| 80 |
goto out_clear_bit; |
| 81 |
|
| 82 |
if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ)) |
| 83 |
ret = -ETIMEDOUT; |
| 84 |
|
| 85 |
out_clear_bit: |
| 86 |
if (ack) |
| 87 |
clear_bit(ACK_PENDING, &ucsi->flags); |
| 88 |
else |
| 89 |
clear_bit(COMMAND_PENDING, &ucsi->flags); |
| 90 |
|
| 91 |
if (!ret && cci) |
| 92 |
ret = ucsi->ops->read_cci(ucsi, cci); |
| 93 |
|
| 94 |
if (!ret && data && |
| 95 |
(*cci & UCSI_CCI_COMMAND_COMPLETE)) |
| 96 |
ret = ucsi->ops->read_message_in(ucsi, data, size); |
| 97 |
|
| 98 |
return ret; |
| 99 |
} |
| 100 |
EXPORT_SYMBOL_GPL(ucsi_sync_control_common); |
| 101 |
|
| 102 |
static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) |
| 103 |
{ |
| 104 |
u64 ctrl; |
| 105 |
|
| 106 |
ctrl = UCSI_ACK_CC_CI; |
| 107 |
ctrl |= UCSI_ACK_COMMAND_COMPLETE; |
| 108 |
if (conn_ack) { |
| 109 |
clear_bit(EVENT_PENDING, &ucsi->flags); |
| 110 |
ctrl |= UCSI_ACK_CONNECTOR_CHANGE; |
| 111 |
} |
| 112 |
|
| 113 |
return ucsi->ops->sync_control(ucsi, ctrl, NULL, NULL, 0); |
| 114 |
} |
| 115 |
|
| 116 |
static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, |
| 117 |
void *data, size_t size, bool conn_ack) |
| 118 |
{ |
| 119 |
int ret, err; |
| 120 |
|
| 121 |
*cci = 0; |
| 122 |
|
| 123 |
if (size > UCSI_MAX_DATA_LENGTH(ucsi)) |
| 124 |
return -EINVAL; |
| 125 |
|
| 126 |
ret = ucsi->ops->sync_control(ucsi, command, cci, data, size); |
| 127 |
|
| 128 |
if (*cci & UCSI_CCI_BUSY) |
| 129 |
return ucsi_run_command(ucsi, UCSI_CANCEL, cci, NULL, 0, false) ?: -EBUSY; |
| 130 |
if (ret) |
| 131 |
return ret; |
| 132 |
|
| 133 |
if (!(*cci & UCSI_CCI_COMMAND_COMPLETE)) |
| 134 |
return -EIO; |
| 135 |
|
| 136 |
if (*cci & UCSI_CCI_NOT_SUPPORTED) |
| 137 |
err = -EOPNOTSUPP; |
| 138 |
else if (*cci & UCSI_CCI_ERROR) |
| 139 |
err = -EIO; |
| 140 |
else |
| 141 |
err = 0; |
| 142 |
|
| 143 |
/* |
| 144 |
* Don't ACK connection change if there was an error. |
| 145 |
*/ |
| 146 |
ret = ucsi_acknowledge(ucsi, err ? false : conn_ack); |
| 147 |
if (ret) |
| 148 |
return ret; |
| 149 |
|
| 150 |
return err ?: UCSI_CCI_LENGTH(*cci); |
| 151 |
} |
| 152 |
|
| 153 |
static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num) |
| 154 |
{ |
| 155 |
u64 command; |
| 156 |
u16 error; |
| 157 |
u32 cci; |
| 158 |
int ret; |
| 159 |
|
| 160 |
command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num); |
| 161 |
ret = ucsi_run_command(ucsi, command, &cci, &error, sizeof(error), false); |
| 162 |
if (ret < 0) |
| 163 |
return ret; |
| 164 |
|
| 165 |
switch (error) { |
| 166 |
case UCSI_ERROR_INCOMPATIBLE_PARTNER: |
| 167 |
return -EOPNOTSUPP; |
| 168 |
case UCSI_ERROR_CC_COMMUNICATION_ERR: |
| 169 |
return -ECOMM; |
| 170 |
case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL: |
| 171 |
return -EPROTO; |
| 172 |
case UCSI_ERROR_DEAD_BATTERY: |
| 173 |
dev_warn(ucsi->dev, "Dead battery condition!\n"); |
| 174 |
return -EPERM; |
| 175 |
case UCSI_ERROR_INVALID_CON_NUM: |
| 176 |
case UCSI_ERROR_UNREGONIZED_CMD: |
| 177 |
case UCSI_ERROR_INVALID_CMD_ARGUMENT: |
| 178 |
dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error); |
| 179 |
return -EINVAL; |
| 180 |
case UCSI_ERROR_OVERCURRENT: |
| 181 |
dev_warn(ucsi->dev, "Overcurrent condition\n"); |
| 182 |
break; |
| 183 |
case UCSI_ERROR_PARTNER_REJECTED_SWAP: |
| 184 |
dev_warn(ucsi->dev, "Partner rejected swap\n"); |
| 185 |
break; |
| 186 |
case UCSI_ERROR_HARD_RESET: |
| 187 |
dev_warn(ucsi->dev, "Hard reset occurred\n"); |
| 188 |
break; |
| 189 |
case UCSI_ERROR_PPM_POLICY_CONFLICT: |
| 190 |
dev_warn(ucsi->dev, "PPM Policy conflict\n"); |
| 191 |
break; |
| 192 |
case UCSI_ERROR_SWAP_REJECTED: |
| 193 |
dev_warn(ucsi->dev, "Swap rejected\n"); |
| 194 |
break; |
| 195 |
case UCSI_ERROR_REVERSE_CURRENT_PROTECTION: |
| 196 |
dev_warn(ucsi->dev, "Reverse Current Protection detected\n"); |
| 197 |
break; |
| 198 |
case UCSI_ERROR_SET_SINK_PATH_REJECTED: |
| 199 |
dev_warn(ucsi->dev, "Set Sink Path rejected\n"); |
| 200 |
break; |
| 201 |
case UCSI_ERROR_UNDEFINED: |
| 202 |
default: |
| 203 |
dev_err(ucsi->dev, "unknown error %u\n", error); |
| 204 |
break; |
| 205 |
} |
| 206 |
|
| 207 |
return -EIO; |
| 208 |
} |
| 209 |
|
| 210 |
static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd, |
| 211 |
void *data, size_t size, bool conn_ack) |
| 212 |
{ |
| 213 |
u8 connector_num; |
| 214 |
u32 cci; |
| 215 |
int ret; |
| 216 |
|
| 217 |
if (ucsi->version > UCSI_VERSION_1_2) { |
| 218 |
switch (UCSI_COMMAND(cmd)) { |
| 219 |
case UCSI_GET_ALTERNATE_MODES: |
| 220 |
connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd); |
| 221 |
break; |
| 222 |
case UCSI_PPM_RESET: |
| 223 |
case UCSI_CANCEL: |
| 224 |
case UCSI_ACK_CC_CI: |
| 225 |
case UCSI_SET_NOTIFICATION_ENABLE: |
| 226 |
case UCSI_GET_CAPABILITY: |
| 227 |
connector_num = 0; |
| 228 |
break; |
| 229 |
default: |
| 230 |
connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd); |
| 231 |
break; |
| 232 |
} |
| 233 |
} else { |
| 234 |
connector_num = 0; |
| 235 |
} |
| 236 |
|
| 237 |
mutex_lock(&ucsi->ppm_lock); |
| 238 |
|
| 239 |
ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack); |
| 240 |
|
| 241 |
if (cci & UCSI_CCI_ERROR) |
| 242 |
ret = ucsi_read_error(ucsi, connector_num); |
| 243 |
|
| 244 |
mutex_unlock(&ucsi->ppm_lock); |
| 245 |
return ret; |
| 246 |
} |
| 247 |
|
| 248 |
int ucsi_send_command(struct ucsi *ucsi, u64 command, |
| 249 |
void *data, size_t size) |
| 250 |
{ |
| 251 |
return ucsi_send_command_common(ucsi, command, data, size, false); |
| 252 |
} |
| 253 |
EXPORT_SYMBOL_GPL(ucsi_send_command); |
| 254 |
|
| 255 |
/* -------------------------------------------------------------------------- */ |
| 256 |
|
| 257 |
struct ucsi_work { |
| 258 |
struct delayed_work work; |
| 259 |
struct list_head node; |
| 260 |
unsigned long delay; |
| 261 |
unsigned int count; |
| 262 |
struct ucsi_connector *con; |
| 263 |
int (*cb)(struct ucsi_connector *); |
| 264 |
}; |
| 265 |
|
| 266 |
static void ucsi_poll_worker(struct work_struct *work) |
| 267 |
{ |
| 268 |
struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work); |
| 269 |
struct ucsi_connector *con = uwork->con; |
| 270 |
int ret; |
| 271 |
|
| 272 |
mutex_lock(&con->lock); |
| 273 |
|
| 274 |
if (!con->partner) { |
| 275 |
list_del(&uwork->node); |
| 276 |
mutex_unlock(&con->lock); |
| 277 |
kfree(uwork); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
ret = uwork->cb(con); |
| 282 |
|
| 283 |
if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) { |
| 284 |
queue_delayed_work(con->wq, &uwork->work, uwork->delay); |
| 285 |
} else { |
| 286 |
list_del(&uwork->node); |
| 287 |
kfree(uwork); |
| 288 |
} |
| 289 |
|
| 290 |
mutex_unlock(&con->lock); |
| 291 |
} |
| 292 |
|
| 293 |
static int ucsi_partner_task(struct ucsi_connector *con, |
| 294 |
int (*cb)(struct ucsi_connector *), |
| 295 |
int retries, unsigned long delay) |
| 296 |
{ |
| 297 |
struct ucsi_work *uwork; |
| 298 |
|
| 299 |
if (!con->partner) |
| 300 |
return 0; |
| 301 |
|
| 302 |
uwork = kzalloc_obj(*uwork); |
| 303 |
if (!uwork) |
| 304 |
return -ENOMEM; |
| 305 |
|
| 306 |
INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker); |
| 307 |
uwork->count = retries; |
| 308 |
uwork->delay = delay; |
| 309 |
uwork->con = con; |
| 310 |
uwork->cb = cb; |
| 311 |
|
| 312 |
list_add_tail(&uwork->node, &con->partner_tasks); |
| 313 |
queue_delayed_work(con->wq, &uwork->work, delay); |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
/* -------------------------------------------------------------------------- */ |
| 319 |
|
| 320 |
void ucsi_altmode_update_active(struct ucsi_connector *con) |
| 321 |
{ |
| 322 |
const struct typec_altmode *altmode = NULL; |
| 323 |
u64 command; |
| 324 |
u16 svid = 0; |
| 325 |
int ret; |
| 326 |
u8 cur; |
| 327 |
int i; |
| 328 |
|
| 329 |
command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num); |
| 330 |
ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur)); |
| 331 |
if (ret < 0) { |
| 332 |
if (con->ucsi->version > 0x0100) { |
| 333 |
dev_err(con->ucsi->dev, |
| 334 |
"GET_CURRENT_CAM command failed\n"); |
| 335 |
return; |
| 336 |
} |
| 337 |
cur = 0xff; |
| 338 |
} |
| 339 |
|
| 340 |
if (cur < UCSI_MAX_ALTMODES) |
| 341 |
altmode = typec_altmode_get_partner(con->port_altmode[cur]); |
| 342 |
|
| 343 |
for (i = 0; con->partner_altmode[i]; i++) |
| 344 |
typec_altmode_update_active(con->partner_altmode[i], |
| 345 |
con->partner_altmode[i] == altmode); |
| 346 |
|
| 347 |
if (altmode) |
| 348 |
svid = altmode->svid; |
| 349 |
typec_altmode_state_update(con->partner, svid, 0); |
| 350 |
} |
| 351 |
|
| 352 |
static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid) |
| 353 |
{ |
| 354 |
u8 mode = 1; |
| 355 |
int i; |
| 356 |
|
| 357 |
for (i = 0; alt[i]; i++) { |
| 358 |
if (i > MODE_DISCOVERY_MAX) |
| 359 |
return -ERANGE; |
| 360 |
|
| 361 |
if (alt[i]->svid == svid) |
| 362 |
mode++; |
| 363 |
} |
| 364 |
|
| 365 |
return mode; |
| 366 |
} |
| 367 |
|
| 368 |
static int ucsi_next_altmode(struct typec_altmode **alt) |
| 369 |
{ |
| 370 |
int i = 0; |
| 371 |
|
| 372 |
for (i = 0; i < UCSI_MAX_ALTMODES; i++) |
| 373 |
if (!alt[i]) |
| 374 |
return i; |
| 375 |
|
| 376 |
return -ENOENT; |
| 377 |
} |
| 378 |
|
| 379 |
static int ucsi_get_num_altmode(struct typec_altmode **alt) |
| 380 |
{ |
| 381 |
int i; |
| 382 |
|
| 383 |
for (i = 0; i < UCSI_MAX_ALTMODES; i++) |
| 384 |
if (!alt[i]) |
| 385 |
break; |
| 386 |
|
| 387 |
return i; |
| 388 |
} |
| 389 |
|
| 390 |
static int ucsi_register_altmode(struct ucsi_connector *con, |
| 391 |
struct typec_altmode_desc *desc, |
| 392 |
u8 recipient) |
| 393 |
{ |
| 394 |
struct typec_altmode *alt; |
| 395 |
bool override; |
| 396 |
int ret; |
| 397 |
int i; |
| 398 |
|
| 399 |
override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE); |
| 400 |
|
| 401 |
switch (recipient) { |
| 402 |
case UCSI_RECIPIENT_CON: |
| 403 |
i = ucsi_next_altmode(con->port_altmode); |
| 404 |
if (i < 0) { |
| 405 |
ret = i; |
| 406 |
goto err; |
| 407 |
} |
| 408 |
|
| 409 |
ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid); |
| 410 |
if (ret < 0) |
| 411 |
return ret; |
| 412 |
|
| 413 |
desc->mode = ret; |
| 414 |
|
| 415 |
switch (desc->svid) { |
| 416 |
case USB_TYPEC_DP_SID: |
| 417 |
alt = ucsi_register_displayport(con, override, i, desc); |
| 418 |
break; |
| 419 |
case USB_TYPEC_NVIDIA_VLINK_SID: |
| 420 |
if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO) |
| 421 |
alt = typec_port_register_altmode(con->port, |
| 422 |
desc); |
| 423 |
else |
| 424 |
alt = ucsi_register_displayport(con, override, |
| 425 |
i, desc); |
| 426 |
break; |
| 427 |
case USB_TYPEC_TBT_SID: |
| 428 |
alt = ucsi_register_thunderbolt(con, override, i, desc); |
| 429 |
break; |
| 430 |
default: |
| 431 |
alt = typec_port_register_altmode(con->port, desc); |
| 432 |
break; |
| 433 |
} |
| 434 |
|
| 435 |
if (IS_ERR(alt)) { |
| 436 |
ret = PTR_ERR(alt); |
| 437 |
goto err; |
| 438 |
} |
| 439 |
|
| 440 |
con->port_altmode[i] = alt; |
| 441 |
break; |
| 442 |
case UCSI_RECIPIENT_SOP: |
| 443 |
i = ucsi_next_altmode(con->partner_altmode); |
| 444 |
if (i < 0) { |
| 445 |
ret = i; |
| 446 |
goto err; |
| 447 |
} |
| 448 |
|
| 449 |
ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid); |
| 450 |
if (ret < 0) |
| 451 |
return ret; |
| 452 |
|
| 453 |
desc->mode = ret; |
| 454 |
|
| 455 |
alt = typec_partner_register_altmode(con->partner, desc); |
| 456 |
if (IS_ERR(alt)) { |
| 457 |
ret = PTR_ERR(alt); |
| 458 |
goto err; |
| 459 |
} |
| 460 |
|
| 461 |
con->partner_altmode[i] = alt; |
| 462 |
break; |
| 463 |
case UCSI_RECIPIENT_SOP_P: |
| 464 |
i = ucsi_next_altmode(con->plug_altmode); |
| 465 |
if (i < 0) { |
| 466 |
ret = i; |
| 467 |
goto err; |
| 468 |
} |
| 469 |
|
| 470 |
ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid); |
| 471 |
if (ret < 0) |
| 472 |
return ret; |
| 473 |
|
| 474 |
desc->mode = ret; |
| 475 |
|
| 476 |
alt = typec_plug_register_altmode(con->plug, desc); |
| 477 |
if (IS_ERR(alt)) { |
| 478 |
ret = PTR_ERR(alt); |
| 479 |
goto err; |
| 480 |
} |
| 481 |
|
| 482 |
con->plug_altmode[i] = alt; |
| 483 |
break; |
| 484 |
default: |
| 485 |
return -EINVAL; |
| 486 |
} |
| 487 |
|
| 488 |
trace_ucsi_register_altmode(recipient, alt); |
| 489 |
|
| 490 |
return 0; |
| 491 |
|
| 492 |
err: |
| 493 |
dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n", |
| 494 |
desc->svid, desc->mode); |
| 495 |
|
| 496 |
return ret; |
| 497 |
} |
| 498 |
|
| 499 |
static int |
| 500 |
ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient) |
| 501 |
{ |
| 502 |
int max_altmodes = UCSI_MAX_ALTMODES; |
| 503 |
struct typec_altmode_desc desc; |
| 504 |
struct ucsi_altmode alt; |
| 505 |
struct ucsi_altmode orig[UCSI_MAX_ALTMODES]; |
| 506 |
struct ucsi_altmode updated[UCSI_MAX_ALTMODES]; |
| 507 |
struct ucsi *ucsi = con->ucsi; |
| 508 |
bool multi_dp = false; |
| 509 |
u64 command; |
| 510 |
int ret; |
| 511 |
int len; |
| 512 |
int i; |
| 513 |
int k = 0; |
| 514 |
|
| 515 |
if (recipient == UCSI_RECIPIENT_CON) |
| 516 |
max_altmodes = con->ucsi->cap.num_alt_modes; |
| 517 |
|
| 518 |
memset(orig, 0, sizeof(orig)); |
| 519 |
memset(updated, 0, sizeof(updated)); |
| 520 |
|
| 521 |
/* First get all the alternate modes */ |
| 522 |
for (i = 0; i < max_altmodes; i++) { |
| 523 |
memset(&alt, 0, sizeof(alt)); |
| 524 |
command = UCSI_GET_ALTERNATE_MODES; |
| 525 |
command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); |
| 526 |
command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); |
| 527 |
command |= UCSI_GET_ALTMODE_OFFSET(i); |
| 528 |
len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt)); |
| 529 |
/* |
| 530 |
* We are collecting all altmodes first and then registering. |
| 531 |
* Some type-C device will return zero length data beyond last |
| 532 |
* alternate modes. We should not return if length is zero. |
| 533 |
*/ |
| 534 |
if (len < 0) |
| 535 |
return len; |
| 536 |
|
| 537 |
/* We got all altmodes, now break out and register them */ |
| 538 |
if (!len || !alt.svid) |
| 539 |
break; |
| 540 |
|
| 541 |
orig[k].mid = alt.mid; |
| 542 |
orig[k].svid = alt.svid; |
| 543 |
k++; |
| 544 |
} |
| 545 |
/* |
| 546 |
* Update the original altmode table as some ppms may report |
| 547 |
* multiple DP altmodes. |
| 548 |
*/ |
| 549 |
multi_dp = ucsi->ops->update_altmodes(ucsi, recipient, orig, updated); |
| 550 |
|
| 551 |
/* now register altmodes */ |
| 552 |
for (i = 0; i < max_altmodes; i++) { |
| 553 |
memset(&desc, 0, sizeof(desc)); |
| 554 |
if (multi_dp) { |
| 555 |
desc.svid = updated[i].svid; |
| 556 |
desc.vdo = updated[i].mid; |
| 557 |
} else { |
| 558 |
desc.svid = orig[i].svid; |
| 559 |
desc.vdo = orig[i].mid; |
| 560 |
} |
| 561 |
desc.roles = TYPEC_PORT_DRD; |
| 562 |
|
| 563 |
if (!desc.svid) |
| 564 |
return 0; |
| 565 |
|
| 566 |
ret = ucsi_register_altmode(con, &desc, recipient); |
| 567 |
if (ret) |
| 568 |
return ret; |
| 569 |
} |
| 570 |
|
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient) |
| 575 |
{ |
| 576 |
int max_altmodes = UCSI_MAX_ALTMODES; |
| 577 |
struct typec_altmode_desc desc; |
| 578 |
struct ucsi_altmode alt[2]; |
| 579 |
u64 command; |
| 580 |
int num; |
| 581 |
int ret; |
| 582 |
int len; |
| 583 |
int j; |
| 584 |
int i; |
| 585 |
|
| 586 |
if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS)) |
| 587 |
return 0; |
| 588 |
|
| 589 |
if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0]) |
| 590 |
return 0; |
| 591 |
|
| 592 |
if (con->ucsi->ops->update_altmodes) |
| 593 |
return ucsi_register_altmodes_nvidia(con, recipient); |
| 594 |
|
| 595 |
if (recipient == UCSI_RECIPIENT_CON) |
| 596 |
max_altmodes = con->ucsi->cap.num_alt_modes; |
| 597 |
|
| 598 |
for (i = 0; i < max_altmodes;) { |
| 599 |
memset(alt, 0, sizeof(alt)); |
| 600 |
command = UCSI_GET_ALTERNATE_MODES; |
| 601 |
command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); |
| 602 |
command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); |
| 603 |
command |= UCSI_GET_ALTMODE_OFFSET(i); |
| 604 |
len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt)); |
| 605 |
if (len == -EBUSY) |
| 606 |
continue; |
| 607 |
if (len <= 0) |
| 608 |
return len; |
| 609 |
|
| 610 |
/* |
| 611 |
* This code is requesting one alt mode at a time, but some PPMs |
| 612 |
* may still return two. If that happens both alt modes need be |
| 613 |
* registered and the offset for the next alt mode has to be |
| 614 |
* incremented. |
| 615 |
*/ |
| 616 |
num = len / sizeof(alt[0]); |
| 617 |
i += num; |
| 618 |
|
| 619 |
for (j = 0; j < num; j++) { |
| 620 |
if (!alt[j].svid) |
| 621 |
return 0; |
| 622 |
|
| 623 |
memset(&desc, 0, sizeof(desc)); |
| 624 |
desc.vdo = alt[j].mid; |
| 625 |
desc.svid = alt[j].svid; |
| 626 |
desc.roles = TYPEC_PORT_DRD; |
| 627 |
desc.mode_selection = con->ucsi->ops->add_partner_altmodes && |
| 628 |
!con->typec_cap.no_mode_control; |
| 629 |
|
| 630 |
ret = ucsi_register_altmode(con, &desc, recipient); |
| 631 |
if (ret) |
| 632 |
return ret; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient) |
| 640 |
{ |
| 641 |
const struct typec_altmode *pdev; |
| 642 |
struct typec_altmode **adev; |
| 643 |
int i = 0; |
| 644 |
|
| 645 |
switch (recipient) { |
| 646 |
case UCSI_RECIPIENT_CON: |
| 647 |
adev = con->port_altmode; |
| 648 |
break; |
| 649 |
case UCSI_RECIPIENT_SOP: |
| 650 |
adev = con->partner_altmode; |
| 651 |
break; |
| 652 |
case UCSI_RECIPIENT_SOP_P: |
| 653 |
adev = con->plug_altmode; |
| 654 |
break; |
| 655 |
default: |
| 656 |
return; |
| 657 |
} |
| 658 |
|
| 659 |
while (adev[i]) { |
| 660 |
if (recipient == UCSI_RECIPIENT_SOP) { |
| 661 |
pdev = typec_altmode_get_partner(adev[i]); |
| 662 |
|
| 663 |
if (adev[i]->svid == USB_TYPEC_DP_SID || |
| 664 |
(adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID && |
| 665 |
adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO)) |
| 666 |
ucsi_displayport_remove_partner((void *)pdev); |
| 667 |
else if (adev[i]->svid == USB_TYPEC_TBT_SID) |
| 668 |
ucsi_thunderbolt_remove_partner((void *)pdev); |
| 669 |
} |
| 670 |
typec_unregister_altmode(adev[i]); |
| 671 |
adev[i++] = NULL; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
static int ucsi_get_connector_status(struct ucsi_connector *con, bool conn_ack) |
| 676 |
{ |
| 677 |
u64 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); |
| 678 |
size_t size = min(sizeof(con->status), |
| 679 |
UCSI_MAX_DATA_LENGTH(con->ucsi)); |
| 680 |
int ret; |
| 681 |
|
| 682 |
ret = ucsi_send_command_common(con->ucsi, command, &con->status, size, conn_ack); |
| 683 |
|
| 684 |
return ret < 0 ? ret : 0; |
| 685 |
} |
| 686 |
|
| 687 |
static int ucsi_read_pdos(struct ucsi_connector *con, |
| 688 |
enum typec_role role, int is_partner, |
| 689 |
u32 *pdos, int offset, int num_pdos) |
| 690 |
{ |
| 691 |
struct ucsi *ucsi = con->ucsi; |
| 692 |
u64 command; |
| 693 |
int ret; |
| 694 |
|
| 695 |
if (is_partner && |
| 696 |
ucsi->quirks & UCSI_NO_PARTNER_PDOS && |
| 697 |
(UCSI_CONSTAT(con, PWR_DIR) || !is_source(role))) |
| 698 |
return 0; |
| 699 |
|
| 700 |
command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num); |
| 701 |
command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner); |
| 702 |
command |= UCSI_GET_PDOS_PDO_OFFSET(offset); |
| 703 |
command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1); |
| 704 |
command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0; |
| 705 |
ret = ucsi_send_command(ucsi, command, pdos + offset, |
| 706 |
num_pdos * sizeof(u32)); |
| 707 |
if (ret < 0 && ret != -ETIMEDOUT) |
| 708 |
dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret); |
| 709 |
|
| 710 |
return ret; |
| 711 |
} |
| 712 |
|
| 713 |
static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role, |
| 714 |
int is_partner, u32 *pdos) |
| 715 |
{ |
| 716 |
struct ucsi *ucsi = con->ucsi; |
| 717 |
u8 num_pdos; |
| 718 |
int ret; |
| 719 |
|
| 720 |
if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS)) |
| 721 |
return 0; |
| 722 |
|
| 723 |
/* UCSI max payload means only getting at most 4 PDOs at a time */ |
| 724 |
ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS); |
| 725 |
if (ret < 0) |
| 726 |
return ret; |
| 727 |
|
| 728 |
num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */ |
| 729 |
if (num_pdos < UCSI_MAX_PDOS) |
| 730 |
return num_pdos; |
| 731 |
|
| 732 |
/* get the remaining PDOs, if any */ |
| 733 |
ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS, |
| 734 |
PDO_MAX_OBJECTS - UCSI_MAX_PDOS); |
| 735 |
if (ret < 0) |
| 736 |
return ret; |
| 737 |
|
| 738 |
return ret / sizeof(u32) + num_pdos; |
| 739 |
} |
| 740 |
|
| 741 |
static int ucsi_get_src_pdos(struct ucsi_connector *con) |
| 742 |
{ |
| 743 |
int ret; |
| 744 |
|
| 745 |
ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos); |
| 746 |
if (ret < 0) |
| 747 |
return ret; |
| 748 |
|
| 749 |
con->num_pdos = ret; |
| 750 |
|
| 751 |
ucsi_port_psy_changed(con); |
| 752 |
|
| 753 |
return ret; |
| 754 |
} |
| 755 |
|
| 756 |
static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con, |
| 757 |
enum typec_role role, |
| 758 |
bool is_partner) |
| 759 |
{ |
| 760 |
struct usb_power_delivery_capabilities_desc pd_caps; |
| 761 |
int ret; |
| 762 |
|
| 763 |
ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo); |
| 764 |
if (ret <= 0) |
| 765 |
return ERR_PTR(ret); |
| 766 |
|
| 767 |
if (ret < PDO_MAX_OBJECTS) |
| 768 |
pd_caps.pdo[ret] = 0; |
| 769 |
|
| 770 |
pd_caps.role = role; |
| 771 |
|
| 772 |
return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd, |
| 773 |
&pd_caps); |
| 774 |
} |
| 775 |
|
| 776 |
static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient, |
| 777 |
size_t bytes, void *data, u8 type) |
| 778 |
{ |
| 779 |
size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi)); |
| 780 |
u64 command; |
| 781 |
u8 offset; |
| 782 |
int ret; |
| 783 |
|
| 784 |
for (offset = 0; offset < bytes; offset += len) { |
| 785 |
len = min(len, bytes - offset); |
| 786 |
|
| 787 |
command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num); |
| 788 |
command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient); |
| 789 |
command |= UCSI_GET_PD_MESSAGE_OFFSET(offset); |
| 790 |
command |= UCSI_GET_PD_MESSAGE_BYTES(len); |
| 791 |
command |= UCSI_GET_PD_MESSAGE_TYPE(type); |
| 792 |
|
| 793 |
ret = ucsi_send_command(con->ucsi, command, data + offset, len); |
| 794 |
if (ret < 0) |
| 795 |
return ret; |
| 796 |
} |
| 797 |
|
| 798 |
return 0; |
| 799 |
} |
| 800 |
|
| 801 |
static int ucsi_get_partner_identity(struct ucsi_connector *con) |
| 802 |
{ |
| 803 |
u32 vdo[7] = {}; |
| 804 |
int ret; |
| 805 |
|
| 806 |
ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo, |
| 807 |
UCSI_GET_PD_MESSAGE_TYPE_IDENTITY); |
| 808 |
if (ret < 0) |
| 809 |
return ret; |
| 810 |
|
| 811 |
/* VDM Header is not part of struct usb_pd_identity, so dropping it. */ |
| 812 |
con->partner_identity = *(struct usb_pd_identity *)&vdo[1]; |
| 813 |
|
| 814 |
ret = typec_partner_set_identity(con->partner); |
| 815 |
if (ret < 0) |
| 816 |
dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret); |
| 817 |
|
| 818 |
return ret; |
| 819 |
} |
| 820 |
|
| 821 |
static int ucsi_get_cable_identity(struct ucsi_connector *con) |
| 822 |
{ |
| 823 |
u32 vdo[7] = {}; |
| 824 |
int ret; |
| 825 |
|
| 826 |
ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo, |
| 827 |
UCSI_GET_PD_MESSAGE_TYPE_IDENTITY); |
| 828 |
if (ret < 0) |
| 829 |
return ret; |
| 830 |
|
| 831 |
con->cable_identity = *(struct usb_pd_identity *)&vdo[1]; |
| 832 |
|
| 833 |
ret = typec_cable_set_identity(con->cable); |
| 834 |
if (ret < 0) |
| 835 |
dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret); |
| 836 |
|
| 837 |
return ret; |
| 838 |
} |
| 839 |
|
| 840 |
static int ucsi_check_altmodes(struct ucsi_connector *con) |
| 841 |
{ |
| 842 |
int ret, num_partner_am; |
| 843 |
|
| 844 |
ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP); |
| 845 |
if (ret && ret != -ETIMEDOUT) |
| 846 |
dev_err(con->ucsi->dev, |
| 847 |
"con%d: failed to register partner alt modes (%d)\n", |
| 848 |
con->num, ret); |
| 849 |
|
| 850 |
/* Ignoring the errors in this case. */ |
| 851 |
if (con->partner_altmode[0]) { |
| 852 |
num_partner_am = ucsi_get_num_altmode(con->partner_altmode); |
| 853 |
typec_partner_set_num_altmodes(con->partner, num_partner_am); |
| 854 |
if (con->ucsi->ops->add_partner_altmodes) |
| 855 |
con->ucsi->ops->add_partner_altmodes(con); |
| 856 |
ucsi_altmode_update_active(con); |
| 857 |
return 0; |
| 858 |
} else { |
| 859 |
typec_partner_set_num_altmodes(con->partner, 0); |
| 860 |
} |
| 861 |
|
| 862 |
return ret; |
| 863 |
} |
| 864 |
|
| 865 |
static void ucsi_register_device_pdos(struct ucsi_connector *con) |
| 866 |
{ |
| 867 |
struct ucsi *ucsi = con->ucsi; |
| 868 |
struct usb_power_delivery_desc desc = { ucsi->cap.pd_version }; |
| 869 |
struct usb_power_delivery_capabilities *pd_cap; |
| 870 |
|
| 871 |
if (con->pd) |
| 872 |
return; |
| 873 |
|
| 874 |
con->pd = usb_power_delivery_register(ucsi->dev, &desc); |
| 875 |
|
| 876 |
pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false); |
| 877 |
if (!IS_ERR(pd_cap)) |
| 878 |
con->port_source_caps = pd_cap; |
| 879 |
|
| 880 |
pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false); |
| 881 |
if (!IS_ERR(pd_cap)) |
| 882 |
con->port_sink_caps = pd_cap; |
| 883 |
|
| 884 |
typec_port_set_usb_power_delivery(con->port, con->pd); |
| 885 |
} |
| 886 |
|
| 887 |
static int ucsi_register_partner_pdos(struct ucsi_connector *con) |
| 888 |
{ |
| 889 |
struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version }; |
| 890 |
struct usb_power_delivery_capabilities *cap; |
| 891 |
|
| 892 |
if (con->partner_pd) |
| 893 |
return 0; |
| 894 |
|
| 895 |
con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc); |
| 896 |
if (IS_ERR(con->partner_pd)) |
| 897 |
return PTR_ERR(con->partner_pd); |
| 898 |
|
| 899 |
cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true); |
| 900 |
if (IS_ERR(cap)) |
| 901 |
return PTR_ERR(cap); |
| 902 |
|
| 903 |
con->partner_source_caps = cap; |
| 904 |
|
| 905 |
cap = ucsi_get_pd_caps(con, TYPEC_SINK, true); |
| 906 |
if (IS_ERR(cap)) |
| 907 |
return PTR_ERR(cap); |
| 908 |
|
| 909 |
con->partner_sink_caps = cap; |
| 910 |
|
| 911 |
return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd); |
| 912 |
} |
| 913 |
|
| 914 |
static void ucsi_unregister_partner_pdos(struct ucsi_connector *con) |
| 915 |
{ |
| 916 |
usb_power_delivery_unregister_capabilities(con->partner_sink_caps); |
| 917 |
con->partner_sink_caps = NULL; |
| 918 |
usb_power_delivery_unregister_capabilities(con->partner_source_caps); |
| 919 |
con->partner_source_caps = NULL; |
| 920 |
usb_power_delivery_unregister(con->partner_pd); |
| 921 |
con->partner_pd = NULL; |
| 922 |
} |
| 923 |
|
| 924 |
static int ucsi_register_plug(struct ucsi_connector *con) |
| 925 |
{ |
| 926 |
struct typec_plug *plug; |
| 927 |
struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P}; |
| 928 |
|
| 929 |
plug = typec_register_plug(con->cable, &desc); |
| 930 |
if (IS_ERR(plug)) { |
| 931 |
dev_err(con->ucsi->dev, |
| 932 |
"con%d: failed to register plug (%ld)\n", con->num, |
| 933 |
PTR_ERR(plug)); |
| 934 |
return PTR_ERR(plug); |
| 935 |
} |
| 936 |
|
| 937 |
con->plug = plug; |
| 938 |
return 0; |
| 939 |
} |
| 940 |
|
| 941 |
static void ucsi_unregister_plug(struct ucsi_connector *con) |
| 942 |
{ |
| 943 |
if (!con->plug) |
| 944 |
return; |
| 945 |
|
| 946 |
ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P); |
| 947 |
typec_unregister_plug(con->plug); |
| 948 |
con->plug = NULL; |
| 949 |
} |
| 950 |
|
| 951 |
static int ucsi_register_cable(struct ucsi_connector *con) |
| 952 |
{ |
| 953 |
struct ucsi_cable_property cable_prop; |
| 954 |
struct typec_cable *cable; |
| 955 |
struct typec_cable_desc desc = {}; |
| 956 |
u64 command; |
| 957 |
int ret; |
| 958 |
|
| 959 |
command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num); |
| 960 |
ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop)); |
| 961 |
if (ret < 0) { |
| 962 |
dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret); |
| 963 |
return ret; |
| 964 |
} |
| 965 |
|
| 966 |
switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) { |
| 967 |
case UCSI_CABLE_PROPERTY_PLUG_TYPE_A: |
| 968 |
desc.type = USB_PLUG_TYPE_A; |
| 969 |
break; |
| 970 |
case UCSI_CABLE_PROPERTY_PLUG_TYPE_B: |
| 971 |
desc.type = USB_PLUG_TYPE_B; |
| 972 |
break; |
| 973 |
case UCSI_CABLE_PROPERTY_PLUG_TYPE_C: |
| 974 |
desc.type = USB_PLUG_TYPE_C; |
| 975 |
break; |
| 976 |
default: |
| 977 |
desc.type = USB_PLUG_NONE; |
| 978 |
break; |
| 979 |
} |
| 980 |
|
| 981 |
if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) |
| 982 |
desc.identity = &con->cable_identity; |
| 983 |
desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags); |
| 984 |
|
| 985 |
if (con->ucsi->version >= UCSI_VERSION_2_1) |
| 986 |
desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags); |
| 987 |
|
| 988 |
cable = typec_register_cable(con->port, &desc); |
| 989 |
if (IS_ERR(cable)) { |
| 990 |
dev_err(con->ucsi->dev, |
| 991 |
"con%d: failed to register cable (%ld)\n", con->num, |
| 992 |
PTR_ERR(cable)); |
| 993 |
return PTR_ERR(cable); |
| 994 |
} |
| 995 |
|
| 996 |
con->cable = cable; |
| 997 |
return 0; |
| 998 |
} |
| 999 |
|
| 1000 |
static void ucsi_unregister_cable(struct ucsi_connector *con) |
| 1001 |
{ |
| 1002 |
if (!con->cable) |
| 1003 |
return; |
| 1004 |
|
| 1005 |
ucsi_unregister_plug(con); |
| 1006 |
typec_unregister_cable(con->cable); |
| 1007 |
memset(&con->cable_identity, 0, sizeof(con->cable_identity)); |
| 1008 |
con->cable = NULL; |
| 1009 |
} |
| 1010 |
|
| 1011 |
static int ucsi_check_connector_capability(struct ucsi_connector *con) |
| 1012 |
{ |
| 1013 |
u64 pd_revision; |
| 1014 |
u64 command; |
| 1015 |
int ret; |
| 1016 |
|
| 1017 |
if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1) |
| 1018 |
return 0; |
| 1019 |
|
| 1020 |
command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num); |
| 1021 |
ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap)); |
| 1022 |
if (ret < 0) { |
| 1023 |
dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret); |
| 1024 |
return ret; |
| 1025 |
} |
| 1026 |
|
| 1027 |
pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1); |
| 1028 |
typec_partner_set_pd_revision(con->partner, UCSI_SPEC_REVISION_TO_BCD(pd_revision)); |
| 1029 |
|
| 1030 |
return ret; |
| 1031 |
} |
| 1032 |
|
| 1033 |
static void ucsi_orientation(struct ucsi_connector *con) |
| 1034 |
{ |
| 1035 |
if (con->ucsi->version < UCSI_VERSION_2_0) |
| 1036 |
return; |
| 1037 |
|
| 1038 |
if (!UCSI_CONSTAT(con, CONNECTED)) { |
| 1039 |
typec_set_orientation(con->port, TYPEC_ORIENTATION_NONE); |
| 1040 |
return; |
| 1041 |
} |
| 1042 |
|
| 1043 |
switch (UCSI_CONSTAT(con, ORIENTATION)) { |
| 1044 |
case UCSI_CONSTAT_ORIENTATION_NORMAL: |
| 1045 |
typec_set_orientation(con->port, TYPEC_ORIENTATION_NORMAL); |
| 1046 |
break; |
| 1047 |
case UCSI_CONSTAT_ORIENTATION_REVERSE: |
| 1048 |
typec_set_orientation(con->port, TYPEC_ORIENTATION_REVERSE); |
| 1049 |
break; |
| 1050 |
default: |
| 1051 |
break; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
static void ucsi_pwr_opmode_change(struct ucsi_connector *con) |
| 1056 |
{ |
| 1057 |
switch (UCSI_CONSTAT(con, PWR_OPMODE)) { |
| 1058 |
case UCSI_CONSTAT_PWR_OPMODE_PD: |
| 1059 |
con->rdo = UCSI_CONSTAT(con, RDO); |
| 1060 |
typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD); |
| 1061 |
ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0); |
| 1062 |
ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ); |
| 1063 |
ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ); |
| 1064 |
ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ); |
| 1065 |
break; |
| 1066 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: |
| 1067 |
con->rdo = 0; |
| 1068 |
typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A); |
| 1069 |
ucsi_port_psy_changed(con); |
| 1070 |
break; |
| 1071 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: |
| 1072 |
con->rdo = 0; |
| 1073 |
typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A); |
| 1074 |
ucsi_port_psy_changed(con); |
| 1075 |
break; |
| 1076 |
default: |
| 1077 |
con->rdo = 0; |
| 1078 |
typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB); |
| 1079 |
ucsi_port_psy_changed(con); |
| 1080 |
break; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
static int ucsi_register_partner(struct ucsi_connector *con) |
| 1085 |
{ |
| 1086 |
u8 pwr_opmode = UCSI_CONSTAT(con, PWR_OPMODE); |
| 1087 |
struct typec_partner_desc desc; |
| 1088 |
struct typec_partner *partner; |
| 1089 |
|
| 1090 |
if (con->partner) |
| 1091 |
return 0; |
| 1092 |
|
| 1093 |
memset(&desc, 0, sizeof(desc)); |
| 1094 |
|
| 1095 |
switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { |
| 1096 |
case UCSI_CONSTAT_PARTNER_TYPE_DEBUG: |
| 1097 |
desc.accessory = TYPEC_ACCESSORY_DEBUG; |
| 1098 |
break; |
| 1099 |
case UCSI_CONSTAT_PARTNER_TYPE_AUDIO: |
| 1100 |
desc.accessory = TYPEC_ACCESSORY_AUDIO; |
| 1101 |
break; |
| 1102 |
default: |
| 1103 |
break; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD) |
| 1107 |
ucsi_register_device_pdos(con); |
| 1108 |
|
| 1109 |
if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) |
| 1110 |
desc.identity = &con->partner_identity; |
| 1111 |
desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD; |
| 1112 |
|
| 1113 |
if (con->ucsi->version >= UCSI_VERSION_2_1) { |
| 1114 |
u64 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1); |
| 1115 |
desc.pd_revision = UCSI_SPEC_REVISION_TO_BCD(pd_revision); |
| 1116 |
} |
| 1117 |
|
| 1118 |
partner = typec_register_partner(con->port, &desc); |
| 1119 |
if (IS_ERR(partner)) { |
| 1120 |
dev_err(con->ucsi->dev, |
| 1121 |
"con%d: failed to register partner (%ld)\n", con->num, |
| 1122 |
PTR_ERR(partner)); |
| 1123 |
return PTR_ERR(partner); |
| 1124 |
} |
| 1125 |
|
| 1126 |
con->partner = partner; |
| 1127 |
|
| 1128 |
if (con->ucsi->version >= UCSI_VERSION_3_0 && |
| 1129 |
UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4)) |
| 1130 |
typec_partner_set_usb_mode(partner, USB_MODE_USB4); |
| 1131 |
else if (con->ucsi->version >= UCSI_VERSION_2_0 && |
| 1132 |
UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3)) |
| 1133 |
typec_partner_set_usb_mode(partner, USB_MODE_USB4); |
| 1134 |
|
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
|
| 1138 |
static void ucsi_unregister_partner(struct ucsi_connector *con) |
| 1139 |
{ |
| 1140 |
if (!con->partner) |
| 1141 |
return; |
| 1142 |
|
| 1143 |
typec_set_mode(con->port, TYPEC_STATE_SAFE); |
| 1144 |
if (con->ucsi->ops->remove_partner_altmodes) |
| 1145 |
con->ucsi->ops->remove_partner_altmodes(con); |
| 1146 |
|
| 1147 |
typec_partner_set_usb_power_delivery(con->partner, NULL); |
| 1148 |
ucsi_unregister_partner_pdos(con); |
| 1149 |
ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP); |
| 1150 |
ucsi_unregister_cable(con); |
| 1151 |
typec_unregister_partner(con->partner); |
| 1152 |
memset(&con->partner_identity, 0, sizeof(con->partner_identity)); |
| 1153 |
con->partner = NULL; |
| 1154 |
} |
| 1155 |
|
| 1156 |
static void ucsi_partner_change(struct ucsi_connector *con) |
| 1157 |
{ |
| 1158 |
enum usb_role u_role = USB_ROLE_NONE; |
| 1159 |
int ret; |
| 1160 |
|
| 1161 |
switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { |
| 1162 |
case UCSI_CONSTAT_PARTNER_TYPE_UFP: |
| 1163 |
case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: |
| 1164 |
u_role = USB_ROLE_HOST; |
| 1165 |
fallthrough; |
| 1166 |
case UCSI_CONSTAT_PARTNER_TYPE_CABLE: |
| 1167 |
typec_set_data_role(con->port, TYPEC_HOST); |
| 1168 |
break; |
| 1169 |
case UCSI_CONSTAT_PARTNER_TYPE_DFP: |
| 1170 |
u_role = USB_ROLE_DEVICE; |
| 1171 |
typec_set_data_role(con->port, TYPEC_DEVICE); |
| 1172 |
break; |
| 1173 |
default: |
| 1174 |
break; |
| 1175 |
} |
| 1176 |
|
| 1177 |
if (UCSI_CONSTAT(con, CONNECTED)) { |
| 1178 |
switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { |
| 1179 |
case UCSI_CONSTAT_PARTNER_TYPE_DEBUG: |
| 1180 |
typec_set_mode(con->port, TYPEC_MODE_DEBUG); |
| 1181 |
break; |
| 1182 |
case UCSI_CONSTAT_PARTNER_TYPE_AUDIO: |
| 1183 |
typec_set_mode(con->port, TYPEC_MODE_AUDIO); |
| 1184 |
break; |
| 1185 |
default: |
| 1186 |
if (UCSI_CONSTAT(con, PARTNER_FLAG_USB)) |
| 1187 |
typec_set_mode(con->port, TYPEC_STATE_USB); |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
/* Only notify USB controller if partner supports USB data */ |
| 1192 |
if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB))) |
| 1193 |
u_role = USB_ROLE_NONE; |
| 1194 |
|
| 1195 |
ret = usb_role_switch_set_role(con->usb_role_sw, u_role); |
| 1196 |
if (ret) |
| 1197 |
dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n", |
| 1198 |
con->num, u_role); |
| 1199 |
} |
| 1200 |
|
| 1201 |
static int ucsi_check_connection(struct ucsi_connector *con) |
| 1202 |
{ |
| 1203 |
u8 prev_state = UCSI_CONSTAT(con, CONNECTED); |
| 1204 |
int ret; |
| 1205 |
|
| 1206 |
ret = ucsi_get_connector_status(con, false); |
| 1207 |
if (ret) { |
| 1208 |
dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret); |
| 1209 |
return ret; |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (UCSI_CONSTAT(con, CONNECTED)) { |
| 1213 |
if (prev_state) |
| 1214 |
return 0; |
| 1215 |
ucsi_register_partner(con); |
| 1216 |
ucsi_pwr_opmode_change(con); |
| 1217 |
ucsi_partner_change(con); |
| 1218 |
} else { |
| 1219 |
ucsi_partner_change(con); |
| 1220 |
ucsi_port_psy_changed(con); |
| 1221 |
ucsi_unregister_partner(con); |
| 1222 |
} |
| 1223 |
|
| 1224 |
return 0; |
| 1225 |
} |
| 1226 |
|
| 1227 |
static int ucsi_check_cable(struct ucsi_connector *con) |
| 1228 |
{ |
| 1229 |
int ret, num_plug_am; |
| 1230 |
|
| 1231 |
if (con->cable) |
| 1232 |
return 0; |
| 1233 |
|
| 1234 |
ret = ucsi_register_cable(con); |
| 1235 |
if (ret < 0) |
| 1236 |
return ret; |
| 1237 |
|
| 1238 |
if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) { |
| 1239 |
ret = ucsi_get_cable_identity(con); |
| 1240 |
if (ret < 0) |
| 1241 |
return ret; |
| 1242 |
} |
| 1243 |
|
| 1244 |
if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) { |
| 1245 |
ret = ucsi_register_plug(con); |
| 1246 |
if (ret < 0) |
| 1247 |
return ret; |
| 1248 |
|
| 1249 |
ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P); |
| 1250 |
if (ret < 0) |
| 1251 |
return ret; |
| 1252 |
|
| 1253 |
if (con->plug_altmode[0]) { |
| 1254 |
num_plug_am = ucsi_get_num_altmode(con->plug_altmode); |
| 1255 |
typec_plug_set_num_altmodes(con->plug, num_plug_am); |
| 1256 |
} else { |
| 1257 |
typec_plug_set_num_altmodes(con->plug, 0); |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
return 0; |
| 1262 |
} |
| 1263 |
|
| 1264 |
static void ucsi_handle_connector_change(struct work_struct *work) |
| 1265 |
{ |
| 1266 |
struct ucsi_connector *con = container_of(work, struct ucsi_connector, |
| 1267 |
work); |
| 1268 |
struct ucsi *ucsi = con->ucsi; |
| 1269 |
u8 curr_scale, volt_scale; |
| 1270 |
enum typec_role role, prev_role; |
| 1271 |
u16 change; |
| 1272 |
int ret; |
| 1273 |
u32 val; |
| 1274 |
|
| 1275 |
mutex_lock(&con->lock); |
| 1276 |
|
| 1277 |
if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags)) |
| 1278 |
dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n", |
| 1279 |
__func__); |
| 1280 |
|
| 1281 |
prev_role = UCSI_CONSTAT(con, PWR_DIR); |
| 1282 |
|
| 1283 |
ret = ucsi_get_connector_status(con, true); |
| 1284 |
if (ret) { |
| 1285 |
dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n", |
| 1286 |
__func__, ret); |
| 1287 |
clear_bit(EVENT_PENDING, &con->ucsi->flags); |
| 1288 |
goto out_unlock; |
| 1289 |
} |
| 1290 |
|
| 1291 |
trace_ucsi_connector_change(con->num, con); |
| 1292 |
|
| 1293 |
if (ucsi->ops->connector_status) |
| 1294 |
ucsi->ops->connector_status(con); |
| 1295 |
|
| 1296 |
change = UCSI_CONSTAT(con, CHANGE); |
| 1297 |
role = UCSI_CONSTAT(con, PWR_DIR); |
| 1298 |
|
| 1299 |
if ((change & UCSI_CONSTAT_POWER_DIR_CHANGE) && role != prev_role) { |
| 1300 |
typec_set_pwr_role(con->port, role); |
| 1301 |
|
| 1302 |
/* Some power_supply properties vary depending on the power direction when |
| 1303 |
* connected |
| 1304 |
*/ |
| 1305 |
if (UCSI_CONSTAT(con, CONNECTED)) |
| 1306 |
ucsi_port_psy_changed(con); |
| 1307 |
|
| 1308 |
/* Complete pending power role swap */ |
| 1309 |
if (!completion_done(&con->complete)) |
| 1310 |
complete(&con->complete); |
| 1311 |
} |
| 1312 |
|
| 1313 |
if (change & UCSI_CONSTAT_CONNECT_CHANGE) { |
| 1314 |
typec_set_pwr_role(con->port, role); |
| 1315 |
ucsi_port_psy_changed(con); |
| 1316 |
ucsi_partner_change(con); |
| 1317 |
ucsi_orientation(con); |
| 1318 |
|
| 1319 |
if (UCSI_CONSTAT(con, CONNECTED)) { |
| 1320 |
ucsi_register_partner(con); |
| 1321 |
ucsi_partner_task(con, ucsi_check_connection, 1, HZ); |
| 1322 |
if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) |
| 1323 |
ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ); |
| 1324 |
if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS) |
| 1325 |
ucsi_partner_task(con, ucsi_check_cable, 1, HZ); |
| 1326 |
|
| 1327 |
if (UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) { |
| 1328 |
ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ); |
| 1329 |
ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ); |
| 1330 |
} |
| 1331 |
} else { |
| 1332 |
ucsi_unregister_partner(con); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (change & (UCSI_CONSTAT_POWER_OPMODE_CHANGE | UCSI_CONSTAT_POWER_LEVEL_CHANGE)) |
| 1337 |
ucsi_pwr_opmode_change(con); |
| 1338 |
|
| 1339 |
if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) { |
| 1340 |
ucsi_partner_change(con); |
| 1341 |
ucsi_altmode_update_active(con); |
| 1342 |
|
| 1343 |
/* Complete pending data role swap */ |
| 1344 |
if (!completion_done(&con->complete)) |
| 1345 |
complete(&con->complete); |
| 1346 |
} |
| 1347 |
|
| 1348 |
if (change & UCSI_CONSTAT_CAM_CHANGE) |
| 1349 |
ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ); |
| 1350 |
|
| 1351 |
if (change & (UCSI_CONSTAT_BC_CHANGE | UCSI_CONSTAT_SINK_PATH_CHANGE)) |
| 1352 |
ucsi_port_psy_changed(con); |
| 1353 |
|
| 1354 |
if (con->ucsi->version >= UCSI_VERSION_2_1 && |
| 1355 |
UCSI_CONSTAT(con, PWR_READING_READY_V2_1)) { |
| 1356 |
curr_scale = UCSI_CONSTAT(con, CURRENT_SCALE_V2_1); |
| 1357 |
volt_scale = UCSI_CONSTAT(con, VOLTAGE_SCALE_V2_1); |
| 1358 |
|
| 1359 |
val = UCSI_CONSTAT(con, PEAK_CURRENT_V2_1); |
| 1360 |
con->peak_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val; |
| 1361 |
|
| 1362 |
val = UCSI_CONSTAT(con, AVG_CURRENT_V2_1); |
| 1363 |
con->avg_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val; |
| 1364 |
|
| 1365 |
val = UCSI_CONSTAT(con, VBUS_VOLTAGE_V2_1); |
| 1366 |
con->vbus_voltage = UCSI_CONSTAT_VOLT_SCALE_MULT * volt_scale * val; |
| 1367 |
} |
| 1368 |
|
| 1369 |
out_unlock: |
| 1370 |
mutex_unlock(&con->lock); |
| 1371 |
} |
| 1372 |
|
| 1373 |
/** |
| 1374 |
* ucsi_connector_change - Process Connector Change Event |
| 1375 |
* @ucsi: UCSI Interface |
| 1376 |
* @num: Connector number |
| 1377 |
*/ |
| 1378 |
void ucsi_connector_change(struct ucsi *ucsi, u8 num) |
| 1379 |
{ |
| 1380 |
struct ucsi_connector *con; |
| 1381 |
|
| 1382 |
if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) { |
| 1383 |
dev_dbg(ucsi->dev, "Early connector change event\n"); |
| 1384 |
return; |
| 1385 |
} |
| 1386 |
|
| 1387 |
if (!num || num > ucsi->cap.num_connectors) { |
| 1388 |
dev_warn_ratelimited(ucsi->dev, |
| 1389 |
"Bogus connector change on %u (max %u)\n", |
| 1390 |
num, ucsi->cap.num_connectors); |
| 1391 |
return; |
| 1392 |
} |
| 1393 |
|
| 1394 |
con = &ucsi->connector[num - 1]; |
| 1395 |
|
| 1396 |
if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags)) |
| 1397 |
schedule_work(&con->work); |
| 1398 |
} |
| 1399 |
EXPORT_SYMBOL_GPL(ucsi_connector_change); |
| 1400 |
|
| 1401 |
/* -------------------------------------------------------------------------- */ |
| 1402 |
|
| 1403 |
/* |
| 1404 |
* Hard Reset bit field was defined with value 1 in UCSI spec version 1.0. |
| 1405 |
* Starting with spec version 1.1, Hard Reset bit field was removed from the |
| 1406 |
* CONNECTOR_RESET command, until spec 2.0 reintroduced it with value 0, so, in effect, |
| 1407 |
* the value to pass in to the command for a Hard Reset is different depending |
| 1408 |
* on the supported UCSI version by the LPM. |
| 1409 |
* |
| 1410 |
* For performing a Data Reset on LPMs supporting version 2.0 and greater, |
| 1411 |
* this function needs to be called with the second argument set to 0. |
| 1412 |
*/ |
| 1413 |
static int ucsi_reset_connector(struct ucsi_connector *con, bool hard) |
| 1414 |
{ |
| 1415 |
u64 command; |
| 1416 |
|
| 1417 |
command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num); |
| 1418 |
|
| 1419 |
if (con->ucsi->version < UCSI_VERSION_1_1) |
| 1420 |
command |= hard ? UCSI_CONNECTOR_RESET_HARD_VER_1_0 : 0; |
| 1421 |
else if (con->ucsi->version >= UCSI_VERSION_2_0) |
| 1422 |
command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0; |
| 1423 |
|
| 1424 |
return ucsi_send_command(con->ucsi, command, NULL, 0); |
| 1425 |
} |
| 1426 |
|
| 1427 |
static int ucsi_reset_ppm(struct ucsi *ucsi) |
| 1428 |
{ |
| 1429 |
u64 command; |
| 1430 |
unsigned long tmo; |
| 1431 |
u32 cci; |
| 1432 |
int ret; |
| 1433 |
|
| 1434 |
mutex_lock(&ucsi->ppm_lock); |
| 1435 |
|
| 1436 |
ret = ucsi->ops->poll_cci(ucsi, &cci); |
| 1437 |
if (ret < 0) |
| 1438 |
goto out; |
| 1439 |
|
| 1440 |
/* |
| 1441 |
* If UCSI_CCI_RESET_COMPLETE is already set we must clear |
| 1442 |
* the flag before we start another reset. Send a |
| 1443 |
* UCSI_SET_NOTIFICATION_ENABLE command to achieve this. |
| 1444 |
* Ignore a timeout and try the reset anyway if this fails. |
| 1445 |
*/ |
| 1446 |
if (cci & UCSI_CCI_RESET_COMPLETE) { |
| 1447 |
command = UCSI_SET_NOTIFICATION_ENABLE; |
| 1448 |
ret = ucsi->ops->async_control(ucsi, command); |
| 1449 |
if (ret < 0) |
| 1450 |
goto out; |
| 1451 |
|
| 1452 |
tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); |
| 1453 |
do { |
| 1454 |
ret = ucsi->ops->poll_cci(ucsi, &cci); |
| 1455 |
if (ret < 0) |
| 1456 |
goto out; |
| 1457 |
if (cci & UCSI_CCI_COMMAND_COMPLETE) |
| 1458 |
break; |
| 1459 |
if (time_is_before_jiffies(tmo)) |
| 1460 |
break; |
| 1461 |
msleep(20); |
| 1462 |
} while (1); |
| 1463 |
|
| 1464 |
WARN_ON(cci & UCSI_CCI_RESET_COMPLETE); |
| 1465 |
} |
| 1466 |
|
| 1467 |
command = UCSI_PPM_RESET; |
| 1468 |
ret = ucsi->ops->async_control(ucsi, command); |
| 1469 |
if (ret < 0) |
| 1470 |
goto out; |
| 1471 |
|
| 1472 |
tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); |
| 1473 |
|
| 1474 |
do { |
| 1475 |
if (time_is_before_jiffies(tmo)) { |
| 1476 |
ret = -ETIMEDOUT; |
| 1477 |
goto out; |
| 1478 |
} |
| 1479 |
|
| 1480 |
/* Give the PPM time to process a reset before reading CCI */ |
| 1481 |
msleep(20); |
| 1482 |
|
| 1483 |
ret = ucsi->ops->poll_cci(ucsi, &cci); |
| 1484 |
if (ret) |
| 1485 |
goto out; |
| 1486 |
|
| 1487 |
/* If the PPM is still doing something else, reset it again. */ |
| 1488 |
if (cci & ~UCSI_CCI_RESET_COMPLETE) { |
| 1489 |
ret = ucsi->ops->async_control(ucsi, command); |
| 1490 |
if (ret < 0) |
| 1491 |
goto out; |
| 1492 |
} |
| 1493 |
|
| 1494 |
} while (!(cci & UCSI_CCI_RESET_COMPLETE)); |
| 1495 |
|
| 1496 |
out: |
| 1497 |
mutex_unlock(&ucsi->ppm_lock); |
| 1498 |
return ret; |
| 1499 |
} |
| 1500 |
|
| 1501 |
static int ucsi_role_cmd(struct ucsi_connector *con, u64 command) |
| 1502 |
{ |
| 1503 |
int ret; |
| 1504 |
|
| 1505 |
ret = ucsi_send_command(con->ucsi, command, NULL, 0); |
| 1506 |
if (ret == -ETIMEDOUT) { |
| 1507 |
u64 c; |
| 1508 |
|
| 1509 |
/* PPM most likely stopped responding. Resetting everything. */ |
| 1510 |
ucsi_reset_ppm(con->ucsi); |
| 1511 |
|
| 1512 |
c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy; |
| 1513 |
ucsi_send_command(con->ucsi, c, NULL, 0); |
| 1514 |
|
| 1515 |
ucsi_reset_connector(con, true); |
| 1516 |
} |
| 1517 |
|
| 1518 |
return ret; |
| 1519 |
} |
| 1520 |
|
| 1521 |
static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role) |
| 1522 |
{ |
| 1523 |
struct ucsi_connector *con = typec_get_drvdata(port); |
| 1524 |
u8 partner_type; |
| 1525 |
u64 command; |
| 1526 |
int ret = 0; |
| 1527 |
|
| 1528 |
mutex_lock(&con->lock); |
| 1529 |
|
| 1530 |
if (!con->partner) { |
| 1531 |
ret = -ENOTCONN; |
| 1532 |
goto out_unlock; |
| 1533 |
} |
| 1534 |
|
| 1535 |
partner_type = UCSI_CONSTAT(con, PARTNER_TYPE); |
| 1536 |
if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP && |
| 1537 |
role == TYPEC_DEVICE) || |
| 1538 |
(partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP && |
| 1539 |
role == TYPEC_HOST)) |
| 1540 |
goto out_unlock; |
| 1541 |
|
| 1542 |
reinit_completion(&con->complete); |
| 1543 |
|
| 1544 |
command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num); |
| 1545 |
command |= UCSI_SET_UOR_ROLE(role); |
| 1546 |
command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS; |
| 1547 |
ret = ucsi_role_cmd(con, command); |
| 1548 |
if (ret < 0) |
| 1549 |
goto out_unlock; |
| 1550 |
|
| 1551 |
mutex_unlock(&con->lock); |
| 1552 |
|
| 1553 |
if (!wait_for_completion_timeout(&con->complete, |
| 1554 |
msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) |
| 1555 |
return -ETIMEDOUT; |
| 1556 |
|
| 1557 |
return 0; |
| 1558 |
|
| 1559 |
out_unlock: |
| 1560 |
mutex_unlock(&con->lock); |
| 1561 |
|
| 1562 |
return ret; |
| 1563 |
} |
| 1564 |
|
| 1565 |
static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) |
| 1566 |
{ |
| 1567 |
struct ucsi_connector *con = typec_get_drvdata(port); |
| 1568 |
enum typec_role cur_role; |
| 1569 |
u64 command; |
| 1570 |
int ret = 0; |
| 1571 |
|
| 1572 |
mutex_lock(&con->lock); |
| 1573 |
|
| 1574 |
if (!con->partner) { |
| 1575 |
ret = -ENOTCONN; |
| 1576 |
goto out_unlock; |
| 1577 |
} |
| 1578 |
|
| 1579 |
cur_role = UCSI_CONSTAT(con, PWR_DIR); |
| 1580 |
|
| 1581 |
if (cur_role == role) |
| 1582 |
goto out_unlock; |
| 1583 |
|
| 1584 |
reinit_completion(&con->complete); |
| 1585 |
|
| 1586 |
command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num); |
| 1587 |
command |= UCSI_SET_PDR_ROLE(role); |
| 1588 |
command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS; |
| 1589 |
ret = ucsi_role_cmd(con, command); |
| 1590 |
if (ret < 0) |
| 1591 |
goto out_unlock; |
| 1592 |
|
| 1593 |
mutex_unlock(&con->lock); |
| 1594 |
|
| 1595 |
if (!wait_for_completion_timeout(&con->complete, |
| 1596 |
msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) |
| 1597 |
return -ETIMEDOUT; |
| 1598 |
|
| 1599 |
mutex_lock(&con->lock); |
| 1600 |
|
| 1601 |
/* Something has gone wrong while swapping the role */ |
| 1602 |
if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) { |
| 1603 |
ucsi_reset_connector(con, true); |
| 1604 |
ret = -EPROTO; |
| 1605 |
} |
| 1606 |
|
| 1607 |
out_unlock: |
| 1608 |
mutex_unlock(&con->lock); |
| 1609 |
|
| 1610 |
return ret; |
| 1611 |
} |
| 1612 |
|
| 1613 |
static const struct typec_operations ucsi_ops = { |
| 1614 |
.dr_set = ucsi_dr_swap, |
| 1615 |
.pr_set = ucsi_pr_swap |
| 1616 |
}; |
| 1617 |
|
| 1618 |
/* Caller must call fwnode_handle_put() after use */ |
| 1619 |
static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con) |
| 1620 |
{ |
| 1621 |
struct fwnode_handle *fwnode; |
| 1622 |
int i = 1; |
| 1623 |
|
| 1624 |
device_for_each_child_node(con->ucsi->dev, fwnode) |
| 1625 |
if (i++ == con->num) |
| 1626 |
return fwnode; |
| 1627 |
return NULL; |
| 1628 |
} |
| 1629 |
|
| 1630 |
static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) |
| 1631 |
{ |
| 1632 |
struct typec_capability *cap = &con->typec_cap; |
| 1633 |
enum typec_accessory *accessory = cap->accessory; |
| 1634 |
enum usb_role u_role = USB_ROLE_NONE; |
| 1635 |
u64 command; |
| 1636 |
char *name; |
| 1637 |
int ret; |
| 1638 |
|
| 1639 |
name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num); |
| 1640 |
if (!name) |
| 1641 |
return -ENOMEM; |
| 1642 |
|
| 1643 |
con->wq = create_singlethread_workqueue(name); |
| 1644 |
kfree(name); |
| 1645 |
if (!con->wq) |
| 1646 |
return -ENOMEM; |
| 1647 |
|
| 1648 |
INIT_WORK(&con->work, ucsi_handle_connector_change); |
| 1649 |
init_completion(&con->complete); |
| 1650 |
mutex_init(&con->lock); |
| 1651 |
INIT_LIST_HEAD(&con->partner_tasks); |
| 1652 |
con->ucsi = ucsi; |
| 1653 |
|
| 1654 |
cap->fwnode = ucsi_find_fwnode(con); |
| 1655 |
con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode); |
| 1656 |
if (IS_ERR(con->usb_role_sw)) |
| 1657 |
return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw), |
| 1658 |
"con%d: failed to get usb role switch\n", con->num); |
| 1659 |
|
| 1660 |
/* Delay other interactions with the con until registration is complete */ |
| 1661 |
mutex_lock(&con->lock); |
| 1662 |
|
| 1663 |
/* Get connector capability */ |
| 1664 |
command = UCSI_GET_CONNECTOR_CAPABILITY; |
| 1665 |
command |= UCSI_CONNECTOR_NUMBER(con->num); |
| 1666 |
ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap)); |
| 1667 |
if (ret < 0) |
| 1668 |
goto out_unlock; |
| 1669 |
|
| 1670 |
if (UCSI_CONCAP(con, OPMODE_DRP)) |
| 1671 |
cap->data = TYPEC_PORT_DRD; |
| 1672 |
else if (UCSI_CONCAP(con, OPMODE_DFP)) |
| 1673 |
cap->data = TYPEC_PORT_DFP; |
| 1674 |
else if (UCSI_CONCAP(con, OPMODE_UFP)) |
| 1675 |
cap->data = TYPEC_PORT_UFP; |
| 1676 |
|
| 1677 |
if (UCSI_CONCAP(con, PROVIDER) && UCSI_CONCAP(con, CONSUMER)) |
| 1678 |
cap->type = TYPEC_PORT_DRP; |
| 1679 |
else if (UCSI_CONCAP(con, PROVIDER)) |
| 1680 |
cap->type = TYPEC_PORT_SRC; |
| 1681 |
else if (UCSI_CONCAP(con, CONSUMER)) |
| 1682 |
cap->type = TYPEC_PORT_SNK; |
| 1683 |
|
| 1684 |
cap->revision = ucsi->cap.typec_version; |
| 1685 |
cap->pd_revision = ucsi->cap.pd_version; |
| 1686 |
cap->svdm_version = SVDM_VER_2_0; |
| 1687 |
cap->prefer_role = TYPEC_NO_PREFERRED_ROLE; |
| 1688 |
|
| 1689 |
if (UCSI_CONCAP(con, OPMODE_AUDIO_ACCESSORY)) |
| 1690 |
*accessory++ = TYPEC_ACCESSORY_AUDIO; |
| 1691 |
if (UCSI_CONCAP(con, OPMODE_DEBUG_ACCESSORY)) |
| 1692 |
*accessory = TYPEC_ACCESSORY_DEBUG; |
| 1693 |
|
| 1694 |
if (UCSI_CONCAP_USB2_SUPPORT(con)) |
| 1695 |
cap->usb_capability |= USB_CAPABILITY_USB2; |
| 1696 |
if (UCSI_CONCAP_USB3_SUPPORT(con)) |
| 1697 |
cap->usb_capability |= USB_CAPABILITY_USB3; |
| 1698 |
if (UCSI_CONCAP_USB4_SUPPORT(con)) |
| 1699 |
cap->usb_capability |= USB_CAPABILITY_USB4; |
| 1700 |
|
| 1701 |
cap->driver_data = con; |
| 1702 |
cap->ops = &ucsi_ops; |
| 1703 |
cap->no_mode_control = !(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE); |
| 1704 |
|
| 1705 |
if (ucsi->version >= UCSI_VERSION_2_0) |
| 1706 |
con->typec_cap.orientation_aware = true; |
| 1707 |
|
| 1708 |
if (ucsi->ops->update_connector) |
| 1709 |
ucsi->ops->update_connector(con); |
| 1710 |
|
| 1711 |
ret = ucsi_register_port_psy(con); |
| 1712 |
if (ret) |
| 1713 |
goto out; |
| 1714 |
|
| 1715 |
/* Register the connector */ |
| 1716 |
con->port = typec_register_port(ucsi->dev, cap); |
| 1717 |
if (IS_ERR(con->port)) { |
| 1718 |
ret = PTR_ERR(con->port); |
| 1719 |
goto out; |
| 1720 |
} |
| 1721 |
|
| 1722 |
if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS)) |
| 1723 |
ucsi_register_device_pdos(con); |
| 1724 |
|
| 1725 |
/* Alternate modes */ |
| 1726 |
ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON); |
| 1727 |
if (ret) { |
| 1728 |
dev_err(ucsi->dev, "con%d: failed to register alt modes\n", |
| 1729 |
con->num); |
| 1730 |
goto out; |
| 1731 |
} |
| 1732 |
|
| 1733 |
/* Get the status */ |
| 1734 |
ret = ucsi_get_connector_status(con, false); |
| 1735 |
if (ret) { |
| 1736 |
dev_err(ucsi->dev, "con%d: failed to get status\n", con->num); |
| 1737 |
goto out; |
| 1738 |
} |
| 1739 |
|
| 1740 |
if (ucsi->ops->connector_status) |
| 1741 |
ucsi->ops->connector_status(con); |
| 1742 |
|
| 1743 |
switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { |
| 1744 |
case UCSI_CONSTAT_PARTNER_TYPE_UFP: |
| 1745 |
case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: |
| 1746 |
u_role = USB_ROLE_HOST; |
| 1747 |
fallthrough; |
| 1748 |
case UCSI_CONSTAT_PARTNER_TYPE_CABLE: |
| 1749 |
typec_set_data_role(con->port, TYPEC_HOST); |
| 1750 |
break; |
| 1751 |
case UCSI_CONSTAT_PARTNER_TYPE_DFP: |
| 1752 |
u_role = USB_ROLE_DEVICE; |
| 1753 |
typec_set_data_role(con->port, TYPEC_DEVICE); |
| 1754 |
break; |
| 1755 |
default: |
| 1756 |
break; |
| 1757 |
} |
| 1758 |
|
| 1759 |
/* Check if there is already something connected */ |
| 1760 |
if (UCSI_CONSTAT(con, CONNECTED)) { |
| 1761 |
typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR)); |
| 1762 |
ucsi_register_partner(con); |
| 1763 |
ucsi_pwr_opmode_change(con); |
| 1764 |
ucsi_orientation(con); |
| 1765 |
ucsi_port_psy_changed(con); |
| 1766 |
if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) |
| 1767 |
ucsi_get_partner_identity(con); |
| 1768 |
if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS) |
| 1769 |
ucsi_check_cable(con); |
| 1770 |
} |
| 1771 |
|
| 1772 |
/* Only notify USB controller if partner supports USB data */ |
| 1773 |
if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB))) |
| 1774 |
u_role = USB_ROLE_NONE; |
| 1775 |
|
| 1776 |
ret = usb_role_switch_set_role(con->usb_role_sw, u_role); |
| 1777 |
if (ret) { |
| 1778 |
dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n", |
| 1779 |
con->num, u_role); |
| 1780 |
ret = 0; |
| 1781 |
} |
| 1782 |
|
| 1783 |
if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) { |
| 1784 |
ucsi_register_device_pdos(con); |
| 1785 |
ucsi_get_src_pdos(con); |
| 1786 |
ucsi_check_altmodes(con); |
| 1787 |
ucsi_check_connector_capability(con); |
| 1788 |
} |
| 1789 |
|
| 1790 |
trace_ucsi_register_port(con->num, con); |
| 1791 |
|
| 1792 |
out: |
| 1793 |
fwnode_handle_put(cap->fwnode); |
| 1794 |
out_unlock: |
| 1795 |
mutex_unlock(&con->lock); |
| 1796 |
|
| 1797 |
if (ret && con->wq) { |
| 1798 |
destroy_workqueue(con->wq); |
| 1799 |
con->wq = NULL; |
| 1800 |
} |
| 1801 |
|
| 1802 |
return ret; |
| 1803 |
} |
| 1804 |
|
| 1805 |
static u64 ucsi_get_supported_notifications(struct ucsi *ucsi) |
| 1806 |
{ |
| 1807 |
u16 features = ucsi->cap.features; |
| 1808 |
u64 ntfy = UCSI_ENABLE_NTFY_ALL; |
| 1809 |
|
| 1810 |
if (!(features & UCSI_CAP_ALT_MODE_DETAILS)) |
| 1811 |
ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE; |
| 1812 |
|
| 1813 |
if (!(features & UCSI_CAP_PDO_DETAILS)) |
| 1814 |
ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE | |
| 1815 |
UCSI_ENABLE_NTFY_CAP_CHANGE); |
| 1816 |
|
| 1817 |
if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS)) |
| 1818 |
ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE; |
| 1819 |
|
| 1820 |
if (!(features & UCSI_CAP_PD_RESET)) |
| 1821 |
ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE; |
| 1822 |
|
| 1823 |
if (ucsi->version <= UCSI_VERSION_1_2) |
| 1824 |
return ntfy; |
| 1825 |
|
| 1826 |
ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE; |
| 1827 |
|
| 1828 |
if (features & UCSI_CAP_GET_ATTENTION_VDO) |
| 1829 |
ntfy |= UCSI_ENABLE_NTFY_ATTENTION; |
| 1830 |
|
| 1831 |
if (features & UCSI_CAP_FW_UPDATE_REQUEST) |
| 1832 |
ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ; |
| 1833 |
|
| 1834 |
if (features & UCSI_CAP_SECURITY_REQUEST) |
| 1835 |
ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER; |
| 1836 |
|
| 1837 |
if (features & UCSI_CAP_SET_RETIMER_MODE) |
| 1838 |
ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE; |
| 1839 |
|
| 1840 |
return ntfy; |
| 1841 |
} |
| 1842 |
|
| 1843 |
/** |
| 1844 |
* ucsi_init - Initialize UCSI interface |
| 1845 |
* @ucsi: UCSI to be initialized |
| 1846 |
* |
| 1847 |
* Registers all ports @ucsi has and enables all notification events. |
| 1848 |
*/ |
| 1849 |
static int ucsi_init(struct ucsi *ucsi) |
| 1850 |
{ |
| 1851 |
struct ucsi_connector *con, *connector; |
| 1852 |
u64 command, ntfy; |
| 1853 |
u32 cci; |
| 1854 |
int ret; |
| 1855 |
int i; |
| 1856 |
|
| 1857 |
/* Reset the PPM */ |
| 1858 |
ret = ucsi_reset_ppm(ucsi); |
| 1859 |
if (ret) { |
| 1860 |
dev_err(ucsi->dev, "failed to reset PPM!\n"); |
| 1861 |
goto err; |
| 1862 |
} |
| 1863 |
|
| 1864 |
/* Enable basic notifications */ |
| 1865 |
ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR; |
| 1866 |
command = UCSI_SET_NOTIFICATION_ENABLE | ntfy; |
| 1867 |
ret = ucsi_send_command(ucsi, command, NULL, 0); |
| 1868 |
if (ret < 0) |
| 1869 |
goto err_reset; |
| 1870 |
|
| 1871 |
/* Get PPM capabilities */ |
| 1872 |
command = UCSI_GET_CAPABILITY; |
| 1873 |
ret = ucsi_send_command(ucsi, command, &ucsi->cap, |
| 1874 |
BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE)); |
| 1875 |
if (ret < 0) |
| 1876 |
goto err_reset; |
| 1877 |
|
| 1878 |
if (!ucsi->cap.num_connectors) { |
| 1879 |
ret = -ENODEV; |
| 1880 |
goto err_reset; |
| 1881 |
} |
| 1882 |
/* Check if reserved bit set. This is out of spec but happens in buggy FW */ |
| 1883 |
if (ucsi->cap.num_connectors & 0x80) { |
| 1884 |
dev_warn(ucsi->dev, "UCSI: Invalid num_connectors %d. Likely buggy FW\n", |
| 1885 |
ucsi->cap.num_connectors); |
| 1886 |
ucsi->cap.num_connectors &= 0x7f; // clear bit and carry on |
| 1887 |
} |
| 1888 |
|
| 1889 |
/* Allocate the connectors. Released in ucsi_unregister() */ |
| 1890 |
connector = kzalloc_objs(*connector, ucsi->cap.num_connectors + 1); |
| 1891 |
if (!connector) { |
| 1892 |
ret = -ENOMEM; |
| 1893 |
goto err_reset; |
| 1894 |
} |
| 1895 |
|
| 1896 |
/* Register all connectors */ |
| 1897 |
for (i = 0; i < ucsi->cap.num_connectors; i++) { |
| 1898 |
connector[i].num = i + 1; |
| 1899 |
ret = ucsi_register_port(ucsi, &connector[i]); |
| 1900 |
if (ret) |
| 1901 |
goto err_unregister; |
| 1902 |
} |
| 1903 |
|
| 1904 |
/* Enable all supported notifications */ |
| 1905 |
ntfy = ucsi_get_supported_notifications(ucsi); |
| 1906 |
command = UCSI_SET_NOTIFICATION_ENABLE | ntfy; |
| 1907 |
ret = ucsi_send_command(ucsi, command, NULL, 0); |
| 1908 |
if (ret < 0) |
| 1909 |
goto err_unregister; |
| 1910 |
|
| 1911 |
ucsi->connector = connector; |
| 1912 |
ucsi->ntfy = ntfy; |
| 1913 |
|
| 1914 |
mutex_lock(&ucsi->ppm_lock); |
| 1915 |
ret = ucsi->ops->read_cci(ucsi, &cci); |
| 1916 |
mutex_unlock(&ucsi->ppm_lock); |
| 1917 |
if (ret) |
| 1918 |
return ret; |
| 1919 |
if (UCSI_CCI_CONNECTOR(cci)) |
| 1920 |
ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci)); |
| 1921 |
|
| 1922 |
return 0; |
| 1923 |
|
| 1924 |
err_unregister: |
| 1925 |
for (con = connector; con->port; con++) { |
| 1926 |
if (con->wq) |
| 1927 |
destroy_workqueue(con->wq); |
| 1928 |
ucsi_unregister_partner(con); |
| 1929 |
ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON); |
| 1930 |
ucsi_unregister_port_psy(con); |
| 1931 |
|
| 1932 |
usb_power_delivery_unregister_capabilities(con->port_sink_caps); |
| 1933 |
con->port_sink_caps = NULL; |
| 1934 |
usb_power_delivery_unregister_capabilities(con->port_source_caps); |
| 1935 |
con->port_source_caps = NULL; |
| 1936 |
usb_power_delivery_unregister(con->pd); |
| 1937 |
con->pd = NULL; |
| 1938 |
typec_unregister_port(con->port); |
| 1939 |
con->port = NULL; |
| 1940 |
} |
| 1941 |
kfree(connector); |
| 1942 |
err_reset: |
| 1943 |
memset(&ucsi->cap, 0, sizeof(ucsi->cap)); |
| 1944 |
ucsi_reset_ppm(ucsi); |
| 1945 |
err: |
| 1946 |
return ret; |
| 1947 |
} |
| 1948 |
|
| 1949 |
static void ucsi_resume_work(struct work_struct *work) |
| 1950 |
{ |
| 1951 |
struct ucsi *ucsi = container_of(work, struct ucsi, resume_work); |
| 1952 |
struct ucsi_connector *con; |
| 1953 |
u64 command; |
| 1954 |
int ret; |
| 1955 |
|
| 1956 |
/* Restore UCSI notification enable mask after system resume */ |
| 1957 |
command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; |
| 1958 |
ret = ucsi_send_command(ucsi, command, NULL, 0); |
| 1959 |
if (ret < 0) { |
| 1960 |
dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret); |
| 1961 |
return; |
| 1962 |
} |
| 1963 |
|
| 1964 |
for (con = ucsi->connector; con->port; con++) { |
| 1965 |
mutex_lock(&con->lock); |
| 1966 |
ucsi_partner_task(con, ucsi_check_connection, 1, 0); |
| 1967 |
mutex_unlock(&con->lock); |
| 1968 |
} |
| 1969 |
} |
| 1970 |
|
| 1971 |
int ucsi_resume(struct ucsi *ucsi) |
| 1972 |
{ |
| 1973 |
if (ucsi->connector) |
| 1974 |
queue_work(system_long_wq, &ucsi->resume_work); |
| 1975 |
return 0; |
| 1976 |
} |
| 1977 |
EXPORT_SYMBOL_GPL(ucsi_resume); |
| 1978 |
|
| 1979 |
static void ucsi_init_work(struct work_struct *work) |
| 1980 |
{ |
| 1981 |
struct ucsi *ucsi = container_of(work, struct ucsi, work.work); |
| 1982 |
int ret; |
| 1983 |
|
| 1984 |
ret = ucsi_init(ucsi); |
| 1985 |
if (ret) |
| 1986 |
dev_err_probe(ucsi->dev, ret, "PPM init failed\n"); |
| 1987 |
|
| 1988 |
if (ret == -EPROBE_DEFER) { |
| 1989 |
if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) { |
| 1990 |
dev_err(ucsi->dev, "PPM init failed, stop trying\n"); |
| 1991 |
return; |
| 1992 |
} |
| 1993 |
|
| 1994 |
queue_delayed_work(system_long_wq, &ucsi->work, |
| 1995 |
UCSI_ROLE_SWITCH_INTERVAL); |
| 1996 |
} |
| 1997 |
} |
| 1998 |
|
| 1999 |
/** |
| 2000 |
* ucsi_get_drvdata - Return private driver data pointer |
| 2001 |
* @ucsi: UCSI interface |
| 2002 |
*/ |
| 2003 |
void *ucsi_get_drvdata(struct ucsi *ucsi) |
| 2004 |
{ |
| 2005 |
return ucsi->driver_data; |
| 2006 |
} |
| 2007 |
EXPORT_SYMBOL_GPL(ucsi_get_drvdata); |
| 2008 |
|
| 2009 |
/** |
| 2010 |
* ucsi_set_drvdata - Assign private driver data pointer |
| 2011 |
* @ucsi: UCSI interface |
| 2012 |
* @data: Private data pointer |
| 2013 |
*/ |
| 2014 |
void ucsi_set_drvdata(struct ucsi *ucsi, void *data) |
| 2015 |
{ |
| 2016 |
ucsi->driver_data = data; |
| 2017 |
} |
| 2018 |
EXPORT_SYMBOL_GPL(ucsi_set_drvdata); |
| 2019 |
|
| 2020 |
/** |
| 2021 |
* ucsi_con_mutex_lock - Acquire the connector mutex |
| 2022 |
* @con: The connector interface to lock |
| 2023 |
* |
| 2024 |
* Returns true on success, false if the connector is disconnected |
| 2025 |
*/ |
| 2026 |
bool ucsi_con_mutex_lock(struct ucsi_connector *con) |
| 2027 |
{ |
| 2028 |
bool mutex_locked = false; |
| 2029 |
bool connected = true; |
| 2030 |
|
| 2031 |
while (connected && !mutex_locked) { |
| 2032 |
mutex_locked = mutex_trylock(&con->lock) != 0; |
| 2033 |
connected = UCSI_CONSTAT(con, CONNECTED); |
| 2034 |
if (connected && !mutex_locked) |
| 2035 |
msleep(20); |
| 2036 |
} |
| 2037 |
|
| 2038 |
connected = connected && con->partner; |
| 2039 |
if (!connected && mutex_locked) |
| 2040 |
mutex_unlock(&con->lock); |
| 2041 |
|
| 2042 |
return connected; |
| 2043 |
} |
| 2044 |
|
| 2045 |
/** |
| 2046 |
* ucsi_con_mutex_unlock - Release the connector mutex |
| 2047 |
* @con: The connector interface to unlock |
| 2048 |
*/ |
| 2049 |
void ucsi_con_mutex_unlock(struct ucsi_connector *con) |
| 2050 |
{ |
| 2051 |
mutex_unlock(&con->lock); |
| 2052 |
} |
| 2053 |
|
| 2054 |
/** |
| 2055 |
* ucsi_create - Allocate UCSI instance |
| 2056 |
* @dev: Device interface to the PPM (Platform Policy Manager) |
| 2057 |
* @ops: I/O routines |
| 2058 |
*/ |
| 2059 |
struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) |
| 2060 |
{ |
| 2061 |
struct ucsi *ucsi; |
| 2062 |
|
| 2063 |
if (!ops || |
| 2064 |
!ops->read_version || !ops->read_cci || !ops->poll_cci || |
| 2065 |
!ops->read_message_in || !ops->sync_control || !ops->async_control) |
| 2066 |
return ERR_PTR(-EINVAL); |
| 2067 |
|
| 2068 |
ucsi = kzalloc_obj(*ucsi); |
| 2069 |
if (!ucsi) |
| 2070 |
return ERR_PTR(-ENOMEM); |
| 2071 |
|
| 2072 |
INIT_WORK(&ucsi->resume_work, ucsi_resume_work); |
| 2073 |
INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work); |
| 2074 |
mutex_init(&ucsi->ppm_lock); |
| 2075 |
init_completion(&ucsi->complete); |
| 2076 |
ucsi->dev = dev; |
| 2077 |
ucsi->ops = ops; |
| 2078 |
|
| 2079 |
return ucsi; |
| 2080 |
} |
| 2081 |
EXPORT_SYMBOL_GPL(ucsi_create); |
| 2082 |
|
| 2083 |
/** |
| 2084 |
* ucsi_destroy - Free UCSI instance |
| 2085 |
* @ucsi: UCSI instance to be freed |
| 2086 |
*/ |
| 2087 |
void ucsi_destroy(struct ucsi *ucsi) |
| 2088 |
{ |
| 2089 |
ucsi_debugfs_unregister(ucsi); |
| 2090 |
kfree(ucsi); |
| 2091 |
} |
| 2092 |
EXPORT_SYMBOL_GPL(ucsi_destroy); |
| 2093 |
|
| 2094 |
/** |
| 2095 |
* ucsi_register - Register UCSI interface |
| 2096 |
* @ucsi: UCSI instance |
| 2097 |
*/ |
| 2098 |
int ucsi_register(struct ucsi *ucsi) |
| 2099 |
{ |
| 2100 |
int ret; |
| 2101 |
|
| 2102 |
ret = ucsi->ops->read_version(ucsi, &ucsi->version); |
| 2103 |
if (ret) |
| 2104 |
return ret; |
| 2105 |
|
| 2106 |
if (!ucsi->version) |
| 2107 |
return -ENODEV; |
| 2108 |
|
| 2109 |
/* |
| 2110 |
* Version format is JJ.M.N (JJ = Major version, M = Minor version, |
| 2111 |
* N = sub-minor version). |
| 2112 |
*/ |
| 2113 |
dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x", |
| 2114 |
UCSI_BCD_GET_MAJOR(ucsi->version), |
| 2115 |
UCSI_BCD_GET_MINOR(ucsi->version), |
| 2116 |
UCSI_BCD_GET_SUBMINOR(ucsi->version)); |
| 2117 |
|
| 2118 |
queue_delayed_work(system_long_wq, &ucsi->work, 0); |
| 2119 |
|
| 2120 |
ucsi_debugfs_register(ucsi); |
| 2121 |
return 0; |
| 2122 |
} |
| 2123 |
EXPORT_SYMBOL_GPL(ucsi_register); |
| 2124 |
|
| 2125 |
/** |
| 2126 |
* ucsi_unregister - Unregister UCSI interface |
| 2127 |
* @ucsi: UCSI interface to be unregistered |
| 2128 |
* |
| 2129 |
* Unregister UCSI interface that was created with ucsi_register(). |
| 2130 |
*/ |
| 2131 |
void ucsi_unregister(struct ucsi *ucsi) |
| 2132 |
{ |
| 2133 |
u64 cmd = UCSI_SET_NOTIFICATION_ENABLE; |
| 2134 |
int i; |
| 2135 |
|
| 2136 |
/* Make sure that we are not in the middle of driver initialization */ |
| 2137 |
cancel_delayed_work_sync(&ucsi->work); |
| 2138 |
cancel_work_sync(&ucsi->resume_work); |
| 2139 |
|
| 2140 |
/* Disable notifications */ |
| 2141 |
ucsi->ops->async_control(ucsi, cmd); |
| 2142 |
|
| 2143 |
if (!ucsi->connector) |
| 2144 |
return; |
| 2145 |
|
| 2146 |
for (i = 0; i < ucsi->cap.num_connectors; i++) { |
| 2147 |
cancel_work_sync(&ucsi->connector[i].work); |
| 2148 |
|
| 2149 |
if (ucsi->connector[i].wq) { |
| 2150 |
struct ucsi_work *uwork; |
| 2151 |
|
| 2152 |
mutex_lock(&ucsi->connector[i].lock); |
| 2153 |
/* |
| 2154 |
* queue delayed items immediately so they can execute |
| 2155 |
* and free themselves before the wq is destroyed |
| 2156 |
*/ |
| 2157 |
list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node) |
| 2158 |
mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0); |
| 2159 |
mutex_unlock(&ucsi->connector[i].lock); |
| 2160 |
destroy_workqueue(ucsi->connector[i].wq); |
| 2161 |
} |
| 2162 |
|
| 2163 |
ucsi_unregister_partner(&ucsi->connector[i]); |
| 2164 |
ucsi_unregister_altmodes(&ucsi->connector[i], |
| 2165 |
UCSI_RECIPIENT_CON); |
| 2166 |
ucsi_unregister_port_psy(&ucsi->connector[i]); |
| 2167 |
|
| 2168 |
usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps); |
| 2169 |
ucsi->connector[i].port_sink_caps = NULL; |
| 2170 |
usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps); |
| 2171 |
ucsi->connector[i].port_source_caps = NULL; |
| 2172 |
usb_power_delivery_unregister(ucsi->connector[i].pd); |
| 2173 |
ucsi->connector[i].pd = NULL; |
| 2174 |
typec_unregister_port(ucsi->connector[i].port); |
| 2175 |
} |
| 2176 |
|
| 2177 |
kfree(ucsi->connector); |
| 2178 |
} |
| 2179 |
EXPORT_SYMBOL_GPL(ucsi_unregister); |
| 2180 |
|
| 2181 |
static int __init ucsi_module_init(void) |
| 2182 |
{ |
| 2183 |
ucsi_debugfs_init(); |
| 2184 |
return 0; |
| 2185 |
} |
| 2186 |
module_init(ucsi_module_init); |
| 2187 |
|
| 2188 |
static void __exit ucsi_module_exit(void) |
| 2189 |
{ |
| 2190 |
ucsi_debugfs_exit(); |
| 2191 |
} |
| 2192 |
module_exit(ucsi_module_exit); |
| 2193 |
|
| 2194 |
MODULE_AUTHOR("Heikki Krogerus <[email protected]>"); |
| 2195 |
MODULE_LICENSE("GPL v2"); |
| 2196 |
MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver"); |