1 /*
2  * Copyright (c) 2021, Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/sys/util.h>
8 #include <zephyr/logging/log.h>
9 
10 #include "mpu9250.h"
11 
12 LOG_MODULE_DECLARE(MPU9250, CONFIG_SENSOR_LOG_LEVEL);
13 
14 
15 #define MPU9250_REG_INT_EN	0x38
16 #define MPU9250_DRDY_EN		BIT(0)
17 
18 
mpu9250_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)19 int mpu9250_trigger_set(const struct device *dev,
20 			const struct sensor_trigger *trig,
21 			sensor_trigger_handler_t handler)
22 {
23 	struct mpu9250_data *drv_data = dev->data;
24 	const struct mpu9250_config *cfg = dev->config;
25 	int ret;
26 
27 	if (trig->type != SENSOR_TRIG_DATA_READY) {
28 		return -ENOTSUP;
29 	}
30 
31 	ret = gpio_pin_interrupt_configure_dt(&cfg->int_pin, GPIO_INT_DISABLE);
32 	if (ret < 0) {
33 		LOG_ERR("Failed to disable gpio interrupt.");
34 		return ret;
35 	}
36 
37 	drv_data->data_ready_handler = handler;
38 	if (handler == NULL) {
39 		return 0;
40 	}
41 
42 	drv_data->data_ready_trigger = trig;
43 
44 	ret = gpio_pin_interrupt_configure_dt(&cfg->int_pin,
45 					      GPIO_INT_EDGE_TO_ACTIVE);
46 	if (ret < 0) {
47 		LOG_ERR("Failed to enable gpio interrupt.");
48 		return ret;
49 	}
50 
51 	return 0;
52 }
53 
mpu9250_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)54 static void mpu9250_gpio_callback(const struct device *dev,
55 				  struct gpio_callback *cb, uint32_t pins)
56 {
57 	struct mpu9250_data *drv_data =
58 		CONTAINER_OF(cb, struct mpu9250_data, gpio_cb);
59 	const struct mpu9250_config *cfg = drv_data->dev->config;
60 	int ret;
61 
62 	ARG_UNUSED(pins);
63 
64 	ret = gpio_pin_interrupt_configure_dt(&cfg->int_pin, GPIO_INT_DISABLE);
65 	if (ret < 0) {
66 		LOG_ERR("Disabling gpio interrupt failed with err: %d", ret);
67 		return;
68 	}
69 
70 #if defined(CONFIG_MPU9250_TRIGGER_OWN_THREAD)
71 	k_sem_give(&drv_data->gpio_sem);
72 #elif defined(CONFIG_MPU9250_TRIGGER_GLOBAL_THREAD)
73 	k_work_submit(&drv_data->work);
74 #endif
75 }
76 
mpu9250_thread_cb(const struct device * dev)77 static void mpu9250_thread_cb(const struct device *dev)
78 {
79 	struct mpu9250_data *drv_data = dev->data;
80 	const struct mpu9250_config *cfg = dev->config;
81 	int ret;
82 
83 	if (drv_data->data_ready_handler != NULL) {
84 		drv_data->data_ready_handler(dev,
85 					     drv_data->data_ready_trigger);
86 	}
87 
88 	ret = gpio_pin_interrupt_configure_dt(&cfg->int_pin,
89 					      GPIO_INT_EDGE_TO_ACTIVE);
90 	if (ret < 0) {
91 		LOG_ERR("Enabling gpio interrupt failed with err: %d", ret);
92 	}
93 
94 }
95 
96 #ifdef CONFIG_MPU9250_TRIGGER_OWN_THREAD
mpu9250_thread(void * p1,void * p2,void * p3)97 static void mpu9250_thread(void *p1, void *p2, void *p3)
98 {
99 	ARG_UNUSED(p2);
100 	ARG_UNUSED(p3);
101 
102 	struct mpu9250_data *drv_data = p1;
103 
104 	while (1) {
105 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
106 		mpu9250_thread_cb(drv_data->dev);
107 	}
108 }
109 #endif
110 
111 #ifdef CONFIG_MPU9250_TRIGGER_GLOBAL_THREAD
mpu9250_work_cb(struct k_work * work)112 static void mpu9250_work_cb(struct k_work *work)
113 {
114 	struct mpu9250_data *drv_data =
115 		CONTAINER_OF(work, struct mpu9250_data, work);
116 
117 	mpu9250_thread_cb(drv_data->dev);
118 }
119 #endif
120 
mpu9250_init_interrupt(const struct device * dev)121 int mpu9250_init_interrupt(const struct device *dev)
122 {
123 	struct mpu9250_data *drv_data = dev->data;
124 	const struct mpu9250_config *cfg = dev->config;
125 	int ret;
126 
127 	/* setup data ready gpio interrupt */
128 	if (!gpio_is_ready_dt(&cfg->int_pin)) {
129 		LOG_ERR("Interrupt pin is not ready.");
130 		return -EIO;
131 	}
132 
133 	drv_data->dev = dev;
134 
135 	ret = gpio_pin_configure_dt(&cfg->int_pin, GPIO_INPUT);
136 	if (ret < 0) {
137 		LOG_ERR("Failed to configure interrupt pin.");
138 		return ret;
139 	}
140 
141 	gpio_init_callback(&drv_data->gpio_cb,
142 			   mpu9250_gpio_callback,
143 			   BIT(cfg->int_pin.pin));
144 
145 	ret = gpio_add_callback(cfg->int_pin.port, &drv_data->gpio_cb);
146 	if (ret < 0) {
147 		LOG_ERR("Failed to set gpio callback.");
148 		return ret;
149 	}
150 
151 	/* enable data ready interrupt */
152 	ret = i2c_reg_write_byte_dt(&cfg->i2c, MPU9250_REG_INT_EN,
153 				    MPU9250_DRDY_EN);
154 	if (ret < 0) {
155 		LOG_ERR("Failed to enable data ready interrupt.");
156 		return ret;
157 	}
158 
159 #if defined(CONFIG_MPU9250_TRIGGER_OWN_THREAD)
160 	ret = k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
161 	if (ret < 0) {
162 		LOG_ERR("Failed to enable semaphore");
163 		return ret;
164 	}
165 
166 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
167 			CONFIG_MPU9250_THREAD_STACK_SIZE,
168 			mpu9250_thread, drv_data,
169 			NULL, NULL, K_PRIO_COOP(CONFIG_MPU9250_THREAD_PRIORITY),
170 			0, K_NO_WAIT);
171 #elif defined(CONFIG_MPU9250_TRIGGER_GLOBAL_THREAD)
172 	drv_data->work.handler = mpu9250_work_cb;
173 #endif
174 
175 	ret = gpio_pin_interrupt_configure_dt(&cfg->int_pin,
176 					      GPIO_INT_EDGE_TO_ACTIVE);
177 	if (ret < 0) {
178 		LOG_ERR("Failed to enable interrupt");
179 		return ret;
180 	}
181 
182 	return 0;
183 }
184