summaryrefslogtreecommitdiff
path: root/driver
diff options
context:
space:
mode:
authorGwendal Grignou <gwendal@chromium.org>2015-05-30 11:52:28 -0700
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2015-07-10 03:40:19 +0000
commita7c4132d2581ec7fa0155d414c2c3e0b9729b34c (patch)
treea26cf1a1b925532e4e573f69ae40fa77cf7a3f59 /driver
parentb21efba26afaa1a8c5e7e7e8a71f31a51cbc230c (diff)
downloadchrome-ec-a7c4132d2581ec7fa0155d414c2c3e0b9729b34c.tar.gz
driver: bmi160: Add FIFO and interrupt support
Add FIFO support, where bmi160 hardware FIFO is copied in local fifo. Add rudimentary support for single/double tap and lift detection. BUG=chrome-os-partner:39900 BRANCH=smaug TEST=Check on F411 that FIFO data is retrieved and correct. Check on Smaug as well, with proper kernel the collect the FIFO: - check that increasing sampling_frequency we are collecting the FIFO less often - check no frames are lost. - check tap/lift interrupts are working - if latency is less than 100ms, check we are collecting much faster. Change-Id: Ic2317c27fad0ef31dacd6e18cd5f71ccd2cec807 Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/274227
Diffstat (limited to 'driver')
-rw-r--r--driver/accelgyro_bmi160.c301
-rw-r--r--driver/accelgyro_bmi160.h201
2 files changed, 421 insertions, 81 deletions
diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c
index 48190a963d..9e8682d41d 100644
--- a/driver/accelgyro_bmi160.c
+++ b/driver/accelgyro_bmi160.c
@@ -21,6 +21,7 @@
#define CPUTS(outstr) cputs(CC_ACCEL, outstr)
#define CPRINTF(format, args...) cprintf(CC_ACCEL, format, ## args)
+#define CPRINTS(format, args...) cprints(CC_ACCEL, format, ## args)
/*
* Struct for pairing an engineering value with the register value for a
@@ -117,7 +118,7 @@ static int get_engineering_val(const int reg_val,
}
/**
- * Read register from accelerometer.
+ * Read 8bit register from accelerometer.
*/
static inline int raw_read8(const int addr, const int reg, int *data_ptr)
{
@@ -125,13 +126,29 @@ static inline int raw_read8(const int addr, const int reg, int *data_ptr)
}
/**
- * Write register from accelerometer.
+ * Write 8bit register from accelerometer.
*/
static inline int raw_write8(const int addr, const int reg, int data)
{
return i2c_write8(I2C_PORT_ACCEL, addr, reg, data);
}
+/**
+ * Read 16bit register from accelerometer.
+ */
+static inline int raw_read16(const int addr, const int reg, int *data_ptr)
+{
+ return i2c_read16(I2C_PORT_ACCEL, addr, reg, data_ptr);
+}
+
+/**
+ * Read 32bit register from accelerometer.
+ */
+static inline int raw_read32(const int addr, const int reg, int *data_ptr)
+{
+ return i2c_read32(I2C_PORT_ACCEL, addr, reg, data_ptr);
+}
+
#ifdef CONFIG_MAG_BMI160_BMM150
/**
* Control access to the compass on the secondary i2c interface:
@@ -184,8 +201,7 @@ static int set_range(const struct motion_sensor_t *s,
int ret, range_tbl_size;
uint8_t reg_val, ctrl_reg;
const struct accel_param_pair *ranges;
- struct motion_data_t *data =
- &((struct bmi160_drv_data_t *)s->drv_data)->saved_data[s->type];
+ struct motion_data_t *data = BMI160_GET_SAVED_DATA(s);
if (s->type == MOTIONSENSE_TYPE_MAG) {
data->range = range;
@@ -207,8 +223,7 @@ static int set_range(const struct motion_sensor_t *s,
static int get_range(const struct motion_sensor_t *s,
int *range)
{
- struct motion_data_t *data =
- &((struct bmi160_drv_data_t *)s->drv_data)->saved_data[s->type];
+ struct motion_data_t *data = BMI160_GET_SAVED_DATA(s);
*range = data->range;
return EC_SUCCESS;
@@ -235,15 +250,20 @@ static int set_data_rate(const struct motion_sensor_t *s,
{
int ret, val, normalized_rate;
uint8_t ctrl_reg, reg_val;
- struct motion_data_t *data =
- &((struct bmi160_drv_data_t *)s->drv_data)->saved_data[s->type];
+ struct motion_data_t *data = BMI160_GET_SAVED_DATA(s);
if (rate == 0) {
- /* suspend */
+ /* go to suspend mode */
ret = raw_write8(s->i2c_addr, BMI160_CMD_REG,
BMI160_CMD_MODE_SUSPEND(s->type));
- msleep(30);
+ msleep(3);
+ data->odr = 0;
return ret;
+ } else if (data->odr == 0) {
+ /* back from suspend mode */
+ ret = raw_write8(s->i2c_addr, BMI160_CMD_REG,
+ BMI160_CMD_MODE_NORMAL(s->type));
+ msleep(3);
}
ctrl_reg = BMI160_CONF_REG(s->type);
reg_val = BMI160_ODR_TO_REG(rate);
@@ -311,8 +331,7 @@ accel_cleanup:
static int get_data_rate(const struct motion_sensor_t *s,
int *rate)
{
- struct motion_data_t *data =
- &((struct bmi160_drv_data_t *)s->drv_data)->saved_data[s->type];
+ struct motion_data_t *data = BMI160_GET_SAVED_DATA(s);
*rate = data->odr;
return EC_SUCCESS;
@@ -350,13 +369,249 @@ void normalize(const struct motion_sensor_t *s, vector_3_t v, uint8_t *data)
}
#ifdef CONFIG_ACCEL_INTERRUPTS
+/**
+ * bmi160_interrupt - called when the sensor activate the interrupt line.
+ *
+ * This is a "top half" interrupt handler, it just asks motion sense ask
+ * to schedule the "bottom half", ->irq_handler().
+ */
+void bmi160_interrupt(enum gpio_signal signal)
+{
+ task_set_event(TASK_ID_MOTIONSENSE, TASK_EVENT_MOTION_INTERRUPT, 0);
+}
+
+
static int set_interrupt(const struct motion_sensor_t *s,
unsigned int threshold)
{
- /* Currently unsupported. */
- return EC_ERROR_UNKNOWN;
+ int ret, tmp;
+ if (s->type != MOTIONSENSE_TYPE_ACCEL)
+ return EC_SUCCESS;
+
+ mutex_lock(s->mutex);
+ raw_write8(s->i2c_addr, BMI160_CMD_REG, BMI160_CMD_FIFO_FLUSH);
+ msleep(30);
+ raw_write8(s->i2c_addr, BMI160_CMD_REG, BMI160_CMD_INT_RESET);
+
+ /* Latch until interupts */
+ /* configure int2 as an external input */
+ tmp = BMI160_INT2_INPUT_EN | BMI160_LATCH_FOREVER;
+ ret = raw_write8(s->i2c_addr, BMI160_INT_LATCH, tmp);
+
+ /* configure int1 as an interupt */
+ ret = raw_write8(s->i2c_addr, BMI160_INT_OUT_CTRL,
+ BMI160_INT_CTRL(1, OUTPUT_EN));
+
+ /* Map Simple/Double Tap to int 1
+ * Map Flat interrupt to int 1
+ */
+ ret = raw_write8(s->i2c_addr, BMI160_INT_MAP_REG(1),
+ BMI160_INT_FLAT | BMI160_INT_D_TAP | BMI160_INT_S_TAP);
+
+#ifdef CONFIG_ACCEL_FIFO
+ /* map fifo water mark to int 1 */
+ ret = raw_write8(s->i2c_addr, BMI160_INT_FIFO_MAP,
+ BMI160_INT_MAP(1, FWM));
+
+ /* configure fifo watermark at 50% */
+ ret = raw_write8(s->i2c_addr, BMI160_FIFO_CONFIG_0,
+ 512 / sizeof(uint32_t));
+ ret = raw_write8(s->i2c_addr, BMI160_FIFO_CONFIG_1,
+ BMI160_FIFO_TAG_INT1_EN |
+ BMI160_FIFO_TAG_INT2_EN |
+ BMI160_FIFO_HEADER_EN |
+ BMI160_FIFO_MAG_EN |
+ BMI160_FIFO_ACC_EN |
+ BMI160_FIFO_GYR_EN);
+#endif
+
+ /* Set double tap interrupt and fifo*/
+ ret = raw_read8(s->i2c_addr, BMI160_INT_EN_0, &tmp);
+ tmp |= BMI160_INT_FLAT_EN | BMI160_INT_D_TAP_EN | BMI160_INT_S_TAP_EN;
+ ret = raw_write8(s->i2c_addr, BMI160_INT_EN_0, tmp);
+
+#ifdef CONFIG_ACCEL_FIFO
+ ret = raw_read8(s->i2c_addr, BMI160_INT_EN_1, &tmp);
+ tmp |= BMI160_INT_FWM_EN;
+ ret = raw_write8(s->i2c_addr, BMI160_INT_EN_1, tmp);
+#endif
+
+ mutex_unlock(s->mutex);
+ return ret;
}
+
+/**
+ * irq_handler - bottom half of the interrupt stack.
+ * Ran from the motion_sense task, finds the events that raised the interrupt.
+ *
+ * For now, we just print out. We should set a bitmask motion sense code will
+ * act upon.
+ */
+int irq_handler(const struct motion_sensor_t *s)
+{
+ int interrupt;
+
+ raw_read32(s->i2c_addr, BMI160_INT_STATUS_0, &interrupt);
+ raw_write8(s->i2c_addr, BMI160_CMD_REG, BMI160_CMD_INT_RESET);
+
+ if (interrupt & BMI160_S_TAP_INT)
+ CPRINTS("single tap: %08x", interrupt);
+ if (interrupt & BMI160_D_TAP_INT)
+ CPRINTS("double tap: %08x", interrupt);
+ if (interrupt & BMI160_FLAT_INT)
+ CPRINTS("flat: %08x", interrupt);
+ /*
+ * No need to read the FIFO here, motion sense task is
+ * doing it on every interrupt.
+ */
+ return EC_SUCCESS;
+}
+
+#endif /* CONFIG_ACCEL_INTERRUPTS */
+
+#ifdef CONFIG_ACCEL_FIFO
+enum fifo_state {
+ FIFO_HEADER,
+ FIFO_DATA_SKIP,
+ FIFO_DATA_TIME,
+ FIFO_DATA_CONFIG,
+};
+
+
+#define BMI160_FIFO_BUFFER 64
+static uint8_t bmi160_buffer[BMI160_FIFO_BUFFER];
+#define BUFFER_END(_buffer) ((_buffer) + sizeof(_buffer))
+/*
+ * Decode the header from the fifo.
+ * Return 0 if we need further processing.
+ * Sensor mutex must be held during processing, to protect the fifos.
+ *
+ * @s: base sensor
+ * @hdr: the header to decode
+ * @bp: current pointer in the buffer, updated when processing the header.
+ */
+static int bmi160_decode_header(struct motion_sensor_t *s,
+ enum fifo_header hdr, uint8_t **bp)
+{
+ if ((hdr & BMI160_FH_MODE_MASK) == BMI160_EMPTY &&
+ (hdr & BMI160_FH_PARM_MASK) != 0) {
+ int i, size = 0;
+ /* Check if there is enough space for the data frame */
+ for (i = MOTIONSENSE_TYPE_MAG; i >= MOTIONSENSE_TYPE_ACCEL;
+ i--) {
+ if (hdr & (1 << (i + BMI160_FH_PARM_OFFSET)))
+ size += (i == MOTIONSENSE_TYPE_MAG ? 8 : 6);
+ }
+ if (*bp + size > BUFFER_END(bmi160_buffer)) {
+ /* frame is not complete, it
+ * will be retransmitted.
+ */
+ *bp = BUFFER_END(bmi160_buffer);
+ return 1;
+ }
+ for (i = MOTIONSENSE_TYPE_MAG; i >= MOTIONSENSE_TYPE_ACCEL;
+ i--) {
+ if (hdr & (1 << (i + BMI160_FH_PARM_OFFSET))) {
+ struct ec_response_motion_sensor_data vector;
+ int *v = (s + i)->raw_xyz;
+ vector.flags = 0;
+ normalize(s + i, v, *bp);
+ vector.data[X] = v[X];
+ vector.data[Y] = v[Y];
+ vector.data[Z] = v[Z];
+ motion_sense_fifo_add_unit(&vector, s + i);
+ *bp += (i == MOTIONSENSE_TYPE_MAG ? 8 : 6);
+ }
+ }
+#if 0
+ if (hdr & BMI160_FH_EXT_MASK)
+ CPRINTF("%s%s\n",
+ (hdr & 0x1 ? "INT1" : ""),
+ (hdr & 0x2 ? "INT2" : ""));
#endif
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+static int load_fifo(struct motion_sensor_t *s)
+{
+ int done = 0;
+ int fifo_length;
+
+ if (s->type != MOTIONSENSE_TYPE_ACCEL)
+ return EC_SUCCESS;
+
+ /* Read fifo length */
+ raw_read16(s->i2c_addr, BMI160_FIFO_LENGTH_0, &fifo_length);
+ fifo_length &= BMI160_FIFO_LENGTH_MASK;
+ if (fifo_length == 0)
+ return EC_SUCCESS;
+ do {
+ enum fifo_state state = FIFO_HEADER;
+ uint8_t fifo_reg = BMI160_FIFO_DATA;
+ uint8_t *bp = bmi160_buffer;
+ i2c_lock(I2C_PORT_ACCEL, 1);
+ i2c_xfer(I2C_PORT_ACCEL, s->i2c_addr,
+ &fifo_reg, 1, bmi160_buffer,
+ sizeof(bmi160_buffer), I2C_XFER_SINGLE);
+ i2c_lock(I2C_PORT_ACCEL, 0);
+ while (!done && bp != BUFFER_END(bmi160_buffer)) {
+ switch (state) {
+ case FIFO_HEADER: {
+ enum fifo_header hdr = *bp++;
+ if (bmi160_decode_header(s, hdr, &bp))
+ continue;
+ /* Other cases */
+ hdr &= 0xdc;
+ switch (hdr) {
+ case BMI160_EMPTY:
+ done = 1;
+ break;
+ case BMI160_SKIP:
+ state = FIFO_DATA_SKIP;
+ break;
+ case BMI160_TIME:
+ state = FIFO_DATA_TIME;
+ break;
+ case BMI160_CONFIG:
+ state = FIFO_DATA_CONFIG;
+ break;
+ default:
+ CPRINTS("Unknown header: 0x%02x", hdr);
+ }
+ break;
+ }
+ case FIFO_DATA_SKIP:
+ CPRINTF("skipped %d frames\n", *bp++);
+ state = FIFO_HEADER;
+ break;
+ case FIFO_DATA_CONFIG:
+ CPRINTF("config change: 0x%02x\n", *bp++);
+ state = FIFO_HEADER;
+ break;
+ case FIFO_DATA_TIME:
+ if (bp + 3 > BUFFER_END(bmi160_buffer)) {
+ bp = BUFFER_END(bmi160_buffer);
+ continue;
+ }
+ /* We are not requesting timestamp */
+ CPRINTF("timestamp %d\n", (bp[2] << 16) |
+ (bp[1] << 8) | bp[0]);
+ state = FIFO_HEADER;
+ bp += 3;
+ break;
+ default:
+ CPRINTS("Unknown data: 0x%02x\n", *bp++);
+ state = FIFO_HEADER;
+ }
+ }
+ } while (!done);
+ return EC_SUCCESS;
+}
+#endif /* CONFIG_ACCEL_FIFO */
+
static int is_data_ready(const struct motion_sensor_t *s, int *ready)
{
@@ -425,8 +680,7 @@ static int init(const struct motion_sensor_t *s)
if (s->type == MOTIONSENSE_TYPE_ACCEL) {
- struct bmi160_drv_data_t *data =
- (struct bmi160_drv_data_t *)s->drv_data;
+ struct bmi160_drv_data_t *data = BMI160_GET_DATA(s);
/* Reset the chip to be in a good state */
raw_write8(s->i2c_addr, BMI160_CMD_REG,
@@ -446,8 +700,7 @@ static int init(const struct motion_sensor_t *s)
#ifdef CONFIG_MAG_BMI160_BMM150
if (s->type == MOTIONSENSE_TYPE_MAG) {
- struct bmi160_drv_data_t *data =
- (struct bmi160_drv_data_t *)s->drv_data;
+ struct bmi160_drv_data_t *data = BMI160_GET_DATA(s);
if ((data->flags & BMI160_FLAG_SEC_I2C_ENABLED) == 0) {
int ext_page_reg, pullup_reg;
/* Enable secondary interface */
@@ -527,7 +780,9 @@ static int init(const struct motion_sensor_t *s)
bmm150_mag_access_ctrl(s->i2c_addr, 0);
}
#endif
-
+#ifdef CONFIG_ACCEL_INTERRUPTS
+ ret = s->drv->set_interrupt(s, 0);
+#endif
/* Fifo setup is done elsewhere */
CPRINTF("[%T %s: MS Done Init type:0x%X range:%d odr:%d]\n",
s->name, s->type, s->runtime_config.range,
@@ -546,5 +801,13 @@ const struct accelgyro_drv bmi160_drv = {
.get_data_rate = get_data_rate,
#ifdef CONFIG_ACCEL_INTERRUPTS
.set_interrupt = set_interrupt,
+ .irq_handler = irq_handler,
+#endif
+#ifdef CONFIG_ACCEL_FIFO
+ .load_fifo = load_fifo,
#endif
};
+
+struct bmi160_drv_data_t g_bmi160_data = {
+ .flags = 0,
+};
diff --git a/driver/accelgyro_bmi160.h b/driver/accelgyro_bmi160.h
index b73b28bbb9..d8b12a0bc8 100644
--- a/driver/accelgyro_bmi160.h
+++ b/driver/accelgyro_bmi160.h
@@ -49,39 +49,82 @@
#define BMI160_SENSORTIME_2 0x1a
#define BMI160_STATUS 0x1b
-#define BMI160_DRDY_ACC 0x80
-#define BMI160_DRDY_GYR 0x40
-#define BMI160_DRDY_MAG 0x20
+#define BMI160_POR_DETECTED (1 << 0)
+#define BMI160_GYR_SLF_TST (1 << 1)
+#define BMI160_MAG_MAN_OP (1 << 2)
+#define BMI160_FOC_RDY (1 << 3)
+#define BMI160_NVM_RDY (1 << 4)
+#define BMI160_DRDY_MAG (1 << 5)
+#define BMI160_DRDY_GYR (1 << 6)
+#define BMI160_DRDY_ACC (1 << 7)
#define BMI160_DRDY_OFF(_sensor) (7 - (_sensor))
#define BMI160_DRDY_MASK(_sensor) (1 << BMI160_DRDY_OFF(_sensor))
-#define BMI160_NVM_RDY 0x10
-#define BMI160_FOC_RDY 0x08
-#define BMI160_MAG_MAN_OP 0x04
-#define BMI160_GYR_SLF_TST 0x02
-#define BMI160_POR_DETECTED 0x01
-
+/* first 2 bytes are the interrupt reasons, next 2 some qualifier */
#define BMI160_INT_STATUS_0 0x1c
+#define BMI160_STEP_INT (1 << 0)
+#define BMI160_SIGMOT_INT (1 << 1)
+#define BMI160_ANYM_INT (1 << 2)
+#define BMI160_PMU_TRIGGER_INT (1 << 3)
+#define BMI160_D_TAP_INT (1 << 4)
+#define BMI160_S_TAP_INT (1 << 5)
+#define BMI160_ORIENT_INT (1 << 6)
+#define BMI160_FLAT_INT (1 << 7)
+
#define BMI160_INT_STATUS_1 0x1d
+#define BMI160_HIGHG_INT (1 << (2 + 8))
+#define BMI160_LOWG_INT (1 << (3 + 8))
+#define BMI160_DRDY_INT (1 << (4 + 8))
+#define BMI160_FFULL_INT (1 << (5 + 8))
+#define BMI160_FWM_INT (1 << (6 + 8))
+#define BMI160_NOMO_INT (1 << (7 + 8))
+
+#define BMI160_INT_MASK 0xFFFF
+
#define BMI160_INT_STATUS_2 0x1e
#define BMI160_INT_STATUS_3 0x1f
+#define BMI160_FIRST_X (1 << (0 + 16))
+#define BMI160_FIRST_Y (1 << (1 + 16))
+#define BMI160_FIRST_Z (1 << (2 + 16))
+#define BMI160_SIGN (1 << (3 + 16))
+#define BMI160_ANYM_OFFSET 0
+#define BMI160_TAP_OFFSET 4
+#define BMI160_HIGH_OFFSET 8
+#define BMI160_INT_INFO(_type, _data) \
+(CONCAT2(BMI160_, _data) << CONCAT3(BMI160_, _type, _OFFSET))
+
+#define BMI160_ORIENT_Z (1 << (6 + 24))
+#define BMI160_FLAT (1 << (7 + 24))
#define BMI160_TEMPERATURE_0 0x20
#define BMI160_TEMPERATURE_1 0x21
#define BMI160_FIFO_LENGTH_0 0x22
#define BMI160_FIFO_LENGTH_1 0x23
+#define BMI160_FIFO_LENGTH_MASK ((1 << 11) - 1)
#define BMI160_FIFO_DATA 0x24
+enum fifo_header {
+ BMI160_EMPTY = 0x80,
+ BMI160_SKIP = 0x40,
+ BMI160_TIME = 0x44,
+ BMI160_CONFIG = 0x48
+};
+
+#define BMI160_FH_MODE_MASK 0xc0
+#define BMI160_FH_PARM_OFFSET 2
+#define BMI160_FH_PARM_MASK (0x7 << BMI160_FH_PARM_OFFSET)
+#define BMI160_FH_EXT_MASK 0x03
+
#define BMI160_ACC_CONF 0x40
-#define BMI160_GSEL_2G 0X03
-#define BMI160_GSEL_4G 0X05
-#define BMI160_GSEL_8G 0X08
-#define BMI160_GSEL_16G 0X0C
+#define BMI160_GSEL_2G 0x03
+#define BMI160_GSEL_4G 0x05
+#define BMI160_GSEL_8G 0x08
+#define BMI160_GSEL_16G 0x0c
#define BMI160_ODR_MASK 0x0F
-#define BMI160_ACC_BW_OFFSET 4
+#define BMI160_ACC_BW_OFFSET 4
#define BMI160_ACC_BW_MASK (0x7 << BMI160_ACC_BW_OFFSET)
#define BMI160_ACC_RANGE 0x41
@@ -119,6 +162,18 @@
#define BMI160_FIFO_DOWNS 0x45
#define BMI160_FIFO_CONFIG_0 0x46
#define BMI160_FIFO_CONFIG_1 0x47
+#define BMI160_FIFO_TAG_TIME_EN (1 << 1)
+#define BMI160_FIFO_TAG_INT2_EN (1 << 2)
+#define BMI160_FIFO_TAG_INT1_EN (1 << 3)
+#define BMI160_FIFO_HEADER_EN (1 << 4)
+#define BMI160_FIFO_MAG_EN (1 << 5)
+#define BMI160_FIFO_ACC_EN (1 << 6)
+#define BMI160_FIFO_GYR_EN (1 << 7)
+#define BMI160_FIFO_TARG_INT(_i) CONCAT3(BMI160_FIFO_TAG_INT, _i, _EN)
+#define BMI160_FIFO_SENSOR_EN(_sensor) \
+ ((_sensor) == MOTIONSENSE_TYPE_ACCEL ? BMI160_FIFO_ACC_EN : \
+ ((_sensor) == MOTIONSENSE_TYPE_GYRO ? BMI160_FIFO_GYR_EN : \
+ BMI160_FIFO_MAG_EN))
#define BMI160_MAG_IF_0 0x4b
#define BMI160_MAG_I2C_ADDRESS BMI160_MAG_IF_0
@@ -142,16 +197,71 @@
#define BMI160_MAG_I2C_READ_DATA BMI160_MAG_X_L_G
#define BMI160_INT_EN_0 0x50
+#define BMI160_INT_ANYMO_X_EN (1 << 0)
+#define BMI160_INT_ANYMO_Y_EN (1 << 1)
+#define BMI160_INT_ANYMO_Z_EN (1 << 2)
+#define BMI160_INT_D_TAP_EN (1 << 4)
+#define BMI160_INT_S_TAP_EN (1 << 5)
+#define BMI160_INT_ORIENT_EN (1 << 6)
+#define BMI160_INT_FLAT_EN (1 << 7)
#define BMI160_INT_EN_1 0x51
+#define BMI160_INT_HIGHG_X_EN (1 << 0)
+#define BMI160_INT_HIGHG_Y_EN (1 << 1)
+#define BMI160_INT_HIGHG_Z_EN (1 << 2)
+#define BMI160_INT_LOW_EN (1 << 3)
+#define BMI160_INT_DRDY_EN (1 << 4)
+#define BMI160_INT_FFUL_EN (1 << 5)
+#define BMI160_INT_FWM_EN (1 << 6)
#define BMI160_INT_EN_2 0x52
+#define BMI160_INT_NOMOX_EN (1 << 0)
+#define BMI160_INT_NOMOY_EN (1 << 1)
+#define BMI160_INT_NOMOZ_EN (1 << 2)
+#define BMI160_INT_STEP_DET_EN (1 << 3)
#define BMI160_INT_OUT_CTRL 0x53
+#define BMI160_INT_EDGE_CTRL (1 << 0)
+#define BMI160_INT_LVL_CTRL (1 << 1)
+#define BMI160_INT_OD (1 << 2)
+#define BMI160_INT_OUTPUT_EN (1 << 3)
+#define BMI160_INT1_CTRL_OFFSET 0
+#define BMI160_INT2_CTRL_OFFSET 4
+#define BMI160_INT_CTRL(_i, _bit) \
+(CONCAT2(BMI160_INT_, _bit) << CONCAT3(BMI160_INT, _i, _CTRL_OFFSET))
+
#define BMI160_INT_LATCH 0x54
+#define BMI160_INT1_INPUT_EN (1 << 4)
+#define BMI160_INT2_INPUT_EN (1 << 5)
+#define BMI160_LATCH_MASK 0xf
+#define BMI160_LATCH_NONE 0
+#define BMI160_LATCH_FOREVER 0xf
#define BMI160_INT_MAP_0 0x55
+#define BMI160_INT_LOWG_STEP (1 << 0)
+#define BMI160_INT_HIGHG (1 << 1)
+#define BMI160_INT_ANYMOTION (1 << 2)
+#define BMI160_INT_NOMOTION (1 << 3)
+#define BMI160_INT_D_TAP (1 << 4)
+#define BMI160_INT_S_TAP (1 << 5)
+#define BMI160_INT_ORIENT (1 << 6)
+#define BMI160_INT_FLAT (1 << 7)
+
#define BMI160_INT_MAP_1 0x56
+#define BMI160_INT_PMU_TRIG (1 << 0)
+#define BMI160_INT_FFULL (1 << 1)
+#define BMI160_INT_FWM (1 << 2)
+#define BMI160_INT_DRDY (1 << 3)
+#define BMI160_INT1_MAP_OFFSET 4
+#define BMI160_INT2_MAP_OFFSET 0
+#define BMI160_INT_MAP(_i, _bit) \
+(CONCAT2(BMI160_INT_, _bit) << CONCAT3(BMI160_INT, _i, _MAP_OFFSET))
+#define BMI160_INT_FIFO_MAP BMI160_INT_MAP_1
+
#define BMI160_INT_MAP_2 0x57
+#define BMI160_INT_MAP_INT_1 BMI160_INT_MAP_0
+#define BMI160_INT_MAP_INT_2 BMI160_INT_MAP_2
+#define BMI160_INT_MAP_REG(_i) CONCAT2(BMI160_INT_MAP_INT_, _i)
+
#define BMI160_INT_DATA_0 0x58
#define BMI160_INT_DATA_1 0x59
@@ -193,8 +303,10 @@
#define BMI160_CMD_MAG_MODE_SUSP 0x18
#define BMI160_CMD_MAG_MODE_NORMAL 0x19
#define BMI160_CMD_MAG_MODE_LOWPOWER 0x1a
-#define BMI160_CMD_MODE_NORMAL(_sensor) (0x11 + 4 * (_sensor))
-#define BMI160_CMD_MODE_SUSPEND(_sensor) (0x10 + 4 * (_sensor))
+#define BMI160_CMD_MODE_SUSPEND(_sensor) \
+ (BMI160_CMD_ACC_MODE_SUSP + 4 * (_sensor))
+#define BMI160_CMD_MODE_NORMAL(_sensor) \
+ (BMI160_CMD_ACC_MODE_NORMAL + 4 * (_sensor))
#define BMI160_CMD_FIFO_FLUSH 0xb0
#define BMI160_CMD_INT_RESET 0xb1
@@ -221,52 +333,6 @@
#define BMI160_FF_DATA_LEN_GYR 6
#define BMI160_FF_DATA_LEN_MAG 8
-#if 0
-#define BMI160_DPS_SEL_245 (0 << 3)
-#define BMI160_DPS_SEL_500 (1 << 3)
-#define BMI160_DPS_SEL_1000 (2 << 3)
-#define BMI160_DPS_SEL_2000 (3 << 3)
-#define BMI160_GSEL_2G (0 << 3)
-#define BMI160_GSEL_4G (2 << 3)
-#define BMI160_GSEL_8G (3 << 3)
-
-#define BMI160_RANGE_MASK (3 << 3)
-
-#define BMI160_ODR_PD (0 << 5)
-#define BMI160_ODR_10HZ (1 << 5)
-#define BMI160_ODR_15HZ (1 << 5)
-#define BMI160_ODR_50HZ (2 << 5)
-#define BMI160_ODR_59HZ (2 << 5)
-#define BMI160_ODR_119HZ (3 << 5)
-#define BMI160_ODR_238HZ (4 << 5)
-#define BMI160_ODR_476HZ (5 << 5)
-#define BMI160_ODR_952HZ (6 << 5)
-
-#define BMI160_ODR_MASK (7 << 5)
-
-/*
- * Register : STATUS_REG
- * Address : 0X27
- */
-enum bmi160_status {
- BMI160_STS_DOWN = 0x00,
- BMI160_STS_XLDA_UP = 0x01,
- BMI160_STS_GDA_UP = 0x02,
-};
-#define BMI160_STS_XLDA_MASK 0x01
-#define BMI160_STS_GDA_MASK 0x02
-
-/*
- * Register : CTRL_REG8
- * Address : 0X22
- * Bit Group Name: BDU
- */
-enum bmi160_bdu {
- BMI160_BDU_DISABLE = 0x00,
- BMI160_BDU_ENABLE = 0x40,
-};
-#endif
-
/* Sensor resolution in number of bits. This sensor has fixed resolution. */
#define BMI160_RESOLUTION 16
@@ -285,8 +351,19 @@ enum bmi160_running_mode {
};
#define BMI160_FLAG_SEC_I2C_ENABLED (1 << 0)
+
struct bmi160_drv_data_t {
struct motion_data_t saved_data[3];
uint8_t flags;
};
+
+#define BMI160_GET_DATA(_s) \
+ ((struct bmi160_drv_data_t *)(_s)->drv_data)
+#define BMI160_GET_SAVED_DATA(_s) \
+ (&BMI160_GET_DATA(_s)->saved_data[(_s)->type])
+
+extern struct bmi160_drv_data_t g_bmi160_data;
+
+void bmi160_interrupt(enum gpio_signal signal);
+
#endif /* __CROS_EC_ACCELGYRO_BMI160_H */