summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Palatin <vpalatin@chromium.org>2012-11-19 18:06:46 -0800
committerGerrit <chrome-bot@google.com>2012-12-13 09:01:09 -0800
commite927d1506a51ce975a47d44b20c45a49b5b0e59c (patch)
treef2f9307144a5da6bfa6d4c931b18559b4ebc0d83
parent243e935ad471b8ad706a4a2989f0b986f39b9a63 (diff)
downloadchrome-ec-e927d1506a51ce975a47d44b20c45a49b5b0e59c.tar.gz
spring: add basic TSU6721 driver
for now, just control the USB pins muxing. Signed-off-by: Vincent Palatin <vpalatin@chromium.org> BRANCH=none BUG=chrome-os-partner:14318 TEST=on Spring, put the EC UART on the micro-B connector and read it using a modified FTDI cable. Change-Id: Ib0c87e483fb0bbe1835bd6ea008176b88d6f12f8 Reviewed-on: https://gerrit.chromium.org/gerrit/38361 Commit-Ready: Vincent Palatin <vpalatin@chromium.org> Reviewed-by: Vincent Palatin <vpalatin@chromium.org> Tested-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r--board/spring/board.h3
-rw-r--r--common/build.mk1
-rw-r--r--common/tsu6721.c143
-rw-r--r--include/tsu6721.h44
4 files changed, 191 insertions, 0 deletions
diff --git a/board/spring/board.h b/board/spring/board.h
index 192eddc550..e4c0e27e64 100644
--- a/board/spring/board.h
+++ b/board/spring/board.h
@@ -55,6 +55,9 @@
/* Battery */
#define CONFIG_BATTERY_BQ20Z453
+/* Charger/accessories detection */
+#define CONFIG_TSU6721
+
/* Timer selection */
#define TIM_CLOCK_MSB 2
#define TIM_CLOCK_LSB 4
diff --git a/common/build.mk b/common/build.mk
index a115d84651..ee73575399 100644
--- a/common/build.mk
+++ b/common/build.mk
@@ -33,4 +33,5 @@ common-$(CONFIG_TASK_VBOOTHASH)+=sha256.o vboot_hash.o
common-$(CONFIG_TASK_X86POWER)+=x86_power.o
common-$(CONFIG_TEMP_SENSOR)+=temp_sensor.o
common-$(CONFIG_TMP006)+=tmp006.o
+common-$(CONFIG_TSU6721)+=tsu6721.o
common-$(CONFIG_USB_CHARGE)+=usb_charge.o
diff --git a/common/tsu6721.c b/common/tsu6721.c
new file mode 100644
index 0000000000..babd9c862b
--- /dev/null
+++ b/common/tsu6721.c
@@ -0,0 +1,143 @@
+/* Copyright (c) 2012 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.
+ *
+ * TI TSU6721 USB port switch driver.
+ */
+
+#include "board.h"
+#include "console.h"
+#include "hooks.h"
+#include "i2c.h"
+#include "timer.h"
+#include "tsu6721.h"
+#include "uart.h"
+#include "util.h"
+
+/* Console output macros */
+#define CPUTS(outstr) cputs(CC_USBCHARGE, outstr)
+#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args)
+
+/* 8-bit I2C address */
+#define TSU6721_I2C_ADDR (0x25 << 1)
+
+uint8_t tsu6721_read(uint8_t reg)
+{
+ int res;
+ int val;
+
+ res = i2c_read8(I2C_PORT_HOST, TSU6721_I2C_ADDR, reg, &val);
+ if (res)
+ return 0xee;
+
+ return val;
+}
+
+void tsu6721_write(uint8_t reg, uint8_t val)
+{
+ int res;
+
+ res = i2c_write8(I2C_PORT_HOST, TSU6721_I2C_ADDR, reg, val);
+ if (res)
+ CPRINTF("[%T TSU6721 I2C write failed]\n");
+}
+
+int tsu6721_mux(enum tsu6721_mux sel)
+{
+ uint8_t id = tsu6721_read(TSU6721_REG_ADC);
+ uint8_t vbus1 = tsu6721_read(TSU6721_REG_DEV_TYPE1) & 0x74;
+ uint8_t vbus3 = tsu6721_read(TSU6721_REG_DEV_TYPE3) & 0x74;
+ uint8_t ctrl = tsu6721_read(TSU6721_REG_CONTROL);
+
+ /*
+ * silicon limitation: the chip stays in low power mode and cannot
+ * activate manual mode if it is not detecting either a VBUS or
+ * something known on the ID pin
+ */
+ if ((id == 0x1f) && !vbus1 && !vbus3) {
+ CPRINTF("TSU6721 cannot use manual mode: no VBUS or ID\n");
+ return EC_ERROR_INVAL;
+ }
+
+ if (sel == TSU6721_MUX_NONE) {
+ tsu6721_write(TSU6721_REG_CONTROL, ctrl | TSU6721_CTRL_AUTO);
+ } else {
+ tsu6721_write(TSU6721_REG_MANUAL1, sel);
+ tsu6721_write(TSU6721_REG_CONTROL, ctrl & ~TSU6721_CTRL_AUTO);
+ }
+
+ return EC_SUCCESS;
+}
+
+/*
+ * TODO(vpalatin): using the I2C early in the HOOK_INIT
+ * currently triggers all sort of badness, I need to debug
+ * this before re-activatin this initialization.
+ */
+#if 0
+static void tsu6721_init(void)
+{
+ uint8_t settings;
+ uint8_t dev_id = tsu6721_read(TSU6721_REG_DEV_ID);
+
+ if (dev_id != 0x0a) {
+ CPRINTF("TSU6721 invalid device ID 0x%02x\n", dev_id);
+ return;
+ }
+
+ /* set USB charger detection timeout to 600ms */
+ settings = tsu6721_read(TSU6721_REG_TIMER);
+ settings = (settings & ~0x38);
+ tsu6721_write(TSU6721_REG_TIMER, settings);
+}
+DECLARE_HOOK(HOOK_INIT, tsu6721_init, HOOK_PRIO_DEFAULT);
+#endif
+
+static void tsu6721_dump(void)
+{
+ int i;
+ uint8_t id = tsu6721_read(TSU6721_REG_ADC);
+ uint8_t ctrl = tsu6721_read(TSU6721_REG_CONTROL);
+
+ if (ctrl & TSU6721_CTRL_AUTO)
+ ccprintf("Auto: %02x %02x %02x\n",
+ tsu6721_read(TSU6721_REG_DEV_TYPE1),
+ tsu6721_read(TSU6721_REG_DEV_TYPE2),
+ tsu6721_read(TSU6721_REG_DEV_TYPE3));
+ else
+ ccprintf("Manual: %02x %02x\n",
+ tsu6721_read(TSU6721_REG_MANUAL1),
+ tsu6721_read(TSU6721_REG_MANUAL2));
+ ccprintf("ID: 0x%02x\n", id);
+ for (i = 1; i < 0x24; i++)
+ ccprintf("%02x ", tsu6721_read(i));
+ ccprintf("\n");
+}
+
+static int command_usbmux(int argc, char **argv)
+{
+ if (1 == argc) { /* dump all registers */
+ tsu6721_dump();
+ return EC_SUCCESS;
+ } else if (2 == argc) {
+ if (!strcasecmp(argv[1], "usb")) {
+ tsu6721_mux(TSU6721_MUX_USB);
+ } else if (!strcasecmp(argv[1], "uart1")) {
+ tsu6721_mux(TSU6721_MUX_UART);
+ } else if (!strcasecmp(argv[1], "uart2")) {
+ tsu6721_mux(TSU6721_MUX_AUDIO);
+ } else if (!strcasecmp(argv[1], "auto")) {
+ tsu6721_mux(TSU6721_MUX_NONE);
+ } else { /* read one register */
+ ccprintf("Invalid mux value: %s\n", argv[1]);
+ return EC_ERROR_INVAL;
+ }
+ return EC_SUCCESS;
+ }
+
+ return EC_ERROR_INVAL;
+}
+DECLARE_CONSOLE_COMMAND(usbmux, command_usbmux,
+ "[usb|uart1|uart2|auto]",
+ "TSU6721 USB mux control",
+ NULL);
diff --git a/include/tsu6721.h b/include/tsu6721.h
new file mode 100644
index 0000000000..cfd9c6aaee
--- /dev/null
+++ b/include/tsu6721.h
@@ -0,0 +1,44 @@
+/* Copyright (c) 2012 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.
+ *
+ * TI TSU6721 USB port switch.
+ */
+
+#ifndef TSU6721_H
+#define TSU6721_H
+
+#define TSU6721_REG_DEV_ID 0x01
+#define TSU6721_REG_CONTROL 0x02
+#define TSU6721_REG_INT1 0x03
+#define TSU6721_REG_INT2 0x04
+#define TSU6721_REG_INT_MASK1 0x05
+#define TSU6721_REG_INT_MASK2 0x06
+#define TSU6721_REG_ADC 0x07
+#define TSU6721_REG_TIMING1 0x08
+#define TSU6721_REG_TIMING2 0x09
+#define TSU6721_REG_DEV_TYPE1 0x0A
+#define TSU6721_REG_DEV_TYPE2 0x0B
+#define TSU6721_REG_BUTTON1 0x0C
+#define TSU6721_REG_BUTTON2 0x0D
+#define TSU6721_REG_MANUAL1 0x13
+#define TSU6721_REG_MANUAL2 0x14
+#define TSU6721_REG_DEV_TYPE3 0x15
+#define TSU6721_REG_RESET 0x1B
+#define TSU6721_REG_TIMER 0x20
+#define TSU6721_REG_OCP1 0x21
+#define TSU6721_REG_OCP2 0x22
+
+#define TSU6721_CTRL_AUTO (1 << 2)
+
+enum tsu6721_mux {
+ TSU6721_MUX_NONE = 0x00,
+ TSU6721_MUX_USB = 0x24,
+ TSU6721_MUX_AUDIO = 0x48,
+ TSU6721_MUX_UART = 0x6C,
+};
+
+uint8_t tsu6721_read(uint8_t reg);
+void tsu6721_write(uint8_t reg, uint8_t val);
+
+#endif /* TSU6721_H */