1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/i2c.h>
9 #include <zephyr/sys/util.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/drivers/sensor.h>
12 #include <zephyr/logging/log.h>
13 #include "mpu6050.h"
14 
15 LOG_MODULE_DECLARE(MPU6050, CONFIG_SENSOR_LOG_LEVEL);
16 
mpu6050_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)17 int mpu6050_trigger_set(const struct device *dev,
18 			const struct sensor_trigger *trig,
19 			sensor_trigger_handler_t handler)
20 {
21 	struct mpu6050_data *drv_data = dev->data;
22 	const struct mpu6050_config *cfg = dev->config;
23 
24 	if (!cfg->int_gpio.port) {
25 		return -ENOTSUP;
26 	}
27 
28 	if (trig->type != SENSOR_TRIG_DATA_READY) {
29 		return -ENOTSUP;
30 	}
31 
32 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
33 
34 	drv_data->data_ready_handler = handler;
35 	if (handler == NULL) {
36 		return 0;
37 	}
38 
39 	drv_data->data_ready_trigger = trig;
40 
41 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio,
42 					GPIO_INT_EDGE_TO_ACTIVE);
43 
44 	return 0;
45 }
46 
mpu6050_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)47 static void mpu6050_gpio_callback(const struct device *dev,
48 				  struct gpio_callback *cb, uint32_t pins)
49 {
50 	struct mpu6050_data *drv_data =
51 		CONTAINER_OF(cb, struct mpu6050_data, gpio_cb);
52 	const struct mpu6050_config *cfg = drv_data->dev->config;
53 
54 	ARG_UNUSED(pins);
55 
56 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
57 
58 #if defined(CONFIG_MPU6050_TRIGGER_OWN_THREAD)
59 	k_sem_give(&drv_data->gpio_sem);
60 #elif defined(CONFIG_MPU6050_TRIGGER_GLOBAL_THREAD)
61 	k_work_submit(&drv_data->work);
62 #endif
63 }
64 
mpu6050_thread_cb(const struct device * dev)65 static void mpu6050_thread_cb(const struct device *dev)
66 {
67 	struct mpu6050_data *drv_data = dev->data;
68 	const struct mpu6050_config *cfg = dev->config;
69 
70 	if (drv_data->data_ready_handler != NULL) {
71 		drv_data->data_ready_handler(dev,
72 					     drv_data->data_ready_trigger);
73 	}
74 
75 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio,
76 					GPIO_INT_EDGE_TO_ACTIVE);
77 }
78 
79 #ifdef CONFIG_MPU6050_TRIGGER_OWN_THREAD
mpu6050_thread(void * p1,void * p2,void * p3)80 static void mpu6050_thread(void *p1, void *p2, void *p3)
81 {
82 	ARG_UNUSED(p2);
83 	ARG_UNUSED(p3);
84 
85 	struct mpu6050_data *drv_data = p1;
86 
87 	while (1) {
88 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
89 		mpu6050_thread_cb(drv_data->dev);
90 	}
91 }
92 #endif
93 
94 #ifdef CONFIG_MPU6050_TRIGGER_GLOBAL_THREAD
mpu6050_work_cb(struct k_work * work)95 static void mpu6050_work_cb(struct k_work *work)
96 {
97 	struct mpu6050_data *drv_data =
98 		CONTAINER_OF(work, struct mpu6050_data, work);
99 
100 	mpu6050_thread_cb(drv_data->dev);
101 }
102 #endif
103 
mpu6050_init_interrupt(const struct device * dev)104 int mpu6050_init_interrupt(const struct device *dev)
105 {
106 	struct mpu6050_data *drv_data = dev->data;
107 	const struct mpu6050_config *cfg = dev->config;
108 
109 	if (!gpio_is_ready_dt(&cfg->int_gpio)) {
110 		LOG_ERR("GPIO device not ready");
111 		return -ENODEV;
112 	}
113 
114 	drv_data->dev = dev;
115 
116 	gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
117 
118 	gpio_init_callback(&drv_data->gpio_cb,
119 			   mpu6050_gpio_callback,
120 			   BIT(cfg->int_gpio.pin));
121 
122 	if (gpio_add_callback(cfg->int_gpio.port, &drv_data->gpio_cb) < 0) {
123 		LOG_ERR("Failed to set gpio callback");
124 		return -EIO;
125 	}
126 
127 	/* enable data ready interrupt */
128 	if (i2c_reg_write_byte_dt(&cfg->i2c, MPU6050_REG_INT_EN,
129 				  MPU6050_DRDY_EN) < 0) {
130 		LOG_ERR("Failed to enable data ready interrupt.");
131 		return -EIO;
132 	}
133 
134 #if defined(CONFIG_MPU6050_TRIGGER_OWN_THREAD)
135 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
136 
137 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
138 			CONFIG_MPU6050_THREAD_STACK_SIZE,
139 			mpu6050_thread, drv_data,
140 			NULL, NULL, K_PRIO_COOP(CONFIG_MPU6050_THREAD_PRIORITY),
141 			0, K_NO_WAIT);
142 #elif defined(CONFIG_MPU6050_TRIGGER_GLOBAL_THREAD)
143 	drv_data->work.handler = mpu6050_work_cb;
144 #endif
145 
146 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio,
147 					GPIO_INT_EDGE_TO_ACTIVE);
148 
149 	return 0;
150 }
151