1 /* ST Microelectronics LIS2MDL 3-axis magnetometer sensor
2  *
3  * Copyright (c) 2018-2019 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2mdl.pdf
9  */
10 
11 #ifndef __MAG_LIS2MDL_H
12 #define __MAG_LIS2MDL_H
13 
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/sys/util.h>
17 #include <stmemsc.h>
18 #include "lis2mdl_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 struct lis2mdl_config {
29 	stmdev_ctx_t ctx;
30 	union {
31 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
32 		const struct i2c_dt_spec i2c;
33 #endif
34 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
35 		const struct spi_dt_spec spi;
36 #endif
37 	} stmemsc_cfg;
38 	bool cancel_offset;
39 	bool single_mode;
40 	bool spi_4wires;
41 
42 #ifdef CONFIG_LIS2MDL_TRIGGER
43 	bool trig_enabled;
44 	const struct gpio_dt_spec gpio_drdy;
45 #endif  /* CONFIG_LIS2MDL_TRIGGER */
46 };
47 
48 /* Sensor data */
49 struct lis2mdl_data {
50 	const struct device *dev;
51 	int16_t mag[3];
52 	int16_t temp_sample;
53 	struct k_sem fetch_sem;
54 
55 #ifdef CONFIG_LIS2MDL_TRIGGER
56 	struct gpio_callback gpio_cb;
57 
58 	sensor_trigger_handler_t handler_drdy;
59 	const struct sensor_trigger *trig_drdy;
60 
61 #if defined(CONFIG_LIS2MDL_TRIGGER_OWN_THREAD)
62 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2MDL_THREAD_STACK_SIZE);
63 	struct k_thread thread;
64 	struct k_sem gpio_sem;
65 #elif defined(CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD)
66 	struct k_work work;
67 #endif  /* CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD */
68 #endif  /* CONFIG_LIS2MDL_TRIGGER */
69 };
70 
71 #ifdef CONFIG_LIS2MDL_TRIGGER
72 int lis2mdl_init_interrupt(const struct device *dev);
73 int lis2mdl_trigger_set(const struct device *dev,
74 			  const struct sensor_trigger *trig,
75 			  sensor_trigger_handler_t handler);
76 #endif /* CONFIG_LIS2MDL_TRIGGER */
77 
78 #endif /* __MAG_LIS2MDL_H */
79