1 /* ST Microelectronics LIS2DU12 3-axis accelerometer sensor driver
2  *
3  * Copyright (c) 2023 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2du12.pdf
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DU12_LIS2DU12_H_
12 #define ZEPHYR_DRIVERS_SENSOR_LIS2DU12_LIS2DU12_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 "lis2du12_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 LIS2DU12_EN_BIT  0x01
30 #define LIS2DU12_DIS_BIT 0x00
31 
32 /* Accel sensor sensitivity grain is 61 ug/LSB */
33 #define GAIN_UNIT_XL	(61LL)
34 
35 #define SENSOR_G_DOUBLE	(SENSOR_G / 1000000.0)
36 
37 struct lis2du12_config {
38 	stmdev_ctx_t ctx;
39 	union {
40 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
41 		const struct i2c_dt_spec i2c;
42 #endif
43 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
44 		const struct spi_dt_spec spi;
45 #endif
46 	} stmemsc_cfg;
47 	uint8_t accel_pm;
48 	uint8_t accel_odr;
49 	uint8_t accel_range;
50 	uint8_t drdy_pulsed;
51 #ifdef CONFIG_LIS2DU12_TRIGGER
52 	const struct gpio_dt_spec int1_gpio;
53 	const struct gpio_dt_spec int2_gpio;
54 	uint8_t drdy_pin;
55 	bool trig_enabled;
56 #endif /* CONFIG_LIS2DU12_TRIGGER */
57 };
58 
59 union samples {
60 	uint8_t raw[6];
61 	struct {
62 		int16_t axis[3];
63 	};
64 } __aligned(2);
65 
66 struct lis2du12_data {
67 	const struct device *dev;
68 	int16_t acc[3];
69 	uint32_t acc_gain;
70 	uint16_t accel_freq;
71 	uint8_t accel_fs;
72 
73 #ifdef CONFIG_LIS2DU12_TRIGGER
74 	struct gpio_dt_spec *drdy_gpio;
75 
76 	struct gpio_callback gpio_cb;
77 	sensor_trigger_handler_t handler_drdy_acc;
78 	const struct sensor_trigger *trig_drdy_acc;
79 
80 #if defined(CONFIG_LIS2DU12_TRIGGER_OWN_THREAD)
81 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DU12_THREAD_STACK_SIZE);
82 	struct k_thread thread;
83 	struct k_sem gpio_sem;
84 #elif defined(CONFIG_LIS2DU12_TRIGGER_GLOBAL_THREAD)
85 	struct k_work work;
86 #endif
87 #endif /* CONFIG_LIS2DU12_TRIGGER */
88 };
89 
90 #ifdef CONFIG_LIS2DU12_TRIGGER
91 int lis2du12_trigger_set(const struct device *dev,
92 			const struct sensor_trigger *trig,
93 			sensor_trigger_handler_t handler);
94 
95 int lis2du12_init_interrupt(const struct device *dev);
96 #endif
97 
98 #endif /* ZEPHYR_DRIVERS_SENSOR_LIS2DU12_LIS2DU12_H_ */
99