1 /* ST Microelectronics IIS2MDC 3-axis magnetometer sensor
2  *
3  * Copyright (c) 2020 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/iis2mdc.pdf
9  */
10 
11 #ifndef __MAG_IIS2MDC_H
12 #define __MAG_IIS2MDC_H
13 
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/sys/util.h>
18 #include <stmemsc.h>
19 #include "iis2mdc_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 struct iis2mdc_dev_config {
30 	union {
31 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
32 		struct i2c_dt_spec i2c;
33 #endif
34 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
35 		struct spi_dt_spec spi;
36 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
37 	};
38 	int (*bus_init)(const struct device *dev);
39 #ifdef CONFIG_IIS2MDC_TRIGGER
40 	const struct gpio_dt_spec gpio_drdy;
41 #endif  /* CONFIG_IIS2MDC_TRIGGER */
42 };
43 
44 /* Sensor data */
45 struct iis2mdc_data {
46 	const struct device *dev;
47 	int16_t mag[3];
48 	int32_t temp_sample;
49 
50 	stmdev_ctx_t *ctx;
51 
52 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
53 	stmdev_ctx_t ctx_i2c;
54 #endif
55 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
56 	stmdev_ctx_t ctx_spi;
57 #endif
58 
59 #ifdef CONFIG_IIS2MDC_TRIGGER
60 	struct gpio_callback gpio_cb;
61 
62 	sensor_trigger_handler_t handler_drdy;
63 	const struct sensor_trigger *trig_drdy;
64 
65 #if defined(CONFIG_IIS2MDC_TRIGGER_OWN_THREAD)
66 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_IIS2MDC_THREAD_STACK_SIZE);
67 	struct k_thread thread;
68 	struct k_sem gpio_sem;
69 #elif defined(CONFIG_IIS2MDC_TRIGGER_GLOBAL_THREAD)
70 	struct k_work work;
71 #endif  /* CONFIG_IIS2MDC_TRIGGER_GLOBAL_THREAD */
72 #endif  /* CONFIG_IIS2MDC_TRIGGER */
73 };
74 
75 int iis2mdc_spi_init(const struct device *dev);
76 int iis2mdc_i2c_init(const struct device *dev);
77 
78 #ifdef CONFIG_IIS2MDC_TRIGGER
79 int iis2mdc_init_interrupt(const struct device *dev);
80 int iis2mdc_trigger_set(const struct device *dev,
81 			  const struct sensor_trigger *trig,
82 			  sensor_trigger_handler_t handler);
83 #endif /* CONFIG_IIS2MDC_TRIGGER */
84 
85 #endif /* __MAG_IIS2MDC_H */
86