1 /* ST Microelectronics LIS2DE12 3-axis accelerometer sensor driver
2  *
3  * Copyright (c) 2024 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2de12.pdf
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DE12_LIS2DE12_H_
12 #define ZEPHYR_DRIVERS_SENSOR_LIS2DE12_LIS2DE12_H_
13 
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/types.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <zephyr/sys/util.h>
18 #include <stmemsc.h>
19 #include "lis2de12_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 #define LIS2DE12_EN_BIT  0x01
30 #define LIS2DE12_DIS_BIT 0x00
31 
32 #define SENSOR_G_DOUBLE	(SENSOR_G / 1000000.0)
33 
34 struct lis2de12_config {
35 	stmdev_ctx_t ctx;
36 	union {
37 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
38 		const struct i2c_dt_spec i2c;
39 #endif
40 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
41 		const struct spi_dt_spec spi;
42 #endif
43 	} stmemsc_cfg;
44 	uint8_t accel_pm;
45 	uint8_t accel_odr;
46 	uint8_t accel_range;
47 	uint8_t drdy_pulsed;
48 #ifdef CONFIG_LIS2DE12_TRIGGER
49 	const struct gpio_dt_spec int1_gpio;
50 	const struct gpio_dt_spec int2_gpio;
51 	bool trig_enabled;
52 #endif /* CONFIG_LIS2DE12_TRIGGER */
53 };
54 
55 union samples {
56 	uint8_t raw[6];
57 	struct {
58 		int16_t axis[3];
59 	};
60 } __aligned(2);
61 
62 struct lis2de12_data {
63 	const struct device *dev;
64 	int16_t acc[3];
65 	int16_t temp_sample;
66 	uint32_t acc_gain;
67 	uint8_t accel_freq;
68 	uint8_t accel_fs;
69 
70 #ifdef CONFIG_LIS2DE12_TRIGGER
71 	struct gpio_dt_spec *drdy_gpio;
72 
73 	struct gpio_callback gpio_cb;
74 	sensor_trigger_handler_t handler_drdy_acc;
75 	const struct sensor_trigger *trig_drdy_acc;
76 
77 #if defined(CONFIG_LIS2DE12_TRIGGER_OWN_THREAD)
78 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DE12_THREAD_STACK_SIZE);
79 	struct k_thread thread;
80 	struct k_sem gpio_sem;
81 #elif defined(CONFIG_LIS2DE12_TRIGGER_GLOBAL_THREAD)
82 	struct k_work work;
83 #endif
84 #endif /* CONFIG_LIS2DE12_TRIGGER */
85 };
86 
87 #ifdef CONFIG_LIS2DE12_TRIGGER
88 int lis2de12_trigger_set(const struct device *dev,
89 			const struct sensor_trigger *trig,
90 			sensor_trigger_handler_t handler);
91 
92 int lis2de12_init_interrupt(const struct device *dev);
93 #endif
94 
95 #endif /* ZEPHYR_DRIVERS_SENSOR_LIS2DE12_LIS2DE12_H_ */
96