Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: typec: ucsi: Support for DisplayPort alt mode

This makes it possible to bind a driver to a DisplayPort
alt mode adapter devices.

The driver attempts to cope with the limitations of UCSI by
"emulating" behaviour and attempting to guess things when
ever possible in order to satisfy the requirements the
standard DisplayPort alt mode driver has.

Tested-by: Ajay Gupta <ajayg@nvidia.com>
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Heikki Krogerus and committed by
Greg Kroah-Hartman
af8622f6 ad74b864

+364 -8
+9 -6
drivers/usb/typec/ucsi/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 - CFLAGS_trace.o := -I$(src) 2 + CFLAGS_trace.o := -I$(src) 3 3 4 - obj-$(CONFIG_TYPEC_UCSI) += typec_ucsi.o 4 + obj-$(CONFIG_TYPEC_UCSI) += typec_ucsi.o 5 5 6 - typec_ucsi-y := ucsi.o 6 + typec_ucsi-y := ucsi.o 7 7 8 - typec_ucsi-$(CONFIG_TRACING) += trace.o 8 + typec_ucsi-$(CONFIG_TRACING) += trace.o 9 9 10 - obj-$(CONFIG_UCSI_ACPI) += ucsi_acpi.o 10 + ifneq ($(CONFIG_TYPEC_DP_ALTMODE),) 11 + typec_ucsi-y += displayport.o 12 + endif 11 13 12 - obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o 14 + obj-$(CONFIG_UCSI_ACPI) += ucsi_acpi.o 15 + obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o
+315
drivers/usb/typec/ucsi/displayport.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * UCSI DisplayPort Alternate Mode Support 4 + * 5 + * Copyright (C) 2018, Intel Corporation 6 + * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 + */ 8 + 9 + #include <linux/usb/typec_dp.h> 10 + #include <linux/usb/pd_vdo.h> 11 + 12 + #include "ucsi.h" 13 + 14 + #define UCSI_CMD_SET_NEW_CAM(_con_num_, _enter_, _cam_, _am_) \ 15 + (UCSI_SET_NEW_CAM | ((_con_num_) << 16) | ((_enter_) << 23) | \ 16 + ((_cam_) << 24) | ((u64)(_am_) << 32)) 17 + 18 + struct ucsi_dp { 19 + struct typec_displayport_data data; 20 + struct ucsi_connector *con; 21 + struct typec_altmode *alt; 22 + struct work_struct work; 23 + int offset; 24 + 25 + bool override; 26 + bool initialized; 27 + 28 + u32 header; 29 + u32 *vdo_data; 30 + u8 vdo_size; 31 + }; 32 + 33 + /* 34 + * Note. Alternate mode control is optional feature in UCSI. It means that even 35 + * if the system supports alternate modes, the OS may not be aware of them. 36 + * 37 + * In most cases however, the OS will be able to see the supported alternate 38 + * modes, but it may still not be able to configure them, not even enter or exit 39 + * them. That is because UCSI defines alt mode details and alt mode "overriding" 40 + * as separate options. 41 + * 42 + * In case alt mode details are supported, but overriding is not, the driver 43 + * will still display the supported pin assignments and configuration, but any 44 + * changes the user attempts to do will lead into failure with return value of 45 + * -EOPNOTSUPP. 46 + */ 47 + 48 + static int ucsi_displayport_enter(struct typec_altmode *alt) 49 + { 50 + struct ucsi_dp *dp = typec_altmode_get_drvdata(alt); 51 + struct ucsi_control ctrl; 52 + u8 cur = 0; 53 + int ret; 54 + 55 + mutex_lock(&dp->con->lock); 56 + 57 + if (!dp->override && dp->initialized) { 58 + const struct typec_altmode *p = typec_altmode_get_partner(alt); 59 + 60 + dev_warn(&p->dev, 61 + "firmware doesn't support alternate mode overriding\n"); 62 + mutex_unlock(&dp->con->lock); 63 + return -EOPNOTSUPP; 64 + } 65 + 66 + UCSI_CMD_GET_CURRENT_CAM(ctrl, dp->con->num); 67 + ret = ucsi_send_command(dp->con->ucsi, &ctrl, &cur, sizeof(cur)); 68 + if (ret < 0) { 69 + if (dp->con->ucsi->ppm->data->version > 0x0100) { 70 + mutex_unlock(&dp->con->lock); 71 + return ret; 72 + } 73 + cur = 0xff; 74 + } 75 + 76 + if (cur != 0xff) { 77 + mutex_unlock(&dp->con->lock); 78 + return -EBUSY; 79 + } 80 + 81 + /* 82 + * We can't send the New CAM command yet to the PPM as it needs the 83 + * configuration value as well. Pretending that we have now entered the 84 + * mode, and letting the alt mode driver continue. 85 + */ 86 + 87 + dp->header = VDO(USB_TYPEC_DP_SID, 1, CMD_ENTER_MODE); 88 + dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE); 89 + dp->header |= VDO_CMDT(CMDT_RSP_ACK); 90 + 91 + dp->vdo_data = NULL; 92 + dp->vdo_size = 1; 93 + 94 + schedule_work(&dp->work); 95 + 96 + mutex_unlock(&dp->con->lock); 97 + 98 + return 0; 99 + } 100 + 101 + static int ucsi_displayport_exit(struct typec_altmode *alt) 102 + { 103 + struct ucsi_dp *dp = typec_altmode_get_drvdata(alt); 104 + struct ucsi_control ctrl; 105 + int ret = 0; 106 + 107 + mutex_lock(&dp->con->lock); 108 + 109 + if (!dp->override) { 110 + const struct typec_altmode *p = typec_altmode_get_partner(alt); 111 + 112 + dev_warn(&p->dev, 113 + "firmware doesn't support alternate mode overriding\n"); 114 + ret = -EOPNOTSUPP; 115 + goto out_unlock; 116 + } 117 + 118 + ctrl.raw_cmd = UCSI_CMD_SET_NEW_CAM(dp->con->num, 0, dp->offset, 0); 119 + ret = ucsi_send_command(dp->con->ucsi, &ctrl, NULL, 0); 120 + if (ret < 0) 121 + goto out_unlock; 122 + 123 + dp->header = VDO(USB_TYPEC_DP_SID, 1, CMD_EXIT_MODE); 124 + dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE); 125 + dp->header |= VDO_CMDT(CMDT_RSP_ACK); 126 + 127 + dp->vdo_data = NULL; 128 + dp->vdo_size = 1; 129 + 130 + schedule_work(&dp->work); 131 + 132 + out_unlock: 133 + mutex_unlock(&dp->con->lock); 134 + 135 + return ret; 136 + } 137 + 138 + /* 139 + * We do not actually have access to the Status Update VDO, so we have to guess 140 + * things. 141 + */ 142 + static int ucsi_displayport_status_update(struct ucsi_dp *dp) 143 + { 144 + u32 cap = dp->alt->vdo; 145 + 146 + dp->data.status = DP_STATUS_ENABLED; 147 + 148 + /* 149 + * If pin assignement D is supported, claiming always 150 + * that Multi-function is preferred. 151 + */ 152 + if (DP_CAP_CAPABILITY(cap) & DP_CAP_UFP_D) { 153 + dp->data.status |= DP_STATUS_CON_UFP_D; 154 + 155 + if (DP_CAP_UFP_D_PIN_ASSIGN(cap) & BIT(DP_PIN_ASSIGN_D)) 156 + dp->data.status |= DP_STATUS_PREFER_MULTI_FUNC; 157 + } else { 158 + dp->data.status |= DP_STATUS_CON_DFP_D; 159 + 160 + if (DP_CAP_DFP_D_PIN_ASSIGN(cap) & BIT(DP_PIN_ASSIGN_D)) 161 + dp->data.status |= DP_STATUS_PREFER_MULTI_FUNC; 162 + } 163 + 164 + dp->vdo_data = &dp->data.status; 165 + dp->vdo_size = 2; 166 + 167 + return 0; 168 + } 169 + 170 + static int ucsi_displayport_configure(struct ucsi_dp *dp) 171 + { 172 + u32 pins = DP_CONF_GET_PIN_ASSIGN(dp->data.conf); 173 + struct ucsi_control ctrl; 174 + 175 + if (!dp->override) 176 + return 0; 177 + 178 + ctrl.raw_cmd = UCSI_CMD_SET_NEW_CAM(dp->con->num, 1, dp->offset, pins); 179 + 180 + return ucsi_send_command(dp->con->ucsi, &ctrl, NULL, 0); 181 + } 182 + 183 + static int ucsi_displayport_vdm(struct typec_altmode *alt, 184 + u32 header, const u32 *data, int count) 185 + { 186 + struct ucsi_dp *dp = typec_altmode_get_drvdata(alt); 187 + int cmd_type = PD_VDO_CMDT(header); 188 + int cmd = PD_VDO_CMD(header); 189 + 190 + mutex_lock(&dp->con->lock); 191 + 192 + if (!dp->override && dp->initialized) { 193 + const struct typec_altmode *p = typec_altmode_get_partner(alt); 194 + 195 + dev_warn(&p->dev, 196 + "firmware doesn't support alternate mode overriding\n"); 197 + mutex_unlock(&dp->con->lock); 198 + return -EOPNOTSUPP; 199 + } 200 + 201 + switch (cmd_type) { 202 + case CMDT_INIT: 203 + dp->header = VDO(USB_TYPEC_DP_SID, 1, cmd); 204 + dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE); 205 + 206 + switch (cmd) { 207 + case DP_CMD_STATUS_UPDATE: 208 + if (ucsi_displayport_status_update(dp)) 209 + dp->header |= VDO_CMDT(CMDT_RSP_NAK); 210 + else 211 + dp->header |= VDO_CMDT(CMDT_RSP_ACK); 212 + break; 213 + case DP_CMD_CONFIGURE: 214 + dp->data.conf = *data; 215 + if (ucsi_displayport_configure(dp)) { 216 + dp->header |= VDO_CMDT(CMDT_RSP_NAK); 217 + } else { 218 + dp->header |= VDO_CMDT(CMDT_RSP_ACK); 219 + if (dp->initialized) 220 + ucsi_altmode_update_active(dp->con); 221 + else 222 + dp->initialized = true; 223 + } 224 + break; 225 + default: 226 + dp->header |= VDO_CMDT(CMDT_RSP_ACK); 227 + break; 228 + } 229 + 230 + schedule_work(&dp->work); 231 + break; 232 + default: 233 + break; 234 + } 235 + 236 + mutex_unlock(&dp->con->lock); 237 + 238 + return 0; 239 + } 240 + 241 + static const struct typec_altmode_ops ucsi_displayport_ops = { 242 + .enter = ucsi_displayport_enter, 243 + .exit = ucsi_displayport_exit, 244 + .vdm = ucsi_displayport_vdm, 245 + }; 246 + 247 + static void ucsi_displayport_work(struct work_struct *work) 248 + { 249 + struct ucsi_dp *dp = container_of(work, struct ucsi_dp, work); 250 + int ret; 251 + 252 + mutex_lock(&dp->con->lock); 253 + 254 + ret = typec_altmode_vdm(dp->alt, dp->header, 255 + dp->vdo_data, dp->vdo_size); 256 + if (ret) 257 + dev_err(&dp->alt->dev, "VDM 0x%x failed\n", dp->header); 258 + 259 + dp->vdo_data = NULL; 260 + dp->vdo_size = 0; 261 + dp->header = 0; 262 + 263 + mutex_unlock(&dp->con->lock); 264 + } 265 + 266 + void ucsi_displayport_remove_partner(struct typec_altmode *alt) 267 + { 268 + struct ucsi_dp *dp; 269 + 270 + if (!alt) 271 + return; 272 + 273 + dp = typec_altmode_get_drvdata(alt); 274 + dp->data.conf = 0; 275 + dp->data.status = 0; 276 + dp->initialized = false; 277 + } 278 + 279 + struct typec_altmode *ucsi_register_displayport(struct ucsi_connector *con, 280 + bool override, int offset, 281 + struct typec_altmode_desc *desc) 282 + { 283 + u8 all_assignments = BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D) | 284 + BIT(DP_PIN_ASSIGN_E); 285 + struct typec_altmode *alt; 286 + struct ucsi_dp *dp; 287 + 288 + /* We can't rely on the firmware with the capabilities. */ 289 + desc->vdo |= DP_CAP_DP_SIGNALING | DP_CAP_RECEPTACLE; 290 + 291 + /* Claiming that we support all pin assignments */ 292 + desc->vdo |= all_assignments << 8; 293 + desc->vdo |= all_assignments << 16; 294 + 295 + alt = typec_port_register_altmode(con->port, desc); 296 + if (IS_ERR(alt)) 297 + return alt; 298 + 299 + dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL); 300 + if (!dp) { 301 + typec_unregister_altmode(alt); 302 + return ERR_PTR(-ENOMEM); 303 + } 304 + 305 + INIT_WORK(&dp->work, ucsi_displayport_work); 306 + dp->override = override; 307 + dp->offset = offset; 308 + dp->con = con; 309 + dp->alt = alt; 310 + 311 + alt->ops = &ucsi_displayport_ops; 312 + typec_altmode_set_drvdata(alt, dp); 313 + 314 + return alt; 315 + }
+19 -2
drivers/usb/typec/ucsi/ucsi.c
··· 12 12 #include <linux/module.h> 13 13 #include <linux/delay.h> 14 14 #include <linux/slab.h> 15 - #include <linux/usb/typec_altmode.h> 15 + #include <linux/usb/typec_dp.h> 16 16 17 17 #include "ucsi.h" 18 18 #include "trace.h" ··· 264 264 u8 recipient) 265 265 { 266 266 struct typec_altmode *alt; 267 + bool override; 267 268 int ret; 268 269 int i; 270 + 271 + override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE); 269 272 270 273 switch (recipient) { 271 274 case UCSI_RECIPIENT_CON: ··· 281 278 desc->mode = ucsi_altmode_next_mode(con->port_altmode, 282 279 desc->svid); 283 280 284 - alt = typec_port_register_altmode(con->port, desc); 281 + switch (desc->svid) { 282 + case USB_TYPEC_DP_SID: 283 + alt = ucsi_register_displayport(con, override, i, desc); 284 + break; 285 + default: 286 + alt = typec_port_register_altmode(con->port, desc); 287 + break; 288 + } 289 + 285 290 if (IS_ERR(alt)) { 286 291 ret = PTR_ERR(alt); 287 292 goto err; ··· 387 376 388 377 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient) 389 378 { 379 + const struct typec_altmode *pdev; 390 380 struct typec_altmode **adev; 391 381 int i = 0; 392 382 ··· 403 391 } 404 392 405 393 while (adev[i]) { 394 + if (recipient == UCSI_RECIPIENT_SOP && 395 + adev[i]->svid == USB_TYPEC_DP_SID) { 396 + pdev = typec_altmode_get_partner(adev[i]); 397 + ucsi_displayport_remove_partner((void *)pdev); 398 + } 406 399 typec_unregister_altmode(adev[i]); 407 400 adev[i++] = NULL; 408 401 }
+21
drivers/usb/typec/ucsi/ucsi.h
··· 431 431 432 432 void ucsi_altmode_update_active(struct ucsi_connector *con); 433 433 434 + #if IS_ENABLED(CONFIG_TYPEC_DP_ALTMODE) 435 + struct typec_altmode * 436 + ucsi_register_displayport(struct ucsi_connector *con, 437 + bool override, int offset, 438 + struct typec_altmode_desc *desc); 439 + 440 + void ucsi_displayport_remove_partner(struct typec_altmode *adev); 441 + 442 + #else 443 + static inline struct typec_altmode * 444 + ucsi_register_displayport(struct ucsi_connector *con, 445 + bool override, int offset, 446 + struct typec_altmode_desc *desc) 447 + { 448 + return NULL; 449 + } 450 + 451 + static inline void 452 + ucsi_displayport_remove_partner(struct typec_altmode *adev) { } 453 + #endif /* CONFIG_TYPEC_DP_ALTMODE */ 454 + 434 455 #endif /* __DRIVER_USB_TYPEC_UCSI_H */