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