1 /* ST Microelectronics IIS328DQ 3-axis accelerometer driver
2  *
3  * Copyright (c) 2020 STMicroelectronics
4  * Copyright (c) 2024 SILA Embedded Solutions GmbH
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Datasheet:
9  * https://www.st.com/resource/en/datasheet/iis328dq.pdf
10  */
11 
12 #ifndef ZEPHYR_DRIVERS_SENSOR_IIS328DQ_IIS328DQ_H_
13 #define ZEPHYR_DRIVERS_SENSOR_IIS328DQ_IIS328DQ_H_
14 
15 #include <zephyr/drivers/gpio.h>
16 #include <zephyr/sys/util.h>
17 #include <zephyr/drivers/sensor.h>
18 #include <stmemsc.h>
19 #include "iis328dq_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 /**
30  * struct iis328dq_dev_config - iis328dq hw configuration
31  * @bus_name: Pointer to bus master identifier.
32  * @pm: Power mode (lis2dh_powermode).
33  * @irq_dev_name: Pointer to GPIO PORT identifier.
34  * @irq_pin: GPIO pin number connected to sensor int pin.
35  * @drdy_int: Sensor drdy int (int1/int2).
36  */
37 struct iis328dq_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 range;
48 #ifdef CONFIG_IIS328DQ_TRIGGER
49 	const struct gpio_dt_spec gpio_int1;
50 	const struct gpio_dt_spec gpio_int2;
51 	/* interrupt pad to be used for DRDY interrupts, or -1 if not configured */
52 	int8_t drdy_pad;
53 #ifdef CONFIG_IIS328DQ_THRESHOLD
54 	/* interrupt pad to be used for threshold interrupts, or -1 if not configured */
55 	int8_t threshold_pad;
56 #endif /* CONFIG_IIS328DQ_THRESHOLD */
57 #endif /* CONFIG_IIS328DQ_TRIGGER */
58 };
59 
60 /* sensor data */
61 struct iis328dq_data {
62 	const struct device *dev;
63 	int16_t acc[3];
64 
65 	/* sensitivity in mg/LSB */
66 	uint8_t gain;
67 
68 #ifdef CONFIG_IIS328DQ_TRIGGER
69 	struct gpio_callback int1_cb;
70 	struct gpio_callback int2_cb;
71 	sensor_trigger_handler_t drdy_handler;
72 	const struct sensor_trigger *drdy_trig;
73 #ifdef CONFIG_IIS328DQ_THRESHOLD
74 	sensor_trigger_handler_t threshold_handler;
75 	const struct sensor_trigger *threshold_trig;
76 #endif /* CONFIG_IIS328DQ_THRESHOLD */
77 #if defined(CONFIG_IIS328DQ_TRIGGER_OWN_THREAD)
78 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_IIS328DQ_THREAD_STACK_SIZE);
79 	struct k_thread thread;
80 	struct k_sem gpio_sem;
81 #elif defined(CONFIG_IIS328DQ_TRIGGER_GLOBAL_THREAD)
82 	struct k_work work;
83 #endif /* CONFIG_IIS328DQ_TRIGGER_GLOBAL_THREAD */
84 #endif /* CONFIG_IIS328DQ_TRIGGER */
85 };
86 
87 #ifdef CONFIG_IIS328DQ_TRIGGER
88 int iis328dq_init_interrupt(const struct device *dev);
89 int iis328dq_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
90 			 sensor_trigger_handler_t handler);
91 #endif /* CONFIG_IIS328DQ_TRIGGER */
92 
93 #endif /* ZEPHYR_DRIVERS_SENSOR_IIS328DQ_IIS328DQ_H_ */
94