summaryrefslogtreecommitdiff
path: root/driver
diff options
context:
space:
mode:
Diffstat (limited to 'driver')
-rw-r--r--driver/build.mk3
-rw-r--r--driver/mcdp28x0.c149
-rw-r--r--driver/mcdp28x0.h56
3 files changed, 208 insertions, 0 deletions
diff --git a/driver/build.mk b/driver/build.mk
index 2d5de551a8..923be6ac1a 100644
--- a/driver/build.mk
+++ b/driver/build.mk
@@ -53,3 +53,6 @@ driver-$(CONFIG_USB_SWITCH_TSU6721)+=usb_switch_tsu6721.o
# Firmware Update
driver-$(CONFIG_SB_FIRMWARE_UPDATE)+=battery/sb_fw_update.o
+
+# video converters
+driver-$(CONFIG_MCDP28X0)+=mcdp28x0.o
diff --git a/driver/mcdp28x0.c b/driver/mcdp28x0.c
new file mode 100644
index 0000000000..de334b5c59
--- /dev/null
+++ b/driver/mcdp28x0.c
@@ -0,0 +1,149 @@
+/* Copyright (c) 2014 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.
+ *
+ * Megachips DisplayPort to HDMI protocol converter / level shifter driver.
+ */
+
+#include "config.h"
+#include "console.h"
+#include "common.h"
+#include "mcdp28x0.h"
+#include "timer.h"
+#include "usart-stm32f0.h"
+#include "util.h"
+
+#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
+
+#undef MCDP_DEBUG
+
+#ifdef MCDP_DEBUG
+static inline void print_buffer(uint8_t *buf, int cnt)
+{
+ int i;
+ CPRINTF("buf:");
+ for (i = 0; i < cnt; i++) {
+ if (i && !(i % 4))
+ CPRINTF("\n ");
+ CPRINTF("[%02d]0x%02x ", i, buf[i]);
+ }
+ CPRINTF("\n");
+}
+#else
+static inline void print_buffer(uint8_t *buf, int cnt) {}
+#endif
+
+USART_CONFIG(usart_mcdp, CONFIG_MCDP28X0, 115200, MCDP_INBUF_MAX,
+ MCDP_OUTBUF_MAX, NULL, NULL);
+
+/**
+ * Compute checksum.
+ *
+ * @msg message bytes to compute checksum on.
+ * @cnt count of message bytes.
+ * @return partial checksum.
+ */
+static uint8_t compute_checksum(const uint8_t *msg, int cnt)
+{
+ int i;
+ /* 1st byte (not in msg) is always cnt + 2, so seed chksum with that */
+ uint8_t chksum = cnt + 2;
+
+ for (i = 0; i < cnt; i++)
+ chksum += msg[i];
+ return ~chksum + 1;
+}
+
+/**
+ * transmit message over serial
+ *
+ * Packet consists of:
+ * msg[0] == length including this byte + cnt + chksum
+ * msg[1] == 1st message byte
+ * msg[cnt+1] == last message byte
+ * msg[cnt+2] == checksum
+ *
+ * @msg message bytes
+ * @cnt count of message bytes
+ * @return zero if success, error code otherwise
+ */
+static int tx_serial(const uint8_t *msg, int cnt)
+{
+ uint8_t out = cnt + 2;
+ uint8_t chksum = compute_checksum(msg, cnt);
+
+ if (out_stream_write(&usart_mcdp.out, &out, 1) != 1)
+ return EC_ERROR_UNKNOWN;
+
+ if (out_stream_write(&usart_mcdp.out, msg, cnt) != cnt)
+ return EC_ERROR_UNKNOWN;
+
+ if (out_stream_write(&usart_mcdp.out, &chksum, 1) != 1)
+ return EC_ERROR_UNKNOWN;
+
+ print_buffer(usart_mcdp_tx_buffer, cnt + 2);
+
+ return EC_SUCCESS;
+}
+
+/**
+ * receive message over serial
+ *
+ * @msg pointer to buffer to read message into
+ * @cnt count of message bytes
+ * @return zero if success, error code otherwise
+ */
+static int rx_serial(uint8_t *msg, int cnt)
+{
+ size_t read;
+ int retry = 2;
+
+ read = in_stream_read(&usart_mcdp.in, msg, cnt);
+ while ((read < cnt) && retry) {
+ usleep(100*MSEC);
+ read += in_stream_read(&usart_mcdp.in, msg + read,
+ cnt - read);
+ retry--;
+ }
+
+ print_buffer(msg, cnt);
+
+ return !(read == cnt);
+}
+
+void mcdp_enable(void)
+{
+ usart_init(&usart_mcdp);
+}
+
+void mcdp_disable(void)
+{
+ usart_shutdown(&usart_mcdp);
+}
+
+int mcdp_get_info(struct mcdp_info *info)
+{
+ uint8_t inbuf[MCDP_RSP_LEN(MCDP_LEN_GETINFO)];
+ const uint8_t msg[2] = {MCDP_CMD_GETINFO, 0x00}; /* cmd + msg type */
+
+ if (tx_serial(msg, sizeof(msg)))
+ return EC_ERROR_UNKNOWN;
+
+ if (rx_serial(inbuf, sizeof(inbuf)))
+ return EC_ERROR_UNKNOWN;
+
+ /* check length & returned command ... no checksum provided */
+ if (((inbuf[0] - 1) != sizeof(inbuf)) ||
+ (inbuf[1] != MCDP_CMD_GETINFO))
+ return EC_ERROR_UNKNOWN;
+
+ memcpy(info, &inbuf[2], MCDP_LEN_GETINFO);
+
+#ifdef MCDP_DEBUG
+ CPRINTF("family:%04x chipid:%04x irom:%d.%d.%d fw:%d.%d.%d\n",
+ MCDP_FAMILY(info->family), MCDP_CHIPID(info->chipid),
+ info->irom.major, info->irom.minor, info->irom.build,
+ info->fw.major, info->fw.minor, info->fw.build);
+#endif
+ return EC_SUCCESS;
+}
diff --git a/driver/mcdp28x0.h b/driver/mcdp28x0.h
new file mode 100644
index 0000000000..c753816495
--- /dev/null
+++ b/driver/mcdp28x0.h
@@ -0,0 +1,56 @@
+/* Copyright (c) 2014 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.
+ *
+ * Megachips DisplayPort to HDMI protocol converter / level shifter driver.
+ */
+
+#ifndef MCDP28X0_H
+#define MCDP28X0_H
+
+#include <util.h>
+
+#define MCDP_OUTBUF_MAX 16
+#define MCDP_INBUF_MAX 16
+
+#define MCDP_CHIPID(chipid) ((chipid[0] << 8) | chipid[1])
+#define MCDP_FAMILY(family) ((family[0] << 8) | family[1])
+
+#define MCDP_CMD_GETINFO 0x40
+#define MCDP_LEN_GETINFO 12
+
+/* length byte + cmd byte + data len */
+#define MCDP_RSP_LEN(len) (len + 2)
+
+struct mcdp_version {
+ uint8_t major;
+ uint8_t minor;
+ uint16_t build;
+};
+
+struct mcdp_info {
+ uint8_t family[2];
+ uint8_t chipid[2];
+ struct mcdp_version irom;
+ struct mcdp_version fw;
+};
+
+/**
+ * Enable mcdp driver.
+ */
+void mcdp_enable(void);
+
+/**
+ * Disable mcdp driver.
+ */
+void mcdp_disable(void);
+
+/**
+ * get get information command from mcdp.
+ *
+ * @info pointer to mcdp_info structure
+ * @return zero if success, error code otherwise.
+ */
+int mcdp_get_info(struct mcdp_info *info);
+
+#endif