blob: 0aee57cb2f4a44af7b483e0e609e39ba584a06c0 [file] [log] [blame]
Sebastian Reichel1db4c0a2024-10-15 17:26:43 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2015-2017 Google, Inc
4 *
5 * USB Power Delivery protocol stack.
6 */
7
8#include <asm/gpio.h>
9#include <asm/io.h>
10#include <dm.h>
11#include <dm/device_compat.h>
12#include <dm/device-internal.h>
13#include <dm/devres.h>
14#include <linux/compat.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/iopoll.h>
18#include <time.h>
19#include <usb/tcpm.h>
20#include "tcpm-internal.h"
21
22DECLARE_GLOBAL_DATA_PTR;
23
24const char * const tcpm_states[] = {
25 FOREACH_TCPM_STATE(GENERATE_TCPM_STRING)
26};
27
28const char * const typec_pd_rev_name[] = {
29 [PD_REV10] = "rev1",
30 [PD_REV20] = "rev2",
31 [PD_REV30] = "rev3",
32};
33
34const char * const typec_role_name[] = {
35 [TYPEC_SINK] = "sink",
36 [TYPEC_SOURCE] = "source",
37};
38
39const char * const typec_data_role_name[] = {
40 [TYPEC_DEVICE] = "device",
41 [TYPEC_HOST] = "host",
42};
43
44const char * const typec_orientation_name[] = {
45 [TYPEC_ORIENTATION_NONE] = "none",
46 [TYPEC_ORIENTATION_NORMAL] = "normal",
47 [TYPEC_ORIENTATION_REVERSE] = "reverse",
48};
49
50const char * const typec_cc_status_name[] = {
51 [TYPEC_CC_OPEN] = "open",
52 [TYPEC_CC_RA] = "ra",
53 [TYPEC_CC_RD] = "rd",
54 [TYPEC_CC_RP_DEF] = "rp-def",
55 [TYPEC_CC_RP_1_5] = "rp-1.5",
56 [TYPEC_CC_RP_3_0] = "rp-3.0",
57};
58
59static inline bool tcpm_cc_is_sink(enum typec_cc_status cc)
60{
61 return cc == TYPEC_CC_RP_DEF ||
62 cc == TYPEC_CC_RP_1_5 ||
63 cc == TYPEC_CC_RP_3_0;
64}
65
66static inline bool tcpm_port_is_sink(struct tcpm_port *port)
67{
68 bool cc1_is_snk = tcpm_cc_is_sink(port->cc1);
69 bool cc2_is_snk = tcpm_cc_is_sink(port->cc2);
70
71 return (cc1_is_snk && !cc2_is_snk) ||
72 (cc2_is_snk && !cc1_is_snk);
73}
74
75static inline bool tcpm_cc_is_source(enum typec_cc_status cc)
76{
77 return cc == TYPEC_CC_RD;
78}
79
80static inline bool tcpm_port_is_source(struct tcpm_port *port)
81{
82 bool cc1_is_src = tcpm_cc_is_source(port->cc1);
83 bool cc2_is_src = tcpm_cc_is_source(port->cc2);
84
85 return (cc1_is_src && !cc2_is_src) ||
86 (cc2_is_src && !cc1_is_src);
87}
88
89static inline bool tcpm_try_src(struct tcpm_port *port)
90{
91 return port->try_role == TYPEC_SOURCE &&
92 port->port_type == TYPEC_PORT_DRP;
93}
94
95static inline void tcpm_reset_event_cnt(struct udevice *dev)
96{
97 struct tcpm_port *port = dev_get_uclass_plat(dev);
98
99 port->poll_event_cnt = 0;
100}
101
102static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
103{
104 if (port->port_type == TYPEC_PORT_DRP) {
105 if (port->try_role == TYPEC_SINK)
106 return SNK_UNATTACHED;
107 else if (port->try_role == TYPEC_SOURCE)
108 return SRC_UNATTACHED;
109 } else if (port->port_type == TYPEC_PORT_SNK) {
110 return SNK_UNATTACHED;
111 }
112 return SRC_UNATTACHED;
113}
114
115static bool tcpm_port_is_disconnected(struct tcpm_port *port)
116{
117 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
118 port->cc2 == TYPEC_CC_OPEN) ||
119 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
120 port->cc1 == TYPEC_CC_OPEN) ||
121 (port->polarity == TYPEC_POLARITY_CC2 &&
122 port->cc2 == TYPEC_CC_OPEN)));
123}
124
125static void tcpm_set_cc(struct udevice *dev, enum typec_cc_status cc)
126{
127 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
128 struct tcpm_port *port = dev_get_uclass_plat(dev);
129
130 dev_dbg(dev, "TCPM: set cc = %d\n", cc);
131 port->cc_req = cc;
132 drvops->set_cc(dev, cc);
133}
134
135/*
136 * Determine RP value to set based on maximum current supported
137 * by a port if configured as source.
138 * Returns CC value to report to link partner.
139 */
140static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
141{
142 const u32 *src_pdo = port->src_pdo;
143 int nr_pdo = port->nr_src_pdo;
144 int i;
145
146 /*
147 * Search for first entry with matching voltage.
148 * It should report the maximum supported current.
149 */
150 for (i = 0; i < nr_pdo; i++) {
151 const u32 pdo = src_pdo[i];
152
153 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
154 pdo_fixed_voltage(pdo) == 5000) {
155 unsigned int curr = pdo_max_current(pdo);
156
157 if (curr >= 3000)
158 return TYPEC_CC_RP_3_0;
159 else if (curr >= 1500)
160 return TYPEC_CC_RP_1_5;
161 return TYPEC_CC_RP_DEF;
162 }
163 }
164
165 return TYPEC_CC_RP_DEF;
166}
167
168static void tcpm_check_and_run_delayed_work(struct udevice *dev);
169
170static bool tcpm_transmit_helper(struct udevice *dev)
171{
172 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
173 struct tcpm_port *port = dev_get_uclass_plat(dev);
174
175 drvops->poll_event(dev);
176 udelay(500);
177 tcpm_check_and_run_delayed_work(dev);
178 return port->tx_complete;
179}
180
181static int tcpm_pd_transmit(struct udevice *dev,
182 enum tcpm_transmit_type type,
183 const struct pd_message *msg)
184{
185 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
186 struct tcpm_port *port = dev_get_uclass_plat(dev);
187 u32 timeout_us = PD_T_TCPC_TX_TIMEOUT * 1000;
188 bool tx_complete;
189 int ret;
190
191 if (msg)
192 dev_dbg(dev, "TCPM: PD TX, header: %#x\n",
193 le16_to_cpu(msg->header));
194 else
195 dev_dbg(dev, "TCPM: PD TX, type: %#x\n", type);
196
197 port->tx_complete = false;
198 ret = drvops->pd_transmit(dev, type, msg, port->negotiated_rev);
199 if (ret < 0)
200 return ret;
201
202 /*
203 * At this point we basically need to block until the TCPM controller
204 * returns successful transmission. Since this is usually done using
205 * the generic interrupt status bits, we poll for any events. That
206 * will clear the interrupt status, so we also need to process any
207 * of the incoming events. This means we will do more processing and
208 * thus let's give everything a bit more time.
209 */
210 timeout_us *= 5;
211 ret = read_poll_timeout(tcpm_transmit_helper, tx_complete,
212 !tx_complete, false, timeout_us, dev);
213 if (ret < 0) {
214 dev_err(dev, "TCPM: PD transmit data failed: %d\n", ret);
215 return ret;
216 }
217
218 switch (port->tx_status) {
219 case TCPC_TX_SUCCESS:
220 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
221 break;
222 case TCPC_TX_DISCARDED:
223 ret = -EAGAIN;
224 break;
225 case TCPC_TX_FAILED:
226 default:
227 ret = -EIO;
228 break;
229 }
230
231 return ret;
232}
233
234void tcpm_pd_transmit_complete(struct udevice *dev,
235 enum tcpm_transmit_status status)
236{
237 struct tcpm_port *port = dev_get_uclass_plat(dev);
238
239 dev_dbg(dev, "TCPM: PD TX complete, status: %u\n", status);
240 tcpm_reset_event_cnt(dev);
241 port->tx_status = status;
242 port->tx_complete = true;
243}
244
245static int tcpm_set_polarity(struct udevice *dev,
246 enum typec_cc_polarity polarity)
247{
248 struct tcpm_port *port = dev_get_uclass_plat(dev);
249 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
250 int ret;
251
252 dev_dbg(dev, "TCPM: set polarity = %d\n", polarity);
253
254 if (drvops->set_polarity) {
255 ret = drvops->set_polarity(dev, polarity);
256 if (ret < 0)
257 return ret;
258 }
259
260 port->polarity = polarity;
261
262 return 0;
263}
264
265static int tcpm_set_vconn(struct udevice *dev, bool enable)
266{
267 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
268 struct tcpm_port *port = dev_get_uclass_plat(dev);
269 int ret;
270
271 dev_dbg(dev, "TCPM: set vconn = %d\n", enable);
272
273 ret = drvops->set_vconn(dev, enable);
274 if (!ret)
275 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
276
277 return ret;
278}
279
280static inline u32 tcpm_get_current_limit(struct tcpm_port *port)
281{
282 switch (port->polarity ? port->cc2 : port->cc1) {
283 case TYPEC_CC_RP_1_5:
284 return 1500;
285 case TYPEC_CC_RP_3_0:
286 return 3000;
287 case TYPEC_CC_RP_DEF:
288 default:
289 return 0;
290 }
291}
292
293static int tcpm_set_current_limit(struct udevice *dev, u32 max_ma, u32 mv)
294{
295 struct tcpm_port *port = dev_get_uclass_plat(dev);
296 int ret = -EOPNOTSUPP;
297
298 dev_info(dev, "TCPM: set voltage limit = %u mV\n", mv);
299 dev_info(dev, "TCPM: set current limit = %u mA\n", max_ma);
300
301 port->supply_voltage = mv;
302 port->current_limit = max_ma;
303
304 return ret;
305}
306
307static int tcpm_set_attached_state(struct udevice *dev, bool attached)
308{
309 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
310 struct tcpm_port *port = dev_get_uclass_plat(dev);
311
312 return drvops->set_roles(dev, attached, port->pwr_role,
313 port->data_role);
314}
315
316static int tcpm_set_roles(struct udevice *dev, bool attached,
317 enum typec_role role, enum typec_data_role data)
318{
319 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
320 struct tcpm_port *port = dev_get_uclass_plat(dev);
321 int ret;
322
323 ret = drvops->set_roles(dev, attached, role, data);
324 if (ret < 0)
325 return ret;
326
327 port->pwr_role = role;
328 port->data_role = data;
329
330 return 0;
331}
332
333static int tcpm_pd_send_source_caps(struct udevice *dev)
334{
335 struct tcpm_port *port = dev_get_uclass_plat(dev);
336 struct pd_message msg;
337 int i;
338
339 memset(&msg, 0, sizeof(msg));
340
341 if (!port->nr_src_pdo) {
342 /* No source capabilities defined, sink only */
343 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
344 port->pwr_role,
345 port->data_role,
346 port->negotiated_rev,
347 port->message_id, 0);
348 } else {
349 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
350 port->pwr_role,
351 port->data_role,
352 port->negotiated_rev,
353 port->message_id,
354 port->nr_src_pdo);
355 }
356
357 for (i = 0; i < port->nr_src_pdo; i++)
358 msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
359
360 return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
361}
362
363static int tcpm_pd_send_sink_caps(struct udevice *dev)
364{
365 struct tcpm_port *port = dev_get_uclass_plat(dev);
366 struct pd_message msg;
367 unsigned int i;
368
369 memset(&msg, 0, sizeof(msg));
370
371 if (!port->nr_snk_pdo) {
372 /* No sink capabilities defined, source only */
373 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
374 port->pwr_role,
375 port->data_role,
376 port->negotiated_rev,
377 port->message_id, 0);
378 } else {
379 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
380 port->pwr_role,
381 port->data_role,
382 port->negotiated_rev,
383 port->message_id,
384 port->nr_snk_pdo);
385 }
386
387 for (i = 0; i < port->nr_snk_pdo; i++)
388 msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
389
390 return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
391}
392
393static void tcpm_state_machine(struct udevice *dev);
394
395static inline void tcpm_timer_uninit(struct udevice *dev)
396{
397 struct tcpm_port *port = dev_get_uclass_plat(dev);
398
399 port->delay_target = 0;
400}
401
402static void tcpm_timer_init(struct udevice *dev, uint32_t ms)
403{
404 struct tcpm_port *port = dev_get_uclass_plat(dev);
405 unsigned long time_us = ms * 1000;
406
407 port->delay_target = timer_get_us() + time_us;
408}
409
410static void tcpm_check_and_run_delayed_work(struct udevice *dev)
411{
412 struct tcpm_port *port = dev_get_uclass_plat(dev);
413
414 /* no delayed state changes scheduled */
415 if (port->delay_target == 0)
416 return;
417
418 /* it's not yet time */
419 if (timer_get_us() < port->delay_target)
420 return;
421
422 tcpm_timer_uninit(dev);
423 tcpm_state_machine(dev);
424}
425
426static void mod_tcpm_delayed_work(struct udevice *dev, unsigned int delay_ms)
427{
428 if (delay_ms) {
429 tcpm_timer_init(dev, delay_ms);
430 } else {
431 tcpm_timer_uninit(dev);
432 tcpm_state_machine(dev);
433 }
434}
435
436static void tcpm_set_state(struct udevice *dev, enum tcpm_state state,
437 unsigned int delay_ms)
438{
439 struct tcpm_port *port = dev_get_uclass_plat(dev);
440
441 if (delay_ms) {
442 dev_dbg(dev, "TCPM: pending state change %s -> %s @ %u ms [%s]\n",
443 tcpm_states[port->state], tcpm_states[state], delay_ms,
444 typec_pd_rev_name[port->negotiated_rev]);
445 port->delayed_state = state;
446 mod_tcpm_delayed_work(dev, delay_ms);
447 port->delay_ms = delay_ms;
448 } else {
449 dev_dbg(dev, "TCPM: state change %s -> %s\n",
450 tcpm_states[port->state], tcpm_states[state]);
451 port->delayed_state = INVALID_STATE;
452 port->prev_state = port->state;
453 port->state = state;
454 /*
455 * Don't re-queue the state machine work item if we're currently
456 * in the state machine and we're immediately changing states.
457 * tcpm_state_machine_work() will continue running the state
458 * machine.
459 */
460 if (!port->state_machine_running)
461 mod_tcpm_delayed_work(dev, 0);
462 }
463}
464
465static void tcpm_set_state_cond(struct udevice *dev, enum tcpm_state state,
466 unsigned int delay_ms)
467{
468 struct tcpm_port *port = dev_get_uclass_plat(dev);
469
470 if (port->enter_state == port->state)
471 tcpm_set_state(dev, state, delay_ms);
472 else
473 dev_dbg(dev, "TCPM: skipped %sstate change %s -> %s [%u ms], context state %s [%s]\n",
474 delay_ms ? "delayed " : "",
475 tcpm_states[port->state], tcpm_states[state],
476 delay_ms, tcpm_states[port->enter_state],
477 typec_pd_rev_name[port->negotiated_rev]);
478}
479
480static void tcpm_queue_message(struct udevice *dev,
481 enum pd_msg_request message)
482{
483 struct tcpm_port *port = dev_get_uclass_plat(dev);
484
485 port->queued_message = message;
486 mod_tcpm_delayed_work(dev, 0);
487}
488
489enum pdo_err {
490 PDO_NO_ERR,
491 PDO_ERR_NO_VSAFE5V,
492 PDO_ERR_VSAFE5V_NOT_FIRST,
493 PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
494 PDO_ERR_FIXED_NOT_SORTED,
495 PDO_ERR_VARIABLE_BATT_NOT_SORTED,
496 PDO_ERR_DUPE_PDO,
497 PDO_ERR_PPS_APDO_NOT_SORTED,
498 PDO_ERR_DUPE_PPS_APDO,
499};
500
501static const char * const pdo_err_msg[] = {
502 [PDO_ERR_NO_VSAFE5V] =
503 " err: source/sink caps should at least have vSafe5V",
504 [PDO_ERR_VSAFE5V_NOT_FIRST] =
505 " err: vSafe5V Fixed Supply Object Shall always be the first object",
506 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
507 " err: PDOs should be in the following order: Fixed; Battery; Variable",
508 [PDO_ERR_FIXED_NOT_SORTED] =
509 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
510 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
511 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
512 [PDO_ERR_DUPE_PDO] =
513 " err: Variable/Batt supply pdos cannot have same min/max voltage",
514 [PDO_ERR_PPS_APDO_NOT_SORTED] =
515 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
516 [PDO_ERR_DUPE_PPS_APDO] =
517 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
518};
519
520static enum pdo_err tcpm_caps_err(struct udevice *dev, const u32 *pdo,
521 unsigned int nr_pdo)
522{
523 unsigned int i;
524
525 /* Should at least contain vSafe5v */
526 if (nr_pdo < 1)
527 return PDO_ERR_NO_VSAFE5V;
528
529 /* The vSafe5V Fixed Supply Object Shall always be the first object */
530 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
531 pdo_fixed_voltage(pdo[0]) != VSAFE5V)
532 return PDO_ERR_VSAFE5V_NOT_FIRST;
533
534 for (i = 1; i < nr_pdo; i++) {
535 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
536 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
537 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
538 enum pd_pdo_type type = pdo_type(pdo[i]);
539
540 switch (type) {
541 /*
542 * The remaining Fixed Supply Objects, if
543 * present, shall be sent in voltage order;
544 * lowest to highest.
545 */
546 case PDO_TYPE_FIXED:
547 if (pdo_fixed_voltage(pdo[i]) <=
548 pdo_fixed_voltage(pdo[i - 1]))
549 return PDO_ERR_FIXED_NOT_SORTED;
550 break;
551 /*
552 * The Battery Supply Objects and Variable
553 * supply, if present shall be sent in Minimum
554 * Voltage order; lowest to highest.
555 */
556 case PDO_TYPE_VAR:
557 case PDO_TYPE_BATT:
558 if (pdo_min_voltage(pdo[i]) <
559 pdo_min_voltage(pdo[i - 1]))
560 return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
561 else if ((pdo_min_voltage(pdo[i]) ==
562 pdo_min_voltage(pdo[i - 1])) &&
563 (pdo_max_voltage(pdo[i]) ==
564 pdo_max_voltage(pdo[i - 1])))
565 return PDO_ERR_DUPE_PDO;
566 break;
567 /*
568 * The Programmable Power Supply APDOs, if present,
569 * shall be sent in Maximum Voltage order;
570 * lowest to highest.
571 */
572 case PDO_TYPE_APDO:
573 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
574 break;
575
576 if (pdo_pps_apdo_max_voltage(pdo[i]) <
577 pdo_pps_apdo_max_voltage(pdo[i - 1]))
578 return PDO_ERR_PPS_APDO_NOT_SORTED;
579 else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
580 pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
581 pdo_pps_apdo_max_voltage(pdo[i]) ==
582 pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
583 pdo_pps_apdo_max_current(pdo[i]) ==
584 pdo_pps_apdo_max_current(pdo[i - 1]))
585 return PDO_ERR_DUPE_PPS_APDO;
586 break;
587 default:
588 dev_err(dev, "TCPM: Unknown pdo type\n");
589 }
590 }
591 }
592
593 return PDO_NO_ERR;
594}
595
596static int tcpm_validate_caps(struct udevice *dev, const u32 *pdo,
597 unsigned int nr_pdo)
598{
599 enum pdo_err err_index = tcpm_caps_err(dev, pdo, nr_pdo);
600
601 if (err_index != PDO_NO_ERR) {
602 dev_err(dev, "TCPM:%s\n", pdo_err_msg[err_index]);
603 return -EINVAL;
604 }
605
606 return 0;
607}
608
609/*
610 * PD (data, control) command handling functions
611 */
612static inline enum tcpm_state ready_state(struct tcpm_port *port)
613{
614 if (port->pwr_role == TYPEC_SOURCE)
615 return SRC_READY;
616 else
617 return SNK_READY;
618}
619
620static void tcpm_pd_data_request(struct udevice *dev,
621 const struct pd_message *msg)
622{
623 enum pd_data_msg_type type = pd_header_type_le(msg->header);
624 struct tcpm_port *port = dev_get_uclass_plat(dev);
625 unsigned int cnt = pd_header_cnt_le(msg->header);
626 unsigned int rev = pd_header_rev_le(msg->header);
627 unsigned int i;
628
629 switch (type) {
630 case PD_DATA_SOURCE_CAP:
631 for (i = 0; i < cnt; i++)
632 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
633
634 port->nr_source_caps = cnt;
635
636 tcpm_validate_caps(dev, port->source_caps,
637 port->nr_source_caps);
638
639 /*
640 * Adjust revision in subsequent message headers, as required,
641 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
642 * support Rev 1.0 so just do nothing in that scenario.
643 */
644 if (rev == PD_REV10)
645 break;
646
647 if (rev < PD_MAX_REV)
648 port->negotiated_rev = rev;
649
650 if ((pdo_type(port->source_caps[0]) == PDO_TYPE_FIXED) &&
651 (port->source_caps[0] & PDO_FIXED_DUAL_ROLE) &&
652 (port->source_caps[0] & PDO_FIXED_DATA_SWAP)) {
653 /* Dual role power and data, eg: self-powered Type-C */
654 port->wait_dr_swap_message = true;
655 } else {
656 /* Non-Dual role power, eg: adapter */
657 port->wait_dr_swap_message = false;
658 }
659
660 /*
661 * This message may be received even if VBUS is not
662 * present. This is quite unexpected; see USB PD
663 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
664 * However, at the same time, we must be ready to
665 * receive this message and respond to it 15ms after
666 * receiving PS_RDY during power swap operations, no matter
667 * if VBUS is available or not (USB PD specification,
668 * section 6.5.9.2).
669 * So we need to accept the message either way,
670 * but be prepared to keep waiting for VBUS after it was
671 * handled.
672 */
673 tcpm_set_state(dev, SNK_NEGOTIATE_CAPABILITIES, 0);
674 break;
675 case PD_DATA_REQUEST:
676 /*
677 * Adjust revision in subsequent message headers, as required,
678 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
679 * support Rev 1.0 so just reject in that scenario.
680 */
681 if (rev == PD_REV10) {
682 tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
683 break;
684 }
685
686 if (rev < PD_MAX_REV)
687 port->negotiated_rev = rev;
688
689 port->sink_request = le32_to_cpu(msg->payload[0]);
690
691 tcpm_set_state(dev, SRC_NEGOTIATE_CAPABILITIES, 0);
692 break;
693 case PD_DATA_SINK_CAP:
694 /* We don't do anything with this at the moment... */
695 for (i = 0; i < cnt; i++)
696 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
697
698 port->nr_sink_caps = cnt;
699 break;
700 default:
701 break;
702 }
703}
704
705static void tcpm_pd_ctrl_request(struct udevice *dev,
706 const struct pd_message *msg)
707{
708 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
709 struct tcpm_port *port = dev_get_uclass_plat(dev);
710 enum tcpm_state next_state;
711
712 switch (type) {
713 case PD_CTRL_GOOD_CRC:
714 case PD_CTRL_PING:
715 break;
716 case PD_CTRL_GET_SOURCE_CAP:
717 switch (port->state) {
718 case SRC_READY:
719 case SNK_READY:
720 tcpm_queue_message(dev, PD_MSG_DATA_SOURCE_CAP);
721 break;
722 default:
723 tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
724 break;
725 }
726 break;
727 case PD_CTRL_GET_SINK_CAP:
728 switch (port->state) {
729 case SRC_READY:
730 case SNK_READY:
731 tcpm_queue_message(dev, PD_MSG_DATA_SINK_CAP);
732 break;
733 default:
734 tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
735 break;
736 }
737 break;
738 case PD_CTRL_GOTO_MIN:
739 break;
740 case PD_CTRL_PS_RDY:
741 switch (port->state) {
742 case SNK_TRANSITION_SINK:
743 if (port->vbus_present) {
744 tcpm_set_current_limit(dev,
745 port->req_current_limit,
746 port->req_supply_voltage);
747 port->explicit_contract = true;
748 tcpm_set_state(dev, SNK_READY, 0);
749 } else {
750 /*
751 * Seen after power swap. Keep waiting for VBUS
752 * in a transitional state.
753 */
754 tcpm_set_state(dev,
755 SNK_TRANSITION_SINK_VBUS, 0);
756 }
757 break;
758 default:
759 break;
760 }
761 break;
762 case PD_CTRL_REJECT:
763 case PD_CTRL_WAIT:
764 case PD_CTRL_NOT_SUPP:
765 switch (port->state) {
766 case SNK_NEGOTIATE_CAPABILITIES:
767 /* USB PD specification, Figure 8-43 */
768 if (port->explicit_contract)
769 next_state = SNK_READY;
770 else
771 next_state = SNK_WAIT_CAPABILITIES;
772
773 tcpm_set_state(dev, next_state, 0);
774 break;
775 default:
776 break;
777 }
778 break;
779 case PD_CTRL_ACCEPT:
780 switch (port->state) {
781 case SNK_NEGOTIATE_CAPABILITIES:
782 tcpm_set_state(dev, SNK_TRANSITION_SINK, 0);
783 break;
784 case SOFT_RESET_SEND:
785 port->message_id = 0;
786 port->rx_msgid = -1;
787 if (port->pwr_role == TYPEC_SOURCE)
788 next_state = SRC_SEND_CAPABILITIES;
789 else
790 next_state = SNK_WAIT_CAPABILITIES;
791 tcpm_set_state(dev, next_state, 0);
792 break;
793 default:
794 break;
795 }
796 break;
797 case PD_CTRL_SOFT_RESET:
798 tcpm_set_state(dev, SOFT_RESET, 0);
799 break;
800 case PD_CTRL_DR_SWAP:
801 if (port->port_type != TYPEC_PORT_DRP) {
802 tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
803 break;
804 }
805 /*
806 * 6.3.9: If an alternate mode is active, a request to swap
807 * alternate modes shall trigger a port reset.
808 */
809 switch (port->state) {
810 case SRC_READY:
811 case SNK_READY:
812 tcpm_set_state(dev, DR_SWAP_ACCEPT, 0);
813 break;
814 default:
815 tcpm_queue_message(dev, PD_MSG_CTRL_WAIT);
816 break;
817 }
818 break;
819 case PD_CTRL_PR_SWAP:
820 case PD_CTRL_VCONN_SWAP:
821 case PD_CTRL_GET_SOURCE_CAP_EXT:
822 case PD_CTRL_GET_STATUS:
823 case PD_CTRL_FR_SWAP:
824 case PD_CTRL_GET_PPS_STATUS:
825 case PD_CTRL_GET_COUNTRY_CODES:
826 /* Currently not supported */
827 dev_err(dev, "TCPM: Currently not supported type %#x\n", type);
828 tcpm_queue_message(dev, PD_MSG_CTRL_NOT_SUPP);
829 break;
830 default:
831 dev_err(dev, "TCPM: Unrecognized ctrl message type %#x\n", type);
832 break;
833 }
834}
835
836static void tcpm_pd_rx_handler(struct udevice *dev,
837 const struct pd_message *msg)
838{
839 struct tcpm_port *port = dev_get_uclass_plat(dev);
840 unsigned int cnt = pd_header_cnt_le(msg->header);
841 bool remote_is_host, local_is_host;
842
843 dev_dbg(dev, "TCPM: PD RX, header: %#x [%d]\n",
844 le16_to_cpu(msg->header), port->attached);
845
846 if (port->attached) {
847 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
848 unsigned int msgid = pd_header_msgid_le(msg->header);
849
850 /*
851 * USB PD standard, 6.6.1.2:
852 * "... if MessageID value in a received Message is the
853 * same as the stored value, the receiver shall return a
854 * GoodCRC Message with that MessageID value and drop
855 * the Message (this is a retry of an already received
856 * Message). Note: this shall not apply to the Soft_Reset
857 * Message which always has a MessageID value of zero."
858 */
859 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
860 return;
861 port->rx_msgid = msgid;
862
863 /*
864 * If both ends believe to be DFP/host, we have a data role
865 * mismatch.
866 */
867 remote_is_host = !!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE);
868 local_is_host = port->data_role == TYPEC_HOST;
869 if (remote_is_host == local_is_host) {
870 dev_err(dev, "TCPM: data role mismatch, initiating error recovery\n");
871 tcpm_set_state(dev, ERROR_RECOVERY, 0);
872 } else {
873 if (cnt)
874 tcpm_pd_data_request(dev, msg);
875 else
876 tcpm_pd_ctrl_request(dev, msg);
877 }
878 }
879}
880
881void tcpm_pd_receive(struct udevice *dev, const struct pd_message *msg)
882{
883 tcpm_reset_event_cnt(dev);
884 tcpm_pd_rx_handler(dev, msg);
885}
886
887static int tcpm_pd_send_control(struct udevice *dev,
888 enum pd_ctrl_msg_type type)
889{
890 struct tcpm_port *port = dev_get_uclass_plat(dev);
891 struct pd_message msg;
892
893 memset(&msg, 0, sizeof(msg));
894 msg.header = PD_HEADER_LE(type, port->pwr_role,
895 port->data_role,
896 port->negotiated_rev,
897 port->message_id, 0);
898
899 return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
900}
901
902/*
903 * Send queued message without affecting state.
904 * Return true if state machine should go back to sleep,
905 * false otherwise.
906 */
907static bool tcpm_send_queued_message(struct udevice *dev)
908{
909 struct tcpm_port *port = dev_get_uclass_plat(dev);
910 enum pd_msg_request queued_message;
911 int max_messages = 100;
912
913 do {
914 queued_message = port->queued_message;
915 port->queued_message = PD_MSG_NONE;
916 max_messages--;
917
918 switch (queued_message) {
919 case PD_MSG_CTRL_WAIT:
920 tcpm_pd_send_control(dev, PD_CTRL_WAIT);
921 break;
922 case PD_MSG_CTRL_REJECT:
923 tcpm_pd_send_control(dev, PD_CTRL_REJECT);
924 break;
925 case PD_MSG_CTRL_NOT_SUPP:
926 tcpm_pd_send_control(dev, PD_CTRL_NOT_SUPP);
927 break;
928 case PD_MSG_DATA_SINK_CAP:
929 tcpm_pd_send_sink_caps(dev);
930 break;
931 case PD_MSG_DATA_SOURCE_CAP:
932 tcpm_pd_send_source_caps(dev);
933 break;
934 default:
935 break;
936 }
937 } while (max_messages > 0 && port->queued_message != PD_MSG_NONE);
938
939 if (!max_messages)
940 dev_err(dev, "Aborted sending of too many queued messages\n");
941
942 return false;
943}
944
945static int tcpm_pd_check_request(struct udevice *dev)
946{
947 struct tcpm_port *port = dev_get_uclass_plat(dev);
948 u32 pdo, rdo = port->sink_request;
949 unsigned int max, op, pdo_max, index;
950 enum pd_pdo_type type;
951
952 index = rdo_index(rdo);
953 if (!index || index > port->nr_src_pdo)
954 return -EINVAL;
955
956 pdo = port->src_pdo[index - 1];
957 type = pdo_type(pdo);
958 switch (type) {
959 case PDO_TYPE_FIXED:
960 case PDO_TYPE_VAR:
961 max = rdo_max_current(rdo);
962 op = rdo_op_current(rdo);
963 pdo_max = pdo_max_current(pdo);
964
965 if (op > pdo_max)
966 return -EINVAL;
967 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
968 return -EINVAL;
969
970 if (type == PDO_TYPE_FIXED)
971 dev_dbg(dev, "TCPM: Requested %u mV, %u mA for %u / %u mA\n",
972 pdo_fixed_voltage(pdo), pdo_max, op, max);
973 else
974 dev_dbg(dev, "TCPM: Requested %u -> %u mV, %u mA for %u / %u mA\n",
975 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
976 pdo_max, op, max);
977 break;
978 case PDO_TYPE_BATT:
979 max = rdo_max_power(rdo);
980 op = rdo_op_power(rdo);
981 pdo_max = pdo_max_power(pdo);
982
983 if (op > pdo_max)
984 return -EINVAL;
985 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
986 return -EINVAL;
987 dev_info(dev, "TCPM: Requested %u -> %u mV, %u mW for %u / %u mW\n",
988 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
989 pdo_max, op, max);
990 break;
991 default:
992 return -EINVAL;
993 }
994
995 return 0;
996}
997
998#define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
999#define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
1000
1001static int tcpm_pd_select_pdo(struct udevice *dev, int *sink_pdo,
1002 int *src_pdo)
1003{
1004 struct tcpm_port *port = dev_get_uclass_plat(dev);
1005 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
1006 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
1007 min_snk_mv = 0;
1008 int ret = -EINVAL;
1009
1010 /*
1011 * Select the source PDO providing the most power which has a
1012 * matchig sink cap.
1013 */
1014 for (i = 0; i < port->nr_source_caps; i++) {
1015 u32 pdo = port->source_caps[i];
1016 enum pd_pdo_type type = pdo_type(pdo);
1017
1018 switch (type) {
1019 case PDO_TYPE_FIXED:
1020 max_src_mv = pdo_fixed_voltage(pdo);
1021 min_src_mv = max_src_mv;
1022 break;
1023 case PDO_TYPE_BATT:
1024 case PDO_TYPE_VAR:
1025 max_src_mv = pdo_max_voltage(pdo);
1026 min_src_mv = pdo_min_voltage(pdo);
1027 break;
1028 case PDO_TYPE_APDO:
1029 continue;
1030 default:
1031 dev_err(dev, "TCPM: Invalid source PDO type, ignoring\n");
1032 continue;
1033 }
1034
1035 switch (type) {
1036 case PDO_TYPE_FIXED:
1037 case PDO_TYPE_VAR:
1038 src_ma = pdo_max_current(pdo);
1039 src_mw = src_ma * min_src_mv / 1000;
1040 break;
1041 case PDO_TYPE_BATT:
1042 src_mw = pdo_max_power(pdo);
1043 break;
1044 case PDO_TYPE_APDO:
1045 continue;
1046 default:
1047 dev_err(dev, "TCPM: Invalid source PDO type, ignoring\n");
1048 continue;
1049 }
1050
1051 for (j = 0; j < port->nr_snk_pdo; j++) {
1052 pdo = port->snk_pdo[j];
1053
1054 switch (pdo_type(pdo)) {
1055 case PDO_TYPE_FIXED:
1056 max_snk_mv = pdo_fixed_voltage(pdo);
1057 min_snk_mv = max_snk_mv;
1058 break;
1059 case PDO_TYPE_BATT:
1060 case PDO_TYPE_VAR:
1061 max_snk_mv = pdo_max_voltage(pdo);
1062 min_snk_mv = pdo_min_voltage(pdo);
1063 break;
1064 case PDO_TYPE_APDO:
1065 continue;
1066 default:
1067 dev_err(dev, "TCPM: Invalid sink PDO type, ignoring\n");
1068 continue;
1069 }
1070
1071 if (max_src_mv <= max_snk_mv && min_src_mv >= min_snk_mv) {
1072 /* Prefer higher voltages if available */
1073 if ((src_mw == max_mw && min_src_mv > max_mv) ||
1074 src_mw > max_mw) {
1075 *src_pdo = i;
1076 *sink_pdo = j;
1077 max_mw = src_mw;
1078 max_mv = min_src_mv;
1079 ret = 0;
1080 }
1081 }
1082 }
1083 }
1084
1085 return ret;
1086}
1087
1088static int tcpm_pd_build_request(struct udevice *dev, u32 *rdo)
1089{
1090 struct tcpm_port *port = dev_get_uclass_plat(dev);
1091 unsigned int mv, ma, mw, flags;
1092 unsigned int max_ma, max_mw;
1093 enum pd_pdo_type type;
1094 u32 pdo, matching_snk_pdo;
1095 int src_pdo_index = 0;
1096 int snk_pdo_index = 0;
1097 int ret;
1098
1099 ret = tcpm_pd_select_pdo(dev, &snk_pdo_index, &src_pdo_index);
1100 if (ret < 0)
1101 return ret;
1102
1103 pdo = port->source_caps[src_pdo_index];
1104 matching_snk_pdo = port->snk_pdo[snk_pdo_index];
1105 type = pdo_type(pdo);
1106
1107 switch (type) {
1108 case PDO_TYPE_FIXED:
1109 mv = pdo_fixed_voltage(pdo);
1110 break;
1111 case PDO_TYPE_BATT:
1112 case PDO_TYPE_VAR:
1113 mv = pdo_min_voltage(pdo);
1114 break;
1115 default:
1116 dev_err(dev, "TCPM: Invalid PDO selected!\n");
1117 return -EINVAL;
1118 }
1119
1120 /* Select maximum available current within the sink pdo's limit */
1121 if (type == PDO_TYPE_BATT) {
1122 mw = min_power(pdo, matching_snk_pdo);
1123 ma = 1000 * mw / mv;
1124 } else {
1125 ma = min_current(pdo, matching_snk_pdo);
1126 mw = ma * mv / 1000;
1127 }
1128
1129 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
1130
1131 /* Set mismatch bit if offered power is less than operating power */
1132 max_ma = ma;
1133 max_mw = mw;
1134 if (mw < port->operating_snk_mw) {
1135 flags |= RDO_CAP_MISMATCH;
1136 if (type == PDO_TYPE_BATT &&
1137 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
1138 max_mw = pdo_max_power(matching_snk_pdo);
1139 else if (pdo_max_current(matching_snk_pdo) >
1140 pdo_max_current(pdo))
1141 max_ma = pdo_max_current(matching_snk_pdo);
1142 }
1143
1144 dev_dbg(dev, "TCPM: cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d\n",
1145 port->cc_req, port->cc1, port->cc2, port->vbus_source,
1146 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
1147 port->polarity);
1148
1149 if (type == PDO_TYPE_BATT) {
1150 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
1151
1152 dev_info(dev, "TCPM: requesting PDO %d: %u mV, %u mW%s\n",
1153 src_pdo_index, mv, mw,
1154 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1155 } else {
1156 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
1157
1158 dev_info(dev, "TCPM: requesting PDO %d: %u mV, %u mA%s\n",
1159 src_pdo_index, mv, ma,
1160 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1161 }
1162
1163 port->req_current_limit = ma;
1164 port->req_supply_voltage = mv;
1165
1166 return 0;
1167}
1168
1169static int tcpm_pd_send_request(struct udevice *dev)
1170{
1171 struct tcpm_port *port = dev_get_uclass_plat(dev);
1172 struct pd_message msg;
1173 int ret;
1174 u32 rdo;
1175
1176 ret = tcpm_pd_build_request(dev, &rdo);
1177 if (ret < 0)
1178 return ret;
1179
1180 memset(&msg, 0, sizeof(msg));
1181 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
1182 port->pwr_role,
1183 port->data_role,
1184 port->negotiated_rev,
1185 port->message_id, 1);
1186 msg.payload[0] = cpu_to_le32(rdo);
1187
1188 return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
1189}
1190
1191static int tcpm_set_vbus(struct udevice *dev, bool enable)
1192{
1193 struct tcpm_port *port = dev_get_uclass_plat(dev);
1194 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1195 int ret;
1196
1197 if (enable && port->vbus_charge)
1198 return -EINVAL;
1199
1200 dev_dbg(dev, "TCPM: set vbus = %d charge = %d\n",
1201 enable, port->vbus_charge);
1202
1203 ret = drvops->set_vbus(dev, enable, port->vbus_charge);
1204 if (ret < 0)
1205 return ret;
1206
1207 port->vbus_source = enable;
1208 return 0;
1209}
1210
1211static int tcpm_set_charge(struct udevice *dev, bool charge)
1212{
1213 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1214 struct tcpm_port *port = dev_get_uclass_plat(dev);
1215 int ret;
1216
1217 if (charge && port->vbus_source)
1218 return -EINVAL;
1219
1220 if (charge != port->vbus_charge) {
1221 dev_dbg(dev, "TCPM: set vbus = %d charge = %d\n",
1222 port->vbus_source, charge);
1223 ret = drvops->set_vbus(dev, port->vbus_source,
1224 charge);
1225 if (ret < 0)
1226 return ret;
1227 }
1228 port->vbus_charge = charge;
1229 return 0;
1230}
1231
1232static bool tcpm_start_toggling(struct udevice *dev, enum typec_cc_status cc)
1233{
1234 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1235 struct tcpm_port *port = dev_get_uclass_plat(dev);
1236 int ret;
1237
1238 if (!drvops->start_toggling)
1239 return false;
1240
1241 dev_dbg(dev, "TCPM: Start toggling\n");
1242 ret = drvops->start_toggling(dev, port->port_type, cc);
1243 return ret == 0;
1244}
1245
1246static int tcpm_init_vbus(struct udevice *dev)
1247{
1248 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1249 struct tcpm_port *port = dev_get_uclass_plat(dev);
1250 int ret;
1251
1252 ret = drvops->set_vbus(dev, false, false);
1253 port->vbus_source = false;
1254 port->vbus_charge = false;
1255 return ret;
1256}
1257
1258static int tcpm_init_vconn(struct udevice *dev)
1259{
1260 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1261 struct tcpm_port *port = dev_get_uclass_plat(dev);
1262 int ret;
1263
1264 ret = drvops->set_vconn(dev, false);
1265 port->vconn_role = TYPEC_SINK;
1266 return ret;
1267}
1268
1269static inline void tcpm_typec_connect(struct tcpm_port *port)
1270{
1271 if (!port->connected)
1272 port->connected = true;
1273}
1274
1275static int tcpm_src_attach(struct udevice *dev)
1276{
1277 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1278 struct tcpm_port *port = dev_get_uclass_plat(dev);
1279 enum typec_cc_polarity polarity =
1280 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
1281 : TYPEC_POLARITY_CC1;
1282 int ret;
1283
1284 if (port->attached)
1285 return 0;
1286
1287 ret = tcpm_set_polarity(dev, polarity);
1288 if (ret < 0)
1289 return ret;
1290
1291 ret = tcpm_set_roles(dev, true, TYPEC_SOURCE, TYPEC_HOST);
1292 if (ret < 0)
1293 return ret;
1294
1295 ret = drvops->set_pd_rx(dev, true);
1296 if (ret < 0)
1297 goto out_disable_mux;
1298
1299 /*
1300 * USB Type-C specification, version 1.2,
1301 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
1302 * Enable VCONN only if the non-RD port is set to RA.
1303 */
1304 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
1305 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
1306 ret = tcpm_set_vconn(dev, true);
1307 if (ret < 0)
1308 goto out_disable_pd;
1309 }
1310
1311 ret = tcpm_set_vbus(dev, true);
1312 if (ret < 0)
1313 goto out_disable_vconn;
1314
1315 port->pd_capable = false;
1316
1317 port->partner = NULL;
1318
1319 port->attached = true;
1320
1321 return 0;
1322
1323out_disable_vconn:
1324 tcpm_set_vconn(dev, false);
1325out_disable_pd:
1326 drvops->set_pd_rx(dev, false);
1327out_disable_mux:
1328 dev_err(dev, "TCPM: CC connected in %s as DFP\n",
1329 polarity ? "CC2" : "CC1");
1330 return 0;
1331}
1332
1333static inline void tcpm_typec_disconnect(struct tcpm_port *port)
1334{
1335 if (port->connected) {
1336 port->partner = NULL;
1337 port->connected = false;
1338 }
1339}
1340
1341static void tcpm_reset_port(struct udevice *dev)
1342{
1343 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1344 struct tcpm_port *port = dev_get_uclass_plat(dev);
1345
1346 tcpm_timer_uninit(dev);
1347 tcpm_typec_disconnect(port);
1348 tcpm_reset_event_cnt(dev);
1349 port->wait_dr_swap_message = false;
1350 port->attached = false;
1351 port->pd_capable = false;
1352
1353 /*
1354 * First Rx ID should be 0; set this to a sentinel of -1 so that
1355 * we can check tcpm_pd_rx_handler() if we had seen it before.
1356 */
1357 port->rx_msgid = -1;
1358
1359 drvops->set_pd_rx(dev, false);
1360 tcpm_init_vbus(dev); /* also disables charging */
1361 tcpm_init_vconn(dev);
1362 tcpm_set_current_limit(dev, 0, 0);
1363 tcpm_set_polarity(dev, TYPEC_POLARITY_CC1);
1364 tcpm_set_attached_state(dev, false);
1365 port->nr_sink_caps = 0;
1366}
1367
1368static void tcpm_detach(struct udevice *dev)
1369{
1370 struct tcpm_port *port = dev_get_uclass_plat(dev);
1371
1372 if (tcpm_port_is_disconnected(port))
1373 port->hard_reset_count = 0;
1374
1375 if (!port->attached)
1376 return;
1377
1378 tcpm_reset_port(dev);
1379}
1380
1381static void tcpm_src_detach(struct udevice *dev)
1382{
1383 tcpm_detach(dev);
1384}
1385
1386static int tcpm_snk_attach(struct udevice *dev)
1387{
1388 struct tcpm_port *port = dev_get_uclass_plat(dev);
1389 int ret;
1390
1391 if (port->attached)
1392 return 0;
1393
1394 ret = tcpm_set_polarity(dev, port->cc2 != TYPEC_CC_OPEN ?
1395 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
1396 if (ret < 0)
1397 return ret;
1398
1399 ret = tcpm_set_roles(dev, true, TYPEC_SINK, TYPEC_DEVICE);
1400 if (ret < 0)
1401 return ret;
1402
1403 port->pd_capable = false;
1404
1405 port->partner = NULL;
1406
1407 port->attached = true;
1408 dev_info(dev, "TCPM: CC connected in %s as UFP\n",
1409 port->cc1 != TYPEC_CC_OPEN ? "CC1" : "CC2");
1410
1411 return 0;
1412}
1413
1414static void tcpm_snk_detach(struct udevice *dev)
1415{
1416 tcpm_detach(dev);
1417}
1418
1419static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
1420{
1421 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
1422 return HARD_RESET_SEND;
1423 if (port->pd_capable)
1424 return ERROR_RECOVERY;
1425 if (port->pwr_role == TYPEC_SOURCE)
1426 return SRC_UNATTACHED;
1427 if (port->state == SNK_WAIT_CAPABILITIES)
1428 return SNK_READY;
1429 return SNK_UNATTACHED;
1430}
1431
1432static inline enum tcpm_state unattached_state(struct tcpm_port *port)
1433{
1434 if (port->port_type == TYPEC_PORT_DRP) {
1435 if (port->pwr_role == TYPEC_SOURCE)
1436 return SRC_UNATTACHED;
1437 else
1438 return SNK_UNATTACHED;
1439 } else if (port->port_type == TYPEC_PORT_SRC) {
1440 return SRC_UNATTACHED;
1441 }
1442
1443 return SNK_UNATTACHED;
1444}
1445
1446static void run_state_machine(struct udevice *dev)
1447{
1448 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
1449 struct tcpm_port *port = dev_get_uclass_plat(dev);
1450 int ret;
1451
1452 port->enter_state = port->state;
1453 switch (port->state) {
1454 case TOGGLING:
1455 break;
1456 /* SRC states */
1457 case SRC_UNATTACHED:
1458 tcpm_src_detach(dev);
1459 if (tcpm_start_toggling(dev, tcpm_rp_cc(port))) {
1460 tcpm_set_state(dev, TOGGLING, 0);
1461 break;
1462 }
1463 tcpm_set_cc(dev, tcpm_rp_cc(port));
1464 if (port->port_type == TYPEC_PORT_DRP)
1465 tcpm_set_state(dev, SNK_UNATTACHED, PD_T_DRP_SNK);
1466 break;
1467 case SRC_ATTACH_WAIT:
1468 if (tcpm_port_is_source(port))
1469 tcpm_set_state(dev, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
1470 break;
1471
1472 case SRC_ATTACHED:
1473 ret = tcpm_src_attach(dev);
1474 /*
1475 * Currently, vbus control is not implemented,
1476 * and the SRC detection process cannot be fully implemented.
1477 */
1478 tcpm_set_state(dev, SRC_READY, 0);
1479 break;
1480 case SRC_STARTUP:
1481 port->caps_count = 0;
1482 port->negotiated_rev = PD_MAX_REV;
1483 port->message_id = 0;
1484 port->rx_msgid = -1;
1485 port->explicit_contract = false;
1486 tcpm_set_state(dev, SRC_SEND_CAPABILITIES, 0);
1487 break;
1488 case SRC_SEND_CAPABILITIES:
1489 port->caps_count++;
1490 if (port->caps_count > PD_N_CAPS_COUNT) {
1491 tcpm_set_state(dev, SRC_READY, 0);
1492 break;
1493 }
1494 ret = tcpm_pd_send_source_caps(dev);
1495 if (ret < 0) {
1496 tcpm_set_state(dev, SRC_SEND_CAPABILITIES,
1497 PD_T_SEND_SOURCE_CAP);
1498 } else {
1499 /*
1500 * Per standard, we should clear the reset counter here.
1501 * However, that can result in state machine hang-ups.
1502 * Reset it only in READY state to improve stability.
1503 */
1504 /* port->hard_reset_count = 0; */
1505 port->caps_count = 0;
1506 port->pd_capable = true;
1507 tcpm_set_state_cond(dev, SRC_SEND_CAPABILITIES_TIMEOUT,
1508 PD_T_SEND_SOURCE_CAP);
1509 }
1510 break;
1511 case SRC_SEND_CAPABILITIES_TIMEOUT:
1512 /*
1513 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
1514 *
1515 * PD 2.0 sinks are supposed to accept src-capabilities with a
1516 * 3.0 header and simply ignore any src PDOs which the sink does
1517 * not understand such as PPS but some 2.0 sinks instead ignore
1518 * the entire PD_DATA_SOURCE_CAP message, causing contract
1519 * negotiation to fail.
1520 *
1521 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
1522 * sending src-capabilities with a lower PD revision to
1523 * make these broken sinks work.
1524 */
1525 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
1526 tcpm_set_state(dev, HARD_RESET_SEND, 0);
1527 } else if (port->negotiated_rev > PD_REV20) {
1528 port->negotiated_rev--;
1529 port->hard_reset_count = 0;
1530 tcpm_set_state(dev, SRC_SEND_CAPABILITIES, 0);
1531 } else {
1532 tcpm_set_state(dev, hard_reset_state(port), 0);
1533 }
1534 break;
1535 case SRC_NEGOTIATE_CAPABILITIES:
1536 ret = tcpm_pd_check_request(dev);
1537 if (ret < 0) {
1538 tcpm_pd_send_control(dev, PD_CTRL_REJECT);
1539 if (!port->explicit_contract) {
1540 tcpm_set_state(dev,
1541 SRC_WAIT_NEW_CAPABILITIES, 0);
1542 } else {
1543 tcpm_set_state(dev, SRC_READY, 0);
1544 }
1545 } else {
1546 tcpm_pd_send_control(dev, PD_CTRL_ACCEPT);
1547 tcpm_set_state(dev, SRC_TRANSITION_SUPPLY,
1548 PD_T_SRC_TRANSITION);
1549 }
1550 break;
1551 case SRC_TRANSITION_SUPPLY:
1552 /* XXX: regulator_set_voltage(vbus, ...) */
1553 tcpm_pd_send_control(dev, PD_CTRL_PS_RDY);
1554 port->explicit_contract = true;
1555 tcpm_set_state_cond(dev, SRC_READY, 0);
1556 break;
1557 case SRC_READY:
1558 port->hard_reset_count = 0;
1559
1560 tcpm_typec_connect(port);
1561 break;
1562 case SRC_WAIT_NEW_CAPABILITIES:
1563 /* Nothing to do... */
1564 break;
1565
1566 /* SNK states */
1567 case SNK_UNATTACHED:
1568 tcpm_snk_detach(dev);
1569 if (tcpm_start_toggling(dev, TYPEC_CC_RD)) {
1570 tcpm_set_state(dev, TOGGLING, 0);
1571 break;
1572 }
1573 tcpm_set_cc(dev, TYPEC_CC_RD);
1574 if (port->port_type == TYPEC_PORT_DRP)
1575 tcpm_set_state(dev, SRC_UNATTACHED, PD_T_DRP_SRC);
1576 break;
1577 case SNK_ATTACH_WAIT:
1578 if ((port->cc1 == TYPEC_CC_OPEN &&
1579 port->cc2 != TYPEC_CC_OPEN) ||
1580 (port->cc1 != TYPEC_CC_OPEN &&
1581 port->cc2 == TYPEC_CC_OPEN))
1582 tcpm_set_state(dev, SNK_DEBOUNCED,
1583 PD_T_CC_DEBOUNCE);
1584 else if (tcpm_port_is_disconnected(port))
1585 tcpm_set_state(dev, SNK_UNATTACHED,
1586 PD_T_CC_DEBOUNCE);
1587 break;
1588 case SNK_DEBOUNCED:
1589 if (tcpm_port_is_disconnected(port))
1590 tcpm_set_state(dev, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
1591 else if (port->vbus_present)
1592 tcpm_set_state(dev, SNK_ATTACHED, 0);
1593 else
1594 /* Wait for VBUS, but not forever */
1595 tcpm_set_state(dev, PORT_RESET, PD_T_PS_SOURCE_ON);
1596 break;
1597 case SNK_ATTACHED:
1598 ret = tcpm_snk_attach(dev);
1599 if (ret < 0)
1600 tcpm_set_state(dev, SNK_UNATTACHED, 0);
1601 else
1602 tcpm_set_state(dev, SNK_STARTUP, 0);
1603 break;
1604 case SNK_STARTUP:
1605 port->negotiated_rev = PD_MAX_REV;
1606 port->message_id = 0;
1607 port->rx_msgid = -1;
1608 port->explicit_contract = false;
1609 tcpm_set_state(dev, SNK_DISCOVERY, 0);
1610 break;
1611 case SNK_DISCOVERY:
1612 if (port->vbus_present) {
1613 tcpm_set_current_limit(dev,
1614 tcpm_get_current_limit(port),
1615 5000);
1616 tcpm_set_charge(dev, true);
1617 tcpm_set_state(dev, SNK_WAIT_CAPABILITIES, 0);
1618 break;
1619 }
1620 /*
1621 * For DRP, timeouts differ. Also, handling is supposed to be
1622 * different and much more complex (dead battery detection;
1623 * see USB power delivery specification, section 8.3.3.6.1.5.1).
1624 */
1625 tcpm_set_state(dev, hard_reset_state(port),
1626 port->port_type == TYPEC_PORT_DRP ?
1627 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
1628 break;
1629 case SNK_DISCOVERY_DEBOUNCE:
1630 tcpm_set_state(dev, SNK_DISCOVERY_DEBOUNCE_DONE,
1631 PD_T_CC_DEBOUNCE);
1632 break;
1633 case SNK_DISCOVERY_DEBOUNCE_DONE:
1634 tcpm_set_state(dev, unattached_state(port), 0);
1635 break;
1636 case SNK_WAIT_CAPABILITIES:
1637 ret = drvops->set_pd_rx(dev, true);
1638 if (ret < 0) {
1639 tcpm_set_state(dev, SNK_READY, 0);
1640 break;
1641 }
1642 /*
1643 * If VBUS has never been low, and we time out waiting
1644 * for source cap, try a soft reset first, in case we
1645 * were already in a stable contract before this boot.
1646 * Do this only once.
1647 */
1648 if (port->vbus_never_low) {
1649 port->vbus_never_low = false;
1650 tcpm_set_state(dev, SOFT_RESET_SEND,
1651 PD_T_SINK_WAIT_CAP);
1652 } else {
1653 tcpm_set_state(dev, hard_reset_state(port),
1654 PD_T_SINK_WAIT_CAP);
1655 }
1656 break;
1657 case SNK_NEGOTIATE_CAPABILITIES:
1658 port->pd_capable = true;
1659 port->hard_reset_count = 0;
1660 ret = tcpm_pd_send_request(dev);
1661 if (ret < 0) {
1662 /* Let the Source send capabilities again. */
1663 tcpm_set_state(dev, SNK_WAIT_CAPABILITIES, 0);
1664 } else {
1665 tcpm_set_state_cond(dev, hard_reset_state(port),
1666 PD_T_SENDER_RESPONSE);
1667 }
1668 break;
1669 case SNK_TRANSITION_SINK:
1670 case SNK_TRANSITION_SINK_VBUS:
1671 tcpm_set_state(dev, hard_reset_state(port),
1672 PD_T_PS_TRANSITION);
1673 break;
1674 case SNK_READY:
1675 port->update_sink_caps = false;
1676 tcpm_typec_connect(port);
1677 /*
1678 * Here poll_event_cnt is cleared, waiting for self-powered Type-C devices
1679 * to send DR_swap Messge until 1s (TCPM_POLL_EVENT_TIME_OUT * 500us)timeout
1680 */
1681 if (port->wait_dr_swap_message)
1682 tcpm_reset_event_cnt(dev);
1683
1684 break;
1685
1686 /* Hard_Reset states */
1687 case HARD_RESET_SEND:
1688 tcpm_pd_transmit(dev, TCPC_TX_HARD_RESET, NULL);
1689 tcpm_set_state(dev, HARD_RESET_START, 0);
1690 port->wait_dr_swap_message = false;
1691 break;
1692 case HARD_RESET_START:
1693 port->hard_reset_count++;
1694 drvops->set_pd_rx(dev, false);
1695 port->nr_sink_caps = 0;
1696 if (port->pwr_role == TYPEC_SOURCE)
1697 tcpm_set_state(dev, SRC_HARD_RESET_VBUS_OFF,
1698 PD_T_PS_HARD_RESET);
1699 else
1700 tcpm_set_state(dev, SNK_HARD_RESET_SINK_OFF, 0);
1701 break;
1702 case SRC_HARD_RESET_VBUS_OFF:
1703 tcpm_set_vconn(dev, true);
1704 tcpm_set_vbus(dev, false);
1705 tcpm_set_roles(dev, port->self_powered, TYPEC_SOURCE,
1706 TYPEC_HOST);
1707 tcpm_set_state(dev, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
1708 break;
1709 case SRC_HARD_RESET_VBUS_ON:
1710 tcpm_set_vconn(dev, true);
1711 tcpm_set_vbus(dev, true);
1712 drvops->set_pd_rx(dev, true);
1713 tcpm_set_attached_state(dev, true);
1714 tcpm_set_state(dev, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
1715 break;
1716 case SNK_HARD_RESET_SINK_OFF:
1717 tcpm_set_vconn(dev, false);
1718 if (port->pd_capable)
1719 tcpm_set_charge(dev, false);
1720 tcpm_set_roles(dev, port->self_powered, TYPEC_SINK,
1721 TYPEC_DEVICE);
1722 /*
1723 * VBUS may or may not toggle, depending on the adapter.
1724 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
1725 * directly after timeout.
1726 */
1727 tcpm_set_state(dev, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
1728 break;
1729 case SNK_HARD_RESET_WAIT_VBUS:
1730 /* Assume we're disconnected if VBUS doesn't come back. */
1731 tcpm_set_state(dev, SNK_UNATTACHED,
1732 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
1733 break;
1734 case SNK_HARD_RESET_SINK_ON:
1735 /* Note: There is no guarantee that VBUS is on in this state */
1736 /*
1737 * XXX:
1738 * The specification suggests that dual mode ports in sink
1739 * mode should transition to state PE_SRC_Transition_to_default.
1740 * See USB power delivery specification chapter 8.3.3.6.1.3.
1741 * This would mean to
1742 * - turn off VCONN, reset power supply
1743 * - request hardware reset
1744 * - turn on VCONN
1745 * - Transition to state PE_Src_Startup
1746 * SNK only ports shall transition to state Snk_Startup
1747 * (see chapter 8.3.3.3.8).
1748 * Similar, dual-mode ports in source mode should transition
1749 * to PE_SNK_Transition_to_default.
1750 */
1751 if (port->pd_capable) {
1752 tcpm_set_current_limit(dev,
1753 tcpm_get_current_limit(port),
1754 5000);
1755 tcpm_set_charge(dev, true);
1756 }
1757 tcpm_set_attached_state(dev, true);
1758 tcpm_set_state(dev, SNK_STARTUP, 0);
1759 break;
1760
1761 /* Soft_Reset states */
1762 case SOFT_RESET:
1763 port->message_id = 0;
1764 port->rx_msgid = -1;
1765 tcpm_pd_send_control(dev, PD_CTRL_ACCEPT);
1766 if (port->pwr_role == TYPEC_SOURCE)
1767 tcpm_set_state(dev, SRC_SEND_CAPABILITIES, 0);
1768 else
1769 tcpm_set_state(dev, SNK_WAIT_CAPABILITIES, 0);
1770 break;
1771 case SOFT_RESET_SEND:
1772 port->message_id = 0;
1773 port->rx_msgid = -1;
1774 if (tcpm_pd_send_control(dev, PD_CTRL_SOFT_RESET))
1775 tcpm_set_state_cond(dev, hard_reset_state(port), 0);
1776 else
1777 tcpm_set_state_cond(dev, hard_reset_state(port),
1778 PD_T_SENDER_RESPONSE);
1779 break;
1780
1781 /* DR_Swap states */
1782 case DR_SWAP_ACCEPT:
1783 tcpm_pd_send_control(dev, PD_CTRL_ACCEPT);
1784 tcpm_set_state_cond(dev, DR_SWAP_CHANGE_DR, 0);
1785 break;
1786 case DR_SWAP_CHANGE_DR:
1787 if (port->data_role == TYPEC_HOST) {
1788 tcpm_set_roles(dev, true, port->pwr_role,
1789 TYPEC_DEVICE);
1790 } else {
1791 tcpm_set_roles(dev, true, port->pwr_role,
1792 TYPEC_HOST);
1793 }
1794 /* DR_swap process complete, wait_dr_swap_message is cleared */
1795 port->wait_dr_swap_message = false;
1796 tcpm_set_state(dev, ready_state(port), 0);
1797 break;
1798 case ERROR_RECOVERY:
1799 tcpm_set_state(dev, PORT_RESET, 0);
1800 break;
1801 case PORT_RESET:
1802 tcpm_reset_port(dev);
1803 if (port->self_powered)
1804 tcpm_set_cc(dev, TYPEC_CC_OPEN);
1805 else
1806 tcpm_set_cc(dev, tcpm_default_state(port) == SNK_UNATTACHED ?
1807 TYPEC_CC_RD : tcpm_rp_cc(port));
1808 tcpm_set_state(dev, PORT_RESET_WAIT_OFF,
1809 PD_T_ERROR_RECOVERY);
1810 break;
1811 case PORT_RESET_WAIT_OFF:
1812 tcpm_set_state(dev,
1813 tcpm_default_state(port),
1814 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
1815 break;
1816 default:
1817 dev_err(dev, "TCPM: Unexpected port state %d\n", port->state);
1818 break;
1819 }
1820}
1821
1822static void tcpm_state_machine(struct udevice *dev)
1823{
1824 struct tcpm_port *port = dev_get_uclass_plat(dev);
1825 enum tcpm_state prev_state;
1826
1827 mutex_lock(&port->lock);
1828 port->state_machine_running = true;
1829
1830 if (port->queued_message && tcpm_send_queued_message(dev))
1831 goto done;
1832
1833 /* If we were queued due to a delayed state change, update it now */
1834 if (port->delayed_state) {
1835 dev_dbg(dev, "TCPM: state change %s -> %s [delayed %ld ms]\n",
1836 tcpm_states[port->state],
1837 tcpm_states[port->delayed_state], port->delay_ms);
1838 port->prev_state = port->state;
1839 port->state = port->delayed_state;
1840 port->delayed_state = INVALID_STATE;
1841 }
1842
1843 /*
1844 * Continue running as long as we have (non-delayed) state changes
1845 * to make.
1846 */
1847 do {
1848 prev_state = port->state;
1849 run_state_machine(dev);
1850 if (port->queued_message)
1851 tcpm_send_queued_message(dev);
1852 } while (port->state != prev_state && !port->delayed_state);
1853
1854done:
1855 port->state_machine_running = false;
1856 mutex_unlock(&port->lock);
1857}
1858
1859static void _tcpm_cc_change(struct udevice *dev, enum typec_cc_status cc1,
1860 enum typec_cc_status cc2)
1861{
1862 struct tcpm_port *port = dev_get_uclass_plat(dev);
1863 enum typec_cc_status old_cc1, old_cc2;
1864 enum tcpm_state new_state;
1865
1866 old_cc1 = port->cc1;
1867 old_cc2 = port->cc2;
1868 port->cc1 = cc1;
1869 port->cc2 = cc2;
1870
1871 dev_dbg(dev, "TCPM: CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]\n",
1872 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
1873 port->polarity,
1874 tcpm_port_is_disconnected(port) ? "disconnected" : "connected");
1875
1876 switch (port->state) {
1877 case TOGGLING:
1878 if (tcpm_port_is_source(port))
1879 tcpm_set_state(dev, SRC_ATTACH_WAIT, 0);
1880 else if (tcpm_port_is_sink(port))
1881 tcpm_set_state(dev, SNK_ATTACH_WAIT, 0);
1882 break;
1883 case SRC_UNATTACHED:
1884 case SRC_ATTACH_WAIT:
1885 if (tcpm_port_is_disconnected(port))
1886 tcpm_set_state(dev, SRC_UNATTACHED, 0);
1887 else if (cc1 != old_cc1 || cc2 != old_cc2)
1888 tcpm_set_state(dev, SRC_ATTACH_WAIT, 0);
1889 break;
1890 case SRC_ATTACHED:
1891 case SRC_SEND_CAPABILITIES:
1892 case SRC_READY:
1893 if (tcpm_port_is_disconnected(port) ||
1894 !tcpm_port_is_source(port))
1895 tcpm_set_state(dev, SRC_UNATTACHED, 0);
1896 break;
1897 case SNK_UNATTACHED:
1898 if (tcpm_port_is_sink(port))
1899 tcpm_set_state(dev, SNK_ATTACH_WAIT, 0);
1900 break;
1901 case SNK_ATTACH_WAIT:
1902 if ((port->cc1 == TYPEC_CC_OPEN &&
1903 port->cc2 != TYPEC_CC_OPEN) ||
1904 (port->cc1 != TYPEC_CC_OPEN &&
1905 port->cc2 == TYPEC_CC_OPEN))
1906 new_state = SNK_DEBOUNCED;
1907 else if (tcpm_port_is_disconnected(port))
1908 new_state = SNK_UNATTACHED;
1909 else
1910 break;
1911 if (new_state != port->delayed_state)
1912 tcpm_set_state(dev, SNK_ATTACH_WAIT, 0);
1913 break;
1914 case SNK_DEBOUNCED:
1915 if (tcpm_port_is_disconnected(port))
1916 new_state = SNK_UNATTACHED;
1917 else if (port->vbus_present)
1918 new_state = tcpm_try_src(port) ? INVALID_STATE : SNK_ATTACHED;
1919 else
1920 new_state = SNK_UNATTACHED;
1921 if (new_state != port->delayed_state)
1922 tcpm_set_state(dev, SNK_DEBOUNCED, 0);
1923 break;
1924 case SNK_READY:
1925 if (tcpm_port_is_disconnected(port))
1926 tcpm_set_state(dev, unattached_state(port), 0);
1927 else if (!port->pd_capable &&
1928 (cc1 != old_cc1 || cc2 != old_cc2))
1929 tcpm_set_current_limit(dev,
1930 tcpm_get_current_limit(port),
1931 5000);
1932 break;
1933
1934 case SNK_DISCOVERY:
1935 /* CC line is unstable, wait for debounce */
1936 if (tcpm_port_is_disconnected(port))
1937 tcpm_set_state(dev, SNK_DISCOVERY_DEBOUNCE, 0);
1938 break;
1939 case SNK_DISCOVERY_DEBOUNCE:
1940 break;
1941
1942 case PORT_RESET:
1943 case PORT_RESET_WAIT_OFF:
1944 /*
1945 * State set back to default mode once the timer completes.
1946 * Ignore CC changes here.
1947 */
1948 break;
1949 default:
1950 /*
1951 * While acting as sink and auto vbus discharge is enabled, Allow disconnect
1952 * to be driven by vbus disconnect.
1953 */
1954 if (tcpm_port_is_disconnected(port))
1955 tcpm_set_state(dev, unattached_state(port), 0);
1956 break;
1957 }
1958}
1959
1960static void _tcpm_pd_vbus_on(struct udevice *dev)
1961{
1962 struct tcpm_port *port = dev_get_uclass_plat(dev);
1963
1964 dev_dbg(dev, "TCPM: VBUS on event\n");
1965 port->vbus_present = true;
1966 /*
1967 * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly
1968 * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here.
1969 */
1970 port->vbus_vsafe0v = false;
1971
1972 switch (port->state) {
1973 case SNK_TRANSITION_SINK_VBUS:
1974 port->explicit_contract = true;
1975 tcpm_set_state(dev, SNK_READY, 0);
1976 break;
1977 case SNK_DISCOVERY:
1978 tcpm_set_state(dev, SNK_DISCOVERY, 0);
1979 break;
1980 case SNK_DEBOUNCED:
1981 tcpm_set_state(dev, SNK_ATTACHED, 0);
1982 break;
1983 case SNK_HARD_RESET_WAIT_VBUS:
1984 tcpm_set_state(dev, SNK_HARD_RESET_SINK_ON, 0);
1985 break;
1986 case SRC_ATTACHED:
1987 tcpm_set_state(dev, SRC_STARTUP, 0);
1988 break;
1989 case SRC_HARD_RESET_VBUS_ON:
1990 tcpm_set_state(dev, SRC_STARTUP, 0);
1991 break;
1992
1993 case PORT_RESET:
1994 case PORT_RESET_WAIT_OFF:
1995 /*
1996 * State set back to default mode once the timer completes.
1997 * Ignore vbus changes here.
1998 */
1999 break;
2000
2001 default:
2002 break;
2003 }
2004}
2005
2006static void _tcpm_pd_vbus_off(struct udevice *dev)
2007{
2008 struct tcpm_port *port = dev_get_uclass_plat(dev);
2009
2010 dev_dbg(dev, "TCPM: VBUS off event\n");
2011 port->vbus_present = false;
2012 port->vbus_never_low = false;
2013 switch (port->state) {
2014 case SNK_HARD_RESET_SINK_OFF:
2015 tcpm_set_state(dev, SNK_HARD_RESET_WAIT_VBUS, 0);
2016 break;
2017 case HARD_RESET_SEND:
2018 break;
2019 case SNK_ATTACH_WAIT:
2020 tcpm_set_state(dev, SNK_UNATTACHED, 0);
2021 break;
2022
2023 case SNK_NEGOTIATE_CAPABILITIES:
2024 break;
2025
2026 case PORT_RESET_WAIT_OFF:
2027 tcpm_set_state(dev, tcpm_default_state(port), 0);
2028 break;
2029
2030 case PORT_RESET:
2031 /*
2032 * State set back to default mode once the timer completes.
2033 * Ignore vbus changes here.
2034 */
2035 break;
2036
2037 default:
2038 if (port->pwr_role == TYPEC_SINK && port->attached)
2039 tcpm_set_state(dev, SNK_UNATTACHED, 0);
2040 break;
2041 }
2042}
2043
2044void tcpm_cc_change(struct udevice *dev)
2045{
2046 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
2047 enum typec_cc_status cc1, cc2;
2048
2049 tcpm_reset_event_cnt(dev);
2050 if (drvops->get_cc(dev, &cc1, &cc2) == 0)
2051 _tcpm_cc_change(dev, cc1, cc2);
2052}
2053
2054void tcpm_vbus_change(struct udevice *dev)
2055{
2056 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
2057 bool vbus;
2058
2059 tcpm_reset_event_cnt(dev);
2060 vbus = drvops->get_vbus(dev);
2061 if (vbus)
2062 _tcpm_pd_vbus_on(dev);
2063 else
2064 _tcpm_pd_vbus_off(dev);
2065}
2066
2067void tcpm_pd_hard_reset(struct udevice *dev)
2068{
2069 struct tcpm_port *port = dev_get_uclass_plat(dev);
2070
2071 tcpm_reset_event_cnt(dev);
2072 dev_dbg(dev, "TCPM: Received hard reset\n");
2073
2074 /* If a hard reset message is received during the port reset process,
2075 * we should ignore it, that is, do not set port->state to HARD_RESET_START.
2076 */
2077 if (port->state == PORT_RESET || port->state == PORT_RESET_WAIT_OFF)
2078 return;
2079
2080 /*
2081 * If we keep receiving hard reset requests, executing the hard reset
2082 * must have failed. Revert to error recovery if that happens.
2083 */
2084 tcpm_set_state(dev,
2085 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
2086 HARD_RESET_START : ERROR_RECOVERY,
2087 0);
2088}
2089
2090static void tcpm_init(struct udevice *dev)
2091{
2092 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
2093 struct tcpm_port *port = dev_get_uclass_plat(dev);
2094 enum typec_cc_status cc1, cc2;
2095
2096 drvops->init(dev);
2097
2098 tcpm_reset_port(dev);
2099
2100 /*
2101 * XXX
2102 * Should possibly wait for VBUS to settle if it was enabled locally
2103 * since tcpm_reset_port() will disable VBUS.
2104 */
2105 port->vbus_present = drvops->get_vbus(dev);
2106 if (port->vbus_present)
2107 port->vbus_never_low = true;
2108
2109 /*
2110 * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V.
2111 * So implicitly vbus_vsafe0v = false.
2112 *
2113 * 2. When vbus_present is false and TCPC does NOT support querying
2114 * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e.
2115 * vbus_vsafe0v is true.
2116 *
2117 * 3. When vbus_present is false and TCPC does support querying vsafe0v,
2118 * then, query tcpc for vsafe0v status.
2119 */
2120 if (port->vbus_present)
2121 port->vbus_vsafe0v = false;
2122 else
2123 port->vbus_vsafe0v = true;
2124
2125 tcpm_set_state(dev, tcpm_default_state(port), 0);
2126
2127 if (drvops->get_cc(dev, &cc1, &cc2) == 0)
2128 _tcpm_cc_change(dev, cc1, cc2);
2129}
2130
2131static int tcpm_fw_get_caps(struct udevice *dev)
2132{
2133 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
2134 struct tcpm_port *port = dev_get_uclass_plat(dev);
2135 ofnode node;
2136 const char *cap_str;
2137 int ret;
2138 u32 mw;
2139
2140 ret = drvops->get_connector_node(dev, &node);
2141 if (ret)
2142 return ret;
2143
2144 cap_str = ofnode_read_string(node, "power-role");
2145 if (!cap_str)
2146 return -EINVAL;
2147
2148 if (!strcmp("dual", cap_str))
2149 port->typec_type = TYPEC_PORT_DRP;
2150 else if (!strcmp("source", cap_str))
2151 port->typec_type = TYPEC_PORT_SRC;
2152 else if (!strcmp("sink", cap_str))
2153 port->typec_type = TYPEC_PORT_SNK;
2154 else
2155 return -EINVAL;
2156
2157 port->port_type = port->typec_type;
2158
2159 if (port->port_type == TYPEC_PORT_SNK)
2160 goto sink;
2161
2162 /* Get source pdos */
2163 ret = ofnode_read_size(node, "source-pdos") / sizeof(u32);
2164 if (ret <= 0)
2165 return -EINVAL;
2166
2167 port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
2168 ret = ofnode_read_u32_array(node, "source-pdos",
2169 port->src_pdo, port->nr_src_pdo);
2170 if (ret || tcpm_validate_caps(dev, port->src_pdo, port->nr_src_pdo))
2171 return -EINVAL;
2172
2173 if (port->port_type == TYPEC_PORT_SRC)
2174 return 0;
2175
2176 /* Get the preferred power role for DRP */
2177 cap_str = ofnode_read_string(node, "try-power-role");
2178 if (!cap_str)
2179 return -EINVAL;
2180
2181 if (!strcmp("sink", cap_str))
2182 port->typec_prefer_role = TYPEC_SINK;
2183 else if (!strcmp("source", cap_str))
2184 port->typec_prefer_role = TYPEC_SOURCE;
2185 else
2186 return -EINVAL;
2187
2188 if (port->typec_prefer_role < 0)
2189 return -EINVAL;
2190sink:
2191 /* Get sink pdos */
2192 ret = ofnode_read_size(node, "sink-pdos") / sizeof(u32);
2193 if (ret <= 0)
2194 return -EINVAL;
2195
2196 port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
2197 ret = ofnode_read_u32_array(node, "sink-pdos",
2198 port->snk_pdo, port->nr_snk_pdo);
2199 if (ret || tcpm_validate_caps(dev, port->snk_pdo, port->nr_snk_pdo))
2200 return -EINVAL;
2201
2202 if (ofnode_read_u32_array(node, "op-sink-microwatt", &mw, 1))
2203 return -EINVAL;
2204 port->operating_snk_mw = mw / 1000;
2205
2206 port->self_powered = ofnode_read_bool(node, "self-powered");
2207
2208 return 0;
2209}
2210
2211static int tcpm_port_init(struct udevice *dev)
2212{
2213 struct tcpm_port *port = dev_get_uclass_plat(dev);
2214 int err;
2215
2216 err = tcpm_fw_get_caps(dev);
2217 if (err < 0) {
2218 dev_err(dev, "TCPM: please check the dts config: %d\n", err);
2219 return err;
2220 }
2221
2222 port->try_role = port->typec_prefer_role;
2223 port->port_type = port->typec_type;
2224
2225 tcpm_init(dev);
2226
2227 dev_info(dev, "TCPM: init finished\n");
2228
2229 return 0;
2230}
2231
2232static void tcpm_poll_event(struct udevice *dev)
2233{
2234 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
2235 struct tcpm_port *port = dev_get_uclass_plat(dev);
2236
2237 if (!drvops->get_vbus(dev))
2238 return;
2239
2240 while (port->poll_event_cnt < TCPM_POLL_EVENT_TIME_OUT) {
2241 if (!port->wait_dr_swap_message &&
2242 (port->state == SNK_READY || port->state == SRC_READY))
2243 break;
2244
2245 drvops->poll_event(dev);
2246 port->poll_event_cnt++;
2247 udelay(500);
2248 tcpm_check_and_run_delayed_work(dev);
2249 }
2250
2251 if (port->state != SNK_READY && port->state != SRC_READY)
2252 dev_warn(dev, "TCPM: exit in state %s\n",
2253 tcpm_states[port->state]);
2254
2255 /*
2256 * At this time, call the callback function of the respective pd chip
2257 * to enter the low-power mode. In order to reduce the time spent on
2258 * the PD chip driver as much as possible, the tcpm framework does not
2259 * fully process the communication initiated by the device,so it should
2260 * be noted that we can disable the internal oscillator, etc., but do
2261 * not turn off the power of the transceiver module, otherwise the
2262 * self-powered Type-C device will initiate a Message(eg: self-powered
2263 * Type-C hub initiates a SINK capability request(PD_CTRL_GET_SINK_CAP))
2264 * and the pd chip cannot reply to GoodCRC, causing the self-powered Type-C
2265 * device to switch vbus to vSafe5v, or even turn off vbus.
2266 */
2267 if (!drvops->enter_low_power_mode)
2268 return;
2269
2270 if (drvops->enter_low_power_mode(dev, port->attached, port->pd_capable))
2271 dev_err(dev, "TCPM: failed to enter low power\n");
2272 else
2273 dev_info(dev, "TCPM: PD chip enter low power mode\n");
2274}
2275
2276int tcpm_post_probe(struct udevice *dev)
2277{
2278 int ret = tcpm_port_init(dev);
2279
2280 if (ret < 0) {
2281 dev_err(dev, "failed to tcpm port init\n");
2282 return ret;
2283 }
2284
2285 tcpm_poll_event(dev);
2286
2287 return 0;
2288}