]>
Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
c1b0bc2d HK |
2 | /* |
3 | * USB Type-C Connector System Software Interface driver | |
4 | * | |
5 | * Copyright (C) 2017, Intel Corporation | |
6 | * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> | |
c1b0bc2d HK |
7 | */ |
8 | ||
9 | #include <linux/completion.h> | |
10 | #include <linux/property.h> | |
11 | #include <linux/device.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/delay.h> | |
14 | #include <linux/slab.h> | |
af8622f6 | 15 | #include <linux/usb/typec_dp.h> |
c1b0bc2d HK |
16 | |
17 | #include "ucsi.h" | |
18 | #include "trace.h" | |
19 | ||
c1b0bc2d HK |
20 | /* |
21 | * UCSI_TIMEOUT_MS - PPM communication timeout | |
22 | * | |
23 | * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI | |
24 | * specification) here as reference, but unfortunately we can't. It is very | |
25 | * difficult to estimate the time it takes for the system to process the command | |
26 | * before it is actually passed to the PPM. | |
27 | */ | |
b1b59e16 | 28 | #define UCSI_TIMEOUT_MS 5000 |
c1b0bc2d HK |
29 | |
30 | /* | |
31 | * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests | |
32 | * | |
33 | * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even | |
34 | * if the PPM does not generate Connector Change events before that with | |
35 | * partners that do not support USB Power Delivery, this should still work. | |
36 | */ | |
37 | #define UCSI_SWAP_TIMEOUT_MS 5000 | |
38 | ||
bdc62f2b HK |
39 | static int ucsi_acknowledge_command(struct ucsi *ucsi) |
40 | { | |
41 | u64 ctrl; | |
42 | ||
43 | ctrl = UCSI_ACK_CC_CI; | |
44 | ctrl |= UCSI_ACK_COMMAND_COMPLETE; | |
45 | ||
46 | return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl)); | |
47 | } | |
48 | ||
49 | static int ucsi_acknowledge_connector_change(struct ucsi *ucsi) | |
50 | { | |
51 | u64 ctrl; | |
52 | ||
53 | ctrl = UCSI_ACK_CC_CI; | |
54 | ctrl |= UCSI_ACK_CONNECTOR_CHANGE; | |
55 | ||
217504a0 | 56 | return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl)); |
bdc62f2b HK |
57 | } |
58 | ||
59 | static int ucsi_exec_command(struct ucsi *ucsi, u64 command); | |
60 | ||
61 | static int ucsi_read_error(struct ucsi *ucsi) | |
62 | { | |
63 | u16 error; | |
64 | int ret; | |
65 | ||
66 | /* Acknowlege the command that failed */ | |
67 | ret = ucsi_acknowledge_command(ucsi); | |
68 | if (ret) | |
69 | return ret; | |
70 | ||
71 | ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS); | |
72 | if (ret < 0) | |
73 | return ret; | |
74 | ||
75 | ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error)); | |
76 | if (ret) | |
77 | return ret; | |
78 | ||
79 | switch (error) { | |
80 | case UCSI_ERROR_INCOMPATIBLE_PARTNER: | |
81 | return -EOPNOTSUPP; | |
82 | case UCSI_ERROR_CC_COMMUNICATION_ERR: | |
83 | return -ECOMM; | |
84 | case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL: | |
85 | return -EPROTO; | |
86 | case UCSI_ERROR_DEAD_BATTERY: | |
87 | dev_warn(ucsi->dev, "Dead battery condition!\n"); | |
88 | return -EPERM; | |
bdc62f2b HK |
89 | case UCSI_ERROR_INVALID_CON_NUM: |
90 | case UCSI_ERROR_UNREGONIZED_CMD: | |
91 | case UCSI_ERROR_INVALID_CMD_ARGUMENT: | |
e716bb38 | 92 | dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error); |
bdc62f2b | 93 | return -EINVAL; |
e716bb38 HK |
94 | case UCSI_ERROR_OVERCURRENT: |
95 | dev_warn(ucsi->dev, "Overcurrent condition\n"); | |
96 | break; | |
97 | case UCSI_ERROR_PARTNER_REJECTED_SWAP: | |
98 | dev_warn(ucsi->dev, "Partner rejected swap\n"); | |
99 | break; | |
100 | case UCSI_ERROR_HARD_RESET: | |
101 | dev_warn(ucsi->dev, "Hard reset occurred\n"); | |
102 | break; | |
103 | case UCSI_ERROR_PPM_POLICY_CONFLICT: | |
104 | dev_warn(ucsi->dev, "PPM Policy conflict\n"); | |
105 | break; | |
106 | case UCSI_ERROR_SWAP_REJECTED: | |
107 | dev_warn(ucsi->dev, "Swap rejected\n"); | |
108 | break; | |
109 | case UCSI_ERROR_UNDEFINED: | |
bdc62f2b | 110 | default: |
e716bb38 HK |
111 | dev_err(ucsi->dev, "unknown error %u\n", error); |
112 | break; | |
bdc62f2b HK |
113 | } |
114 | ||
e716bb38 | 115 | return -EIO; |
bdc62f2b HK |
116 | } |
117 | ||
118 | static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) | |
119 | { | |
120 | u32 cci; | |
121 | int ret; | |
122 | ||
123 | ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); | |
124 | if (ret) | |
125 | return ret; | |
126 | ||
127 | ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); | |
128 | if (ret) | |
129 | return ret; | |
130 | ||
131 | if (cci & UCSI_CCI_BUSY) | |
132 | return -EBUSY; | |
133 | ||
134 | if (!(cci & UCSI_CCI_COMMAND_COMPLETE)) | |
135 | return -EIO; | |
136 | ||
137 | if (cci & UCSI_CCI_NOT_SUPPORTED) | |
138 | return -EOPNOTSUPP; | |
139 | ||
140 | if (cci & UCSI_CCI_ERROR) { | |
141 | if (cmd == UCSI_GET_ERROR_STATUS) | |
142 | return -EIO; | |
143 | return ucsi_read_error(ucsi); | |
144 | } | |
145 | ||
146 | return UCSI_CCI_LENGTH(cci); | |
147 | } | |
148 | ||
25794e30 HG |
149 | int ucsi_send_command(struct ucsi *ucsi, u64 command, |
150 | void *data, size_t size) | |
c1b0bc2d | 151 | { |
2ede5546 | 152 | u8 length; |
c1b0bc2d HK |
153 | int ret; |
154 | ||
25794e30 | 155 | mutex_lock(&ucsi->ppm_lock); |
7e90057f | 156 | |
470ce43a | 157 | ret = ucsi_exec_command(ucsi, command); |
2ede5546 | 158 | if (ret < 0) |
25794e30 | 159 | goto out; |
bdc62f2b | 160 | |
2ede5546 | 161 | length = ret; |
bdc62f2b | 162 | |
2ede5546 HK |
163 | if (data) { |
164 | ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size); | |
bdc62f2b | 165 | if (ret) |
25794e30 | 166 | goto out; |
bdc62f2b HK |
167 | } |
168 | ||
2ede5546 | 169 | ret = ucsi_acknowledge_command(ucsi); |
c1b0bc2d | 170 | if (ret) |
25794e30 | 171 | goto out; |
c1b0bc2d | 172 | |
25794e30 HG |
173 | ret = length; |
174 | out: | |
ad74b864 | 175 | mutex_unlock(&ucsi->ppm_lock); |
ad74b864 HK |
176 | return ret; |
177 | } | |
a94ecde4 | 178 | EXPORT_SYMBOL_GPL(ucsi_send_command); |
ad74b864 | 179 | |
a94ecde4 AG |
180 | int ucsi_resume(struct ucsi *ucsi) |
181 | { | |
470ce43a | 182 | u64 command; |
a94ecde4 AG |
183 | |
184 | /* Restore UCSI notification enable mask after system resume */ | |
71a1fa0d | 185 | command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; |
470ce43a HK |
186 | |
187 | return ucsi_send_command(ucsi, command, NULL, 0); | |
a94ecde4 AG |
188 | } |
189 | EXPORT_SYMBOL_GPL(ucsi_resume); | |
c1b0bc2d HK |
190 | /* -------------------------------------------------------------------------- */ |
191 | ||
ad74b864 HK |
192 | void ucsi_altmode_update_active(struct ucsi_connector *con) |
193 | { | |
194 | const struct typec_altmode *altmode = NULL; | |
2ede5546 | 195 | u64 command; |
ad74b864 HK |
196 | int ret; |
197 | u8 cur; | |
198 | int i; | |
199 | ||
470ce43a | 200 | command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num); |
0ff0705a | 201 | ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur)); |
ad74b864 | 202 | if (ret < 0) { |
2ede5546 | 203 | if (con->ucsi->version > 0x0100) { |
ad74b864 HK |
204 | dev_err(con->ucsi->dev, |
205 | "GET_CURRENT_CAM command failed\n"); | |
206 | return; | |
207 | } | |
208 | cur = 0xff; | |
209 | } | |
210 | ||
211 | if (cur < UCSI_MAX_ALTMODES) | |
212 | altmode = typec_altmode_get_partner(con->port_altmode[cur]); | |
213 | ||
214 | for (i = 0; con->partner_altmode[i]; i++) | |
215 | typec_altmode_update_active(con->partner_altmode[i], | |
216 | con->partner_altmode[i] == altmode); | |
217 | } | |
218 | ||
386e15a6 | 219 | static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid) |
ad74b864 HK |
220 | { |
221 | u8 mode = 1; | |
222 | int i; | |
223 | ||
386e15a6 HK |
224 | for (i = 0; alt[i]; i++) { |
225 | if (i > MODE_DISCOVERY_MAX) | |
226 | return -ERANGE; | |
227 | ||
ad74b864 HK |
228 | if (alt[i]->svid == svid) |
229 | mode++; | |
386e15a6 | 230 | } |
ad74b864 HK |
231 | |
232 | return mode; | |
233 | } | |
234 | ||
235 | static int ucsi_next_altmode(struct typec_altmode **alt) | |
236 | { | |
237 | int i = 0; | |
238 | ||
239 | for (i = 0; i < UCSI_MAX_ALTMODES; i++) | |
240 | if (!alt[i]) | |
241 | return i; | |
242 | ||
243 | return -ENOENT; | |
244 | } | |
245 | ||
246 | static int ucsi_register_altmode(struct ucsi_connector *con, | |
247 | struct typec_altmode_desc *desc, | |
248 | u8 recipient) | |
249 | { | |
250 | struct typec_altmode *alt; | |
af8622f6 | 251 | bool override; |
ad74b864 HK |
252 | int ret; |
253 | int i; | |
254 | ||
af8622f6 HK |
255 | override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE); |
256 | ||
ad74b864 HK |
257 | switch (recipient) { |
258 | case UCSI_RECIPIENT_CON: | |
259 | i = ucsi_next_altmode(con->port_altmode); | |
260 | if (i < 0) { | |
261 | ret = i; | |
262 | goto err; | |
263 | } | |
264 | ||
386e15a6 HK |
265 | ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid); |
266 | if (ret < 0) | |
267 | return ret; | |
268 | ||
269 | desc->mode = ret; | |
ad74b864 | 270 | |
af8622f6 HK |
271 | switch (desc->svid) { |
272 | case USB_TYPEC_DP_SID: | |
273 | alt = ucsi_register_displayport(con, override, i, desc); | |
274 | break; | |
c2ae4928 AG |
275 | case USB_TYPEC_NVIDIA_VLINK_SID: |
276 | if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO) | |
277 | alt = typec_port_register_altmode(con->port, | |
278 | desc); | |
279 | else | |
280 | alt = ucsi_register_displayport(con, override, | |
281 | i, desc); | |
282 | break; | |
af8622f6 HK |
283 | default: |
284 | alt = typec_port_register_altmode(con->port, desc); | |
285 | break; | |
286 | } | |
287 | ||
ad74b864 HK |
288 | if (IS_ERR(alt)) { |
289 | ret = PTR_ERR(alt); | |
290 | goto err; | |
291 | } | |
292 | ||
293 | con->port_altmode[i] = alt; | |
294 | break; | |
295 | case UCSI_RECIPIENT_SOP: | |
296 | i = ucsi_next_altmode(con->partner_altmode); | |
297 | if (i < 0) { | |
298 | ret = i; | |
299 | goto err; | |
300 | } | |
301 | ||
386e15a6 HK |
302 | ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid); |
303 | if (ret < 0) | |
304 | return ret; | |
305 | ||
306 | desc->mode = ret; | |
ad74b864 HK |
307 | |
308 | alt = typec_partner_register_altmode(con->partner, desc); | |
309 | if (IS_ERR(alt)) { | |
310 | ret = PTR_ERR(alt); | |
311 | goto err; | |
312 | } | |
313 | ||
314 | con->partner_altmode[i] = alt; | |
315 | break; | |
316 | default: | |
317 | return -EINVAL; | |
318 | } | |
319 | ||
320 | trace_ucsi_register_altmode(recipient, alt); | |
321 | ||
322 | return 0; | |
323 | ||
324 | err: | |
325 | dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n", | |
326 | desc->svid, desc->mode); | |
327 | ||
328 | return ret; | |
329 | } | |
330 | ||
170a6726 AG |
331 | static int |
332 | ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient) | |
333 | { | |
334 | int max_altmodes = UCSI_MAX_ALTMODES; | |
335 | struct typec_altmode_desc desc; | |
336 | struct ucsi_altmode alt; | |
337 | struct ucsi_altmode orig[UCSI_MAX_ALTMODES]; | |
338 | struct ucsi_altmode updated[UCSI_MAX_ALTMODES]; | |
339 | struct ucsi *ucsi = con->ucsi; | |
340 | bool multi_dp = false; | |
341 | u64 command; | |
342 | int ret; | |
343 | int len; | |
344 | int i; | |
345 | int k = 0; | |
346 | ||
347 | if (recipient == UCSI_RECIPIENT_CON) | |
348 | max_altmodes = con->ucsi->cap.num_alt_modes; | |
349 | ||
350 | memset(orig, 0, sizeof(orig)); | |
351 | memset(updated, 0, sizeof(updated)); | |
352 | ||
353 | /* First get all the alternate modes */ | |
354 | for (i = 0; i < max_altmodes; i++) { | |
355 | memset(&alt, 0, sizeof(alt)); | |
356 | command = UCSI_GET_ALTERNATE_MODES; | |
357 | command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); | |
358 | command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); | |
359 | command |= UCSI_GET_ALTMODE_OFFSET(i); | |
0ff0705a | 360 | len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt)); |
170a6726 AG |
361 | /* |
362 | * We are collecting all altmodes first and then registering. | |
363 | * Some type-C device will return zero length data beyond last | |
364 | * alternate modes. We should not return if length is zero. | |
365 | */ | |
366 | if (len < 0) | |
367 | return len; | |
368 | ||
369 | /* We got all altmodes, now break out and register them */ | |
370 | if (!len || !alt.svid) | |
371 | break; | |
372 | ||
373 | orig[k].mid = alt.mid; | |
374 | orig[k].svid = alt.svid; | |
375 | k++; | |
376 | } | |
377 | /* | |
378 | * Update the original altmode table as some ppms may report | |
379 | * multiple DP altmodes. | |
380 | */ | |
381 | if (recipient == UCSI_RECIPIENT_CON) | |
382 | multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated); | |
383 | ||
384 | /* now register altmodes */ | |
385 | for (i = 0; i < max_altmodes; i++) { | |
386 | memset(&desc, 0, sizeof(desc)); | |
387 | if (multi_dp && recipient == UCSI_RECIPIENT_CON) { | |
388 | desc.svid = updated[i].svid; | |
389 | desc.vdo = updated[i].mid; | |
390 | } else { | |
391 | desc.svid = orig[i].svid; | |
392 | desc.vdo = orig[i].mid; | |
393 | } | |
394 | desc.roles = TYPEC_PORT_DRD; | |
395 | ||
396 | if (!desc.svid) | |
397 | return 0; | |
398 | ||
399 | ret = ucsi_register_altmode(con, &desc, recipient); | |
400 | if (ret) | |
401 | return ret; | |
402 | } | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
ad74b864 HK |
407 | static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient) |
408 | { | |
409 | int max_altmodes = UCSI_MAX_ALTMODES; | |
410 | struct typec_altmode_desc desc; | |
411 | struct ucsi_altmode alt[2]; | |
470ce43a | 412 | u64 command; |
85798543 | 413 | int num; |
ad74b864 HK |
414 | int ret; |
415 | int len; | |
416 | int j; | |
417 | int i; | |
418 | ||
419 | if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS)) | |
420 | return 0; | |
421 | ||
422 | if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0]) | |
423 | return 0; | |
424 | ||
170a6726 AG |
425 | if (con->ucsi->ops->update_altmodes) |
426 | return ucsi_register_altmodes_nvidia(con, recipient); | |
427 | ||
ad74b864 HK |
428 | if (recipient == UCSI_RECIPIENT_CON) |
429 | max_altmodes = con->ucsi->cap.num_alt_modes; | |
430 | ||
431 | for (i = 0; i < max_altmodes;) { | |
432 | memset(alt, 0, sizeof(alt)); | |
470ce43a HK |
433 | command = UCSI_GET_ALTERNATE_MODES; |
434 | command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); | |
435 | command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); | |
436 | command |= UCSI_GET_ALTMODE_OFFSET(i); | |
0ff0705a | 437 | len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt)); |
ad74b864 HK |
438 | if (len <= 0) |
439 | return len; | |
440 | ||
441 | /* | |
442 | * This code is requesting one alt mode at a time, but some PPMs | |
443 | * may still return two. If that happens both alt modes need be | |
444 | * registered and the offset for the next alt mode has to be | |
445 | * incremented. | |
446 | */ | |
447 | num = len / sizeof(alt[0]); | |
448 | i += num; | |
449 | ||
450 | for (j = 0; j < num; j++) { | |
451 | if (!alt[j].svid) | |
452 | return 0; | |
453 | ||
454 | memset(&desc, 0, sizeof(desc)); | |
455 | desc.vdo = alt[j].mid; | |
456 | desc.svid = alt[j].svid; | |
457 | desc.roles = TYPEC_PORT_DRD; | |
458 | ||
459 | ret = ucsi_register_altmode(con, &desc, recipient); | |
460 | if (ret) | |
461 | return ret; | |
462 | } | |
463 | } | |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
468 | static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient) | |
469 | { | |
af8622f6 | 470 | const struct typec_altmode *pdev; |
ad74b864 HK |
471 | struct typec_altmode **adev; |
472 | int i = 0; | |
473 | ||
474 | switch (recipient) { | |
475 | case UCSI_RECIPIENT_CON: | |
476 | adev = con->port_altmode; | |
477 | break; | |
478 | case UCSI_RECIPIENT_SOP: | |
479 | adev = con->partner_altmode; | |
480 | break; | |
481 | default: | |
482 | return; | |
483 | } | |
484 | ||
485 | while (adev[i]) { | |
af8622f6 | 486 | if (recipient == UCSI_RECIPIENT_SOP && |
cf28369c | 487 | (adev[i]->svid == USB_TYPEC_DP_SID || |
c2ae4928 AG |
488 | (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID && |
489 | adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) { | |
af8622f6 HK |
490 | pdev = typec_altmode_get_partner(adev[i]); |
491 | ucsi_displayport_remove_partner((void *)pdev); | |
492 | } | |
ad74b864 HK |
493 | typec_unregister_altmode(adev[i]); |
494 | adev[i++] = NULL; | |
495 | } | |
496 | } | |
497 | ||
dab212db JP |
498 | static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner, |
499 | u32 *pdos, int offset, int num_pdos) | |
4dbc6a4e A |
500 | { |
501 | struct ucsi *ucsi = con->ucsi; | |
502 | u64 command; | |
503 | int ret; | |
504 | ||
505 | command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num); | |
506 | command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner); | |
dab212db JP |
507 | command |= UCSI_GET_PDOS_PDO_OFFSET(offset); |
508 | command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1); | |
4dbc6a4e | 509 | command |= UCSI_GET_PDOS_SRC_PDOS; |
dab212db JP |
510 | ret = ucsi_send_command(ucsi, command, pdos + offset, |
511 | num_pdos * sizeof(u32)); | |
512 | if (ret < 0) | |
4dbc6a4e | 513 | dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret); |
dab212db JP |
514 | if (ret == 0 && offset == 0) |
515 | dev_warn(ucsi->dev, "UCSI_GET_PDOS returned 0 bytes\n"); | |
516 | ||
517 | return ret; | |
518 | } | |
519 | ||
520 | static void ucsi_get_src_pdos(struct ucsi_connector *con, int is_partner) | |
521 | { | |
522 | int ret; | |
523 | ||
524 | /* UCSI max payload means only getting at most 4 PDOs at a time */ | |
525 | ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS); | |
526 | if (ret < 0) | |
4dbc6a4e | 527 | return; |
dab212db | 528 | |
4dbc6a4e | 529 | con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */ |
dab212db JP |
530 | if (con->num_pdos < UCSI_MAX_PDOS) |
531 | return; | |
532 | ||
533 | /* get the remaining PDOs, if any */ | |
534 | ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS, | |
535 | PDO_MAX_OBJECTS - UCSI_MAX_PDOS); | |
536 | if (ret < 0) | |
537 | return; | |
538 | ||
539 | con->num_pdos += ret / sizeof(u32); | |
4dbc6a4e A |
540 | } |
541 | ||
c1b0bc2d HK |
542 | static void ucsi_pwr_opmode_change(struct ucsi_connector *con) |
543 | { | |
3cf657f0 | 544 | switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) { |
c1b0bc2d | 545 | case UCSI_CONSTAT_PWR_OPMODE_PD: |
4dbc6a4e | 546 | con->rdo = con->status.request_data_obj; |
c1b0bc2d | 547 | typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD); |
dab212db | 548 | ucsi_get_src_pdos(con, 1); |
c1b0bc2d HK |
549 | break; |
550 | case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: | |
4dbc6a4e | 551 | con->rdo = 0; |
c1b0bc2d HK |
552 | typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A); |
553 | break; | |
554 | case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: | |
4dbc6a4e | 555 | con->rdo = 0; |
c1b0bc2d HK |
556 | typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A); |
557 | break; | |
558 | default: | |
4dbc6a4e | 559 | con->rdo = 0; |
c1b0bc2d HK |
560 | typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB); |
561 | break; | |
562 | } | |
563 | } | |
564 | ||
565 | static int ucsi_register_partner(struct ucsi_connector *con) | |
566 | { | |
3cf657f0 | 567 | u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags); |
cf6e06cd HK |
568 | struct typec_partner_desc desc; |
569 | struct typec_partner *partner; | |
c1b0bc2d HK |
570 | |
571 | if (con->partner) | |
572 | return 0; | |
573 | ||
cf6e06cd | 574 | memset(&desc, 0, sizeof(desc)); |
c1b0bc2d | 575 | |
3cf657f0 | 576 | switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) { |
c1b0bc2d | 577 | case UCSI_CONSTAT_PARTNER_TYPE_DEBUG: |
cf6e06cd | 578 | desc.accessory = TYPEC_ACCESSORY_DEBUG; |
c1b0bc2d HK |
579 | break; |
580 | case UCSI_CONSTAT_PARTNER_TYPE_AUDIO: | |
cf6e06cd | 581 | desc.accessory = TYPEC_ACCESSORY_AUDIO; |
c1b0bc2d HK |
582 | break; |
583 | default: | |
584 | break; | |
585 | } | |
586 | ||
3cf657f0 | 587 | desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD; |
c1b0bc2d | 588 | |
cf6e06cd HK |
589 | partner = typec_register_partner(con->port, &desc); |
590 | if (IS_ERR(partner)) { | |
591 | dev_err(con->ucsi->dev, | |
592 | "con%d: failed to register partner (%ld)\n", con->num, | |
593 | PTR_ERR(partner)); | |
594 | return PTR_ERR(partner); | |
c1b0bc2d HK |
595 | } |
596 | ||
cf6e06cd HK |
597 | con->partner = partner; |
598 | ||
c1b0bc2d HK |
599 | return 0; |
600 | } | |
601 | ||
602 | static void ucsi_unregister_partner(struct ucsi_connector *con) | |
603 | { | |
cf6e06cd HK |
604 | if (!con->partner) |
605 | return; | |
606 | ||
ad74b864 | 607 | ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP); |
c1b0bc2d HK |
608 | typec_unregister_partner(con->partner); |
609 | con->partner = NULL; | |
610 | } | |
611 | ||
ad74b864 HK |
612 | static void ucsi_partner_change(struct ucsi_connector *con) |
613 | { | |
614 | int ret; | |
615 | ||
616 | if (!con->partner) | |
617 | return; | |
618 | ||
3cf657f0 | 619 | switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) { |
ad74b864 | 620 | case UCSI_CONSTAT_PARTNER_TYPE_UFP: |
91813ef8 MR |
621 | case UCSI_CONSTAT_PARTNER_TYPE_CABLE: |
622 | case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: | |
ad74b864 HK |
623 | typec_set_data_role(con->port, TYPEC_HOST); |
624 | break; | |
625 | case UCSI_CONSTAT_PARTNER_TYPE_DFP: | |
626 | typec_set_data_role(con->port, TYPEC_DEVICE); | |
627 | break; | |
628 | default: | |
629 | break; | |
630 | } | |
631 | ||
632 | /* Complete pending data role swap */ | |
633 | if (!completion_done(&con->complete)) | |
634 | complete(&con->complete); | |
635 | ||
636 | /* Can't rely on Partner Flags field. Always checking the alt modes. */ | |
637 | ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP); | |
638 | if (ret) | |
639 | dev_err(con->ucsi->dev, | |
640 | "con%d: failed to register partner alternate modes\n", | |
641 | con->num); | |
642 | else | |
643 | ucsi_altmode_update_active(con); | |
644 | } | |
645 | ||
bdc62f2b | 646 | static void ucsi_handle_connector_change(struct work_struct *work) |
c1b0bc2d HK |
647 | { |
648 | struct ucsi_connector *con = container_of(work, struct ucsi_connector, | |
649 | work); | |
650 | struct ucsi *ucsi = con->ucsi; | |
217504a0 BB |
651 | struct ucsi_connector_status pre_ack_status; |
652 | struct ucsi_connector_status post_ack_status; | |
3cf657f0 | 653 | enum typec_role role; |
217504a0 BB |
654 | u16 inferred_changes; |
655 | u16 changed_flags; | |
470ce43a | 656 | u64 command; |
c1b0bc2d HK |
657 | int ret; |
658 | ||
ad74b864 | 659 | mutex_lock(&con->lock); |
c1b0bc2d | 660 | |
217504a0 BB |
661 | /* |
662 | * Some/many PPMs have an issue where all fields in the change bitfield | |
663 | * are cleared when an ACK is send. This will causes any change | |
664 | * between GET_CONNECTOR_STATUS and ACK to be lost. | |
665 | * | |
666 | * We work around this by re-fetching the connector status afterwards. | |
667 | * We then infer any changes that we see have happened but that may not | |
668 | * be represented in the change bitfield. | |
669 | * | |
670 | * Also, even though we don't need to know the currently supported alt | |
671 | * modes, we run the GET_CAM_SUPPORTED command to ensure the PPM does | |
672 | * not get stuck in case it assumes we do. | |
673 | * Always do this, rather than relying on UCSI_CONSTAT_CAM_CHANGE to be | |
674 | * set in the change bitfield. | |
675 | * | |
676 | * We end up with the following actions: | |
677 | * 1. UCSI_GET_CONNECTOR_STATUS, store result, update unprocessed_changes | |
678 | * 2. UCSI_GET_CAM_SUPPORTED, discard result | |
679 | * 3. ACK connector change | |
680 | * 4. UCSI_GET_CONNECTOR_STATUS, store result | |
681 | * 5. Infere lost changes by comparing UCSI_GET_CONNECTOR_STATUS results | |
682 | * 6. If PPM reported a new change, then restart in order to ACK | |
683 | * 7. Process everything as usual. | |
684 | * | |
685 | * We may end up seeing a change twice, but we can only miss extremely | |
686 | * short transitional changes. | |
687 | */ | |
688 | ||
689 | /* 1. First UCSI_GET_CONNECTOR_STATUS */ | |
470ce43a | 690 | command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); |
217504a0 BB |
691 | ret = ucsi_send_command(ucsi, command, &pre_ack_status, |
692 | sizeof(pre_ack_status)); | |
c1b0bc2d HK |
693 | if (ret < 0) { |
694 | dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n", | |
695 | __func__, ret); | |
696 | goto out_unlock; | |
697 | } | |
217504a0 BB |
698 | con->unprocessed_changes |= pre_ack_status.change; |
699 | ||
700 | /* 2. Run UCSI_GET_CAM_SUPPORTED and discard the result. */ | |
701 | command = UCSI_GET_CAM_SUPPORTED; | |
702 | command |= UCSI_CONNECTOR_NUMBER(con->num); | |
703 | ucsi_send_command(con->ucsi, command, NULL, 0); | |
704 | ||
705 | /* 3. ACK connector change */ | |
217504a0 | 706 | ret = ucsi_acknowledge_connector_change(ucsi); |
c5300d52 | 707 | clear_bit(EVENT_PENDING, &ucsi->flags); |
217504a0 BB |
708 | if (ret) { |
709 | dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret); | |
710 | goto out_unlock; | |
711 | } | |
712 | ||
713 | /* 4. Second UCSI_GET_CONNECTOR_STATUS */ | |
714 | command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); | |
715 | ret = ucsi_send_command(ucsi, command, &post_ack_status, | |
716 | sizeof(post_ack_status)); | |
717 | if (ret < 0) { | |
718 | dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n", | |
719 | __func__, ret); | |
720 | goto out_unlock; | |
721 | } | |
722 | ||
723 | /* 5. Inferre any missing changes */ | |
724 | changed_flags = pre_ack_status.flags ^ post_ack_status.flags; | |
725 | inferred_changes = 0; | |
726 | if (UCSI_CONSTAT_PWR_OPMODE(changed_flags) != 0) | |
727 | inferred_changes |= UCSI_CONSTAT_POWER_OPMODE_CHANGE; | |
728 | ||
729 | if (changed_flags & UCSI_CONSTAT_CONNECTED) | |
730 | inferred_changes |= UCSI_CONSTAT_CONNECT_CHANGE; | |
731 | ||
732 | if (changed_flags & UCSI_CONSTAT_PWR_DIR) | |
733 | inferred_changes |= UCSI_CONSTAT_POWER_DIR_CHANGE; | |
734 | ||
735 | if (UCSI_CONSTAT_PARTNER_FLAGS(changed_flags) != 0) | |
736 | inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE; | |
737 | ||
738 | if (UCSI_CONSTAT_PARTNER_TYPE(changed_flags) != 0) | |
739 | inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE; | |
740 | ||
741 | /* Mask out anything that was correctly notified in the later call. */ | |
742 | inferred_changes &= ~post_ack_status.change; | |
743 | if (inferred_changes) | |
744 | dev_dbg(ucsi->dev, "%s: Inferred changes that would have been lost: 0x%04x\n", | |
745 | __func__, inferred_changes); | |
746 | ||
747 | con->unprocessed_changes |= inferred_changes; | |
748 | ||
749 | /* 6. If PPM reported a new change, then restart in order to ACK */ | |
750 | if (post_ack_status.change) | |
751 | goto out_unlock; | |
752 | ||
753 | /* 7. Continue as if nothing happened */ | |
754 | con->status = post_ack_status; | |
755 | con->status.change = con->unprocessed_changes; | |
756 | con->unprocessed_changes = 0; | |
c1b0bc2d | 757 | |
3cf657f0 HK |
758 | role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR); |
759 | ||
a0d46187 | 760 | if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE || |
0e6371fb | 761 | con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE) { |
c1b0bc2d | 762 | ucsi_pwr_opmode_change(con); |
0e6371fb HK |
763 | ucsi_port_psy_changed(con); |
764 | } | |
c1b0bc2d HK |
765 | |
766 | if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) { | |
3cf657f0 | 767 | typec_set_pwr_role(con->port, role); |
c1b0bc2d HK |
768 | |
769 | /* Complete pending power role swap */ | |
770 | if (!completion_done(&con->complete)) | |
771 | complete(&con->complete); | |
772 | } | |
773 | ||
c1b0bc2d | 774 | if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) { |
3cf657f0 | 775 | typec_set_pwr_role(con->port, role); |
68816e16 | 776 | |
3cf657f0 | 777 | switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) { |
68816e16 | 778 | case UCSI_CONSTAT_PARTNER_TYPE_UFP: |
91813ef8 MR |
779 | case UCSI_CONSTAT_PARTNER_TYPE_CABLE: |
780 | case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: | |
68816e16 HK |
781 | typec_set_data_role(con->port, TYPEC_HOST); |
782 | break; | |
783 | case UCSI_CONSTAT_PARTNER_TYPE_DFP: | |
784 | typec_set_data_role(con->port, TYPEC_DEVICE); | |
785 | break; | |
786 | default: | |
787 | break; | |
788 | } | |
789 | ||
3cf657f0 | 790 | if (con->status.flags & UCSI_CONSTAT_CONNECTED) |
c1b0bc2d HK |
791 | ucsi_register_partner(con); |
792 | else | |
793 | ucsi_unregister_partner(con); | |
0e6371fb HK |
794 | |
795 | ucsi_port_psy_changed(con); | |
c1b0bc2d HK |
796 | } |
797 | ||
ad74b864 HK |
798 | if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) |
799 | ucsi_partner_change(con); | |
800 | ||
c1b0bc2d HK |
801 | trace_ucsi_connector_change(con->num, &con->status); |
802 | ||
803 | out_unlock: | |
217504a0 BB |
804 | if (test_and_clear_bit(EVENT_PENDING, &ucsi->flags)) { |
805 | schedule_work(&con->work); | |
806 | mutex_unlock(&con->lock); | |
807 | return; | |
808 | } | |
809 | ||
810 | clear_bit(EVENT_PROCESSING, &ucsi->flags); | |
ad74b864 | 811 | mutex_unlock(&con->lock); |
c1b0bc2d HK |
812 | } |
813 | ||
bdc62f2b HK |
814 | /** |
815 | * ucsi_connector_change - Process Connector Change Event | |
816 | * @ucsi: UCSI Interface | |
817 | * @num: Connector number | |
818 | */ | |
819 | void ucsi_connector_change(struct ucsi *ucsi, u8 num) | |
820 | { | |
821 | struct ucsi_connector *con = &ucsi->connector[num - 1]; | |
822 | ||
71a1fa0d | 823 | if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) { |
497210f2 | 824 | dev_dbg(ucsi->dev, "Bogus connector change event\n"); |
71a1fa0d HK |
825 | return; |
826 | } | |
827 | ||
217504a0 BB |
828 | set_bit(EVENT_PENDING, &ucsi->flags); |
829 | ||
830 | if (!test_and_set_bit(EVENT_PROCESSING, &ucsi->flags)) | |
bdc62f2b HK |
831 | schedule_work(&con->work); |
832 | } | |
833 | EXPORT_SYMBOL_GPL(ucsi_connector_change); | |
834 | ||
c1b0bc2d HK |
835 | /* -------------------------------------------------------------------------- */ |
836 | ||
837 | static int ucsi_reset_connector(struct ucsi_connector *con, bool hard) | |
838 | { | |
470ce43a | 839 | u64 command; |
c1b0bc2d | 840 | |
470ce43a HK |
841 | command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num); |
842 | command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0; | |
c1b0bc2d | 843 | |
470ce43a | 844 | return ucsi_send_command(con->ucsi, command, NULL, 0); |
c1b0bc2d HK |
845 | } |
846 | ||
847 | static int ucsi_reset_ppm(struct ucsi *ucsi) | |
848 | { | |
2ede5546 | 849 | u64 command = UCSI_PPM_RESET; |
c1b0bc2d | 850 | unsigned long tmo; |
2ede5546 | 851 | u32 cci; |
c1b0bc2d HK |
852 | int ret; |
853 | ||
25794e30 HG |
854 | mutex_lock(&ucsi->ppm_lock); |
855 | ||
2ede5546 HK |
856 | ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command, |
857 | sizeof(command)); | |
858 | if (ret < 0) | |
25794e30 | 859 | goto out; |
c1b0bc2d HK |
860 | |
861 | tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); | |
862 | ||
863 | do { | |
25794e30 HG |
864 | if (time_is_before_jiffies(tmo)) { |
865 | ret = -ETIMEDOUT; | |
866 | goto out; | |
867 | } | |
c1b0bc2d | 868 | |
2ede5546 HK |
869 | ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); |
870 | if (ret) | |
25794e30 | 871 | goto out; |
c1b0bc2d HK |
872 | |
873 | /* If the PPM is still doing something else, reset it again. */ | |
2ede5546 HK |
874 | if (cci & ~UCSI_CCI_RESET_COMPLETE) { |
875 | ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, | |
876 | &command, | |
877 | sizeof(command)); | |
878 | if (ret < 0) | |
25794e30 | 879 | goto out; |
c1b0bc2d HK |
880 | } |
881 | ||
c1b0bc2d | 882 | msleep(20); |
2ede5546 | 883 | } while (!(cci & UCSI_CCI_RESET_COMPLETE)); |
c1b0bc2d | 884 | |
25794e30 HG |
885 | out: |
886 | mutex_unlock(&ucsi->ppm_lock); | |
887 | return ret; | |
c1b0bc2d HK |
888 | } |
889 | ||
470ce43a | 890 | static int ucsi_role_cmd(struct ucsi_connector *con, u64 command) |
c1b0bc2d HK |
891 | { |
892 | int ret; | |
893 | ||
470ce43a | 894 | ret = ucsi_send_command(con->ucsi, command, NULL, 0); |
c1b0bc2d | 895 | if (ret == -ETIMEDOUT) { |
470ce43a | 896 | u64 c; |
c1b0bc2d HK |
897 | |
898 | /* PPM most likely stopped responding. Resetting everything. */ | |
899 | ucsi_reset_ppm(con->ucsi); | |
900 | ||
71a1fa0d | 901 | c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy; |
470ce43a | 902 | ucsi_send_command(con->ucsi, c, NULL, 0); |
c1b0bc2d HK |
903 | |
904 | ucsi_reset_connector(con, true); | |
905 | } | |
906 | ||
907 | return ret; | |
908 | } | |
909 | ||
6df475f8 | 910 | static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role) |
c1b0bc2d | 911 | { |
6df475f8 | 912 | struct ucsi_connector *con = typec_get_drvdata(port); |
3cf657f0 | 913 | u8 partner_type; |
470ce43a | 914 | u64 command; |
c1b0bc2d HK |
915 | int ret = 0; |
916 | ||
ad74b864 | 917 | mutex_lock(&con->lock); |
c1b0bc2d | 918 | |
ad74b864 HK |
919 | if (!con->partner) { |
920 | ret = -ENOTCONN; | |
921 | goto out_unlock; | |
922 | } | |
c1b0bc2d | 923 | |
3cf657f0 HK |
924 | partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags); |
925 | if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP && | |
c1b0bc2d | 926 | role == TYPEC_DEVICE) || |
3cf657f0 | 927 | (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP && |
c1b0bc2d HK |
928 | role == TYPEC_HOST)) |
929 | goto out_unlock; | |
930 | ||
470ce43a HK |
931 | command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num); |
932 | command |= UCSI_SET_UOR_ROLE(role); | |
933 | command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS; | |
934 | ret = ucsi_role_cmd(con, command); | |
c1b0bc2d HK |
935 | if (ret < 0) |
936 | goto out_unlock; | |
937 | ||
c1b0bc2d HK |
938 | if (!wait_for_completion_timeout(&con->complete, |
939 | msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) | |
ad74b864 | 940 | ret = -ETIMEDOUT; |
c1b0bc2d HK |
941 | |
942 | out_unlock: | |
ad74b864 | 943 | mutex_unlock(&con->lock); |
c1b0bc2d | 944 | |
ad74b864 | 945 | return ret < 0 ? ret : 0; |
c1b0bc2d HK |
946 | } |
947 | ||
6df475f8 | 948 | static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) |
c1b0bc2d | 949 | { |
6df475f8 | 950 | struct ucsi_connector *con = typec_get_drvdata(port); |
3cf657f0 | 951 | enum typec_role cur_role; |
470ce43a | 952 | u64 command; |
c1b0bc2d HK |
953 | int ret = 0; |
954 | ||
ad74b864 | 955 | mutex_lock(&con->lock); |
c1b0bc2d | 956 | |
ad74b864 HK |
957 | if (!con->partner) { |
958 | ret = -ENOTCONN; | |
959 | goto out_unlock; | |
960 | } | |
c1b0bc2d | 961 | |
3cf657f0 HK |
962 | cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR); |
963 | ||
964 | if (cur_role == role) | |
c1b0bc2d HK |
965 | goto out_unlock; |
966 | ||
470ce43a HK |
967 | command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num); |
968 | command |= UCSI_SET_PDR_ROLE(role); | |
969 | command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS; | |
970 | ret = ucsi_role_cmd(con, command); | |
c1b0bc2d HK |
971 | if (ret < 0) |
972 | goto out_unlock; | |
973 | ||
c1b0bc2d | 974 | if (!wait_for_completion_timeout(&con->complete, |
ad74b864 HK |
975 | msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) { |
976 | ret = -ETIMEDOUT; | |
977 | goto out_unlock; | |
978 | } | |
c1b0bc2d HK |
979 | |
980 | /* Something has gone wrong while swapping the role */ | |
3cf657f0 HK |
981 | if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) != |
982 | UCSI_CONSTAT_PWR_OPMODE_PD) { | |
c1b0bc2d HK |
983 | ucsi_reset_connector(con, true); |
984 | ret = -EPROTO; | |
985 | } | |
986 | ||
987 | out_unlock: | |
ad74b864 | 988 | mutex_unlock(&con->lock); |
c1b0bc2d HK |
989 | |
990 | return ret; | |
991 | } | |
992 | ||
6df475f8 HK |
993 | static const struct typec_operations ucsi_ops = { |
994 | .dr_set = ucsi_dr_swap, | |
995 | .pr_set = ucsi_pr_swap | |
996 | }; | |
997 | ||
85fed448 | 998 | /* Caller must call fwnode_handle_put() after use */ |
c1b0bc2d HK |
999 | static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con) |
1000 | { | |
1001 | struct fwnode_handle *fwnode; | |
1002 | int i = 1; | |
1003 | ||
1004 | device_for_each_child_node(con->ucsi->dev, fwnode) | |
1005 | if (i++ == con->num) | |
1006 | return fwnode; | |
1007 | return NULL; | |
1008 | } | |
1009 | ||
1010 | static int ucsi_register_port(struct ucsi *ucsi, int index) | |
1011 | { | |
1012 | struct ucsi_connector *con = &ucsi->connector[index]; | |
1013 | struct typec_capability *cap = &con->typec_cap; | |
1014 | enum typec_accessory *accessory = cap->accessory; | |
470ce43a | 1015 | u64 command; |
c1b0bc2d HK |
1016 | int ret; |
1017 | ||
bdc62f2b | 1018 | INIT_WORK(&con->work, ucsi_handle_connector_change); |
c1b0bc2d | 1019 | init_completion(&con->complete); |
ad74b864 | 1020 | mutex_init(&con->lock); |
c1b0bc2d HK |
1021 | con->num = index + 1; |
1022 | con->ucsi = ucsi; | |
1023 | ||
bed97b30 HG |
1024 | /* Delay other interactions with the con until registration is complete */ |
1025 | mutex_lock(&con->lock); | |
1026 | ||
c1b0bc2d | 1027 | /* Get connector capability */ |
470ce43a HK |
1028 | command = UCSI_GET_CONNECTOR_CAPABILITY; |
1029 | command |= UCSI_CONNECTOR_NUMBER(con->num); | |
0ff0705a | 1030 | ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap)); |
c1b0bc2d | 1031 | if (ret < 0) |
85fed448 | 1032 | goto out_unlock; |
c1b0bc2d HK |
1033 | |
1034 | if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP) | |
ceeb1625 | 1035 | cap->data = TYPEC_PORT_DRD; |
c1b0bc2d | 1036 | else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP) |
ceeb1625 | 1037 | cap->data = TYPEC_PORT_DFP; |
c1b0bc2d | 1038 | else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP) |
ceeb1625 HK |
1039 | cap->data = TYPEC_PORT_UFP; |
1040 | ||
3cf657f0 HK |
1041 | if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) && |
1042 | (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)) | |
ceeb1625 | 1043 | cap->type = TYPEC_PORT_DRP; |
3cf657f0 | 1044 | else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) |
ceeb1625 | 1045 | cap->type = TYPEC_PORT_SRC; |
3cf657f0 | 1046 | else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER) |
ceeb1625 | 1047 | cap->type = TYPEC_PORT_SNK; |
c1b0bc2d HK |
1048 | |
1049 | cap->revision = ucsi->cap.typec_version; | |
1050 | cap->pd_revision = ucsi->cap.pd_version; | |
1051 | cap->prefer_role = TYPEC_NO_PREFERRED_ROLE; | |
1052 | ||
1053 | if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY) | |
1054 | *accessory++ = TYPEC_ACCESSORY_AUDIO; | |
1055 | if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY) | |
1056 | *accessory = TYPEC_ACCESSORY_DEBUG; | |
1057 | ||
1058 | cap->fwnode = ucsi_find_fwnode(con); | |
6df475f8 HK |
1059 | cap->driver_data = con; |
1060 | cap->ops = &ucsi_ops; | |
c1b0bc2d | 1061 | |
992a60ed A |
1062 | ret = ucsi_register_port_psy(con); |
1063 | if (ret) | |
bed97b30 | 1064 | goto out; |
992a60ed | 1065 | |
c1b0bc2d HK |
1066 | /* Register the connector */ |
1067 | con->port = typec_register_port(ucsi->dev, cap); | |
bed97b30 HG |
1068 | if (IS_ERR(con->port)) { |
1069 | ret = PTR_ERR(con->port); | |
1070 | goto out; | |
1071 | } | |
c1b0bc2d | 1072 | |
ad74b864 HK |
1073 | /* Alternate modes */ |
1074 | ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON); | |
bed97b30 | 1075 | if (ret) { |
ad74b864 HK |
1076 | dev_err(ucsi->dev, "con%d: failed to register alt modes\n", |
1077 | con->num); | |
bed97b30 HG |
1078 | goto out; |
1079 | } | |
ad74b864 | 1080 | |
c1b0bc2d | 1081 | /* Get the status */ |
470ce43a | 1082 | command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); |
0ff0705a | 1083 | ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status)); |
c1b0bc2d HK |
1084 | if (ret < 0) { |
1085 | dev_err(ucsi->dev, "con%d: failed to get status\n", con->num); | |
bed97b30 HG |
1086 | ret = 0; |
1087 | goto out; | |
c1b0bc2d | 1088 | } |
bed97b30 | 1089 | ret = 0; /* ucsi_send_command() returns length on success */ |
c1b0bc2d | 1090 | |
3cf657f0 | 1091 | switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) { |
c1b0bc2d | 1092 | case UCSI_CONSTAT_PARTNER_TYPE_UFP: |
91813ef8 MR |
1093 | case UCSI_CONSTAT_PARTNER_TYPE_CABLE: |
1094 | case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: | |
c1b0bc2d HK |
1095 | typec_set_data_role(con->port, TYPEC_HOST); |
1096 | break; | |
1097 | case UCSI_CONSTAT_PARTNER_TYPE_DFP: | |
1098 | typec_set_data_role(con->port, TYPEC_DEVICE); | |
1099 | break; | |
1100 | default: | |
1101 | break; | |
1102 | } | |
1103 | ||
1104 | /* Check if there is already something connected */ | |
3cf657f0 HK |
1105 | if (con->status.flags & UCSI_CONSTAT_CONNECTED) { |
1106 | typec_set_pwr_role(con->port, | |
1107 | !!(con->status.flags & UCSI_CONSTAT_PWR_DIR)); | |
1108 | ucsi_pwr_opmode_change(con); | |
c1b0bc2d | 1109 | ucsi_register_partner(con); |
0e6371fb | 1110 | ucsi_port_psy_changed(con); |
3cf657f0 | 1111 | } |
c1b0bc2d | 1112 | |
ad74b864 HK |
1113 | if (con->partner) { |
1114 | ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP); | |
bed97b30 | 1115 | if (ret) { |
ad74b864 HK |
1116 | dev_err(ucsi->dev, |
1117 | "con%d: failed to register alternate modes\n", | |
1118 | con->num); | |
bed97b30 HG |
1119 | ret = 0; |
1120 | } else { | |
ad74b864 | 1121 | ucsi_altmode_update_active(con); |
bed97b30 | 1122 | } |
ad74b864 HK |
1123 | } |
1124 | ||
c1b0bc2d HK |
1125 | trace_ucsi_register_port(con->num, &con->status); |
1126 | ||
bed97b30 | 1127 | out: |
85fed448 AS |
1128 | fwnode_handle_put(cap->fwnode); |
1129 | out_unlock: | |
bed97b30 HG |
1130 | mutex_unlock(&con->lock); |
1131 | return ret; | |
c1b0bc2d HK |
1132 | } |
1133 | ||
bdc62f2b HK |
1134 | /** |
1135 | * ucsi_init - Initialize UCSI interface | |
1136 | * @ucsi: UCSI to be initialized | |
1137 | * | |
1138 | * Registers all ports @ucsi has and enables all notification events. | |
1139 | */ | |
e6b073de | 1140 | static int ucsi_init(struct ucsi *ucsi) |
c1b0bc2d | 1141 | { |
c1b0bc2d | 1142 | struct ucsi_connector *con; |
470ce43a | 1143 | u64 command; |
c1b0bc2d HK |
1144 | int ret; |
1145 | int i; | |
1146 | ||
c1b0bc2d HK |
1147 | /* Reset the PPM */ |
1148 | ret = ucsi_reset_ppm(ucsi); | |
1149 | if (ret) { | |
1150 | dev_err(ucsi->dev, "failed to reset PPM!\n"); | |
1151 | goto err; | |
1152 | } | |
1153 | ||
1154 | /* Enable basic notifications */ | |
71a1fa0d HK |
1155 | ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR; |
1156 | command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; | |
25794e30 | 1157 | ret = ucsi_send_command(ucsi, command, NULL, 0); |
c1b0bc2d HK |
1158 | if (ret < 0) |
1159 | goto err_reset; | |
1160 | ||
1161 | /* Get PPM capabilities */ | |
470ce43a | 1162 | command = UCSI_GET_CAPABILITY; |
25794e30 | 1163 | ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap)); |
c1b0bc2d HK |
1164 | if (ret < 0) |
1165 | goto err_reset; | |
1166 | ||
1167 | if (!ucsi->cap.num_connectors) { | |
1168 | ret = -ENODEV; | |
1169 | goto err_reset; | |
1170 | } | |
1171 | ||
1172 | /* Allocate the connectors. Released in ucsi_unregister_ppm() */ | |
1173 | ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1, | |
1174 | sizeof(*ucsi->connector), GFP_KERNEL); | |
1175 | if (!ucsi->connector) { | |
1176 | ret = -ENOMEM; | |
1177 | goto err_reset; | |
1178 | } | |
1179 | ||
1180 | /* Register all connectors */ | |
1181 | for (i = 0; i < ucsi->cap.num_connectors; i++) { | |
1182 | ret = ucsi_register_port(ucsi, i); | |
1183 | if (ret) | |
1184 | goto err_unregister; | |
1185 | } | |
1186 | ||
1187 | /* Enable all notifications */ | |
9521e47e | 1188 | ucsi->ntfy = UCSI_ENABLE_NTFY_ALL; |
71a1fa0d | 1189 | command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; |
0ff0705a | 1190 | ret = ucsi_send_command(ucsi, command, NULL, 0); |
c1b0bc2d HK |
1191 | if (ret < 0) |
1192 | goto err_unregister; | |
1193 | ||
bdc62f2b | 1194 | return 0; |
c1b0bc2d HK |
1195 | |
1196 | err_unregister: | |
1197 | for (con = ucsi->connector; con->port; con++) { | |
1198 | ucsi_unregister_partner(con); | |
ad74b864 | 1199 | ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON); |
992a60ed | 1200 | ucsi_unregister_port_psy(con); |
c1b0bc2d HK |
1201 | typec_unregister_port(con->port); |
1202 | con->port = NULL; | |
1203 | } | |
1204 | ||
1205 | err_reset: | |
f624a82d | 1206 | memset(&ucsi->cap, 0, sizeof(ucsi->cap)); |
c1b0bc2d HK |
1207 | ucsi_reset_ppm(ucsi); |
1208 | err: | |
bdc62f2b HK |
1209 | return ret; |
1210 | } | |
bdc62f2b HK |
1211 | |
1212 | static void ucsi_init_work(struct work_struct *work) | |
1213 | { | |
1214 | struct ucsi *ucsi = container_of(work, struct ucsi, work); | |
1215 | int ret; | |
1216 | ||
1217 | ret = ucsi_init(ucsi); | |
1218 | if (ret) | |
1219 | dev_err(ucsi->dev, "PPM init failed (%d)\n", ret); | |
c1b0bc2d HK |
1220 | } |
1221 | ||
1222 | /** | |
bdc62f2b HK |
1223 | * ucsi_get_drvdata - Return private driver data pointer |
1224 | * @ucsi: UCSI interface | |
c1b0bc2d | 1225 | */ |
bdc62f2b HK |
1226 | void *ucsi_get_drvdata(struct ucsi *ucsi) |
1227 | { | |
1228 | return ucsi->driver_data; | |
1229 | } | |
1230 | EXPORT_SYMBOL_GPL(ucsi_get_drvdata); | |
1231 | ||
1232 | /** | |
1233 | * ucsi_get_drvdata - Assign private driver data pointer | |
1234 | * @ucsi: UCSI interface | |
1235 | * @data: Private data pointer | |
1236 | */ | |
1237 | void ucsi_set_drvdata(struct ucsi *ucsi, void *data) | |
1238 | { | |
1239 | ucsi->driver_data = data; | |
1240 | } | |
1241 | EXPORT_SYMBOL_GPL(ucsi_set_drvdata); | |
1242 | ||
1243 | /** | |
1244 | * ucsi_create - Allocate UCSI instance | |
1245 | * @dev: Device interface to the PPM (Platform Policy Manager) | |
1246 | * @ops: I/O routines | |
1247 | */ | |
1248 | struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) | |
c1b0bc2d HK |
1249 | { |
1250 | struct ucsi *ucsi; | |
1251 | ||
bdc62f2b HK |
1252 | if (!ops || !ops->read || !ops->sync_write || !ops->async_write) |
1253 | return ERR_PTR(-EINVAL); | |
1254 | ||
c1b0bc2d HK |
1255 | ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); |
1256 | if (!ucsi) | |
1257 | return ERR_PTR(-ENOMEM); | |
1258 | ||
bdc62f2b | 1259 | INIT_WORK(&ucsi->work, ucsi_init_work); |
c1b0bc2d | 1260 | mutex_init(&ucsi->ppm_lock); |
c1b0bc2d | 1261 | ucsi->dev = dev; |
bdc62f2b HK |
1262 | ucsi->ops = ops; |
1263 | ||
1264 | return ucsi; | |
1265 | } | |
1266 | EXPORT_SYMBOL_GPL(ucsi_create); | |
1267 | ||
1268 | /** | |
1269 | * ucsi_destroy - Free UCSI instance | |
1270 | * @ucsi: UCSI instance to be freed | |
1271 | */ | |
1272 | void ucsi_destroy(struct ucsi *ucsi) | |
1273 | { | |
1274 | kfree(ucsi); | |
1275 | } | |
1276 | EXPORT_SYMBOL_GPL(ucsi_destroy); | |
1277 | ||
1278 | /** | |
1279 | * ucsi_register - Register UCSI interface | |
1280 | * @ucsi: UCSI instance | |
1281 | */ | |
1282 | int ucsi_register(struct ucsi *ucsi) | |
1283 | { | |
1284 | int ret; | |
1285 | ||
1286 | ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version, | |
1287 | sizeof(ucsi->version)); | |
1288 | if (ret) | |
1289 | return ret; | |
1290 | ||
1291 | if (!ucsi->version) | |
1292 | return -ENODEV; | |
c1b0bc2d | 1293 | |
c1b0bc2d HK |
1294 | queue_work(system_long_wq, &ucsi->work); |
1295 | ||
bdc62f2b | 1296 | return 0; |
c1b0bc2d | 1297 | } |
bdc62f2b | 1298 | EXPORT_SYMBOL_GPL(ucsi_register); |
c1b0bc2d HK |
1299 | |
1300 | /** | |
bdc62f2b HK |
1301 | * ucsi_unregister - Unregister UCSI interface |
1302 | * @ucsi: UCSI interface to be unregistered | |
c1b0bc2d | 1303 | * |
bdc62f2b | 1304 | * Unregister UCSI interface that was created with ucsi_register(). |
c1b0bc2d | 1305 | */ |
bdc62f2b | 1306 | void ucsi_unregister(struct ucsi *ucsi) |
c1b0bc2d | 1307 | { |
74ce3e41 | 1308 | u64 cmd = UCSI_SET_NOTIFICATION_ENABLE; |
c1b0bc2d HK |
1309 | int i; |
1310 | ||
1311 | /* Make sure that we are not in the middle of driver initialization */ | |
1312 | cancel_work_sync(&ucsi->work); | |
1313 | ||
74ce3e41 HK |
1314 | /* Disable notifications */ |
1315 | ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); | |
c1b0bc2d HK |
1316 | |
1317 | for (i = 0; i < ucsi->cap.num_connectors; i++) { | |
1318 | cancel_work_sync(&ucsi->connector[i].work); | |
1319 | ucsi_unregister_partner(&ucsi->connector[i]); | |
ad74b864 HK |
1320 | ucsi_unregister_altmodes(&ucsi->connector[i], |
1321 | UCSI_RECIPIENT_CON); | |
992a60ed | 1322 | ucsi_unregister_port_psy(&ucsi->connector[i]); |
c1b0bc2d HK |
1323 | typec_unregister_port(ucsi->connector[i].port); |
1324 | } | |
1325 | ||
c1b0bc2d | 1326 | kfree(ucsi->connector); |
bdc62f2b HK |
1327 | } |
1328 | EXPORT_SYMBOL_GPL(ucsi_unregister); | |
1329 | ||
c1b0bc2d HK |
1330 | MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); |
1331 | MODULE_LICENSE("GPL v2"); | |
1332 | MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver"); |