summaryrefslogtreecommitdiff
path: root/driver/retimer/nb7v904m.c
blob: 81fd5abc93be4e3fcde120563625204132869c39 (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
/* Copyright 2020 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.
 *
 * ON Semiconductor NB7V904M USB Type-C DisplayPort Alt Mode Redriver
 */
#include <stdbool.h>
#include "common.h"
#include "console.h"
#include "ec_commands.h"
#include "i2c.h"
#include "nb7v904m.h"
#include "usb_mux.h"

#define CPRINTS(format, args...) cprints(CC_USB, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_USB, format, ## args)

static int nb7v904m_write(int port, int offset, int data)
{
	return i2c_write8(usb_retimers[port].i2c_port,
			  usb_retimers[port].i2c_addr_flags,
			  offset, data);

}

static int nb7v904m_read(int port, int offset, int *regval)
{
	return i2c_read8(usb_retimers[port].i2c_port,
			 usb_retimers[port].i2c_addr_flags,
			 offset, regval);

}

static int set_low_power_mode(int port, bool enable)
{
	int regval;
	int rv;

	rv = nb7v904m_read(port, NB7V904M_REG_GEN_DEV_SETTINGS, &regval);
	if (rv)
		return rv;

	if (enable)
		regval |= NB7V904M_CHIP_EN;
	else
		regval &= ~NB7V904M_CHIP_EN;

	return nb7v904m_write(port, NB7V904M_REG_GEN_DEV_SETTINGS, regval);
}

static int nb7v904m_enter_low_power_mode(int port)
{
	int rv = set_low_power_mode(port, 1);

	if (rv)
		CPRINTS("C%d: NB7V904M: Failed to enter low power mode!", port);
	return rv;
}

static int nb7v904m_init(int port)
{
	int rv = set_low_power_mode(port, 0);

	if (rv)
		CPRINTS("C%d: NB7V904M: init failed!", port);
	return rv;
}

static int nb7v904m_set_mux(int port, mux_state_t mux_state)
{
	int rv = EC_SUCCESS;
	int regval;
	int flipped = !!(mux_state & USB_PD_MUX_POLARITY_INVERTED);

	/* Turn off redriver if it's not needed at all. */
	if (mux_state == USB_PD_MUX_NONE)
		return nb7v904m_enter_low_power_mode(port);

	rv = nb7v904m_init(port);
	if (rv)
		return rv;

	/* Clear operation mode field */
	rv = nb7v904m_read(port, NB7V904M_REG_GEN_DEV_SETTINGS, &regval);
	if (rv) {
		CPRINTS("C%d %s: Failed to obtain dev settings!", port,
			__func__);
		return rv;
	}
	regval &= ~NB7V904M_OP_MODE_MASK;

	if (mux_state & USB_PD_MUX_USB_ENABLED) {
		/* USB with DP */
		if (mux_state & USB_PD_MUX_DP_ENABLED) {
			if (flipped)
				regval |= NB7V904M_USB_DP_FLIPPED;
			else
				regval |= NB7V904M_USB_DP_NORMAL;
		} else {
			/* USB only */
			regval |= NB7V904M_USB_ONLY;
		}

	} else if (mux_state & USB_PD_MUX_DP_ENABLED) {
		/* 4 lanes DP */
		regval |= NB7V904M_DP_ONLY;
	}

	if (mux_state & USB_PD_MUX_DP_ENABLED) {
		/* Connect AUX */
		rv = nb7v904m_write(port, NB7V904M_REG_AUX_CH_CTRL, flipped ?
				    NB7V904M_AUX_CH_FLIPPED :
				    NB7V904M_AUX_CH_NORMAL);
		/* Enable all channels for DP */
		regval |= NB7V904M_CH_EN_MASK;
	} else {
		/* Disconnect AUX since it's not being used. */
		rv = nb7v904m_write(port, NB7V904M_REG_AUX_CH_CTRL,
				    NB7V904M_AUX_CH_HI_Z);

		/* Disable the unused channels to save power */
		regval &= ~NB7V904M_CH_EN_MASK;
		if (flipped) {
			/* Only enable channels A & B */
			regval |= NB7V904M_CH_A_EN | NB7V904M_CH_B_EN;
		} else {
			/* Only enable channels C & D */
			regval |= NB7V904M_CH_C_EN | NB7V904M_CH_D_EN;
		}
	}

	rv |= nb7v904m_write(port, NB7V904M_REG_GEN_DEV_SETTINGS, regval);
	if (rv)
		CPRINTS("C%d: %s failed!", port, __func__);

	return rv;
}

const struct usb_retimer_driver nb7v904m_usb_redriver_drv = {
	.enter_low_power_mode = &nb7v904m_enter_low_power_mode,
	.init = &nb7v904m_init,
	.set = &nb7v904m_set_mux,
};