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(struct mpu9250_data * drv_data)97 static void mpu9250_thread(struct mpu9250_data *drv_data)
98 {
99 	while (1) {
100 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
101 		mpu9250_thread_cb(drv_data->dev);
102 	}
103 }
104 #endif
105 
106 #ifdef CONFIG_MPU9250_TRIGGER_GLOBAL_THREAD
mpu9250_work_cb(struct k_work * work)107 static void mpu9250_work_cb(struct k_work *work)
108 {
109 	struct mpu9250_data *drv_data =
110 		CONTAINER_OF(work, struct mpu9250_data, work);
111 
112 	mpu9250_thread_cb(drv_data->dev);
113 }
114 #endif
115 
mpu9250_init_interrupt(const struct device * dev)116 int mpu9250_init_interrupt(const struct device *dev)
117 {
118 	struct mpu9250_data *drv_data = dev->data;
119 	const struct mpu9250_config *cfg = dev->config;
120 	int ret;
121 
122 	/* setup data ready gpio interrupt */
123 	if (!gpio_is_ready_dt(&cfg->int_pin)) {
124 		LOG_ERR("Interrupt pin is not ready.");
125 		return -EIO;
126 	}
127 
128 	drv_data->dev = dev;
129 
130 	ret = gpio_pin_configure_dt(&cfg->int_pin, GPIO_INPUT);
131 	if (ret < 0) {
132 		LOG_ERR("Failed to configure interrupt pin.");
133 		return ret;
134 	}
135 
136 	gpio_init_callback(&drv_data->gpio_cb,
137 			   mpu9250_gpio_callback,
138 			   BIT(cfg->int_pin.pin));
139 
140 	ret = gpio_add_callback(cfg->int_pin.port, &drv_data->gpio_cb);
141 	if (ret < 0) {
142 		LOG_ERR("Failed to set gpio callback.");
143 		return ret;
144 	}
145 
146 	/* enable data ready interrupt */
147 	ret = i2c_reg_write_byte_dt(&cfg->i2c, MPU9250_REG_INT_EN,
148 				    MPU9250_DRDY_EN);
149 	if (ret < 0) {
150 		LOG_ERR("Failed to enable data ready interrupt.");
151 		return ret;
152 	}
153 
154 #if defined(CONFIG_MPU9250_TRIGGER_OWN_THREAD)
155 	ret = k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
156 	if (ret < 0) {
157 		LOG_ERR("Failed to enable semaphore");
158 		return ret;
159 	}
160 
161 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
162 			CONFIG_MPU9250_THREAD_STACK_SIZE,
163 			(k_thread_entry_t)mpu9250_thread, drv_data,
164 			NULL, NULL, K_PRIO_COOP(CONFIG_MPU9250_THREAD_PRIORITY),
165 			0, K_NO_WAIT);
166 #elif defined(CONFIG_MPU9250_TRIGGER_GLOBAL_THREAD)
167 	drv_data->work.handler = mpu9250_work_cb;
168 #endif
169 
170 	ret = gpio_pin_interrupt_configure_dt(&cfg->int_pin,
171 					      GPIO_INT_EDGE_TO_ACTIVE);
172 	if (ret < 0) {
173 		LOG_ERR("Failed to enable interrupt");
174 		return ret;
175 	}
176 
177 	return 0;
178 }
179