summaryrefslogtreecommitdiffstats
path: root/i2c_helper.h
diff options
context:
space:
mode:
authorShiyu Sun <sshiyu@google.com>2020-03-19 16:59:52 +1100
committerEdward O'Callaghan <quasisec@chromium.org>2020-04-01 06:05:01 +0000
commitdc2c83bbc7dcf071af1a9ad80192fa1f80f09b14 (patch)
treed6a9af270befbc19734bc0781170d831d231ced0 /i2c_helper.h
parentfe6b98b91ccff0b96351a41d662933361fe29ebe (diff)
downloadflashrom-dc2c83bbc7dcf071af1a9ad80192fa1f80f09b14.tar.gz
flashrom-dc2c83bbc7dcf071af1a9ad80192fa1f80f09b14.tar.bz2
flashrom-dc2c83bbc7dcf071af1a9ad80192fa1f80f09b14.zip
i2c_helper: Add support for the Linux I2C subsystem
See https://www.kernel.org/doc/Documentation/i2c/ for details. This creates common interface for I2C access functions, and adds implementation for linux I2C functions. BUG=b:148746232 BRANCH=none TEST=build success Signed-off-by: Shiyu Sun <sshiyu@chromium.org> Change-Id: Ie0487824dfb71970bede17f617dbbb30ddf78c12 Reviewed-on: https://review.coreboot.org/c/flashrom/+/39686 Tested-by: Edward O'Callaghan <quasisec@chromium.org> Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Diffstat (limited to 'i2c_helper.h')
-rw-r--r--i2c_helper.h112
1 files changed, 112 insertions, 0 deletions
diff --git a/i2c_helper.h b/i2c_helper.h
new file mode 100644
index 000000000..599e08bbd
--- /dev/null
+++ b/i2c_helper.h
@@ -0,0 +1,112 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2020 The Chromium OS Authors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef I2C_HELPER_H
+#define I2C_HELPER_H
+
+#include <inttypes.h>
+
+/**
+ * An convinent structure that contains the buffer size and the buffer
+ * pointer. Used to wrap buffer details while doing the I2C data
+ * transfer on both input and output. It is the client's responsibility
+ * to use i2c_buffer_t_fill to initialize this struct instead of
+ * trying to construct it directly.
+ */
+typedef struct {
+ void *buf;
+ uint16_t len;
+} i2c_buffer_t;
+
+/**
+ * i2c_buffer_t_fill - fills in the i2c_buffer_t
+ *
+ * @i2c_buf: pointer to the be constructed.
+ * @buf: buffer contains data to be included in i2c_buffer_t.
+ * @len: length of buffer to be included in i2c_buffer_t.
+ *
+ * This function takes in a pointer to an initialized i2c_buffer_t
+ * object with related information, and fill in the i2c_buffer_t with
+ * some validation applied. The function does allow initialization with
+ * NULL buffer but will make sure len == 0 in such case.
+ *
+ * returns 0 on success, <0 to indicate failure
+ */
+static inline int i2c_buffer_t_fill(i2c_buffer_t *i2c_buf, void *buf, uint16_t len)
+{
+ if (!i2c_buf || (!buf && len))
+ return -1;
+
+ i2c_buf->buf = buf;
+ i2c_buf->len = len;
+
+ return 0;
+}
+
+/**
+ * i2c_open - opens the target I2C device and set the I2C slave address
+ *
+ * @bus: I2C bus number of the target device.
+ * @addr: I2C slave address.
+ * @force: whether to force set the I2C slave address.
+ *
+ * This function returns a file descriptor for the target device. It is
+ * the client's responsibility to pass the return value to i2c_close to
+ * clean up.
+ *
+ * returns fd of target device on success, <0 to indicate failure
+ */
+int i2c_open(int bus, uint16_t addr, int force);
+
+/**
+ * i2c_close - closes the file descriptor returned by i2c_open
+ *
+ * @fd: file descriptor to be closed.
+ *
+ * It is the client's responsibility to set fd = -1 when it is
+ * done with it.
+ *
+ * returns 0 on success, <0 to indicate failure
+ */
+int i2c_close(int fd);
+
+/**
+ * i2c_read - reads data from the I2C device
+ *
+ * @fd: file descriptor of the target device.
+ * @addr: I2C slave address of the target device.
+ * @buf_read: data struct includes reading buffer and size.
+ *
+ * This function does accept empty read and do nothing on such case.
+ *
+ * returns read length on success, <0 to indicate failure
+ */
+int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf_read);
+
+/**
+ * i2c_write - writes command/data into the I2C device
+ *
+ * @fd: file descriptor of the target device.
+ * @addr: I2C slave address of the target device.
+ * @buf_write: data struct includes writting buffer and size.
+ *
+ * This function does accept empty write and do nothing on such case.
+ *
+ * returns wrote length on success, <0 to indicate failure.
+ */
+int i2c_write(int fd, uint16_t addr, const i2c_buffer_t *buf_write);
+
+#endif /* !I2C_HELPER_H */