summaryrefslogtreecommitdiff
path: root/board/ampton/board.c
diff options
context:
space:
mode:
Diffstat (limited to 'board/ampton/board.c')
-rw-r--r--board/ampton/board.c147
1 files changed, 144 insertions, 3 deletions
diff --git a/board/ampton/board.c b/board/ampton/board.c
index be6c20f1d3..63e124fa79 100644
--- a/board/ampton/board.c
+++ b/board/ampton/board.c
@@ -10,6 +10,9 @@
#include "button.h"
#include "charge_state.h"
#include "common.h"
+#include "cros_board_info.h"
+#include "driver/accel_kionix.h"
+#include "driver/accelgyro_bmi160.h"
#include "driver/ppc/sn5s330.h"
#include "driver/tcpm/it83xx_pd.h"
#include "driver/tcpm/ps8xxx.h"
@@ -21,13 +24,14 @@
#include "intc.h"
#include "keyboard_scan.h"
#include "lid_switch.h"
+#include "motion_sense.h"
#include "power.h"
#include "power_button.h"
#include "spi.h"
#include "switch.h"
#include "system.h"
-#include "tcpci.h"
#include "tablet_mode.h"
+#include "tcpci.h"
#include "temp_sensor.h"
#include "thermistor.h"
#include "uart.h"
@@ -35,6 +39,8 @@
#include "usbc_ppc.h"
#include "util.h"
+static uint8_t sku_id;
+
static void ppc_interrupt(enum gpio_signal signal)
{
if (signal == GPIO_USB_C0_PD_INT_ODL)
@@ -100,6 +106,127 @@ const struct temp_sensor_t temp_sensors[] = {
};
BUILD_ASSERT(ARRAY_SIZE(temp_sensors) == TEMP_SENSOR_COUNT);
+/* Motion sensors */
+/* Mutexes */
+static struct mutex g_lid_mutex;
+static struct mutex g_base_mutex;
+
+/* Matrix to rotate accelrator into standard reference frame */
+/* TODO(b/118756407): Ampton/Apel: tune motion sensors */
+const mat33_fp_t base_standard_ref = {
+ { 0, FLOAT_TO_FP(-1), 0},
+ { FLOAT_TO_FP(1), 0, 0},
+ { 0, 0, FLOAT_TO_FP(1)}
+};
+
+/* sensor private data */
+static struct kionix_accel_data g_kx022_data;
+static struct bmi160_drv_data_t g_bmi160_data;
+
+/* Drivers */
+struct motion_sensor_t motion_sensors[] = {
+ [LID_ACCEL] = {
+ .name = "Lid Accel",
+ .active_mask = SENSOR_ACTIVE_S0_S3,
+ .chip = MOTIONSENSE_CHIP_KX022,
+ .type = MOTIONSENSE_TYPE_ACCEL,
+ .location = MOTIONSENSE_LOC_LID,
+ .drv = &kionix_accel_drv,
+ .mutex = &g_lid_mutex,
+ .drv_data = &g_kx022_data,
+ .port = I2C_PORT_SENSOR,
+ .addr = KX022_ADDR1,
+ .rot_standard_ref = NULL, /* Identity matrix. */
+ .default_range = 4, /* g */
+ .config = {
+ /* EC use accel for angle detection */
+ [SENSOR_CONFIG_EC_S0] = {
+ .odr = 10000 | ROUND_UP_FLAG,
+ },
+ /* Sensor on for lid angle detection */
+ [SENSOR_CONFIG_EC_S3] = {
+ .odr = 10000 | ROUND_UP_FLAG,
+ },
+ },
+ },
+ [BASE_ACCEL] = {
+ .name = "Base Accel",
+ .active_mask = SENSOR_ACTIVE_S0_S3,
+ .chip = MOTIONSENSE_CHIP_BMI160,
+ .type = MOTIONSENSE_TYPE_ACCEL,
+ .location = MOTIONSENSE_LOC_BASE,
+ .drv = &bmi160_drv,
+ .mutex = &g_base_mutex,
+ .drv_data = &g_bmi160_data,
+ .port = I2C_PORT_SENSOR,
+ .addr = BMI160_ADDR0,
+ .rot_standard_ref = &base_standard_ref,
+ .default_range = 4, /* g */
+ .min_frequency = BMI160_ACCEL_MIN_FREQ,
+ .max_frequency = BMI160_ACCEL_MAX_FREQ,
+ .config = {
+ /* EC use accel for angle detection */
+ [SENSOR_CONFIG_EC_S0] = {
+ .odr = 13000 | ROUND_UP_FLAG,
+ .ec_rate = 100 * MSEC,
+ },
+ /* Sensor on for angle detection */
+ [SENSOR_CONFIG_EC_S3] = {
+ .odr = 10000 | ROUND_UP_FLAG,
+ .ec_rate = 100 * MSEC,
+ },
+ },
+ },
+ [BASE_GYRO] = {
+ .name = "Base Gyro",
+ .active_mask = SENSOR_ACTIVE_S0_S3,
+ .chip = MOTIONSENSE_CHIP_BMI160,
+ .type = MOTIONSENSE_TYPE_GYRO,
+ .location = MOTIONSENSE_LOC_BASE,
+ .drv = &bmi160_drv,
+ .mutex = &g_base_mutex,
+ .drv_data = &g_bmi160_data,
+ .port = I2C_PORT_SENSOR,
+ .addr = BMI160_ADDR0,
+ .default_range = 1000, /* dps */
+ .rot_standard_ref = &base_standard_ref,
+ .min_frequency = BMI160_GYRO_MIN_FREQ,
+ .max_frequency = BMI160_GYRO_MAX_FREQ,
+ },
+};
+
+unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors);
+
+static int board_is_convertible(void)
+{
+ /* SKU IDs of Ampton & unprovisioned: 1, 2, 255 */
+ return sku_id == 1 || sku_id == 2 || sku_id == 255;
+}
+
+static void board_update_sensor_config_from_sku(void)
+{
+ if (board_is_convertible()) {
+ motion_sensor_count = ARRAY_SIZE(motion_sensors);
+ } else {
+ motion_sensor_count = 0;
+ tablet_disable_switch();
+ }
+}
+
+/* Read CBI from i2c eeprom and initialize variables for board variants */
+static void cbi_init(void)
+{
+ uint32_t val;
+
+ if (cbi_get_sku_id(&val) != EC_SUCCESS)
+ return;
+ sku_id = val;
+ ccprints("SKU: %d", sku_id);
+
+ board_update_sensor_config_from_sku();
+}
+DECLARE_HOOK(HOOK_INIT, cbi_init, HOOK_PRIO_INIT_I2C + 1);
+
void board_hibernate_late(void)
{
/*
@@ -112,10 +239,24 @@ void board_hibernate_late(void)
gpio_set_flags_by_mask(GPIO_KSI, 0xff, GPIO_INPUT);
}
-/* TODO(b/115501243): Ampton/Apel: implement motion sensors in EC code */
-
void board_overcurrent_event(int port)
{
/* TODO(b/78344554): pass this signal upstream once hardware reworked */
cprints(CC_USBPD, "p%d: overcurrent!", port);
}
+
+#ifndef TEST_BUILD
+/* This callback disables keyboard when convertibles are fully open */
+void lid_angle_peripheral_enable(int enable)
+{
+ /*
+ * If the lid is in tablet position via other sensors,
+ * ignore the lid angle, which might be faulty then
+ * disable keyboard.
+ */
+ if (tablet_get_mode())
+ enable = 0;
+ if (board_is_convertible())
+ keyboard_scan_enable(enable, KB_SCAN_DISABLE_LID_ANGLE);
+}
+#endif