1 /* ST Microelectronics LIS2DW12 3-axis accelerometer driver
2  *
3  * Copyright (c) 2019 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2dw12.pdf
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_
12 #define ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_
13 
14 #include <zephyr/drivers/spi.h>
15 #include <zephyr/drivers/gpio.h>
16 #include <zephyr/sys/util.h>
17 #include <zephyr/drivers/sensor.h>
18 #include <stmemsc.h>
19 #include "lis2dw12_reg.h"
20 
21 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
22 #include <zephyr/drivers/spi.h>
23 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
24 
25 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
26 #include <zephyr/drivers/i2c.h>
27 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
28 
29 /* Return ODR reg value based on data rate set */
30 #define LIS2DW12_ODR_TO_REG(_odr) \
31 	((_odr <= 1) ? LIS2DW12_XL_ODR_1Hz6_LP_ONLY : \
32 	 (_odr <= 12) ? LIS2DW12_XL_ODR_12Hz5 : \
33 	 ((31 - __builtin_clz(_odr / 25))) + 3)
34 
35 /* Return data rate in Hz for given register value */
36 #define LIS2DW12_REG_TO_ODR(_reg) \
37 	((_reg == 0) ? 0 : \
38 	(_reg == 1) ? 1 : \
39 	(_reg == 2) ? 12 : \
40 	(_reg > 9) ? 1600 : \
41 	(1 << (_reg - 3)) * 25)
42 
43 /* FS reg value from Full Scale */
44 #define LIS2DW12_FS_TO_REG(_fs)	(30 - __builtin_clz(_fs))
45 
46 /* Acc Gain value in ug/LSB in High Perf mode */
47 #define LIS2DW12_FS_2G_GAIN		244
48 #define LIS2DW12_FS_4G_GAIN		488
49 #define LIS2DW12_FS_8G_GAIN		976
50 #define LIS2DW12_FS_16G_GAIN		1952
51 
52 #define LIS2DW12_SHFT_GAIN_NOLP1	2
53 #define LIS2DW12_ACCEL_GAIN_DEFAULT_VAL LIS2DW12_FS_2G_GAIN
54 #define LIS2DW12_FS_TO_GAIN(_fs, _lp1) \
55 		(LIS2DW12_FS_2G_GAIN << ((_fs) + (_lp1)))
56 
57 /* shift value for power mode */
58 #define LIS2DW12_SHIFT_PM1		4
59 #define LIS2DW12_SHIFT_PMOTHER		2
60 
61 /**
62  * struct lis2dw12_device_config - lis2dw12 hw configuration
63  * @bus_name: Pointer to bus master identifier.
64  * @pm: Power mode (lis2dh_powermode).
65  * @int_pin: Sensor int pin (int1/int2).
66  */
67 struct lis2dw12_device_config {
68 	stmdev_ctx_t ctx;
69 	union {
70 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
71 		const struct i2c_dt_spec i2c;
72 #endif
73 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
74 		const struct spi_dt_spec spi;
75 #endif
76 	} stmemsc_cfg;
77 	lis2dw12_mode_t pm;
78 	uint16_t odr;
79 	uint8_t range;
80 	uint8_t bw_filt;
81 	bool low_noise;
82 	bool hp_filter_path;
83 	bool hp_ref_mode;
84 	bool drdy_pulsed;
85 #ifdef CONFIG_LIS2DW12_TRIGGER
86 	struct gpio_dt_spec gpio_int;
87 	uint8_t int_pin;
88 #ifdef CONFIG_LIS2DW12_TAP
89 	uint8_t tap_mode;
90 	uint8_t tap_threshold[3];
91 	uint8_t tap_shock;
92 	uint8_t tap_latency;
93 	uint8_t tap_quiet;
94 #endif /* CONFIG_LIS2DW12_TAP */
95 #ifdef CONFIG_LIS2DW12_FREEFALL
96 	uint8_t freefall_duration;
97 	uint8_t freefall_threshold;
98 #endif /* CONFIG_LIS2DW12_FREEFALL */
99 #endif /* CONFIG_LIS2DW12_TRIGGER */
100 };
101 
102 /* sensor data */
103 struct lis2dw12_data {
104 	int16_t acc[3];
105 
106 	 /* save sensitivity */
107 	uint16_t gain;
108 	 /* output data rate */
109 	uint16_t odr;
110 
111 #ifdef CONFIG_LIS2DW12_TRIGGER
112 	const struct device *dev;
113 
114 	struct gpio_callback gpio_cb;
115 	sensor_trigger_handler_t drdy_handler;
116 	const struct sensor_trigger *drdy_trig;
117 #ifdef CONFIG_LIS2DW12_TAP
118 	sensor_trigger_handler_t tap_handler;
119 	const struct sensor_trigger *tap_trig;
120 	sensor_trigger_handler_t double_tap_handler;
121 	const struct sensor_trigger *double_tap_trig;
122 #endif /* CONFIG_LIS2DW12_TAP */
123 #ifdef CONFIG_LIS2DW12_THRESHOLD
124 	sensor_trigger_handler_t threshold_handler;
125 	const struct sensor_trigger *threshold_trig;
126 #endif /* CONFIG_LIS2DW12_THRESHOLD */
127 #ifdef CONFIG_LIS2DW12_FREEFALL
128 	sensor_trigger_handler_t freefall_handler;
129 	const struct sensor_trigger *freefall_trig;
130 #endif /* CONFIG_LIS2DW12_FREEFALL */
131 #if defined(CONFIG_LIS2DW12_TRIGGER_OWN_THREAD)
132 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DW12_THREAD_STACK_SIZE);
133 	struct k_thread thread;
134 	struct k_sem gpio_sem;
135 #elif defined(CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD)
136 	struct k_work work;
137 #endif /* CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD */
138 #endif /* CONFIG_LIS2DW12_TRIGGER */
139 };
140 
141 #ifdef CONFIG_LIS2DW12_TRIGGER
142 int lis2dw12_init_interrupt(const struct device *dev);
143 int lis2dw12_trigger_set(const struct device *dev,
144 			  const struct sensor_trigger *trig,
145 			  sensor_trigger_handler_t handler);
146 #endif /* CONFIG_LIS2DW12_TRIGGER */
147 
148 #endif /* ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ */
149