summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Yilun Lin <yllin@chromium.org>2022-01-18 11:33:38 +0800
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-04-28 03:56:58 +0000
commit95d12b1d877b3c0ac61f361cb81ed30e6342c26f (patch)
treedd530867264991f753089984d8dc7f602c1d49b2
parent1f2a66dffa1481e0e302f3453be4f67ce8448b83 (diff)
downloadchrome-ec-95d12b1d877b3c0ac61f361cb81ed30e6342c26f.tar.gz
tcpm/anx7477: support TCPCI get_vbus_voltage
Fix anx7447_get_vbus_voltage which was using raw ADC value, and the new implementation let it report the real reading in mV, and move the common VBUS calculation to the header so both tcpci and anx7447 can benefit from it. Also, unguarded the function by dropping VBUS_DETECT_TCPC macro. BUG=b:203739613 TEST=uart:~$ vbus VBUS C0 = 20200 mV VBUS C1 = 0 mV BRANCH=none Change-Id: I61a8eecf1f77a7c87b2ef435af6809821388e66a Signed-off-by: Eric Yilun Lin <yllin@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3397145 Tested-by: Eric Yilun Lin <yllin@google.com> Auto-Submit: Eric Yilun Lin <yllin@google.com> Reviewed-by: Ting Shen <phoenixshen@chromium.org> Commit-Queue: Eric Yilun Lin <yllin@google.com>
-rw-r--r--driver/tcpm/anx7447.c51
-rw-r--r--driver/tcpm/tcpci.c12
-rw-r--r--include/driver/tcpm/tcpci.h14
3 files changed, 52 insertions, 25 deletions
diff --git a/driver/tcpm/anx7447.c b/driver/tcpm/anx7447.c
index 76177af886..d025191c6d 100644
--- a/driver/tcpm/anx7447.c
+++ b/driver/tcpm/anx7447.c
@@ -19,16 +19,8 @@
#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args)
-#define vsafe5v_min (3800/25)
-#define vsafe0v_max (800/25)
-/*
- * These interface are workable while ADC is enabled, before
- * calling them should make sure ec driver finished chip initilization.
- */
-#define is_equal_greater_safe5v(port) \
- (((anx7447_get_vbus_voltage(port))) > vsafe5v_min)
-#define is_equal_greater_safe0v(port) \
- (((anx7447_get_vbus_voltage(port))) > vsafe0v_max)
+#define VSAFE5V_MIN 3800
+#define VSAFE0V_MAX 800
struct anx_state {
uint16_t i2c_addr_flags;
@@ -403,14 +395,44 @@ static int anx7447_release(int port)
return EC_SUCCESS;
}
+static int anx7447_get_vbus_voltage(int port, int *vbus)
+{
+ int val;
+ int error;
+
+ /*
+ * b:214893572#comment33: This function is partially copied from
+ * tcpci_get_vbus_voltage because ANX7447 dev_cap_1 reports VBUS_MEASURE
+ * unsupported, however, it actually does. So we have an identical
+ * implementation but just skip the dev_cap_1 check.
+ */
+
+ error = tcpc_read16(port, TCPC_REG_VBUS_VOLTAGE, &val);
+ if (error)
+ return error;
+
+ *vbus = TCPC_REG_VBUS_VOLTAGE_VBUS(val);
+ return EC_SUCCESS;
+}
+
#ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC
-static int anx7447_get_vbus_voltage(int port)
+/*
+ * This interface are workable while ADC is enabled, before
+ * calling them should make sure ec driver finished chip initialization.
+ */
+static bool is_equal_greater_safe0v(int port)
{
- int vbus_volt = 0;
+ int vbus;
+ int error;
+
+ error = anx7447_get_vbus_voltage(port, &vbus);
+ if (error)
+ return true;
- tcpc_read16(port, TCPC_REG_VBUS_VOLTAGE, &vbus_volt);
+ if (vbus > VSAFE0V_MAX)
+ return true;
- return vbus_volt;
+ return false;
}
int anx7447_set_power_supply_ready(int port)
@@ -839,6 +861,7 @@ const struct tcpm_drv anx7447_tcpm_drv = {
#ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC
.check_vbus_level = &tcpci_tcpm_check_vbus_level,
#endif
+ .get_vbus_voltage = &anx7447_get_vbus_voltage,
.select_rp_value = &tcpci_tcpm_select_rp_value,
.set_cc = &anx7447_set_cc,
.set_polarity = &anx7447_set_polarity,
diff --git a/driver/tcpm/tcpci.c b/driver/tcpm/tcpci.c
index 735ababffe..42e7bb357a 100644
--- a/driver/tcpm/tcpci.c
+++ b/driver/tcpm/tcpci.c
@@ -1334,7 +1334,6 @@ void tcpci_tcpc_alert(int port)
int tcpci_get_vbus_voltage(int port, int *vbus)
{
int error, val;
- int scale, measure;
if (!(dev_cap_1[port] & TCPC_REG_DEV_CAP_1_VBUS_MEASURE_ALARM_CAPABLE))
return EC_ERROR_UNIMPLEMENTED;
@@ -1343,16 +1342,7 @@ int tcpci_get_vbus_voltage(int port, int *vbus)
if (error)
return error;
- /*
- * 00: the measurement is not scaled
- * 01: the measurement is divided by 2
- * 10: the measurement is divided by 4
- * 11: reserved
- */
- scale = (val & TCPC_REG_VBUS_VOLTAGE_SCALE_FACTOR) >> 9;
- measure = val & TCPC_REG_VBUS_VOLTAGE_MEASUREMENT;
-
- *vbus = (1 << scale) * measure * TCPC_REG_VBUS_VOLTAGE_LSB;
+ *vbus = TCPC_REG_VBUS_VOLTAGE_VBUS(val);
return EC_SUCCESS;
}
diff --git a/include/driver/tcpm/tcpci.h b/include/driver/tcpm/tcpci.h
index 04016d0f06..6eebb074b0 100644
--- a/include/driver/tcpm/tcpci.h
+++ b/include/driver/tcpm/tcpci.h
@@ -281,6 +281,20 @@
#define TCPC_REG_VBUS_VOLTAGE_SCALE_FACTOR GENMASK(11, 10)
#define TCPC_REG_VBUS_VOLTAGE_LSB 25
+/*
+ * 00: the measurement is not scaled
+ * 01: the measurement is divided by 2
+ * 10: the measurement is divided by 4
+ * 11: reserved
+ */
+#define TCPC_REG_VBUS_VOLTAGE_SCALE(x) \
+ (1 << (((x) & TCPC_REG_VBUS_VOLTAGE_SCALE_FACTOR) >> 9))
+#define TCPC_REG_VBUS_VOLTAGE_MEASURE(x) \
+ ((x) & TCPC_REG_VBUS_VOLTAGE_MEASUREMENT)
+#define TCPC_REG_VBUS_VOLTAGE_VBUS(x) \
+ (TCPC_REG_VBUS_VOLTAGE_SCALE(x) * TCPC_REG_VBUS_VOLTAGE_MEASURE(x) * \
+ TCPC_REG_VBUS_VOLTAGE_LSB)
+
#define TCPC_REG_VBUS_SINK_DISCONNECT_THRESH 0x72
#define TCPC_REG_VBUS_SINK_DISCONNECT_THRESH_DEFAULT 0x008C /* 3.5 V */