1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DH_LIS2DH_H_
8 #define ZEPHYR_DRIVERS_SENSOR_LIS2DH_LIS2DH_H_
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/device.h>
12 #include <zephyr/sys/util.h>
13 #include <stdint.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <string.h>
17 
18 #define LIS2DH_REG_WAI			0x0f
19 #define LIS2DH_CHIP_ID			0x33
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 #define LIS2DH_AUTOINCREMENT_ADDR	BIT(7)
30 
31 #define LIS2DH_REG_CTRL0		0x1e
32 #define LIS2DH_SDO_PU_DISC_MASK		BIT(7)
33 
34 #define LIS2DH_REG_CTRL1		0x20
35 #define LIS2DH_ACCEL_X_EN_BIT		BIT(0)
36 #define LIS2DH_ACCEL_Y_EN_BIT		BIT(1)
37 #define LIS2DH_ACCEL_Z_EN_BIT		BIT(2)
38 #define LIS2DH_ACCEL_EN_BITS		(LIS2DH_ACCEL_X_EN_BIT | \
39 					LIS2DH_ACCEL_Y_EN_BIT | \
40 					LIS2DH_ACCEL_Z_EN_BIT)
41 #define LIS2DH_ACCEL_XYZ_MASK		BIT_MASK(3)
42 
43 #define LIS2DH_LP_EN_BIT_MASK		BIT(3)
44 #if defined(CONFIG_LIS2DH_OPER_MODE_LOW_POWER)
45 	#define LIS2DH_LP_EN_BIT	BIT(3)
46 #else
47 	#define LIS2DH_LP_EN_BIT	0
48 #endif
49 
50 #define LIS2DH_SUSPEND			0
51 
52 #define LIS2DH_ODR_1			1
53 #define LIS2DH_ODR_2			2
54 #define LIS2DH_ODR_3			3
55 #define LIS2DH_ODR_4			4
56 #define LIS2DH_ODR_5			5
57 #define LIS2DH_ODR_6			6
58 #define LIS2DH_ODR_7			7
59 #define LIS2DH_ODR_8			8
60 #define LIS2DH_ODR_9			9
61 
62 #if defined(CONFIG_LIS2DH_ODR_1)
63 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_1
64 #elif defined(CONFIG_LIS2DH_ODR_2)
65 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_2
66 #elif defined(CONFIG_LIS2DH_ODR_3)
67 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_3
68 #elif defined(CONFIG_LIS2DH_ODR_4) || defined(CONFIG_LIS2DH_ODR_RUNTIME)
69 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_4
70 #elif defined(CONFIG_LIS2DH_ODR_5)
71 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_5
72 #elif defined(CONFIG_LIS2DH_ODR_6)
73 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_6
74 #elif defined(CONFIG_LIS2DH_ODR_7)
75 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_7
76 #elif defined(CONFIG_LIS2DH_ODR_8)
77 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_8
78 #elif defined(CONFIG_LIS2DH_ODR_9_NORMAL) || defined(CONFIG_LIS2DH_ODR_9_LOW)
79 	#define LIS2DH_ODR_IDX		LIS2DH_ODR_9
80 #endif
81 
82 #define LIS2DH_ODR_SHIFT		4
83 #define LIS2DH_ODR_RATE(r)		((r) << LIS2DH_ODR_SHIFT)
84 #define LIS2DH_ODR_BITS			(LIS2DH_ODR_RATE(LIS2DH_ODR_IDX))
85 #define LIS2DH_ODR_MASK			(BIT_MASK(4) << LIS2DH_ODR_SHIFT)
86 
87 #define LIS2DH_REG_CTRL2		0x21
88 #define LIS2DH_HPIS1_EN_BIT		BIT(0)
89 #define LIS2DH_HPIS2_EN_BIT		BIT(1)
90 #define LIS2DH_FDS_EN_BIT		BIT(3)
91 
92 #define LIS2DH_HPIS_EN_MASK		BIT_MASK(2)
93 
94 #define LIS2DH_REG_CTRL3		0x22
95 #define LIS2DH_EN_CLICK_INT1		BIT(7)
96 #define LIS2DH_EN_IA_INT1		BIT(6)
97 #define LIS2DH_EN_DRDY1_INT1		BIT(4)
98 
99 #define LIS2DH_REG_CTRL4		0x23
100 #define LIS2DH_CTRL4_BDU_BIT		BIT(7)
101 #define LIS2DH_FS_SHIFT			4
102 #define LIS2DH_FS_MASK			(BIT_MASK(2) << LIS2DH_FS_SHIFT)
103 
104 #if defined(CONFIG_LIS2DH_ACCEL_RANGE_2G) ||\
105 	defined(CONFIG_LIS2DH_ACCEL_RANGE_RUNTIME)
106 	#define LIS2DH_FS_IDX		0
107 #elif defined(CONFIG_LIS2DH_ACCEL_RANGE_4G)
108 	#define LIS2DH_FS_IDX		1
109 #elif defined(CONFIG_LIS2DH_ACCEL_RANGE_8G)
110 	#define LIS2DH_FS_IDX		2
111 #elif defined(CONFIG_LIS2DH_ACCEL_RANGE_16G)
112 	#define LIS2DH_FS_IDX		3
113 #endif
114 
115 #define LIS2DH_FS_SELECT(fs)		((fs) << LIS2DH_FS_SHIFT)
116 #define LIS2DH_FS_BITS			(LIS2DH_FS_SELECT(LIS2DH_FS_IDX))
117 #if defined(CONFIG_LIS2DH_OPER_MODE_HIGH_RES)
118 	#define LIS2DH_HR_BIT		BIT(3)
119 #else
120 	#define LIS2DH_HR_BIT		0
121 #endif
122 
123 #define LIS2DH_REG_CTRL5		0x24
124 #define LIS2DH_EN_LIR_INT2		BIT(1)
125 #define LIS2DH_EN_LIR_INT1		BIT(3)
126 
127 #define LIS2DH_REG_CTRL6		0x25
128 #define LIS2DH_EN_CLICK_INT2		BIT(7)
129 #define LIS2DH_EN_IA_INT2		BIT(5)
130 
131 #define LIS2DH_REG_REFERENCE		0x26
132 
133 #define LIS2DH_REG_STATUS		0x27
134 #define LIS2DH_STATUS_ZYZ_OVR		BIT(7)
135 #define LIS2DH_STATUS_Z_OVR		BIT(6)
136 #define LIS2DH_STATUS_Y_OVR		BIT(5)
137 #define LIS2DH_STATUS_X_OVR		BIT(4)
138 #define LIS2DH_STATUS_OVR_MASK		(BIT_MASK(4) << 4)
139 #define LIS2DH_STATUS_ZYX_DRDY		BIT(3)
140 #define LIS2DH_STATUS_Z_DRDY		BIT(2)
141 #define LIS2DH_STATUS_Y_DRDY		BIT(1)
142 #define LIS2DH_STATUS_X_DRDY		BIT(0)
143 #define LIS2DH_STATUS_DRDY_MASK		BIT_MASK(4)
144 
145 #define LIS2DH_REG_ACCEL_X_LSB		0x28
146 #define LIS2DH_REG_ACCEL_Y_LSB		0x2A
147 #define LIS2DH_REG_ACCEL_Z_LSB		0x2C
148 #define LIS2DH_REG_ACCEL_X_MSB		0x29
149 #define LIS2DH_REG_ACCEL_Y_MSB		0x2B
150 #define LIS2DH_REG_ACCEL_Z_MSB		0x2D
151 
152 #define LIS2DH_REG_INT1_CFG		0x30
153 #define LIS2DH_REG_INT1_SRC		0x31
154 #define LIS2DH_REG_INT1_THS		0x32
155 #define LIS2DH_REG_INT1_DUR		0x33
156 #define LIS2DH_REG_INT2_CFG		0x34
157 #define LIS2DH_REG_INT2_SRC		0x35
158 #define LIS2DH_REG_INT2_THS		0x36
159 #define LIS2DH_REG_INT2_DUR		0x37
160 
161 #define LIS2DH_INT_CFG_MODE_SHIFT	6
162 #define LIS2DH_INT_CFG_AOI_CFG		BIT(7)
163 #define LIS2DH_INT_CFG_6D_CFG		BIT(6)
164 #define LIS2DH_INT_CFG_ZHIE_ZUPE	BIT(5)
165 #define LIS2DH_INT_CFG_ZLIE_ZDOWNE	BIT(4)
166 #define LIS2DH_INT_CFG_YHIE_YUPE	BIT(3)
167 #define LIS2DH_INT_CFG_YLIE_YDOWNE	BIT(2)
168 #define LIS2DH_INT_CFG_XHIE_XUPE	BIT(1)
169 #define LIS2DH_INT_CFG_XLIE_XDOWNE	BIT(0)
170 
171 #define LIS2DH_REG_CFG_CLICK		0x38
172 #define LIS2DH_EN_CLICK_ZD		BIT(5)
173 #define LIS2DH_EN_CLICK_ZS		BIT(4)
174 #define LIS2DH_EN_CLICK_YD		BIT(3)
175 #define LIS2DH_EN_CLICK_YS		BIT(2)
176 #define LIS2DH_EN_CLICK_XD		BIT(1)
177 #define LIS2DH_EN_CLICK_XS		BIT(0)
178 
179 #define LIS2DH_REG_CLICK_SRC		0x39
180 #define LIS2DH_CLICK_SRC_DCLICK		BIT(5)
181 #define LIS2DH_CLICK_SRC_SCLICK		BIT(4)
182 
183 #define LIS2DH_REG_CFG_CLICK_THS	0x3A
184 #define LIS2DH_CLICK_LIR		BIT(7)
185 
186 #define LIS2DH_REG_TIME_LIMIT		0x3B
187 
188 /* sample buffer size includes status register */
189 #define LIS2DH_BUF_SZ			7
190 
191 union lis2dh_sample {
192 	uint8_t raw[LIS2DH_BUF_SZ];
193 	struct {
194 		uint8_t status;
195 		int16_t xyz[3];
196 	} __packed;
197 };
198 
199 union lis2dh_bus_cfg {
200 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
201 	struct i2c_dt_spec i2c;
202 #endif
203 
204 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
205 	struct spi_dt_spec spi;
206 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
207 };
208 
209 struct temperature {
210 	uint8_t cfg_addr;
211 	uint8_t enable_mask;
212 	uint8_t dout_addr;
213 	uint8_t fractional_bits;
214 };
215 
216 struct lis2dh_config {
217 	int (*bus_init)(const struct device *dev);
218 	const union lis2dh_bus_cfg bus_cfg;
219 #ifdef CONFIG_LIS2DH_TRIGGER
220 	const struct gpio_dt_spec gpio_drdy;
221 	const struct gpio_dt_spec gpio_int;
222 	const uint8_t int1_mode;
223 	const uint8_t int2_mode;
224 #endif /* CONFIG_LIS2DH_TRIGGER */
225 	struct {
226 		bool is_lsm303agr_dev : 1;
227 		bool disc_pull_up : 1;
228 		bool anym_on_int1 : 1;
229 		bool anym_latch : 1;
230 		uint8_t anym_mode : 2;
231 	} hw;
232 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE
233 	const struct temperature temperature;
234 #endif
235 };
236 
237 struct lis2dh_transfer_function {
238 	int (*read_data)(const struct device *dev, uint8_t reg_addr,
239 			 uint8_t *value, uint8_t len);
240 	int (*write_data)(const struct device *dev, uint8_t reg_addr,
241 			  uint8_t *value, uint8_t len);
242 	int (*read_reg)(const struct device *dev, uint8_t reg_addr,
243 			uint8_t *value);
244 	int (*write_reg)(const struct device *dev, uint8_t reg_addr,
245 			 uint8_t value);
246 	int (*update_reg)(const struct device *dev, uint8_t reg_addr,
247 			  uint8_t mask, uint8_t value);
248 };
249 
250 struct lis2dh_data {
251 	const struct device *bus;
252 	const struct lis2dh_transfer_function *hw_tf;
253 
254 	union lis2dh_sample sample;
255 	/* current scaling factor, in micro m/s^2 / lsb */
256 	uint32_t scale;
257 
258 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE
259 	struct sensor_value temperature;
260 #endif
261 
262 #ifdef CONFIG_PM_DEVICE
263 	uint8_t reg_ctrl1_active_val;
264 #endif
265 
266 #ifdef CONFIG_LIS2DH_TRIGGER
267 	const struct device *dev;
268 	struct gpio_callback gpio_int1_cb;
269 	struct gpio_callback gpio_int2_cb;
270 
271 	sensor_trigger_handler_t handler_drdy;
272 	const struct sensor_trigger *trig_drdy;
273 	sensor_trigger_handler_t handler_anymotion;
274 	const struct sensor_trigger *trig_anymotion;
275 	sensor_trigger_handler_t handler_tap;
276 	const struct sensor_trigger *trig_tap;
277 	atomic_t trig_flags;
278 	enum sensor_channel chan_drdy;
279 
280 #if defined(CONFIG_LIS2DH_TRIGGER_OWN_THREAD)
281 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DH_THREAD_STACK_SIZE);
282 	struct k_thread thread;
283 	struct k_sem gpio_sem;
284 #elif defined(CONFIG_LIS2DH_TRIGGER_GLOBAL_THREAD)
285 	struct k_work work;
286 #endif
287 
288 #endif /* CONFIG_LIS2DH_TRIGGER */
289 };
290 
291 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
292 int lis2dh_spi_access(struct lis2dh_data *ctx, uint8_t cmd,
293 		      void *data, size_t length);
294 #endif
295 
296 #ifdef CONFIG_LIS2DH_TRIGGER
297 int lis2dh_trigger_set(const struct device *dev,
298 		       const struct sensor_trigger *trig,
299 		       sensor_trigger_handler_t handler);
300 
301 int lis2dh_init_interrupt(const struct device *dev);
302 
303 int lis2dh_acc_slope_config(const struct device *dev,
304 			    enum sensor_attribute attr,
305 			    const struct sensor_value *val);
306 #endif
307 
308 #ifdef CONFIG_LIS2DH_ACCEL_HP_FILTERS
309 int lis2dh_acc_hp_filter_set(const struct device *dev,
310 			     int32_t val);
311 #endif
312 
313 int lis2dh_spi_init(const struct device *dev);
314 int lis2dh_i2c_init(const struct device *dev);
315 
316 
317 #endif /* __SENSOR_LIS2DH__ */
318