1 /* ST Microelectronics LIS2DS12 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/lis2ds12.pdf
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DS12_LIS2DS12_H_
12 #define ZEPHYR_DRIVERS_SENSOR_LIS2DS12_LIS2DS12_H_
13 
14 #include <zephyr/types.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <stmemsc.h>
18 #include "lis2ds12_reg.h"
19 
20 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
21 #include <zephyr/drivers/spi.h>
22 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
23 
24 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
25 #include <zephyr/drivers/i2c.h>
26 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
27 
28 /* Return ODR reg value based on data rate set */
29 #define LIS2DS12_ODR_TO_REG(_odr) \
30 	((_odr <= 1) ? 1 : \
31 	((31 - __builtin_clz(_odr / 25))) + 3)
32 
33 struct lis2ds12_config {
34 	stmdev_ctx_t ctx;
35 	union {
36 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
37 		const struct i2c_dt_spec i2c;
38 #endif
39 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
40 		const struct spi_dt_spec spi;
41 #endif
42 	} stmemsc_cfg;
43 	uint8_t range;
44 	uint8_t pm;
45 	uint8_t odr;
46 #ifdef CONFIG_LIS2DS12_TRIGGER
47 	struct gpio_dt_spec gpio_int;
48 #endif
49 };
50 
51 struct lis2ds12_data {
52 	int sample_x;
53 	int sample_y;
54 	int sample_z;
55 	float gain;
56 
57 #ifdef CONFIG_LIS2DS12_TRIGGER
58 	struct gpio_callback gpio_cb;
59 
60 	const struct sensor_trigger *data_ready_trigger;
61 	sensor_trigger_handler_t data_ready_handler;
62 	const struct device *dev;
63 
64 #if defined(CONFIG_LIS2DS12_TRIGGER_OWN_THREAD)
65 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DS12_THREAD_STACK_SIZE);
66 	struct k_thread thread;
67 	struct k_sem trig_sem;
68 #elif defined(CONFIG_LIS2DS12_TRIGGER_GLOBAL_THREAD)
69 	struct k_work work;
70 #endif
71 
72 #endif /* CONFIG_LIS2DS12_TRIGGER */
73 };
74 
75 #ifdef CONFIG_LIS2DS12_TRIGGER
76 int lis2ds12_trigger_set(const struct device *dev,
77 			 const struct sensor_trigger *trig,
78 			 sensor_trigger_handler_t handler);
79 
80 int lis2ds12_trigger_init(const struct device *dev);
81 #endif
82 
83 #endif /* ZEPHYR_DRIVERS_SENSOR_LIS2DS12_LIS2DS12_H_ */
84