1 /*
2 * Copyright (c) 2016 TDK Invensense
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 "icm42605.h"
14 #include "icm42605_setup.h"
15
16 LOG_MODULE_DECLARE(ICM42605, CONFIG_SENSOR_LOG_LEVEL);
17
icm42605_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)18 int icm42605_trigger_set(const struct device *dev,
19 const struct sensor_trigger *trig,
20 sensor_trigger_handler_t handler)
21 {
22 struct icm42605_data *drv_data = dev->data;
23 const struct icm42605_config *cfg = dev->config;
24
25 if (trig->type != SENSOR_TRIG_DATA_READY
26 && trig->type != SENSOR_TRIG_TAP
27 && trig->type != SENSOR_TRIG_DOUBLE_TAP) {
28 return -ENOTSUP;
29 }
30
31 gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
32
33 if (handler == NULL) {
34 icm42605_turn_off_sensor(dev);
35 return 0;
36 }
37
38 if (trig->type == SENSOR_TRIG_DATA_READY) {
39 drv_data->data_ready_handler = handler;
40 drv_data->data_ready_trigger = trig;
41 } else if (trig->type == SENSOR_TRIG_TAP) {
42 drv_data->tap_handler = handler;
43 drv_data->tap_trigger = trig;
44 drv_data->tap_en = true;
45 } else if (trig->type == SENSOR_TRIG_DOUBLE_TAP) {
46 drv_data->double_tap_handler = handler;
47 drv_data->double_tap_trigger = trig;
48 drv_data->tap_en = true;
49 } else {
50 return -ENOTSUP;
51 }
52
53 gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_EDGE_TO_ACTIVE);
54
55 icm42605_turn_on_sensor(dev);
56
57 return 0;
58 }
59
icm42605_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)60 static void icm42605_gpio_callback(const struct device *dev,
61 struct gpio_callback *cb, uint32_t pins)
62 {
63 struct icm42605_data *drv_data =
64 CONTAINER_OF(cb, struct icm42605_data, gpio_cb);
65 const struct icm42605_config *cfg = drv_data->dev->config;
66
67 ARG_UNUSED(pins);
68
69 gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
70
71 k_sem_give(&drv_data->gpio_sem);
72 }
73
icm42605_thread_cb(const struct device * dev)74 static void icm42605_thread_cb(const struct device *dev)
75 {
76 struct icm42605_data *drv_data = dev->data;
77 const struct icm42605_config *cfg = dev->config;
78
79 if (drv_data->data_ready_handler != NULL) {
80 drv_data->data_ready_handler(dev,
81 drv_data->data_ready_trigger);
82 }
83
84 if (drv_data->tap_handler != NULL ||
85 drv_data->double_tap_handler != NULL) {
86 icm42605_tap_fetch(dev);
87 }
88
89 gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_EDGE_TO_ACTIVE);
90 }
91
icm42605_thread(void * p1,void * p2,void * p3)92 static void icm42605_thread(void *p1, void *p2, void *p3)
93 {
94 ARG_UNUSED(p2);
95 ARG_UNUSED(p3);
96
97 struct icm42605_data *drv_data = p1;
98
99 while (1) {
100 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
101 icm42605_thread_cb(drv_data->dev);
102 }
103 }
104
icm42605_init_interrupt(const struct device * dev)105 int icm42605_init_interrupt(const struct device *dev)
106 {
107 struct icm42605_data *drv_data = dev->data;
108 const struct icm42605_config *cfg = dev->config;
109 int result = 0;
110
111 if (!gpio_is_ready_dt(&cfg->gpio_int)) {
112 LOG_ERR("gpio_int gpio not ready");
113 return -ENODEV;
114 }
115
116 drv_data->dev = dev;
117
118 gpio_pin_configure_dt(&cfg->gpio_int, GPIO_INPUT);
119 gpio_init_callback(&drv_data->gpio_cb, icm42605_gpio_callback, BIT(cfg->gpio_int.pin));
120 result = gpio_add_callback(cfg->gpio_int.port, &drv_data->gpio_cb);
121
122 if (result < 0) {
123 LOG_ERR("Failed to set gpio callback");
124 return result;
125 }
126
127 k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
128
129 k_thread_create(&drv_data->thread, drv_data->thread_stack,
130 CONFIG_ICM42605_THREAD_STACK_SIZE, icm42605_thread, drv_data, NULL, NULL,
131 K_PRIO_COOP(CONFIG_ICM42605_THREAD_PRIORITY), 0, K_NO_WAIT);
132
133 gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_EDGE_TO_INACTIVE);
134
135 return 0;
136 }
137