Skip to content

Commit af8622f

Browse files
Heikki Krogerusgregkh
authored andcommitted
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 <[email protected]> Signed-off-by: Heikki Krogerus <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent ad74b86 commit af8622f

File tree

4 files changed

+364
-8
lines changed

4 files changed

+364
-8
lines changed

drivers/usb/typec/ucsi/Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
CFLAGS_trace.o := -I$(src)
2+
CFLAGS_trace.o := -I$(src)
33

4-
obj-$(CONFIG_TYPEC_UCSI) += typec_ucsi.o
4+
obj-$(CONFIG_TYPEC_UCSI) += typec_ucsi.o
55

6-
typec_ucsi-y := ucsi.o
6+
typec_ucsi-y := ucsi.o
77

8-
typec_ucsi-$(CONFIG_TRACING) += trace.o
8+
typec_ucsi-$(CONFIG_TRACING) += trace.o
99

10-
obj-$(CONFIG_UCSI_ACPI) += ucsi_acpi.o
10+
ifneq ($(CONFIG_TYPEC_DP_ALTMODE),)
11+
typec_ucsi-y += displayport.o
12+
endif
1113

12-
obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o
14+
obj-$(CONFIG_UCSI_ACPI) += ucsi_acpi.o
15+
obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o

drivers/usb/typec/ucsi/displayport.c

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
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 <[email protected]>
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+
}

drivers/usb/typec/ucsi/ucsi.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <linux/module.h>
1313
#include <linux/delay.h>
1414
#include <linux/slab.h>
15-
#include <linux/usb/typec_altmode.h>
15+
#include <linux/usb/typec_dp.h>
1616

1717
#include "ucsi.h"
1818
#include "trace.h"
@@ -264,9 +264,12 @@ static int ucsi_register_altmode(struct ucsi_connector *con,
264264
u8 recipient)
265265
{
266266
struct typec_altmode *alt;
267+
bool override;
267268
int ret;
268269
int i;
269270

271+
override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
272+
270273
switch (recipient) {
271274
case UCSI_RECIPIENT_CON:
272275
i = ucsi_next_altmode(con->port_altmode);
@@ -278,7 +281,15 @@ static int ucsi_register_altmode(struct ucsi_connector *con,
278281
desc->mode = ucsi_altmode_next_mode(con->port_altmode,
279282
desc->svid);
280283

281-
alt = typec_port_register_altmode(con->port, desc);
284+
switch (desc->svid) {
285+
case USB_TYPEC_DP_SID:
286+
alt = ucsi_register_displayport(con, override, i, desc);
287+
break;
288+
default:
289+
alt = typec_port_register_altmode(con->port, desc);
290+
break;
291+
}
292+
282293
if (IS_ERR(alt)) {
283294
ret = PTR_ERR(alt);
284295
goto err;
@@ -376,6 +387,7 @@ static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
376387

377388
static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
378389
{
390+
const struct typec_altmode *pdev;
379391
struct typec_altmode **adev;
380392
int i = 0;
381393

@@ -391,6 +403,11 @@ static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
391403
}
392404

393405
while (adev[i]) {
406+
if (recipient == UCSI_RECIPIENT_SOP &&
407+
adev[i]->svid == USB_TYPEC_DP_SID) {
408+
pdev = typec_altmode_get_partner(adev[i]);
409+
ucsi_displayport_remove_partner((void *)pdev);
410+
}
394411
typec_unregister_altmode(adev[i]);
395412
adev[i++] = NULL;
396413
}

0 commit comments

Comments
 (0)