1 /* Bosch BMI160 inertial measurement unit driver, trigger implementation
2  *
3  * Copyright (c) 2016 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/kernel.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/drivers/gpio.h>
11 
12 #include "bmi160.h"
13 
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_DECLARE(BMI160, CONFIG_SENSOR_LOG_LEVEL);
16 
bmi160_handle_anymotion(const struct device * dev)17 static void bmi160_handle_anymotion(const struct device *dev)
18 {
19 	struct bmi160_data *data = dev->data;
20 
21 	if (data->handler_anymotion) {
22 		data->handler_anymotion(dev, data->trig_anymotion);
23 	}
24 }
25 
bmi160_handle_drdy(const struct device * dev,uint8_t status)26 static void bmi160_handle_drdy(const struct device *dev, uint8_t status)
27 {
28 	struct bmi160_data *data = dev->data;
29 
30 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
31 	if (data->handler_drdy_acc && (status & BMI160_STATUS_ACC_DRDY)) {
32 		data->handler_drdy_acc(dev, data->trig_drdy_acc);
33 	}
34 #endif
35 
36 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
37 	if (data->handler_drdy_gyr && (status & BMI160_STATUS_GYR_DRDY)) {
38 		data->handler_drdy_gyr(dev, data->trig_drdy_gyr);
39 	}
40 #endif
41 }
42 
bmi160_handle_interrupts(const struct device * dev)43 static void bmi160_handle_interrupts(const struct device *dev)
44 {
45 	union {
46 		uint8_t raw[5];
47 		struct {
48 			uint8_t status;
49 			uint8_t int_status[4];
50 		};
51 	} buf;
52 
53 	if (bmi160_read(dev, BMI160_REG_STATUS, buf.raw, sizeof(buf)) < 0) {
54 		return;
55 	}
56 
57 	if ((buf.int_status[0] & BMI160_INT_STATUS0_ANYM) &&
58 	    (buf.int_status[2] & (BMI160_INT_STATUS2_ANYM_FIRST_X |
59 				  BMI160_INT_STATUS2_ANYM_FIRST_Y |
60 				  BMI160_INT_STATUS2_ANYM_FIRST_Z))) {
61 		bmi160_handle_anymotion(dev);
62 	}
63 
64 	if (buf.int_status[1] & BMI160_INT_STATUS1_DRDY) {
65 		bmi160_handle_drdy(dev, buf.status);
66 	}
67 
68 }
69 
70 #ifdef CONFIG_BMI160_TRIGGER_OWN_THREAD
71 static K_KERNEL_STACK_DEFINE(bmi160_thread_stack, CONFIG_BMI160_THREAD_STACK_SIZE);
72 static struct k_thread bmi160_thread;
73 
bmi160_thread_main(void * p1,void * p2,void * p3)74 static void bmi160_thread_main(void *p1, void *p2, void *p3)
75 {
76 	struct bmi160_data *data = p1;
77 
78 	while (1) {
79 		k_sem_take(&data->sem, K_FOREVER);
80 		bmi160_handle_interrupts(data->dev);
81 	}
82 }
83 #endif
84 
85 #ifdef CONFIG_BMI160_TRIGGER_GLOBAL_THREAD
bmi160_work_handler(struct k_work * work)86 static void bmi160_work_handler(struct k_work *work)
87 {
88 	struct bmi160_data *data = CONTAINER_OF(work, struct bmi160_data, work);
89 
90 	bmi160_handle_interrupts(data->dev);
91 }
92 #endif
93 
94 extern struct bmi160_data bmi160_data;
95 
bmi160_gpio_callback(const struct device * port,struct gpio_callback * cb,uint32_t pin)96 static void bmi160_gpio_callback(const struct device *port,
97 				 struct gpio_callback *cb, uint32_t pin)
98 {
99 	struct bmi160_data *data =
100 		CONTAINER_OF(cb, struct bmi160_data, gpio_cb);
101 
102 	ARG_UNUSED(port);
103 	ARG_UNUSED(pin);
104 
105 #if defined(CONFIG_BMI160_TRIGGER_OWN_THREAD)
106 	k_sem_give(&data->sem);
107 #elif defined(CONFIG_BMI160_TRIGGER_GLOBAL_THREAD)
108 	k_work_submit(&data->work);
109 #endif
110 }
111 
bmi160_trigger_drdy_set(const struct device * dev,enum sensor_channel chan,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)112 static int bmi160_trigger_drdy_set(const struct device *dev,
113 				   enum sensor_channel chan,
114 				   const struct sensor_trigger *trig,
115 				   sensor_trigger_handler_t handler)
116 {
117 	struct bmi160_data *data = dev->data;
118 	uint8_t drdy_en = 0U;
119 
120 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
121 	if (chan == SENSOR_CHAN_ACCEL_XYZ) {
122 		data->handler_drdy_acc = handler;
123 		data->trig_drdy_acc = trig;
124 	}
125 
126 	if (data->handler_drdy_acc) {
127 		drdy_en = BMI160_INT_DRDY_EN;
128 	}
129 #endif
130 
131 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
132 	if (chan == SENSOR_CHAN_GYRO_XYZ) {
133 		data->handler_drdy_gyr = handler;
134 		data->trig_drdy_gyr = trig;
135 	}
136 
137 	if (data->handler_drdy_gyr) {
138 		drdy_en = BMI160_INT_DRDY_EN;
139 	}
140 #endif
141 
142 	if (bmi160_reg_update(dev, BMI160_REG_INT_EN1,
143 			      BMI160_INT_DRDY_EN, drdy_en) < 0) {
144 		return -EIO;
145 	}
146 
147 	return 0;
148 }
149 
150 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
bmi160_trigger_anym_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)151 static int bmi160_trigger_anym_set(const struct device *dev,
152 				   const struct sensor_trigger *trig,
153 				   sensor_trigger_handler_t handler)
154 {
155 	struct bmi160_data *data = dev->data;
156 	uint8_t anym_en = 0U;
157 
158 	data->handler_anymotion = handler;
159 	data->trig_anymotion = trig;
160 
161 	if (handler) {
162 		anym_en = BMI160_INT_ANYM_X_EN |
163 			  BMI160_INT_ANYM_Y_EN |
164 			  BMI160_INT_ANYM_Z_EN;
165 	}
166 
167 	if (bmi160_reg_update(dev, BMI160_REG_INT_EN0,
168 			      BMI160_INT_ANYM_MASK, anym_en) < 0) {
169 		return -EIO;
170 	}
171 
172 	return 0;
173 }
174 
bmi160_trigger_set_acc(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)175 static int bmi160_trigger_set_acc(const struct device *dev,
176 				  const struct sensor_trigger *trig,
177 				  sensor_trigger_handler_t handler)
178 {
179 	if (trig->type == SENSOR_TRIG_DATA_READY) {
180 		return bmi160_trigger_drdy_set(dev, trig->chan, trig, handler);
181 	} else if (trig->type == SENSOR_TRIG_DELTA) {
182 		return bmi160_trigger_anym_set(dev, trig, handler);
183 	}
184 
185 	return -ENOTSUP;
186 }
187 
bmi160_acc_slope_config(const struct device * dev,enum sensor_attribute attr,const struct sensor_value * val)188 int bmi160_acc_slope_config(const struct device *dev,
189 			    enum sensor_attribute attr,
190 			    const struct sensor_value *val)
191 {
192 	uint8_t acc_range_g, reg_val;
193 	uint32_t slope_th_ums2;
194 
195 	if (attr == SENSOR_ATTR_SLOPE_TH) {
196 		if (bmi160_byte_read(dev, BMI160_REG_ACC_RANGE, &reg_val) < 0) {
197 			return -EIO;
198 		}
199 
200 		acc_range_g = bmi160_acc_reg_val_to_range(reg_val);
201 
202 		slope_th_ums2 = val->val1 * 1000000 + val->val2;
203 
204 		/* make sure the provided threshold does not exceed range / 2 */
205 		if (slope_th_ums2 > (acc_range_g / 2 * SENSOR_G)) {
206 			return -EINVAL;
207 		}
208 
209 		reg_val = (slope_th_ums2 - 1) * 512U / (acc_range_g * SENSOR_G);
210 
211 		if (bmi160_byte_write(dev, BMI160_REG_INT_MOTION1,
212 				      reg_val) < 0) {
213 			return -EIO;
214 		}
215 	} else { /* SENSOR_ATTR_SLOPE_DUR */
216 		/* slope duration is measured in number of samples */
217 		if (val->val1 < 1 || val->val1 > 4) {
218 			return -ENOTSUP;
219 		}
220 
221 		if (bmi160_reg_field_update(dev, BMI160_REG_INT_MOTION0,
222 					    BMI160_ANYM_DUR_POS,
223 					    BMI160_ANYM_DUR_MASK,
224 					    val->val1) < 0) {
225 			return -EIO;
226 		}
227 	}
228 
229 	return 0;
230 }
231 #endif
232 
233 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
bmi160_trigger_set_gyr(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)234 static int bmi160_trigger_set_gyr(const struct device *dev,
235 				  const struct sensor_trigger *trig,
236 				  sensor_trigger_handler_t handler)
237 {
238 	if (trig->type == SENSOR_TRIG_DATA_READY) {
239 		return bmi160_trigger_drdy_set(dev, trig->chan, trig, handler);
240 	}
241 
242 	return -ENOTSUP;
243 }
244 #endif
245 
bmi160_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)246 int bmi160_trigger_set(const struct device *dev,
247 		       const struct sensor_trigger *trig,
248 		       sensor_trigger_handler_t handler)
249 {
250 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
251 	if (trig->chan == SENSOR_CHAN_ACCEL_XYZ) {
252 		return bmi160_trigger_set_acc(dev, trig, handler);
253 	}
254 #endif
255 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
256 	if (trig->chan == SENSOR_CHAN_GYRO_XYZ) {
257 		return bmi160_trigger_set_gyr(dev, trig, handler);
258 	}
259 #endif
260 	return -ENOTSUP;
261 }
262 
bmi160_trigger_mode_init(const struct device * dev)263 int bmi160_trigger_mode_init(const struct device *dev)
264 {
265 	struct bmi160_data *data = dev->data;
266 	const struct bmi160_cfg *cfg = dev->config;
267 	int ret;
268 
269 	if (!gpio_is_ready_dt(&cfg->interrupt)) {
270 		LOG_DBG("GPIO port %s not ready", cfg->interrupt.port->name);
271 		return -EINVAL;
272 	}
273 
274 	data->dev = dev;
275 
276 #if defined(CONFIG_BMI160_TRIGGER_OWN_THREAD)
277 	k_sem_init(&data->sem, 0, K_SEM_MAX_LIMIT);
278 
279 	k_thread_create(&bmi160_thread, bmi160_thread_stack,
280 			CONFIG_BMI160_THREAD_STACK_SIZE,
281 			bmi160_thread_main,
282 			data, NULL, NULL,
283 			K_PRIO_COOP(CONFIG_BMI160_THREAD_PRIORITY),
284 			0, K_NO_WAIT);
285 #elif defined(CONFIG_BMI160_TRIGGER_GLOBAL_THREAD)
286 	data->work.handler = bmi160_work_handler;
287 #endif
288 
289 	/* map all interrupts to INT1 pin */
290 	if (bmi160_word_write(dev, BMI160_REG_INT_MAP0, 0xf0ff) < 0) {
291 		LOG_DBG("Failed to map interrupts.");
292 		return -EIO;
293 	}
294 
295 	ret = gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
296 	if (ret < 0) {
297 		return ret;
298 	}
299 
300 	gpio_init_callback(&data->gpio_cb,
301 			   bmi160_gpio_callback,
302 			   BIT(cfg->interrupt.pin));
303 
304 	ret = gpio_add_callback(cfg->interrupt.port, &data->gpio_cb);
305 	if (ret < 0) {
306 		return ret;
307 	}
308 
309 	ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
310 					      GPIO_INT_EDGE_TO_ACTIVE);
311 	if (ret < 0) {
312 		return ret;
313 	}
314 
315 	return bmi160_byte_write(dev, BMI160_REG_INT_OUT_CTRL,
316 				 BMI160_INT1_OUT_EN | BMI160_INT1_EDGE_CTRL);
317 }
318