diff options
author | Michał Barnaś <mb@semihalf.com> | 2021-09-01 13:36:27 +0200 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-09-30 00:27:38 +0000 |
commit | a6f76a36befdf6d2c28c52fc8f0a93aec7e008f6 (patch) | |
tree | 7ea2f904925b4860a24b27d9e0085e94969e26ed | |
parent | 84aa3cb9dd0643e9fe0dd188163435d6d282aac9 (diff) | |
download | chrome-ec-a6f76a36befdf6d2c28c52fc8f0a93aec7e008f6.tar.gz |
zephyr: motion: add support for icm426xx driver
Add support for icm426xx accel and gyro sensors.
This allows to define these sensors in board's device tree.
Add support for runtime probing of this sensor.
BRANCH=main
BUG=b:194424288
TEST=icm426xx sensor can be defined in zephyr build
all dependencies are generated correctly
Change-Id: I7a6499cb099c3edca8513b664d4948afea59998c
Signed-off-by: Michał Barnaś <mb@semihalf.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3137943
Reviewed-by: Keith Short <keithshort@chromium.org>
Commit-Queue: Keith Short <keithshort@chromium.org>
-rw-r--r-- | driver/accelgyro_icm426xx.c | 36 | ||||
-rw-r--r-- | driver/accelgyro_icm_common.h | 1 | ||||
-rw-r--r-- | zephyr/Kconfig.sensor_devices | 2 | ||||
-rw-r--r-- | zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-accel.yaml | 13 | ||||
-rw-r--r-- | zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-gyro.yaml | 13 | ||||
-rw-r--r-- | zephyr/dts/bindings/motionsense/driver/icm426xx.yaml | 17 | ||||
-rw-r--r-- | zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-icm426xx.yaml | 18 | ||||
-rw-r--r-- | zephyr/shim/src/motionsense_driver/icm426xx-drvinfo.inc | 57 | ||||
-rw-r--r-- | zephyr/shim/src/motionsense_driver/sensor_drv_list.inc | 3 |
9 files changed, 159 insertions, 1 deletions
diff --git a/driver/accelgyro_icm426xx.c b/driver/accelgyro_icm426xx.c index 5d09c8e4ef..68d45c4a6a 100644 --- a/driver/accelgyro_icm426xx.c +++ b/driver/accelgyro_icm426xx.c @@ -26,6 +26,27 @@ #define CPRINTF(format, args...) cprintf(CC_ACCEL, format, ## args) #define CPRINTS(format, args...) cprints(CC_ACCEL, format, ## args) +#if defined(CONFIG_ZEPHYR) && defined(CONFIG_ACCEL_INTERRUPTS) + +/* Get the motion sensor ID of the ICM426xx 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 (icm426xx_int) to get the motion sensor ID. This alias + * MUST be defined for this driver to work. + * aliases { + * icm426xx-int = &base_accel; + * }; + */ +#if DT_NODE_EXISTS(DT_ALIAS(icm426xx_int)) +#define CONFIG_ACCELGYRO_ICM426XX_INT_EVENT \ + TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(icm426xx_int))) +#else +#error Missing aliases/icm426xx-int in device tree +#endif + +#endif /* defined(CONFIG_ZEPHYR) && defined(CONFIG_ACCEL_INTERRUPTS) */ + STATIC_IF(CONFIG_ACCEL_FIFO) volatile uint32_t last_interrupt_timestamp; static int icm426xx_normalize(const struct motion_sensor_t *s, intv3_t v, @@ -963,6 +984,19 @@ out_unlock: return ret; } +static int icm426xx_probe(const struct motion_sensor_t *s) +{ + int val; + + if (icm_read8(s, ICM426XX_REG_WHO_AM_I, &val) != EC_SUCCESS) + return EC_ERROR_NOT_HANDLED; + + if (val != ICM426XX_CHIP_ICM40608 && val != ICM426XX_CHIP_ICM42605) + return EC_ERROR_NOT_HANDLED; + + return EC_SUCCESS; +} + const struct accelgyro_drv icm426xx_drv = { .init = icm426xx_init, .read = icm426xx_read, @@ -975,7 +1009,9 @@ const struct accelgyro_drv icm426xx_drv = { .get_offset = icm426xx_get_offset, .set_scale = icm_set_scale, .get_scale = icm_get_scale, + .probe = icm426xx_probe, #ifdef CONFIG_ACCEL_INTERRUPTS + .interrupt = icm426xx_interrupt, .irq_handler = icm426xx_irq_handler, #endif }; diff --git a/driver/accelgyro_icm_common.h b/driver/accelgyro_icm_common.h index 8cf3b1e41d..c6e6ce2ff2 100644 --- a/driver/accelgyro_icm_common.h +++ b/driver/accelgyro_icm_common.h @@ -11,6 +11,7 @@ #include "accelgyro.h" #include "hwtimer.h" #include "timer.h" +#include "builtin/stddef.h" #if !defined(CONFIG_ACCELGYRO_ICM_COMM_SPI) && \ !defined(CONFIG_ACCELGYRO_ICM_COMM_I2C) diff --git a/zephyr/Kconfig.sensor_devices b/zephyr/Kconfig.sensor_devices index 623919d775..94204b7b35 100644 --- a/zephyr/Kconfig.sensor_devices +++ b/zephyr/Kconfig.sensor_devices @@ -88,7 +88,7 @@ config PLATFORM_EC_ACCELGYRO_ICM426XX bool "ICM426XX Accelgyro Driver" select PLATFORM_EC_ACCELGYRO_ICM help - The driver supports ICM425XX which provides both accelerometer and + The driver supports ICM426XX which provides both accelerometer and gyroscope readings. config PLATFORM_EC_ACCELGYRO_ICM42607 diff --git a/zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-accel.yaml b/zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-accel.yaml new file mode 100644 index 0000000000..fbc9f44051 --- /dev/null +++ b/zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-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: motion sense sensor node for ICM426xx accel + +compatible: "cros-ec,icm426xx-accel" + +include: icm426xx.yaml + +properties: + default-range: + default: 4 diff --git a/zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-gyro.yaml b/zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-gyro.yaml new file mode 100644 index 0000000000..1f0ae26ced --- /dev/null +++ b/zephyr/dts/bindings/motionsense/driver/cros-ec,icm426xx-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: motion sense sensor node for ICM426xx gyro + +compatible: "cros-ec,icm426xx-gyro" + +include: icm426xx.yaml + +properties: + default-range: + default: 1000 diff --git a/zephyr/dts/bindings/motionsense/driver/icm426xx.yaml b/zephyr/dts/bindings/motionsense/driver/icm426xx.yaml new file mode 100644 index 0000000000..5c33931706 --- /dev/null +++ b/zephyr/dts/bindings/motionsense/driver/icm426xx.yaml @@ -0,0 +1,17 @@ +# 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 ICM426xx 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 + enum: + - "ICM426XX_ADDR0_FLAGS" + - "ICM426XX_ADDR1_FLAGS" + default: "ICM426XX_ADDR0_FLAGS" diff --git a/zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-icm426xx.yaml b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-icm426xx.yaml new file mode 100644 index 0000000000..b88ad7eacd --- /dev/null +++ b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,drvdata-icm426xx.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: ICM426xx driver data node + +compatible: "cros-ec,drvdata-icm426xx" + +include: drvdata-base.yaml + +# +# examples: +# +# icm426xx_data: icm426xx-drv-data { +# compatible = "cros-ec,drvdata-icm426xx"; +# status = "okay"; +# }; +# diff --git a/zephyr/shim/src/motionsense_driver/icm426xx-drvinfo.inc b/zephyr/shim/src/motionsense_driver/icm426xx-drvinfo.inc new file mode 100644 index 0000000000..5513ff934c --- /dev/null +++ b/zephyr/shim/src/motionsense_driver/icm426xx-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_icm_common.h" +#include "driver/accelgyro_icm426xx.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 icm426xx driver. + */ +#define CREATE_SENSOR_DATA_ICM426XX(id, drvdata_name) \ + static struct icm_drv_data_t drvdata_name; + +/* + * Create driver data for each icm426xx drvinfo instance in device tree. + * (compatible = "cros-ec,drvdata-icm426xx") + */ +CREATE_SENSOR_DATA(cros_ec_drvdata_icm426xx, CREATE_SENSOR_DATA_ICM426XX) +/* + * 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 icm426xx accel instance(compatible = "cros-ec,icm426xx-accel") + * in device tree. + */ +CREATE_MOTION_SENSOR(cros_ec_icm426xx_accel, MOTIONSENSE_CHIP_ICM426XX, \ + MOTIONSENSE_TYPE_ACCEL, icm426xx_drv, \ + ICM426XX_ACCEL_MIN_FREQ, ICM426XX_ACCEL_MAX_FREQ) + +/* + * Here, we call CREATE_MOTION_SENSOR to create a motion_sensor_t entry + * for each icm426xx gyro instance (compatible = "cros-ec,icm426xx-gyro") + * in device tree. + */ +CREATE_MOTION_SENSOR(cros_ec_icm426xx_gyro, MOTIONSENSE_CHIP_ICM426XX, \ + MOTIONSENSE_TYPE_GYRO, icm426xx_drv, \ + ICM426XX_GYRO_MIN_FREQ, ICM426XX_GYRO_MAX_FREQ) diff --git a/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc b/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc index f8fa4b7e53..0062dccccd 100644 --- a/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc +++ b/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc @@ -37,3 +37,6 @@ #ifdef CONFIG_PLATFORM_EC_ALS_TCS3400 #include "tcs3400-drvinfo.inc" #endif +#ifdef CONFIG_PLATFORM_EC_ACCELGYRO_ICM426XX +#include "icm426xx-drvinfo.inc" +#endif |