1 /*
2 * Copyright (c) 2019 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT adi_adxl362
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/drivers/sensor.h>
14
15 #include "adxl362.h"
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_DECLARE(ADXL362, CONFIG_SENSOR_LOG_LEVEL);
19
20 #if defined(CONFIG_ADXL362_TRIGGER_OWN_THREAD) || defined(CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD)
adxl362_thread_cb(const struct device * dev)21 static void adxl362_thread_cb(const struct device *dev)
22 {
23 struct adxl362_data *drv_data = dev->data;
24 uint8_t status_buf;
25
26 /* Clears activity and inactivity interrupt */
27 if (adxl362_get_status(dev, &status_buf)) {
28 LOG_ERR("Unable to get status.");
29 return;
30 }
31
32 k_mutex_lock(&drv_data->trigger_mutex, K_FOREVER);
33 if (drv_data->inact_handler != NULL) {
34 if (ADXL362_STATUS_CHECK_INACT(status_buf)) {
35 drv_data->inact_handler(dev, drv_data->inact_trigger);
36 }
37 }
38
39 if (drv_data->act_handler != NULL) {
40 if (ADXL362_STATUS_CHECK_ACTIVITY(status_buf)) {
41 drv_data->act_handler(dev, drv_data->act_trigger);
42 }
43 }
44
45 if (drv_data->drdy_handler != NULL &&
46 ADXL362_STATUS_CHECK_DATA_READY(status_buf)) {
47 drv_data->drdy_handler(dev, drv_data->drdy_trigger);
48 }
49 k_mutex_unlock(&drv_data->trigger_mutex);
50 }
51 #endif /* CONFIG_ADXL362_TRIGGER_OWN_THREAD || CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD */
52
adxl362_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)53 static void adxl362_gpio_callback(const struct device *dev,
54 struct gpio_callback *cb, uint32_t pins)
55 {
56 struct adxl362_data *drv_data =
57 CONTAINER_OF(cb, struct adxl362_data, gpio_cb);
58
59 if (IS_ENABLED(CONFIG_ADXL362_STREAM)) {
60 adxl362_stream_irq_handler(drv_data->dev);
61 }
62
63 #if defined(CONFIG_ADXL362_TRIGGER_OWN_THREAD)
64 k_sem_give(&drv_data->gpio_sem);
65 #elif defined(CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD)
66 k_work_submit(&drv_data->work);
67 #endif
68 }
69
70 #if defined(CONFIG_ADXL362_TRIGGER_OWN_THREAD)
adxl362_thread(void * p1,void * p2,void * p3)71 static void adxl362_thread(void *p1, void *p2, void *p3)
72 {
73 ARG_UNUSED(p2);
74 ARG_UNUSED(p3);
75
76 struct adxl362_data *drv_data = p1;
77
78 while (true) {
79 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
80 adxl362_thread_cb(drv_data->dev);
81 }
82 }
83 #elif defined(CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD)
adxl362_work_cb(struct k_work * work)84 static void adxl362_work_cb(struct k_work *work)
85 {
86 struct adxl362_data *drv_data =
87 CONTAINER_OF(work, struct adxl362_data, work);
88
89 adxl362_thread_cb(drv_data->dev);
90 }
91 #endif
92
adxl362_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)93 int adxl362_trigger_set(const struct device *dev,
94 const struct sensor_trigger *trig,
95 sensor_trigger_handler_t handler)
96 {
97 struct adxl362_data *drv_data = dev->data;
98 const struct adxl362_config *config = dev->config;
99 uint8_t int_mask, int_en, status_buf;
100
101 if (!config->interrupt.port) {
102 return -ENOTSUP;
103 }
104
105 switch (trig->type) {
106 case SENSOR_TRIG_MOTION:
107 k_mutex_lock(&drv_data->trigger_mutex, K_FOREVER);
108 drv_data->act_handler = handler;
109 drv_data->act_trigger = trig;
110 k_mutex_unlock(&drv_data->trigger_mutex);
111 int_mask = ADXL362_INTMAP1_ACT;
112 /* Clear activity and inactivity interrupts */
113 adxl362_get_status(dev, &status_buf);
114 break;
115 case SENSOR_TRIG_STATIONARY:
116 k_mutex_lock(&drv_data->trigger_mutex, K_FOREVER);
117 drv_data->inact_handler = handler;
118 drv_data->inact_trigger = trig;
119 k_mutex_unlock(&drv_data->trigger_mutex);
120 int_mask = ADXL362_INTMAP1_INACT;
121 /* Clear activity and inactivity interrupts */
122 adxl362_get_status(dev, &status_buf);
123 break;
124 case SENSOR_TRIG_DATA_READY:
125 k_mutex_lock(&drv_data->trigger_mutex, K_FOREVER);
126 drv_data->drdy_handler = handler;
127 drv_data->drdy_trigger = trig;
128 k_mutex_unlock(&drv_data->trigger_mutex);
129 int_mask = ADXL362_INTMAP1_DATA_READY;
130 adxl362_clear_data_ready(dev);
131 break;
132 default:
133 LOG_ERR("Unsupported sensor trigger");
134 return -ENOTSUP;
135 }
136
137 if (handler) {
138 int_en = int_mask;
139 } else {
140 int_en = 0U;
141 }
142
143 return adxl362_reg_write_mask(dev, ADXL362_REG_INTMAP1, int_mask, int_en);
144 }
145
adxl362_init_interrupt(const struct device * dev)146 int adxl362_init_interrupt(const struct device *dev)
147 {
148 const struct adxl362_config *cfg = dev->config;
149 struct adxl362_data *drv_data = dev->data;
150 int ret;
151
152 k_mutex_init(&drv_data->trigger_mutex);
153
154 if (!gpio_is_ready_dt(&cfg->interrupt)) {
155 LOG_ERR("GPIO port %s not ready", cfg->interrupt.port->name);
156 return -ENODEV;
157 }
158
159 ret = adxl362_set_interrupt_mode(dev, CONFIG_ADXL362_INTERRUPT_MODE);
160 if (ret < 0) {
161 return ret;
162 }
163
164 ret = gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
165 if (ret < 0) {
166 return ret;
167 }
168
169 gpio_init_callback(&drv_data->gpio_cb,
170 adxl362_gpio_callback,
171 BIT(cfg->interrupt.pin));
172
173 ret = gpio_add_callback(cfg->interrupt.port, &drv_data->gpio_cb);
174 if (ret < 0) {
175 return ret;
176 }
177
178 drv_data->dev = dev;
179
180 #if defined(CONFIG_ADXL362_TRIGGER_OWN_THREAD)
181 k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
182
183 k_thread_create(&drv_data->thread, drv_data->thread_stack,
184 CONFIG_ADXL362_THREAD_STACK_SIZE,
185 adxl362_thread, drv_data,
186 NULL, NULL, K_PRIO_COOP(CONFIG_ADXL362_THREAD_PRIORITY),
187 0, K_NO_WAIT);
188
189 k_thread_name_set(&drv_data->thread, dev->name);
190 #elif defined(CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD)
191 drv_data->work.handler = adxl362_work_cb;
192 #endif
193
194 ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
195 GPIO_INT_EDGE_TO_ACTIVE);
196 if (ret < 0) {
197 return ret;
198 }
199
200 return 0;
201 }
202