summaryrefslogtreecommitdiff
path: root/common/usbc_ppc.c
blob: 0281ceaf647d17f19288aa7a6c85eda249e37a23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* Copyright 2017 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* USB-C Power Path Controller Common Code */

#include "atomic.h"
#include "common.h"
#include "console.h"
#include "hooks.h"
#include "timer.h"
#include "usb_pd.h"
#include "usbc_ppc.h"
#include "util.h"

#ifndef TEST_BUILD
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args)
#else
#define CPRINTF(args...)
#define CPRINTS(args...)
#endif

int ppc_prints(const char *string, int port)
{
#ifndef TEST_BUILD
	return CPRINTS("ppc p%d %s", port, string);
#else
	return 0;
#endif
}

int ppc_err_prints(const char *string, int port, int error)
{
#ifndef TEST_BUILD
	return CPRINTS("ppc p%d %s (%d)", port, string, error);
#else
	return 0;
#endif
}

/* Simple wrappers to dispatch to the drivers. */

int ppc_init(int port)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->init) {
		rv = ppc->drv->init(port);
		if (rv)
			ppc_err_prints("init failed!", port, rv);
		else
			ppc_prints("init'd.", port);
	}

	return rv;
}

int ppc_is_sourcing_vbus(int port)
{
	int rv = 0;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return 0;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->is_sourcing_vbus)
		rv = ppc->drv->is_sourcing_vbus(port);

	return rv;
}

#ifdef CONFIG_USBC_PPC_POLARITY
int ppc_set_polarity(int port, int polarity)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->set_polarity)
		rv = ppc->drv->set_polarity(port, polarity);

	return rv;
}
#endif

int ppc_set_vbus_source_current_limit(int port, enum tcpc_rp_value rp)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->set_vbus_source_current_limit)
		rv = ppc->drv->set_vbus_source_current_limit(port, rp);

	return rv;
}

int ppc_discharge_vbus(int port, int enable)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->discharge_vbus)
		rv = ppc->drv->discharge_vbus(port, enable);

	return rv;
}

#ifdef CONFIG_USBC_PPC_SBU
int ppc_set_sbu(int port, int enable)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->set_sbu)
		rv = ppc->drv->set_sbu(port, enable);

	return rv;
}
#endif /* defined(CONFIG_USBC_PPC_SBU) */

#ifdef CONFIG_USBC_PPC_VCONN
int ppc_set_vconn(int port, int enable)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->set_vconn)
		rv = ppc->drv->set_vconn(port, enable);

	return rv;
}
#endif

int ppc_dev_is_connected(int port, enum ppc_device_role dev)
{
	int rv = EC_SUCCESS;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->dev_is_connected)
		rv = ppc->drv->dev_is_connected(port, dev);

	return rv;
}

int ppc_vbus_sink_enable(int port, int enable)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->vbus_sink_enable)
		rv = ppc->drv->vbus_sink_enable(port, enable);

	return rv;
}

int ppc_enter_low_power_mode(int port)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->enter_low_power_mode)
		rv = ppc->drv->enter_low_power_mode(port);

	return rv;
}

int ppc_vbus_source_enable(int port, int enable)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->vbus_source_enable)
		rv = ppc->drv->vbus_source_enable(port, enable);

	return rv;
}

#ifdef CONFIG_USB_PD_FRS_PPC
int ppc_set_frs_enable(int port, int enable)
{
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];

	if (ppc->drv->set_frs_enable)
		rv = ppc->drv->set_frs_enable(port,enable);

	return rv;
}
#endif /* defined(CONFIG_USB_PD_FRS_PPC) */

#ifdef CONFIG_USB_PD_VBUS_DETECT_PPC
int ppc_is_vbus_present(int port)
{
	int rv = 0;
	const struct ppc_config_t *ppc;

	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return 0;
	}

	ppc = &ppc_chips[port];

	if (ppc->drv->is_vbus_present)
		rv = ppc->drv->is_vbus_present(port);

	return rv;
}
#endif /* defined(CONFIG_USB_PD_VBUS_DETECT_PPC) */

#ifdef CONFIG_CMD_PPC_DUMP
static int command_ppc_dump(int argc, char **argv)
{
	int port;
	int rv = EC_ERROR_UNIMPLEMENTED;
	const struct ppc_config_t *ppc;

	if (argc < 2)
		return EC_ERROR_PARAM_COUNT;

	port = atoi(argv[1]);
	if ((port < 0) || (port >= ppc_cnt)) {
		CPRINTS("%s(%d) Invalid port!", __func__, port);
		return EC_ERROR_INVAL;
	}

	ppc = &ppc_chips[port];
	if (ppc->drv->reg_dump)
		rv = ppc->drv->reg_dump(port);

	return rv;
}
DECLARE_CONSOLE_COMMAND(ppc_dump, command_ppc_dump, "<Type-C port>",
			"dump the PPC regs");
#endif /* defined(CONFIG_CMD_PPC_DUMP) */