blob: d4fe260e0db44be896f0a94ee10ddccbc2f588cf [file] [log] [blame]
Sebastian Reichel3dd23322024-10-15 17:26:43 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2024 Collabora Ltd.
4 *
5 * USB Power Delivery protocol stack.
6 */
7
8#include <dm/device.h>
9#include <dm/device_compat.h>
10#include <dm/uclass.h>
11#include <linux/err.h>
12#include <usb/tcpm.h>
13#include "tcpm-internal.h"
14
15int tcpm_get_voltage(struct udevice *dev)
16{
17 struct tcpm_port *port = dev_get_uclass_plat(dev);
18
19 return port->supply_voltage;
20}
21
22int tcpm_get_current(struct udevice *dev)
23{
24 struct tcpm_port *port = dev_get_uclass_plat(dev);
25
26 return port->current_limit;
27}
28
29enum typec_orientation tcpm_get_orientation(struct udevice *dev)
30{
31 struct tcpm_port *port = dev_get_uclass_plat(dev);
32
33 switch (port->polarity) {
34 case TYPEC_POLARITY_CC1:
35 return TYPEC_ORIENTATION_NORMAL;
36 case TYPEC_POLARITY_CC2:
37 return TYPEC_ORIENTATION_REVERSE;
38 default:
39 return TYPEC_ORIENTATION_NONE;
40 }
41}
42
43const char *tcpm_get_state(struct udevice *dev)
44{
45 struct tcpm_port *port = dev_get_uclass_plat(dev);
46
47 return tcpm_states[port->state];
48}
49
50int tcpm_get_pd_rev(struct udevice *dev)
51{
52 struct tcpm_port *port = dev_get_uclass_plat(dev);
53
54 return port->negotiated_rev;
55}
56
57enum typec_role tcpm_get_pwr_role(struct udevice *dev)
58{
59 struct tcpm_port *port = dev_get_uclass_plat(dev);
60
61 return port->pwr_role;
62}
63
64enum typec_data_role tcpm_get_data_role(struct udevice *dev)
65{
66 struct tcpm_port *port = dev_get_uclass_plat(dev);
67
68 return port->data_role;
69}
70
71bool tcpm_is_connected(struct udevice *dev)
72{
73 struct tcpm_port *port = dev_get_uclass_plat(dev);
74
75 return port->connected;
76}
77
78int tcpm_get(int index, struct udevice **devp)
79{
80 return uclass_get_device(UCLASS_TCPM, index, devp);
81}
82
83static int tcpm_post_bind(struct udevice *dev)
84{
85 const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
86 const char *cap_str;
87 ofnode node;
88 int ret;
89
90 /*
91 * USB Power Delivery (USB PD) specification requires, that communication
92 * with a sink happens within roughly 5 seconds. Otherwise the source
93 * might assume that the sink does not support USB PD. Starting to do
94 * USB PD communication after that results in a hard reset, which briefly
95 * removes any power from the USB-C port.
96 *
97 * On systems with alternative power supplies this is not an issue, but
98 * systems, which get soleley powered through their USB-C port will end
99 * up losing their power supply and doing a board level reset. The hard
100 * reset will also restart the 5 second timeout. That means a operating
101 * system initializing USB PD will put the system into a boot loop when
102 * it takes more than 5 seconds from cold boot to the operating system
103 * starting to transmit USB PD messages.
104 *
105 * The issue can be avoided by doing the initial USB PD communication
106 * in U-Boot. The operating system can then re-negotiate by doing a
107 * soft reset, which does not trigger removal of the supply voltage.
108 *
109 * Since the TCPM state machine is quite complex and depending on the
110 * remote side can take quite some time to finish, this tries to limit
111 * the automatic probing to systems probably relying on power being
112 * provided by the USB-C port(s):
113 *
114 * 1. self-powered devices won't reset when the USB-C port looses power
115 * 2. if the power is allowed to go into anything else than sink mode
116 * it is not the only power source
117 */
118 ret = drvops->get_connector_node(dev, &node);
119 if (ret)
120 return ret;
121
122 if (ofnode_read_bool(node, "self-powered"))
123 return 0;
124
125 cap_str = ofnode_read_string(node, "power-role");
126 if (!cap_str)
127 return -EINVAL;
128
129 if (strcmp("sink", cap_str))
130 return 0;
131
132 /* Do not auto-probe PD controller when PD is disabled */
133 if (ofnode_read_bool(node, "pd-disable"))
134 return 0;
135
136 dev_info(dev, "probing Type-C port manager...");
137
138 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
139
140 return 0;
141}
142
143UCLASS_DRIVER(tcpm) = {
144 .id = UCLASS_TCPM,
145 .name = "tcpm",
146 .per_device_plat_auto = sizeof(struct tcpm_port),
147 .post_bind = tcpm_post_bind,
148 .post_probe = tcpm_post_probe,
149};