1 // SPDX-License-Identifier: GPL-2.0
3 * UCSI DisplayPort Alternate Mode Support
5 * Copyright (C) 2018, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/usb/typec_dp.h>
10 #include <linux/usb/pd_vdo.h>
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))
19 struct typec_displayport_data data;
20 struct ucsi_connector *con;
21 struct typec_altmode *alt;
22 struct work_struct work;
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.
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.
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
48 static int ucsi_displayport_enter(struct typec_altmode *alt, u32 *vdo)
50 struct ucsi_dp *dp = typec_altmode_get_drvdata(alt);
51 struct ucsi *ucsi = dp->con->ucsi;
57 mutex_lock(&dp->con->lock);
59 if (!dp->override && dp->initialized) {
60 const struct typec_altmode *p = typec_altmode_get_partner(alt);
63 "firmware doesn't support alternate mode overriding\n");
68 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(dp->con->num);
69 ret = ucsi_send_command(ucsi, command, &cur, sizeof(cur));
71 if (ucsi->version > 0x0100)
77 ret = dp->con->port_altmode[cur] == alt ? 0 : -EBUSY;
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.
87 svdm_version = typec_altmode_get_svdm_version(alt);
88 if (svdm_version < 0) {
93 dp->header = VDO(USB_TYPEC_DP_SID, 1, svdm_version, CMD_ENTER_MODE);
94 dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE);
95 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
100 schedule_work(&dp->work);
103 mutex_unlock(&dp->con->lock);
108 static int ucsi_displayport_exit(struct typec_altmode *alt)
110 struct ucsi_dp *dp = typec_altmode_get_drvdata(alt);
115 mutex_lock(&dp->con->lock);
118 const struct typec_altmode *p = typec_altmode_get_partner(alt);
121 "firmware doesn't support alternate mode overriding\n");
126 command = UCSI_CMD_SET_NEW_CAM(dp->con->num, 0, dp->offset, 0);
127 ret = ucsi_send_command(dp->con->ucsi, command, NULL, 0);
131 svdm_version = typec_altmode_get_svdm_version(alt);
132 if (svdm_version < 0) {
137 dp->header = VDO(USB_TYPEC_DP_SID, 1, svdm_version, CMD_EXIT_MODE);
138 dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE);
139 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
144 schedule_work(&dp->work);
147 mutex_unlock(&dp->con->lock);
153 * We do not actually have access to the Status Update VDO, so we have to guess
156 static int ucsi_displayport_status_update(struct ucsi_dp *dp)
158 u32 cap = dp->alt->vdo;
160 dp->data.status = DP_STATUS_ENABLED;
163 * If pin assignement D is supported, claiming always
164 * that Multi-function is preferred.
166 if (DP_CAP_CAPABILITY(cap) & DP_CAP_UFP_D) {
167 dp->data.status |= DP_STATUS_CON_UFP_D;
169 if (DP_CAP_UFP_D_PIN_ASSIGN(cap) & BIT(DP_PIN_ASSIGN_D))
170 dp->data.status |= DP_STATUS_PREFER_MULTI_FUNC;
172 dp->data.status |= DP_STATUS_CON_DFP_D;
174 if (DP_CAP_DFP_D_PIN_ASSIGN(cap) & BIT(DP_PIN_ASSIGN_D))
175 dp->data.status |= DP_STATUS_PREFER_MULTI_FUNC;
178 dp->vdo_data = &dp->data.status;
184 static int ucsi_displayport_configure(struct ucsi_dp *dp)
186 u32 pins = DP_CONF_GET_PIN_ASSIGN(dp->data.conf);
192 command = UCSI_CMD_SET_NEW_CAM(dp->con->num, 1, dp->offset, pins);
194 return ucsi_send_command(dp->con->ucsi, command, NULL, 0);
197 static int ucsi_displayport_vdm(struct typec_altmode *alt,
198 u32 header, const u32 *data, int count)
200 struct ucsi_dp *dp = typec_altmode_get_drvdata(alt);
201 int cmd_type = PD_VDO_CMDT(header);
202 int cmd = PD_VDO_CMD(header);
205 mutex_lock(&dp->con->lock);
207 if (!dp->override && dp->initialized) {
208 const struct typec_altmode *p = typec_altmode_get_partner(alt);
211 "firmware doesn't support alternate mode overriding\n");
212 mutex_unlock(&dp->con->lock);
216 svdm_version = typec_altmode_get_svdm_version(alt);
217 if (svdm_version < 0) {
218 mutex_unlock(&dp->con->lock);
224 if (PD_VDO_SVDM_VER(header) < svdm_version) {
225 typec_partner_set_svdm_version(dp->con->partner, PD_VDO_SVDM_VER(header));
226 svdm_version = PD_VDO_SVDM_VER(header);
229 dp->header = VDO(USB_TYPEC_DP_SID, 1, svdm_version, cmd);
230 dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE);
233 case DP_CMD_STATUS_UPDATE:
234 if (ucsi_displayport_status_update(dp))
235 dp->header |= VDO_CMDT(CMDT_RSP_NAK);
237 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
239 case DP_CMD_CONFIGURE:
240 dp->data.conf = *data;
241 if (ucsi_displayport_configure(dp)) {
242 dp->header |= VDO_CMDT(CMDT_RSP_NAK);
244 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
246 ucsi_altmode_update_active(dp->con);
248 dp->initialized = true;
252 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
256 schedule_work(&dp->work);
262 mutex_unlock(&dp->con->lock);
267 static const struct typec_altmode_ops ucsi_displayport_ops = {
268 .enter = ucsi_displayport_enter,
269 .exit = ucsi_displayport_exit,
270 .vdm = ucsi_displayport_vdm,
273 static void ucsi_displayport_work(struct work_struct *work)
275 struct ucsi_dp *dp = container_of(work, struct ucsi_dp, work);
278 mutex_lock(&dp->con->lock);
280 ret = typec_altmode_vdm(dp->alt, dp->header,
281 dp->vdo_data, dp->vdo_size);
283 dev_err(&dp->alt->dev, "VDM 0x%x failed\n", dp->header);
289 mutex_unlock(&dp->con->lock);
292 void ucsi_displayport_remove_partner(struct typec_altmode *alt)
299 dp = typec_altmode_get_drvdata(alt);
305 dp->initialized = false;
308 struct typec_altmode *ucsi_register_displayport(struct ucsi_connector *con,
309 bool override, int offset,
310 struct typec_altmode_desc *desc)
312 u8 all_assignments = BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D) |
313 BIT(DP_PIN_ASSIGN_E);
314 struct typec_altmode *alt;
317 /* We can't rely on the firmware with the capabilities. */
318 desc->vdo |= DP_CAP_DP_SIGNALING | DP_CAP_RECEPTACLE;
320 /* Claiming that we support all pin assignments */
321 desc->vdo |= all_assignments << 8;
322 desc->vdo |= all_assignments << 16;
324 alt = typec_port_register_altmode(con->port, desc);
328 dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
330 typec_unregister_altmode(alt);
331 return ERR_PTR(-ENOMEM);
334 INIT_WORK(&dp->work, ucsi_displayport_work);
335 dp->override = override;
340 alt->ops = &ucsi_displayport_ops;
341 typec_altmode_set_drvdata(alt, dp);