From f956457f58ec6db287f75e190066a9f7ebf44c87 Mon Sep 17 00:00:00 2001 From: Andrew McRae Date: Wed, 29 Dec 2021 19:42:21 +1100 Subject: zephyr: Add initial shim for lsm6dso sensor Add the initial shim for the lsm6dso accel/gyro sensor. BUG=b:212497341 TEST=zmake testall BRANCH=none Signed-off-by: Andrew McRae Change-Id: I01b5787f960971a637da743131ef4b74ba07885c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3360326 Reviewed-by: Yuval Peress --- driver/accelgyro_lsm6dso.c | 17 +++++++ driver/accelgyro_lsm6dso.h | 13 +---- include/driver/accelgyro_lsm6dso_public.h | 25 ++++++++++ zephyr/CMakeLists.txt | 2 + zephyr/Kconfig.sensor_devices | 17 ++++++- .../motionsense/driver/cros-ec,lsm6dso-accel.yaml | 13 +++++ .../motionsense/driver/cros-ec,lsm6dso-gyro.yaml | 13 +++++ .../dts/bindings/motionsense/driver/lsm6dso.yaml | 19 ++++++++ .../drvdata/cros-ec,drvdata-lsm6dso.yaml | 18 +++++++ zephyr/shim/include/config_chip.h | 5 ++ .../src/motionsense_driver/lsm6dso-drvinfo.inc | 57 ++++++++++++++++++++++ .../src/motionsense_driver/sensor_drv_list.inc | 3 ++ 12 files changed, 189 insertions(+), 13 deletions(-) create mode 100644 include/driver/accelgyro_lsm6dso_public.h create mode 100644 zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-accel.yaml create mode 100644 zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-gyro.yaml create mode 100644 zephyr/dts/bindings/motionsense/driver/lsm6dso.yaml create mode 100644 zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-lsm6dso.yaml create mode 100644 zephyr/shim/src/motionsense_driver/lsm6dso-drvinfo.inc diff --git a/driver/accelgyro_lsm6dso.c b/driver/accelgyro_lsm6dso.c index 61e93cd1cd..ca5850ac8f 100644 --- a/driver/accelgyro_lsm6dso.c +++ b/driver/accelgyro_lsm6dso.c @@ -25,6 +25,23 @@ STATIC_IF(CONFIG_ACCEL_FIFO) volatile uint32_t last_interrupt_timestamp; STATIC_IF(CONFIG_ACCEL_INTERRUPTS) int config_interrupt( const struct motion_sensor_t *s); +#if defined(CONFIG_ZEPHYR) && defined(CONFIG_ACCEL_INTERRUPTS) +/* Get the motion sensor ID of the LSM6DSO sensor that generates the + * interrupt. The interrupt is converted to the event and transferred to + * motion sense task that actually handles the interrupt. + * + * Here we use an alias (lsm6dso_int) to get the motion sensor ID. This alias + * MUST be defined for this driver to work. + * aliases { + * lsm6dso-int = &lid_accel; + * }; + */ +#if DT_NODE_EXISTS(DT_ALIAS(lsm6dso_int)) +#define CONFIG_ACCEL_LSM6DSO_INT_EVENT \ + TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(lsm6dso_int))) +#endif +#endif + /* * When ODR change, the sensor filters need settling time; * Add a counter to discard a well known number of data with diff --git a/driver/accelgyro_lsm6dso.h b/driver/accelgyro_lsm6dso.h index 9a58fe7d36..15c17f167a 100644 --- a/driver/accelgyro_lsm6dso.h +++ b/driver/accelgyro_lsm6dso.h @@ -8,15 +8,9 @@ #ifndef __CROS_EC_ACCELGYRO_LSM6DSO_H #define __CROS_EC_ACCELGYRO_LSM6DSO_H +#include "driver/accelgyro_lsm6dso_public.h" #include "stm_mems_common.h" -/* - * 7-bit address is 110101xb. Where 'x' is determined - * by the voltage on the ADDR pin - */ -#define LSM6DSO_ADDR0_FLAGS 0x6a -#define LSM6DSO_ADDR1_FLAGS 0x6b - /* Access to embedded sensor hub register bank */ #define LSM6DSO_FUNC_CFG_ACC_ADDR 0x01 #define LSM6DSO_FUNC_CFG_EN 0x80 @@ -113,11 +107,6 @@ struct lsm6dso_fstatus { uint16_t pattern; }; -/* Absolute maximum rate for Acc and Gyro sensors */ -#define LSM6DSO_ODR_MIN_VAL 13000 -#define LSM6DSO_ODR_MAX_VAL \ - MOTION_MAX_SENSOR_FREQUENCY(416000, 13000) - /* ODR reg value from selected data rate in mHz */ #define LSM6DSO_ODR_TO_REG(_odr) (__fls(_odr / LSM6DSO_ODR_MIN_VAL) + 1) diff --git a/include/driver/accelgyro_lsm6dso_public.h b/include/driver/accelgyro_lsm6dso_public.h new file mode 100644 index 0000000000..65e98bccec --- /dev/null +++ b/include/driver/accelgyro_lsm6dso_public.h @@ -0,0 +1,25 @@ +/* Copyright 2021 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. + */ + +/* LSM6DSO Accel and Gyro driver for Chrome EC */ + +#ifndef __CROS_EC_ACCELGYRO_LSM6DSO_PUBLIC_H +#define __CROS_EC_ACCELGYRO_LSM6DSO_PUBLIC_H + +#include "driver/accelgyro_lsm6dso_public.h" + +/* + * 7-bit address is 110101xb. Where 'x' is determined + * by the voltage on the ADDR pin + */ +#define LSM6DSO_ADDR0_FLAGS 0x6a +#define LSM6DSO_ADDR1_FLAGS 0x6b + +/* Absolute maximum rate for Acc and Gyro sensors */ +#define LSM6DSO_ODR_MIN_VAL 13000 +#define LSM6DSO_ODR_MAX_VAL \ + MOTION_MAX_SENSOR_FREQUENCY(416000, 13000) + +#endif /* __CROS_EC_ACCELGYRO_LSM6DSO_PUBLIC_H */ diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 709d4fafb0..f9cf915613 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -144,6 +144,8 @@ zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ACCELGYRO_ICM426XX "${PLATFORM_EC}/driver/accelgyro_icm426xx.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ACCELGYRO_ICM42607 "${PLATFORM_EC}/driver/accelgyro_icm42607.c") +zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ACCELGYRO_LSM6DSO + "${PLATFORM_EC}/driver/accelgyro_lsm6dso.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ACCEL_FIFO "${PLATFORM_EC}/common/motion_sense_fifo.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ADC_CMD diff --git a/zephyr/Kconfig.sensor_devices b/zephyr/Kconfig.sensor_devices index 3ddf65148c..2d01f41249 100644 --- a/zephyr/Kconfig.sensor_devices +++ b/zephyr/Kconfig.sensor_devices @@ -54,7 +54,7 @@ config PLATFORM_EC_ACCEL_LIS2DW12_AS_BASE help The LIS2DW driver supports fifo and interrupt, but letting lid accel sensor work at polling mode is a common selection in current usage - model. This option will select interrupt (foced mode). + model. This option will select interrupt (forced mode). config PLATFORM_EC_ACCELGYRO_BMI160 bool "BMI160 Accelgyrometer Driver" @@ -100,6 +100,21 @@ config PLATFORM_EC_ACCELGYRO_ICM42607 The driver supports ICM42607 which provides both accelerometer and gyroscope readings. +config PLATFORM_EC_ACCELGYRO_LSM6DSO + bool "LSM6DSO Accelgyro Driver" + select PLATFORM_EC_STM_MEMS_COMMON + help + The driver supports ST's LSM6DSO 3D digital accelerometer sensor. + It allows measurements of acceleration in three perpendicular axes. + +config PLATFORM_EC_ACCELGYRO_LSM6DSO_AS_BASE + bool "LSM6DSO Interrupt force mode" + depends on PLATFORM_EC_ACCELGYRO_LSM6DSO + help + The LSM6DSO driver supports fifo and interrupt, but letting lid accel + sensor work at polling mode is a common selection in current usage + model. This option will select interrupt (forced mode). + config PLATFORM_EC_STM_MEMS_COMMON bool help diff --git a/zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-accel.yaml b/zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-accel.yaml new file mode 100644 index 0000000000..5c3c6172f0 --- /dev/null +++ b/zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-accel.yaml @@ -0,0 +1,13 @@ +# Copyright 2021 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. + +description: motionsense sensor node for LSM6DSO Accelerometer + +compatible: "cros-ec,lsm6dso-accel" + +include: lsm6dso.yaml + +properties: + default-range: + default: 2 diff --git a/zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-gyro.yaml b/zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-gyro.yaml new file mode 100644 index 0000000000..a10a98d97f --- /dev/null +++ b/zephyr/dts/bindings/motionsense/driver/cros-ec,lsm6dso-gyro.yaml @@ -0,0 +1,13 @@ +# Copyright 2021 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. + +description: motionsense sensor node for LSM6DSO Gyro + +compatible: "cros-ec,lsm6dso-gyro" + +include: lsm6dso.yaml + +properties: + default-range: + default: 1000 diff --git a/zephyr/dts/bindings/motionsense/driver/lsm6dso.yaml b/zephyr/dts/bindings/motionsense/driver/lsm6dso.yaml new file mode 100644 index 0000000000..dd345854be --- /dev/null +++ b/zephyr/dts/bindings/motionsense/driver/lsm6dso.yaml @@ -0,0 +1,19 @@ +# Copyright 2021 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. +# +# common fields for both LSM6DSO accel and gyro + +# every motionsense sensor node should include motionsense-sensor-base.yaml +include: motionsense-sensor-base.yaml + +properties: + i2c-spi-addr-flags: + type: string + description: i2c address or SPI peripheral logic GPIO + # Address is b'0110101x' where x is determined by the + # logic level on SA0 + enum: + - "LSM6DSO_ADDR0_FLAGS" + - "LSM6DSO_ADDR1_FLAGS" + default: "LSM6DSO_ADDR0_FLAGS" diff --git a/zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-lsm6dso.yaml b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-lsm6dso.yaml new file mode 100644 index 0000000000..d3a37da9a1 --- /dev/null +++ b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-lsm6dso.yaml @@ -0,0 +1,18 @@ +# Copyright 2021 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. + +description: LSM6DSO driver data node + +compatible: "cros-ec,drvdata-lsm6dso" + +include: drvdata-base.yaml + +# +# examples: +# +# lsm6dso_data: lsm6dso-drv-data { +# compatible = "cros-ec,drvdata-lsm6dso"; +# status = "okay"; +# }; +# diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h index b24cc80ddb..10490ae1eb 100644 --- a/zephyr/shim/include/config_chip.h +++ b/zephyr/shim/include/config_chip.h @@ -1584,6 +1584,11 @@ #define CONFIG_ACCELGYRO_ICM42607 #endif +#undef CONFIG_ACCELGYRO_LSM6DSO +#ifdef CONFIG_PLATFORM_EC_ACCELGYRO_LSM6DSO +#define CONFIG_ACCELGYRO_LSM6DSO +#endif + #endif /* CONFIG_PLATFORM_EC_MOTIONSENSE */ #undef CONFIG_MATH_UTIL diff --git a/zephyr/shim/src/motionsense_driver/lsm6dso-drvinfo.inc b/zephyr/shim/src/motionsense_driver/lsm6dso-drvinfo.inc new file mode 100644 index 0000000000..a759993ba4 --- /dev/null +++ b/zephyr/shim/src/motionsense_driver/lsm6dso-drvinfo.inc @@ -0,0 +1,57 @@ +/* Copyright 2021 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. + */ + +#include "driver/accelgyro_lsm6dso.h" +#include "driver/stm_mems_common.h" + +/* + * CREATE_SENSOR_DATA which is defined in motionsense_sensors.c is + * the helper to create sensor driver specific data. + * + * CREATE_SENSOR_DATA gets two arguments. One is the compatible + * property value specified in device tree and the other one is the macro + * that actually creates sensor driver specific data. The macro gets + * node id and the name to be used for the sensor driver data. + */ + +/* + * Create driver data. It can be shared among the entries in + * motion_sensors array which are using the same lsm6dso driver. + */ +#define CREATE_SENSOR_DATA_LSM6DSO(id, drvdata_name) \ + static struct stprivate_data drvdata_name; + +/* + * Create driver data for each lsm6dso drvinfo instance in device tree. + * (compatible = "cros-ec,drvdata-lsm6dso") + */ +CREATE_SENSOR_DATA(cros_ec_drvdata_lsm6dso, CREATE_SENSOR_DATA_LSM6DSO) +/* + * CREATE_MOTION_SENSOR which is defined in motionsense_sensors.c is + * the macro to create an entry in motion_sensors array. + * The macro gets value of compatible property of + * the sensor in device tree and sensor specific values like chip ID, + * type of sensor, name of driver, default min/max frequency. + * Then using the values, it creates the corresponding motion_sense_t entry + * in motion_sensors array. + */ + +/* + * Here, we call CREATE_MOTION_SENSOR to create a motion_sensor_t entry + * for each lsm6dso accel instance(compatible = "cros-ec,lsm6dso-accel") + * in device tree. + */ +CREATE_MOTION_SENSOR(cros_ec_lsm6dso_accel, MOTIONSENSE_CHIP_LSM6DSO, \ + MOTIONSENSE_TYPE_ACCEL, lsm6dso_drv, \ + LSM6DSO_ODR_MIN_VAL, LSM6DSO_ODR_MAX_VAL) + +/* + * Here, we call CREATE_MOTION_SENSOR to create a motion_sensor_t entry + * for each lsm6dso gyro instance (compatible = "cros-ec,lsm6dso-gyro") + * in device tree. + */ +CREATE_MOTION_SENSOR(cros_ec_lsm6dso_gyro, MOTIONSENSE_CHIP_LSM6DSO, \ + MOTIONSENSE_TYPE_GYRO, lsm6dso_drv, \ + LSM6DSO_ODR_MIN_VAL, LSM6DSO_ODR_MAX_VAL) diff --git a/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc b/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc index d8319ff193..4138074e95 100644 --- a/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc +++ b/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc @@ -43,3 +43,6 @@ #ifdef CONFIG_PLATFORM_EC_ACCELGYRO_ICM42607 #include "icm42607-drvinfo.inc" #endif +#ifdef CONFIG_PLATFORM_EC_ACCELGYRO_LSM6DSO +#include "lsm6dso-drvinfo.inc" +#endif -- cgit v1.2.1