summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBolat Dinc <bolat.dinc@intel.com>2015-03-26 12:15:18 -0700
committerchrome-bot <chrome-bot@chromium.org>2015-10-20 15:02:49 -0700
commit7b9ec099105a390b5301be6c12036061ecc8721e (patch)
tree6904dee1e9ccfe0152f0aeb13e0aeca4648e7b30
parentceb35e56584fbf7ece433c444b8de7bae0ba77be (diff)
downloadchrome-ec-7b9ec099105a390b5301be6c12036061ecc8721e.tar.gz
driver: Add L3GD20H gyrometer basic driver support
BUG=chrome-os-partner:40465 BRANCH=None TEST=Added Gyro config to test L3GD20H Gyro sensor in Kunimitsu. Able to read the gyro data from "accelinfo" console command. Rotated the sensor in X,Y,Z axis, respective axis values change. Fits into the existing accel/gyro framework. Change-Id: I19369560ddad5160c2fc9c7ef9823bd37b5389fa Signed-off-by: Bolat Dinc <bolat.dinc@intel.com> Signed-off-by: Divya Jyothi <divya.jyothi@intel.com> Signed-off-by: Vijay Hiremath <vijay.p.hiremath@intel.com> Reviewed-on: https://chromium-review.googlesource.com/264650 Commit-Ready: Vijay P Hiremath <vijay.p.hiremath@intel.com> Tested-by: Vijay P Hiremath <vijay.p.hiremath@intel.com> Reviewed-by: Shawn N <shawnn@chromium.org>
-rw-r--r--driver/build.mk3
-rw-r--r--driver/gyro_l3gd20h.c437
-rw-r--r--driver/gyro_l3gd20h.h102
-rw-r--r--include/config.h3
-rw-r--r--include/ec_commands.h1
5 files changed, 546 insertions, 0 deletions
diff --git a/driver/build.mk b/driver/build.mk
index e87a1896e7..48e8c47825 100644
--- a/driver/build.mk
+++ b/driver/build.mk
@@ -13,6 +13,9 @@ driver-$(CONFIG_ACCELGYRO_LSM6DS0)+=accelgyro_lsm6ds0.o
driver-$(CONFIG_ACCELGYRO_BMI160)+=accelgyro_bmi160.o
driver-$(CONFIG_MAG_BMI160_BMM150)+=mag_bmm150.o
+# Gyrometers
+driver-$(CONFIG_GYRO_L3GD20H)+=gyro_l3gd20h.o
+
# ALS drivers
driver-$(CONFIG_ALS_ISL29035)+=als_isl29035.o
driver-$(CONFIG_ALS_OPT3001)+=als_opt3001.o
diff --git a/driver/gyro_l3gd20h.c b/driver/gyro_l3gd20h.c
new file mode 100644
index 0000000000..1137728463
--- /dev/null
+++ b/driver/gyro_l3gd20h.c
@@ -0,0 +1,437 @@
+/* Copyright 2015 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * L3GD20H gyro module for Chrome EC 3D digital gyroscope.
+ */
+
+#include "accelgyro.h"
+#include "common.h"
+#include "console.h"
+#include "driver/gyro_l3gd20h.h"
+#include "hooks.h"
+#include "i2c.h"
+#include "task.h"
+#include "util.h"
+
+#define CPUTS(outstr) cputs(CC_ACCEL, outstr)
+#define CPRINTF(format, args...) cprintf(CC_ACCEL, format, ## args)
+
+/*
+ * Struct for pairing an engineering value with the register value for a
+ * parameter.
+ */
+struct gyro_param_pair {
+ int val; /* Value in engineering units. */
+ int reg_val; /* Corresponding register value. */
+};
+
+/*
+ * List of angular rate range values in +/-dps's
+ * and their associated register values.
+ */
+const struct gyro_param_pair dps_ranges[] = {
+ {245, L3GD20_DPS_SEL_245},
+ {500, L3GD20_DPS_SEL_500},
+ {2000, L3GD20_DPS_SEL_2000_0},
+ {2000, L3GD20_DPS_SEL_2000_1}
+};
+
+static inline const struct gyro_param_pair *get_range_table(
+ enum motionsensor_type type, int *psize)
+{
+ if (psize)
+ *psize = ARRAY_SIZE(dps_ranges);
+ return dps_ranges;
+}
+
+/* List of ODR values in mHz and their associated register values. */
+const struct gyro_param_pair gyro_odr[] = {
+ {0, L3GD20_ODR_PD | L3GD20_LOW_ODR_MASK},
+ {12500, L3GD20_ODR_12_5HZ | L3GD20_ODR_PD_MASK | L3GD20_LOW_ODR_MASK},
+ {25000, L3GD20_ODR_25HZ | L3GD20_ODR_PD_MASK | L3GD20_LOW_ODR_MASK},
+ {50000, L3GD20_ODR_50HZ_0 | L3GD20_ODR_PD_MASK | L3GD20_LOW_ODR_MASK},
+ {50000, L3GD20_ODR_50HZ_1 | L3GD20_ODR_PD_MASK | L3GD20_LOW_ODR_MASK},
+ {100000, L3GD20_ODR_100HZ | L3GD20_ODR_PD_MASK},
+ {200000, L3GD20_ODR_200HZ | L3GD20_ODR_PD_MASK},
+ {400000, L3GD20_ODR_400HZ | L3GD20_ODR_PD_MASK},
+ {800000, L3GD20_ODR_800HZ | L3GD20_ODR_PD_MASK},
+};
+
+static inline const struct gyro_param_pair *get_odr_table(
+ enum motionsensor_type type, int *psize)
+{
+ if (psize)
+ *psize = ARRAY_SIZE(gyro_odr);
+ return gyro_odr;
+}
+
+static inline int get_ctrl_reg(enum motionsensor_type type)
+{
+ return L3GD20_CTRL_REG1;
+}
+
+static inline int get_xyz_reg(enum motionsensor_type type)
+{
+ return L3GD20_OUT_X_L | (1 << 7);
+}
+
+/**
+ * @return reg value that matches the given engineering value passed in.
+ * The round_up flag is used to specify whether to round up or down.
+ * Note, this function always returns a valid reg value. If the request is
+ * outside the range of values, it returns the closest valid reg value.
+ */
+static int get_reg_val(const int eng_val, const int round_up,
+ const struct gyro_param_pair *pairs, const int size)
+{
+ int i;
+ for (i = 0; i < size - 1; i++) {
+ if (eng_val <= pairs[i].val)
+ break;
+
+ if (eng_val < pairs[i+1].val) {
+ if (round_up)
+ i += 1;
+ break;
+ }
+ }
+ return pairs[i].reg_val;
+}
+
+/**
+ * @return engineering value that matches the given reg val
+ */
+static int get_engineering_val(const int reg_val,
+ const struct gyro_param_pair *pairs, const int size)
+{
+ int i;
+ for (i = 0; i < size; i++) {
+ if (reg_val == pairs[i].reg_val)
+ break;
+ }
+ return pairs[i].val;
+}
+
+/**
+ * Read register from Gyrometer.
+ */
+static inline int raw_read8(const int addr, const int reg, int *data_ptr)
+{
+ return i2c_read8(I2C_PORT_GYRO, addr, reg, data_ptr);
+}
+
+/**
+ * Write register from Gyrometer.
+ */
+static inline int raw_write8(const int addr, const int reg, int data)
+{
+ return i2c_write8(I2C_PORT_GYRO, addr, reg, data);
+}
+
+static int set_range(const struct motion_sensor_t *s,
+ int range,
+ int rnd)
+{
+ int ret, ctrl_val, range_tbl_size;
+ uint8_t ctrl_reg, reg_val;
+ const struct gyro_param_pair *ranges;
+ struct l3gd20_data *data = (struct l3gd20_data *)s->drv_data;
+
+ ctrl_reg = L3GD20_CTRL_REG4;
+ ranges = get_range_table(s->type, &range_tbl_size);
+
+ reg_val = get_reg_val(range, rnd, ranges, range_tbl_size);
+
+ /*
+ * Lock Gyro resource to prevent another task from attempting
+ * to write Gyro parameters until we are done.
+ */
+ mutex_lock(s->mutex);
+
+ ret = raw_read8(s->addr, ctrl_reg, &ctrl_val);
+ if (ret != EC_SUCCESS)
+ goto gyro_cleanup;
+
+ ctrl_val = (ctrl_val & ~L3GD20_RANGE_MASK) | reg_val;
+ ret = raw_write8(s->addr, ctrl_reg, ctrl_val);
+
+ /* Now that we have set the range, update the driver's value. */
+ if (ret == EC_SUCCESS)
+ data->base.range = get_engineering_val(reg_val, ranges,
+ range_tbl_size);
+
+gyro_cleanup:
+ mutex_unlock(s->mutex);
+ return EC_SUCCESS;
+}
+
+static int get_range(const struct motion_sensor_t *s)
+{
+ struct l3gd20_data *data = (struct l3gd20_data *)s->drv_data;
+
+ return data->base.range;
+}
+
+static int set_resolution(const struct motion_sensor_t *s,
+ int res,
+ int rnd)
+{
+ /* Only one resolution, L3GD20_RESOLUTION, so nothing to do. */
+ return EC_SUCCESS;
+}
+
+static int get_resolution(const struct motion_sensor_t *s)
+{
+ return L3GD20_RESOLUTION;
+}
+
+static int set_data_rate(const struct motion_sensor_t *s,
+ int rate,
+ int rnd)
+{
+ int ret, val, odr_tbl_size;
+ uint8_t ctrl_reg, reg_val;
+ const struct gyro_param_pair *data_rates;
+ struct l3gd20_data *data = s->drv_data;
+
+ ctrl_reg = get_ctrl_reg(s->type);
+ data_rates = get_odr_table(s->type, &odr_tbl_size);
+ reg_val = get_reg_val(rate, rnd, data_rates, odr_tbl_size);
+
+ /*
+ * Lock gyro resource to prevent another task from attempting
+ * to write gyro parameters until we are done.
+ */
+ mutex_lock(s->mutex);
+
+ ret = raw_read8(s->addr, ctrl_reg, &val);
+ if (ret != EC_SUCCESS)
+ goto gyro_cleanup;
+
+ val = (val & ~(L3GD20_ODR_MASK | L3GD20_ODR_PD_MASK)) |
+ (reg_val & ~L3GD20_LOW_ODR_MASK);
+ ret = raw_write8(s->addr, ctrl_reg, val);
+
+ /* Now that we have set the odr, update the driver's value. */
+ if (ret == EC_SUCCESS)
+ data->base.odr = get_engineering_val(reg_val, data_rates,
+ odr_tbl_size);
+
+ ret = raw_read8(s->addr, L3GD20_LOW_ODR, &val);
+ if (ret != EC_SUCCESS)
+ goto gyro_cleanup;
+
+ /* We need to clear low_ODR bit for higher data rates */
+ if (reg_val & L3GD20_LOW_ODR_MASK)
+ val |= 1;
+ else
+ val &= ~1;
+
+ ret = raw_write8(s->addr, L3GD20_LOW_ODR, val);
+ if (ret != EC_SUCCESS)
+ goto gyro_cleanup;
+
+ /* CTRL_REG5 24h
+ * [7] low-power mode = 0;
+ * [6] fifo disabled = 0;
+ * [5] Stop on fth = 0;
+ * [4] High pass filter enable = 1;
+ * [3:2] int1_sel = 0;
+ * [1:0] out_sel = 1;
+ */
+ ret = raw_read8(s->addr, L3GD20_CTRL_REG5, &val);
+ if (ret != EC_SUCCESS)
+ goto gyro_cleanup;
+
+ val |= (1 << 4); /* high-pass filter enabled */
+ val |= (1 << 0); /* data in data reg are high-pass filtered */
+ ret = raw_write8(s->addr, L3GD20_CTRL_REG5, val);
+ if (ret != EC_SUCCESS)
+ goto gyro_cleanup;
+
+ ret = raw_read8(s->addr, L3GD20_CTRL_REG2, &val);
+ if (ret != EC_SUCCESS)
+ goto gyro_cleanup;
+
+ /*
+ * Table 25. High pass filter mode configuration
+ * Table 26. High pass filter cut off frequency configuration
+ */
+ val &= 0xf0;
+ val |= 0x04;
+ ret = raw_write8(s->addr, L3GD20_CTRL_REG2, val);
+
+gyro_cleanup:
+ mutex_unlock(s->mutex);
+ return ret;
+}
+
+static int get_data_rate(const struct motion_sensor_t *s)
+{
+ struct l3gd20_data *data = (struct l3gd20_data *)s->drv_data;
+
+ return data->base.odr;
+}
+
+static int set_offset(const struct motion_sensor_t *s,
+ const int16_t *offset,
+ int16_t temp)
+{
+ /* temperature is ignored */
+ struct l3gd20_data *data = s->drv_data;
+ data->offset[X] = offset[X];
+ data->offset[Y] = offset[Y];
+ data->offset[Z] = offset[Z];
+ return EC_SUCCESS;
+}
+
+static int get_offset(const struct motion_sensor_t *s,
+ int16_t *offset,
+ int16_t *temp)
+{
+ struct l3gd20_data *data = s->drv_data;
+ offset[X] = data->offset[X];
+ offset[Y] = data->offset[Y];
+ offset[Z] = data->offset[Z];
+ *temp = EC_MOTION_SENSE_INVALID_CALIB_TEMP;
+ return EC_SUCCESS;
+}
+
+#ifdef CONFIG_ACCEL_INTERRUPTS
+static int set_interrupt(const struct motion_sensor_t *s,
+ unsigned int threshold)
+{
+ /* Currently unsupported. */
+ return EC_ERROR_UNKNOWN;
+}
+#endif
+
+static int is_data_ready(const struct motion_sensor_t *s, int *ready)
+{
+ int ret, tmp;
+
+ ret = raw_read8(s->addr, L3GD20_STATUS_REG, &tmp);
+
+ if (ret != EC_SUCCESS) {
+ CPRINTF("[%T %s type:0x%X RS Error]", s->name, s->type);
+ return ret;
+ }
+
+ *ready = (tmp & L3GD20_STS_ZYXDA_MASK) ? 1 : 0;
+
+ return EC_SUCCESS;
+}
+
+static int read(const struct motion_sensor_t *s, vector_3_t v)
+{
+ uint8_t raw[6];
+ uint8_t xyz_reg;
+ int ret, range, i, tmp = 0;
+ struct l3gd20_data *data = s->drv_data;
+
+ ret = is_data_ready(s, &tmp);
+ if (ret != EC_SUCCESS)
+ return ret;
+
+ /*
+ * If sensor data is not ready, return the previous read data.
+ * Note: return success so that motion senor task can read again
+ * to get the latest updated sensor data quickly.
+ */
+ if (!tmp) {
+ if (v != s->raw_xyz)
+ memcpy(v, s->raw_xyz, sizeof(s->raw_xyz));
+ return EC_SUCCESS;
+ }
+
+ xyz_reg = get_xyz_reg(s->type);
+
+ /* Read 6 bytes starting at xyz_reg */
+ i2c_lock(I2C_PORT_GYRO, 1);
+ ret = i2c_xfer(I2C_PORT_GYRO, s->addr,
+ &xyz_reg, 1, raw, 6, I2C_XFER_SINGLE);
+ i2c_lock(I2C_PORT_GYRO, 0);
+
+ if (ret != EC_SUCCESS) {
+ CPRINTF("[%T %s type:0x%X RD XYZ Error]",
+ s->name, s->type);
+ return ret;
+ }
+
+ for (i = X; i <= Z; i++)
+ v[i] = ((int16_t)((raw[i * 2 + 1] << 8) | raw[i * 2]));
+
+ if (s->rot_standard_ref)
+ rotate(v, *s->rot_standard_ref, v);
+
+ /* apply offset in the device coordinates */
+ range = get_range(s);
+ for (i = X; i <= Z; i++)
+ v[i] += (data->offset[i] << 5) / range;
+
+ return EC_SUCCESS;
+}
+
+static int init(const struct motion_sensor_t *s)
+{
+ int ret = 0, tmp;
+
+ ret = raw_read8(s->addr, L3GD20_WHO_AM_I_REG, &tmp);
+ if (ret)
+ return EC_ERROR_UNKNOWN;
+
+ if (tmp != L3GD20_WHO_AM_I)
+ return EC_ERROR_ACCESS_DENIED;
+
+ /* All axes are enabled */
+ ret = raw_write8(s->addr, L3GD20_CTRL_REG1, 0x0f);
+ if (ret)
+ return EC_ERROR_UNKNOWN;
+
+ mutex_lock(s->mutex);
+ ret = raw_read8(s->addr, L3GD20_CTRL_REG4, &tmp);
+ if (ret) {
+ mutex_unlock(s->mutex);
+ return EC_ERROR_UNKNOWN;
+ }
+
+ tmp |= L3GD20_BDU_ENABLE;
+ ret = raw_write8(s->addr, L3GD20_CTRL_REG4, tmp);
+ mutex_unlock(s->mutex);
+ if (ret)
+ return EC_ERROR_UNKNOWN;
+
+ /* Config GYRO ODR */
+ ret = set_data_rate(s, s->default_range, 1);
+ if (ret)
+ return EC_ERROR_UNKNOWN;
+
+ /* Config GYRO Range */
+ ret = set_range(s, s->default_range, 1);
+ if (ret)
+ return EC_ERROR_UNKNOWN;
+
+ CPRINTF("[%T %s: MS Done Init type:0x%X range:%d]\n",
+ s->name, s->type, get_range(s));
+ return ret;
+}
+
+const struct accelgyro_drv l3gd20h_drv = {
+ .init = init,
+ .read = read,
+ .set_range = set_range,
+ .get_range = get_range,
+ .set_resolution = set_resolution,
+ .get_resolution = get_resolution,
+ .set_data_rate = set_data_rate,
+ .get_data_rate = get_data_rate,
+ .set_offset = set_offset,
+ .get_offset = get_offset,
+ .perform_calib = NULL,
+#ifdef CONFIG_ACCEL_INTERRUPTS
+ .set_interrupt = set_interrupt,
+#endif
+};
diff --git a/driver/gyro_l3gd20h.h b/driver/gyro_l3gd20h.h
new file mode 100644
index 0000000000..73c0253d27
--- /dev/null
+++ b/driver/gyro_l3gd20h.h
@@ -0,0 +1,102 @@
+/* Copyright 2015 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* L3GD20H gyro module for Chrome EC */
+
+#ifndef __CROS_EC_GYRO_L3GD20H_H
+#define __CROS_EC_GYRO_L3GD20H_H
+
+#include "accelgyro.h"
+#include "task.h"
+
+/*
+ * 7-bit address is 110101Xb. Where 'X' is determined
+ * by the voltage on the ADDR pin.
+ */
+#define L3GD20_ADDR0 0xd4
+#define L3GD20_ADDR1 0xd6
+
+/* who am I */
+#define L3GD20_WHO_AM_I 0xd7
+
+/* Chip specific registers. */
+#define L3GD20_WHO_AM_I_REG 0x0f
+#define L3GD20_CTRL_REG1 0x20
+#define L3GD20_CTRL_REG2 0x21
+#define L3GD20_CTRL_REG3 0x22
+#define L3GD20_CTRL_REG4 0x23
+#define L3GD20_CTRL_REG5 0x24
+#define L3GD20_CTRL_REFERENCE 0x25
+#define L3GD20_OUT_TEMP 0x26
+#define L3GD20_STATUS_REG 0x27
+#define L3GD20_OUT_X_L 0x28
+#define L3GD20_OUT_X_H 0x29
+#define L3GD20_OUT_Y_L 0x2a
+#define L3GD20_OUT_Y_H 0x2b
+#define L3GD20_OUT_Z_L 0x2c
+#define L3GD20_OUT_Z_H 0x2d
+#define L3GD20_FIFO_CTRL_REG 0x2e
+#define L3GD20_FIFO_SRC_REG 0x2f
+#define L3GD20_INT1_CFG 0x30
+#define L3GD20_INT1_SRC 0x31
+#define L3GD20_INT1_TSH_XH 0x32
+#define L3GD20_INT1_TSH_XL 0x33
+#define L3GD20_INT1_TSH_YH 0x34
+#define L3GD20_INT1_TSH_YL 0x35
+#define L3GD20_INT1_TSH_ZH 0x36
+#define L3GD20_INT1_TSH_ZL 0x37
+#define L3GD20_INT1_DURATION 0x38
+#define L3GD20_LOW_ODR 0x39
+
+#define L3GD20_DPS_SEL_245 (0 << 4)
+#define L3GD20_DPS_SEL_500 (1 << 4)
+#define L3GD20_DPS_SEL_2000_0 (2 << 4)
+#define L3GD20_DPS_SEL_2000_1 (3 << 4)
+
+#define L3GD20_ODR_PD (0 << 3)
+#define L3GD20_ODR_12_5HZ (0 << 6)
+#define L3GD20_ODR_25HZ (1 << 6)
+#define L3GD20_ODR_50HZ_0 (2 << 6)
+#define L3GD20_ODR_50HZ_1 (3 << 6)
+#define L3GD20_ODR_100HZ (0 << 6)
+#define L3GD20_ODR_200HZ (1 << 6)
+#define L3GD20_ODR_400HZ (2 << 6)
+#define L3GD20_ODR_800HZ (3 << 6)
+
+#define L3GD20_ODR_MASK (3 << 6)
+#define L3GD20_STS_ZYXDA_MASK (1 << 3)
+#define L3GD20_RANGE_MASK (3 << 4)
+#define L3GD20_LOW_ODR_MASK (1 << 0)
+#define L3GD20_ODR_PD_MASK (1 << 3)
+
+/*
+ * Register : STATUS_REG
+ * Address : 0X27
+ */
+enum l3gd20_status {
+ L3GD20_STS_DOWN = 0x00,
+ L3GD20_STS_ZYXDA_UP = 0x08,
+};
+
+/*
+ * Register : CTRL_REG4
+ * Address : 0X23
+ * Bit Group Name: BDU
+ */
+enum l3gd20_bdu {
+ L3GD20_BDU_DISABLE = 0x00,
+ L3GD20_BDU_ENABLE = 0x80,
+};
+
+/* Sensor resolution in number of bits. This sensor has fixed resolution. */
+#define L3GD20_RESOLUTION 16
+
+extern const struct accelgyro_drv l3gd20h_drv;
+struct l3gd20_data {
+ struct accelgyro_saved_data_t base;
+ int16_t offset[3];
+};
+
+#endif /* __CROS_EC_GYRO_L3GD20H_H */
diff --git a/include/config.h b/include/config.h
index e93001087c..801bde198a 100644
--- a/include/config.h
+++ b/include/config.h
@@ -78,6 +78,9 @@
*/
#undef CONFIG_ACCELGYRO_BMI160_INT_EVENT
+/* Specify type of Gyrometers attached. */
+#undef CONFIG_GYRO_L3GD20H
+
/* Compile chip support for analog-to-digital convertor */
#undef CONFIG_ADC
diff --git a/include/ec_commands.h b/include/ec_commands.h
index 0fd2093b64..b959db783e 100644
--- a/include/ec_commands.h
+++ b/include/ec_commands.h
@@ -1744,6 +1744,7 @@ enum motionsensor_chip {
MOTIONSENSE_CHIP_SI1142 = 4,
MOTIONSENSE_CHIP_SI1143 = 5,
MOTIONSENSE_CHIP_KX022 = 6,
+ MOTIONSENSE_CHIP_L3GD20H = 7,
};
struct ec_response_motion_sensor_data {