1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT st_lis3mdl_magn
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/sys/__assert.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/logging/log.h>
16 #include "lis3mdl.h"
17
18 LOG_MODULE_DECLARE(LIS3MDL, CONFIG_SENSOR_LOG_LEVEL);
19
lis3mdl_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)20 int lis3mdl_trigger_set(const struct device *dev,
21 const struct sensor_trigger *trig,
22 sensor_trigger_handler_t handler)
23 {
24 struct lis3mdl_data *drv_data = dev->data;
25 const struct lis3mdl_config *config = dev->config;
26 int16_t buf[3];
27 int ret;
28
29 if (!config->irq_gpio.port) {
30 return -ENOTSUP;
31 }
32
33 __ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
34
35 /* dummy read: re-trigger interrupt */
36 ret = i2c_burst_read_dt(&config->i2c, LIS3MDL_REG_SAMPLE_START,
37 (uint8_t *)buf, 6);
38 if (ret != 0) {
39 return ret;
40 }
41
42 gpio_pin_interrupt_configure_dt(&config->irq_gpio, GPIO_INT_DISABLE);
43
44 drv_data->data_ready_handler = handler;
45 if (handler == NULL) {
46 return 0;
47 }
48
49 drv_data->data_ready_trigger = trig;
50
51 gpio_pin_interrupt_configure_dt(&config->irq_gpio,
52 GPIO_INT_EDGE_TO_ACTIVE);
53
54 return 0;
55 }
56
lis3mdl_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)57 static void lis3mdl_gpio_callback(const struct device *dev,
58 struct gpio_callback *cb, uint32_t pins)
59 {
60 struct lis3mdl_data *drv_data =
61 CONTAINER_OF(cb, struct lis3mdl_data, gpio_cb);
62 const struct lis3mdl_config *config = drv_data->dev->config;
63
64 ARG_UNUSED(pins);
65
66 gpio_pin_interrupt_configure_dt(&config->irq_gpio, GPIO_INT_DISABLE);
67
68 #if defined(CONFIG_LIS3MDL_TRIGGER_OWN_THREAD)
69 k_sem_give(&drv_data->gpio_sem);
70 #elif defined(CONFIG_LIS3MDL_TRIGGER_GLOBAL_THREAD)
71 k_work_submit(&drv_data->work);
72 #endif
73 }
74
lis3mdl_thread_cb(const struct device * dev)75 static void lis3mdl_thread_cb(const struct device *dev)
76 {
77 struct lis3mdl_data *drv_data = dev->data;
78 const struct lis3mdl_config *config = dev->config;
79
80 if (drv_data->data_ready_handler != NULL) {
81 drv_data->data_ready_handler(dev,
82 drv_data->data_ready_trigger);
83 }
84
85 gpio_pin_interrupt_configure_dt(&config->irq_gpio,
86 GPIO_INT_EDGE_TO_ACTIVE);
87 }
88
89 #ifdef CONFIG_LIS3MDL_TRIGGER_OWN_THREAD
lis3mdl_thread(void * p1,void * p2,void * p3)90 static void lis3mdl_thread(void *p1, void *p2, void *p3)
91 {
92 ARG_UNUSED(p2);
93 ARG_UNUSED(p3);
94
95 struct lis3mdl_data *drv_data = p1;
96
97 while (1) {
98 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
99 lis3mdl_thread_cb(drv_data->dev);
100 }
101 }
102 #endif
103
104 #ifdef CONFIG_LIS3MDL_TRIGGER_GLOBAL_THREAD
lis3mdl_work_cb(struct k_work * work)105 static void lis3mdl_work_cb(struct k_work *work)
106 {
107 struct lis3mdl_data *drv_data =
108 CONTAINER_OF(work, struct lis3mdl_data, work);
109
110 lis3mdl_thread_cb(drv_data->dev);
111 }
112 #endif
113
lis3mdl_init_interrupt(const struct device * dev)114 int lis3mdl_init_interrupt(const struct device *dev)
115 {
116 struct lis3mdl_data *drv_data = dev->data;
117 const struct lis3mdl_config *config = dev->config;
118
119 if (!gpio_is_ready_dt(&config->irq_gpio)) {
120 LOG_ERR("GPIO device not ready");
121 return -ENODEV;
122 }
123
124 gpio_pin_configure_dt(&config->irq_gpio, GPIO_INPUT);
125
126 gpio_init_callback(&drv_data->gpio_cb,
127 lis3mdl_gpio_callback,
128 BIT(config->irq_gpio.pin));
129
130 if (gpio_add_callback(config->irq_gpio.port, &drv_data->gpio_cb) < 0) {
131 LOG_DBG("Could not set gpio callback.");
132 return -EIO;
133 }
134
135 /* clear data ready interrupt line by reading sample data */
136 if (lis3mdl_sample_fetch(dev, SENSOR_CHAN_ALL) < 0) {
137 LOG_DBG("Could not clear data ready interrupt line.");
138 return -EIO;
139 }
140
141 drv_data->dev = dev;
142
143 #if defined(CONFIG_LIS3MDL_TRIGGER_OWN_THREAD)
144 k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
145
146 k_thread_create(&drv_data->thread, drv_data->thread_stack,
147 CONFIG_LIS3MDL_THREAD_STACK_SIZE,
148 lis3mdl_thread, drv_data,
149 NULL, NULL, K_PRIO_COOP(CONFIG_LIS3MDL_THREAD_PRIORITY),
150 0, K_NO_WAIT);
151 #elif defined(CONFIG_LIS3MDL_TRIGGER_GLOBAL_THREAD)
152 drv_data->work.handler = lis3mdl_work_cb;
153 #endif
154
155 gpio_pin_interrupt_configure_dt(&config->irq_gpio,
156 GPIO_INT_EDGE_TO_ACTIVE);
157
158 return 0;
159 }
160