1 /*
2  * Copyright (c) 2018 Analog Devices Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT adi_adxl345
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 #include "adxl345.h"
15 
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_DECLARE(ADXL345, CONFIG_SENSOR_LOG_LEVEL);
18 
19 #if defined(CONFIG_ADXL345_TRIGGER_OWN_THREAD) || defined(CONFIG_ADXL345_TRIGGER_GLOBAL_THREAD)
adxl345_thread_cb(const struct device * dev)20 static void adxl345_thread_cb(const struct device *dev)
21 {
22 	const struct adxl345_dev_config *cfg = dev->config;
23 	struct adxl345_dev_data *drv_data = dev->data;
24 	uint8_t status1;
25 	int ret;
26 
27 	/* Clear the status */
28 	if (adxl345_get_status(dev, &status1, NULL) < 0) {
29 		return;
30 	}
31 
32 	if ((drv_data->drdy_handler != NULL) &&
33 		ADXL345_STATUS_DATA_RDY(status1)) {
34 		drv_data->drdy_handler(dev, drv_data->drdy_trigger);
35 	}
36 
37 	ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
38 					      GPIO_INT_EDGE_TO_ACTIVE);
39 	__ASSERT(ret == 0, "Interrupt configuration failed");
40 }
41 #endif
42 
adxl345_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)43 static void adxl345_gpio_callback(const struct device *dev,
44 				  struct gpio_callback *cb, uint32_t pins)
45 {
46 	struct adxl345_dev_data *drv_data =
47 		CONTAINER_OF(cb, struct adxl345_dev_data, gpio_cb);
48 	const struct adxl345_dev_config *cfg = drv_data->dev->config;
49 
50 	gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_DISABLE);
51 
52 	if (IS_ENABLED(CONFIG_ADXL345_STREAM)) {
53 		adxl345_stream_irq_handler(drv_data->dev);
54 	}
55 
56 #if defined(CONFIG_ADXL345_TRIGGER_OWN_THREAD)
57 	k_sem_give(&drv_data->gpio_sem);
58 #elif defined(CONFIG_ADXL345_TRIGGER_GLOBAL_THREAD)
59 	k_work_submit(&drv_data->work);
60 #endif
61 }
62 
63 #if defined(CONFIG_ADXL345_TRIGGER_OWN_THREAD)
adxl345_thread(void * p1,void * p2,void * p3)64 static void adxl345_thread(void *p1, void *p2, void *p3)
65 {
66 	ARG_UNUSED(p2);
67 	ARG_UNUSED(p3);
68 
69 	struct adxl345_dev_data *drv_data = p1;
70 
71 	while (true) {
72 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
73 		adxl345_thread_cb(drv_data->dev);
74 	}
75 }
76 
77 #elif defined(CONFIG_ADXL345_TRIGGER_GLOBAL_THREAD)
adxl345_work_cb(struct k_work * work)78 static void adxl345_work_cb(struct k_work *work)
79 {
80 	struct adxl345_dev_data *drv_data =
81 		CONTAINER_OF(work, struct adxl345_dev_data, work);
82 
83 	adxl345_thread_cb(drv_data->dev);
84 }
85 #endif
86 
adxl345_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)87 int adxl345_trigger_set(const struct device *dev,
88 			const struct sensor_trigger *trig,
89 			sensor_trigger_handler_t handler)
90 {
91 	const struct adxl345_dev_config *cfg = dev->config;
92 	struct adxl345_dev_data *drv_data = dev->data;
93 	uint8_t int_mask, int_en, status1;
94 	int ret;
95 
96 	ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
97 					      GPIO_INT_DISABLE);
98 	if (ret < 0) {
99 		return ret;
100 	}
101 
102 	switch (trig->type) {
103 	case SENSOR_TRIG_DATA_READY:
104 		drv_data->drdy_handler = handler;
105 		drv_data->drdy_trigger = trig;
106 		int_mask = ADXL345_INT_MAP_DATA_RDY_MSK;
107 		break;
108 	default:
109 		LOG_ERR("Unsupported sensor trigger");
110 		return -ENOTSUP;
111 	}
112 
113 	if (handler) {
114 		int_en = int_mask;
115 	} else {
116 		int_en = 0U;
117 	}
118 
119 	ret = adxl345_reg_write_mask(dev, ADXL345_INT_MAP, int_mask, int_en);
120 	if (ret < 0) {
121 		return ret;
122 	}
123 	/* Clear status */
124 	ret = adxl345_get_status(dev, &status1, NULL);
125 	if (ret < 0) {
126 		return ret;
127 	}
128 
129 	ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
130 					      GPIO_INT_EDGE_TO_ACTIVE);
131 	if (ret < 0) {
132 		return ret;
133 	}
134 
135 	return 0;
136 }
137 
adxl345_init_interrupt(const struct device * dev)138 int adxl345_init_interrupt(const struct device *dev)
139 {
140 	const struct adxl345_dev_config *cfg = dev->config;
141 	struct adxl345_dev_data *drv_data = dev->data;
142 	int ret;
143 
144 	if (!gpio_is_ready_dt(&cfg->interrupt)) {
145 		LOG_ERR("GPIO port %s not ready", cfg->interrupt.port->name);
146 		return -EINVAL;
147 	}
148 
149 	ret = gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
150 	if (ret < 0) {
151 		return ret;
152 	}
153 
154 	gpio_init_callback(&drv_data->gpio_cb,
155 			   adxl345_gpio_callback,
156 			   BIT(cfg->interrupt.pin));
157 
158 	ret = gpio_add_callback(cfg->interrupt.port, &drv_data->gpio_cb);
159 	if (ret < 0) {
160 		LOG_ERR("Failed to set gpio callback!");
161 		return ret;
162 	}
163 
164 	drv_data->dev = dev;
165 
166 #if defined(CONFIG_ADXL345_TRIGGER_OWN_THREAD)
167 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
168 
169 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
170 			CONFIG_ADXL345_THREAD_STACK_SIZE,
171 			adxl345_thread, drv_data,
172 			NULL, NULL, K_PRIO_COOP(CONFIG_ADXL345_THREAD_PRIORITY),
173 			0, K_NO_WAIT);
174 #elif defined(CONFIG_ADXL345_TRIGGER_GLOBAL_THREAD)
175 	drv_data->work.handler = adxl345_work_cb;
176 #endif
177 
178 	return 0;
179 }
180