From 1ba4f4c7dc0919b648f2079a00c3183fbb1d3c6f Mon Sep 17 00:00:00 2001 From: David Huang Date: Tue, 12 Jan 2021 13:40:11 +0800 Subject: aleena: Add base accel/gyro config for icm-426xx Add icm-426xx config for new second source base accel/gyro. BUG=none BRANCH=grunt TEST=Check ectool motionsense and get x,y,z data. Signed-off-by: David Huang Change-Id: I9a772fe700e97e29c7110c971683fa1c7f6c2f23 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2626796 Reviewed-by: Keith Short (cherry picked from commit 46afea52cc38b00505b24b601b732f035708975b) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2626797 Commit-Queue: Keith Short --- board/aleena/board.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ board/aleena/board.h | 5 +++ board/aleena/gpio.inc | 2 +- 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/board/aleena/board.c b/board/aleena/board.c index 987ef36904..465ca59f3d 100644 --- a/board/aleena/board.c +++ b/board/aleena/board.c @@ -7,6 +7,9 @@ #include "button.h" #include "driver/accelgyro_bmi160.h" +#include "console.h" +#include "driver/accelgyro_icm_common.h" +#include "driver/accelgyro_icm426xx.h" #include "driver/led/lm3630a.h" #include "extpower.h" #include "hooks.h" @@ -18,6 +21,7 @@ #include "pwm_chip.h" #include "switch.h" #include "tablet_mode.h" +#include "task.h" #include "gpio_list.h" @@ -50,6 +54,111 @@ const struct pwm_t pwm_channels[] = { }; BUILD_ASSERT(ARRAY_SIZE(pwm_channels) == PWM_CH_COUNT); +#ifdef HAS_TASK_MOTIONSENSE +/* Motion sensors */ +static struct mutex icm426xx_mutex; + +static struct icm_drv_data_t g_icm426xx_data; + +enum base_accelgyro_type { + BASE_GYRO_NONE = 0, + BASE_GYRO_BMI160 = 1, + BASE_GYRO_ICM426XX = 2, +}; + +const mat33_fp_t base_standard_ref_icm426xx = { + { 0, FLOAT_TO_FP(-1), 0}, + { FLOAT_TO_FP(-1), 0, 0}, + { 0, 0, FLOAT_TO_FP(1)} +}; + +struct motion_sensor_t icm426xx_base_accel = { + .name = "Base Accel", + .active_mask = SENSOR_ACTIVE_S0_S3, + .chip = MOTIONSENSE_CHIP_ICM426XX, + .type = MOTIONSENSE_TYPE_ACCEL, + .location = MOTIONSENSE_LOC_BASE, + .drv = &icm426xx_drv, + .mutex = &icm426xx_mutex, + .drv_data = &g_icm426xx_data, + .port = I2C_PORT_SENSOR, + .i2c_spi_addr_flags = ICM426XX_ADDR0_FLAGS, + .default_range = 2, /* g, enough for laptop */ + .rot_standard_ref = &base_standard_ref_icm426xx, + .min_frequency = ICM426XX_ACCEL_MIN_FREQ, + .max_frequency = ICM426XX_ACCEL_MAX_FREQ, + .config = { + /* EC use accel for angle detection */ + [SENSOR_CONFIG_EC_S0] = { + .odr = 10000 | ROUND_UP_FLAG, + .ec_rate = 100, + }, + /* EC use accel for angle detection */ + [SENSOR_CONFIG_EC_S3] = { + .odr = 10000 | ROUND_UP_FLAG, + }, + }, +}; + +struct motion_sensor_t icm426xx_base_gyro = { + .name = "Base Gyro", + .active_mask = SENSOR_ACTIVE_S0_S3, + .chip = MOTIONSENSE_CHIP_ICM426XX, + .type = MOTIONSENSE_TYPE_GYRO, + .location = MOTIONSENSE_LOC_BASE, + .drv = &icm426xx_drv, + .mutex = &icm426xx_mutex, + .drv_data = &g_icm426xx_data, + .port = I2C_PORT_SENSOR, + .i2c_spi_addr_flags = ICM426XX_ADDR0_FLAGS, + .default_range = 1000, /* dps */ + .rot_standard_ref = &base_standard_ref_icm426xx, + .min_frequency = ICM426XX_GYRO_MIN_FREQ, + .max_frequency = ICM426XX_GYRO_MAX_FREQ, +}; + +static enum base_accelgyro_type base_accelgyro_config; + +void motion_interrupt(enum gpio_signal signal) +{ + switch (base_accelgyro_config) { + case BASE_GYRO_ICM426XX: + icm426xx_interrupt(signal); + break; + case BASE_GYRO_BMI160: + default: + bmi160_interrupt(signal); + break; + } +} + +static void board_detect_motionsensor(void) +{ + int ret; + int val; + + if (base_accelgyro_config != BASE_GYRO_NONE) + return; + + if (board_is_convertible()) { + /* Check base accelgyro chip */ + ret = icm_read8(&icm426xx_base_accel, + ICM426XX_REG_WHO_AM_I, &val); + if (ret) + ccprints("Get ICM fail."); + if (val == ICM426XX_CHIP_ICM40608) { + motion_sensors[BASE_ACCEL] = icm426xx_base_accel; + motion_sensors[BASE_GYRO] = icm426xx_base_gyro; + } + base_accelgyro_config = (val == ICM426XX_CHIP_ICM40608) + ? BASE_GYRO_ICM426XX : BASE_GYRO_BMI160; + ccprints("Base Accelgyro: %s", (val == ICM426XX_CHIP_ICM40608) + ? "ICM40608" : "BMI160"); + } +} +DECLARE_HOOK(HOOK_CHIPSET_STARTUP, board_detect_motionsensor, + HOOK_PRIO_DEFAULT); + void board_update_sensor_config_from_sku(void) { if (board_is_convertible()) { @@ -64,6 +173,7 @@ void board_update_sensor_config_from_sku(void) GPIO_INPUT | GPIO_PULL_DOWN); } } +#endif static void board_kblight_init(void) { diff --git a/board/aleena/board.h b/board/aleena/board.h index 76350a76e6..e0fd8da555 100644 --- a/board/aleena/board.h +++ b/board/aleena/board.h @@ -37,6 +37,9 @@ #define CONFIG_ACCELGYRO_BMI160 #define CONFIG_ACCELGYRO_BMI160_INT_EVENT \ TASK_EVENT_MOTION_SENSOR_INTERRUPT(BASE_ACCEL) +#define CONFIG_ACCELGYRO_ICM426XX /* Base accel second source*/ +#define CONFIG_ACCELGYRO_ICM426XX_INT_EVENT \ + TASK_EVENT_MOTION_SENSOR_INTERRUPT(BASE_ACCEL) #define CONFIG_ACCEL_INTERRUPTS #define CONFIG_ACCEL_KX022 #define CONFIG_CMD_ACCELS @@ -73,6 +76,8 @@ extern const int keyboard_factory_scan_pins[][2]; extern const int keyboard_factory_scan_pins_used; #endif +void motion_interrupt(enum gpio_signal signal); + #endif /* !__ASSEMBLER__ */ #endif /* __CROS_EC_BOARD_H */ diff --git a/board/aleena/gpio.inc b/board/aleena/gpio.inc index e5cf59ba53..6fefcf88f1 100644 --- a/board/aleena/gpio.inc +++ b/board/aleena/gpio.inc @@ -23,7 +23,7 @@ GPIO_INT(WP_L, PIN(A, 1), GPIO_INT_BOTH, switch_interrupt) GPIO_INT(VOLUME_DOWN_L, PIN(7, 0), GPIO_INT_BOTH | GPIO_PULL_UP, button_interrupt) GPIO_INT(VOLUME_UP_L, PIN(7, 5), GPIO_INT_BOTH | GPIO_PULL_UP, button_interrupt) GPIO_INT(USB_C0_CABLE_DET, PIN(3, 7), GPIO_INT_RISING, anx74xx_cable_det_interrupt) -GPIO_INT(6AXIS_INT_L, PIN(8, 6), GPIO_INT_FALLING | GPIO_SEL_1P8V, bmi160_interrupt) +GPIO_INT(6AXIS_INT_L, PIN(8, 6), GPIO_INT_FALLING | GPIO_SEL_1P8V, motion_interrupt) /* GPIO_INT_BOTH is required for PSL wake from hibernate, but we don't need an interrupt handler. */ GPIO(EC_RST_ODL, PIN(0, 2), GPIO_INT_BOTH | GPIO_HIB_WAKE_HIGH | GPIO_LOCKED) -- cgit v1.2.1