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 #define DT_DRV_COMPAT st_lis2mdl
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/drivers/gpio.h>
16 #include <zephyr/logging/log.h>
17 #include "lis2mdl.h"
18 
19 LOG_MODULE_DECLARE(LIS2MDL, CONFIG_SENSOR_LOG_LEVEL);
20 
lis2mdl_enable_int(const struct device * dev,int enable)21 static int lis2mdl_enable_int(const struct device *dev, int enable)
22 {
23 	const struct lis2mdl_config *cfg = dev->config;
24 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
25 
26 	LOG_DBG("Set int with %d", enable);
27 	/* set interrupt on mag */
28 	return lis2mdl_drdy_on_pin_set(ctx, enable);
29 }
30 
31 /* link external trigger to event data ready */
lis2mdl_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)32 int lis2mdl_trigger_set(const struct device *dev,
33 			  const struct sensor_trigger *trig,
34 			  sensor_trigger_handler_t handler)
35 {
36 	const struct lis2mdl_config *cfg = dev->config;
37 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
38 	struct lis2mdl_data *lis2mdl = dev->data;
39 	int16_t raw[3];
40 
41 	if (!cfg->trig_enabled) {
42 		LOG_ERR("trigger_set op not supported");
43 		return -ENOTSUP;
44 	}
45 
46 	if (trig->chan == SENSOR_CHAN_MAGN_XYZ) {
47 		lis2mdl->handler_drdy = handler;
48 		lis2mdl->trig_drdy = trig;
49 		if (handler) {
50 			/* fetch raw data sample: re-trigger lost interrupt */
51 			lis2mdl_magnetic_raw_get(ctx, raw);
52 
53 			return lis2mdl_enable_int(dev, 1);
54 		} else {
55 			return lis2mdl_enable_int(dev, 0);
56 		}
57 	}
58 
59 	return -ENOTSUP;
60 }
61 
62 /* handle the drdy event: read data and call handler if registered any */
lis2mdl_handle_interrupt(const struct device * dev)63 static void lis2mdl_handle_interrupt(const struct device *dev)
64 {
65 	struct lis2mdl_data *lis2mdl = dev->data;
66 	const struct lis2mdl_config *const cfg = dev->config;
67 
68 	if (lis2mdl->handler_drdy != NULL) {
69 		lis2mdl->handler_drdy(dev, lis2mdl->trig_drdy);
70 	}
71 
72 	if (cfg->single_mode) {
73 		k_sem_give(&lis2mdl->fetch_sem);
74 	}
75 
76 	gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy,
77 					GPIO_INT_EDGE_TO_ACTIVE);
78 }
79 
lis2mdl_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)80 static void lis2mdl_gpio_callback(const struct device *dev,
81 				    struct gpio_callback *cb, uint32_t pins)
82 {
83 	struct lis2mdl_data *lis2mdl =
84 		CONTAINER_OF(cb, struct lis2mdl_data, gpio_cb);
85 	const struct lis2mdl_config *const cfg = lis2mdl->dev->config;
86 
87 	ARG_UNUSED(pins);
88 
89 	gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy, GPIO_INT_DISABLE);
90 
91 #if defined(CONFIG_LIS2MDL_TRIGGER_OWN_THREAD)
92 	k_sem_give(&lis2mdl->gpio_sem);
93 #elif defined(CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD)
94 	k_work_submit(&lis2mdl->work);
95 #endif
96 }
97 
98 #ifdef CONFIG_LIS2MDL_TRIGGER_OWN_THREAD
lis2mdl_thread(void * p1,void * p2,void * p3)99 static void lis2mdl_thread(void *p1, void *p2, void *p3)
100 {
101 	ARG_UNUSED(p2);
102 	ARG_UNUSED(p3);
103 
104 	struct lis2mdl_data *lis2mdl = p1;
105 
106 	while (1) {
107 		k_sem_take(&lis2mdl->gpio_sem, K_FOREVER);
108 		lis2mdl_handle_interrupt(lis2mdl->dev);
109 	}
110 }
111 #endif
112 
113 #ifdef CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD
lis2mdl_work_cb(struct k_work * work)114 static void lis2mdl_work_cb(struct k_work *work)
115 {
116 	struct lis2mdl_data *lis2mdl =
117 		CONTAINER_OF(work, struct lis2mdl_data, work);
118 
119 	lis2mdl_handle_interrupt(lis2mdl->dev);
120 }
121 #endif
122 
lis2mdl_init_interrupt(const struct device * dev)123 int lis2mdl_init_interrupt(const struct device *dev)
124 {
125 	struct lis2mdl_data *lis2mdl = dev->data;
126 	const struct lis2mdl_config *const cfg = dev->config;
127 	int ret;
128 
129 	/* setup data ready gpio interrupt */
130 	if (!gpio_is_ready_dt(&cfg->gpio_drdy)) {
131 		LOG_ERR("Cannot get pointer to drdy_gpio device");
132 		return -EINVAL;
133 	}
134 
135 #if defined(CONFIG_LIS2MDL_TRIGGER_OWN_THREAD)
136 	k_sem_init(&lis2mdl->gpio_sem, 0, K_SEM_MAX_LIMIT);
137 	k_thread_create(&lis2mdl->thread, lis2mdl->thread_stack,
138 			CONFIG_LIS2MDL_THREAD_STACK_SIZE,
139 			lis2mdl_thread, lis2mdl,
140 			NULL, NULL, K_PRIO_COOP(CONFIG_LIS2MDL_THREAD_PRIORITY),
141 			0, K_NO_WAIT);
142 #elif defined(CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD)
143 	lis2mdl->work.handler = lis2mdl_work_cb;
144 #endif
145 
146 	ret = gpio_pin_configure_dt(&cfg->gpio_drdy, GPIO_INPUT);
147 	if (ret < 0) {
148 		LOG_ERR("Could not configure gpio");
149 		return ret;
150 	}
151 
152 	gpio_init_callback(&lis2mdl->gpio_cb,
153 			   lis2mdl_gpio_callback,
154 			   BIT(cfg->gpio_drdy.pin));
155 
156 	if (gpio_add_callback(cfg->gpio_drdy.port, &lis2mdl->gpio_cb) < 0) {
157 		LOG_ERR("Could not set gpio callback");
158 		return -EIO;
159 	}
160 
161 	return gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy,
162 					       GPIO_INT_EDGE_TO_ACTIVE);
163 }
164