1 /* ST Microelectronics IIS328DQ 3-axis accelerometer driver
2  *
3  * Copyright (c) 2020 STMicroelectronics
4  * Copyright (c) 2024 SILA Embedded Solutions GmbH
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Datasheet:
9  * https://www.st.com/resource/en/datasheet/iis328dq.pdf
10  */
11 
12 #define DT_DRV_COMPAT st_iis328dq
13 
14 #include <zephyr/kernel.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <zephyr/logging/log.h>
18 
19 #include "iis328dq.h"
20 
21 LOG_MODULE_DECLARE(IIS328DQ, CONFIG_SENSOR_LOG_LEVEL);
22 
iis328dq_set_int_pad_state(const struct device * dev,uint8_t pad,bool enable)23 static int iis328dq_set_int_pad_state(const struct device *dev, uint8_t pad, bool enable)
24 {
25 	const struct iis328dq_config *cfg = dev->config;
26 
27 	int state = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE;
28 
29 	if (pad == 1) {
30 		return gpio_pin_interrupt_configure_dt(&cfg->gpio_int1, state);
31 	} else if (pad == 2) {
32 		return gpio_pin_interrupt_configure_dt(&cfg->gpio_int2, state);
33 	} else {
34 		return -EINVAL;
35 	}
36 }
37 
38 /**
39  * iis328dq_enable_int - enable selected int pin to generate interrupt
40  */
iis328dq_enable_int(const struct device * dev,const struct sensor_trigger * trig,int enable)41 static int iis328dq_enable_int(const struct device *dev, const struct sensor_trigger *trig,
42 			       int enable)
43 {
44 	const struct iis328dq_config *cfg = dev->config;
45 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
46 
47 	switch (trig->type) {
48 	case SENSOR_TRIG_DATA_READY:
49 		if (cfg->drdy_pad == 1) {
50 			/* route DRDY to PAD1 */
51 			if (iis328dq_pin_int1_route_set(ctx, IIS328DQ_PAD1_DRDY) != 0) {
52 				return -EIO;
53 			}
54 		} else if (cfg->drdy_pad == 2) {
55 			/* route DRDY to PAD2 */
56 			if (iis328dq_pin_int2_route_set(ctx, IIS328DQ_PAD2_DRDY) != 0) {
57 				return -EIO;
58 			}
59 		} else {
60 			LOG_ERR("No interrupt pin configured for DRDY in devicetree");
61 			return -ENOTSUP;
62 		}
63 		return iis328dq_set_int_pad_state(dev, cfg->drdy_pad, enable);
64 #ifdef CONFIG_IIS328DQ_THRESHOLD
65 	case SENSOR_TRIG_THRESHOLD: {
66 		/* set up internal INT1 for lower thresholds */
67 		int1_on_th_conf_t int1_conf = {0};
68 
69 		switch (trig->chan) {
70 		case SENSOR_CHAN_ACCEL_X:
71 			int1_conf.int1_xlie = 1;
72 			break;
73 		case SENSOR_CHAN_ACCEL_Y:
74 			int1_conf.int1_ylie = 1;
75 			break;
76 		case SENSOR_CHAN_ACCEL_Z:
77 			int1_conf.int1_zlie = 1;
78 			break;
79 		case SENSOR_CHAN_ACCEL_XYZ:
80 			int1_conf.int1_xlie = 1;
81 			int1_conf.int1_ylie = 1;
82 			int1_conf.int1_zlie = 1;
83 			break;
84 		default:
85 			LOG_ERR("Invalid sensor channel %d", trig->chan);
86 			return -EINVAL;
87 		}
88 
89 		if (iis328dq_int1_on_threshold_conf_set(ctx, int1_conf) != 0) {
90 			return -EIO;
91 		}
92 
93 		/* set up internal INT2 for uppper thresholds */
94 		int2_on_th_conf_t int2_conf = {0};
95 
96 		int2_conf.int2_xhie = int1_conf.int1_xlie;
97 		int2_conf.int2_yhie = int1_conf.int1_ylie;
98 		int2_conf.int2_zhie = int1_conf.int1_zlie;
99 		if (iis328dq_int2_on_threshold_conf_set(ctx, int2_conf) != 0) {
100 			return -EIO;
101 		}
102 
103 		if (cfg->threshold_pad == 1) {
104 			/* route both internal interrupts to PAD1 */
105 			if (iis328dq_pin_int1_route_set(ctx, IIS328DQ_PAD1_INT1_OR_INT2_SRC) != 0) {
106 				return -EIO;
107 			}
108 		} else if (cfg->threshold_pad == 2) {
109 			/* route both internal interrupts to PAD2 */
110 			if (iis328dq_pin_int2_route_set(ctx, IIS328DQ_PAD2_INT1_OR_INT2_SRC) != 0) {
111 				return -EIO;
112 			}
113 		} else {
114 			LOG_ERR("No interrupt pin configured for DRDY in devicetree");
115 			return -ENOTSUP;
116 		}
117 		return iis328dq_set_int_pad_state(dev, cfg->threshold_pad, enable);
118 	}
119 #endif /* CONFIG_IIS328DQ_THRESHOLD */
120 	default:
121 		LOG_ERR("Unsupported trigger interrupt route %d", trig->type);
122 		return -ENOTSUP;
123 	}
124 
125 	return 0;
126 }
127 
128 /**
129  * iis328dq_trigger_set - link external trigger to event data ready
130  */
iis328dq_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)131 int iis328dq_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
132 			 sensor_trigger_handler_t handler)
133 {
134 	const struct iis328dq_config *cfg = dev->config;
135 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
136 	struct iis328dq_data *iis328dq = dev->data;
137 	int16_t raw[3];
138 	int state = (handler != NULL) ? PROPERTY_ENABLE : PROPERTY_DISABLE;
139 
140 	if (!cfg->gpio_int1.port && !cfg->gpio_int2.port) {
141 		/* no interrupts configured */
142 		return -ENOTSUP;
143 	}
144 
145 	switch (trig->type) {
146 	case SENSOR_TRIG_DATA_READY:
147 		iis328dq->drdy_handler = handler;
148 		iis328dq->drdy_trig = trig;
149 		if (state) {
150 			/* dummy read: re-trigger interrupt */
151 			iis328dq_acceleration_raw_get(ctx, raw);
152 		}
153 		break;
154 #ifdef CONFIG_IIS328DQ_THRESHOLD
155 	case SENSOR_TRIG_THRESHOLD:
156 		iis328dq->threshold_handler = handler;
157 		iis328dq->threshold_trig = trig;
158 		break;
159 #endif /* CONFIG_IIS328DQ_THRESHOLD */
160 	default:
161 		LOG_ERR("Unsupported sensor trigger");
162 		return -ENOTSUP;
163 	}
164 
165 	return iis328dq_enable_int(dev, trig, state);
166 }
167 
168 /**
169  * iis328dq_handle_interrupt - handle the drdy event
170  * read data and call handler if registered any
171  */
iis328dq_handle_interrupt(const struct device * dev)172 static void iis328dq_handle_interrupt(const struct device *dev)
173 {
174 	const struct iis328dq_config *cfg = dev->config;
175 	struct iis328dq_data *data = dev->data;
176 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
177 	iis328dq_status_reg_t status;
178 	iis328dq_int1_src_t sources1;
179 	iis328dq_int2_src_t sources2;
180 
181 	iis328dq_status_reg_get(ctx, &status);
182 
183 	if (status.zyxda) {
184 		if (data->drdy_handler) {
185 			data->drdy_handler(dev, data->drdy_trig);
186 		}
187 
188 		iis328dq_set_int_pad_state(dev, cfg->drdy_pad, true);
189 	}
190 #ifdef CONFIG_IIS328DQ_THRESHOLD
191 	iis328dq_int1_src_get(ctx, &sources1);
192 	iis328dq_int2_src_get(ctx, &sources2);
193 	if (sources1.ia || sources2.ia) {
194 		if (data->threshold_handler) {
195 			data->threshold_handler(dev, data->threshold_trig);
196 		}
197 
198 		iis328dq_set_int_pad_state(dev, cfg->threshold_pad, true);
199 	}
200 #endif /* CONFIG_IIS328DQ_THRESHOLD */
201 }
202 
iis328dq_int1_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)203 static void iis328dq_int1_gpio_callback(const struct device *dev, struct gpio_callback *cb,
204 					uint32_t pins)
205 {
206 	struct iis328dq_data *iis328dq = CONTAINER_OF(cb, struct iis328dq_data, int1_cb);
207 
208 	ARG_UNUSED(pins);
209 
210 	iis328dq_set_int_pad_state(iis328dq->dev, 1, true);
211 
212 #if defined(CONFIG_IIS328DQ_TRIGGER_OWN_THREAD)
213 	k_sem_give(&iis328dq->gpio_sem);
214 #elif defined(CONFIG_IIS328DQ_TRIGGER_GLOBAL_THREAD)
215 	k_work_submit(&iis328dq->work);
216 #endif /* CONFIG_IIS328DQ_TRIGGER_OWN_THREAD */
217 }
218 
iis328dq_int2_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)219 static void iis328dq_int2_gpio_callback(const struct device *dev, struct gpio_callback *cb,
220 					uint32_t pins)
221 {
222 	struct iis328dq_data *iis328dq = CONTAINER_OF(cb, struct iis328dq_data, int2_cb);
223 
224 	ARG_UNUSED(pins);
225 
226 	iis328dq_set_int_pad_state(iis328dq->dev, 2, true);
227 
228 #if defined(CONFIG_IIS328DQ_TRIGGER_OWN_THREAD)
229 	k_sem_give(&iis328dq->gpio_sem);
230 #elif defined(CONFIG_IIS328DQ_TRIGGER_GLOBAL_THREAD)
231 	k_work_submit(&iis328dq->work);
232 #endif /* CONFIG_IIS328DQ_TRIGGER_OWN_THREAD */
233 }
234 
235 #ifdef CONFIG_IIS328DQ_TRIGGER_OWN_THREAD
iis328dq_thread(void * p1,void * p2,void * p3)236 static void iis328dq_thread(void *p1, void *p2, void *p3)
237 {
238 	ARG_UNUSED(p2);
239 	ARG_UNUSED(p3);
240 
241 	struct iis328dq_data *iis328dq = p1;
242 
243 	while (1) {
244 		k_sem_take(&iis328dq->gpio_sem, K_FOREVER);
245 		iis328dq_handle_interrupt(iis328dq->dev);
246 	}
247 }
248 #endif /* CONFIG_IIS328DQ_TRIGGER_OWN_THREAD */
249 
250 #ifdef CONFIG_IIS328DQ_TRIGGER_GLOBAL_THREAD
iis328dq_work_cb(struct k_work * work)251 static void iis328dq_work_cb(struct k_work *work)
252 {
253 	struct iis328dq_data *iis328dq = CONTAINER_OF(work, struct iis328dq_data, work);
254 
255 	iis328dq_handle_interrupt(iis328dq->dev);
256 }
257 #endif /* CONFIG_IIS328DQ_TRIGGER_GLOBAL_THREAD */
258 
iis328dq_init_interrupt(const struct device * dev)259 int iis328dq_init_interrupt(const struct device *dev)
260 {
261 	struct iis328dq_data *iis328dq = dev->data;
262 	const struct iis328dq_config *cfg = dev->config;
263 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
264 	int ret;
265 
266 	if (!cfg->gpio_int1.port && !cfg->gpio_int2.port) {
267 		/* no interrupts configured, nothing to do */
268 		return 0;
269 	}
270 
271 	/* setup data ready gpio interrupt (INT1 and INT2) */
272 	if (cfg->gpio_int1.port) {
273 		if (!gpio_is_ready_dt(&cfg->gpio_int1)) {
274 			LOG_ERR("INT_1 pin is not ready");
275 			return -EINVAL;
276 		}
277 	}
278 	if (cfg->gpio_int2.port) {
279 		if (!gpio_is_ready_dt(&cfg->gpio_int2)) {
280 			LOG_ERR("INT_2 pin is not ready");
281 			return -EINVAL;
282 		}
283 	}
284 
285 #if defined(CONFIG_IIS328DQ_TRIGGER_OWN_THREAD)
286 	k_sem_init(&iis328dq->gpio_sem, 0, K_SEM_MAX_LIMIT);
287 
288 	k_thread_create(&iis328dq->thread, iis328dq->thread_stack,
289 			CONFIG_IIS328DQ_THREAD_STACK_SIZE, iis328dq_thread, iis328dq, NULL, NULL,
290 			K_PRIO_COOP(CONFIG_IIS328DQ_THREAD_PRIORITY), 0, K_NO_WAIT);
291 #elif defined(CONFIG_IIS328DQ_TRIGGER_GLOBAL_THREAD)
292 	iis328dq->work.handler = iis328dq_work_cb;
293 #endif /* CONFIG_IIS328DQ_TRIGGER_OWN_THREAD */
294 
295 	if (cfg->gpio_int1.port) {
296 		ret = gpio_pin_configure_dt(&cfg->gpio_int1, GPIO_INPUT);
297 		if (ret < 0) {
298 			LOG_ERR("Could not configure INT_1 gpio");
299 			return ret;
300 		}
301 		gpio_init_callback(&iis328dq->int1_cb, iis328dq_int1_gpio_callback,
302 				   BIT(cfg->gpio_int1.pin));
303 		if (gpio_add_callback(cfg->gpio_int1.port, &iis328dq->int1_cb) < 0) {
304 			LOG_ERR("Could not set INT1 callback");
305 			return -EIO;
306 		}
307 	}
308 
309 	if (cfg->gpio_int2.port) {
310 		ret = gpio_pin_configure_dt(&cfg->gpio_int2, GPIO_INPUT);
311 		if (ret < 0) {
312 			LOG_ERR("Could not configure INT_2 gpio");
313 			return ret;
314 		}
315 		gpio_init_callback(&iis328dq->int2_cb, iis328dq_int2_gpio_callback,
316 				   BIT(cfg->gpio_int2.pin));
317 		if (gpio_add_callback(cfg->gpio_int2.port, &iis328dq->int2_cb) < 0) {
318 			LOG_ERR("Could not set INT2 callback");
319 			return -EIO;
320 		}
321 	}
322 
323 	if (iis328dq_int1_notification_set(ctx, IIS328DQ_INT1_PULSED) != 0) {
324 		return -EIO;
325 	}
326 
327 	if (iis328dq_int2_notification_set(ctx, IIS328DQ_INT2_PULSED) != 0) {
328 		return -EIO;
329 	}
330 
331 	return 0;
332 }
333