summaryrefslogtreecommitdiff
path: root/driver/retimer/ps8818.c
blob: 6059f7f0d4d907c7070cf6f644a2f06757441d3b (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
/* 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.
 *
 * PS8818 retimer.
 */

#include "chipset.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "i2c.h"
#include "ioexpander.h"
#include "ps8818.h"
#include "usb_mux.h"

#define PS8818_DEBUG 0

int ps8818_i2c_read(int port, int page, int offset, int *data)
{
	int rv;

	rv = i2c_read8(usb_retimers[port].i2c_port,
		       usb_retimers[port].i2c_addr_flags + page,
		       offset, data);

	if (PS8818_DEBUG)
		ccprintf("%s(%d:0x%02X, 0x%02X) => 0x%02X\n", __func__,
			 usb_retimers[port].i2c_port,
			 usb_retimers[port].i2c_addr_flags + page,
			 offset, *data);

	return rv;
}

int ps8818_i2c_write(int port, int page, int offset, int data)
{
	if (PS8818_DEBUG)
		ccprintf("%s(%d:0x%02X, 0x%02X, 0x%02X)\n", __func__,
			 usb_retimers[port].i2c_port,
			 usb_retimers[port].i2c_addr_flags + page,
			 offset, data);

	return i2c_write8(usb_retimers[port].i2c_port,
			  usb_retimers[port].i2c_addr_flags + page,
			  offset, data);
}

int ps8818_detect(int port)
{
	int rv = EC_ERROR_NOT_POWERED;
	int val;

	/* Detected if we are powered and can read the device */
	if (!chipset_in_state(CHIPSET_STATE_HARD_OFF))
		rv = ps8818_i2c_read(port,
				     PS8818_REG_PAGE0,
				     PS8818_REG0_FLIP,
				     &val);

	return rv;
}

static int ps8818_set_mux(int port, mux_state_t mux_state)
{
	int rv;
	int val = 0;

	if (chipset_in_state(CHIPSET_STATE_HARD_OFF))
		return (mux_state == USB_PD_MUX_NONE) ? EC_SUCCESS
						      : EC_ERROR_NOT_POWERED;

	if (PS8818_DEBUG)
		ccprintf("%s(%d, 0x%02X) %s %s %s\n",
			 __func__, 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 */
	if (mux_state & USB_PD_MUX_USB_ENABLED)
		val |= PS8818_MODE_USB_ENABLE;
	if (mux_state & USB_PD_MUX_DP_ENABLED)
		val |= PS8818_MODE_DP_ENABLE;

	rv = ps8818_i2c_write(port,
			      PS8818_REG_PAGE0,
			      PS8818_REG0_MODE,
			      val);
	if (rv)
		return rv;

	/* Set the flip */
	val = 0;
	if (mux_state & USB_PD_MUX_POLARITY_INVERTED)
		val |= PS8818_FLIP_CONFIG;

	rv = ps8818_i2c_write(port,
			      PS8818_REG_PAGE0,
			      PS8818_REG0_FLIP,
			      val);
	if (rv)
		return rv;

	/* Set the IN_HPD */
	val = PS8818_DPHPD_CONFIG_INHPD_DISABLE;
	if (mux_state & USB_PD_MUX_DP_ENABLED)
		val |= PS8818_DPHPD_PLUGGED;

	rv = ps8818_i2c_write(port,
			      PS8818_REG_PAGE0,
			      PS8818_REG0_DPHPD_CONFIG,
			      val);
	if (rv)
		return rv;

	/* Board specific retimer mux tuning */
	if (usb_retimers[port].tune) {
		rv = usb_retimers[port].tune(port, mux_state);
		if (rv)
			return rv;
	}

	if (PS8818_DEBUG) {
		int tx_status;
		int rx_status;

		rv = ps8818_i2c_read(port,
				     PS8818_REG_PAGE2,
				     PS8818_REG2_TX_STATUS,
				     &tx_status);
		if (rv)
			return rv;

		rv = ps8818_i2c_read(port,
				     PS8818_REG_PAGE2,
				     PS8818_REG2_RX_STATUS,
				     &rx_status);
		if (rv)
			return rv;

		ccprintf("%s: tx:channel %snormal %s10Gbps\n",
			 __func__,
			 (tx_status & PS8818_STATUS_NORMAL_OPERATION)
								? "" : "NOT-",
			 (tx_status & PS8818_STATUS_10_GBPS)	? "" : "NON-");
		ccprintf("%s: rx:channel %snormal %s10Gbps\n",
			 __func__,
			 (rx_status & PS8818_STATUS_NORMAL_OPERATION)
								? "" : "NOT-",
			 (rx_status & PS8818_STATUS_10_GBPS)	? "" : "NON-");
	}

	return rv;
}

const struct usb_retimer_driver ps8818_usb_retimer = {
	.set = ps8818_set_mux,
};