summaryrefslogtreecommitdiff
path: root/driver
diff options
context:
space:
mode:
authorJonathan Brandmeyer <jbrandmeyer@chromium.org>2018-08-07 16:04:23 -0600
committerchrome-bot <chrome-bot@chromium.org>2018-08-09 22:04:40 -0700
commit740427a24398b3fd66904e913b742f751371d09c (patch)
tree1efe5fd4db314fbbb44bdd3a5bc726c4ee818ad0 /driver
parentaba9f5e09f7fb1f18a297fb5552404706a642351 (diff)
downloadchrome-ec-740427a24398b3fd66904e913b742f751371d09c.tar.gz
drivers: Refactor to use high-level i2c APIs
Using the high-level APIs for block transfers eliminates some code duplication and error-prone manual locking sequences. - common/cbi: Block transfers - driver/accel_bma2x2: Block transfers - driver/accel_kionix: Block transfers - driver/accelgyro_bmi160: Block transfers - driver/accelgyro_lsm6ds0: Block transfers - driver/baro_bmp280: Block and byte transfers - driver/charger/rt946x: Block transfers - driver/gyro_l3gd20h: Block transfers - driver/stm_mems_common: Block transfers - driver/tcpm: Block transfers TEST=buildall; motionsense and PD testing on Grunt convertible which exercises the bma160, kionix and tcpcm drivers. BRANCH=none BUG=chromium:871851 Change-Id: I1732253a244c3343459265ce1e1e54488cee65b8 Signed-off-by: Jonathan Brandmeyer <jbrandmeyer@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1167958 Reviewed-by: Jett Rink <jettrink@chromium.org>
Diffstat (limited to 'driver')
-rw-r--r--driver/accel_bma2x2.c15
-rw-r--r--driver/accel_kionix.c5
-rw-r--r--driver/accelgyro_bmi160.c6
-rw-r--r--driver/accelgyro_lsm6ds0.c5
-rw-r--r--driver/baro_bmp280.c48
-rw-r--r--driver/charger/rt946x.c15
-rw-r--r--driver/gyro_l3gd20h.c5
-rw-r--r--driver/stm_mems_common.c17
-rw-r--r--driver/tcpm/tcpci.c41
-rw-r--r--driver/tcpm/tcpm.h21
10 files changed, 66 insertions, 112 deletions
diff --git a/driver/accel_bma2x2.c b/driver/accel_bma2x2.c
index 7548b5d3be..48e38fc14d 100644
--- a/driver/accel_bma2x2.c
+++ b/driver/accel_bma2x2.c
@@ -43,19 +43,6 @@ static inline int raw_write8(const int port, const int addr, const int reg,
return i2c_write8(port, addr, reg, data);
}
-static int raw_read_multi(const int port, int addr, uint8_t reg,
- uint8_t *rxdata, int rxlen)
-{
- int rv;
-
- i2c_lock(port, 1);
- rv = i2c_xfer(port, addr, &reg, 1, rxdata, rxlen,
- I2C_XFER_SINGLE);
- i2c_lock(port, 0);
-
- return rv;
-}
-
static int set_range(const struct motion_sensor_t *s, int range, int rnd)
{
int ret, range_val, reg_val, range_reg_val;
@@ -173,7 +160,7 @@ static int read(const struct motion_sensor_t *s, vector_3_t v)
/* Read 6 bytes starting at X_AXIS_LSB. */
mutex_lock(s->mutex);
- ret = raw_read_multi(s->port, s->addr, BMA2x2_X_AXIS_LSB_ADDR, acc, 6);
+ ret = i2c_read_block(s->port, s->addr, BMA2x2_X_AXIS_LSB_ADDR, acc, 6);
mutex_unlock(s->mutex);
if (ret != EC_SUCCESS)
diff --git a/driver/accel_kionix.c b/driver/accel_kionix.c
index 150a050226..8e6984ad79 100644
--- a/driver/accel_kionix.c
+++ b/driver/accel_kionix.c
@@ -187,10 +187,7 @@ static int raw_read_multi(const int port, int addr, uint8_t reg,
&reg, 1, rxdata, rxlen);
#endif
} else {
- i2c_lock(port, 1);
- rv = i2c_xfer(port, addr, &reg, 1, rxdata, rxlen,
- I2C_XFER_SINGLE);
- i2c_lock(port, 0);
+ rv = i2c_read_block(port, addr, reg, rxdata, rxlen);
}
return rv;
}
diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c
index 85a6ff5235..aa911e3f0c 100644
--- a/driver/accelgyro_bmi160.c
+++ b/driver/accelgyro_bmi160.c
@@ -232,10 +232,8 @@ static int raw_read_n(const int port, const int addr, const uint8_t reg,
#endif
} else {
#ifdef I2C_PORT_ACCEL
- i2c_lock(port, 1);
- rv = i2c_xfer(port, BMI160_I2C_ADDRESS(addr), &reg, 1,
- data_ptr, len, I2C_XFER_SINGLE);
- i2c_lock(port, 0);
+ rv = i2c_read_block(port, BMI160_I2C_ADDRESS(addr), reg,
+ data_ptr, len);
#endif
}
return rv;
diff --git a/driver/accelgyro_lsm6ds0.c b/driver/accelgyro_lsm6ds0.c
index cf2eb78ff4..04cb7a7e78 100644
--- a/driver/accelgyro_lsm6ds0.c
+++ b/driver/accelgyro_lsm6ds0.c
@@ -343,10 +343,7 @@ static int read(const struct motion_sensor_t *s, vector_3_t v)
xyz_reg = get_xyz_reg(s->type);
/* Read 6 bytes starting at xyz_reg */
- i2c_lock(s->port, 1);
- ret = i2c_xfer(s->port, s->addr,
- &xyz_reg, 1, raw, 6, I2C_XFER_SINGLE);
- i2c_lock(s->port, 0);
+ ret = i2c_read_block(s->port, s->addr, xyz_reg, raw, 6);
if (ret != EC_SUCCESS) {
CPRINTF("[%T %s type:0x%X RD XYZ Error]",
diff --git a/driver/baro_bmp280.c b/driver/baro_bmp280.c
index 3ca0084704..e6220c7e35 100644
--- a/driver/baro_bmp280.c
+++ b/driver/baro_bmp280.c
@@ -68,37 +68,6 @@
static const uint16_t standby_durn[] = {1, 63, 125, 250, 500, 1000, 2000, 4000};
-static inline int raw_read8(const int port, const int addr, const int reg,
- int *data_ptr)
-{
- return i2c_read8(port, addr, reg, data_ptr);
-}
-
-/*
- * Read n bytes from barometer.
- */
-static inline int raw_read_n(const int port, const int addr, const uint8_t reg,
- uint8_t *data_ptr, const int len)
-{
- int rv;
-
- i2c_lock(port, 1);
- rv = i2c_xfer(port, addr, &reg, 1,
- data_ptr, len, I2C_XFER_SINGLE);
- i2c_lock(port, 0);
- return rv;
-}
-
-/*
- * Write 8bit register from accelerometer.
- */
-static inline int raw_write8(const int port, const int addr, const int reg,
- int data)
-{
- return i2c_write8(port, addr, reg, data);
-}
-
-
/*
* This function is used to get calibration parameters used for
* calculation in the registers
@@ -122,7 +91,6 @@ static inline int raw_write8(const int port, const int addr, const int reg,
* @retval 0 -> Success
*
*/
-
static int bmp280_get_calib_param(const struct motion_sensor_t *s)
{
int ret;
@@ -130,7 +98,7 @@ static int bmp280_get_calib_param(const struct motion_sensor_t *s)
uint8_t a_data_u8[BMP280_CALIB_DATA_SIZE] = {0};
struct bmp280_drv_data_t *data = BMP280_GET_DATA(s);
- ret = raw_read_n(s->port, s->addr,
+ ret = i2c_read_block(s->port, s->addr,
BMP280_TEMPERATURE_CALIB_DIG_T1_LSB_REG,
a_data_u8, BMP280_CALIB_DATA_SIZE);
@@ -161,7 +129,7 @@ static int bmp280_read_uncomp_pressure(const struct motion_sensor_t *s,
int ret;
uint8_t a_data_u8[BMP280_PRESSURE_DATA_SIZE] = {0};
- ret = raw_read_n(s->port, s->addr,
+ ret = i2c_read_block(s->port, s->addr,
BMP280_PRESSURE_MSB_REG,
a_data_u8, BMP280_PRESSURE_DATA_SIZE);
@@ -250,13 +218,13 @@ static int bmp280_set_standby_durn(const struct motion_sensor_t *s,
{
int ret, val;
- ret = raw_read8(s->port, s->addr,
+ ret = i2c_read8(s->port, s->addr,
BMP280_CONFIG_REG, &val);
if (ret == EC_SUCCESS) {
val = (val & 0xE0) | ((durn << 5) & 0xE0);
/* write the standby duration*/
- ret = raw_write8(s->port, s->addr,
+ ret = i2c_write8(s->port, s->addr,
BMP280_CONFIG_REG, val);
}
@@ -271,7 +239,7 @@ static int bmp280_set_power_mode(const struct motion_sensor_t *s,
val = (BMP280_OVERSAMP_TEMP << 5) +
(BMP280_OVERSAMP_PRES << 2) + power_mode;
- return raw_write8(s->port, s->addr, BMP280_CTRL_MEAS_REG, val);
+ return i2c_write8(s->port, s->addr, BMP280_CTRL_MEAS_REG, val);
}
static int bmp280_set_range(const struct motion_sensor_t *s,
@@ -309,7 +277,7 @@ static int bmp280_init(const struct motion_sensor_t *s)
return EC_ERROR_INVAL;
/* Read chip id */
- ret = raw_read8(s->port, s->addr,
+ ret = i2c_read8(s->port, s->addr,
BMP280_CHIP_ID_REG, &val);
if (ret)
return ret;
@@ -415,7 +383,7 @@ struct i2c_stress_test_dev bmp280_i2c_stress_test_dev = {
.read_val = BMP280_CHIP_ID,
.write_reg = BMP280_CONFIG_REG,
},
- .i2c_read = &raw_read8,
- .i2c_write = &raw_write8,
+ .i2c_read = &i2c_read8,
+ .i2c_write = &i2c_write8,
};
#endif /* CONFIG_CMD_I2C_STRESS_TEST_ACCEL */
diff --git a/driver/charger/rt946x.c b/driver/charger/rt946x.c
index c299c075aa..2fbac37629 100644
--- a/driver/charger/rt946x.c
+++ b/driver/charger/rt946x.c
@@ -153,20 +153,7 @@ static int rt946x_write8(int reg, int val)
static int rt946x_block_write(int reg, const uint8_t *val, int len)
{
- int rv;
- uint8_t buf[I2C_MAX_HOST_PACKET_SIZE];
-
- if (len + 1 > I2C_MAX_HOST_PACKET_SIZE)
- return EC_ERROR_INVAL;
-
- buf[0] = reg & 0xff;
- memcpy(&buf[1], val, len);
-
- i2c_lock(I2C_PORT_CHARGER, 1);
- rv = i2c_xfer(I2C_PORT_CHARGER, RT946X_ADDR, buf, len + 1, NULL, 0,
- I2C_XFER_SINGLE);
- i2c_lock(I2C_PORT_CHARGER, 0);
- return rv;
+ return i2c_write_block(I2C_PORT_CHARGER, RT946X_ADDR, reg, val, len);
}
static int rt946x_update_bits(int reg, int mask, int val)
diff --git a/driver/gyro_l3gd20h.c b/driver/gyro_l3gd20h.c
index 2c7e9b11da..0a70d0b36a 100644
--- a/driver/gyro_l3gd20h.c
+++ b/driver/gyro_l3gd20h.c
@@ -335,10 +335,7 @@ static int read(const struct motion_sensor_t *s, vector_3_t v)
xyz_reg = get_xyz_reg(s->type);
/* Read 6 bytes starting at xyz_reg */
- i2c_lock(s->port, 1);
- ret = i2c_xfer(s->port, s->addr,
- &xyz_reg, 1, raw, 6, I2C_XFER_SINGLE);
- i2c_lock(s->port, 0);
+ i2c_block_read(s->port, s->addr, xyz_reg, raw, 6);
if (ret != EC_SUCCESS) {
CPRINTF("[%T %s type:0x%X RD XYZ Error]",
diff --git a/driver/stm_mems_common.c b/driver/stm_mems_common.c
index b3fe4d94fb..c22f0d42e5 100644
--- a/driver/stm_mems_common.c
+++ b/driver/stm_mems_common.c
@@ -14,15 +14,8 @@
int st_raw_read_n(const int port, const int addr, const uint8_t reg,
uint8_t *data_ptr, const int len)
{
- int rv = -EC_ERROR_PARAM1;
- uint8_t reg_a = reg | 0x80;
-
/* TODO: Implement SPI interface support */
- i2c_lock(port, 1);
- rv = i2c_xfer(port, addr, &reg_a, 1, data_ptr, len, I2C_XFER_SINGLE);
- i2c_lock(port, 0);
-
- return rv;
+ return i2c_read_block(port, addr, reg | 0x80, data_ptr, len);
}
/**
@@ -31,14 +24,8 @@ int st_raw_read_n(const int port, const int addr, const uint8_t reg,
int st_raw_read_n_noinc(const int port, const int addr, const uint8_t reg,
uint8_t *data_ptr, const int len)
{
- int rv = -EC_ERROR_PARAM1;
-
/* TODO: Implement SPI interface support */
- i2c_lock(port, 1);
- rv = i2c_xfer(port, addr, &reg, 1, data_ptr, len, I2C_XFER_SINGLE);
- i2c_lock(port, 0);
-
- return rv;
+ return i2c_read_block(port, addr, reg, data_ptr, len);
}
/**
diff --git a/driver/tcpm/tcpci.c b/driver/tcpm/tcpci.c
index 0700d7c361..a2562d48c2 100644
--- a/driver/tcpm/tcpci.c
+++ b/driver/tcpm/tcpci.c
@@ -77,6 +77,34 @@ int tcpc_read16(int port, int reg, int *val)
return rv;
}
+int tcpc_read_block(int port, int reg, uint8_t *in, int size)
+{
+ int rv = i2c_read_block(tcpc_config[port].i2c_host_port,
+ tcpc_config[port].i2c_slave_addr, reg, in, size);
+ if (rv && pd_device_in_low_power(port)) {
+ pd_wait_for_wakeup(port);
+ rv = i2c_read_block(tcpc_config[port].i2c_host_port,
+ tcpc_config[port].i2c_slave_addr, reg,
+ in, size);
+ }
+ pd_device_accessed(port);
+ return rv;
+}
+
+int tcpc_write_block(int port, int reg, const uint8_t *out, int size)
+{
+ int rv = i2c_write_block(tcpc_config[port].i2c_host_port,
+ tcpc_config[port].i2c_slave_addr, reg, out, size);
+ if (rv && pd_device_in_low_power(port)) {
+ pd_wait_for_wakeup(port);
+ rv = i2c_write_block(tcpc_config[port].i2c_host_port,
+ tcpc_config[port].i2c_slave_addr, reg,
+ out, size);
+ }
+ pd_device_accessed(port);
+ return rv;
+}
+
int tcpc_xfer(int port, const uint8_t *out, int out_size,
uint8_t *in, int in_size, int flags)
{
@@ -314,11 +342,7 @@ int tcpci_tcpm_get_message(int port, uint32_t *payload, int *head)
cnt = cnt - 3;
if (rv == EC_SUCCESS && cnt > 0) {
- tcpc_lock(port, 1);
- rv = tcpc_xfer(port,
- (uint8_t *)&reg, 1, (uint8_t *)payload,
- cnt, I2C_XFER_SINGLE);
- tcpc_lock(port, 0);
+ tcpc_read_block(port, reg, (uint8_t *)payload, cnt);
}
clear:
@@ -344,12 +368,7 @@ int tcpci_tcpm_transmit(int port, enum tcpm_transmit_type type,
return rv;
if (cnt > 0) {
- tcpc_lock(port, 1);
- rv = tcpc_xfer(port,
- (uint8_t *)&reg, 1, NULL, 0, I2C_XFER_START);
- rv |= tcpc_xfer(port,
- (uint8_t *)data, cnt, NULL, 0, I2C_XFER_STOP);
- tcpc_lock(port, 0);
+ tcpc_write_block(port, reg, (const uint8_t *)data, cnt);
}
/* If tcpc read fails, return error */
diff --git a/driver/tcpm/tcpm.h b/driver/tcpm/tcpm.h
index bc2d699ea0..cc360ba75d 100644
--- a/driver/tcpm/tcpm.h
+++ b/driver/tcpm/tcpm.h
@@ -57,13 +57,30 @@ static inline int tcpc_xfer(int port, const uint8_t *out, int out_size,
tcpc_config[port].i2c_slave_addr, out, out_size, in,
in_size, flags);
}
+
+static inline int tcpc_read_block(int port, int reg, uint8_t *in, int size)
+{
+ return i2c_read_block(tcpc_config[port].i2c_host_port,
+ tcpc_config[port].i2c_slave_addr, reg, in, size);
+}
+
+static inline int tcpc_write_block(int port, int reg,
+ const uint8_t *out, int size)
+{
+ return i2c_write_block(tcpc_config[port].i2c_host_port,
+ tcpc_config[port].i2c_slave_addr, reg, out, size);
+}
+
#else /* !CONFIG_USB_PD_TCPC_LOW_POWER */
int tcpc_write(int port, int reg, int val);
int tcpc_write16(int port, int reg, int val);
int tcpc_read(int port, int reg, int *val);
int tcpc_read16(int port, int reg, int *val);
-int tcpc_xfer(int port, const uint8_t *out, int out_size, uint8_t *in,
- int in_size, int flags);
+int tcpc_read_block(int port, int reg, uint8_t *in, int size);
+int tcpc_write_block(int port, int reg, const uint8_t *out, int size);
+int tcpc_xfer(int port, const uint8_t *out, int out_size,
+ uint8_t *in, int in_size, int flags);
+
#endif /* CONFIG_USB_PD_TCPC_LOW_POWER */
static inline void tcpc_lock(int port, int lock)