1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-06-30 21:52:36 +02:00

lspcon_i2c_spi: support a devpath option

Some callers may find it easier to provide the path to an I2C device
at which to communicate with the device, rather than specify the bus
number- doing so might involve trying to parse a path to extract the
number only for flashrom to do the reverse, which is error-prone and
unnecessary.

This change adds support for a `devpath` option, continuing to
allow `bus` and requiring only one of them be specified.

TEST=Verified --flash-size outputs correct values with both
     devpath=/dev/i2c-7 and bus=7, as well as noting that one is
     required if neither is specified and only one may be specified
     if both are given.

Signed-off-by: Peter Marheine <pmarheine@chromium.org>
Change-Id: Id2adf8a307b9205ce5e5804a6c3e22f19d0c34c9
Reviewed-on: https://review.coreboot.org/c/flashrom/+/51967
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Peter Marheine
2021-03-31 11:28:11 +11:00
committed by Angel Pons
parent d4063bf3a7
commit 0969e43b3f
3 changed files with 78 additions and 50 deletions

View File

@ -37,14 +37,31 @@ int i2c_close(int fd)
return fd == -1 ? 0 : close(fd);
}
int i2c_open_path(const char *path, uint16_t addr, int force)
{
int fd = open(path, O_RDWR);
if (fd < 0) {
msg_perr("Unable to open I2C device %s: %s.\n", path, strerror(errno));
return fd;
}
int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE;
int 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);
return ret;
}
return fd;
}
int i2c_open(int bus, uint16_t addr, int force)
{
char dev[sizeof(I2C_DEV_PREFIX)] = {0};
int ret = -1;
int fd = -1;
int request = force ? I2C_SLAVE_FORCE : I2C_SLAVE;
if (bus < 0 || bus > I2C_MAX_BUS) {
msg_perr("Invalid I2C bus %d.\n", bus);
@ -57,20 +74,7 @@ int i2c_open(int bus, uint16_t addr, int force)
return ret;
}
fd = open(dev, O_RDWR);
if (fd < 0) {
msg_perr("Unable to open I2C device %s: %s.\n", dev, strerror(errno));
return fd;
}
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);
return ret;
}
return fd;
return i2c_open_path(dev, addr, force);
}
int i2c_read(int fd, uint16_t addr, i2c_buffer_t *buf)