summaryrefslogtreecommitdiff
path: root/driver/accel_kionix.c
diff options
context:
space:
mode:
authorDenis Brockus <dbrockus@chromium.org>2019-06-25 12:44:16 -0600
committerCommit Bot <commit-bot@chromium.org>2019-07-19 21:11:02 +0000
commitd1a18f82ed831d4e640336ff5571f5fa64bc7b36 (patch)
treec46aeb6136de1c27c66e3d5f662e9620161bef7b /driver/accel_kionix.c
parent1f14229fa7e499dfcee07d17add187598ff0a46c (diff)
downloadchrome-ec-d1a18f82ed831d4e640336ff5571f5fa64bc7b36.tar.gz
Use 7bit I2C/SPI slave addresses in EC
Opt for 7bit slave addresses in EC code. If 8bit is expected by a driver, make it local and show this in the naming. Use __7b, __7bf and __8b as name extensions for i2c/spi addresses used in the EC codebase. __7b indicates a 7bit address by itself. __7bf indicates a 7bit address with optional flags attached. __8b indicates a 8bit address by itself. Allow space for 10bit addresses, even though this is not currently being used by any of our attached devices. These extensions are for verification purposes only and will be removed in the last pass of this ticket. I want to make sure the variable names reflect the type to help eliminate future 7/8/7-flags confusion. BUG=chromium:971296 BRANCH=none TEST=make buildall -j Change-Id: I2fc3d1b52ce76184492b2aaff3060f486ca45f45 Signed-off-by: Denis Brockus <dbrockus@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1699893 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Diffstat (limited to 'driver/accel_kionix.c')
-rw-r--r--driver/accel_kionix.c98
1 files changed, 60 insertions, 38 deletions
diff --git a/driver/accel_kionix.c b/driver/accel_kionix.c
index d486d4bcd5..4e1f5bb4fc 100644
--- a/driver/accel_kionix.c
+++ b/driver/accel_kionix.c
@@ -133,24 +133,27 @@ static int find_param_index(const int eng_val, const int round_up,
/**
* Read register from accelerometer.
*/
-static int raw_read8(const int port, const int addr, const int reg,
- int *data_ptr)
+static int raw_read8__7bf(const int port,
+ const uint16_t i2c_spi_addr__7bf,
+ const int reg, int *data_ptr)
{
int rv = EC_ERROR_INVAL;
- if (KIONIX_IS_SPI(addr)) {
+ if (SLAVE_IS_SPI(i2c_spi_addr__7bf)) {
#ifdef CONFIG_SPI_ACCEL_PORT
uint8_t val;
uint8_t cmd = 0x80 | reg;
- rv = spi_transaction(&spi_devices[KIONIX_SPI_ADDRESS(addr)],
- &cmd, 1, &val, 1);
+ rv = spi_transaction(
+ &spi_devices[SLAVE_GET_SPI_ADDR__7b(i2c_spi_addr__7bf)],
+ &cmd, 1, &val, 1);
if (rv == EC_SUCCESS)
*data_ptr = val;
#endif
} else {
- rv = i2c_read8(port, addr, reg, data_ptr);
+ rv = i2c_read8__7bf(port, i2c_spi_addr__7bf,
+ reg, data_ptr);
}
return rv;
}
@@ -158,36 +161,43 @@ static int raw_read8(const int port, const int addr, const int reg,
/**
* Write register from accelerometer.
*/
-static int raw_write8(const int port, const int addr, const int reg, int data)
+static int raw_write8__7bf(const int port,
+ const uint16_t i2c_spi_addr__7bf,
+ const int reg, int data)
{
int rv = EC_ERROR_INVAL;
- if (KIONIX_IS_SPI(addr)) {
+ if (SLAVE_IS_SPI(i2c_spi_addr__7bf)) {
#ifdef CONFIG_SPI_ACCEL_PORT
uint8_t cmd[2] = { reg, data };
- rv = spi_transaction(&spi_devices[KIONIX_SPI_ADDRESS(addr)],
- cmd, 2, NULL, 0);
+ rv = spi_transaction(
+ &spi_devices[SLAVE_GET_SPI_ADDR__7b(i2c_spi_addr__7bf)],
+ cmd, 2, NULL, 0);
#endif
} else {
- rv = i2c_write8(port, addr, reg, data);
+ rv = i2c_write8__7bf(port, i2c_spi_addr__7bf,
+ reg, data);
}
return rv;
}
-static int raw_read_multi(const int port, int addr, uint8_t reg,
- uint8_t *rxdata, int rxlen)
+static int raw_read_multi__7bf(const int port,
+ const uint16_t i2c_spi_addr__7bf,
+ uint8_t reg, uint8_t *rxdata, int rxlen)
{
int rv = EC_ERROR_INVAL;
- if (KIONIX_IS_SPI(addr)) {
+ if (SLAVE_IS_SPI(i2c_spi_addr__7bf)) {
#ifdef CONFIG_SPI_ACCEL_PORT
reg |= 0x80;
- rv = spi_transaction(&spi_devices[KIONIX_SPI_ADDRESS(addr)],
- &reg, 1, rxdata, rxlen);
+ rv = spi_transaction(
+ &spi_devices[SLAVE_GET_SPI_ADDR__7b(i2c_spi_addr__7bf)],
+ &reg, 1, rxdata, rxlen);
#endif
} else {
- rv = i2c_read_block(port, addr, reg, rxdata, rxlen);
+ rv = i2c_read_block__7bf(port, i2c_spi_addr__7bf,
+ reg, rxdata, rxlen);
}
return rv;
}
@@ -215,13 +225,15 @@ static int disable_sensor(const struct motion_sensor_t *s, int *reg_val)
* so that we can restore it later.
*/
for (i = 0; i < SENSOR_ENABLE_ATTEMPTS; i++) {
- ret = raw_read8(s->port, s->addr, reg, reg_val);
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, reg_val);
if (ret != EC_SUCCESS)
continue;
*reg_val &= ~pc1_field;
- ret = raw_write8(s->port, s->addr, reg, *reg_val);
+ ret = raw_write8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, *reg_val);
if (ret == EC_SUCCESS)
return EC_SUCCESS;
}
@@ -246,7 +258,8 @@ static int enable_sensor(const struct motion_sensor_t *s, int reg_val)
pc1_field = KIONIX_PC1_FIELD(V(s));
for (i = 0; i < SENSOR_ENABLE_ATTEMPTS; i++) {
- ret = raw_read8(s->port, s->addr, reg, &reg_val);
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, &reg_val);
if (ret != EC_SUCCESS)
continue;
@@ -257,8 +270,8 @@ static int enable_sensor(const struct motion_sensor_t *s, int reg_val)
#endif
/* Enable accelerometer based on reg_val value. */
- ret = raw_write8(s->port, s->addr, reg,
- reg_val | pc1_field);
+ ret = raw_write8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, reg_val | pc1_field);
/* On first success, we are done. */
if (ret == EC_SUCCESS)
@@ -292,7 +305,8 @@ static int set_value(const struct motion_sensor_t *s, int reg, int val,
/* Determine new value of control reg and attempt to write it. */
reg_val_new = (reg_val & ~field) | val;
- ret = raw_write8(s->port, s->addr, reg, reg_val_new);
+ ret = raw_write8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, reg_val_new);
/* If successfully written, then save the range. */
if (ret == EC_SUCCESS)
@@ -433,7 +447,7 @@ static int check_orientation_locked(const struct motion_sensor_t *s)
int orientation, raw_orientation;
int ret;
- ret = raw_read8(s->port, s->addr,
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf,
KX022_TSCP, &raw_orientation);
if (ret != EC_SUCCESS)
return ret;
@@ -459,7 +473,7 @@ static int read(const struct motion_sensor_t *s, intv3_t v)
/* Read 6 bytes starting at XOUT_L. */
reg = KIONIX_XOUT_L(V(s));
mutex_lock(s->mutex);
- ret = raw_read_multi(s->port, s->addr, reg, acc, 6);
+ ret = raw_read_multi__7bf(s->port, s->i2c_spi_addr__7bf, reg, acc, 6);
#ifdef CONFIG_KX022_ORIENTATION_SENSOR
if ((s->location == MOTIONSENSE_LOC_LID) && (V(s) == 0) &&
(ret == EC_SUCCESS))
@@ -518,7 +532,8 @@ static int init(const struct motion_sensor_t *s)
do {
msleep(1);
/* Read WHO_AM_I to be sure the device has booted */
- ret = raw_read8(s->port, s->addr, reg, &val);
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, &val);
if (ret == EC_SUCCESS)
break;
@@ -531,16 +546,22 @@ static int init(const struct motion_sensor_t *s)
} else {
/* Write 0x00 to the internal register for KX022 */
reg = KX022_INTERNAL;
- ret = raw_write8(s->port, s->addr, reg, 0x0);
+ ret = raw_write8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, 0x0);
if (ret != EC_SUCCESS) {
/*
* For I2C communication, if ACK was not received
* from the first address, resend the command using
* the second address.
*/
- if (!KIONIX_IS_SPI(s->addr)) {
- ret = raw_write8(s->port, s->addr & ~4, reg,
- 0x0);
+ if (!SLAVE_IS_SPI(s->i2c_spi_addr__7bf)) {
+ const uint16_t i2c_alt_addr__7bf =
+ I2C_GET_ADDR__7b(
+ s->i2c_spi_addr__7bf)
+ & ~2;
+ ret = raw_write8__7bf(s->port,
+ i2c_alt_addr__7bf,
+ reg, 0x0);
}
}
}
@@ -557,21 +578,21 @@ static int init(const struct motion_sensor_t *s)
ret = disable_sensor(s, &val);
if (ret != EC_SUCCESS)
goto reset_failed;
- ret = raw_read8(s->port, s->addr, reg, &val);
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf, reg, &val);
if (ret != EC_SUCCESS)
goto reset_failed;
val |= reset_field;
} else {
/* Write 0 to CTRL2 for KX022 */
- ret = raw_write8(s->port, s->addr, reg, 0x0);
+ ret = raw_write8__7bf(s->port, s->i2c_spi_addr__7bf, reg, 0x0);
if (ret != EC_SUCCESS)
goto reset_failed;
val = reset_field;
}
- ret = raw_write8(s->port, s->addr, reg, val);
+ ret = raw_write8__7bf(s->port, s->i2c_spi_addr__7bf, reg, val);
if (ret != EC_SUCCESS)
goto reset_failed;
@@ -581,7 +602,8 @@ static int init(const struct motion_sensor_t *s)
do {
msleep(1);
- ret = raw_read8(s->port, s->addr, reg, &val);
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf,
+ reg, &val);
/* Reset complete. */
if ((ret == EC_SUCCESS) && !(val & reset_field))
break;
@@ -596,7 +618,7 @@ static int init(const struct motion_sensor_t *s)
msleep(2);
reg = KX022_COTR;
- ret = raw_read8(s->port, s->addr, reg, &val);
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf, reg, &val);
if (val != KX022_COTR_VAL_DEFAULT) {
CPRINTF("[%s: the software reset failed]\n", s->name);
ret = EC_ERROR_HW_INTERNAL;
@@ -605,7 +627,7 @@ static int init(const struct motion_sensor_t *s)
}
reg = KIONIX_WHO_AM_I(V(s));
- ret = raw_read8(s->port, s->addr, reg, &val);
+ ret = raw_read8__7bf(s->port, s->i2c_spi_addr__7bf, reg, &val);
if (ret != EC_SUCCESS || val != KIONIX_WHO_AM_I_VAL(V(s))) {
ret = EC_ERROR_HW_INTERNAL;
goto reset_failed;
@@ -648,7 +670,7 @@ struct i2c_stress_test_dev kionix_i2c_stress_test_dev = {
.read_val = KIONIX_WHO_AM_I_VAL(V(s)),
.write_reg = KIONIX_ODR_REG(V(s)),
},
- .i2c_read = &raw_read8,
- .i2c_write = &raw_write8,
+ .i2c_read__7bf = &raw_read8__7bf,
+ .i2c_write__7bf = &raw_write8__7bf,
};
#endif /* CONFIG_CMD_I2C_STRESS_TEST_ACCEL */