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;
56 mutex_lock(&dp->con->lock);
58 if (!dp->override && dp->initialized) {
59 const struct typec_altmode *p = typec_altmode_get_partner(alt);
62 "firmware doesn't support alternate mode overriding\n");
67 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(dp->con->num);
68 ret = ucsi_send_command(ucsi, command, &cur, sizeof(cur));
70 if (ucsi->version > 0x0100)
76 ret = dp->con->port_altmode[cur] == alt ? 0 : -EBUSY;
81 * We can't send the New CAM command yet to the PPM as it needs the
82 * configuration value as well. Pretending that we have now entered the
83 * mode, and letting the alt mode driver continue.
86 dp->header = VDO(USB_TYPEC_DP_SID, 1, CMD_ENTER_MODE);
87 dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE);
88 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
93 schedule_work(&dp->work);
96 mutex_unlock(&dp->con->lock);
101 static int ucsi_displayport_exit(struct typec_altmode *alt)
103 struct ucsi_dp *dp = typec_altmode_get_drvdata(alt);
107 mutex_lock(&dp->con->lock);
110 const struct typec_altmode *p = typec_altmode_get_partner(alt);
113 "firmware doesn't support alternate mode overriding\n");
118 command = UCSI_CMD_SET_NEW_CAM(dp->con->num, 0, dp->offset, 0);
119 ret = ucsi_send_command(dp->con->ucsi, command, NULL, 0);
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);
130 schedule_work(&dp->work);
133 mutex_unlock(&dp->con->lock);
139 * We do not actually have access to the Status Update VDO, so we have to guess
142 static int ucsi_displayport_status_update(struct ucsi_dp *dp)
144 u32 cap = dp->alt->vdo;
146 dp->data.status = DP_STATUS_ENABLED;
149 * If pin assignement D is supported, claiming always
150 * that Multi-function is preferred.
152 if (DP_CAP_CAPABILITY(cap) & DP_CAP_UFP_D) {
153 dp->data.status |= DP_STATUS_CON_UFP_D;
155 if (DP_CAP_UFP_D_PIN_ASSIGN(cap) & BIT(DP_PIN_ASSIGN_D))
156 dp->data.status |= DP_STATUS_PREFER_MULTI_FUNC;
158 dp->data.status |= DP_STATUS_CON_DFP_D;
160 if (DP_CAP_DFP_D_PIN_ASSIGN(cap) & BIT(DP_PIN_ASSIGN_D))
161 dp->data.status |= DP_STATUS_PREFER_MULTI_FUNC;
164 dp->vdo_data = &dp->data.status;
170 static int ucsi_displayport_configure(struct ucsi_dp *dp)
172 u32 pins = DP_CONF_GET_PIN_ASSIGN(dp->data.conf);
178 command = UCSI_CMD_SET_NEW_CAM(dp->con->num, 1, dp->offset, pins);
180 return ucsi_send_command(dp->con->ucsi, command, NULL, 0);
183 static int ucsi_displayport_vdm(struct typec_altmode *alt,
184 u32 header, const u32 *data, int count)
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);
190 mutex_lock(&dp->con->lock);
192 if (!dp->override && dp->initialized) {
193 const struct typec_altmode *p = typec_altmode_get_partner(alt);
196 "firmware doesn't support alternate mode overriding\n");
197 mutex_unlock(&dp->con->lock);
203 dp->header = VDO(USB_TYPEC_DP_SID, 1, cmd);
204 dp->header |= VDO_OPOS(USB_TYPEC_DP_MODE);
207 case DP_CMD_STATUS_UPDATE:
208 if (ucsi_displayport_status_update(dp))
209 dp->header |= VDO_CMDT(CMDT_RSP_NAK);
211 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
213 case DP_CMD_CONFIGURE:
214 dp->data.conf = *data;
215 if (ucsi_displayport_configure(dp)) {
216 dp->header |= VDO_CMDT(CMDT_RSP_NAK);
218 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
220 ucsi_altmode_update_active(dp->con);
222 dp->initialized = true;
226 dp->header |= VDO_CMDT(CMDT_RSP_ACK);
230 schedule_work(&dp->work);
236 mutex_unlock(&dp->con->lock);
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,
247 static void ucsi_displayport_work(struct work_struct *work)
249 struct ucsi_dp *dp = container_of(work, struct ucsi_dp, work);
252 mutex_lock(&dp->con->lock);
254 ret = typec_altmode_vdm(dp->alt, dp->header,
255 dp->vdo_data, dp->vdo_size);
257 dev_err(&dp->alt->dev, "VDM 0x%x failed\n", dp->header);
263 mutex_unlock(&dp->con->lock);
266 void ucsi_displayport_remove_partner(struct typec_altmode *alt)
273 dp = typec_altmode_get_drvdata(alt);
279 dp->initialized = false;
282 struct typec_altmode *ucsi_register_displayport(struct ucsi_connector *con,
283 bool override, int offset,
284 struct typec_altmode_desc *desc)
286 u8 all_assignments = BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D) |
287 BIT(DP_PIN_ASSIGN_E);
288 struct typec_altmode *alt;
291 /* We can't rely on the firmware with the capabilities. */
292 desc->vdo |= DP_CAP_DP_SIGNALING | DP_CAP_RECEPTACLE;
294 /* Claiming that we support all pin assignments */
295 desc->vdo |= all_assignments << 8;
296 desc->vdo |= all_assignments << 16;
298 alt = typec_port_register_altmode(con->port, desc);
302 dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
304 typec_unregister_altmode(alt);
305 return ERR_PTR(-ENOMEM);
308 INIT_WORK(&dp->work, ucsi_displayport_work);
309 dp->override = override;
314 alt->ops = &ucsi_displayport_ops;
315 typec_altmode_set_drvdata(alt, dp);