diff options
author | Yuval Peress <peress@chromium.org> | 2021-04-22 10:56:19 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-04-23 20:01:23 +0000 |
commit | 4aaa85760faeec02f94b86b99d40768362d8c158 (patch) | |
tree | bacf5655e96231da3437ffb5ab7be844b5601760 /zephyr | |
parent | cea345aa111d93617806d55ed68950d2e072db0e (diff) | |
download | chrome-ec-4aaa85760faeec02f94b86b99d40768362d8c158.tar.gz |
zephyr: sensors: Add alternate sensors support
In some boards there's a runtime decision for which sensor to use. In
order to support this we'll need an instance of the alternate sensors
as well as a mapping for them.
Add an optional phandle `alternate-for` to the base sensors bindings.
When used, the sensor will be excluded from the primary
motion_sensors array and added instead to the motion_sensors_alt
array.
BRANCH=none
BUG=b:183990188, b:185966444
TEST=zmake testall
Signed-off-by: Yuval Peress <peress@chromium.org>
Change-Id: I6f6e3a9659a381c45fd97fbce9fecb2a807f7fa2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2846415
Reviewed-by: Keith Short <keithshort@chromium.org>
Commit-Queue: Keith Short <keithshort@chromium.org>
Diffstat (limited to 'zephyr')
-rw-r--r-- | zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml | 4 | ||||
-rw-r--r-- | zephyr/shim/include/motionsense_sensors.h | 26 | ||||
-rw-r--r-- | zephyr/shim/src/motionsense_sensors.c | 43 |
3 files changed, 66 insertions, 7 deletions
diff --git a/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml b/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml index e6325a3ef1..9e8b7be2c3 100644 --- a/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml +++ b/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml @@ -54,6 +54,10 @@ properties: type: phandle required: false description: phandle to driver data to be used for the motion sensor + alternate-for: + type: phandle + description: phandle to another sensor that can be swapped with this one + at runtime. # # examples: diff --git a/zephyr/shim/include/motionsense_sensors.h b/zephyr/shim/include/motionsense_sensors.h index 013a03d8dc..bf7ff95f85 100644 --- a/zephyr/shim/include/motionsense_sensors.h +++ b/zephyr/shim/include/motionsense_sensors.h @@ -10,10 +10,17 @@ #define SENSOR_NODE DT_PATH(motionsense_sensor) #define SENSOR_INFO_NODE DT_PATH(motionsense_sensor_info) +#define SENSOR_ALT_NODE DT_PATH(motionsesne_sensor_alt) #define SENSOR_ID(id) DT_CAT(SENSOR_, id) -#define SENSOR_ID_WITH_COMMA(id) \ - IF_ENABLED(DT_NODE_HAS_STATUS(id, okay), (SENSOR_ID(id),)) + +/* Define the SENSOR_ID if: + * DT_NODE_HAS_STATUS(id, okay) && !DT_NODE_HAS_PROP(id, alternate_for) + */ +#define SENSOR_ID_WITH_COMMA(id) \ + IF_ENABLED(DT_NODE_HAS_STATUS(id, okay), \ + (COND_CODE_0(DT_NODE_HAS_PROP(id, alternate_for), \ + (SENSOR_ID(id), ), ()))) enum sensor_id { #if DT_NODE_EXISTS(SENSOR_NODE) @@ -22,6 +29,21 @@ enum sensor_id { SENSOR_COUNT, }; +#undef SENSOR_ID_WITH_COMMA +/* Define the SENSOR_ID if: + * DT_NODE_HAS_STATUS(id, okay) && DT_NODE_HAS_PROP(id, alternate_for) + */ +#define SENSOR_ID_WITH_COMMA(id) \ + IF_ENABLED(DT_NODE_HAS_STATUS(id, okay), \ + (COND_CODE_1(DT_NODE_HAS_PROP(id, alternate_for), \ + (SENSOR_ID(id), ), ()))) +enum sensor_alt_id { +#if DT_NODE_EXISTS(SENSOR_ALT_NODE) + DT_FOREACH_CHILD(SENSOR_ALT_NODE, SENSOR_ID_WITH_COMMA) +#endif + SENSOR_ALT_COUNT, +}; + /* * Find the accelerometers for lid angle calculation. * diff --git a/zephyr/shim/src/motionsense_sensors.c b/zephyr/shim/src/motionsense_sensors.c index 0a74595ce3..bac92e58e8 100644 --- a/zephyr/shim/src/motionsense_sensors.c +++ b/zephyr/shim/src/motionsense_sensors.c @@ -94,7 +94,7 @@ DT_FOREACH_CHILD(SENSOR_ROT_REF_NODE, DECLARE_SENSOR_ROT_REF) compat, create_data_macro) /* - * sensor_drv_list.inc is included two times in this file. This is the first + * sensor_drv_list.inc is included three times in this file. This is the first * time and it is for creating sensor driver-specific data. So we ignore * CREATE_MOTION_SENSOR() that creates motion sensor at this time. */ @@ -209,10 +209,23 @@ DT_FOREACH_CHILD(SENSOR_ROT_REF_NODE, DECLARE_SENSOR_ROT_REF) .max_frequency = s_max_freq \ }, -#define MK_SENSOR_ENTRY(inst, s_compat, s_chip, s_type, s_drv, \ - s_min_freq, s_max_freq) \ - DO_MK_SENSOR_ENTRY(DT_INST(inst, s_compat), \ - s_chip, s_type, s_drv, s_min_freq, s_max_freq) +/* Construct an entry iff the alternate_for property is missing. */ +#define MK_SENSOR_ENTRY(inst, s_compat, s_chip, s_type, s_drv, s_min_freq, \ + s_max_freq) \ + COND_CODE_0(DT_NODE_HAS_PROP(DT_INST(inst, s_compat), alternate_for), \ + (DO_MK_SENSOR_ENTRY(DT_INST(inst, s_compat), s_chip, \ + s_type, s_drv, s_min_freq, \ + s_max_freq)), \ + ()) + +/* Construct an entry iff the alternate_for property exists. */ +#define MK_SENSOR_ALT_ENTRY(inst, s_compat, s_chip, s_type, s_drv, s_min_freq, \ + s_max_freq) \ + COND_CODE_1(DT_NODE_HAS_PROP(DT_INST(inst, s_compat), alternate_for), \ + (DO_MK_SENSOR_ENTRY(DT_INST(inst, s_compat), s_chip, \ + s_type, s_drv, s_min_freq, \ + s_max_freq)), \ + ()) #undef CREATE_SENSOR_DATA /* @@ -269,6 +282,26 @@ struct motion_sensor_t motion_sensors[] = { #endif }; +/* + * Remap the CREATE_MOTION_SENSOR to call MK_SENSOR_ALT_ENTRY to create a list + * of alternate sensors that will be used at runtime. + */ +#undef CREATE_MOTION_SENSOR +#define CREATE_MOTION_SENSOR(s_compat, s_chip, s_type, s_drv, s_min_freq, \ + s_max_freq) \ + UTIL_LISTIFY(DT_NUM_INST_STATUS_OKAY(s_compat), MK_SENSOR_ALT_ENTRY, \ + s_compat, s_chip, s_type, s_drv, s_min_freq, s_max_freq) + +/* + * The list of alternate motion sensors that may be used at runtime to replace + * an entry in the motion_sensors array. + */ +__maybe_unused static struct motion_sensor_t motion_sensors_alt[] = { +#if DT_NODE_EXISTS(SENSOR_ALT_NODE) +#include "motionsense_driver/sensor_drv_list.inc" +#endif +}; + #ifdef CONFIG_DYNAMIC_MOTION_SENSOR_COUNT unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors); #else |