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(struct lis2mdl_data * lis2mdl)99 static void lis2mdl_thread(struct lis2mdl_data *lis2mdl)
100 {
101 while (1) {
102 k_sem_take(&lis2mdl->gpio_sem, K_FOREVER);
103 lis2mdl_handle_interrupt(lis2mdl->dev);
104 }
105 }
106 #endif
107
108 #ifdef CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD
lis2mdl_work_cb(struct k_work * work)109 static void lis2mdl_work_cb(struct k_work *work)
110 {
111 struct lis2mdl_data *lis2mdl =
112 CONTAINER_OF(work, struct lis2mdl_data, work);
113
114 lis2mdl_handle_interrupt(lis2mdl->dev);
115 }
116 #endif
117
lis2mdl_init_interrupt(const struct device * dev)118 int lis2mdl_init_interrupt(const struct device *dev)
119 {
120 struct lis2mdl_data *lis2mdl = dev->data;
121 const struct lis2mdl_config *const cfg = dev->config;
122 int ret;
123
124 /* setup data ready gpio interrupt */
125 if (!gpio_is_ready_dt(&cfg->gpio_drdy)) {
126 LOG_ERR("Cannot get pointer to drdy_gpio device");
127 return -EINVAL;
128 }
129
130 #if defined(CONFIG_LIS2MDL_TRIGGER_OWN_THREAD)
131 k_sem_init(&lis2mdl->gpio_sem, 0, K_SEM_MAX_LIMIT);
132 k_thread_create(&lis2mdl->thread, lis2mdl->thread_stack,
133 CONFIG_LIS2MDL_THREAD_STACK_SIZE,
134 (k_thread_entry_t)lis2mdl_thread, lis2mdl,
135 NULL, NULL, K_PRIO_COOP(CONFIG_LIS2MDL_THREAD_PRIORITY),
136 0, K_NO_WAIT);
137 #elif defined(CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD)
138 lis2mdl->work.handler = lis2mdl_work_cb;
139 #endif
140
141 ret = gpio_pin_configure_dt(&cfg->gpio_drdy, GPIO_INPUT);
142 if (ret < 0) {
143 LOG_ERR("Could not configure gpio");
144 return ret;
145 }
146
147 gpio_init_callback(&lis2mdl->gpio_cb,
148 lis2mdl_gpio_callback,
149 BIT(cfg->gpio_drdy.pin));
150
151 if (gpio_add_callback(cfg->gpio_drdy.port, &lis2mdl->gpio_cb) < 0) {
152 LOG_ERR("Could not set gpio callback");
153 return -EIO;
154 }
155
156 return gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy,
157 GPIO_INT_EDGE_TO_ACTIVE);
158 }
159