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(struct mpu6050_data * drv_data)80 static void mpu6050_thread(struct mpu6050_data *drv_data)
81 {
82 while (1) {
83 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
84 mpu6050_thread_cb(drv_data->dev);
85 }
86 }
87 #endif
88
89 #ifdef CONFIG_MPU6050_TRIGGER_GLOBAL_THREAD
mpu6050_work_cb(struct k_work * work)90 static void mpu6050_work_cb(struct k_work *work)
91 {
92 struct mpu6050_data *drv_data =
93 CONTAINER_OF(work, struct mpu6050_data, work);
94
95 mpu6050_thread_cb(drv_data->dev);
96 }
97 #endif
98
mpu6050_init_interrupt(const struct device * dev)99 int mpu6050_init_interrupt(const struct device *dev)
100 {
101 struct mpu6050_data *drv_data = dev->data;
102 const struct mpu6050_config *cfg = dev->config;
103
104 if (!gpio_is_ready_dt(&cfg->int_gpio)) {
105 LOG_ERR("GPIO device not ready");
106 return -ENODEV;
107 }
108
109 drv_data->dev = dev;
110
111 gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
112
113 gpio_init_callback(&drv_data->gpio_cb,
114 mpu6050_gpio_callback,
115 BIT(cfg->int_gpio.pin));
116
117 if (gpio_add_callback(cfg->int_gpio.port, &drv_data->gpio_cb) < 0) {
118 LOG_ERR("Failed to set gpio callback");
119 return -EIO;
120 }
121
122 /* enable data ready interrupt */
123 if (i2c_reg_write_byte_dt(&cfg->i2c, MPU6050_REG_INT_EN,
124 MPU6050_DRDY_EN) < 0) {
125 LOG_ERR("Failed to enable data ready interrupt.");
126 return -EIO;
127 }
128
129 #if defined(CONFIG_MPU6050_TRIGGER_OWN_THREAD)
130 k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
131
132 k_thread_create(&drv_data->thread, drv_data->thread_stack,
133 CONFIG_MPU6050_THREAD_STACK_SIZE,
134 (k_thread_entry_t)mpu6050_thread, drv_data,
135 NULL, NULL, K_PRIO_COOP(CONFIG_MPU6050_THREAD_PRIORITY),
136 0, K_NO_WAIT);
137 #elif defined(CONFIG_MPU6050_TRIGGER_GLOBAL_THREAD)
138 drv_data->work.handler = mpu6050_work_cb;
139 #endif
140
141 gpio_pin_interrupt_configure_dt(&cfg->int_gpio,
142 GPIO_INT_EDGE_TO_ACTIVE);
143
144 return 0;
145 }
146