diff options
author | Shiyu Sun <sshiyu@google.com> | 2020-03-19 16:59:52 +1100 |
---|---|---|
committer | Edward O'Callaghan <quasisec@chromium.org> | 2020-04-01 06:05:01 +0000 |
commit | dc2c83bbc7dcf071af1a9ad80192fa1f80f09b14 (patch) | |
tree | d6a9af270befbc19734bc0781170d831d231ced0 /i2c_helper_linux.c | |
parent | fe6b98b91ccff0b96351a41d662933361fe29ebe (diff) | |
download | flashrom-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_linux.c')
-rw-r--r-- | i2c_helper_linux.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/i2c_helper_linux.c b/i2c_helper_linux.c new file mode 100644 index 00000000..0703518a --- /dev/null +++ b/i2c_helper_linux.c @@ -0,0 +1,113 @@ +/* + * 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. + */ + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <sys/fcntl.h> +#include <sys/ioctl.h> +#include <unistd.h> + +#include <stdlib.h> +#include <linux/i2c.h> +#include <linux/i2c-dev.h> + +#include "flash.h" +#include "i2c_helper.h" + +#define I2C_DEV_PREFIX "/dev/i2c-" +#define I2C_MAX_BUS 255 + +int i2c_close(int fd) +{ + return fd == -1 ? 0 : close(fd); +} + +int i2c_open(int bus, uint16_t addr, int force) +{ + int ret = -1; + int fd = -1; + + /* Maximum i2c bus number is 255(3 char), +1 for null terminated string. */ + int path_len = strlen(I2C_DEV_PREFIX) + 4; + int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE; + + if (bus < 0 || bus > I2C_MAX_BUS) { + msg_perr("Invalid I2C bus %d.\n", bus); + return ret; + } + + char *dev = calloc(1, path_len); + if (!dev) { + msg_perr("Unable to allocate space for device name of len %d: %s.\n", + path_len, strerror(errno)); + goto linux_i2c_open_err; + } + + ret = snprintf(dev, path_len, "%s%d", I2C_DEV_PREFIX, bus); + if (ret < 0) { + msg_perr("Unable to join bus number to device name: %s.\n", strerror(errno)); + goto linux_i2c_open_err; + } + + fd = open(dev, O_RDWR); + if (fd < 0) { + msg_perr("Unable to open I2C device %s: %s.\n", dev, strerror(errno)); + ret = fd; + goto linux_i2c_open_err; + } + + ret = ioctl(fd, request, addr); + if (ret < 0) { + msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); + i2c_close(fd); + goto linux_i2c_open_err; + } + +linux_i2c_open_err: + if (dev) + free(dev); + + return ret ? ret : fd; +} + +int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf) +{ + if (buf->len == 0) + return 0; + + int ret = ioctl(fd, I2C_SLAVE, addr); + if (ret < 0) { + msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); + return ret; + } + + return read(fd, buf->buf, buf->len); +} + +int i2c_write(int fd, uint16_t addr, const i2c_buffer_t *buf) +{ + if (buf->len == 0) + return 0; + + int ret = ioctl(fd, I2C_SLAVE, addr); + if (ret < 0) { + msg_perr("Unable to set I2C slave address to 0x%02x: %s.\n", addr, strerror(errno)); + return ret; + } + + return write(fd, buf->buf, buf->len); +} |