summaryrefslogtreecommitdiff
path: root/zephyr/shim/include/motionsense_sensors.h
blob: e00eae426e214630801bfce9db564f489627308f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Copyright 2021 The ChromiumOS Authors
 * 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_H
#define __CROS_EC_MOTIONSENSE_SENSORS_H

#include <zephyr/devicetree.h>
#include "motion_sense.h"

extern struct motion_sensor_t motion_sensors_alt[];

/*
 * Common macros.
 */
#define SENSOR_ROT_STD_REF_NAME(id) DT_CAT(ROT_REF_, id)
#define SENSOR_ROT_REF_NODE DT_PATH(motionsense_rotation_ref)

/*
 * Declare rotation parameters, since they may be
 * dynamically selected.
 */
#define DECLARE_EXTERN_SENSOR_ROT_REF(id) \
	extern const mat33_fp_t SENSOR_ROT_STD_REF_NAME(id);

#if DT_NODE_EXISTS(SENSOR_ROT_REF_NODE)
DT_FOREACH_CHILD(SENSOR_ROT_REF_NODE, DECLARE_EXTERN_SENSOR_ROT_REF)
#endif
#undef DECLARE_EXTERN_SENSOR_ROT_REF

/*
 * 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.
 */
int motion_sense_probe(enum sensor_alt_id alt_idx);

/*
 * Performs checking CBI SSFC fields defined in DTS to verify if an alternate
 * motion sensor is present. If there is a match, the function replaces
 * a default motion sensor in the motion_sensors array.
 */
void motion_sensors_check_ssfc(void);

#define ENABLE_ALT_MOTION_SENSOR(alt_id)                               \
	motion_sensors[SENSOR_ID(DT_PHANDLE(alt_id, alternate_for))] = \
		motion_sensors_alt[SENSOR_ID(alt_id)];

/*
 * Replaces a default motion sensor with an alternate one pointed by nodelabel.
 */
#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)

/*
 * Probes and replaces a default motion sensor with an alternate one pointed by
 * nodelabel, if the probe was successful.
 */
#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 */