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(struct lis3mdl_data * drv_data)90 static void lis3mdl_thread(struct lis3mdl_data *drv_data)
91 {
92 	while (1) {
93 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
94 		lis3mdl_thread_cb(drv_data->dev);
95 	}
96 }
97 #endif
98 
99 #ifdef CONFIG_LIS3MDL_TRIGGER_GLOBAL_THREAD
lis3mdl_work_cb(struct k_work * work)100 static void lis3mdl_work_cb(struct k_work *work)
101 {
102 	struct lis3mdl_data *drv_data =
103 		CONTAINER_OF(work, struct lis3mdl_data, work);
104 
105 	lis3mdl_thread_cb(drv_data->dev);
106 }
107 #endif
108 
lis3mdl_init_interrupt(const struct device * dev)109 int lis3mdl_init_interrupt(const struct device *dev)
110 {
111 	struct lis3mdl_data *drv_data = dev->data;
112 	const struct lis3mdl_config *config = dev->config;
113 
114 	if (!gpio_is_ready_dt(&config->irq_gpio)) {
115 		LOG_ERR("GPIO device not ready");
116 		return -ENODEV;
117 	}
118 
119 	gpio_pin_configure_dt(&config->irq_gpio, GPIO_INPUT);
120 
121 	gpio_init_callback(&drv_data->gpio_cb,
122 			   lis3mdl_gpio_callback,
123 			   BIT(config->irq_gpio.pin));
124 
125 	if (gpio_add_callback(config->irq_gpio.port, &drv_data->gpio_cb) < 0) {
126 		LOG_DBG("Could not set gpio callback.");
127 		return -EIO;
128 	}
129 
130 	/* clear data ready interrupt line by reading sample data */
131 	if (lis3mdl_sample_fetch(dev, SENSOR_CHAN_ALL) < 0) {
132 		LOG_DBG("Could not clear data ready interrupt line.");
133 		return -EIO;
134 	}
135 
136 	drv_data->dev = dev;
137 
138 #if defined(CONFIG_LIS3MDL_TRIGGER_OWN_THREAD)
139 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
140 
141 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
142 			CONFIG_LIS3MDL_THREAD_STACK_SIZE,
143 			(k_thread_entry_t)lis3mdl_thread, drv_data,
144 			NULL, NULL, K_PRIO_COOP(CONFIG_LIS3MDL_THREAD_PRIORITY),
145 			0, K_NO_WAIT);
146 #elif defined(CONFIG_LIS3MDL_TRIGGER_GLOBAL_THREAD)
147 	drv_data->work.handler = lis3mdl_work_cb;
148 #endif
149 
150 	gpio_pin_interrupt_configure_dt(&config->irq_gpio,
151 					GPIO_INT_EDGE_TO_ACTIVE);
152 
153 	return 0;
154 }
155