1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB Typec-C DisplayPort Alternate Mode driver 4 * 5 * Copyright (C) 2018 Intel Corporation 6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 * 8 * DisplayPort is trademark of VESA (www.vesa.org) 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/mutex.h> 13 #include <linux/module.h> 14 #include <linux/property.h> 15 #include <linux/usb/pd_vdo.h> 16 #include <linux/usb/typec_dp.h> 17 #include <drm/drm_connector.h> 18 #include "displayport.h" 19 20 #define DP_HEADER(_dp, ver, cmd) (VDO((_dp)->alt->svid, 1, ver, cmd) \ 21 | VDO_OPOS(USB_TYPEC_DP_MODE)) 22 23 enum { 24 DP_CONF_USB, 25 DP_CONF_DFP_D, 26 DP_CONF_UFP_D, 27 DP_CONF_DUAL_D, 28 }; 29 30 /* Pin assignments that use USB3.1 Gen2 signaling to carry DP protocol */ 31 #define DP_PIN_ASSIGN_GEN2_BR_MASK (BIT(DP_PIN_ASSIGN_A) | \ 32 BIT(DP_PIN_ASSIGN_B)) 33 34 /* Pin assignments that use DP v1.3 signaling to carry DP protocol */ 35 #define DP_PIN_ASSIGN_DP_BR_MASK (BIT(DP_PIN_ASSIGN_C) | \ 36 BIT(DP_PIN_ASSIGN_D) | \ 37 BIT(DP_PIN_ASSIGN_E) | \ 38 BIT(DP_PIN_ASSIGN_F)) 39 40 /* DP only pin assignments */ 41 #define DP_PIN_ASSIGN_DP_ONLY_MASK (BIT(DP_PIN_ASSIGN_A) | \ 42 BIT(DP_PIN_ASSIGN_C) | \ 43 BIT(DP_PIN_ASSIGN_E)) 44 45 /* Pin assignments where one channel is for USB */ 46 #define DP_PIN_ASSIGN_MULTI_FUNC_MASK (BIT(DP_PIN_ASSIGN_B) | \ 47 BIT(DP_PIN_ASSIGN_D) | \ 48 BIT(DP_PIN_ASSIGN_F)) 49 50 enum dp_state { 51 DP_STATE_IDLE, 52 DP_STATE_ENTER, 53 DP_STATE_UPDATE, 54 DP_STATE_CONFIGURE, 55 DP_STATE_EXIT, 56 }; 57 58 struct dp_altmode { 59 struct typec_displayport_data data; 60 61 enum dp_state state; 62 bool hpd; 63 64 struct mutex lock; /* device lock */ 65 struct work_struct work; 66 struct typec_altmode *alt; 67 const struct typec_altmode *port; 68 struct fwnode_handle *connector_fwnode; 69 }; 70 71 static int dp_altmode_notify(struct dp_altmode *dp) 72 { 73 unsigned long conf; 74 u8 state; 75 76 if (dp->data.conf) { 77 state = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf)); 78 conf = TYPEC_MODAL_STATE(state); 79 } else { 80 conf = TYPEC_STATE_USB; 81 } 82 83 return typec_altmode_notify(dp->alt, conf, &dp->data); 84 } 85 86 static int dp_altmode_configure(struct dp_altmode *dp, u8 con) 87 { 88 u32 conf = DP_CONF_SIGNALING_DP; /* Only DP signaling supported */ 89 u8 pin_assign = 0; 90 91 switch (con) { 92 case DP_STATUS_CON_DISABLED: 93 return 0; 94 case DP_STATUS_CON_DFP_D: 95 conf |= DP_CONF_UFP_U_AS_DFP_D; 96 pin_assign = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo) & 97 DP_CAP_DFP_D_PIN_ASSIGN(dp->port->vdo); 98 break; 99 case DP_STATUS_CON_UFP_D: 100 case DP_STATUS_CON_BOTH: /* NOTE: First acting as DP source */ 101 conf |= DP_CONF_UFP_U_AS_UFP_D; 102 pin_assign = DP_CAP_PIN_ASSIGN_UFP_D(dp->alt->vdo) & 103 DP_CAP_PIN_ASSIGN_DFP_D(dp->port->vdo); 104 break; 105 default: 106 break; 107 } 108 109 /* Determining the initial pin assignment. */ 110 if (!DP_CONF_GET_PIN_ASSIGN(dp->data.conf)) { 111 /* Is USB together with DP preferred */ 112 if (dp->data.status & DP_STATUS_PREFER_MULTI_FUNC && 113 pin_assign & DP_PIN_ASSIGN_MULTI_FUNC_MASK) 114 pin_assign &= DP_PIN_ASSIGN_MULTI_FUNC_MASK; 115 else if (pin_assign & DP_PIN_ASSIGN_DP_ONLY_MASK) 116 pin_assign &= DP_PIN_ASSIGN_DP_ONLY_MASK; 117 118 if (!pin_assign) 119 return -EINVAL; 120 121 conf |= DP_CONF_SET_PIN_ASSIGN(pin_assign); 122 } 123 124 dp->data.conf = conf; 125 126 return 0; 127 } 128 129 static int dp_altmode_status_update(struct dp_altmode *dp) 130 { 131 bool configured = !!DP_CONF_GET_PIN_ASSIGN(dp->data.conf); 132 bool hpd = !!(dp->data.status & DP_STATUS_HPD_STATE); 133 u8 con = DP_STATUS_CONNECTION(dp->data.status); 134 int ret = 0; 135 136 if (configured && (dp->data.status & DP_STATUS_SWITCH_TO_USB)) { 137 dp->data.conf = 0; 138 dp->state = DP_STATE_CONFIGURE; 139 } else if (dp->data.status & DP_STATUS_EXIT_DP_MODE) { 140 dp->state = DP_STATE_EXIT; 141 } else if (!(con & DP_CONF_CURRENTLY(dp->data.conf))) { 142 ret = dp_altmode_configure(dp, con); 143 if (!ret) 144 dp->state = DP_STATE_CONFIGURE; 145 } else { 146 if (dp->hpd != hpd) { 147 drm_connector_oob_hotplug_event(dp->connector_fwnode); 148 dp->hpd = hpd; 149 } 150 } 151 152 return ret; 153 } 154 155 static int dp_altmode_configured(struct dp_altmode *dp) 156 { 157 sysfs_notify(&dp->alt->dev.kobj, "displayport", "configuration"); 158 sysfs_notify(&dp->alt->dev.kobj, "displayport", "pin_assignment"); 159 160 return dp_altmode_notify(dp); 161 } 162 163 static int dp_altmode_configure_vdm(struct dp_altmode *dp, u32 conf) 164 { 165 int svdm_version = typec_altmode_get_svdm_version(dp->alt); 166 u32 header; 167 int ret; 168 169 if (svdm_version < 0) 170 return svdm_version; 171 172 header = DP_HEADER(dp, svdm_version, DP_CMD_CONFIGURE); 173 ret = typec_altmode_notify(dp->alt, TYPEC_STATE_SAFE, &dp->data); 174 if (ret) { 175 dev_err(&dp->alt->dev, 176 "unable to put to connector to safe mode\n"); 177 return ret; 178 } 179 180 ret = typec_altmode_vdm(dp->alt, header, &conf, 2); 181 if (ret) 182 dp_altmode_notify(dp); 183 184 return ret; 185 } 186 187 static void dp_altmode_work(struct work_struct *work) 188 { 189 struct dp_altmode *dp = container_of(work, struct dp_altmode, work); 190 int svdm_version; 191 u32 header; 192 u32 vdo; 193 int ret; 194 195 mutex_lock(&dp->lock); 196 197 switch (dp->state) { 198 case DP_STATE_ENTER: 199 ret = typec_altmode_enter(dp->alt, NULL); 200 if (ret && ret != -EBUSY) 201 dev_err(&dp->alt->dev, "failed to enter mode\n"); 202 break; 203 case DP_STATE_UPDATE: 204 svdm_version = typec_altmode_get_svdm_version(dp->alt); 205 if (svdm_version < 0) 206 break; 207 header = DP_HEADER(dp, svdm_version, DP_CMD_STATUS_UPDATE); 208 vdo = 1; 209 ret = typec_altmode_vdm(dp->alt, header, &vdo, 2); 210 if (ret) 211 dev_err(&dp->alt->dev, 212 "unable to send Status Update command (%d)\n", 213 ret); 214 break; 215 case DP_STATE_CONFIGURE: 216 ret = dp_altmode_configure_vdm(dp, dp->data.conf); 217 if (ret) 218 dev_err(&dp->alt->dev, 219 "unable to send Configure command (%d)\n", ret); 220 break; 221 case DP_STATE_EXIT: 222 if (typec_altmode_exit(dp->alt)) 223 dev_err(&dp->alt->dev, "Exit Mode Failed!\n"); 224 break; 225 default: 226 break; 227 } 228 229 dp->state = DP_STATE_IDLE; 230 231 mutex_unlock(&dp->lock); 232 } 233 234 static void dp_altmode_attention(struct typec_altmode *alt, const u32 vdo) 235 { 236 struct dp_altmode *dp = typec_altmode_get_drvdata(alt); 237 u8 old_state; 238 239 mutex_lock(&dp->lock); 240 241 old_state = dp->state; 242 dp->data.status = vdo; 243 244 if (old_state != DP_STATE_IDLE) 245 dev_warn(&alt->dev, "ATTENTION while processing state %d\n", 246 old_state); 247 248 if (dp_altmode_status_update(dp)) 249 dev_warn(&alt->dev, "%s: status update failed\n", __func__); 250 251 if (dp_altmode_notify(dp)) 252 dev_err(&alt->dev, "%s: notification failed\n", __func__); 253 254 if (old_state == DP_STATE_IDLE && dp->state != DP_STATE_IDLE) 255 schedule_work(&dp->work); 256 257 mutex_unlock(&dp->lock); 258 } 259 260 static int dp_altmode_vdm(struct typec_altmode *alt, 261 const u32 hdr, const u32 *vdo, int count) 262 { 263 struct dp_altmode *dp = typec_altmode_get_drvdata(alt); 264 int cmd_type = PD_VDO_CMDT(hdr); 265 int cmd = PD_VDO_CMD(hdr); 266 int ret = 0; 267 268 mutex_lock(&dp->lock); 269 270 if (dp->state != DP_STATE_IDLE) { 271 ret = -EBUSY; 272 goto err_unlock; 273 } 274 275 switch (cmd_type) { 276 case CMDT_RSP_ACK: 277 switch (cmd) { 278 case CMD_ENTER_MODE: 279 dp->state = DP_STATE_UPDATE; 280 break; 281 case CMD_EXIT_MODE: 282 dp->data.status = 0; 283 dp->data.conf = 0; 284 break; 285 case DP_CMD_STATUS_UPDATE: 286 dp->data.status = *vdo; 287 ret = dp_altmode_status_update(dp); 288 break; 289 case DP_CMD_CONFIGURE: 290 ret = dp_altmode_configured(dp); 291 break; 292 default: 293 break; 294 } 295 break; 296 case CMDT_RSP_NAK: 297 switch (cmd) { 298 case DP_CMD_CONFIGURE: 299 dp->data.conf = 0; 300 ret = dp_altmode_configured(dp); 301 break; 302 default: 303 break; 304 } 305 break; 306 default: 307 break; 308 } 309 310 if (dp->state != DP_STATE_IDLE) 311 schedule_work(&dp->work); 312 313 err_unlock: 314 mutex_unlock(&dp->lock); 315 return ret; 316 } 317 318 static int dp_altmode_activate(struct typec_altmode *alt, int activate) 319 { 320 return activate ? typec_altmode_enter(alt, NULL) : 321 typec_altmode_exit(alt); 322 } 323 324 static const struct typec_altmode_ops dp_altmode_ops = { 325 .attention = dp_altmode_attention, 326 .vdm = dp_altmode_vdm, 327 .activate = dp_altmode_activate, 328 }; 329 330 static const char * const configurations[] = { 331 [DP_CONF_USB] = "USB", 332 [DP_CONF_DFP_D] = "source", 333 [DP_CONF_UFP_D] = "sink", 334 }; 335 336 static ssize_t 337 configuration_store(struct device *dev, struct device_attribute *attr, 338 const char *buf, size_t size) 339 { 340 struct dp_altmode *dp = dev_get_drvdata(dev); 341 u32 conf; 342 u32 cap; 343 int con; 344 int ret = 0; 345 346 con = sysfs_match_string(configurations, buf); 347 if (con < 0) 348 return con; 349 350 mutex_lock(&dp->lock); 351 352 if (dp->state != DP_STATE_IDLE) { 353 ret = -EBUSY; 354 goto err_unlock; 355 } 356 357 cap = DP_CAP_CAPABILITY(dp->alt->vdo); 358 359 if ((con == DP_CONF_DFP_D && !(cap & DP_CAP_DFP_D)) || 360 (con == DP_CONF_UFP_D && !(cap & DP_CAP_UFP_D))) { 361 ret = -EINVAL; 362 goto err_unlock; 363 } 364 365 conf = dp->data.conf & ~DP_CONF_DUAL_D; 366 conf |= con; 367 368 if (dp->alt->active) { 369 ret = dp_altmode_configure_vdm(dp, conf); 370 if (ret) 371 goto err_unlock; 372 } 373 374 dp->data.conf = conf; 375 376 err_unlock: 377 mutex_unlock(&dp->lock); 378 379 return ret ? ret : size; 380 } 381 382 static ssize_t configuration_show(struct device *dev, 383 struct device_attribute *attr, char *buf) 384 { 385 struct dp_altmode *dp = dev_get_drvdata(dev); 386 int len; 387 u8 cap; 388 u8 cur; 389 int i; 390 391 mutex_lock(&dp->lock); 392 393 cap = DP_CAP_CAPABILITY(dp->alt->vdo); 394 cur = DP_CONF_CURRENTLY(dp->data.conf); 395 396 len = sprintf(buf, "%s ", cur ? "USB" : "[USB]"); 397 398 for (i = 1; i < ARRAY_SIZE(configurations); i++) { 399 if (i == cur) 400 len += sprintf(buf + len, "[%s] ", configurations[i]); 401 else if ((i == DP_CONF_DFP_D && cap & DP_CAP_DFP_D) || 402 (i == DP_CONF_UFP_D && cap & DP_CAP_UFP_D)) 403 len += sprintf(buf + len, "%s ", configurations[i]); 404 } 405 406 mutex_unlock(&dp->lock); 407 408 buf[len - 1] = '\n'; 409 return len; 410 } 411 static DEVICE_ATTR_RW(configuration); 412 413 static const char * const pin_assignments[] = { 414 [DP_PIN_ASSIGN_A] = "A", 415 [DP_PIN_ASSIGN_B] = "B", 416 [DP_PIN_ASSIGN_C] = "C", 417 [DP_PIN_ASSIGN_D] = "D", 418 [DP_PIN_ASSIGN_E] = "E", 419 [DP_PIN_ASSIGN_F] = "F", 420 }; 421 422 static ssize_t 423 pin_assignment_store(struct device *dev, struct device_attribute *attr, 424 const char *buf, size_t size) 425 { 426 struct dp_altmode *dp = dev_get_drvdata(dev); 427 u8 assignments; 428 u32 conf; 429 int ret; 430 431 ret = sysfs_match_string(pin_assignments, buf); 432 if (ret < 0) 433 return ret; 434 435 conf = DP_CONF_SET_PIN_ASSIGN(BIT(ret)); 436 ret = 0; 437 438 mutex_lock(&dp->lock); 439 440 if (conf & dp->data.conf) 441 goto out_unlock; 442 443 if (dp->state != DP_STATE_IDLE) { 444 ret = -EBUSY; 445 goto out_unlock; 446 } 447 448 if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D) 449 assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo); 450 else 451 assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo); 452 453 if (!(DP_CONF_GET_PIN_ASSIGN(conf) & assignments)) { 454 ret = -EINVAL; 455 goto out_unlock; 456 } 457 458 conf |= dp->data.conf & ~DP_CONF_PIN_ASSIGNEMENT_MASK; 459 460 /* Only send Configure command if a configuration has been set */ 461 if (dp->alt->active && DP_CONF_CURRENTLY(dp->data.conf)) { 462 ret = dp_altmode_configure_vdm(dp, conf); 463 if (ret) 464 goto out_unlock; 465 } 466 467 dp->data.conf = conf; 468 469 out_unlock: 470 mutex_unlock(&dp->lock); 471 472 return ret ? ret : size; 473 } 474 475 static ssize_t pin_assignment_show(struct device *dev, 476 struct device_attribute *attr, char *buf) 477 { 478 struct dp_altmode *dp = dev_get_drvdata(dev); 479 u8 assignments; 480 int len = 0; 481 u8 cur; 482 int i; 483 484 mutex_lock(&dp->lock); 485 486 cur = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf)); 487 488 if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D) 489 assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo); 490 else 491 assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo); 492 493 for (i = 0; assignments; assignments >>= 1, i++) { 494 if (assignments & 1) { 495 if (i == cur) 496 len += sprintf(buf + len, "[%s] ", 497 pin_assignments[i]); 498 else 499 len += sprintf(buf + len, "%s ", 500 pin_assignments[i]); 501 } 502 } 503 504 mutex_unlock(&dp->lock); 505 506 buf[len - 1] = '\n'; 507 return len; 508 } 509 static DEVICE_ATTR_RW(pin_assignment); 510 511 static struct attribute *dp_altmode_attrs[] = { 512 &dev_attr_configuration.attr, 513 &dev_attr_pin_assignment.attr, 514 NULL 515 }; 516 517 static const struct attribute_group dp_altmode_group = { 518 .name = "displayport", 519 .attrs = dp_altmode_attrs, 520 }; 521 522 int dp_altmode_probe(struct typec_altmode *alt) 523 { 524 const struct typec_altmode *port = typec_altmode_get_partner(alt); 525 struct fwnode_handle *fwnode; 526 struct dp_altmode *dp; 527 int ret; 528 529 /* FIXME: Port can only be DFP_U. */ 530 531 /* Make sure we have compatiple pin configurations */ 532 if (!(DP_CAP_DFP_D_PIN_ASSIGN(port->vdo) & 533 DP_CAP_UFP_D_PIN_ASSIGN(alt->vdo)) && 534 !(DP_CAP_UFP_D_PIN_ASSIGN(port->vdo) & 535 DP_CAP_DFP_D_PIN_ASSIGN(alt->vdo))) 536 return -ENODEV; 537 538 ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group); 539 if (ret) 540 return ret; 541 542 dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL); 543 if (!dp) 544 return -ENOMEM; 545 546 INIT_WORK(&dp->work, dp_altmode_work); 547 mutex_init(&dp->lock); 548 dp->port = port; 549 dp->alt = alt; 550 551 alt->desc = "DisplayPort"; 552 alt->ops = &dp_altmode_ops; 553 554 fwnode = dev_fwnode(alt->dev.parent->parent); /* typec_port fwnode */ 555 dp->connector_fwnode = fwnode_find_reference(fwnode, "displayport", 0); 556 if (IS_ERR(dp->connector_fwnode)) 557 dp->connector_fwnode = NULL; 558 559 typec_altmode_set_drvdata(alt, dp); 560 561 dp->state = DP_STATE_ENTER; 562 schedule_work(&dp->work); 563 564 return 0; 565 } 566 EXPORT_SYMBOL_GPL(dp_altmode_probe); 567 568 void dp_altmode_remove(struct typec_altmode *alt) 569 { 570 struct dp_altmode *dp = typec_altmode_get_drvdata(alt); 571 572 sysfs_remove_group(&alt->dev.kobj, &dp_altmode_group); 573 cancel_work_sync(&dp->work); 574 575 if (dp->connector_fwnode) { 576 if (dp->hpd) 577 drm_connector_oob_hotplug_event(dp->connector_fwnode); 578 579 fwnode_handle_put(dp->connector_fwnode); 580 } 581 } 582 EXPORT_SYMBOL_GPL(dp_altmode_remove); 583 584 static const struct typec_device_id dp_typec_id[] = { 585 { USB_TYPEC_DP_SID, USB_TYPEC_DP_MODE }, 586 { }, 587 }; 588 MODULE_DEVICE_TABLE(typec, dp_typec_id); 589 590 static struct typec_altmode_driver dp_altmode_driver = { 591 .id_table = dp_typec_id, 592 .probe = dp_altmode_probe, 593 .remove = dp_altmode_remove, 594 .driver = { 595 .name = "typec_displayport", 596 .owner = THIS_MODULE, 597 }, 598 }; 599 module_typec_altmode_driver(dp_altmode_driver); 600 601 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); 602 MODULE_LICENSE("GPL v2"); 603 MODULE_DESCRIPTION("DisplayPort Alternate Mode"); 604