1 /* ST Microelectronics LIS2DUX12 3-axis accelerometer 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/lis2dux12.pdf
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DUX12_LIS2DUX12_H_
12 #define ZEPHYR_DRIVERS_SENSOR_LIS2DUX12_LIS2DUX12_H_
13 
14 #include <zephyr/types.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <stmemsc.h>
18 
19 #if DT_HAS_COMPAT_STATUS_OKAY(st_lis2dux12)
20 #include "lis2dux12_reg.h"
21 #endif
22 
23 #if DT_HAS_COMPAT_STATUS_OKAY(st_lis2duxs12)
24 #include "lis2duxs12_reg.h"
25 #endif
26 
27 #if DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2dux12, spi) || \
28 	DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2duxs12, spi)
29 #include <zephyr/drivers/spi.h>
30 #endif
31 
32 #if DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2dux12, i2c) || \
33 	DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2duxs12, i2c)
34 #include <zephyr/drivers/i2c.h>
35 #endif
36 
37 typedef int32_t (*api_lis2dux12_set_odr_raw)(const struct device *dev, uint8_t odr);
38 typedef int32_t (*api_lis2dux12_set_range)(const struct device *dev, uint8_t range);
39 typedef int32_t (*api_lis2dux12_sample_fetch_accel)(const struct device *dev);
40 #ifdef CONFIG_LIS2DUX12_ENABLE_TEMP
41 typedef int32_t (*api_lis2dux12_sample_fetch_temp)(const struct device *dev);
42 #endif
43 #ifdef CONFIG_LIS2DUX12_TRIGGER
44 typedef void (*api_lis2dux12_handle_interrupt)(const struct device *dev);
45 typedef int32_t (*api_lis2dux12_init_interrupt)(const struct device *dev);
46 #endif
47 
48 struct lis2dux12_chip_api {
49 	api_lis2dux12_set_odr_raw set_odr_raw;
50 	api_lis2dux12_set_range	set_range;
51 	api_lis2dux12_sample_fetch_accel sample_fetch_accel;
52 #ifdef CONFIG_LIS2DUX12_ENABLE_TEMP
53 	api_lis2dux12_sample_fetch_temp sample_fetch_temp;
54 #endif
55 #ifdef CONFIG_LIS2DUX12_TRIGGER
56 	api_lis2dux12_handle_interrupt handle_interrupt;
57 	api_lis2dux12_init_interrupt init_interrupt;
58 #endif
59 };
60 
61 struct lis2dux12_config {
62 	stmdev_ctx_t ctx;
63 	union {
64 #if DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2dux12, i2c) || \
65 	DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2duxs12, i2c)
66 		const struct i2c_dt_spec i2c;
67 #endif
68 #if DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2dux12, spi) || \
69 	DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(st_lis2duxs12, spi)
70 		const struct spi_dt_spec spi;
71 #endif
72 	} stmemsc_cfg;
73 	uint8_t range;
74 	uint8_t pm;
75 	uint8_t odr;
76 #ifdef CONFIG_LIS2DUX12_TRIGGER
77 	const struct gpio_dt_spec int1_gpio;
78 	const struct gpio_dt_spec int2_gpio;
79 	uint8_t drdy_pin;
80 	bool trig_enabled;
81 #endif
82 
83 	const struct lis2dux12_chip_api *chip_api;
84 };
85 
86 struct lis2dux12_data {
87 	int sample_x;
88 	int sample_y;
89 	int sample_z;
90 	float gain;
91 	uint8_t range;
92 	uint8_t odr;
93 
94 #ifdef CONFIG_LIS2DUX12_ENABLE_TEMP
95 	float sample_temp;
96 #endif
97 
98 #ifdef CONFIG_LIS2DUX12_TRIGGER
99 	struct gpio_dt_spec *drdy_gpio;
100 	struct gpio_callback gpio_cb;
101 
102 	const struct sensor_trigger *data_ready_trigger;
103 	sensor_trigger_handler_t data_ready_handler;
104 	const struct device *dev;
105 
106 #if defined(CONFIG_LIS2DUX12_TRIGGER_OWN_THREAD)
107 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DUX12_THREAD_STACK_SIZE);
108 	struct k_thread thread;
109 	struct k_sem trig_sem;
110 #elif defined(CONFIG_LIS2DUX12_TRIGGER_GLOBAL_THREAD)
111 	struct k_work work;
112 #endif
113 
114 #endif /* CONFIG_LIS2DUX12_TRIGGER */
115 };
116 
117 #ifdef CONFIG_LIS2DUX12_TRIGGER
118 int lis2dux12_trigger_set(const struct device *dev,
119 			 const struct sensor_trigger *trig,
120 			 sensor_trigger_handler_t handler);
121 
122 int lis2dux12_trigger_init(const struct device *dev);
123 #endif
124 
125 #endif /* ZEPHYR_DRIVERS_SENSOR_LIS2DUX12_LIS2DUX12_H_ */
126