summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDawid Niedzwiecki <dn@semihalf.com>2021-11-18 11:37:46 +0100
committerCommit Bot <commit-bot@chromium.org>2021-12-06 23:45:59 +0000
commitee9053226b1118f797d16fbb3490c26178ee5a6e (patch)
tree7bd505b709c1622f1aa508fd1ccb0d06444da12d
parentee5fe104f5378a17de5a24438500f9e3eccfa08d (diff)
downloadchrome-ec-ee9053226b1118f797d16fbb3490c26178ee5a6e.tar.gz
zephyr: move probing of alt sensors to a function
Remove a function to probe all alternate motion sensors, but prepare a function to probe a specific sensor. It introduces more flexibility. Also, create a separated header file motionsense_sensors_defs.h with definitions needed in board.h. BUG=b:202451919 TEST=zmake testall & test if an alternate sensors are used as it is done in CL:3310639 BRANCH=main Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com> Change-Id: Ibfab10cbff39e17c8a86aad0c6d7c5897285a0a4 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3289415 Commit-Queue: Keith Short <keithshort@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org>
-rw-r--r--zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml3
-rw-r--r--zephyr/shim/include/board.h2
-rw-r--r--zephyr/shim/include/motionsense_sensors.h117
-rw-r--r--zephyr/shim/include/motionsense_sensors_defs.h103
-rw-r--r--zephyr/shim/src/motionsense_sensors.c51
5 files changed, 150 insertions, 126 deletions
diff --git a/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml b/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml
index 6adaa22ee5..9bb602359b 100644
--- a/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml
+++ b/zephyr/dts/bindings/motionsense/motionsense-sensor-base.yaml
@@ -62,9 +62,6 @@ properties:
type: phandle
description: phandle to CBI SSGC value indicating that the sensor
should be used
- runtime-probe:
- type: boolean
- description: runtime probing of sensor will be executed if true
int-signal:
type: phandle
description: pin which triggers interrupt for sensor
diff --git a/zephyr/shim/include/board.h b/zephyr/shim/include/board.h
index a1059eb426..5d480b5615 100644
--- a/zephyr/shim/include/board.h
+++ b/zephyr/shim/include/board.h
@@ -30,7 +30,7 @@
/* Include board specific sensor configuration if motionsense is enabled */
#ifdef CONFIG_MOTIONSENSE
-#include "motionsense_sensors.h"
+#include "motionsense_sensors_defs.h"
#endif
#endif /* __BOARD_H */
diff --git a/zephyr/shim/include/motionsense_sensors.h b/zephyr/shim/include/motionsense_sensors.h
index f4ca97840e..cbc672e34a 100644
--- a/zephyr/shim/include/motionsense_sensors.h
+++ b/zephyr/shim/include/motionsense_sensors.h
@@ -7,104 +7,43 @@
#define __CROS_EC_MOTIONSENSE_SENSORS_H
#include <devicetree.h>
+#include "motion_sense.h"
-#define SENSOR_NODE DT_PATH(motionsense_sensor)
-#define SENSOR_INFO_NODE DT_PATH(motionsense_sensor_info)
-#define SENSOR_ALT_NODE DT_PATH(motionsense_sensor_alt)
-
-#define SENSOR_ID(id) DT_CAT(SENSOR_, 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)
- DT_FOREACH_CHILD(SENSOR_NODE, SENSOR_ID_WITH_COMMA)
-#endif
- 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,
-};
+extern struct motion_sensor_t motion_sensors_alt[];
/*
- * Find the accelerometers for lid angle calculation.
- *
- * The angle calculation requires two accelerometers. One is on the lid
- * and the other one is on the base. So we need to specify which sensor is
- * on the lid and which one is on the base. We use two labels "lid_accel"
- * and "base_accel".
- *
- * base_accel - label for the accelerometer sensor on the base.
- * lid_accel - label for the accelerometer sensor on the lid.
- *
- * e.g) below shows BMA255 is the accelerometer on the lid and bmi260 is
- * the accelerometer on the base.
- *
- * motionsense-sensor {
- * lid_accel: lid-accel {
- * compatible = "cros-ec,bma255";
- * status = "okay";
- * :
- * :
- * };
- *
- * base_accel: base-accel {
- * compatible = "cros-ec,bmi260";
- * status = "okay";
- * :
- * :
- * };
- * };
+ * Performs probing of an alternate sensor.
+ * @param alt_idx Index in motion_sensors_alt of the sensor to be probed.
+ * It should be gained with SENSOR_ID,
+ * e.g. with SENSOR_ID(DT_NODELABEL(label)).
+ * @return EC_SUCCESS if the probe was successful, non-zero otherwise.
*/
-#ifdef CONFIG_LID_ANGLE
-#define CONFIG_LID_ANGLE_SENSOR_LID SENSOR_ID(DT_NODELABEL(lid_accel))
-#define CONFIG_LID_ANGLE_SENSOR_BASE SENSOR_ID(DT_NODELABEL(base_accel))
-#endif
+int motion_sense_probe(enum sensor_alt_id alt_idx);
+
+#define ENABLE_ALT_MOTION_SENSOR(alt_id) \
+ motion_sensors[SENSOR_ID(DT_PHANDLE(alt_id, alternate_for))] = \
+ motion_sensors_alt[SENSOR_ID(alt_id)];
/*
- * Get the sensors running in force mode from DT and create a bit mask for it.
- *
- * e.g) lid accel and als_clear are in accel_force_mode. The macro below finds
- * the corresponding bit for each sensor in bit mask and set it.
- * motionsense-sensor-info {
- * compatible = "cros-ec,motionsense-sensor-info";
- *
- * // list of sensors in force mode
- * accel-force-mode-sensors = <&lid_accel &als_clear>;
- * };
+ * Replaces a default motion sensor with an alternate one pointed by nodelabel.
*/
-#if DT_NODE_HAS_PROP(SENSOR_INFO_NODE, accel_force_mode_sensors)
-#define SENSOR_IN_FORCE_MODE(i, id) \
- | BIT(SENSOR_ID(DT_PHANDLE_BY_IDX(id, accel_force_mode_sensors, i)))
-#define CONFIG_ACCEL_FORCE_MODE_MASK \
- (0 UTIL_LISTIFY(DT_PROP_LEN(SENSOR_INFO_NODE, \
- accel_force_mode_sensors), SENSOR_IN_FORCE_MODE, \
- SENSOR_INFO_NODE))
-#endif
+#define MOTIONSENSE_ENABLE_ALTERNATE(nodelabel) \
+ do { \
+ BUILD_ASSERT(DT_NODE_EXISTS(DT_NODELABEL(nodelabel)), \
+ "Motionsense alternate node does not exist"); \
+ ENABLE_ALT_MOTION_SENSOR(DT_NODELABEL(nodelabel)); \
+ } while (0)
/*
- * Performs probing of alternative sensors marked with "runtime-probe" property
- * in device tree. If an alternative sensor is detected, the function replaces
- * a default one in the motion_sensors array.
+ * Probes and replaces a default motion sensor with an alternate one pointed by
+ * nodelabel, if the probe was successful.
*/
-void motion_sense_probe_sensors(void);
+#define MOTIONSENSE_PROBE_AND_ENABLE_ALTERNATE(nodelabel) \
+ do { \
+ BUILD_ASSERT(DT_NODE_EXISTS(DT_NODELABEL(nodelabel)), \
+ "Motionsense alternate node does not exist"); \
+ if (!motion_sense_probe(SENSOR_ID(DT_NODELABEL(nodelabel)))) \
+ ENABLE_ALT_MOTION_SENSOR(DT_NODELABEL(nodelabel)); \
+ } while (0)
#endif /* __CROS_EC_MOTIONSENSE_SENSORS_H */
diff --git a/zephyr/shim/include/motionsense_sensors_defs.h b/zephyr/shim/include/motionsense_sensors_defs.h
new file mode 100644
index 0000000000..ec2180566e
--- /dev/null
+++ b/zephyr/shim/include/motionsense_sensors_defs.h
@@ -0,0 +1,103 @@
+/* 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.
+ */
+
+#ifndef __CROS_EC_MOTIONSENSE_SENSORS_DEFS_H
+#define __CROS_EC_MOTIONSENSE_SENSORS_DEFS_H
+
+#include <devicetree.h>
+
+#define SENSOR_NODE DT_PATH(motionsense_sensor)
+#define SENSOR_INFO_NODE DT_PATH(motionsense_sensor_info)
+#define SENSOR_ALT_NODE DT_PATH(motionsense_sensor_alt)
+
+#define SENSOR_ID(id) DT_CAT(SENSOR_, 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)
+ DT_FOREACH_CHILD(SENSOR_NODE, SENSOR_ID_WITH_COMMA)
+#endif
+ 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.
+ *
+ * The angle calculation requires two accelerometers. One is on the lid
+ * and the other one is on the base. So we need to specify which sensor is
+ * on the lid and which one is on the base. We use two labels "lid_accel"
+ * and "base_accel".
+ *
+ * base_accel - label for the accelerometer sensor on the base.
+ * lid_accel - label for the accelerometer sensor on the lid.
+ *
+ * e.g) below shows BMA255 is the accelerometer on the lid and bmi260 is
+ * the accelerometer on the base.
+ *
+ * motionsense-sensor {
+ * lid_accel: lid-accel {
+ * compatible = "cros-ec,bma255";
+ * status = "okay";
+ * :
+ * :
+ * };
+ *
+ * base_accel: base-accel {
+ * compatible = "cros-ec,bmi260";
+ * status = "okay";
+ * :
+ * :
+ * };
+ * };
+ */
+#ifdef CONFIG_LID_ANGLE
+#define CONFIG_LID_ANGLE_SENSOR_LID SENSOR_ID(DT_NODELABEL(lid_accel))
+#define CONFIG_LID_ANGLE_SENSOR_BASE SENSOR_ID(DT_NODELABEL(base_accel))
+#endif
+
+/*
+ * Get the sensors running in force mode from DT and create a bit mask for it.
+ *
+ * e.g) lid accel and als_clear are in accel_force_mode. The macro below finds
+ * the corresponding bit for each sensor in bit mask and set it.
+ * motionsense-sensor-info {
+ * compatible = "cros-ec,motionsense-sensor-info";
+ *
+ * // list of sensors in force mode
+ * accel-force-mode-sensors = <&lid_accel &als_clear>;
+ * };
+ */
+#if DT_NODE_HAS_PROP(SENSOR_INFO_NODE, accel_force_mode_sensors)
+#define SENSOR_IN_FORCE_MODE(i, id) \
+ | BIT(SENSOR_ID(DT_PHANDLE_BY_IDX(id, accel_force_mode_sensors, i)))
+#define CONFIG_ACCEL_FORCE_MODE_MASK \
+ (0 UTIL_LISTIFY(DT_PROP_LEN(SENSOR_INFO_NODE, \
+ accel_force_mode_sensors), SENSOR_IN_FORCE_MODE, \
+ SENSOR_INFO_NODE))
+#endif
+
+#endif /* __CROS_EC_MOTIONSENSE_SENSORS_DEFS_H */
diff --git a/zephyr/shim/src/motionsense_sensors.c b/zephyr/shim/src/motionsense_sensors.c
index 66c08b7d0a..f1b9e41c7a 100644
--- a/zephyr/shim/src/motionsense_sensors.c
+++ b/zephyr/shim/src/motionsense_sensors.c
@@ -8,6 +8,7 @@
#include "accelgyro.h"
#include "hooks.h"
#include "drivers/cros_cbi.h"
+#include "motionsense_sensors.h"
LOG_MODULE_REGISTER(shim_cros_motionsense_sensors);
@@ -390,10 +391,6 @@ DECLARE_HOOK(HOOK_INIT, sensor_enable_irqs, HOOK_PRIO_DEFAULT);
#endif
/* Handle the alternative motion sensors */
-#define REPLACE_ALT_MOTION_SENSOR(new_id, old_id) \
- motion_sensors[SENSOR_ID(old_id)] = \
- motion_sensors_alt[SENSOR_ID(new_id)];
-
#define CHECK_AND_REPLACE_ALT_MOTION_SENSOR(id) \
do { \
if (cros_cbi_ssfc_check_match( \
@@ -409,37 +406,25 @@ DECLARE_HOOK(HOOK_INIT, sensor_enable_irqs, HOOK_PRIO_DEFAULT);
DT_NODE_HAS_PROP(id, alternate_indicator)), \
(CHECK_AND_REPLACE_ALT_MOTION_SENSOR(id)), ())
-#define PROBE_SENSOR(id) \
-{ \
- int res; \
- \
- LOG_INF("Probing \"%s\" chip %d type %d loc %d", \
- motion_sensors_alt[SENSOR_ID(id)].name, \
- motion_sensors_alt[SENSOR_ID(id)].chip, \
- motion_sensors_alt[SENSOR_ID(id)].type, \
- motion_sensors_alt[SENSOR_ID(id)].location); \
- \
- __ASSERT(motion_sensors_alt[SENSOR_ID(id)].drv->probe != NULL, \
- "No probing function for alt sensor: %d", SENSOR_ID(id)); \
- res = motion_sensors_alt[SENSOR_ID(id)].drv->probe( \
- &motion_sensors_alt[SENSOR_ID(id)]); \
- LOG_INF("%sfound\n", (res != EC_SUCCESS ? "not " : "")); \
- \
- if (res == EC_SUCCESS) { \
- REPLACE_ALT_MOTION_SENSOR(id, \
- DT_PHANDLE(id, alternate_for)); \
- } \
-}
-
-#define PROBE_IF_NEEDED(id) \
- COND_CODE_1(DT_PROP(id, runtime_probe), \
- (PROBE_SENSOR(id)), \
- ())
-
#if DT_NODE_EXISTS(SENSOR_ALT_NODE)
-void motion_sense_probe_sensors(void)
+
+int motion_sense_probe(enum sensor_alt_id alt_idx)
{
- DT_FOREACH_CHILD(SENSOR_ALT_NODE, PROBE_IF_NEEDED);
+ int res;
+
+ LOG_INF("Probing \"%s\" chip %d type %d loc %d",
+ motion_sensors_alt[alt_idx].name,
+ motion_sensors_alt[alt_idx].chip,
+ motion_sensors_alt[alt_idx].type,
+ motion_sensors_alt[alt_idx].location);
+
+ __ASSERT(motion_sensors_alt[alt_idx].drv->probe != NULL,
+ "No probing function for alt sensor: %d", alt_idx);
+ res = motion_sensors_alt[alt_idx].drv->probe(
+ &motion_sensors_alt[alt_idx]);
+ LOG_INF("%sfound\n", (res != EC_SUCCESS ? "not " : ""));
+
+ return res;
}
static void motion_sensors_init_alt(void)