1 /*
2  * Copyright (c) 2018 Analog Devices Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT adi_adxl372
8 
9 #include <device.h>
10 #include <drivers/gpio.h>
11 #include <sys/util.h>
12 #include <kernel.h>
13 #include <drivers/sensor.h>
14 #include "adxl372.h"
15 
16 #include <logging/log.h>
17 LOG_MODULE_DECLARE(ADXL372, CONFIG_SENSOR_LOG_LEVEL);
18 
adxl372_thread_cb(const struct device * dev)19 static void adxl372_thread_cb(const struct device *dev)
20 {
21 	const struct adxl372_dev_config *cfg = dev->config;
22 	struct adxl372_data *drv_data = dev->data;
23 	uint8_t status1, status2;
24 	int ret;
25 
26 	/* Clear the status */
27 	if (adxl372_get_status(dev, &status1, &status2, NULL) < 0) {
28 		return;
29 	}
30 
31 	if (drv_data->th_handler != NULL) {
32 		/* In max peak mode we wait until we settle below the inactivity
33 		 * threshold and then call the trigger handler.
34 		 */
35 		if (cfg->max_peak_detect_mode &&
36 			ADXL372_STATUS_2_INACT(status2)) {
37 			drv_data->th_handler(dev, &drv_data->th_trigger);
38 		} else if (!cfg->max_peak_detect_mode &&
39 			(ADXL372_STATUS_2_INACT(status2) ||
40 			ADXL372_STATUS_2_ACTIVITY(status2))) {
41 			drv_data->th_handler(dev, &drv_data->th_trigger);
42 		}
43 	}
44 
45 	if ((drv_data->drdy_handler != NULL) &&
46 		ADXL372_STATUS_1_DATA_RDY(status1)) {
47 		drv_data->drdy_handler(dev, &drv_data->drdy_trigger);
48 	}
49 
50 	ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
51 					      GPIO_INT_EDGE_TO_ACTIVE);
52 	__ASSERT(ret == 0, "Interrupt configuration failed");
53 }
54 
adxl372_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)55 static void adxl372_gpio_callback(const struct device *dev,
56 				  struct gpio_callback *cb, uint32_t pins)
57 {
58 	struct adxl372_data *drv_data =
59 		CONTAINER_OF(cb, struct adxl372_data, gpio_cb);
60 	const struct adxl372_dev_config *cfg = drv_data->dev->config;
61 
62 	gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_DISABLE);
63 
64 #if defined(CONFIG_ADXL372_TRIGGER_OWN_THREAD)
65 	k_sem_give(&drv_data->gpio_sem);
66 #elif defined(CONFIG_ADXL372_TRIGGER_GLOBAL_THREAD)
67 	k_work_submit(&drv_data->work);
68 #endif
69 }
70 
71 #if defined(CONFIG_ADXL372_TRIGGER_OWN_THREAD)
adxl372_thread(struct adxl372_data * drv_data)72 static void adxl372_thread(struct adxl372_data *drv_data)
73 {
74 	while (true) {
75 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
76 		adxl372_thread_cb(drv_data->dev);
77 	}
78 }
79 
80 #elif defined(CONFIG_ADXL372_TRIGGER_GLOBAL_THREAD)
adxl372_work_cb(struct k_work * work)81 static void adxl372_work_cb(struct k_work *work)
82 {
83 	struct adxl372_data *drv_data =
84 		CONTAINER_OF(work, struct adxl372_data, work);
85 
86 	adxl372_thread_cb(drv_data->dev);
87 }
88 #endif
89 
adxl372_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)90 int adxl372_trigger_set(const struct device *dev,
91 			const struct sensor_trigger *trig,
92 			sensor_trigger_handler_t handler)
93 {
94 	const struct adxl372_dev_config *cfg = dev->config;
95 	struct adxl372_data *drv_data = dev->data;
96 	uint8_t int_mask, int_en, status1, status2;
97 	int ret;
98 
99 	ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
100 					      GPIO_INT_DISABLE);
101 	if (ret < 0) {
102 		return ret;
103 	}
104 
105 	switch (trig->type) {
106 	case SENSOR_TRIG_THRESHOLD:
107 		drv_data->th_handler = handler;
108 		drv_data->th_trigger = *trig;
109 		int_mask = ADXL372_INT1_MAP_ACT_MSK |
110 			   ADXL372_INT1_MAP_INACT_MSK;
111 		break;
112 	case SENSOR_TRIG_DATA_READY:
113 		drv_data->drdy_handler = handler;
114 		drv_data->drdy_trigger = *trig;
115 		int_mask = ADXL372_INT1_MAP_DATA_RDY_MSK;
116 		break;
117 	default:
118 		LOG_ERR("Unsupported sensor trigger");
119 		return -ENOTSUP;
120 	}
121 
122 	if (handler) {
123 		int_en = int_mask;
124 	} else {
125 		int_en = 0U;
126 	}
127 
128 	ret = adxl372_reg_write_mask(dev, ADXL372_INT1_MAP, int_mask, int_en);
129 	if (ret < 0) {
130 		return ret;
131 	}
132 
133 	ret = adxl372_get_status(dev, &status1, &status2, NULL); /* Clear status */
134 	if (ret < 0) {
135 		return ret;
136 	}
137 
138 	ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
139 					      GPIO_INT_EDGE_TO_ACTIVE);
140 	if (ret < 0) {
141 		return ret;
142 	}
143 
144 	return 0;
145 }
146 
adxl372_init_interrupt(const struct device * dev)147 int adxl372_init_interrupt(const struct device *dev)
148 {
149 	const struct adxl372_dev_config *cfg = dev->config;
150 	struct adxl372_data *drv_data = dev->data;
151 	int ret;
152 
153 	if (!device_is_ready(cfg->interrupt.port)) {
154 		LOG_ERR("GPIO port %s not ready", cfg->interrupt.port->name);
155 		return -EINVAL;
156 	}
157 
158 	ret = gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
159 	if (ret < 0) {
160 		return ret;
161 	}
162 
163 	gpio_init_callback(&drv_data->gpio_cb,
164 			   adxl372_gpio_callback,
165 			   BIT(cfg->interrupt.pin));
166 
167 	ret = gpio_add_callback(cfg->interrupt.port, &drv_data->gpio_cb);
168 	if (ret < 0) {
169 		LOG_ERR("Failed to set gpio callback!");
170 		return ret;
171 	}
172 
173 	drv_data->dev = dev;
174 
175 #if defined(CONFIG_ADXL372_TRIGGER_OWN_THREAD)
176 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
177 
178 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
179 			CONFIG_ADXL372_THREAD_STACK_SIZE,
180 			(k_thread_entry_t)adxl372_thread, drv_data,
181 			NULL, NULL, K_PRIO_COOP(CONFIG_ADXL372_THREAD_PRIORITY),
182 			0, K_NO_WAIT);
183 #elif defined(CONFIG_ADXL372_TRIGGER_GLOBAL_THREAD)
184 	drv_data->work.handler = adxl372_work_cb;
185 #endif
186 
187 	return 0;
188 }
189