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
|
/* Copyright 2019 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.
*
* PS8802 retimer.
*/
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "i2c.h"
#include "ps8802.h"
#include "timer.h"
#include "usb_mux.h"
#define PS8802_DEBUG 0
#define PS8802_I2C_WAKE_DELAY 500
int ps8802_i2c_read(const struct usb_mux *me, int page, int offset, int *data)
{
int rv;
rv = i2c_read8(me->i2c_port,
me->i2c_addr_flags + page,
offset, data);
if (PS8802_DEBUG)
ccprintf("%s(%d:0x%02X, 0x%02X) =>0x%02X\n", __func__,
me->i2c_port,
me->i2c_addr_flags + page,
offset, *data);
return rv;
}
int ps8802_i2c_write(const struct usb_mux *me, int page, int offset, int data)
{
int rv;
int pre_val, post_val;
if (PS8802_DEBUG)
i2c_read8(me->i2c_port,
me->i2c_addr_flags + page,
offset, &pre_val);
rv = i2c_write8(me->i2c_port,
me->i2c_addr_flags + page,
offset, data);
if (PS8802_DEBUG) {
i2c_read8(me->i2c_port,
me->i2c_addr_flags + page,
offset, &post_val);
ccprintf("%s(%d:0x%02X, 0x%02X, 0x%02X) "
"0x%02X=>0x%02X\n",
__func__,
me->i2c_port,
me->i2c_addr_flags + page,
offset, data,
pre_val, post_val);
}
return rv;
}
int ps8802_i2c_write16(const struct usb_mux *me, int page, int offset,
int data)
{
int rv;
int pre_val, post_val;
if (PS8802_DEBUG)
i2c_read16(me->i2c_port,
me->i2c_addr_flags + page,
offset, &pre_val);
rv = i2c_write16(me->i2c_port,
me->i2c_addr_flags + page,
offset, data);
if (PS8802_DEBUG) {
i2c_read16(me->i2c_port,
me->i2c_addr_flags + page,
offset, &post_val);
ccprintf("%s(%d:0x%02X, 0x%02X, 0x%04X) "
"0x%04X=>0x%04X\n",
__func__,
me->i2c_port,
me->i2c_addr_flags + page,
offset, data,
pre_val, post_val);
}
return rv;
}
int ps8802_i2c_field_update8(const struct usb_mux *me, int page, int offset,
uint8_t field_mask, uint8_t set_value)
{
int rv;
int pre_val, post_val;
if (PS8802_DEBUG)
i2c_read8(me->i2c_port,
me->i2c_addr_flags + page,
offset, &pre_val);
rv = i2c_field_update8(me->i2c_port,
me->i2c_addr_flags + page,
offset,
field_mask,
set_value);
if (PS8802_DEBUG) {
i2c_read8(me->i2c_port,
me->i2c_addr_flags + page,
offset, &post_val);
ccprintf("%s(%d:0x%02X, 0x%02X, 0x%02X, 0x%02X) "
"0x%02X=>0x%02X\n",
__func__,
me->i2c_port,
me->i2c_addr_flags + page,
offset, field_mask, set_value,
pre_val, post_val);
}
return rv;
}
int ps8802_i2c_field_update16(const struct usb_mux *me, int page, int offset,
uint16_t field_mask, uint16_t set_value)
{
int rv;
int pre_val, post_val;
if (PS8802_DEBUG)
i2c_read16(me->i2c_port,
me->i2c_addr_flags + page,
offset, &pre_val);
rv = i2c_field_update16(me->i2c_port,
me->i2c_addr_flags + page,
offset,
field_mask,
set_value);
if (PS8802_DEBUG) {
i2c_read16(me->i2c_port,
me->i2c_addr_flags + page,
offset, &post_val);
ccprintf("%s(%d:0x%02X, 0x%02X, 0x%02X, 0x%04X) "
"0x%04X=>0x%04X\n",
__func__,
me->i2c_port,
me->i2c_addr_flags + page,
offset, field_mask, set_value,
pre_val, post_val);
}
return rv;
}
/*
* If PS8802 is in I2C standby mode, wake it up by reading PS8802_REG_MODE.
* From Application Note: 1) Activate by reading any Page 2 register. 2) Wait
* 500 microseconds. 3) After 5 seconds idle, PS8802 will return to standby.
*/
int ps8802_i2c_wake(const struct usb_mux *me)
{
int data;
int rv = EC_ERROR_UNKNOWN;
/* If in standby, first read will fail, second should succeed. */
for (int i = 0; i < 2; i++) {
rv = ps8802_i2c_read(me,
PS8802_REG_PAGE2,
PS8802_REG2_MODE,
&data);
if (rv == EC_SUCCESS)
return rv;
usleep(PS8802_I2C_WAKE_DELAY);
}
return rv;
}
static int ps8802_init(const struct usb_mux *me)
{
return EC_SUCCESS;
}
static int ps8802_set_mux(const struct usb_mux *me, mux_state_t mux_state)
{
int val;
int rv;
if (chipset_in_state(CHIPSET_STATE_HARD_OFF))
return (mux_state == USB_PD_MUX_NONE) ? EC_SUCCESS
: EC_ERROR_NOT_POWERED;
/* Make sure the PS8802 is awake */
rv = ps8802_i2c_wake(me);
if (rv)
return rv;
if (PS8802_DEBUG)
ccprintf("%s(%d, 0x%02X) %s %s %s\n",
__func__, me->usb_port, mux_state,
(mux_state & USB_PD_MUX_USB_ENABLED) ? "USB" : "",
(mux_state & USB_PD_MUX_DP_ENABLED) ? "DP" : "",
(mux_state & USB_PD_MUX_POLARITY_INVERTED)
? "FLIP" : "");
/* Set the mode and flip */
val = (PS8802_MODE_DP_REG_CONTROL |
PS8802_MODE_USB_REG_CONTROL |
PS8802_MODE_FLIP_REG_CONTROL |
PS8802_MODE_IN_HPD_REG_CONTROL);
if (mux_state & USB_PD_MUX_USB_ENABLED)
val |= PS8802_MODE_USB_ENABLE;
if (mux_state & USB_PD_MUX_DP_ENABLED)
val |= PS8802_MODE_DP_ENABLE | PS8802_MODE_IN_HPD_ENABLE;
if (mux_state & USB_PD_MUX_POLARITY_INVERTED)
val |= PS8802_MODE_FLIP_ENABLE;
rv = ps8802_i2c_write(me,
PS8802_REG_PAGE2,
PS8802_REG2_MODE,
val);
return rv;
}
static int ps8802_get_mux(const struct usb_mux *me, mux_state_t *mux_state)
{
int rv;
int val;
*mux_state = USB_PD_MUX_NONE;
if (chipset_in_state(CHIPSET_STATE_HARD_OFF))
return EC_ERROR_NOT_POWERED;
rv = ps8802_i2c_wake(me);
if (rv)
return rv;
rv = ps8802_i2c_read(me,
PS8802_REG_PAGE2,
PS8802_REG2_MODE,
&val);
if (rv)
return rv;
if (val & PS8802_MODE_USB_ENABLE)
*mux_state |= USB_PD_MUX_USB_ENABLED;
if (val & PS8802_MODE_DP_ENABLE)
*mux_state |= USB_PD_MUX_DP_ENABLED;
if (val & PS8802_MODE_FLIP_ENABLE)
*mux_state |= USB_PD_MUX_POLARITY_INVERTED;
return rv;
}
const struct usb_mux_driver ps8802_usb_mux_driver = {
.init = ps8802_init,
.set = ps8802_set_mux,
.get = ps8802_get_mux,
};
|